[nautilus] application: simplify use of NautilusPreviewer



commit 3a5dd43288ecc401f19627c88f4c8bfcededdeaf
Author: Cosimo Cecchi <cosimoc gnome org>
Date:   Mon Apr 28 22:33:53 2014 -0700

    application: simplify use of NautilusPreviewer
    
    Just use the GDBusConnection that the application gives us, instead of
    requiring a separate one.

 src/nautilus-application.c |    7 +--
 src/nautilus-previewer.c   |  109 ++++----------------------------------------
 src/nautilus-previewer.h   |   39 ++--------------
 src/nautilus-view.c        |    4 +-
 4 files changed, 17 insertions(+), 142 deletions(-)
---
diff --git a/src/nautilus-application.c b/src/nautilus-application.c
index 936be40..1ff62b3 100644
--- a/src/nautilus-application.c
+++ b/src/nautilus-application.c
@@ -1220,9 +1220,6 @@ nautilus_application_startup (GApplication *app)
 
        gtk_window_set_default_icon_name ("system-file-manager");
 
-       /* initialize the previewer singleton */
-       nautilus_previewer_get_singleton ();
-
        /* create DBus manager */
        self->priv->dbus_manager = nautilus_dbus_manager_new ();
        self->priv->fdb_manager = nautilus_freedesktop_dbus_new ();
@@ -1373,7 +1370,6 @@ static void
 nautilus_application_window_removed (GtkApplication *app,
                                     GtkWindow *window)
 {
-       NautilusPreviewer *previewer;
        GList *l;
 
        /* chain to parent */
@@ -1382,8 +1378,7 @@ nautilus_application_window_removed (GtkApplication *app,
        /* if this was the last window, close the previewer */
        for (l = gtk_application_get_windows (GTK_APPLICATION (app)); l && !NAUTILUS_IS_WINDOW (l->data); l = 
l->next);
        if (!l) {
-               previewer = nautilus_previewer_get_singleton ();
-               nautilus_previewer_call_close (previewer);
+               nautilus_previewer_call_close ();
        }
 
        g_signal_handlers_disconnect_by_func (window, on_slot_added, app);
diff --git a/src/nautilus-previewer.c b/src/nautilus-previewer.c
index 47ba764..5594401 100644
--- a/src/nautilus-previewer.c
+++ b/src/nautilus-previewer.c
@@ -29,88 +29,18 @@
 
 #include <gio/gio.h>
 
-G_DEFINE_TYPE (NautilusPreviewer, nautilus_previewer, G_TYPE_OBJECT);
-
 #define PREVIEWER_DBUS_NAME "org.gnome.NautilusPreviewer"
 #define PREVIEWER_DBUS_IFACE "org.gnome.NautilusPreviewer"
 #define PREVIEWER_DBUS_PATH "/org/gnome/NautilusPreviewer"
 
-static NautilusPreviewer *singleton = NULL;
-
-struct _NautilusPreviewerPriv {
-  GDBusConnection *connection;
-};
-
-static void
-nautilus_previewer_dispose (GObject *object)
-{
-  NautilusPreviewer *self = NAUTILUS_PREVIEWER (object);
-
-  DEBUG ("%p", self);
-
-  g_clear_object (&self->priv->connection);
-
-  G_OBJECT_CLASS (nautilus_previewer_parent_class)->dispose (object);
-}
-
-static GObject *
-nautilus_previewer_constructor (GType type,
-                                guint n_construct_params,
-                                GObjectConstructParam *construct_params)
-{
-  GObject *retval;
-
-  if (singleton != NULL)
-    return G_OBJECT (singleton);
-
-  retval = G_OBJECT_CLASS (nautilus_previewer_parent_class)->constructor
-    (type, n_construct_params, construct_params);
-
-  singleton = NAUTILUS_PREVIEWER (retval);
-  g_object_add_weak_pointer (retval, (gpointer) &singleton);
-
-  return retval;
-}
-
-static void
-nautilus_previewer_init (NautilusPreviewer *self)
-{
-  GError *error = NULL;
-
-  self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, NAUTILUS_TYPE_PREVIEWER,
-                                            NautilusPreviewerPriv);
-
-  self->priv->connection = g_bus_get_sync (G_BUS_TYPE_SESSION,
-                                           NULL, &error);
-
-  if (error != NULL) {
-    g_printerr ("Unable to initialize DBus connection: %s", error->message);
-    g_error_free (error);
-    return;
-  }
-}
-
-static void
-nautilus_previewer_class_init (NautilusPreviewerClass *klass)
-{
-  GObjectClass *oclass;
-
-  oclass = G_OBJECT_CLASS (klass);
-  oclass->constructor = nautilus_previewer_constructor;
-  oclass->dispose = nautilus_previewer_dispose;
-
-  g_type_class_add_private (klass, sizeof (NautilusPreviewerPriv));
-}
-
 static void
 previewer_show_file_ready_cb (GObject *source,
                               GAsyncResult *res,
                               gpointer user_data)
 {
-  NautilusPreviewer *self = user_data;
   GError *error = NULL;
 
-  g_dbus_connection_call_finish (self->priv->connection,
+  g_dbus_connection_call_finish (G_DBUS_CONNECTION (source),
                                  res, &error);
 
   if (error != NULL) {
@@ -118,8 +48,6 @@ previewer_show_file_ready_cb (GObject *source,
            error->message);
     g_error_free (error);
   }
-
-  g_object_unref (self);
 }
 
 static void
@@ -127,10 +55,9 @@ previewer_close_ready_cb (GObject *source,
                           GAsyncResult *res,
                           gpointer user_data)
 {
-  NautilusPreviewer *self = user_data;
   GError *error = NULL;
 
-  g_dbus_connection_call_finish (self->priv->connection,
+  g_dbus_connection_call_finish (G_DBUS_CONNECTION (source),
                                  res, &error);
 
   if (error != NULL) {
@@ -138,33 +65,20 @@ previewer_close_ready_cb (GObject *source,
            error->message);
     g_error_free (error);
   }
-
-  g_object_unref (self);
-}
-
-NautilusPreviewer *
-nautilus_previewer_get_singleton (void)
-{
-  return g_object_new (NAUTILUS_TYPE_PREVIEWER, NULL);
 }
 
 void
-nautilus_previewer_call_show_file (NautilusPreviewer *self,
-                                   const gchar *uri,
+nautilus_previewer_call_show_file (const gchar *uri,
                                    guint xid,
                                   gboolean close_if_already_visible)
 {
+  GDBusConnection *connection = g_application_get_dbus_connection (g_application_get_default ());
   GVariant *variant;
 
   variant = g_variant_new ("(sib)",
                            uri, xid, close_if_already_visible);
 
-  if (self->priv->connection == NULL) {
-    g_printerr ("No DBus connection available");
-    return;
-  }
-
-  g_dbus_connection_call (self->priv->connection,
+  g_dbus_connection_call (connection,
                           PREVIEWER_DBUS_NAME,
                           PREVIEWER_DBUS_PATH,
                           PREVIEWER_DBUS_IFACE,
@@ -175,19 +89,16 @@ nautilus_previewer_call_show_file (NautilusPreviewer *self,
                           -1,
                           NULL,
                           previewer_show_file_ready_cb,
-                          g_object_ref (self));
+                          NULL);
 }
 
 void
-nautilus_previewer_call_close (NautilusPreviewer *self)
+nautilus_previewer_call_close (void)
 {
-  if (self->priv->connection == NULL) {
-    g_printerr ("No DBus connection available");
-    return;
-  }
+  GDBusConnection *connection = g_application_get_dbus_connection (g_application_get_default ());
 
   /* don't autostart the previewer if it's not running */
-  g_dbus_connection_call (self->priv->connection,
+  g_dbus_connection_call (connection,
                           PREVIEWER_DBUS_NAME,
                           PREVIEWER_DBUS_PATH,
                           PREVIEWER_DBUS_IFACE,
@@ -198,5 +109,5 @@ nautilus_previewer_call_close (NautilusPreviewer *self)
                           -1,
                           NULL,
                           previewer_close_ready_cb,
-                          g_object_ref (self));
+                          NULL);
 }
diff --git a/src/nautilus-previewer.h b/src/nautilus-previewer.h
index 4895531..8134d02 100644
--- a/src/nautilus-previewer.h
+++ b/src/nautilus-previewer.h
@@ -23,43 +23,14 @@
 #ifndef __NAUTILUS_PREVIEWER_H__
 #define __NAUTILUS_PREVIEWER_H__
 
-#include <glib-object.h>
+#include <glib.h>
 
 G_BEGIN_DECLS
 
-#define NAUTILUS_TYPE_PREVIEWER nautilus_previewer_get_type()
-#define NAUTILUS_PREVIEWER(obj) \
-  (G_TYPE_CHECK_INSTANCE_CAST ((obj), NAUTILUS_TYPE_PREVIEWER, NautilusPreviewer))
-#define NAUTILUS_PREVIEWER_CLASS(klass) \
-  (G_TYPE_CHECK_CLASS_CAST ((klass), NAUTILUS_TYPE_PREVIEWER, NautilusPreviewerClass))
-#define NAUTILUS_IS_PREVIEWER(obj) \
-  (G_TYPE_CHECK_INSTANCE_TYPE ((obj), NAUTILUS_TYPE_PREVIEWER))
-#define NAUTILUS_IS_PREVIEWER_CLASS(klass) \
-  (G_TYPE_CHECK_CLASS_TYPE ((klass), NAUTILUS_TYPE_PREVIEWER))
-#define NAUTILUS_PREVIEWER_GET_CLASS(obj) \
-  (G_TYPE_INSTANCE_GET_CLASS ((obj), NAUTILUS_TYPE_PREVIEWER, NautilusPreviewerClass))
-
-typedef struct _NautilusPreviewerPriv NautilusPreviewerPriv;
-
-typedef struct {
-  GObject parent;
-
-  /* private */
-  NautilusPreviewerPriv *priv;
-} NautilusPreviewer;
-
-typedef struct {
-  GObjectClass parent_class;
-} NautilusPreviewerClass;
-
-GType nautilus_previewer_get_type (void);
-
-NautilusPreviewer *nautilus_previewer_get_singleton (void);
-void nautilus_previewer_call_show_file (NautilusPreviewer *previewer,
-                                        const gchar *uri,
-                                        guint xid,
-                                       gboolean close_if_already_visible);
-void nautilus_previewer_call_close (NautilusPreviewer *previewer);
+void nautilus_previewer_call_show_file (const gchar *uri,
+                                        guint        xid,
+                                       gboolean     close_if_already_visible);
+void nautilus_previewer_call_close     (void);
 
 G_END_DECLS
 
diff --git a/src/nautilus-view.c b/src/nautilus-view.c
index b17031b..6b9e374 100644
--- a/src/nautilus-view.c
+++ b/src/nautilus-view.c
@@ -1034,17 +1034,15 @@ nautilus_view_preview_files (NautilusView *view,
                             GList *files,
                             GArray *locations)
 {
-       NautilusPreviewer *previewer;
        gchar *uri;
        guint xid;
        GtkWidget *toplevel;
 
-       previewer = nautilus_previewer_get_singleton ();
        uri = nautilus_file_get_uri (files->data);
        toplevel = gtk_widget_get_toplevel (GTK_WIDGET (view));
 
        xid = gdk_x11_window_get_xid (gtk_widget_get_window (toplevel));
-       nautilus_previewer_call_show_file (previewer, uri, xid, TRUE);
+       nautilus_previewer_call_show_file (uri, xid, TRUE);
 
        g_free (uri);
 }


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