[hotssh] Add infrastructure for a GNOME Shell search provider



commit ca5dab6f0efc56d5f43a8292d41c54db00e0d0ea
Author: Owen W. Taylor <otaylor fishsoup net>
Date:   Thu Nov 14 17:41:42 2013 -0500

    Add infrastructure for a GNOME Shell search provider
    
    Add a stubbed-out GNOME Shell search provider that currently just matches
    for foo.example.com and does nothing when activated.
    
    The XML for the search provider interface is imported to avoid creating
    a build-dependency on GNOME Shell.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=712345

 Makefile-src.am                        |   23 +++-
 src/hotssh-app.c                       |    5 +
 src/hotssh-search-provider.c           |  267 ++++++++++++++++++++++++++++++++
 src/hotssh-search-provider.h           |   33 ++++
 src/org.gnome.ShellSearchProvider2.xml |   87 +++++++++++
 5 files changed, 414 insertions(+), 1 deletions(-)
---
diff --git a/Makefile-src.am b/Makefile-src.am
index bf859cd..a041221 100644
--- a/Makefile-src.am
+++ b/Makefile-src.am
@@ -1,16 +1,34 @@
 bin_PROGRAMS += hotssh
 
+hotssh_dbus_h_files =
+hotssh_dbus_c_files =
+
+hotssh-search-glue.c hotssh-search-glue.h: Makefile-src.am src/org.gnome.ShellSearchProvider2.xml
+       $(AM_V_GEN)gdbus-codegen                                        \
+               --c-namespace=HotSshSearch                              \
+               --interface-prefix=org.gnome                            \
+               --generate-c-code=hotssh-search-glue                    \
+               $(srcdir)/src/org.gnome.ShellSearchProvider2.xml
+hotssh_dbus_h_files += hotssh-search-glue.h
+hotssh_dbus_c_files += hotssh-search-glue.c
+
+BUILT_SOURCES += $(hotssh_dbus_h_files) $(hotssh_dbus_c_files)
+
 hotssh_headers = $(addprefix src/, \
        hotssh-app.h \
+       hotssh-search-provider.h \
        hotssh-tab.h \
        hotssh-password-interaction.h \
        hotssh-win.h \
        hotssh-prefs.h \
-       )
+       ) \
+       $(hotssh_dbus_h_files)
 
 hotssh_SOURCES = $(hotssh_headers) \
+       $(hotssh_dbus_c_files) \
        src/main.c \
        src/hotssh-app.c \
+       src/hotssh-search-provider.c \
        src/hotssh-tab.c \
        src/hotssh-password-interaction.c \
        src/hotssh-win.c \
@@ -34,6 +52,9 @@ gsettingsschema_DATA = src/org.gnome.hotssh.gschema.xml
 desktopdir=$(datadir)/applications
 desktop_DATA = src/hotssh.desktop
 
+searchproviderdir = $(datadir)/gnome-shell/search-providers
+searchprovider_DATA = src/hotssh-search-provider.ini
+
 scalableiconsdir = $(datadir)/icons/hicolor/scalable/apps/
 scalableicons_DATA = src/hotssh.svg
 
diff --git a/src/hotssh-app.c b/src/hotssh-app.c
index f7eecb9..644c52c 100644
--- a/src/hotssh-app.c
+++ b/src/hotssh-app.c
@@ -19,6 +19,7 @@
  */
 
 #include "hotssh-app.h"
+#include "hotssh-search-provider.h"
 #include "hotssh-win.h"
 #include "hotssh-prefs.h"
 
@@ -27,6 +28,7 @@
 struct _HotSshApp
 {
   GtkApplication parent;
+  HotSshSearchProvider *search_provider;
 };
 
 struct _HotSshAppClass
@@ -71,11 +73,14 @@ static GActionEntry app_entries[] =
 static void
 hotssh_app_startup (GApplication *app)
 {
+  HotSshApp *hotssh_app = HOTSSH_APP (app);
   GtkBuilder *builder;
   GMenuModel *app_menu;
 
   G_APPLICATION_CLASS (hotssh_app_parent_class)->startup (app);
 
+  hotssh_app->search_provider = hotssh_search_provider_new (hotssh_app);
+
   g_action_map_add_action_entries (G_ACTION_MAP (app),
                                    app_entries, G_N_ELEMENTS (app_entries),
                                    app);
diff --git a/src/hotssh-search-provider.c b/src/hotssh-search-provider.c
new file mode 100644
index 0000000..285e5a3
--- /dev/null
+++ b/src/hotssh-search-provider.c
@@ -0,0 +1,267 @@
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
+ *
+ * Copyright (C) 2013 Colin Walters <walters verbum org>
+ * Copyright (C) 2013 Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#include <string.h>
+
+#include "hotssh-search-provider.h"
+#include "hotssh-search-glue.h"
+
+struct _HotSshSearchProvider
+{
+  GObject parent;
+};
+
+struct _HotSshSearchProviderClass
+{
+  GObjectClass parent_class;
+};
+
+typedef struct _HotSshSearchProviderPrivate HotSshSearchProviderPrivate;
+
+struct _HotSshSearchProviderPrivate
+{
+  HotSshApp *app;
+  guint owner_id;
+  HotSshSearchShellSearchProvider2 *skeleton;
+};
+
+G_DEFINE_TYPE_WITH_PRIVATE(HotSshSearchProvider, hotssh_search_provider, G_TYPE_OBJECT)
+
+static char **
+get_results (HotSshSearchProvider *search_provider,
+             char                **terms)
+{
+  char **term;
+  gboolean match = TRUE;
+
+  for (term = terms; *term; term++)
+    {
+      if (strstr ("foo.example.com", *term) == NULL)
+        match = FALSE;
+    }
+
+  if (match)
+    {
+      const char * const results[] = { "foo.example.com", NULL };
+      return g_strdupv ((char **)results);
+    }
+  else
+    {
+      const char * const results[] = { NULL };
+      return g_strdupv ((char **)results);
+    }
+}
+
+static gboolean
+handle_get_initial_result_set (HotSshSearchShellSearchProvider2 *skeleton,
+                               GDBusMethodInvocation            *invocation,
+                               char                            **terms,
+                               HotSshSearchProvider             *search_provider)
+{
+  char **results = get_results (search_provider, terms);
+  hot_ssh_search_shell_search_provider2_complete_get_initial_result_set (skeleton, invocation, (const char * 
const *)results);
+  g_strfreev (results);
+
+  return TRUE;
+}
+
+static gboolean
+handle_get_subsearch_result_set (HotSshSearchShellSearchProvider2 *skeleton,
+                                 GDBusMethodInvocation            *invocation,
+                                 char                            **previous_results,
+                                 char                            **terms,
+                                 HotSshSearchProvider             *search_provider)
+{
+  char **results = get_results (search_provider, terms);
+  hot_ssh_search_shell_search_provider2_complete_get_subsearch_result_set (skeleton, invocation, (const char 
* const *)results);
+  g_strfreev (results);
+
+  return TRUE;
+}
+
+static gboolean
+handle_get_result_metas (HotSshSearchShellSearchProvider2 *skeleton,
+                         GDBusMethodInvocation            *invocation,
+                         char                            **identifiers,
+                         HotSshSearchProvider              *search_provider)
+{
+  GVariantBuilder *b1;
+  GVariant *metas;
+  char **p;
+
+  b1 = g_variant_builder_new (G_VARIANT_TYPE ("aa{sv}"));
+  for (p = identifiers; *p; p++)
+    {
+      const char *id = *p;
+      if (g_strcmp0 (id, "foo.example.com") == 0)
+        {
+          GVariantBuilder *b2 = g_variant_builder_new (G_VARIANT_TYPE ("a{sv}"));
+          GIcon *icon;
+          GVariant *serialized_icon;
+          gchar *string_icon;
+
+          g_variant_builder_add (b2, "{sv}", "id", g_variant_new_string (id));
+          g_variant_builder_add (b2, "{sv}", "name", g_variant_new_string ("foo.example.com"));
+
+          icon = g_themed_icon_new ("gnome-terminal");
+
+          serialized_icon = g_icon_serialize (icon);
+          g_variant_builder_add (b2, "{sv}", "icon", serialized_icon);
+          g_variant_unref (serialized_icon);
+          string_icon = g_icon_to_string (icon);
+          g_variant_builder_add (b2, "{sv}", "gicon", g_variant_new_string (string_icon));
+          g_free (string_icon);
+
+          g_object_unref (icon);
+
+          g_variant_builder_add_value (b1, g_variant_builder_end (b2));
+        }
+
+    }
+
+  metas = g_variant_builder_end (b1);
+
+  hot_ssh_search_shell_search_provider2_complete_get_result_metas (skeleton, invocation, metas);
+  return TRUE;
+}
+
+static gboolean
+handle_activate_result (HotSshSearchShellSearchProvider2 *skeleton,
+                        GDBusMethodInvocation            *invocation,
+                        char                             *identifier,
+                        char                            **terms,
+                        guint                             timestamp,
+                        HotSshSearchProvider             *search_provider)
+{
+  if (g_strcmp0 (identifier, "foo.example.com") == 0)
+    {
+      g_print ("Open up the Foo server!\n");
+    }
+
+  hot_ssh_search_shell_search_provider2_complete_activate_result (skeleton, invocation);
+  return TRUE;
+}
+
+static gboolean
+handle_launch_search (HotSshSearchShellSearchProvider2 *skeleton,
+                      GDBusMethodInvocation            *invocation,
+                      char                            **terms,
+                      guint                             timestamp,
+                      HotSshSearchProvider              *search_provider)
+{
+  return FALSE;
+}
+
+static void
+on_bus_acquired (GDBusConnection *connection,
+                 const gchar *name,
+                 gpointer user_data)
+{
+
+  HotSshSearchProvider *search_provider = user_data;
+  HotSshSearchProviderPrivate *priv = hotssh_search_provider_get_instance_private (search_provider);
+
+  priv->skeleton = hot_ssh_search_shell_search_provider2_skeleton_new ();
+  g_signal_connect (priv->skeleton,
+                    "handle-get-initial-result-set",
+                    G_CALLBACK (handle_get_initial_result_set), search_provider);
+  g_signal_connect (priv->skeleton,
+                    "handle-get-subsearch-result-set",
+                    G_CALLBACK (handle_get_subsearch_result_set), search_provider);
+  g_signal_connect (priv->skeleton,
+                    "handle-get-result-metas",
+                    G_CALLBACK (handle_get_result_metas), search_provider);
+  g_signal_connect (priv->skeleton,
+                    "handle-activate-result",
+                    G_CALLBACK (handle_activate_result), search_provider);
+  g_signal_connect (priv->skeleton,
+                    "handle-launch-search",
+                    G_CALLBACK (handle_launch_search), search_provider);
+
+  g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (priv->skeleton),
+                                    connection,
+                                    "/org/gnome/hotssh/SearchProvider",
+                                    NULL);
+}
+
+static void
+on_name_acquired (GDBusConnection *connection,
+                  const gchar *name,
+                  gpointer user_data)
+{
+}
+
+static void
+on_name_lost (GDBusConnection *connection,
+              const gchar *name,
+              gpointer user_data)
+{
+}
+
+static void
+hotssh_search_provider_init (HotSshSearchProvider *search_provider)
+{
+  HotSshSearchProviderPrivate *priv = hotssh_search_provider_get_instance_private (search_provider);
+
+  priv->owner_id = g_bus_own_name (G_BUS_TYPE_SESSION,
+                                   "org.gnome.HotSsh.SearchProvider",
+                                   G_BUS_NAME_OWNER_FLAGS_NONE,
+                                   on_bus_acquired,
+                                   on_name_acquired,
+                                   on_name_lost,
+                                   search_provider,
+                                   NULL);
+}
+
+static void
+hotssh_search_provider_dispose (GObject *object)
+{
+  HotSshSearchProvider *search_provider = HOTSSH_SEARCH_PROVIDER (object);
+  HotSshSearchProviderPrivate *priv = hotssh_search_provider_get_instance_private (search_provider);
+
+  if (priv->owner_id)
+    {
+      g_bus_unown_name (priv->owner_id);
+      priv->owner_id = 0;
+    }
+
+  G_OBJECT_CLASS (hotssh_search_provider_parent_class)->dispose (object);
+}
+
+static void
+hotssh_search_provider_class_init (HotSshSearchProviderClass *class)
+{
+  G_OBJECT_CLASS (class)->dispose = hotssh_search_provider_dispose;
+}
+
+HotSshSearchProvider *
+hotssh_search_provider_new (HotSshApp *app)
+{
+  HotSshSearchProvider *search_provider;
+  HotSshSearchProviderPrivate *priv;
+
+  search_provider = g_object_new (HOTSSH_TYPE_SEARCH_PROVIDER, NULL);
+  priv = hotssh_search_provider_get_instance_private (search_provider);
+
+  priv->app = app;
+
+  return search_provider;
+}
diff --git a/src/hotssh-search-provider.h b/src/hotssh-search-provider.h
new file mode 100644
index 0000000..c6a5427
--- /dev/null
+++ b/src/hotssh-search-provider.h
@@ -0,0 +1,33 @@
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
+ *
+ * Copyright (C) 2013 Colin Walters <walters verbum org>
+ * Copyright (C) 2013 Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#pragma once
+
+#include "hotssh-app.h"
+
+#define HOTSSH_TYPE_SEARCH_PROVIDER (hotssh_search_provider_get_type ())
+#define HOTSSH_SEARCH_PROVIDER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), HOTSSH_TYPE_SEARCH_PROVIDER, 
HotSshSearchProvider))
+
+typedef struct _HotSshSearchProvider          HotSshSearchProvider;
+typedef struct _HotSshSearchProviderClass     HotSshSearchProviderClass;
+
+GType                   hotssh_search_provider_get_type     (void);
+HotSshSearchProvider   *hotssh_search_provider_new          (HotSshApp *app);
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>


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