photos-utils.c 37.1 KB
Newer Older
1 2
/*
 * Photos - access, organize and share your photos on GNOME
3
 * Copyright © 2012 – 2020 Red Hat, Inc.
Debarshi Ray's avatar
Debarshi Ray committed
4
 * Copyright © 2009 Yorba Foundation
5
 *
6 7 8 9
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
10 11 12 13 14 15 16
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
17
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 19
 */

20 21 22
/* Based on code from:
 *   + Documents
 *   + Eye of GNOME
Debarshi Ray's avatar
Debarshi Ray committed
23
 *   + Shotwell
24 25
 */

26 27 28

#include "config.h"

29 30
#include <math.h>

31
#include <gdk/gdk.h>
32
#include <glib.h>
Debarshi Ray's avatar
Debarshi Ray committed
33
#include <tracker-sparql.h>
34
#include <libgd/gd.h>
35

36
#include "photos-application.h"
Debarshi Ray's avatar
Debarshi Ray committed
37
#include "photos-device-item.h"
38
#include "photos-enums.h"
39
#include "photos-error.h"
40 41
#include "photos-facebook-item.h"
#include "photos-flickr-item.h"
42
#include "photos-gegl.h"
Saurav Agarwalla's avatar
Saurav Agarwalla committed
43
#include "photos-google-item.h"
44
#include "photos-local-item.h"
Pranav Kant's avatar
Pranav Kant committed
45
#include "photos-media-server-item.h"
46
#include "photos-offset-collection-view-controller.h"
47 48
#include "photos-offset-collections-controller.h"
#include "photos-offset-favorites-controller.h"
49
#include "photos-offset-import-controller.h"
50 51
#include "photos-offset-overview-controller.h"
#include "photos-offset-search-controller.h"
52
#include "photos-query.h"
53
#include "photos-share-point.h"
Debarshi Ray's avatar
Debarshi Ray committed
54
#include "photos-share-point-email.h"
Umang Jain's avatar
Umang Jain committed
55
#include "photos-share-point-google.h"
56
#include "photos-share-point-online.h"
57
#include "photos-source.h"
58
#include "photos-thumbnail-factory.h"
59
#include "photos-tool.h"
Debarshi Ray's avatar
Debarshi Ray committed
60
#include "photos-tool-colors.h"
Debarshi Ray's avatar
Debarshi Ray committed
61
#include "photos-tool-crop.h"
Debarshi Ray's avatar
Debarshi Ray committed
62
#include "photos-tool-enhance.h"
Debarshi Ray's avatar
Debarshi Ray committed
63
#include "photos-tool-filters.h"
64
#include "photos-tracker-collection-view-controller.h"
65 66
#include "photos-tracker-collections-controller.h"
#include "photos-tracker-favorites-controller.h"
67
#include "photos-tracker-import-controller.h"
68
#include "photos-tracker-overview-controller.h"
Debarshi Ray's avatar
Debarshi Ray committed
69
#include "photos-tracker-queue.h"
70
#include "photos-tracker-search-controller.h"
71 72 73
#include "photos-utils.h"


74 75 76 77 78
GdkPixbuf *
photos_utils_center_pixbuf (GdkPixbuf *pixbuf, gint size)
{
  GdkPixbuf *ret_val;
  gint height;
79
  gint pixbuf_size;
80 81
  gint width;

82 83
  height = gdk_pixbuf_get_height (pixbuf);
  width = gdk_pixbuf_get_width (pixbuf);
84 85 86 87 88 89 90 91 92 93

  pixbuf_size = MAX (height, width);
  if (pixbuf_size >= size)
    {
      ret_val = g_object_ref (pixbuf);
      goto out;
    }

  ret_val = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, size, size);
  gdk_pixbuf_fill (ret_val, 0x00000000);
94 95
  gdk_pixbuf_copy_area (pixbuf, 0, 0, width, height, ret_val, (size - width) / 2, (size - height) / 2);

96
 out:
97 98 99 100
  return ret_val;
}


101 102 103
gchar *
photos_utils_convert_path_to_uri (const gchar *path)
{
Umang Jain's avatar
Umang Jain committed
104
  g_autoptr (GFile) file = NULL;
105
  gchar *uri = NULL;
106 107

  if (path == NULL)
108 109 110 111
    {
      uri = g_strdup ("");
      goto out;
    }
112 113 114 115

  file = g_file_new_for_path (path);
  uri = g_file_get_uri (file);

116 117
 out:
  g_return_val_if_fail (uri != NULL, NULL);
118 119 120 121
  return uri;
}


122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
GStrv
photos_utils_convert_paths_to_uris (const gchar *const *paths)
{
  GStrv uris = NULL;
  guint i;
  guint n_paths;

  if (paths == NULL)
    goto out;

  n_paths = g_strv_length ((GStrv) paths);
  uris = (GStrv) g_malloc0_n (n_paths + 1, sizeof (gchar *));

  for (i = 0; paths[i] != NULL; i++)
    {
      g_autofree gchar *uri = NULL;

      uri = photos_utils_convert_path_to_uri (paths[i]);
      uris[i] = g_steal_pointer (&uri);
    }

 out:
  return uris;
}


Debarshi Ray's avatar
Debarshi Ray committed
148 149 150
GIcon *
photos_utils_create_collection_icon (gint base_size, GList *pixbufs)
{
Umang Jain's avatar
Umang Jain committed
151 152
  cairo_surface_t *surface; /* TODO: use g_autoptr */
  cairo_t *cr; /* TODO: use g_autoptr */
Debarshi Ray's avatar
Debarshi Ray committed
153 154 155
  GdkPixbuf *pix;
  GIcon *ret_val;
  GList *l;
Umang Jain's avatar
Umang Jain committed
156 157
  g_autoptr (GtkStyleContext) context = NULL;
  g_autoptr (GtkWidgetPath) path = NULL;
Debarshi Ray's avatar
Debarshi Ray committed
158 159 160 161 162 163 164
  gint cur_x;
  gint cur_y;
  gint padding;
  gint pix_height;
  gint pix_width;
  gint scale_size;
  gint tile_size;
165 166 167 168
  guint idx;
  guint n_grid;
  guint n_pixbufs;
  guint n_tiles;
Debarshi Ray's avatar
Debarshi Ray committed
169

170 171 172 173 174 175 176 177 178 179 180
  n_pixbufs = g_list_length (pixbufs);
  if (n_pixbufs < 3)
    {
      n_grid = 1;
      n_tiles = 1;
    }
  else
    {
      n_grid = 2;
      n_tiles = 4;
    }
Debarshi Ray's avatar
Debarshi Ray committed
181

182
  padding = MAX (base_size / 10, 4);
183
  tile_size = (base_size - ((n_grid + 1) * padding)) / n_grid;
Debarshi Ray's avatar
Debarshi Ray committed
184 185

  context = gtk_style_context_new ();
186
  gtk_style_context_add_class (context, "photos-collection-icon");
Debarshi Ray's avatar
Debarshi Ray committed
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201

  path = gtk_widget_path_new ();
  gtk_widget_path_append_type (path, GTK_TYPE_ICON_VIEW);
  gtk_style_context_set_path (context, path);

  surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, base_size, base_size);
  cr = cairo_create (surface);

  gtk_render_background (context, cr, 0, 0, base_size, base_size);

  l = pixbufs;
  idx = 0;
  cur_x = padding;
  cur_y = padding;

202
  while (l != NULL && idx < n_tiles)
Debarshi Ray's avatar
Debarshi Ray committed
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
    {
      pix = l->data;
      pix_width = gdk_pixbuf_get_width (pix);
      pix_height = gdk_pixbuf_get_height (pix);

      scale_size = MIN (pix_width, pix_height);

      cairo_save (cr);

      cairo_translate (cr, cur_x, cur_y);

      cairo_rectangle (cr, 0, 0,
                       tile_size, tile_size);
      cairo_clip (cr);

      cairo_scale (cr, (gdouble) tile_size / (gdouble) scale_size, (gdouble) tile_size / (gdouble) scale_size);
      gdk_cairo_set_source_pixbuf (cr, pix, 0, 0);

      cairo_paint (cr);
      cairo_restore (cr);

224 225 226 227
      idx++;
      l = l->next;

      if ((idx % n_grid) == 0)
Debarshi Ray's avatar
Debarshi Ray committed
228
        {
229 230
          cur_x = padding;
          cur_y += tile_size + padding;
Debarshi Ray's avatar
Debarshi Ray committed
231 232 233
        }
      else
        {
234
          cur_x += tile_size + padding;
Debarshi Ray's avatar
Debarshi Ray committed
235 236 237 238 239 240 241 242 243 244 245 246
        }
    }

  ret_val = G_ICON (gdk_pixbuf_get_from_surface (surface, 0, 0, base_size, base_size));

  cairo_surface_destroy (surface);
  cairo_destroy (cr);

  return ret_val;
}


247 248 249 250
GdkPixbuf *
photos_utils_create_placeholder_icon_for_scale (const gchar *name, gint size, gint scale)
{
  GApplication *app;
Umang Jain's avatar
Umang Jain committed
251 252
  g_autoptr (GdkPixbuf) centered_pixbuf = NULL;
  g_autoptr (GdkPixbuf) pixbuf = NULL;
253
  GdkPixbuf *ret_val = NULL;
Umang Jain's avatar
Umang Jain committed
254
  g_autoptr (GIcon) icon = NULL;
255
  GList *windows;
Umang Jain's avatar
Umang Jain committed
256
  g_autoptr (GtkIconInfo) info = NULL;
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
  GtkIconTheme *theme;
  GtkStyleContext *context;
  gint size_scaled;

  app = g_application_get_default ();
  windows = gtk_application_get_windows (GTK_APPLICATION (app));
  if (windows == NULL)
    goto out;

  icon = g_themed_icon_new (name);
  theme = gtk_icon_theme_get_default ();
  info = gtk_icon_theme_lookup_by_gicon_for_scale (theme,
                                                   icon,
                                                   16,
                                                   scale,
                                                   GTK_ICON_LOOKUP_FORCE_SIZE | GTK_ICON_LOOKUP_FORCE_SYMBOLIC);
  if (info == NULL)
    goto out;

  context = gtk_widget_get_style_context (GTK_WIDGET (windows->data));

Umang Jain's avatar
Umang Jain committed
278 279 280 281 282 283 284 285 286 287
  {
    g_autoptr (GError) error = NULL;

    pixbuf = gtk_icon_info_load_symbolic_for_context (info, context, NULL, &error);
    if (error != NULL)
      {
        g_warning ("Unable to load icon '%s': %s", name, error->message);
        goto out;
      }
  }
288 289 290 291 292 293 294 295 296 297 298 299

  size_scaled = size * scale;
  centered_pixbuf = photos_utils_center_pixbuf (pixbuf, size_scaled);

  ret_val = centered_pixbuf;
  centered_pixbuf = NULL;

 out:
  return ret_val;
}


300
GIcon *
301
photos_utils_create_symbolic_icon_for_scale (const gchar *name, gint base_size, gint scale)
302
{
Umang Jain's avatar
Umang Jain committed
303
  g_autoptr (GIcon) icon = NULL;
304
  GIcon *ret_val = NULL;
Umang Jain's avatar
Umang Jain committed
305 306
  g_autoptr (GdkPixbuf) pixbuf = NULL;
  g_autoptr (GtkIconInfo) info = NULL;
307
  GtkIconTheme *theme;
Umang Jain's avatar
Umang Jain committed
308 309 310 311 312 313
  g_autoptr (GtkStyleContext) style = NULL;
  g_autoptr (GtkWidgetPath) path = NULL;
  cairo_surface_t *icon_surface = NULL; /* TODO: use g_autoptr */
  cairo_surface_t *surface; /* TODO: use g_autoptr */
  cairo_t *cr; /* TODO: use g_autoptr */
  g_autofree gchar *symbolic_name = NULL;
314
  const gint bg_size = 24;
315 316
  const gint emblem_margin = 4;
  gint emblem_pos;
317 318
  gint emblem_size;
  gint total_size;
319
  gint total_size_scaled;
320 321

  total_size = base_size / 2;
322
  total_size_scaled = total_size * scale;
323
  emblem_size = bg_size - emblem_margin * 2;
324

325 326
  surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, total_size_scaled, total_size_scaled);
  cairo_surface_set_device_scale (surface, (gdouble) scale, (gdouble) scale);
327 328 329 330 331 332 333 334
  cr = cairo_create (surface);

  style = gtk_style_context_new ();

  path = gtk_widget_path_new ();
  gtk_widget_path_append_type (path, GTK_TYPE_ICON_VIEW);
  gtk_style_context_set_path (style, path);

335
  gtk_style_context_add_class (style, "photos-icon-bg");
336

337
  gtk_render_background (style, cr, total_size - bg_size, total_size - bg_size, bg_size, bg_size);
338 339 340 341 342

  symbolic_name = g_strconcat (name, "-symbolic", NULL);
  icon = g_themed_icon_new_with_default_fallbacks (symbolic_name);

  theme = gtk_icon_theme_get_default();
343
  info = gtk_icon_theme_lookup_by_gicon_for_scale (theme, icon, emblem_size, scale, GTK_ICON_LOOKUP_FORCE_SIZE);
344 345 346 347 348 349 350
  if (info == NULL)
    goto out;

  pixbuf = gtk_icon_info_load_symbolic_for_context (info, style, NULL, NULL);
  if (pixbuf == NULL)
    goto out;

351
  icon_surface = gdk_cairo_surface_create_from_pixbuf (pixbuf, scale, NULL);
352

353 354 355 356
  emblem_pos = total_size - emblem_size - emblem_margin;
  gtk_render_icon_surface (style, cr, icon_surface, emblem_pos, emblem_pos);

  ret_val = G_ICON (gdk_pixbuf_get_from_surface (surface, 0, 0, total_size_scaled, total_size_scaled));
357 358

 out:
359
  cairo_surface_destroy (icon_surface);
360 361 362 363 364 365 366
  cairo_surface_destroy (surface);
  cairo_destroy (cr);

  return ret_val;
}


367
gboolean
368 369 370
photos_utils_create_thumbnail (GFile *file,
                               const gchar *mime_type,
                               gint64 mtime,
371 372 373
                               GQuark orientation,
                               gint64 original_height,
                               gint64 original_width,
Debarshi Ray's avatar
Debarshi Ray committed
374
                               const gchar *const *pipeline_uris,
375
                               const gchar *thumbnail_path,
376 377
                               GCancellable *cancellable,
                               GError **error)
Debarshi Ray's avatar
Debarshi Ray committed
378
{
Umang Jain's avatar
Umang Jain committed
379
  g_autoptr (PhotosThumbnailFactory) factory = NULL;
380
  gboolean ret_val = FALSE;
Debarshi Ray's avatar
Debarshi Ray committed
381

382 383 384 385 386 387 388
  factory = photos_thumbnail_factory_dup_singleton (NULL, NULL);
  if (!photos_thumbnail_factory_generate_thumbnail (factory,
                                                    file,
                                                    mime_type,
                                                    orientation,
                                                    original_height,
                                                    original_width,
Debarshi Ray's avatar
Debarshi Ray committed
389
                                                    pipeline_uris,
390
                                                    thumbnail_path,
391 392 393
                                                    cancellable,
                                                    error))
    goto out;
Debarshi Ray's avatar
Debarshi Ray committed
394

395
  ret_val = TRUE;
Debarshi Ray's avatar
Debarshi Ray committed
396 397

 out:
398
  return ret_val;
Debarshi Ray's avatar
Debarshi Ray committed
399 400 401
}


402 403 404
GVariant *
photos_utils_create_zoom_target_value (gdouble delta, PhotosZoomEvent event)
{
Umang Jain's avatar
Umang Jain committed
405
  GEnumClass *zoom_event_class = NULL; /* TODO: use g_autoptr */
406 407 408 409
  GEnumValue *event_value;
  GVariant *delta_value;
  GVariant *event_nick_value;
  GVariant *ret_val = NULL;
410
  g_auto (GVariantBuilder) builder = G_VARIANT_BUILDER_INIT (G_VARIANT_TYPE_VARDICT);
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
  const gchar *event_nick = "none";

  g_return_val_if_fail (delta >= 0.0, NULL);
  g_return_val_if_fail (event != PHOTOS_ZOOM_EVENT_NONE, NULL);

  delta_value = g_variant_new_double (delta);
  g_variant_builder_add (&builder, "{sv}", "delta", delta_value);

  zoom_event_class = G_ENUM_CLASS (g_type_class_ref (PHOTOS_TYPE_ZOOM_EVENT));

  event_value = g_enum_get_value (zoom_event_class, (gint) event);
  if (event_value != NULL)
    event_nick = event_value->value_nick;

  event_nick_value = g_variant_new_string (event_nick);
  g_variant_builder_add (&builder, "{sv}", "event", event_nick_value);

  ret_val = g_variant_builder_end (&builder);

  g_type_class_unref (zoom_event_class);
431
  g_return_val_if_fail (g_variant_is_floating (ret_val), ret_val);
432 433 434 435
  return ret_val;
}


436
static GIcon *
Debarshi Ray's avatar
Debarshi Ray committed
437
photos_utils_get_thumbnail_icon (PhotosBaseItem *item)
438
{
Umang Jain's avatar
Umang Jain committed
439 440
  g_autoptr (GFile) thumb_file = NULL;
  g_autoptr (GFileInfo) info = NULL;
441 442
  GIcon *icon = NULL;
  const gchar *thumb_path;
Debarshi Ray's avatar
Debarshi Ray committed
443 444 445 446 447
  const gchar *uri;

  uri = photos_base_item_get_uri (item);
  if (uri == NULL || uri[0] == '\0')
    goto out;
448

Umang Jain's avatar
Umang Jain committed
449 450 451
  {
    g_autoptr (GError) error = NULL;

452 453 454 455 456
    info = photos_base_item_query_info (item,
                                        G_FILE_ATTRIBUTE_THUMBNAIL_PATH,
                                        G_FILE_QUERY_INFO_NONE,
                                        NULL,
                                        &error);
Umang Jain's avatar
Umang Jain committed
457 458 459 460 461 462
    if (error != NULL)
      {
        g_warning ("Unable to fetch thumbnail path for %s: %s", uri, error->message);
        goto out;
      }
  }
463 464

  thumb_path = g_file_info_get_attribute_byte_string (info, G_FILE_ATTRIBUTE_THUMBNAIL_PATH);
465 466 467
  if (thumb_path == NULL)
    goto out;

468 469 470 471 472 473 474 475 476
  thumb_file = g_file_new_for_path (thumb_path);
  icon = g_file_icon_new (thumb_file);

 out:
  return icon;
}


GIcon *
477
photos_utils_get_icon_from_item (PhotosBaseItem *item)
478 479 480 481 482 483
{
  GIcon *icon = NULL;
  gboolean is_remote = FALSE;
  const gchar *identifier;
  const gchar *mime_type;

484
  identifier = photos_base_item_get_identifier (item);
485 486
  if (identifier != NULL)
    {
Saurav Agarwalla's avatar
Saurav Agarwalla committed
487 488 489
      if (g_str_has_prefix (identifier, "facebook:") ||
          g_str_has_prefix (identifier, "flickr:") ||
          g_str_has_prefix (identifier, "google:"))
490 491 492 493
        is_remote = TRUE;
    }

  if (!is_remote)
Debarshi Ray's avatar
Debarshi Ray committed
494
    icon = photos_utils_get_thumbnail_icon (item);
495 496 497 498

  if (icon != NULL)
    goto out;

499
  mime_type = photos_base_item_get_mime_type (item);
500 501 502 503 504 505
  if (mime_type != NULL)
    icon = g_content_type_get_icon (mime_type);

  if (icon != NULL)
    goto out;

506 507 508 509 510 511 512
  if (photos_base_item_is_collection (item))
    {
      gint size;

      size = photos_utils_get_icon_size ();
      icon = photos_utils_create_collection_icon (size, NULL);
    }
513 514 515 516 517 518 519 520 521 522 523

  if (icon != NULL)
    goto out;

  icon = g_themed_icon_new ("image-x-generic");

 out:
  return icon;
}


524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
gdouble
photos_utils_get_zoom_delta (GVariant *dictionary)
{
  gdouble delta;
  gdouble ret_val = -1.0;

  g_return_val_if_fail (dictionary != NULL, -1.0);
  g_return_val_if_fail (g_variant_is_of_type (dictionary, G_VARIANT_TYPE_VARDICT), -1.0);

  if (!g_variant_lookup (dictionary, "delta", "d", &delta))
    goto out;

  ret_val = delta;

 out:
  g_return_val_if_fail (ret_val >= 0.0, -1.0);
  return ret_val;
}


PhotosZoomEvent
photos_utils_get_zoom_event (GVariant *dictionary)
{
  GEnumClass *zoom_event_class = NULL;
  GEnumValue *event_value;
  PhotosZoomEvent ret_val = PHOTOS_ZOOM_EVENT_NONE;
  const gchar *event_str;

  g_return_val_if_fail (dictionary != NULL, PHOTOS_ZOOM_EVENT_NONE);
  g_return_val_if_fail (g_variant_is_of_type (dictionary, G_VARIANT_TYPE_VARDICT), PHOTOS_ZOOM_EVENT_NONE);

  if (!g_variant_lookup (dictionary, "event", "&s", &event_str))
    goto out;

  zoom_event_class = G_ENUM_CLASS (g_type_class_ref (PHOTOS_TYPE_ZOOM_EVENT));

  event_value = g_enum_get_value_by_nick (zoom_event_class, event_str);
  if (event_value == NULL)
    event_value = g_enum_get_value_by_name (zoom_event_class, event_str);
  if (event_value == NULL)
    goto out;

  ret_val = (PhotosZoomEvent) event_value->value;

 out:
  g_clear_pointer (&zoom_event_class, g_type_class_unref);
  g_return_val_if_fail (ret_val != PHOTOS_ZOOM_EVENT_NONE, PHOTOS_ZOOM_EVENT_NONE);
  return ret_val;
}


575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590
GdkPixbuf *
photos_utils_downscale_pixbuf_for_scale (GdkPixbuf *pixbuf, gint size, gint scale)
{
  GdkPixbuf *ret_val;
  gint height;
  gint pixbuf_size;
  gint scaled_size;
  gint width;

  height = gdk_pixbuf_get_height (pixbuf);
  width = gdk_pixbuf_get_width (pixbuf);
  pixbuf_size = MAX (height, width);

  scaled_size = size * scale;

  /* On Hi-Dpi displays, a pixbuf should never appear smaller than on
591 592 593 594 595 596 597 598 599
   * Lo-Dpi.
   *
   * Sometimes, a pixbuf can be slightly smaller than size. eg.,
   * server-generated thumbnails for remote tems. Scaling them up
   * won't cause any discernible loss of quality and will make our
   * letterboxed grid look nicer. 75% of 'scale' has been chosen as
   * the arbitrary definition of 'slightly smaller'.
   *
   * Therefore, if a pixbuf lies between (3 * size / 4, size * scale)
600 601 602 603 604 605 606 607 608
   * we scale it up to size * scale, so that it doesn't look smaller.
   * Similarly, if a pixbuf is smaller than size, then we increase its
   * dimensions by the scale factor.
   */

  if (pixbuf_size == scaled_size)
    {
      ret_val = g_object_ref (pixbuf);
    }
609
  else if (pixbuf_size > 3 * size / 4)
610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
    {
      if (height == width)
        {
          height = scaled_size;
          width = scaled_size;
        }
      else if (height > width)
        {
          width = (gint) (0.5 + (gdouble) (width * scaled_size) / (gdouble) height);
          height = scaled_size;
        }
      else
        {
          height = (gint) (0.5 + (gdouble) (height * scaled_size) / (gdouble) width);
          width = scaled_size;
        }

      height = MAX (height, 1);
      width = MAX (width, 1);
      ret_val = gdk_pixbuf_scale_simple (pixbuf, width, height, GDK_INTERP_BILINEAR);
    }
  else /* pixbuf_size <= size */
    {
      if (scale == 1)
        {
          ret_val = g_object_ref (pixbuf);
        }
      else
        {
          height *= scale;
          width *= scale;

          height = MAX (height, 1);
          width = MAX (width, 1);
          ret_val = gdk_pixbuf_scale_simple (pixbuf, width, height, GDK_INTERP_BILINEAR);
        }
    }

  return ret_val;
}


Debarshi Ray's avatar
Debarshi Ray committed
652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710
void
photos_utils_draw_rectangle_handles (cairo_t *cr,
                                     gdouble x,
                                     gdouble y,
                                     gdouble width,
                                     gdouble height,
                                     gdouble offset,
                                     gdouble radius)
{
  cairo_save (cr);

  cairo_new_sub_path (cr);
  cairo_arc (cr, x - offset, y - offset, radius, 0.0, 2.0 * M_PI);
  cairo_fill (cr);

  cairo_new_sub_path (cr);
  cairo_arc (cr, x + width + offset, y - offset, radius, 0.0, 2.0 * M_PI);
  cairo_fill (cr);

  cairo_new_sub_path (cr);
  cairo_arc (cr, x + width + offset, y + height + offset, radius, 0.0, 2.0 * M_PI);
  cairo_fill (cr);

  cairo_new_sub_path (cr);
  cairo_arc (cr, x - offset, y + height + offset, radius, 0.0, 2.0 * M_PI);
  cairo_fill (cr);

  cairo_restore (cr);
}


void
photos_utils_draw_rectangle_thirds (cairo_t *cr, gdouble x, gdouble y, gdouble width, gdouble height)
{
  const gdouble one_third_x = width / 3.0;
  const gdouble one_third_y = height / 3.0;

  cairo_save (cr);

  cairo_move_to (cr, x + one_third_x, y);
  cairo_line_to (cr, x + one_third_x, y + height);
  cairo_stroke (cr);

  cairo_move_to (cr, x + 2.0 * one_third_x, y);
  cairo_line_to (cr, x + 2.0 * one_third_x, y + height);
  cairo_stroke (cr);

  cairo_move_to (cr, x, y + one_third_y);
  cairo_line_to (cr, x + width, y + one_third_y);
  cairo_stroke (cr);

  cairo_move_to (cr, x, y + 2.0 * one_third_y);
  cairo_line_to (cr, x + width, y + 2.0 * one_third_y);
  cairo_stroke (cr);

  cairo_restore (cr);
}


711 712 713 714 715 716
void
photos_utils_ensure_builtins (void)
{
  static gsize once_init_value = 0;

  photos_utils_ensure_extension_points ();
717
  photos_gegl_ensure_builtins ();
718 719 720

  if (g_once_init_enter (&once_init_value))
    {
Debarshi Ray's avatar
Debarshi Ray committed
721
      g_type_ensure (PHOTOS_TYPE_DEVICE_ITEM);
722 723
      g_type_ensure (PHOTOS_TYPE_FACEBOOK_ITEM);
      g_type_ensure (PHOTOS_TYPE_FLICKR_ITEM);
Saurav Agarwalla's avatar
Saurav Agarwalla committed
724
      g_type_ensure (PHOTOS_TYPE_GOOGLE_ITEM);
725
      g_type_ensure (PHOTOS_TYPE_LOCAL_ITEM);
Pranav Kant's avatar
Pranav Kant committed
726
      g_type_ensure (PHOTOS_TYPE_MEDIA_SERVER_ITEM);
727

Debarshi Ray's avatar
Debarshi Ray committed
728
      g_type_ensure (PHOTOS_TYPE_SHARE_POINT_EMAIL);
Umang Jain's avatar
Umang Jain committed
729 730
      g_type_ensure (PHOTOS_TYPE_SHARE_POINT_GOOGLE);

Debarshi Ray's avatar
Debarshi Ray committed
731
      g_type_ensure (PHOTOS_TYPE_TOOL_COLORS);
Debarshi Ray's avatar
Debarshi Ray committed
732
      g_type_ensure (PHOTOS_TYPE_TOOL_CROP);
Debarshi Ray's avatar
Debarshi Ray committed
733
      g_type_ensure (PHOTOS_TYPE_TOOL_ENHANCE);
Debarshi Ray's avatar
Debarshi Ray committed
734
      g_type_ensure (PHOTOS_TYPE_TOOL_FILTERS);
Debarshi Ray's avatar
Debarshi Ray committed
735

736
      g_type_ensure (PHOTOS_TYPE_TRACKER_COLLECTION_VIEW_CONTROLLER);
737 738
      g_type_ensure (PHOTOS_TYPE_TRACKER_COLLECTIONS_CONTROLLER);
      g_type_ensure (PHOTOS_TYPE_TRACKER_FAVORITES_CONTROLLER);
739
      g_type_ensure (PHOTOS_TYPE_TRACKER_IMPORT_CONTROLLER);
740 741 742
      g_type_ensure (PHOTOS_TYPE_TRACKER_OVERVIEW_CONTROLLER);
      g_type_ensure (PHOTOS_TYPE_TRACKER_SEARCH_CONTROLLER);

743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759
      g_once_init_leave (&once_init_value, 1);
    }
}


void
photos_utils_ensure_extension_points (void)
{
  static gsize once_init_value = 0;

  if (g_once_init_enter (&once_init_value))
    {
      GIOExtensionPoint *extension_point;

      extension_point = g_io_extension_point_register (PHOTOS_BASE_ITEM_EXTENSION_POINT_NAME);
      g_io_extension_point_set_required_type (extension_point, PHOTOS_TYPE_BASE_ITEM);

760 761 762
      extension_point = g_io_extension_point_register (PHOTOS_SHARE_POINT_EXTENSION_POINT_NAME);
      g_io_extension_point_set_required_type (extension_point, PHOTOS_TYPE_SHARE_POINT);

763 764 765
      extension_point = g_io_extension_point_register (PHOTOS_SHARE_POINT_ONLINE_EXTENSION_POINT_NAME);
      g_io_extension_point_set_required_type (extension_point, PHOTOS_TYPE_SHARE_POINT_ONLINE);

766 767 768
      extension_point = g_io_extension_point_register (PHOTOS_TOOL_EXTENSION_POINT_NAME);
      g_io_extension_point_set_required_type (extension_point, PHOTOS_TYPE_TOOL);

769 770 771
      extension_point = g_io_extension_point_register (PHOTOS_TRACKER_CONTROLLER_EXTENSION_POINT_NAME);
      g_io_extension_point_set_required_type (extension_point, PHOTOS_TYPE_TRACKER_CONTROLLER);

772 773 774 775 776
      g_once_init_leave (&once_init_value, 1);
    }
}


Debarshi Ray's avatar
Debarshi Ray committed
777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795
gdouble
photos_utils_eval_radial_line (gdouble crop_center_x,
                               gdouble crop_center_y,
                               gdouble corner_x,
                               gdouble corner_y,
                               gdouble event_x)
{
  gdouble decision_intercept;
  gdouble decision_slope;
  gdouble projected_y;

  decision_slope = (corner_y - crop_center_y) / (corner_x - crop_center_x);
  decision_intercept = corner_y - (decision_slope * corner_x);
  projected_y = decision_slope * event_x + decision_intercept;

  return projected_y;
}


796 797 798 799 800 801 802 803 804
gboolean
photos_utils_file_copy_as_thumbnail (GFile *source,
                                     GFile *destination,
                                     const gchar *original_uri,
                                     gint64 original_height,
                                     gint64 original_width,
                                     GCancellable *cancellable,
                                     GError **error)
{
Umang Jain's avatar
Umang Jain committed
805 806 807
  g_autoptr (GFileInputStream) istream = NULL;
  g_autoptr (GFileOutputStream) ostream = NULL;
  g_autoptr (GdkPixbuf) pixbuf = NULL;
808 809
  gboolean ret_val = FALSE;
  const gchar *prgname;
Umang Jain's avatar
Umang Jain committed
810 811
  g_autofree gchar *original_height_str = NULL;
  g_autofree gchar *original_width_str = NULL;
812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859

  g_return_val_if_fail (G_IS_FILE (source), FALSE);
  g_return_val_if_fail (G_IS_FILE (destination), FALSE);
  g_return_val_if_fail (original_uri != NULL && original_uri[0] != '\0', FALSE);
  g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), FALSE);
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

  istream = g_file_read (source, cancellable, error);
  if (istream == NULL)
    goto out;

  pixbuf = gdk_pixbuf_new_from_stream (G_INPUT_STREAM (istream), cancellable, error);
  if (pixbuf == NULL)
    goto out;

  ostream = g_file_replace (destination,
                            NULL,
                            FALSE,
                            G_FILE_CREATE_PRIVATE | G_FILE_CREATE_REPLACE_DESTINATION,
                            cancellable,
                            error);
  if (ostream == NULL)
    goto out;

  original_height_str = g_strdup_printf ("%" G_GINT64_FORMAT, original_height);
  original_width_str = g_strdup_printf ("%" G_GINT64_FORMAT, original_width);
  prgname = g_get_prgname ();
  if (!gdk_pixbuf_save_to_stream (pixbuf,
                                  G_OUTPUT_STREAM (ostream),
                                  "png",
                                  cancellable,
                                  error,
                                  "tEXt::Software", prgname,
                                  "tEXt::Thumb::URI", original_uri,
                                  "tEXt::Thumb::Image::Height", original_height_str,
                                  "tEXt::Thumb::Image::Width", original_width_str,
                                  NULL))
    {
      goto out;
    }

  ret_val = TRUE;

 out:
  return ret_val;
}


860 861 862 863 864
void
photos_utils_get_controller (PhotosWindowMode mode,
                             PhotosOffsetController **out_offset_cntrlr,
                             PhotosTrackerController **out_trk_cntrlr)
{
Umang Jain's avatar
Umang Jain committed
865 866
  g_autoptr (PhotosOffsetController) offset_cntrlr = NULL;
  g_autoptr (PhotosTrackerController) trk_cntrlr = NULL;
867 868 869 870 871 872 873

  g_return_if_fail (mode != PHOTOS_WINDOW_MODE_NONE);
  g_return_if_fail (mode != PHOTOS_WINDOW_MODE_EDIT);
  g_return_if_fail (mode != PHOTOS_WINDOW_MODE_PREVIEW);

  switch (mode)
    {
874 875 876 877 878
    case PHOTOS_WINDOW_MODE_COLLECTION_VIEW:
      offset_cntrlr = photos_offset_collection_view_controller_dup_singleton ();
      trk_cntrlr = photos_tracker_collection_view_controller_dup_singleton ();
      break;

879 880 881 882 883 884 885 886 887 888
    case PHOTOS_WINDOW_MODE_COLLECTIONS:
      offset_cntrlr = photos_offset_collections_controller_dup_singleton ();
      trk_cntrlr = photos_tracker_collections_controller_dup_singleton ();
      break;

    case PHOTOS_WINDOW_MODE_FAVORITES:
      offset_cntrlr = photos_offset_favorites_controller_dup_singleton ();
      trk_cntrlr = photos_tracker_favorites_controller_dup_singleton ();
      break;

889 890 891 892 893
    case PHOTOS_WINDOW_MODE_IMPORT:
      offset_cntrlr = photos_offset_import_controller_dup_singleton ();
      trk_cntrlr = photos_tracker_import_controller_dup_singleton ();
      break;

894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919
    case PHOTOS_WINDOW_MODE_OVERVIEW:
      offset_cntrlr = photos_offset_overview_controller_dup_singleton ();
      trk_cntrlr = photos_tracker_overview_controller_dup_singleton ();
      break;

    case PHOTOS_WINDOW_MODE_SEARCH:
      offset_cntrlr = photos_offset_search_controller_dup_singleton ();
      trk_cntrlr = photos_tracker_search_controller_dup_singleton ();
      break;

    case PHOTOS_WINDOW_MODE_NONE:
    case PHOTOS_WINDOW_MODE_EDIT:
    case PHOTOS_WINDOW_MODE_PREVIEW:
    default:
      g_assert_not_reached ();
      break;
    }

  if (out_offset_cntrlr != NULL)
    g_set_object (out_offset_cntrlr, offset_cntrlr);

  if (out_trk_cntrlr != NULL)
    g_set_object (out_trk_cntrlr, trk_cntrlr);
}


920 921 922
gchar *
photos_utils_get_extension_from_mime_type (const gchar *mime_type)
{
Umang Jain's avatar
Umang Jain committed
923
  g_autoptr (GSList) formats = NULL;
924 925 926 927 928 929 930 931
  GSList *l;
  gchar *ret_val = NULL;

  formats = gdk_pixbuf_get_formats ();

  for (l = formats; l != NULL; l = l->next)
    {
      GdkPixbufFormat *format = (GdkPixbufFormat*) l->data;
Umang Jain's avatar
Umang Jain committed
932
      g_auto (GStrv) supported_mime_types = NULL;
933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952
      guint i;

      supported_mime_types = gdk_pixbuf_format_get_mime_types (format);
      for (i = 0; supported_mime_types[i] != NULL; i++)
        {
          if (g_strcmp0 (mime_type, supported_mime_types[i]) == 0)
            {
              ret_val = photos_utils_get_pixbuf_common_suffix (format);
              break;
            }
        }

      if (ret_val != NULL)
        break;
    }

  return ret_val;
}


953 954
gint
photos_utils_get_icon_size (void)
955 956 957 958 959 960 961 962 963 964 965 966 967 968
{
  GApplication *app;
  gint scale;
  gint size;

  app = g_application_get_default ();
  scale = photos_application_get_scale_factor (PHOTOS_APPLICATION (app));
  size = photos_utils_get_icon_size_unscaled ();
  return scale * size;
}


gint
photos_utils_get_icon_size_unscaled (void)
969
{
970
  return 256;
971 972 973
}


974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996
gint64
photos_utils_get_mtime_from_sparql_cursor (TrackerSparqlCursor *cursor)
{
  const gchar *mtime_str;
  gint64 mtime = -1;

  mtime_str = tracker_sparql_cursor_get_string (cursor, PHOTOS_QUERY_COLUMNS_MTIME, NULL);
  if (mtime_str != NULL)
    {
      g_autoptr (GDateTime) date_modified = NULL;

      date_modified = g_date_time_new_from_iso8601 (mtime_str, NULL);
      if (date_modified != NULL)
        mtime = g_date_time_to_unix (date_modified);
    }

  if (mtime == -1)
    mtime = g_get_real_time () / 1000000;

  return mtime;
}


997 998 999
gchar *
photos_utils_get_pixbuf_common_suffix (GdkPixbufFormat *format)
{
Umang Jain's avatar
Umang Jain committed
1000
  g_auto (GStrv) extensions = NULL;
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028
  gchar *result = NULL;
  gint i;

  if (format == NULL)
    return NULL;

  extensions = gdk_pixbuf_format_get_extensions (format);
  if (extensions[0] == NULL)
    return NULL;

  /* try to find 3-char suffix first, use the last occurence */
  for (i = 0; extensions [i] != NULL; i++)
    {
      if (strlen (extensions[i]) <= 3)
        {
          g_free (result);
          result = g_ascii_strdown (extensions[i], -1);
        }
    }

  /* otherwise take the first one */
  if (result == NULL)
    result = g_ascii_strdown (extensions[0], -1);

  return result;
}


1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042
const gchar *
photos_utils_get_provider_name (PhotosBaseManager *src_mngr, PhotosBaseItem *item)
{
  PhotosSource *source;
  const gchar *name;
  const gchar *resource_urn;

  resource_urn = photos_base_item_get_resource_urn (item);
  source = PHOTOS_SOURCE (photos_base_manager_get_object_by_id (src_mngr, resource_urn));
  name = photos_source_get_name (source);
  return name;
}


1043 1044 1045 1046 1047
gboolean
photos_utils_get_selection_mode (void)
{
  GAction *action;
  GApplication *app;
Umang Jain's avatar
Umang Jain committed
1048
  g_autoptr (GVariant) state = NULL;
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062
  gboolean selection_mode;

  app = g_application_get_default ();
  action = g_action_map_lookup_action (G_ACTION_MAP (app), "selection-mode");

  state = g_action_get_state (action);
  g_return_val_if_fail (state != NULL, FALSE);

  selection_mode = g_variant_get_boolean (state);

  return selection_mode;
}


Debarshi Ray's avatar
Debarshi Ray committed
1063
GList *
Debarshi Ray's avatar
Debarshi Ray committed
1064
photos_utils_get_urns_from_items (GList *items)
Debarshi Ray's avatar
Debarshi Ray committed
1065 1066 1067 1068
{
  GList *l;
  GList *urns = NULL;

Debarshi Ray's avatar
Debarshi Ray committed
1069
  for (l = items; l != NULL; l = l->next)
Debarshi Ray's avatar
Debarshi Ray committed
1070
    {
Debarshi Ray's avatar
Debarshi Ray committed
1071 1072
      GdMainBoxItem *box_item = GD_MAIN_BOX_ITEM (l->data);
      const gchar *id;
Debarshi Ray's avatar
Debarshi Ray committed
1073

Debarshi Ray's avatar
Debarshi Ray committed
1074 1075
      id = gd_main_box_item_get_id (box_item);
      urns = g_list_prepend (urns, g_strdup (id));
Debarshi Ray's avatar
Debarshi Ray committed
1076 1077 1078 1079
    }

  return g_list_reverse (urns);
}
1080 1081


1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096
const gchar *
photos_utils_get_version (void)
{
  const gchar *ret_val = NULL;

#ifdef PACKAGE_COMMIT_ID
  ret_val = PACKAGE_COMMIT_ID;
#else
  ret_val = PACKAGE_VERSION;
#endif

  return ret_val;
}


1097 1098
void
photos_utils_launch_online_accounts (const gchar *account_id)
1099
{
1100 1101 1102
  GApplication *app;
  g_autoptr (GDBusActionGroup) control_center = NULL;
  GDBusConnection *connection;
Debarshi Ray's avatar
Debarshi Ray committed
1103
  GVariant *parameters;
1104 1105
  g_auto (GVariantBuilder) panel_parameters = G_VARIANT_BUILDER_INIT (G_VARIANT_TYPE ("av"));

1106 1107 1108
  app = g_application_get_default ();
  connection = g_application_get_dbus_connection (app);
  control_center = g_dbus_action_group_get (connection, "org.gnome.ControlCenter", "/org/gnome/ControlCenter");
1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122

  if (account_id != NULL && account_id[0] != '\0')
    {
      GVariant *account_id_variant;

      account_id_variant = g_variant_new_string (account_id);
      g_variant_builder_add (&panel_parameters, "v", account_id_variant);
    }

  parameters = g_variant_new ("(s@av)", "online-accounts", g_variant_builder_end (&panel_parameters));
  g_action_group_activate_action (G_ACTION_GROUP (control_center), "launch-panel", parameters);
}


1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143
void
photos_utils_list_box_header_func (GtkListBoxRow *row, GtkListBoxRow *before, gpointer user_data)
{
  GtkWidget *header;

  if (before == NULL)
    {
      gtk_list_box_row_set_header (row, NULL);
      return;
    }

  header = gtk_list_box_row_get_header (row);
  if (header == NULL)
    {
      header = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
      gtk_widget_show (header);
      gtk_list_box_row_set_header (row, header);
    }
}


1144 1145 1146 1147 1148
GAppLaunchContext *
photos_utils_new_app_launch_context_from_widget (GtkWidget *widget)
{
  GAppLaunchContext *ret_val = NULL;
  g_autoptr (GdkAppLaunchContext) ctx = NULL;
1149 1150
  GdkDisplay *display = NULL;
  GdkScreen *screen = NULL;
1151

1152 1153 1154 1155 1156
  if (widget != NULL)
    {
      screen = gtk_widget_get_screen (widget);
      display = gdk_screen_get_display (screen);
    }
1157

1158 1159
  if (display == NULL)
    display = gdk_display_get_default ();
1160 1161

  ctx = gdk_display_get_app_launch_context (display);
1162 1163
  if (screen != NULL)
    gdk_app_launch_context_set_screen (ctx, screen);
1164 1165 1166 1167 1168 1169

  ret_val = G_APP_LAUNCH_CONTEXT (g_steal_pointer (&ctx));
  return ret_val;
}


1170 1171 1172
void
photos_utils_object_list_free_full (GList *objects)
{
Debarshi Ray's avatar
Debarshi Ray committed
1173
  g_list_free_full (objects, g_object_unref);
1174 1175 1176
}


1177 1178 1179
gchar *
photos_utils_print_zoom_action_detailed_name (const gchar *action_name, gdouble delta, PhotosZoomEvent event)
{
Umang Jain's avatar
Umang Jain committed
1180
  g_autoptr (GVariant) target_value = NULL;
1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195
  gchar *ret_val = NULL;

  g_return_val_if_fail (action_name != NULL && action_name[0] != '\0', NULL);
  g_return_val_if_fail (delta >= 0.0, NULL);
  g_return_val_if_fail (event != PHOTOS_ZOOM_EVENT_NONE, NULL);

  target_value = photos_utils_create_zoom_target_value (delta, event);
  target_value = g_variant_ref_sink (target_value);

  ret_val = g_action_print_detailed_name (action_name, target_value);

  return ret_val;
}


1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264
static gboolean
photos_utils_adjustment_can_scroll (GtkAdjustment *adjustment)
{
  gdouble lower;
  gdouble page_size;
  gdouble upper;

  g_return_val_if_fail (GTK_IS_ADJUSTMENT (adjustment), FALSE);

  lower = gtk_adjustment_get_lower (adjustment);
  page_size = gtk_adjustment_get_page_size (adjustment);
  upper = gtk_adjustment_get_upper (adjustment);

  return upper - lower > page_size;
}


gboolean
photos_utils_scrolled_window_can_scroll (GtkScrolledWindow *scrolled_window)
{
  GtkAdjustment *adjustment;
  gboolean ret_val = TRUE;

  g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), FALSE);

  adjustment = gtk_scrolled_window_get_hadjustment (scrolled_window);
  if (photos_utils_adjustment_can_scroll (adjustment))
    goto out;

  adjustment = gtk_scrolled_window_get_vadjustment (scrolled_window);
  if (photos_utils_adjustment_can_scroll (adjustment))
    goto out;

  ret_val = FALSE;

 out:
  return ret_val;
}


static void
photos_utils_adjustment_scroll (GtkAdjustment *adjustment, gdouble delta)
{
  gdouble value;

  g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));

  value = gtk_adjustment_get_value (adjustment);
  value += delta;
  gtk_adjustment_set_value (adjustment, value);
}


void
photos_utils_scrolled_window_scroll (GtkScrolledWindow *scrolled_window, gdouble delta_x, gdouble delta_y)
{
  GtkAdjustment *adjustment;

  g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
  g_return_if_fail (photos_utils_scrolled_window_can_scroll (scrolled_window));

  adjustment = gtk_scrolled_window_get_hadjustment (scrolled_window);
  photos_utils_adjustment_scroll (adjustment, delta_x);

  adjustment = gtk_scrolled_window_get_vadjustment (scrolled_window);
  photos_utils_adjustment_scroll (adjustment, delta_y);
}


Debarshi Ray's avatar
Debarshi Ray committed
1265
static void
1266
photos_utils_update_executed (GObject *source_object, GAsyncResult *res, gpointer user_data)
Debarshi Ray's avatar
Debarshi Ray committed
1267 1268 1269 1270
{
  TrackerSparqlConnection *connection = TRACKER_SPARQL_CONNECTION (source_object);
  const gchar *urn = (gchar *) user_data;

Umang Jain's avatar
Umang Jain committed
1271 1272 1273 1274 1275
  {
    g_autoptr (GError) error = NULL;

    tracker_sparql_connection_update_finish (connection, res, &error);
    if (error != NULL)
1276
      g_warning ("Unable to update %s: %s", urn, error->message);
Umang Jain's avatar
Umang Jain committed
1277
  }
Debarshi Ray's avatar
Debarshi Ray committed
1278 1279 1280
}


1281 1282 1283
void
photos_utils_set_edited_name (const gchar *urn, const gchar *title)
{
Umang Jain's avatar
Umang Jain committed
1284 1285 1286
  g_autoptr (PhotosQuery) query = NULL;
  g_autoptr (PhotosTrackerQueue) queue = NULL;
  g_autofree gchar *sparql = NULL;
1287

1288
  sparql = g_strdup_printf ("INSERT OR REPLACE { <%s> nie:title \"%s\" }", urn, title);
1289
  query = photos_query_new (NULL, sparql);
1290

Umang Jain's avatar
Umang Jain committed
1291 1292 1293 1294 1295 1296 1297 1298 1299 1300
  {
    g_autoptr (GError) error = NULL;

    queue = photos_tracker_queue_dup_singleton (NULL, &error);
    if (G_UNLIKELY (error != NULL))
      {
        g_warning ("Unable to set edited name %s: %s", urn, error->message);
        goto out;
      }
  }
1301

1302
  photos_tracker_queue_update (queue, query, NULL, photos_utils_update_executed, g_strdup (urn), g_free);
1303 1304

 out:
Umang Jain's avatar
Umang Jain committed
1305
  return;
1306 1307 1308
}


Debarshi Ray's avatar
Debarshi Ray committed
1309 1310 1311
void
photos_utils_set_favorite (const gchar *urn, gboolean is_favorite)
{
Umang Jain's avatar
Umang Jain committed
1312 1313 1314
  g_autoptr (PhotosQuery) query = NULL;
  g_autoptr (PhotosTrackerQueue) queue = NULL;
  g_autofree gchar *sparql = NULL;
Debarshi Ray's avatar
Debarshi Ray committed
1315 1316 1317 1318

  sparql = g_strdup_printf ("%s { <%s> nao:hasTag nao:predefined-tag-favorite }",
                            (is_favorite) ? "INSERT OR REPLACE" : "DELETE",
                            urn);
1319
  query = photos_query_new (NULL, sparql);