[gnome-photos/wip/flickr: 14/14] application: Start the miners and refresh them at 1 minute intervals



commit b1e3796761ae27b1c61ad565611d0a293437d700
Author: Debarshi Ray <debarshir gnome org>
Date:   Tue Jul 2 19:25:58 2013 +0200

    application: Start the miners and refresh them at 1 minute intervals
    
    Fixes: https://bugzilla.gnome.org/697675

 src/Makefile.am             |   10 +++
 src/photos-application.c    |  171 +++++++++++++++++++++++++++++++++++++++++++
 src/photos-gom-miner.xml    |   30 ++++++++
 src/photos-source-manager.c |   45 +++++++++++-
 src/photos-source-manager.h |    8 ++-
 5 files changed, 262 insertions(+), 2 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 6243a14..c2371b0 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -14,6 +14,8 @@ gnome_photos_built_sources = \
        photos-dleyna-renderer-manager.h \
        photos-dleyna-renderer-push-host.c \
        photos-dleyna-renderer-push-host.h \
+       photos-gom-miner.c \
+       photos-gom-miner.h \
        photos-marshalers.c \
        photos-marshalers.h \
        photos-mpris-player.c \
@@ -202,6 +204,7 @@ EXTRA_DIST = \
        photos-marshalers.list \
        photos-preview-menu.ui \
        photos-selection-menu.ui \
+       photos-gom-miner.xml \
        photos-tracker-resources.xml \
        photos-dleyna-renderer-device.xml \
        photos-dleyna-renderer-manager.xml \
@@ -410,6 +413,13 @@ photos-dleyna-renderer-push-host.h photos-dleyna-renderer-push-host.c: photos-dl
                --interface-prefix com.intel.dLeynaRenderer. \
                $<
 
+photos-gom-miner.h photos-gom-miner.c: photos-gom-miner.xml
+       $(AM_V_GEN)gdbus-codegen \
+               --c-namespace Gom \
+               --generate-c-code photos-gom-miner \
+               --interface-prefix org.gnome.OnlineMiners. \
+               $<
+
 photos-mpris-player.h photos-mpris-player.c: photos-mpris-player.xml
        $(AM_V_GEN)gdbus-codegen \
                --c-namespace Mpris \
diff --git a/src/photos-application.c b/src/photos-application.c
index 79f3807..33ef689 100644
--- a/src/photos-application.c
+++ b/src/photos-application.c
@@ -35,15 +35,18 @@
 #include "eog-debug.h"
 #include "photos-application.h"
 #include "photos-dlna-renderers-dialog.h"
+#include "photos-gom-miner.h"
 #include "photos-item-manager.h"
 #include "photos-main-window.h"
 #include "photos-mode-controller.h"
 #include "photos-properties-dialog.h"
 #include "photos-resources.h"
+#include "photos-source-manager.h"
 
 
 struct _PhotosApplicationPrivate
 {
+  GList *miners_running;
   GResource *resource;
   GSettings *settings;
   GSimpleAction *fs_action;
@@ -55,8 +58,10 @@ struct _PhotosApplicationPrivate
   GSimpleAction *sel_none_action;
   GSimpleAction *set_bg_action;
   GSimpleAction *remote_display_action;
+  GomMiner *flickr_miner;
   GtkWidget *main_window;
   PhotosBaseManager *item_mngr;
+  PhotosBaseManager *src_mngr;
   PhotosModeController *mode_cntrlr;
 };
 
@@ -64,6 +69,43 @@ struct _PhotosApplicationPrivate
 G_DEFINE_TYPE (PhotosApplication, photos_application, GTK_TYPE_APPLICATION)
 
 
+enum
+{
+  MINER_REFRESH_TIMEOUT = 60 /* s */
+};
+
+typedef struct _PhotosApplicationRefreshData PhotosApplicationRefreshData;
+
+struct _PhotosApplicationRefreshData
+{
+  PhotosApplication *application;
+  GomMiner *miner;
+};
+
+static gboolean photos_application_refresh_miner_now (PhotosApplication *self, GomMiner *miner);
+
+
+static PhotosApplicationRefreshData *
+photos_application_refresh_data_new (PhotosApplication *application, GomMiner *miner)
+{
+  PhotosApplicationRefreshData *data;
+
+  data = g_slice_new0 (PhotosApplicationRefreshData);
+  data->application = g_object_ref (application);
+  data->miner = g_object_ref (miner);
+  return data;
+}
+
+
+static void
+photos_application_refresh_data_free (PhotosApplicationRefreshData *data)
+{
+  g_object_unref (data->application);
+  g_object_unref (data->miner);
+  g_slice_free (PhotosApplicationRefreshData, data);
+}
+
+
 static void
 photos_application_action_toggle (GSimpleAction *simple, GVariant *parameter, gpointer user_data)
 {
@@ -153,6 +195,77 @@ photos_application_properties (PhotosApplication *self)
 }
 
 
+static gboolean
+photos_application_refresh_miner_timeout (gpointer user_data)
+{
+  PhotosApplicationRefreshData *data = (PhotosApplicationRefreshData *) user_data;
+  return photos_application_refresh_miner_now (data->application, data->miner);
+}
+
+
+static void
+photos_application_refresh_db (GObject *source_object, GAsyncResult *res, gpointer user_data)
+{
+  PhotosApplication *self = PHOTOS_APPLICATION (user_data);
+  PhotosApplicationPrivate *priv = self->priv;
+  GError *error;
+  GomMiner *miner = GOM_MINER (source_object);
+  PhotosApplicationRefreshData *data;
+
+  priv->miners_running = g_list_remove (priv->miners_running, miner);
+
+  error = NULL;
+  if (!gom_miner_call_refresh_db_finish (miner, res, &error))
+    {
+      if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
+        g_warning ("Unable to update the cache: %s", error->message);
+      g_error_free (error);
+      goto out;
+    }
+
+  data = photos_application_refresh_data_new (self, miner);
+  g_timeout_add_seconds_full (G_PRIORITY_DEFAULT,
+                              MINER_REFRESH_TIMEOUT,
+                              photos_application_refresh_miner_timeout,
+                              data,
+                              (GDestroyNotify) photos_application_refresh_data_free);
+
+ out:
+  g_object_unref (self);
+  g_object_unref (miner);
+}
+
+
+static gboolean
+photos_application_refresh_miner_now (PhotosApplication *self, GomMiner *miner)
+{
+  PhotosApplicationPrivate *priv = self->priv;
+  GCancellable *cancellable;
+
+  if (g_getenv ("PHOTOS_DISABLE_MINERS") != NULL)
+    goto out;
+
+  priv->miners_running = g_list_prepend (priv->miners_running, g_object_ref (miner));
+
+  cancellable = g_cancellable_new ();
+  g_object_set_data_full (G_OBJECT (miner), "cancellable", cancellable, g_object_unref);
+  gom_miner_call_refresh_db (miner, cancellable, photos_application_refresh_db, g_object_ref (self));
+
+ out:
+  return G_SOURCE_REMOVE;
+}
+
+
+static void
+photos_application_refresh_miners (PhotosApplication *self)
+{
+  PhotosApplicationPrivate *priv = self->priv;
+
+  if (photos_source_manager_has_provider_type (PHOTOS_SOURCE_MANAGER (priv->src_mngr), "flickr"))
+    photos_application_refresh_miner_now (self, priv->flickr_miner);
+}
+
+
 static void
 photos_application_remote_display_current (PhotosApplication *self)
 {
@@ -199,6 +312,43 @@ photos_application_set_bg (PhotosApplication *self)
 
 
 static void
+photos_application_start_miners (PhotosApplication *self)
+{
+  PhotosApplicationPrivate *priv = self->priv;
+
+  photos_application_refresh_miners (self);
+
+  g_signal_connect_object (priv->src_mngr,
+                           "object-added",
+                           G_CALLBACK (photos_application_refresh_miners),
+                           self,
+                           G_CONNECT_SWAPPED);
+  g_signal_connect_object (priv->src_mngr,
+                           "object-removed",
+                           G_CALLBACK (photos_application_refresh_miners),
+                           self,
+                           G_CONNECT_SWAPPED);
+}
+
+
+static void
+photos_application_stop_miners (PhotosApplication *self)
+{
+  PhotosApplicationPrivate *priv = self->priv;
+  GList *l;
+
+  for (l = priv->miners_running; l != NULL; l = l->next)
+    {
+      GomMiner *miner = GOM_MINER (l->data);
+      GCancellable *cancellable;
+
+      cancellable = g_object_get_data (G_OBJECT (miner), "cancellable");
+      g_cancellable_cancel (cancellable);
+    }
+}
+
+
+static void
 photos_application_window_mode_changed (PhotosApplication *self, PhotosWindowMode mode, PhotosWindowMode 
old_mode)
 {
   PhotosApplicationPrivate *priv = self->priv;
@@ -264,7 +414,15 @@ photos_application_startup (GApplication *application)
   settings = gtk_settings_get_default ();
   g_object_set (settings, "gtk-application-prefer-dark-theme", TRUE, NULL);
 
+  priv->flickr_miner = gom_miner_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
+                                                         G_DBUS_PROXY_FLAGS_NONE,
+                                                         "org.gnome.OnlineMiners.Flickr",
+                                                         "/org/gnome/OnlineMiners/Flickr",
+                                                         NULL,
+                                                         NULL);
+
   priv->item_mngr = photos_item_manager_new ();
+  priv->src_mngr = photos_source_manager_new ();
   priv->mode_cntrlr = photos_mode_controller_new ();
 
   action = g_simple_action_new ("about", NULL);
@@ -337,6 +495,8 @@ photos_application_startup (GApplication *application)
 
   priv->main_window = photos_main_window_new (GTK_APPLICATION (self));
   photos_mode_controller_set_window_mode (priv->mode_cntrlr, PHOTOS_WINDOW_MODE_OVERVIEW);
+
+  photos_application_start_miners (self);
 }
 
 
@@ -364,6 +524,13 @@ photos_application_dispose (GObject *object)
   PhotosApplication *self = PHOTOS_APPLICATION (object);
   PhotosApplicationPrivate *priv = self->priv;
 
+  if (priv->miners_running != NULL)
+    {
+      photos_application_stop_miners (self);
+      g_list_free_full (priv->miners_running, g_object_unref);
+      priv->miners_running = NULL;
+    }
+
   if (priv->resource != NULL)
     {
       g_resources_unregister (priv->resource);
@@ -380,7 +547,9 @@ photos_application_dispose (GObject *object)
   g_clear_object (&priv->sel_all_action);
   g_clear_object (&priv->sel_none_action);
   g_clear_object (&priv->set_bg_action);
+  g_clear_object (&priv->flickr_miner);
   g_clear_object (&priv->item_mngr);
+  g_clear_object (&priv->src_mngr);
   g_clear_object (&priv->mode_cntrlr);
 
   G_OBJECT_CLASS (photos_application_parent_class)
@@ -409,6 +578,8 @@ photos_application_class_init (PhotosApplicationClass *class)
   application_class->activate = photos_application_activate;
   application_class->startup = photos_application_startup;
 
+  /* TODO: Add miners-changed signal */
+
   g_type_class_add_private (class, sizeof (PhotosApplicationPrivate));
 }
 
diff --git a/src/photos-gom-miner.xml b/src/photos-gom-miner.xml
new file mode 100644
index 0000000..94ff188
--- /dev/null
+++ b/src/photos-gom-miner.xml
@@ -0,0 +1,30 @@
+<!DOCTYPE node PUBLIC
+"-//freedesktop//DTD D-BUS Object Introspection 1.0//EN"
+"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd";>
+
+<!--
+ Photos - access, organize and share your photos on GNOME
+ Copyright © 2013 Red Hat, Inc.
+
+ 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 2
+ of the License, or (at your option) any later version.
+
+ 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
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ 02110-1301, USA.
+-->
+
+<node name="/" xmlns:doc="http://www.freedesktop.org/dbus/1.0/doc.dtd";>
+  <interface name="org.gnome.OnlineMiners.Miner">
+    <method name="RefreshDB" />
+    <property name="DisplayName" type="s" access="read" />
+  </interface>
+</node>
diff --git a/src/photos-source-manager.c b/src/photos-source-manager.c
index 83f7d74..72e53e0 100644
--- a/src/photos-source-manager.c
+++ b/src/photos-source-manager.c
@@ -1,6 +1,6 @@
 /*
  * Photos - access, organize and share your photos on GNOME
- * Copyright © 2012 Red Hat, Inc.
+ * Copyright © 2012, 2013 Red Hat, Inc.
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
@@ -197,3 +197,46 @@ photos_source_manager_new (void)
 {
   return g_object_new (PHOTOS_TYPE_SOURCE_MANAGER, NULL);
 }
+
+
+GList *
+photos_source_manager_get_for_provider_type (PhotosSourceManager *self, const gchar *provider_type)
+{
+  GHashTable *sources;
+  GHashTableIter iter;
+  GList *items = NULL;
+  PhotosSource *source;
+
+  sources = photos_base_manager_get_objects (PHOTOS_BASE_MANAGER (self));
+  g_hash_table_iter_init (&iter, sources);
+  while (g_hash_table_iter_next (&iter, NULL, (gpointer) &source))
+    {
+      GoaAccount *account;
+      GoaObject *object;
+
+      object = photos_source_get_goa_object (source);
+      if (object == NULL)
+        continue;
+
+      account = goa_object_peek_account (object);
+      if (g_strcmp0 (goa_account_get_provider_type (account), provider_type) == 0)
+        items = g_list_prepend (items, g_object_ref (source));
+    }
+
+  return items;
+}
+
+
+gboolean
+photos_source_manager_has_provider_type (PhotosSourceManager *self, const gchar *provider_type)
+{
+  GList *items;
+  gboolean ret_val = FALSE;
+
+  items = photos_source_manager_get_for_provider_type (self, provider_type);
+  if (items != NULL)
+    ret_val = TRUE;
+
+  g_list_free_full (items, g_object_unref);
+  return ret_val;
+}
diff --git a/src/photos-source-manager.h b/src/photos-source-manager.h
index e7df308..706de65 100644
--- a/src/photos-source-manager.h
+++ b/src/photos-source-manager.h
@@ -1,6 +1,6 @@
 /*
  * Photos - access, organize and share your photos on GNOME
- * Copyright © 2012 Red Hat, Inc.
+ * Copyright © 2012, 2013 Red Hat, Inc.
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
@@ -70,6 +70,12 @@ GType                     photos_source_manager_get_type           (void) G_GNUC
 
 PhotosBaseManager        *photos_source_manager_new                (void);
 
+GList                    *photos_source_manager_get_for_provider_type (PhotosSourceManager *self,
+                                                                       const gchar *provider_type);
+
+gboolean                  photos_source_manager_has_provider_type     (PhotosSourceManager *self,
+                                                                       const gchar *provider_type);
+
 G_END_DECLS
 
 #endif /* PHOTOS_SOURCE_MANAGER_H */



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