[gnome-photos/wip/search: 10/18] Add PhotosSearchProvider



commit 7607e1d061dc1c2a63edc176a3b21a6e0153462f
Author: Debarshi Ray <debarshir gnome org>
Date:   Thu Dec 5 11:12:07 2013 +0100

    Add PhotosSearchProvider

 src/Makefile.am                        |   12 +
 src/org.gnome.ShellSearchProvider2.xml |   87 +++++++
 src/photos-search-provider.c           |  412 ++++++++++++++++++++++++++++++++
 src/photos-search-provider.h           |   74 ++++++
 4 files changed, 585 insertions(+), 0 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index a848b34..3367aa9 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -22,6 +22,8 @@ gnome_photos_built_sources = \
        photos-mpris-player.h \
        photos-resources.c \
        photos-resources.h \
+       photos-shell-search-provider2.c \
+       photos-shell-search-provider2.h \
        photos-tracker-resources.c \
        photos-tracker-resources.h \
        $(NULL)
@@ -177,6 +179,8 @@ gnome_photos_SOURCES = \
        photos-search-match.h \
        photos-search-match-manager.c \
        photos-search-match-manager.h \
+       photos-search-provider.c \
+       photos-search-provider.h \
        photos-search-type.c \
        photos-search-type.h \
        photos-search-type-manager.c \
@@ -232,6 +236,7 @@ BUILT_SOURCES = \
 
 EXTRA_DIST = \
        gegl-gtk-marshal.list \
+       org.gnome.ShellSearchProvider2.xml \
        photos-enums.c.template \
        photos-enums.h.template \
        photos-generate-about \
@@ -471,6 +476,13 @@ photos-mpris-player.h photos-mpris-player.c: photos-mpris-player.xml
                --interface-prefix org.mpris.MediaPlayer2. \
                $<
 
+photos-shell-search-provider2.h photos-shell-search-provider2.c: org.gnome.ShellSearchProvider2.xml Makefile
+       $(AM_V_GEN)gdbus-codegen \
+               --c-namespace Shell \
+               --generate-c-code photos-shell-search-provider2 \
+               --interface-prefix org.gnome.Shell. \
+               $<
+
 photos-tracker-resources.h photos-tracker-resources.c: photos-tracker-resources.xml
        $(AM_V_GEN)gdbus-codegen \
                --c-namespace Tracker \
diff --git a/src/org.gnome.ShellSearchProvider2.xml b/src/org.gnome.ShellSearchProvider2.xml
new file mode 100644
index 0000000..9502340
--- /dev/null
+++ b/src/org.gnome.ShellSearchProvider2.xml
@@ -0,0 +1,87 @@
+<!DOCTYPE node PUBLIC
+'-//freedesktop//DTD D-BUS Object Introspection 1.0//EN'
+'http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd'>
+<node>
+
+  <!--
+      org.gnome.Shell.SearchProvider2:
+      @short_description: Search provider interface
+
+      The interface used for integrating into GNOME Shell's search
+      interface (version 2).
+  -->
+  <interface name="org.gnome.Shell.SearchProvider2">
+
+    <!--
+        GetInitialResultSet:
+        @terms: Array of search terms, which the provider should treat as logical AND.
+        @results: An array of result identifier strings representing items which match the given search 
terms. Identifiers must be unique within the provider's domain, but other than that may be chosen freely by 
the provider.
+
+        Called when the user first begins a search.
+    -->
+    <method name="GetInitialResultSet">
+      <arg type="as" name="terms" direction="in" />
+      <arg type="as" name="results" direction="out" />
+    </method>
+
+    <!--
+        GetSubsearchResultSet:
+        @previous_results: Array of results previously returned by GetInitialResultSet().
+        @terms: Array of updated search terms, which the provider should treat as logical AND.
+        @results: An array of result identifier strings representing items which match the given search 
terms. Identifiers must be unique within the provider's domain, but other than that may be chosen freely by 
the provider.
+
+        Called when a search is performed which is a "subsearch" of
+        the previous search, e.g. the method may return less results, but
+        not more or different results.
+
+        This allows search providers to only search through the previous
+        result set, rather than possibly performing a full re-query.
+    -->
+    <method name="GetSubsearchResultSet">
+      <arg type="as" name="previous_results" direction="in" />
+      <arg type="as" name="terms" direction="in" />
+      <arg type="as" name="results" direction="out" />
+    </method>
+
+    <!--
+        GetResultMetas:
+        @identifiers: An array of result identifiers as returned by GetInitialResultSet() or 
GetSubsearchResultSet()
+        @metas: A dictionary describing the given search result, containing a human-readable 'name' 
(string), along with the result identifier this meta is for, 'id' (string). Optionally, 'icon' (a serialized 
GIcon as obtained by g_icon_serialize) can be specified if the result can be better served with a thumbnail 
of the content (such as with images). 'gicon' (a serialized GIcon as obtained by g_icon_to_string) or 
'icon-data' (raw image data as (iiibiiay) - width, height, rowstride, has-alpha, bits per sample, channels, 
data) are deprecated values that can also be used for that purpose. A 'description' field (string) may also 
be specified if more context would help the user find the desired result.
+
+        Return an array of meta data used to display each given result
+    -->
+    <method name="GetResultMetas">
+      <arg type="as" name="identifiers" direction="in" />
+      <arg type="aa{sv}" name="metas" direction="out" />
+    </method>
+
+    <!--
+        ActivateResult:
+        @identifier: A result identifier as returned by GetInitialResultSet() or GetSubsearchResultSet()
+        @terms: Array of search terms, which the provider should treat as logical AND.
+        @timestamp: A timestamp of the user interaction that triggered this call
+
+        Called when the users chooses a given result. The result should
+        be displayed in the application associated with the corresponding
+        provider. The provided search terms can be used to allow launching a full search in
+        the application.
+    -->
+    <method name="ActivateResult">
+      <arg type="s" name="identifier" direction="in" />
+      <arg type="as" name="terms" direction="in" />
+      <arg type="u" name="timestamp" direction="in" />
+    </method>
+
+    <!--
+        LaunchSearch:
+        @terms: Array of search terms, which the provider should treat as logical AND.
+        @timestamp: A timestamp of the user interaction that triggered this call
+
+        Asks the search provider to launch a full search in the application for the provided terms.
+    -->
+    <method name="LaunchSearch">
+      <arg type="as" name="terms" direction="in" />
+      <arg type="u" name="timestamp" direction="in" />
+    </method>
+  </interface>
+</node>
diff --git a/src/photos-search-provider.c b/src/photos-search-provider.c
new file mode 100644
index 0000000..69f1532
--- /dev/null
+++ b/src/photos-search-provider.c
@@ -0,0 +1,412 @@
+/*
+ * Photos - access, organize and share your photos on GNOME
+ * Copyright © 2014 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.
+ */
+
+/* Based on code from:
+ *   + Documents
+ */
+
+
+#include "config.h"
+
+#include <glib.h>
+
+#include "photos-fetch-ids-job.h"
+#include "photos-fetch-metas-job.h"
+#include "photos-search-context.h"
+#include "photos-search-provider.h"
+#include "photos-shell-search-provider2.h"
+
+
+struct _PhotosSearchProvider
+{
+  GObject parent_instance;
+  GCancellable *cancellable;
+  GHashTable *cache;
+  PhotosSearchContextState *state;
+  ShellSearchProvider2 *skeleton;
+};
+
+struct _PhotosSearchProviderClass
+{
+  GObjectClass parent_class;
+};
+
+enum
+{
+  ACTIVATE_RESULT,
+  LAUNCH_SEARCH,
+  LAST_SIGNAL
+};
+
+static guint signals[LAST_SIGNAL] = { 0 };
+
+static void photos_search_provider_search_context_iface_init (PhotosSearchContextInterface *iface);
+
+
+G_DEFINE_TYPE_WITH_CODE (PhotosSearchProvider, photos_search_provider, G_TYPE_OBJECT,
+                         G_IMPLEMENT_INTERFACE (PHOTOS_TYPE_SEARCH_CONTEXT,
+                                                photos_search_provider_search_context_iface_init));
+
+
+static gboolean
+photos_search_provider_activate_result (PhotosSearchProvider *self,
+                                        GDBusMethodInvocation *invocation,
+                                        const gchar *identifier,
+                                        const gchar *const *terms,
+                                        guint timestamp)
+{
+  g_signal_emit (self, signals[ACTIVATE_RESULT], 0, identifier, terms, timestamp);
+  shell_search_provider2_complete_activate_result (self->skeleton, invocation);
+  return TRUE;
+}
+
+
+static void
+photos_search_provider_fetch_ids_executed (const gchar *const *ids, gpointer user_data)
+{
+  GApplication *app;
+  GDBusMethodInvocation *invocation = G_DBUS_METHOD_INVOCATION (user_data);
+  GVariant *parameters;
+
+  app = g_application_get_default ();
+  g_application_release (app);
+
+  parameters = g_variant_new ("(^as)", ids);
+  g_dbus_method_invocation_return_value (invocation, parameters);
+  g_object_unref (invocation);
+}
+
+
+static PhotosSearchContextState *
+photos_search_provider_get_state (PhotosSearchContext *context)
+{
+  PhotosSearchProvider *self = PHOTOS_SEARCH_PROVIDER (context);
+  return self->state;
+}
+
+
+static void
+photos_search_provider_return_metas_from_cache (PhotosSearchProvider *self,
+                                                const gchar *const *identifiers,
+                                                GDBusMethodInvocation *invocation)
+{
+  GApplication *app;
+  GVariantBuilder builder;
+  guint i;
+
+  g_variant_builder_init (&builder, G_VARIANT_TYPE ("aa{sv}"));
+
+  for (i = 0; identifiers[i] != NULL; i++)
+    {
+      PhotosFetchMeta *meta;
+      const gchar *id = identifiers[i];
+
+      meta = (PhotosFetchMeta *) g_hash_table_lookup (self->cache, id);
+      if (meta == NULL)
+        continue;
+
+      g_variant_builder_open (&builder, G_VARIANT_TYPE ("a{sv}"));
+      g_variant_builder_add (&builder, "{sv}", "id", g_variant_new_string (meta->id));
+      g_variant_builder_add (&builder, "{sv}", "name", g_variant_new_string (meta->title));
+      if (meta->icon != NULL)
+        g_variant_builder_add (&builder, "{sv}", "icon", g_icon_serialize (meta->icon));
+
+      g_variant_builder_close (&builder);
+    }
+
+  app = g_application_get_default ();
+  g_application_release (app);
+
+  shell_search_provider2_complete_get_result_metas (self->skeleton, invocation, g_variant_builder_end 
(&builder));
+}
+
+
+static void
+photos_search_provider_fetch_metas_executed (GList *metas, gpointer user_data)
+{
+  PhotosSearchProvider *self;
+  GDBusMethodInvocation *invocation = G_DBUS_METHOD_INVOCATION (user_data);
+  GList *l;
+  const gchar *const *identifiers;
+
+  self = PHOTOS_SEARCH_PROVIDER (g_object_get_data (G_OBJECT (invocation), "self"));
+  identifiers = (const gchar *const *) g_object_get_data (G_OBJECT (invocation), "identifiers");
+
+  for (l = metas; l != NULL; l = l->next)
+    {
+      PhotosFetchMeta *copy;
+      PhotosFetchMeta *meta = (PhotosFetchMeta *) l->data;
+
+      copy = photos_fetch_meta_copy (meta);
+      g_hash_table_insert (self->cache, copy->id, copy);
+    }
+
+  photos_search_provider_return_metas_from_cache (self, identifiers, invocation);
+}
+
+
+static gboolean
+photos_search_provider_get_initial_result_set (PhotosSearchProvider *self,
+                                               GDBusMethodInvocation *invocation,
+                                               const gchar *const *terms)
+{
+  GApplication *app;
+  PhotosFetchIdsJob *job;
+
+  app = g_application_get_default ();
+  g_application_hold (app);
+
+  g_cancellable_cancel (self->cancellable);
+  g_cancellable_reset (self->cancellable);
+
+  job = photos_fetch_ids_job_new (terms);
+  photos_fetch_ids_job_run (job,
+                            self->state,
+                            self->cancellable,
+                            photos_search_provider_fetch_ids_executed,
+                            g_object_ref (invocation));
+  g_object_unref (job);
+
+  return TRUE;
+}
+
+
+static gboolean
+photos_search_provider_get_result_metas (PhotosSearchProvider *self,
+                                         GDBusMethodInvocation *invocation,
+                                         const gchar *const *identifiers)
+{
+  GApplication *app;
+  GPtrArray *to_fetch_arr;
+  const gchar *const *to_fetch;
+  guint i;
+  guint n_identifiers;
+  guint n_to_fetch;
+
+  app = g_application_get_default ();
+  g_application_hold (app);
+
+  n_identifiers = g_strv_length ((gchar **) identifiers);
+  to_fetch_arr = g_ptr_array_sized_new (n_identifiers + 1);
+
+  for (i = 0; identifiers[i] != NULL; i++)
+    {
+      if (g_hash_table_lookup (self->cache, identifiers[i]) == NULL)
+        g_ptr_array_add (to_fetch_arr, (gpointer) identifiers[i]);
+    }
+
+  g_ptr_array_add (to_fetch_arr, NULL);
+  to_fetch = (const gchar * const *) to_fetch_arr->pdata;
+
+  n_to_fetch = g_strv_length ((gchar **) to_fetch);
+  if (n_to_fetch > 0)
+    {
+      PhotosFetchMetasJob *job;
+
+      job = photos_fetch_metas_job_new (to_fetch);
+      g_object_set_data_full (G_OBJECT (invocation),
+                              "identifiers",
+                              g_boxed_copy (G_TYPE_STRV, identifiers),
+                              (GDestroyNotify) g_strfreev);
+      g_object_set_data_full (G_OBJECT (invocation), "self", g_object_ref (self), g_object_unref);
+      photos_fetch_metas_job_run (job,
+                                  self->state,
+                                  photos_search_provider_fetch_metas_executed,
+                                  g_object_ref (invocation));
+      g_object_unref (job);
+    }
+  else
+    photos_search_provider_return_metas_from_cache (self, identifiers, invocation);
+
+  g_ptr_array_unref (to_fetch_arr);
+
+  return TRUE;
+}
+
+
+static gboolean
+photos_search_provider_get_subsearch_result_set (PhotosSearchProvider *self,
+                                                 GDBusMethodInvocation *invocation,
+                                                 const gchar *const *previous_results,
+                                                 const gchar *const *terms)
+{
+  GApplication *app;
+  PhotosFetchIdsJob *job;
+
+  app = g_application_get_default ();
+  g_application_hold (app);
+
+  g_cancellable_cancel (self->cancellable);
+  g_cancellable_reset (self->cancellable);
+
+  job = photos_fetch_ids_job_new (terms);
+  photos_fetch_ids_job_run (job,
+                            self->state,
+                            self->cancellable,
+                            photos_search_provider_fetch_ids_executed,
+                            g_object_ref (invocation));
+  g_object_unref (job);
+
+  return TRUE;
+}
+
+
+static gboolean
+photos_search_provider_launch_search (PhotosSearchProvider *self,
+                                      GDBusMethodInvocation *invocation,
+                                      const gchar *const *terms,
+                                      guint timestamp)
+{
+  g_signal_emit (self, signals[LAUNCH_SEARCH], 0, terms, timestamp);
+  shell_search_provider2_complete_launch_search (self->skeleton, invocation);
+  return TRUE;
+}
+
+
+static void
+photos_search_provider_dispose (GObject *object)
+{
+  PhotosSearchProvider *self = PHOTOS_SEARCH_PROVIDER (object);
+
+  g_clear_object (&self->cancellable);
+  g_clear_object (&self->skeleton);
+
+  if (self->state != NULL)
+    {
+      photos_search_context_state_free (self->state);
+      self->state = NULL;
+    }
+
+  G_OBJECT_CLASS (photos_search_provider_parent_class)->dispose (object);
+}
+
+
+static void
+photos_search_provider_finalize (GObject *object)
+{
+  PhotosSearchProvider *self = PHOTOS_SEARCH_PROVIDER (object);
+
+  g_hash_table_unref (self->cache);
+
+  G_OBJECT_CLASS (photos_search_provider_parent_class)->finalize (object);
+}
+
+
+static void
+photos_search_provider_init (PhotosSearchProvider *self)
+{
+  self->cancellable = g_cancellable_new ();
+  self->cache = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, (GDestroyNotify) 
photos_fetch_meta_free);
+  self->state = photos_search_context_state_new (PHOTOS_SEARCH_CONTEXT (self));
+  self->skeleton = shell_search_provider2_skeleton_new ();
+
+  g_signal_connect_swapped (self->skeleton,
+                            "handle-activate-result",
+                            G_CALLBACK (photos_search_provider_activate_result),
+                            self);
+  g_signal_connect_swapped (self->skeleton,
+                            "handle-get-initial-result-set",
+                            G_CALLBACK (photos_search_provider_get_initial_result_set),
+                            self);
+  g_signal_connect_swapped (self->skeleton,
+                            "handle-get-subsearch-result-set",
+                            G_CALLBACK (photos_search_provider_get_subsearch_result_set),
+                            self);
+  g_signal_connect_swapped (self->skeleton,
+                            "handle-get-result-metas",
+                            G_CALLBACK (photos_search_provider_get_result_metas),
+                            self);
+  g_signal_connect_swapped (self->skeleton,
+                            "handle-launch-search",
+                            G_CALLBACK (photos_search_provider_launch_search),
+                            self);
+}
+
+
+static void
+photos_search_provider_class_init (PhotosSearchProviderClass *class)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (class);
+
+  object_class->dispose = photos_search_provider_dispose;
+  object_class->finalize = photos_search_provider_finalize;
+
+  signals[ACTIVATE_RESULT] = g_signal_new ("activate-result",
+                                           G_TYPE_FROM_CLASS (class),
+                                           G_SIGNAL_RUN_LAST,
+                                           0,    /* class_offset */
+                                           NULL, /* accumulator */
+                                           NULL, /* accu_data */
+                                           g_cclosure_marshal_generic,
+                                           G_TYPE_NONE,
+                                           3,
+                                           G_TYPE_STRING,
+                                           G_TYPE_STRV,
+                                           G_TYPE_UINT);
+
+  signals[LAUNCH_SEARCH] = g_signal_new ("launch-search",
+                                         G_TYPE_FROM_CLASS (class),
+                                         G_SIGNAL_RUN_LAST,
+                                         0,    /* class_offset */
+                                         NULL, /* accumulator */
+                                         NULL, /* accu_data */
+                                         g_cclosure_marshal_generic,
+                                         G_TYPE_NONE,
+                                         2,
+                                         G_TYPE_STRV,
+                                         G_TYPE_UINT);
+}
+
+
+static void
+photos_search_provider_search_context_iface_init (PhotosSearchContextInterface *iface)
+{
+  iface->get_state = photos_search_provider_get_state;
+}
+
+
+PhotosSearchProvider *
+photos_search_provider_new (void)
+{
+  return g_object_new (PHOTOS_TYPE_SEARCH_PROVIDER, NULL);
+}
+
+
+gboolean
+photos_search_provider_dbus_export (PhotosSearchProvider *self,
+                                    GDBusConnection *connection,
+                                    const gchar *object_path,
+                                    GError **error)
+{
+  return g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (self->skeleton),
+                                           connection,
+                                           object_path,
+                                           error);
+}
+
+
+void
+photos_search_provider_dbus_unexport (PhotosSearchProvider *self,
+                                      GDBusConnection *connection,
+                                      const gchar *object_path)
+{
+  if (g_dbus_interface_skeleton_has_connection (G_DBUS_INTERFACE_SKELETON (self->skeleton), connection))
+    g_dbus_interface_skeleton_unexport_from_connection (G_DBUS_INTERFACE_SKELETON (self->skeleton), 
connection);
+}
diff --git a/src/photos-search-provider.h b/src/photos-search-provider.h
new file mode 100644
index 0000000..7abf2da
--- /dev/null
+++ b/src/photos-search-provider.h
@@ -0,0 +1,74 @@
+/*
+ * Photos - access, organize and share your photos on GNOME
+ * Copyright © 2014 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.
+ */
+
+/* Based on code from:
+ *   + Documents
+ */
+
+#ifndef PHOTOS_SEARCH_PROVIDER_H
+#define PHOTOS_SEARCH_PROVIDER_H
+
+#include <gio/gio.h>
+
+G_BEGIN_DECLS
+
+#define PHOTOS_TYPE_SEARCH_PROVIDER (photos_search_provider_get_type ())
+
+#define PHOTOS_SEARCH_PROVIDER(obj) \
+  (G_TYPE_CHECK_INSTANCE_CAST ((obj), \
+   PHOTOS_TYPE_SEARCH_PROVIDER, PhotosSearchProvider))
+
+#define PHOTOS_SEARCH_PROVIDER_CLASS(klass) \
+  (G_TYPE_CHECK_CLASS_CAST ((klass), \
+   PHOTOS_TYPE_SEARCH_PROVIDER, PhotosSearchProviderClass))
+
+#define PHOTOS_IS_SEARCH_PROVIDER(obj) \
+  (G_TYPE_CHECK_INSTANCE_TYPE ((obj), \
+   PHOTOS_TYPE_SEARCH_PROVIDER))
+
+#define PHOTOS_IS_SEARCH_PROVIDER_CLASS(klass) \
+  (G_TYPE_CHECK_CLASS_TYPE ((klass), \
+   PHOTOS_TYPE_SEARCH_PROVIDER))
+
+#define PHOTOS_SEARCH_PROVIDER_GET_CLASS(obj) \
+  (G_TYPE_INSTANCE_GET_CLASS ((obj), \
+   PHOTOS_TYPE_SEARCH_PROVIDER, PhotosSearchProviderClass))
+
+#define PHOTOS_SEARCH_PROVIDER_PATH_SUFFIX "/SearchProvider"
+
+typedef struct _PhotosSearchProvider      PhotosSearchProvider;
+typedef struct _PhotosSearchProviderClass PhotosSearchProviderClass;
+
+GType                        photos_search_provider_get_type          (void) G_GNUC_CONST;
+
+PhotosSearchProvider        *photos_search_provider_new               (void);
+
+gboolean                     photos_search_provider_dbus_export       (PhotosSearchProvider *self,
+                                                                       GDBusConnection *connection,
+                                                                       const gchar *object_path,
+                                                                       GError **error);
+
+void                         photos_search_provider_dbus_unexport     (PhotosSearchProvider *self,
+                                                                       GDBusConnection *connection,
+                                                                       const gchar *object_path);
+
+G_END_DECLS
+
+#endif /* PHOTOS_SEARCH_PROVIDER_H */


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