[nautilus] Use g_bus_watch_proxy() to call PackageKit methods.



commit ce766ea529017649b72ea844e73c46e1d73c79b6
Author: Cosimo Cecchi <cosimoc gnome org>
Date:   Sun May 30 19:47:05 2010 +0200

    Use g_bus_watch_proxy() to call PackageKit methods.

 libnautilus-private/nautilus-mime-actions.c |  213 ++++++++++++++------------
 1 files changed, 115 insertions(+), 98 deletions(-)
---
diff --git a/libnautilus-private/nautilus-mime-actions.c b/libnautilus-private/nautilus-mime-actions.c
index b276199..568e28e 100644
--- a/libnautilus-private/nautilus-mime-actions.c
+++ b/libnautilus-private/nautilus-mime-actions.c
@@ -1139,7 +1139,9 @@ typedef struct {
 	char *activation_directory;
 	gboolean user_confirmation;
 	char *uri;
-        GDBusProxy *proxy; /* PackageKit proxy */
+	guint pk_watch_id;
+	GDBusProxy *proxy;
+	GtkWidget *dialog;
 } ActivateParametersInstall;
 
 static void
@@ -1155,8 +1157,9 @@ activate_parameters_install_free (ActivateParametersInstall *parameters_install)
 	nautilus_file_list_free (parameters_install->files);
 	g_free (parameters_install->activation_directory);
 	g_free (parameters_install->uri);
-        if (parameters_install->proxy)
-                g_object_unref (parameters_install->proxy);
+	if (parameters_install->pk_watch_id != 0) {
+		g_bus_unwatch_proxy (parameters_install->pk_watch_id);
+	}
 	g_free (parameters_install);
 }
 
@@ -1303,54 +1306,54 @@ show_unhandled_type_error (ActivateParametersInstall *parameters)
 
 static void
 search_for_application_dbus_call_notify_cb (GDBusProxy   *proxy,
-                                            GAsyncResult *result,
-                                            gpointer      user_data)
+					    GAsyncResult *result,
+					    gpointer      user_data)
 {
-        ActivateParametersInstall *parameters_install = user_data;
-        GVariant *variant;
+	ActivateParametersInstall *parameters_install = user_data;
+	GVariant *variant;
 	GError *error = NULL;
 
-        variant = g_dbus_proxy_call_finish (proxy, result, &error);
+	variant = g_dbus_proxy_call_finish (proxy, result, &error);
 	if (variant == NULL) {
-                if (!g_dbus_error_is_remote_error (error) ||
-                    g_strcmp0 (g_dbus_error_get_remote_error (error), "org.freedesktop.PackageKit.Modify.Failed") == 0) {
-                        char *message;
-
-			message = g_strdup_printf ("%s\n%s",
-						   _("There was an internal error trying to search for applications:"),
-						   error->message);
-			eel_show_error_dialog (_("Unable to search for application"), message,
-					       parameters_install->parent_window);
-			g_free (message);
-		}
+		if (!g_dbus_error_is_remote_error (error) ||
+		    g_strcmp0 (g_dbus_error_get_remote_error (error), "org.freedesktop.PackageKit.Modify.Failed") == 0) {
+			    char *message;
+
+			    message = g_strdup_printf ("%s\n%s",
+						       _("There was an internal error trying to search for applications:"),
+						       error->message);
+			    eel_show_error_dialog (_("Unable to search for application"), message,
+			                           parameters_install->parent_window);
+			    g_free (message);
+		    }
 
 		g_error_free (error);
-                activate_parameters_install_free (parameters_install);
+		activate_parameters_install_free (parameters_install);
 		return;
 	}
 
-        g_variant_unref (variant);
+	g_variant_unref (variant);
 
 	/* activate the file again */
 	nautilus_mime_activate_files (parameters_install->parent_window,
-				      parameters_install->slot_info,
-				      parameters_install->files,
-				      parameters_install->activation_directory,
-				      parameters_install->mode,
-				      parameters_install->flags,
-				      parameters_install->user_confirmation);
+	                              parameters_install->slot_info,
+	                              parameters_install->files,
+	                              parameters_install->activation_directory,
+	                              parameters_install->mode,
+	                              parameters_install->flags,
+	                              parameters_install->user_confirmation);
 
-        activate_parameters_install_free (parameters_install);
+	activate_parameters_install_free (parameters_install);
 }
 
 static void
 search_for_application_mime_type (ActivateParametersInstall *parameters_install, const gchar *mime_type)
 {
-        GdkWindow *window;
+	GdkWindow *window;
 	guint xid = 0;
 	const char *mime_types[2];
 
-        g_assert (parameters_install->proxy != NULL);
+	g_assert (parameters_install->proxy != NULL);	
 
 	/* get XID from parent window */
 	window = gtk_widget_get_window (GTK_WIDGET (parameters_install->parent_window));
@@ -1361,28 +1364,31 @@ search_for_application_mime_type (ActivateParametersInstall *parameters_install,
 	mime_types[0] = mime_type;
 	mime_types[1] = NULL;
 
-        g_dbus_proxy_call (parameters_install->proxy,
-                           "InstallMimeTypes",
-                           g_variant_new ("(u^ass)",
-                                           xid,
-                                           mime_types,
-                                           "hide-confirm-search"),
-                           G_DBUS_CALL_FLAGS_NONE,
-                           G_MAXINT /* no timeout */,
-                           NULL /* cancellable */,
-                           (GAsyncReadyCallback) search_for_application_dbus_call_notify_cb,
-                           parameters_install);
+	g_dbus_proxy_call (parameters_install->proxy,
+			   "InstallMimeTypes",
+			   g_variant_new ("(u^ass)",
+					  xid,
+					  mime_types,
+					  "hide-confirm-search"),
+			   G_DBUS_CALL_FLAGS_NONE,
+			   G_MAXINT /* no timeout */,
+			   NULL /* cancellable */,
+			   (GAsyncReadyCallback) search_for_application_dbus_call_notify_cb,
+			   parameters_install);
 
 	nautilus_debug_log (FALSE, NAUTILUS_DEBUG_LOG_DOMAIN_USER,
-			    "InstallMimeType method invoked for %s", mime_type);
+	                    "InstallMimeType method invoked for %s", mime_type);
 }
 
 static void
-application_unhandled_file_install (GtkDialog *dialog, gint response_id, ActivateParametersInstall *parameters_install)
+application_unhandled_file_install (GtkDialog *dialog,
+                                    gint response_id,
+                                    ActivateParametersInstall *parameters_install)
 {
 	char *mime_type;
 
 	gtk_widget_destroy (GTK_WIDGET (dialog));
+	parameters_install->dialog = NULL;
 
 	if (response_id == GTK_RESPONSE_YES) {
 		mime_type = nautilus_file_get_mime_type (parameters_install->file);
@@ -1397,59 +1403,75 @@ application_unhandled_file_install (GtkDialog *dialog, gint response_id, Activat
 static gboolean
 delete_cb (GtkDialog *dialog)
 {
-        gtk_dialog_response (dialog, GTK_RESPONSE_DELETE_EVENT);
-        return TRUE;
+	gtk_dialog_response (dialog, GTK_RESPONSE_DELETE_EVENT);
+	return TRUE;
 }
 
 static void
-pk_proxy_constructed_cb (GDBusConnection *connection,
-                         GAsyncResult    *result,
-                         gpointer         user_data)
+pk_proxy_appeared_cb (GDBusConnection *connection,
+		      const gchar *name,
+		      const gchar *name_owner,
+		      GDBusProxy *proxy,
+		      gpointer user_data)
 {
         ActivateParametersInstall *parameters_install = user_data;
 	char *mime_type;
 	char *error_message;
 	GtkWidget *dialog;
-	GError *error = NULL;
-
-        parameters_install->proxy = g_dbus_proxy_new_finish (result, &error);
-        if (parameters_install->proxy == NULL) {
-                nautilus_debug_log (FALSE, NAUTILUS_DEBUG_LOG_DOMAIN_USER,
-                                    "Failed to construct PackageKit bus proxy: %s", error->message);
-		g_error_free (error);
-
-		show_unhandled_type_error (parameters_install);
-		activate_parameters_install_free (parameters_install);
-		return;
-	}
 
 	mime_type = nautilus_file_get_mime_type (parameters_install->file);
-	error_message = get_application_no_mime_type_handler_message (parameters_install->file, parameters_install->uri);
+	error_message = get_application_no_mime_type_handler_message (parameters_install->file,
+	                                                              parameters_install->uri);
 	/* use a custom dialog to prompt the user to install new software */
-	dialog = gtk_message_dialog_new (NULL, 0,
-					 GTK_MESSAGE_ERROR,
-					 GTK_BUTTONS_YES_NO,
-					 "%s", error_message);
+	dialog = gtk_message_dialog_new (parameters_install->parent_window, 0,
+	                                 GTK_MESSAGE_ERROR,
+	                                 GTK_BUTTONS_YES_NO,
+	                                 "%s", error_message);
 	gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
-						  _("There is no application installed for %s files.\nDo you want to search for an application to open this file?"),
-						  g_content_type_get_description (mime_type));
+	                                          _("There is no application installed for %s files.\n"
+	                                            "Do you want to search for an application to open this file?"),
+	                                          g_content_type_get_description (mime_type));
 	gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);
 
-	g_signal_connect (dialog, "response", G_CALLBACK (application_unhandled_file_install), parameters_install);
-        g_signal_connect (dialog, "delete-event", G_CALLBACK (delete_cb), NULL);
+	parameters_install->dialog = dialog;
+	parameters_install->proxy = proxy;
+
+	g_signal_connect (dialog, "response",
+	                  G_CALLBACK (application_unhandled_file_install),
+	                  parameters_install);
+	g_signal_connect (dialog, "delete-event",
+	                  G_CALLBACK (delete_cb), NULL);
 	gtk_widget_show_all (dialog);
 	g_free (mime_type);
 }
 
 static void
+pk_proxy_vanished_cb (GDBusConnection *connection,
+                      const gchar *name,
+                      gpointer user_data)
+{
+	ActivateParametersInstall *parameters_install = user_data;
+
+	parameters_install->proxy = NULL;
+	
+	if (parameters_install->dialog != NULL) {
+		gtk_widget_destroy (parameters_install->dialog);
+		parameters_install->dialog = NULL;
+	}
+
+        /* show an unhelpful dialog */
+        show_unhandled_type_error (parameters_install);
+        /* The callback wasn't started, so we have to free the parameters */
+        activate_parameters_install_free (parameters_install);
+}
+
+static void
 application_unhandled_uri (ActivateParameters *parameters, char *uri)
 {
 	gboolean show_install_mime;
 	char *mime_type;
 	NautilusFile *file;
 	ActivateParametersInstall *parameters_install;
-	GDBusConnection *connection;
-	GError *error = NULL;
 
 	file = nautilus_file_get_by_uri (uri);
 
@@ -1481,33 +1503,28 @@ application_unhandled_uri (ActivateParameters *parameters, char *uri)
 	/* There is no use trying to look for handlers of application/octet-stream */
 	if (g_content_type_is_unknown (mime_type)) {
 		show_install_mime = FALSE;
-                goto out;
-	}
-
-        if (!show_install_mime) {
-                goto out;
-        }
-
-        connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
-        if (connection == NULL) {
-                g_warning ("Could not connect to session bus: %s", error->message);
-                goto out;
-        }
-
-        g_dbus_proxy_new (connection,
-                          G_TYPE_DBUS_PROXY,
-                          G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
-                          G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
-                          NULL,
-                          "org.freedesktop.PackageKit",
-                          "/org/freedesktop/PackageKit",
-                          "org.freedesktop.PackageKit.Modify",
-                          NULL /* cancellable */,
-                          (GAsyncReadyCallback) pk_proxy_constructed_cb,
-                          parameters_install);
-
-        g_object_unref (connection);
-        return;
+		goto out;
+	}
+
+	if (!show_install_mime) {
+		goto out;
+	}
+
+	parameters_install->pk_watch_id = 
+		g_bus_watch_proxy (G_BUS_TYPE_SESSION,
+				   "org.freedesktop.PackageKit",
+				   G_BUS_NAME_WATCHER_FLAGS_AUTO_START,
+				   "/org/freedesktop/PackageKit",
+				   "org.freedesktop.PackageKit.Modify",
+				   G_TYPE_DBUS_PROXY,
+				   G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
+		                   G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
+		                   pk_proxy_appeared_cb,
+				   pk_proxy_vanished_cb,
+				   parameters_install,
+				   NULL);
+
+	return;
 
 out:
         /* show an unhelpful dialog */
@@ -1515,7 +1532,7 @@ out:
         /* The callback wasn't started, so we have to free the parameters */
         activate_parameters_install_free (parameters_install);
 
-        g_free (mime_type);
+	g_free (mime_type);
 }
 
 typedef struct {



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