[gnome-terminal/wip/search: 2/2] server: Add a search provider
- From: Debarshi Ray <debarshir src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-terminal/wip/search: 2/2] server: Add a search provider
- Date: Fri, 6 Dec 2013 08:52:41 +0000 (UTC)
commit 7b40fc183f0dd76c062290e61accc7c6de28c4bd
Author: Debarshi Ray <debarshir gnome org>
Date: Wed Dec 4 17:05:05 2013 +0100
server: Add a search provider
https://bugzilla.gnome.org/show_bug.cgi?id=711075
src/Makefile.am | 14 ++
src/gnome-terminal-search-provider.ini | 5 +
src/org.gnome.ShellSearchProvider2.xml | 87 ++++++++++
src/terminal-app.c | 12 ++
src/terminal-search-provider.c | 280 ++++++++++++++++++++++++++++++++
src/terminal-search-provider.h | 51 ++++++
6 files changed, 449 insertions(+), 0 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 48c3ad3..22a2128 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -12,6 +12,9 @@ endif # WITH_NAUTILUS_EXTENSION
dbusservice_DATA = org.gnome.Terminal.service
+searchproviderdir = $(datadir)/gnome-shell/search-providers
+searchprovider_DATA = gnome-terminal-search-provider.ini
+
BUILT_SOURCES = \
terminal-marshal.c \
terminal-marshal.h \
@@ -19,6 +22,8 @@ BUILT_SOURCES = \
terminal-type-builtins.h \
terminal-gdbus-generated.c \
terminal-gdbus-generated.h \
+ terminal-search-provider-gdbus-generated.c \
+ terminal-search-provider-gdbus-generated.h \
terminal-resources.c \
terminal-resources.h \
$(NULL)
@@ -66,6 +71,8 @@ gnome_terminal_server_SOURCES = \
terminal-screen-container.h \
terminal-search-dialog.c \
terminal-search-dialog.h \
+ terminal-search-provider.c \
+ terminal-search-provider.h \
terminal-tab-label.c \
terminal-tab-label.h \
terminal-tabs-menu.c \
@@ -143,6 +150,13 @@ terminal-gdbus-generated.c terminal-gdbus-generated.h: org.gnome.Terminal.xml Ma
--generate-c-code terminal-gdbus-generated \
$<
+terminal-search-provider-gdbus-generated.c terminal-search-provider-gdbus-generated.h:
org.gnome.ShellSearchProvider2.xml Makefile
+ $(AM_V_GEN) $(GDBUS_CODEGEN) \
+ --interface-prefix=org.gnome.Shell \
+ --c-namespace=Terminal \
+ --generate-c-code terminal-search-provider-gdbus-generated \
+ $<
+
terminal-resources.h terminal-resources.c: terminal.gresource.xml Makefile $(shell $(GLIB_COMPILE_RESOURCES)
--generate-dependencies --sourcedir $(srcdir) $(srcdir)/terminal.gresource.xml)
$(AM_V_GEN) XMLLINT=$(XMLLINT) $(GLIB_COMPILE_RESOURCES) --target $@ --sourcedir $(srcdir) --generate
--c-name terminal $<
diff --git a/src/gnome-terminal-search-provider.ini b/src/gnome-terminal-search-provider.ini
new file mode 100644
index 0000000..f4831c1
--- /dev/null
+++ b/src/gnome-terminal-search-provider.ini
@@ -0,0 +1,5 @@
+[Shell Search Provider]
+DesktopId=gnome-terminal
+BusName=org.gnome.Terminal
+ObjectPath=/org/gnome/Terminal
+Version=2
\ No newline at end of file
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/terminal-app.c b/src/terminal-app.c
index cc7c5c6..5b1022d 100644
--- a/src/terminal-app.c
+++ b/src/terminal-app.c
@@ -31,6 +31,7 @@
#include "terminal-accels.h"
#include "terminal-screen.h"
#include "terminal-screen-container.h"
+#include "terminal-search-provider.h"
#include "terminal-window.h"
#include "terminal-profiles-list.h"
#include "terminal-util.h"
@@ -72,6 +73,7 @@ struct _TerminalApp
GtkApplication parent_instance;
GDBusObjectManagerServer *object_manager;
+ TerminalSearchProvider *search_provider;
TerminalSettingsList *profiles_list;
@@ -345,6 +347,9 @@ terminal_app_init (TerminalApp *app)
gtk_window_set_default_icon_name (GNOME_TERMINAL_ICON_NAME);
+ /* Search provider */
+ app->search_provider = terminal_search_provider_new ();
+
/* Desktop proxy settings */
app->system_proxy_settings = g_settings_new (SYSTEM_PROXY_SETTINGS_SCHEMA);
@@ -389,6 +394,7 @@ terminal_app_finalize (GObject *object)
g_object_unref (app->global_settings);
g_object_unref (app->desktop_interface_settings);
g_object_unref (app->system_proxy_settings);
+ g_object_unref (app->search_provider);
terminal_accels_shutdown ();
@@ -411,6 +417,9 @@ terminal_app_dbus_register (GApplication *application,
error))
return FALSE;
+ if (!terminal_search_provider_dbus_register (app->search_provider, connection, object_path, error))
+ return FALSE;
+
object = terminal_object_skeleton_new (TERMINAL_FACTORY_OBJECT_PATH);
factory = terminal_factory_impl_new ();
terminal_object_skeleton_set_factory (object, factory);
@@ -420,6 +429,7 @@ terminal_app_dbus_register (GApplication *application,
/* And export the object */
g_dbus_object_manager_server_set_connection (app->object_manager, connection);
+
return TRUE;
}
@@ -436,6 +446,8 @@ terminal_app_dbus_unregister (GApplication *application,
app->object_manager = NULL;
}
+ terminal_search_provider_dbus_unregister (app->search_provider, connection, object_path);
+
G_APPLICATION_CLASS (terminal_app_parent_class)->dbus_unregister (application,
connection,
object_path);
diff --git a/src/terminal-search-provider.c b/src/terminal-search-provider.c
new file mode 100644
index 0000000..074e180
--- /dev/null
+++ b/src/terminal-search-provider.c
@@ -0,0 +1,280 @@
+/*
+ * 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 3 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, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "config.h"
+
+#include <string.h>
+
+#include "terminal-app.h"
+#include "terminal-libgsystem.h"
+#include "terminal-screen-container.h"
+#include "terminal-search-provider.h"
+#include "terminal-search-provider-gdbus-generated.h"
+#include "terminal-window.h"
+
+struct _TerminalSearchProvider
+{
+ GObject parent;
+
+ GCancellable *cancellable;
+ GHashTable *hash;
+ TerminalSearchProvider2 *skeleton;
+};
+
+struct _TerminalSearchProviderClass
+{
+ GObjectClass parent_class;
+};
+
+G_DEFINE_TYPE (TerminalSearchProvider, terminal_search_provider, G_TYPE_OBJECT)
+
+static gboolean
+handle_get_initial_result_set_cb (TerminalSearchProvider2 *skeleton,
+ GDBusMethodInvocation *invocation,
+ char **terms,
+ gpointer user_data)
+{
+ TerminalSearchProvider *provider = TERMINAL_SEARCH_PROVIDER (user_data);
+ GList *l, *screens = NULL, *windows;
+ GPtrArray *results;
+ TerminalApp *app;
+
+ app = terminal_app_get ();
+ g_application_hold (G_APPLICATION (app));
+ g_cancellable_reset (provider->cancellable);
+
+ windows = gtk_application_get_windows (GTK_APPLICATION (app));
+ for (l = windows; l != NULL; l = l->next)
+ {
+ TerminalWindow *window = TERMINAL_WINDOW (l->data);
+ GList *c, *containers;
+
+ containers = terminal_window_list_screen_containers (window);
+ for (c = containers; c != NULL; c = c->next)
+ {
+ TerminalScreenContainer *container = TERMINAL_SCREEN_CONTAINER (c->data);
+ TerminalScreen *screen;
+
+ screen = terminal_screen_container_get_screen (container);
+ screens = g_list_prepend (screens, screen);
+ }
+ }
+
+ results = g_ptr_array_new ();
+
+ for (l = screens; l != NULL; l = l->next)
+ {
+ TerminalScreen *screen = TERMINAL_SCREEN (l->data);
+ gs_free char *cwd = NULL;
+ gboolean matches = TRUE;
+ guint i;
+
+ cwd = terminal_screen_get_current_dir (screen);
+ for (i = 0; terms[i] != NULL; i++)
+ {
+ if (strstr (cwd, terms[i]) == NULL)
+ {
+ matches = FALSE;
+ break;
+ }
+ }
+
+ if (matches)
+ {
+ gs_free gchar *key;
+
+ key = g_strdup_printf ("%p", screen);
+ g_hash_table_insert (provider->hash, g_strdup (key), screen);
+ g_ptr_array_add (results, g_strdup (key));
+ }
+ }
+
+ g_ptr_array_add (results, NULL);
+ terminal_search_provider2_complete_get_initial_result_set (skeleton,
+ invocation,
+ (const char *const *) results->pdata);
+
+ g_ptr_array_free (results, TRUE);
+ g_application_release (G_APPLICATION (app));
+ return TRUE;
+}
+
+static gboolean
+handle_get_subsearch_result_set_cb (TerminalSearchProvider2 *skeleton,
+ GDBusMethodInvocation *invocation,
+ char **previous_results,
+ char **terms,
+ gpointer user_data)
+{
+ TerminalSearchProvider *provider = TERMINAL_SEARCH_PROVIDER (user_data);
+ return TRUE;
+}
+
+static gboolean
+handle_get_result_metas_cb (TerminalSearchProvider2 *skeleton,
+ GDBusMethodInvocation *invocation,
+ char **results,
+ gpointer user_data)
+{
+ TerminalSearchProvider *provider = TERMINAL_SEARCH_PROVIDER (user_data);
+ GVariantBuilder builder;
+ TerminalApp *app;
+ guint i;
+
+ app = terminal_app_get ();
+ g_application_hold (G_APPLICATION (app));
+ g_cancellable_reset (provider->cancellable);
+
+ g_variant_builder_init (&builder, G_VARIANT_TYPE ("aa{sv}"));
+
+ for (i = 0; results[i] != NULL; i++)
+ {
+ TerminalScreen *screen;
+ const char *title;
+ gpointer value;
+
+ value = g_hash_table_lookup (provider->hash, results[i]);
+ if (!TERMINAL_IS_SCREEN (value))
+ continue;
+
+ screen = TERMINAL_SCREEN (value);
+ title = terminal_screen_get_title (screen);
+
+ g_variant_builder_open (&builder, G_VARIANT_TYPE ("a{sv}"));
+ g_variant_builder_add (&builder, "{sv}", "id", g_variant_new_string (results[i]));
+ g_variant_builder_add (&builder, "{sv}", "name", g_variant_new_string (title));
+ g_variant_builder_close (&builder);
+ }
+
+ terminal_search_provider2_complete_get_result_metas (skeleton, invocation, g_variant_builder_end
(&builder));
+
+ g_application_release (G_APPLICATION (app));
+ return TRUE;
+}
+
+static gboolean
+handle_activate_result_cb (TerminalSearchProvider2 *skeleton,
+ GDBusMethodInvocation *invocation,
+ char *identifier,
+ char **terms,
+ guint timestamp,
+ gpointer user_data)
+{
+ TerminalSearchProvider *provider = TERMINAL_SEARCH_PROVIDER (user_data);
+ GtkWidget *toplevel;
+ TerminalApp *app;
+ TerminalScreen *screen;
+ gpointer value;
+
+ app = terminal_app_get ();
+ g_application_hold (G_APPLICATION (app));
+ g_cancellable_reset (provider->cancellable);
+
+ value = g_hash_table_lookup (provider->hash, identifier);
+ if (TERMINAL_IS_SCREEN (value))
+ goto out;
+
+ screen = TERMINAL_SCREEN (value);
+ toplevel = gtk_widget_get_toplevel (GTK_WIDGET (screen));
+ if (!gtk_widget_is_toplevel (toplevel))
+ goto out;
+
+ gtk_window_present_with_time (GTK_WINDOW (toplevel), timestamp);
+
+ out:
+ terminal_search_provider2_complete_activate_result (skeleton, invocation);
+ g_application_release (G_APPLICATION (app));
+ return TRUE;
+}
+
+static gboolean
+handle_launch_search_cb (TerminalSearchProvider2 *skeleton,
+ GDBusMethodInvocation *invocation,
+ char **terms,
+ guint timestamp,
+ gpointer user_data)
+{
+ terminal_search_provider2_complete_launch_search (skeleton, invocation);
+ return TRUE;
+}
+
+static void
+terminal_search_provider_init (TerminalSearchProvider *provider)
+{
+ provider->cancellable = g_cancellable_new ();
+ provider->hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
+ provider->skeleton = terminal_search_provider2_skeleton_new ();
+
+ g_signal_connect (provider->skeleton, "handle-get-initial-result-set",
+ G_CALLBACK (handle_get_initial_result_set_cb), provider);
+ g_signal_connect (provider->skeleton, "handle-get-subsearch-result-set",
+ G_CALLBACK (handle_get_subsearch_result_set_cb), provider);
+ g_signal_connect (provider->skeleton, "handle-get-result-metas",
+ G_CALLBACK (handle_get_result_metas_cb), provider);
+ g_signal_connect (provider->skeleton, "handle-activate-result",
+ G_CALLBACK (handle_activate_result_cb), provider);
+ g_signal_connect (provider->skeleton, "handle-launch-search",
+ G_CALLBACK (handle_launch_search_cb), provider);
+}
+
+static void
+terminal_search_provider_dispose (GObject *object)
+{
+ TerminalSearchProvider *provider = TERMINAL_SEARCH_PROVIDER (object);
+
+ g_clear_object (&provider->cancellable);
+ g_clear_object (&provider->skeleton);
+ g_clear_pointer (&provider->hash, (GDestroyNotify) g_hash_table_unref);
+
+ G_OBJECT_CLASS (terminal_search_provider_parent_class)->dispose (object);
+}
+
+static void
+terminal_search_provider_class_init (TerminalSearchProviderClass *klass)
+{
+ GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
+
+ gobject_class->dispose = terminal_search_provider_dispose;
+}
+
+TerminalSearchProvider *
+terminal_search_provider_new (void)
+{
+ return g_object_new (TERMINAL_TYPE_SEARCH_PROVIDER, NULL);
+}
+
+gboolean
+terminal_search_provider_dbus_register (TerminalSearchProvider *provider,
+ GDBusConnection *connection,
+ const char *object_path,
+ GError **error)
+{
+ return g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (provider->skeleton),
+ connection,
+ object_path,
+ error);
+}
+
+void
+terminal_search_provider_dbus_unregister (TerminalSearchProvider *provider,
+ GDBusConnection *connection,
+ const char *object_path)
+{
+ if (g_dbus_interface_skeleton_has_connection (G_DBUS_INTERFACE_SKELETON (provider->skeleton), connection))
+ g_dbus_interface_skeleton_unexport_from_connection (G_DBUS_INTERFACE_SKELETON (provider->skeleton),
+ connection);
+}
diff --git a/src/terminal-search-provider.h b/src/terminal-search-provider.h
new file mode 100644
index 0000000..719bd3e
--- /dev/null
+++ b/src/terminal-search-provider.h
@@ -0,0 +1,51 @@
+/*
+ * 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 3 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, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef TERMINAL_SEARCH_PROVIDER_H
+#define TERMINAL_SEARCH_PROVIDER_H
+
+#include <glib-object.h>
+#include <gio/gio.h>
+
+G_BEGIN_DECLS
+
+#define TERMINAL_TYPE_SEARCH_PROVIDER (terminal_search_provider_get_type ())
+#define TERMINAL_SEARCH_PROVIDER(object) (G_TYPE_CHECK_INSTANCE_CAST ((object),
TERMINAL_TYPE_SEARCH_PROVIDER, TerminalSearchProvider))
+#define TERMINAL_SEARCH_PROVIDER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass),
TERMINAL_TYPE_SEARCH_PROVIDER, TerminalSearchProviderClass))
+#define TERMINAL_IS_SEARCH_PROVIDER(object) (G_TYPE_CHECK_INSTANCE_TYPE ((object),
TERMINAL_TYPE_SEARCH_PROVIDER))
+#define TERMINAL_IS_SEARCH_PROVIDER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass),
TERMINAL_TYPE_SEARCH_PROVIDER))
+#define TERMINAL_SEARCH_PROVIDER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj),
TERMINAL_TYPE_SEARCH_PROVIDER, TerminalSearchProviderClass))
+
+typedef struct _TerminalSearchProvider TerminalSearchProvider;
+typedef struct _TerminalSearchProviderClass TerminalSearchProviderClass;
+
+GType terminal_search_provider_get_type (void);
+
+TerminalSearchProvider *terminal_search_provider_new (void);
+
+gboolean terminal_search_provider_dbus_register (TerminalSearchProvider *provider,
+ GDBusConnection *connection,
+ const char *object_path,
+ GError **error);
+
+void terminal_search_provider_dbus_unregister (TerminalSearchProvider *provider,
+ GDBusConnection *connection,
+ const char *object_path);
+
+G_END_DECLS
+
+#endif /* !TERMINAL_SEARCH_PROVIDER_H */
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]