[nautilus] shell-provider: don't use a separate binary



commit 9f90af0138ee883e2cd4ac1d9620b6c03094819d
Author: Cosimo Cecchi <cosimoc gnome org>
Date:   Wed Dec 5 22:09:57 2012 -0500

    shell-provider: don't use a separate binary
    
    Since NautilusApplication is a service, we can now handle searches and
    windows coming and going indipendently just fine.
    
    This also allows us to launch a search directly from the search provider
    very easily.

 data/Makefile.am                                  |    2 +-
 data/org.gnome.Nautilus.SearchProvider.service.in |    2 +-
 src/Makefile.am                                   |   38 ++----
 src/nautilus-application.c                        |   52 +++++++
 src/nautilus-shell-search-provider.c              |  147 +++++++++------------
 src/nautilus-shell-search-provider.h              |   38 ++++++
 6 files changed, 168 insertions(+), 111 deletions(-)
---
diff --git a/data/Makefile.am b/data/Makefile.am
index b17ece0..a0127e9 100644
--- a/data/Makefile.am
+++ b/data/Makefile.am
@@ -34,7 +34,7 @@ org.freedesktop.FileManager1.service: org.freedesktop.FileManager1.service.in Ma
 	$(AM_V_GEN) sed -e "s|\ bindir\@|$(bindir)|" $< > $@
 
 org.gnome.Nautilus.SearchProvider.service: org.gnome.Nautilus.SearchProvider.service.in Makefile
-	$(AM_V_GEN) sed -e "s|\ libexecdir\@|$(libexecdir)|" $< > $@
+	$(AM_V_GEN) sed -e "s|\ bindir\@|$(bindir)|" $< > $@
 
 searchproviderdir = $(datadir)/gnome-shell/search-providers
 searchprovider_DATA = nautilus-search-provider.ini
diff --git a/data/org.gnome.Nautilus.SearchProvider.service.in b/data/org.gnome.Nautilus.SearchProvider.service.in
index 4b78557..85f5a66 100644
--- a/data/org.gnome.Nautilus.SearchProvider.service.in
+++ b/data/org.gnome.Nautilus.SearchProvider.service.in
@@ -1,3 +1,3 @@
 [D-BUS Service]
 Name=org.gnome.Nautilus.SearchProvider
-Exec= libexecdir@/nautilus-shell-search-provider
+Exec= bindir@/nautilus --no-default-window
diff --git a/src/Makefile.am b/src/Makefile.am
index ecac617..53dde97 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -8,7 +8,6 @@ bin_PROGRAMS=					\
 
 libexec_PROGRAMS=				\
 	nautilus-convert-metadata		\
-	nautilus-shell-search-provider		\
 	$(NULL)
 
 INCLUDES =							\
@@ -57,6 +56,18 @@ $(dbus_freedesktop_built_sources) : Makefile.am $(top_srcdir)/data/freedesktop-d
 		$(top_srcdir)/data/freedesktop-dbus-interfaces.xml			\
 		$(NULL)
 
+dbus_shell_search_provider_built_sources =		\
+	nautilus-shell-search-provider-generated.c	\
+	nautilus-shell-search-provider-generated.h
+
+$(dbus_shell_search_provider_built_sources) : Makefile.am $(top_srcdir)/data/shell-search-provider-dbus-interfaces.xml
+	gdbus-codegen									\
+		--interface-prefix org.gnome.						\
+		--c-namespace Nautilus							\
+		--generate-c-code nautilus-shell-search-provider-generated		\
+		$(top_srcdir)/data/shell-search-provider-dbus-interfaces.xml		\
+		$(NULL)
+
 UI_FILES = \
 	nautilus-app-menu.ui			\
 	nautilus-bookmarks-window.ui		\
@@ -119,6 +130,7 @@ nautilus-enum-types.c: $(headers) Makefile
 
 nautilus_built_sources = \
 	$(dbus_freedesktop_built_sources) \
+	$(dbus_shell_search_provider_built_sources) \
 	nautilus-resources.c \
 	nautilus-resources.h \
 	nautilus-enum-types.h \
@@ -183,6 +195,8 @@ nautilus_SOURCES = \
 	nautilus-query-editor.h			\
 	nautilus-self-check-functions.c 	\
 	nautilus-self-check-functions.h 	\
+	nautilus-shell-search-provider.h	\
+	nautilus-shell-search-provider.c	\
 	nautilus-special-location-bar.c		\
 	nautilus-special-location-bar.h		\
 	nautilus-toolbar.c			\
@@ -233,28 +247,6 @@ nautilus_convert_metadata_SOURCES= \
 	nautilus-convert-metadata.c	\
 	$(NULL)
 
-dbus_shell_search_provider_built_sources =		\
-	nautilus-shell-search-provider-generated.c	\
-	nautilus-shell-search-provider-generated.h
-
-$(dbus_shell_search_provider_built_sources) : Makefile.am $(top_srcdir)/data/shell-search-provider-dbus-interfaces.xml
-	gdbus-codegen									\
-		--interface-prefix org.gnome.						\
-		--c-namespace Nautilus							\
-		--generate-c-code nautilus-shell-search-provider-generated		\
-		$(top_srcdir)/data/shell-search-provider-dbus-interfaces.xml		\
-		$(NULL)
-
-nautilus_shell_search_provider_SOURCES = 		\
-	nautilus-bookmark-list.h			\
-	nautilus-bookmark-list.c			\
-	nautilus-shell-search-provider.c		\
-	$(NULL)
-
-nodist_nautilus_shell_search_provider_SOURCES =		\
-	$(dbus_shell_search_provider_built_sources)	\
-	$(NULL)
-
 BUILT_SOURCES = 					\
 	$(nautilus_built_sources) 			\
 	$(dbus_shell_search_provider_built_sources) 	\
diff --git a/src/nautilus-application.c b/src/nautilus-application.c
index 81f2d14..ffe6592 100644
--- a/src/nautilus-application.c
+++ b/src/nautilus-application.c
@@ -46,6 +46,7 @@
 #include "nautilus-previewer.h"
 #include "nautilus-progress-ui-handler.h"
 #include "nautilus-self-check-functions.h"
+#include "nautilus-shell-search-provider.h"
 #include "nautilus-window.h"
 #include "nautilus-window-private.h"
 #include "nautilus-window-slot.h"
@@ -113,6 +114,8 @@ struct _NautilusApplicationPriv {
 	NautilusBookmarkList *bookmark_list;
 
 	GtkWidget *connect_server_window;
+
+	NautilusShellSearchProvider *search_provider;
 };
 
 NautilusBookmarkList *
@@ -907,12 +910,58 @@ action_quit (GSimpleAction *action,
 	g_list_foreach (windows, (GFunc) nautilus_window_close, NULL);
 }
 
+static void
+action_search (GSimpleAction *action,
+	       GVariant *parameter,
+	       gpointer user_data)
+{
+	GtkApplication *application = user_data;
+	const gchar *string, *uri;
+	NautilusQuery *query;
+	NautilusDirectory *directory;
+	gchar *search_uri;
+	NautilusWindow *window;
+	GtkWindow *cur_window;
+	GFile *location;
+
+	g_variant_get (parameter, "(ss)", &uri, &string);
+
+	if (strlen (string) == 0 ||
+	    strlen (uri) == 0) {
+		return;
+	}
+
+	query = nautilus_query_new ();
+	nautilus_query_set_location (query, uri);
+	nautilus_query_set_text (query, string);
+
+	search_uri = nautilus_search_directory_generate_new_uri ();
+	location = g_file_new_for_uri (search_uri);
+	g_free (search_uri);
+
+	directory = nautilus_directory_get (location);
+	nautilus_search_directory_set_query (NAUTILUS_SEARCH_DIRECTORY (directory), query);
+
+	cur_window = gtk_application_get_active_window (application);
+	window = nautilus_application_create_window (NAUTILUS_APPLICATION (application),
+						     cur_window ?
+						     gtk_window_get_screen (cur_window) :
+						     gdk_screen_get_default ());
+
+	nautilus_window_slot_open_location (nautilus_window_get_active_slot (window), location, 0);
+
+	nautilus_directory_unref (directory);
+	g_object_unref (query);
+	g_object_unref (location);
+}
+
 static GActionEntry app_entries[] = {
 	{ "new-window", action_new_window, NULL, NULL, NULL },
 	{ "connect-to-server", action_connect_to_server, NULL, NULL, NULL },
 	{ "enter-location", action_enter_location, NULL, NULL, NULL },
 	{ "bookmarks", action_bookmarks, NULL, NULL, NULL },
 	{ "preferences", action_preferences, NULL, NULL, NULL },
+	{ "search", action_search, "(ss)", NULL, NULL },
 	{ "about", action_about, NULL, NULL, NULL },
 	{ "help", action_help, NULL, NULL, NULL },
 	{ "quit", action_quit, NULL, NULL, NULL },
@@ -976,6 +1025,7 @@ nautilus_application_finalize (GObject *object)
 
 	g_clear_object (&application->priv->dbus_manager);
 	g_clear_object (&application->priv->fdb_manager);
+	g_clear_object (&application->priv->search_provider);
 
 	notify_uninit ();
 
@@ -1454,7 +1504,9 @@ nautilus_application_startup (GApplication *app)
 	g_signal_connect_object (self->priv->volume_monitor, "mount_added",
 				 G_CALLBACK (mount_added_callback), self, 0);
 
+	/* Bookmarks and search */
 	self->priv->bookmark_list = nautilus_bookmark_list_new ();
+	self->priv->search_provider = nautilus_shell_search_provider_new ();
 
 	/* Check the user's .nautilus directories and post warnings
 	 * if there are problems.
diff --git a/src/nautilus-shell-search-provider.c b/src/nautilus-shell-search-provider.c
index 58c8228..7d12d3b 100644
--- a/src/nautilus-shell-search-provider.c
+++ b/src/nautilus-shell-search-provider.c
@@ -35,16 +35,13 @@
 #include <libnautilus-private/nautilus-search-provider.h>
 #include <libnautilus-private/nautilus-ui-utilities.h>
 
+#include "nautilus-application.h"
 #include "nautilus-bookmark-list.h"
 #include "nautilus-shell-search-provider-generated.h"
-
-#define SEARCH_PROVIDER_INACTIVITY_TIMEOUT 12000 /* milliseconds */
-
-typedef GApplicationClass NautilusShellSearchProviderAppClass;
-typedef struct _NautilusShellSearchProviderApp NautilusShellSearchProviderApp;
+#include "nautilus-shell-search-provider.h"
 
 typedef struct {
-  NautilusShellSearchProviderApp *self;
+  NautilusShellSearchProvider *self;
 
   NautilusSearchEngine *engine;
   NautilusQuery *query;
@@ -55,8 +52,8 @@ typedef struct {
   gint64 start_time;
 } PendingSearch;
 
-struct _NautilusShellSearchProviderApp {
-  GApplication parent;
+struct _NautilusShellSearchProvider {
+  GObject parent;
 
   guint name_owner_id;
   GDBusObjectManagerServer *object_manager;
@@ -70,13 +67,7 @@ struct _NautilusShellSearchProviderApp {
   GVolumeMonitor *volumes;
 };
 
-GType nautilus_shell_search_provider_app_get_type (void);
-
-#define NAUTILUS_TYPE_SHELL_SEARCH_PROVIDER_APP nautilus_shell_search_provider_app_get_type()
-#define NAUTILUS_SHELL_SEARCH_PROVIDER_APP(obj) \
-  (G_TYPE_CHECK_INSTANCE_CAST ((obj), NAUTILUS_TYPE_SHELL_SEARCH_PROVIDER_APP, NautilusShellSearchProviderApp))
-
-G_DEFINE_TYPE (NautilusShellSearchProviderApp, nautilus_shell_search_provider_app, G_TYPE_APPLICATION)
+G_DEFINE_TYPE (NautilusShellSearchProvider, nautilus_shell_search_provider, G_TYPE_OBJECT)
 
 static GVariant *
 variant_from_pixbuf (GdkPixbuf *pixbuf)
@@ -101,7 +92,7 @@ variant_from_pixbuf (GdkPixbuf *pixbuf)
 }
 
 static gchar *
-get_display_name (NautilusShellSearchProviderApp *self,
+get_display_name (NautilusShellSearchProvider *self,
                   NautilusFile                   *file)
 {
   GFile *location;
@@ -118,7 +109,7 @@ get_display_name (NautilusShellSearchProviderApp *self,
 }
 
 static GIcon *
-get_gicon (NautilusShellSearchProviderApp *self,
+get_gicon (NautilusShellSearchProvider *self,
            NautilusFile                   *file)
 {
   GFile *location;
@@ -149,19 +140,19 @@ pending_search_finish (PendingSearch         *search,
                        GDBusMethodInvocation *invocation,
                        GVariant              *result)
 {
-  NautilusShellSearchProviderApp *self = search->self;
+  NautilusShellSearchProvider *self = search->self;
 
   g_dbus_method_invocation_return_value (invocation, result);
 
   if (search == self->current_search)
     self->current_search = NULL;
 
-  g_application_release (G_APPLICATION (self));
+  g_application_release (g_application_get_default ());
   pending_search_free (search);
 }
 
 static void
-cancel_current_search (NautilusShellSearchProviderApp *self)
+cancel_current_search (NautilusShellSearchProvider *self)
 {
   if (self->current_search != NULL)
     nautilus_search_provider_stop (NAUTILUS_SEARCH_PROVIDER (self->current_search->engine));
@@ -245,7 +236,7 @@ search_error_cb (NautilusSearchEngine *engine,
                  const gchar          *error_message,
                  gpointer              user_data)
 {
-  NautilusShellSearchProviderApp *self = user_data;
+  NautilusShellSearchProvider *self = user_data;
   PendingSearch *search = self->current_search;
 
   g_debug ("*** Search engine search error");
@@ -413,7 +404,7 @@ search_add_volumes_and_bookmarks (PendingSearch *search)
 }
 
 static void
-execute_search (NautilusShellSearchProviderApp *self,
+execute_search (NautilusShellSearchProvider *self,
                 GDBusMethodInvocation          *invocation,
                 gchar                         **terms)
 {
@@ -454,7 +445,7 @@ execute_search (NautilusShellSearchProviderApp *self,
                     G_CALLBACK (search_error_cb), pending_search);
 
   self->current_search = pending_search;
-  g_application_hold (G_APPLICATION (self));
+  g_application_hold (g_application_get_default ());
 
   search_add_volumes_and_bookmarks (pending_search);
 
@@ -474,7 +465,7 @@ handle_get_initial_result_set (NautilusShellSearchProvider2  *skeleton,
                                gchar                        **terms,
                                gpointer                       user_data)
 {
-  NautilusShellSearchProviderApp *self = user_data;
+  NautilusShellSearchProvider *self = user_data;
 
   g_debug ("****** GetInitialResultSet");
   execute_search (self, invocation, terms);
@@ -487,14 +478,14 @@ handle_get_subsearch_result_set (NautilusShellSearchProvider2  *skeleton,
                                  gchar                        **terms,
                                  gpointer                       user_data)
 {
-  NautilusShellSearchProviderApp *self = user_data;
+  NautilusShellSearchProvider *self = user_data;
 
   g_debug ("****** GetSubSearchResultSet");
   execute_search (self, invocation, terms);
 }
 
 typedef struct {
-  NautilusShellSearchProviderApp *self;
+  NautilusShellSearchProvider *self;
 
   gint64 start_time;
   GDBusMethodInvocation *invocation;
@@ -611,7 +602,7 @@ handle_get_result_metas (NautilusShellSearchProvider2  *skeleton,
                          gchar                        **results,
                          gpointer                       user_data)
 {
-  NautilusShellSearchProviderApp *self = user_data;
+  NautilusShellSearchProvider *self = user_data;
   GList *missing_files = NULL;
   const gchar *uri;
   ResultMetasData *data;
@@ -655,13 +646,30 @@ handle_activate_result (NautilusShellSearchProvider2 *skeleton,
                         guint32                       timestamp,
                         gpointer                      user_data)
 {
-  GError *error = NULL;
-  gtk_show_uri (NULL, result, timestamp, &error);
+  GFile *file = g_file_new_for_uri (result);
+  g_application_open (g_application_get_default (), &file, 1, "");
+  g_object_unref (file);
 
-  if (error != NULL) {
-    g_warning ("Unable to activate %s: %s", result, error->message);
-    g_error_free (error);
-  }
+  nautilus_shell_search_provider2_complete_activate_result (skeleton, invocation);
+}
+
+static void
+handle_launch_search (NautilusShellSearchProvider2 *skeleton,
+                      GDBusMethodInvocation *invocation,
+                      gchar **terms,
+                      gpointer user_data)
+{
+  GApplication *app = g_application_get_default ();
+  gchar *string = g_strjoinv (" ", terms);
+  gchar *uri = nautilus_get_home_directory_uri ();
+
+  g_action_group_activate_action (G_ACTION_GROUP (app), "search",
+                                  g_variant_new ("(ss)", uri, string));
+
+  g_free (string);
+  g_free (uri);
+
+  nautilus_shell_search_provider2_complete_launch_search (skeleton, invocation);
 }
 
 static void
@@ -685,7 +693,7 @@ search_provider_bus_acquired_cb (GDBusConnection *connection,
                                  const gchar *name,
                                  gpointer user_data)
 {
-  NautilusShellSearchProviderApp *self = user_data;
+  NautilusShellSearchProvider *self = user_data;
 
   self->object_manager = g_dbus_object_manager_server_new ("/org/gnome/Nautilus/SearchProvider");
   self->skeleton = nautilus_shell_search_provider2_skeleton_new ();
@@ -698,17 +706,21 @@ search_provider_bus_acquired_cb (GDBusConnection *connection,
                     G_CALLBACK (handle_get_result_metas), self);
   g_signal_connect (self->skeleton, "handle-activate-result",
                     G_CALLBACK (handle_activate_result), self);
+  g_signal_connect (self->skeleton, "handle-launch-search",
+                    G_CALLBACK (handle_launch_search), self);
 
   g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (self->skeleton),
                                     connection,
                                     "/org/gnome/Nautilus/SearchProvider", NULL);
   g_dbus_object_manager_server_set_connection (self->object_manager, connection);
+
+  g_application_release (g_application_get_default ());
 }
 
 static void
-search_provider_app_dispose (GObject *obj)
+search_provider_dispose (GObject *obj)
 {
-  NautilusShellSearchProviderApp *self = NAUTILUS_SHELL_SEARCH_PROVIDER_APP (obj);
+  NautilusShellSearchProvider *self = NAUTILUS_SHELL_SEARCH_PROVIDER (obj);
 
   if (self->name_owner_id != 0) {
     g_bus_unown_name (self->name_owner_id);
@@ -724,77 +736,40 @@ search_provider_app_dispose (GObject *obj)
   g_hash_table_destroy (self->metas_cache);
   cancel_current_search (self);
 
-  g_clear_object (&self->bookmarks);
   g_clear_object (&self->volumes);
 
-  G_OBJECT_CLASS (nautilus_shell_search_provider_app_parent_class)->dispose (obj);
+  G_OBJECT_CLASS (nautilus_shell_search_provider_parent_class)->dispose (obj);
 }
 
 static void
-search_provider_app_startup (GApplication *app)
+nautilus_shell_search_provider_init (NautilusShellSearchProvider *self)
 {
-  NautilusShellSearchProviderApp *self = NAUTILUS_SHELL_SEARCH_PROVIDER_APP (app);
-
-  G_APPLICATION_CLASS (nautilus_shell_search_provider_app_parent_class)->startup (app);
-
-  /* hold indefinitely if we're asked to persist */
-  if (g_getenv ("NAUTILUS_SEARCH_PROVIDER_PERSIST") != NULL)
-    g_application_hold (app);
+  self->metas_cache = g_hash_table_new_full (g_str_hash, g_str_equal,
+                                             g_free, (GDestroyNotify) g_variant_unref);
+  self->bookmarks = nautilus_application_get_bookmarks (NAUTILUS_APPLICATION (g_application_get_default ()));
+  self->volumes = g_volume_monitor_get ();
 
+  g_application_hold (g_application_get_default ());
   self->name_owner_id = g_bus_own_name (G_BUS_TYPE_SESSION,
                                         "org.gnome.Nautilus.SearchProvider",
                                         G_BUS_NAME_OWNER_FLAGS_NONE,
                                         search_provider_bus_acquired_cb,
                                         search_provider_name_acquired_cb,
                                         search_provider_name_lost_cb,
-                                        app, NULL);
+                                        self, NULL);
 }
 
 static void
-nautilus_shell_search_provider_app_init (NautilusShellSearchProviderApp *self)
+nautilus_shell_search_provider_class_init (NautilusShellSearchProviderClass *klass)
 {
-  GApplication *app = G_APPLICATION (self);
-
-  g_application_set_inactivity_timeout (app, SEARCH_PROVIDER_INACTIVITY_TIMEOUT);
-  g_application_set_application_id (app, "org.gnome.Nautilus.SearchProvider");
-  g_application_set_flags (app, G_APPLICATION_IS_SERVICE);
-
-  self->metas_cache = g_hash_table_new_full (g_str_hash, g_str_equal,
-                                             g_free, (GDestroyNotify) g_variant_unref);
-  self->bookmarks = nautilus_bookmark_list_new ();
-  self->volumes = g_volume_monitor_get ();
-}
-
-static void
-nautilus_shell_search_provider_app_class_init (NautilusShellSearchProviderAppClass *klass)
-{
-  GApplicationClass *aclass = G_APPLICATION_CLASS (klass);
   GObjectClass *oclass = G_OBJECT_CLASS (klass);
 
-  aclass->startup = search_provider_app_startup;
-  oclass->dispose = search_provider_app_dispose;
+  oclass->dispose = search_provider_dispose;
 }
 
-static GApplication *
-nautilus_shell_search_provider_app_new (void)
+NautilusShellSearchProvider *
+nautilus_shell_search_provider_new (void)
 {
-  return g_object_new (nautilus_shell_search_provider_app_get_type (),
+  return g_object_new (nautilus_shell_search_provider_get_type (),
                        NULL);
 }
-
-int
-main (int   argc,
-      char *argv[])
-{
-  GApplication *app;
-  gint res;
-
-  gtk_init (&argc, &argv);
-
-  app = nautilus_shell_search_provider_app_new ();
-  res = g_application_run (app, argc, argv);
-  g_object_unref (app);
-
-  return res;
-}
-
diff --git a/src/nautilus-shell-search-provider.h b/src/nautilus-shell-search-provider.h
new file mode 100644
index 0000000..8f4dcec
--- /dev/null
+++ b/src/nautilus-shell-search-provider.h
@@ -0,0 +1,38 @@
+/*
+ * nautilus-shell-search-provider.h - Implementation of a GNOME Shell
+ *   search provider
+ *
+ * Copyright (C) 2012 Red Hat, Inc.
+ *
+ * Nautilus 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.
+ *
+ * Nautilus 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., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * Authors: Cosimo Cecchi <cosimoc gnome org>
+ *
+ */
+
+#ifndef __NAUTILUS_SHELL_SEARCH_PROVIDER_H__
+#define __NAUTILUS_SHELL_SEARCH_PROVIDER_H__
+
+#define NAUTILUS_TYPE_SHELL_SEARCH_PROVIDER nautilus_shell_search_provider_get_type()
+#define NAUTILUS_SHELL_SEARCH_PROVIDER(obj) \
+  (G_TYPE_CHECK_INSTANCE_CAST ((obj), NAUTILUS_TYPE_SHELL_SEARCH_PROVIDER, NautilusShellSearchProvider))
+
+typedef struct _NautilusShellSearchProvider NautilusShellSearchProvider;
+typedef GObjectClass NautilusShellSearchProviderClass;
+
+GType nautilus_shell_search_provider_get_type (void);
+NautilusShellSearchProvider * nautilus_shell_search_provider_new (void);
+
+#endif /* __NAUTILUS_SHELL_SEARCH_PROVIDER_H__ */



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