nautilus-3.6.3/src/nautilus-previewer.c

No issues found

  1 /*
  2  * nautilus-previewer: nautilus previewer DBus wrapper
  3  *
  4  * Copyright (C) 2011, Red Hat, Inc.
  5  *
  6  * Nautilus is free software; you can redistribute it and/or
  7  * modify it under the terms of the GNU General Public License as
  8  * published by the Free Software Foundation; either version 2 of the
  9  * License, or (at your option) any later version.
 10  *
 11  * Nautilus is distributed in the hope that it will be useful,
 12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 14  * General Public License for more details.
 15  *
 16  * You should have received a copy of the GNU General Public License
 17  * along with this program; if not, write to the Free Software
 18  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 19  *
 20  * Author: Cosimo Cecchi <cosimoc@redhat.com>
 21  *
 22  */
 23 
 24 #include "config.h"
 25 
 26 #include "nautilus-previewer.h"
 27 
 28 #define DEBUG_FLAG NAUTILUS_DEBUG_PREVIEWER
 29 #include <libnautilus-private/nautilus-debug.h>
 30 
 31 #include <gio/gio.h>
 32 
 33 G_DEFINE_TYPE (NautilusPreviewer, nautilus_previewer, G_TYPE_OBJECT);
 34 
 35 #define PREVIEWER_DBUS_NAME "org.gnome.NautilusPreviewer"
 36 #define PREVIEWER_DBUS_IFACE "org.gnome.NautilusPreviewer"
 37 #define PREVIEWER_DBUS_PATH "/org/gnome/NautilusPreviewer"
 38 
 39 static NautilusPreviewer *singleton = NULL;
 40 
 41 struct _NautilusPreviewerPriv {
 42   GDBusConnection *connection;
 43 };
 44 
 45 static void
 46 nautilus_previewer_dispose (GObject *object)
 47 {
 48   NautilusPreviewer *self = NAUTILUS_PREVIEWER (object);
 49 
 50   DEBUG ("%p", self);
 51 
 52   g_clear_object (&self->priv->connection);
 53 
 54   G_OBJECT_CLASS (nautilus_previewer_parent_class)->dispose (object);
 55 }
 56 
 57 static GObject *
 58 nautilus_previewer_constructor (GType type,
 59                                 guint n_construct_params,
 60                                 GObjectConstructParam *construct_params)
 61 {
 62   GObject *retval;
 63 
 64   if (singleton != NULL)
 65     return G_OBJECT (singleton);
 66 
 67   retval = G_OBJECT_CLASS (nautilus_previewer_parent_class)->constructor
 68     (type, n_construct_params, construct_params);
 69 
 70   singleton = NAUTILUS_PREVIEWER (retval);
 71   g_object_add_weak_pointer (retval, (gpointer) &singleton);
 72 
 73   return retval;
 74 }
 75 
 76 static void
 77 nautilus_previewer_init (NautilusPreviewer *self)
 78 {
 79   GError *error = NULL;
 80 
 81   self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, NAUTILUS_TYPE_PREVIEWER,
 82                                             NautilusPreviewerPriv);
 83 
 84   self->priv->connection = g_bus_get_sync (G_BUS_TYPE_SESSION,
 85                                            NULL, &error);
 86 
 87   if (error != NULL) {
 88     g_printerr ("Unable to initialize DBus connection: %s", error->message);
 89     g_error_free (error);
 90     return;
 91   }
 92 }
 93 
 94 static void
 95 nautilus_previewer_class_init (NautilusPreviewerClass *klass)
 96 {
 97   GObjectClass *oclass;
 98 
 99   oclass = G_OBJECT_CLASS (klass);
100   oclass->constructor = nautilus_previewer_constructor;
101   oclass->dispose = nautilus_previewer_dispose;
102 
103   g_type_class_add_private (klass, sizeof (NautilusPreviewerPriv));
104 }
105 
106 static void
107 previewer_show_file_ready_cb (GObject *source,
108                               GAsyncResult *res,
109                               gpointer user_data)
110 {
111   NautilusPreviewer *self = user_data;
112   GError *error = NULL;
113 
114   g_dbus_connection_call_finish (self->priv->connection,
115                                  res, &error);
116 
117   if (error != NULL) {
118     DEBUG ("Unable to call ShowFile on NautilusPreviewer: %s",
119            error->message);
120     g_error_free (error);
121   }
122 
123   g_object_unref (self);
124 }
125 
126 static void
127 previewer_close_ready_cb (GObject *source,
128                           GAsyncResult *res,
129                           gpointer user_data)
130 {
131   NautilusPreviewer *self = user_data;
132   GError *error = NULL;
133 
134   g_dbus_connection_call_finish (self->priv->connection,
135                                  res, &error);
136 
137   if (error != NULL) {
138     DEBUG ("Unable to call Close on NautilusPreviewer: %s",
139            error->message);
140     g_error_free (error);
141   }
142 
143   g_object_unref (self);
144 }
145 
146 NautilusPreviewer *
147 nautilus_previewer_get_singleton (void)
148 {
149   return g_object_new (NAUTILUS_TYPE_PREVIEWER, NULL);
150 }
151 
152 void
153 nautilus_previewer_call_show_file (NautilusPreviewer *self,
154                                    const gchar *uri,
155                                    guint xid,
156 				   gboolean close_if_already_visible)
157 {
158   GVariant *variant;
159 
160   variant = g_variant_new ("(sib)",
161                            uri, xid, close_if_already_visible);
162 
163   if (self->priv->connection == NULL) {
164     g_printerr ("No DBus connection available");
165     return;
166   }
167 
168   g_dbus_connection_call (self->priv->connection,
169                           PREVIEWER_DBUS_NAME,
170                           PREVIEWER_DBUS_PATH,
171                           PREVIEWER_DBUS_IFACE,
172                           "ShowFile",
173                           variant,
174                           NULL,
175                           G_DBUS_CALL_FLAGS_NONE,
176                           -1,
177                           NULL,
178                           previewer_show_file_ready_cb,
179                           g_object_ref (self));
180 }
181 
182 void
183 nautilus_previewer_call_close (NautilusPreviewer *self)
184 {
185   if (self->priv->connection == NULL) {
186     g_printerr ("No DBus connection available");
187     return;
188   }
189 
190   /* don't autostart the previewer if it's not running */
191   g_dbus_connection_call (self->priv->connection,
192                           PREVIEWER_DBUS_NAME,
193                           PREVIEWER_DBUS_PATH,
194                           PREVIEWER_DBUS_IFACE,
195                           "Close",
196                           NULL,
197                           NULL,
198                           G_DBUS_CALL_FLAGS_NO_AUTO_START,
199                           -1,
200                           NULL,
201                           previewer_close_ready_cb,
202                           g_object_ref (self));
203 }