[gnome-control-center] background: Add support for Flickr pictures



commit 725b85db775ab8226d3b77b549a2daa2f96044b4
Author: Debarshi Ray <debarshir gnome org>
Date:   Wed Feb 12 15:32:22 2014 +0100

    background: Add support for Flickr pictures
    
    Listen to CcBackgroundGriloMiner::media-found and them to
    BgPicturesSource.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=707569

 configure.ac                           |    5 +-
 panels/background/bg-pictures-source.c |  141 ++++++++++++++++++++++++++++++--
 2 files changed, 139 insertions(+), 7 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index b5943c9..cd39e46 100644
--- a/configure.ac
+++ b/configure.ac
@@ -105,6 +105,7 @@ ACCOUNTSSERVICE_REQUIRED_VERSION=0.6.30
 COLORD_REQUIRED_VERSION=0.1.34
 COLORD_GTK_REQUIRED_VERSION=0.1.24
 PWQUALITY_REQUIRED_VERSION=1.2.2
+GRILO_REQUIRED_VERSION=0.2.6
 
 COMMON_MODULES="gtk+-3.0 >= $GTK_REQUIRED_VERSION
  glib-2.0 >= $GLIB_REQUIRED_VERSION
@@ -119,7 +120,9 @@ PKG_CHECK_MODULES(LIBLANGUAGE, $COMMON_MODULES gnome-desktop-3.0 fontconfig)
 PKG_CHECK_MODULES(LIBSHORTCUTS, $COMMON_MODULES x11)
 PKG_CHECK_MODULES(SHELL, $COMMON_MODULES x11 polkit-gobject-1 >= $POLKIT_REQUIRED_VERSION)
 PKG_CHECK_MODULES(BACKGROUND_PANEL, $COMMON_MODULES libxml-2.0 gnome-desktop-3.0
-                  gdk-pixbuf-2.0 >= $GDKPIXBUF_REQUIRED_VERSION)
+                  gdk-pixbuf-2.0 >= $GDKPIXBUF_REQUIRED_VERSION
+                  goa-1.0 >= $GOA_REQUIRED_VERSION
+                  grilo-0.2 >= $GRILO_REQUIRED_VERSION)
 PKG_CHECK_MODULES(DATETIME_PANEL, $COMMON_MODULES
                   gnome-desktop-3.0 >= $GNOME_DESKTOP_REQUIRED_VERSION
                   polkit-gobject-1 >= $POLKIT_REQUIRED_VERSION
diff --git a/panels/background/bg-pictures-source.c b/panels/background/bg-pictures-source.c
index 73e32b7..0304868 100644
--- a/panels/background/bg-pictures-source.c
+++ b/panels/background/bg-pictures-source.c
@@ -23,10 +23,12 @@
 
 #include "bg-pictures-source.h"
 
+#include "cc-background-grilo-miner.h"
 #include "cc-background-item.h"
 
 #include <string.h>
 #include <gio/gio.h>
+#include <grilo.h>
 #include <libgnome-desktop/gnome-desktop-thumbnail.h>
 #include <gdesktop-enums.h>
 
@@ -43,6 +45,8 @@ struct _BgPicturesSourcePrivate
 {
   GCancellable *cancellable;
 
+  CcBackgroundGriloMiner *grl_miner;
+
   GnomeDesktopThumbnailFactory *thumb_factory;
 
   GFileMonitor *picture_dir_monitor;
@@ -105,6 +109,7 @@ bg_pictures_source_dispose (GObject *object)
       g_clear_object (&priv->cancellable);
     }
 
+  g_clear_object (&priv->grl_miner);
   g_clear_object (&priv->thumb_factory);
 
   G_OBJECT_CLASS (bg_pictures_source_parent_class)->dispose (object);
@@ -194,6 +199,8 @@ picture_scaled (GObject *source_object,
   bg_source = BG_PICTURES_SOURCE (user_data);
   store = bg_source_get_liststore (BG_SOURCE (bg_source));
   uri = cc_background_item_get_uri (item);
+  if (uri == NULL)
+    uri = cc_background_item_get_source_url (item);
 
   /* Ignore screenshots */
   software = gdk_pixbuf_get_option (pixbuf, "tEXt::Software");
@@ -276,6 +283,47 @@ picture_opened_for_read (GObject *source_object,
   g_object_unref (stream);
 }
 
+static void
+picture_copied_for_read (GObject *source_object,
+                         GAsyncResult *res,
+                         gpointer user_data)
+{
+  BgPicturesSource *bg_source;
+  CcBackgroundItem *item;
+  GError *error = NULL;
+  GFile *thumbnail_file = G_FILE (source_object);
+  GFile *native_file;
+
+  if (!g_file_copy_finish (thumbnail_file, res, &error))
+    {
+      if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
+        goto out;
+      else if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_EXISTS))
+        {
+          gchar *uri;
+
+          uri = g_file_get_uri (thumbnail_file);
+          g_warning ("Failed to download '%s': %s", uri, error->message);
+          g_free (uri);
+          goto out;
+        }
+    }
+
+  bg_source = BG_PICTURES_SOURCE (user_data);
+
+  native_file = g_object_get_data (G_OBJECT (thumbnail_file), "native-file");
+  item = g_object_get_data (G_OBJECT (thumbnail_file), "item");
+  g_object_set_data_full (G_OBJECT (native_file), "item", g_object_ref (item), g_object_unref);
+  g_file_read_async (native_file,
+                     G_PRIORITY_DEFAULT,
+                     bg_source->priv->cancellable,
+                     picture_opened_for_read,
+                     bg_source);
+
+ out:
+  g_clear_error (&error);
+}
+
 static gboolean
 in_content_types (const char *content_type)
 {
@@ -303,6 +351,7 @@ add_single_file (BgPicturesSource *bg_source,
 {
   const gchar *content_type;
   CcBackgroundItem *item = NULL;
+  CcBackgroundItemFlags flags = 0;
   GError *error = NULL;
   GdkPixbuf *pixbuf = NULL;
   GtkIconInfo *icon_info = NULL;
@@ -331,7 +380,10 @@ add_single_file (BgPicturesSource *bg_source,
 
   is_native = g_file_is_native (file);
   if (is_native)
-    source_uri = g_strdup (uri);
+    {
+      source_uri = g_strdup (uri);
+      flags |= CC_BACKGROUND_ITEM_HAS_URI;
+    }
   else
     {
       source_uri = uri;
@@ -339,8 +391,9 @@ add_single_file (BgPicturesSource *bg_source,
     }
 
   item = cc_background_item_new (uri);
+  flags |= CC_BACKGROUND_ITEM_HAS_SHADING;
   g_object_set (G_OBJECT (item),
-               "flags", CC_BACKGROUND_ITEM_HAS_URI | CC_BACKGROUND_ITEM_HAS_SHADING,
+               "flags", flags,
                "shading", G_DESKTOP_BACKGROUND_SHADING_SOLID,
                "placement", G_DESKTOP_BACKGROUND_STYLE_ZOOM,
                 "modified", mtime,
@@ -383,11 +436,63 @@ add_single_file (BgPicturesSource *bg_source,
   g_object_set_data_full (G_OBJECT (item), "row-ref", row_ref, (GDestroyNotify) gtk_tree_row_reference_free);
 
  read_file:
-  g_object_set_data_full (G_OBJECT (file), "item", g_object_ref (item), g_object_unref);
+  if (is_native)
+    {
+      g_object_set_data_full (G_OBJECT (file), "item", g_object_ref (item), g_object_unref);
+      g_file_read_async (file, G_PRIORITY_DEFAULT,
+                         bg_source->priv->cancellable,
+                         picture_opened_for_read, bg_source);
+    }
+  else
+    {
+      GFile *native_file;
+      GFile *thumbnail_file = NULL;
+      GrlMedia *media;
+      gchar *native_dir;
+      gchar *native_path;
+
+      media = g_object_get_data (G_OBJECT (file), "grl-media");
+      if (media != NULL)
+        {
+          const gchar *title;
+          const gchar *thumbnail_uri;
 
-  g_file_read_async (file, G_PRIORITY_DEFAULT,
-                     bg_source->priv->cancellable,
-                     picture_opened_for_read, bg_source);
+          title = grl_media_get_title (media);
+          g_object_set (G_OBJECT (item), "name", title, NULL);
+
+          thumbnail_uri = grl_media_get_thumbnail (media);
+          thumbnail_file = g_file_new_for_uri (thumbnail_uri);
+        }
+
+      native_path = gnome_desktop_thumbnail_path_for_uri (source_uri, GNOME_DESKTOP_THUMBNAIL_SIZE_LARGE);
+      native_file = g_file_new_for_path (native_path);
+
+      native_dir = g_path_get_dirname (native_path);
+      g_mkdir_with_parents (native_dir, USER_DIR_MODE);
+
+      if (thumbnail_file != NULL)
+        {
+          g_object_set_data_full (G_OBJECT (thumbnail_file), "item", g_object_ref (item), g_object_unref);
+          g_object_set_data_full (G_OBJECT (thumbnail_file),
+                                  "native-file",
+                                  g_object_ref (native_file),
+                                  g_object_unref);
+          g_file_copy_async (thumbnail_file,
+                             native_file,
+                             G_FILE_COPY_ALL_METADATA,
+                             G_PRIORITY_DEFAULT,
+                             bg_source->priv->cancellable,
+                             NULL,
+                             NULL,
+                             picture_copied_for_read,
+                             bg_source);
+        }
+
+      g_clear_object (&thumbnail_file);
+      g_object_unref (native_file);
+      g_free (native_dir);
+      g_free (native_path);
+    }
 
   retval = TRUE;
 
@@ -711,6 +816,26 @@ files_changed_cb (GFileMonitor      *monitor,
 }
 
 static void
+media_found_cb (BgPicturesSource *self, GrlMedia *media)
+{
+  GFile *file = NULL;
+  const gchar *uri;
+
+  uri = grl_media_get_url (media);
+  file = g_file_new_for_uri (uri);
+  g_object_set_data_full (G_OBJECT (file), "grl-media", g_object_ref (media), g_object_unref);
+  g_file_query_info_async (file,
+                           ATTRIBUTES,
+                           G_FILE_QUERY_INFO_NONE,
+                           G_PRIORITY_DEFAULT,
+                           self->priv->cancellable,
+                           file_info_ready,
+                           self);
+
+  g_clear_object (&file);
+}
+
+static void
 bg_pictures_source_init (BgPicturesSource *self)
 {
   const gchar *pictures_path;
@@ -775,6 +900,10 @@ bg_pictures_source_init (BgPicturesSource *self)
 
   g_object_unref (dir);
 
+  priv->grl_miner = cc_background_grilo_miner_new ();
+  g_signal_connect_swapped (priv->grl_miner, "media-found", G_CALLBACK (media_found_cb), self);
+  cc_background_grilo_miner_start (priv->grl_miner);
+
   priv->thumb_factory =
     gnome_desktop_thumbnail_factory_new (GNOME_DESKTOP_THUMBNAIL_SIZE_LARGE);
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]