]> www.fi.muni.cz Git - evince.git/blob - shell/main.c
Fix some memory leaks.
[evince.git] / shell / main.c
1 /*
2  *  Copyright (C) 2004 Marco Pesenti Gritti
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2, or (at your option)
7  *  any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  *
18  *  $Id$
19  */
20
21 #include "config.h"
22
23 #include "ev-application.h"
24 #include "ev-metadata-manager.h"
25
26 #include <glib/gi18n.h>
27 #include <gdk/gdkx.h>
28 #include <gtk/gtkmain.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <libgnome/gnome-program.h>
32 #include <libgnomeui/gnome-ui-init.h>
33 #include <libgnomeui/gnome-app-helper.h>
34 #include <libgnomeui/gnome-authentication-manager.h>
35 #include <libgnomevfs/gnome-vfs-utils.h>
36
37 #ifdef ENABLE_DBUS
38 #include <dbus/dbus-glib-bindings.h>
39 #endif
40
41 #include "ev-stock-icons.h"
42 #include "ev-debug.h"
43 #include "ev-job-queue.h"
44 #include "ev-file-helpers.h"
45
46 static char *ev_page_label;
47
48 static struct poptOption popt_options[] =
49 {
50         { "page-label", 'p', POPT_ARG_STRING, &ev_page_label, 0, N_("The page of the document to display."), N_("PAGE")},
51         { NULL, 0, 0, NULL, 0, NULL, NULL }
52 };
53
54 static void
55 load_files (const char **files)
56 {
57         int i;
58
59         if (!files) {
60                 ev_application_open_window (EV_APP, GDK_CURRENT_TIME, NULL);
61                 return;
62         }
63
64         for (i = 0; files[i]; i++) {
65                 char *uri;
66
67                 uri = gnome_vfs_make_uri_from_shell_arg (files[i]);
68                 ev_application_open_uri (EV_APP, uri, ev_page_label,
69                                          GDK_CURRENT_TIME, NULL);
70                 g_free (uri);
71         }
72 }
73
74 #ifdef ENABLE_DBUS
75
76 #ifndef HAVE_GTK_WINDOW_PRESENT_WITH_TIME
77 static guint32
78 get_startup_time (void)
79 {
80         const char *envvar, *timestamp;
81         unsigned long value;
82         char *end;
83
84         envvar = getenv ("DESKTOP_STARTUP_ID");
85
86         if (envvar == NULL)
87                 return 0;
88
89 /* DESKTOP_STARTUP_ID is of form "<unique>_TIME<timestamp>".
90  *
91  * <unique> might contain a T but <timestamp> is an integer.  As such,
92  * the last 'T' in the string must be the start of "TIME".
93  */
94         timestamp = rindex (envvar, 'T');
95
96 /* Maybe the word "TIME" was not found... */
97         if (timestamp == NULL || strncmp (timestamp, "TIME", 4))
98                 return 0;
99
100         timestamp += 4;
101
102 /* strtoul sets errno = ERANGE on overflow, but it is not specified
103  * if it sets it to 0 on success.  Doing so ourselves is the only
104  * way to know for sure.
105  */
106         errno = 0;
107         value = strtoul (timestamp, &end, 10);
108
109 /* unsigned long might be 64bit, so double-check! */
110         if (errno != 0 || *end != '\0' || value > G_MAXINT32)
111                 return 0;
112
113         return value;
114 }
115 #endif
116
117 static gboolean
118 load_files_remote (const char **files)
119 {
120         int i;
121         GError *error = NULL;
122         DBusGConnection *connection;
123         gboolean result = FALSE;
124 #if DBUS_VERSION < 35
125         DBusGPendingCall *call;
126 #endif
127         DBusGProxy *remote_object;
128 #ifdef HAVE_GTK_WINDOW_PRESENT_WITH_TIME
129         GdkDisplay *display;
130 #endif
131         guint32 timestamp;
132
133 #ifdef HAVE_GTK_WINDOW_PRESENT_WITH_TIME
134         display = gdk_display_get_default();
135         timestamp = gdk_x11_display_get_user_time (display);
136 #else
137         /* Fake it for GTK+2.6 */
138         timestamp = get_startup_time ();
139 #endif
140         connection = dbus_g_bus_get (DBUS_BUS_STARTER, &error);
141         if (connection == NULL) {
142                 g_warning (error->message);
143                 g_error_free (error);   
144
145                 return FALSE;
146         }
147
148         remote_object = dbus_g_proxy_new_for_name (connection,
149                                                    "org.gnome.evince.ApplicationService",
150                                                    "/org/gnome/evince/Evince",
151                                                    "org.gnome.evince.Application");
152         if (!files) {
153 #if DBUS_VERSION <= 33
154                 call = dbus_g_proxy_begin_call (remote_object, "OpenWindow",
155                                                 DBUS_TYPE_UINT32, &timestamp,
156                                                 DBUS_TYPE_INVALID);
157
158                 if (!dbus_g_proxy_end_call (remote_object, call, &error, DBUS_TYPE_INVALID)) {
159                         g_warning (error->message);
160                         g_clear_error (&error);
161                         g_object_unref (remote_object);
162                         dbus_g_connection_unref (connection);
163                         return FALSE;
164                 }
165 #elif DBUS_VERSION == 34
166                 call = dbus_g_proxy_begin_call (remote_object, "OpenWindow",
167                                                 G_TYPE_UINT, timestamp,
168                                                 G_TYPE_INVALID);
169
170                 if (!dbus_g_proxy_end_call (remote_object, call, &error, G_TYPE_INVALID)) {
171                         g_warning (error->message);
172                         g_clear_error (&error);
173                         g_object_unref (remote_object);
174                         dbus_g_connection_unref (connection);
175                         return FALSE;
176                 }
177 #else
178                 if (!dbus_g_proxy_call (remote_object, "OpenWindow", &error,
179                                         G_TYPE_UINT, timestamp,
180                                         G_TYPE_INVALID,
181                                         G_TYPE_INVALID)) {
182                         g_warning (error->message);
183                         g_clear_error (&error);
184                         g_object_unref (remote_object);
185                         dbus_g_connection_unref (connection);
186                         return FALSE;
187                 }
188 #endif
189                 g_object_unref (remote_object);
190                 dbus_g_connection_unref (connection);
191                 
192                 return TRUE;
193         }
194
195         for (i = 0; files[i]; i++) {
196                 const char *page_label;
197                 char *uri;
198
199                 uri = gnome_vfs_make_uri_from_shell_arg (files[i]);
200                 page_label = ev_page_label ? ev_page_label : "";
201 #if DBUS_VERSION <= 33
202                 call = dbus_g_proxy_begin_call (remote_object, "OpenURI",
203                                                 DBUS_TYPE_STRING, &uri,
204                                                 DBUS_TYPE_STRING, &page_label,
205                                                 DBUS_TYPE_UINT32, &timestamp,
206                                                 DBUS_TYPE_INVALID);
207
208                 if (!dbus_g_proxy_end_call (remote_object, call, &error, DBUS_TYPE_INVALID)) {
209                         g_warning (error->message);
210                         g_clear_error (&error);
211                         g_free (uri);
212                         continue;
213                 }
214 #elif DBUS_VERSION == 34
215                 call = dbus_g_proxy_begin_call (remote_object, "OpenURI",
216                                                 G_TYPE_STRING, uri,
217                                                 G_TYPE_STRING, page_label,
218                                                 G_TYPE_UINT, timestamp,
219                                                 G_TYPE_INVALID);
220
221                 if (!dbus_g_proxy_end_call (remote_object, call, &error, G_TYPE_INVALID)) {
222                         g_warning (error->message);
223                         g_clear_error (&error);
224                         g_free (uri);
225                         continue;
226                 }
227 #else
228                 if (!dbus_g_proxy_call (remote_object, "OpenURI", &error,
229                                         G_TYPE_STRING, uri,
230                                         G_TYPE_STRING, page_label,
231                                         G_TYPE_UINT, timestamp,
232                                         G_TYPE_INVALID,
233                                         G_TYPE_INVALID)) {
234                         g_warning (error->message);
235                         g_clear_error (&error);
236                         g_free (uri);
237                         continue;
238                 }
239 #endif
240                 g_free (uri);
241                 result = TRUE;
242         }
243
244         g_object_unref (remote_object);
245         dbus_g_connection_unref (connection);
246
247         gdk_notify_startup_complete ();
248
249         return result;
250 }
251 #endif /* ENABLE_DBUS */
252
253 int
254 main (int argc, char *argv[])
255 {
256         gboolean enable_metadata = FALSE;
257         poptContext context;
258         GValue context_as_value = { 0 };
259         GnomeProgram *program;
260
261 #ifdef ENABLE_NLS
262         /* Initialize the i18n stuff */
263         bindtextdomain(GETTEXT_PACKAGE, GNOMELOCALEDIR);
264         bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
265         textdomain(GETTEXT_PACKAGE);
266 #endif
267
268         program = gnome_program_init (PACKAGE, VERSION,
269                                       LIBGNOMEUI_MODULE, argc, argv,
270                                       GNOME_PARAM_POPT_TABLE, popt_options,
271                                       GNOME_PARAM_HUMAN_READABLE_NAME, _("Evince"),
272                                       GNOME_PARAM_APP_DATADIR, GNOMEDATADIR,
273                                       NULL);
274         g_object_get_property (G_OBJECT (program),
275                                GNOME_PARAM_POPT_CONTEXT,
276                                g_value_init (&context_as_value, G_TYPE_POINTER));
277         context = g_value_get_pointer (&context_as_value);
278
279
280 #ifdef ENABLE_DBUS
281         if (!ev_application_register_service (EV_APP)) {
282                 if (load_files_remote (poptGetArgs (context))) {
283                         return 0;
284                 }
285         } else {
286                 enable_metadata = TRUE;
287         }
288 #endif
289
290         gnome_authentication_manager_init ();
291
292
293         if (enable_metadata) {
294                 ev_metadata_manager_init ();
295         }
296
297         ev_job_queue_init ();
298         g_set_application_name (_("Evince Document Viewer"));
299
300         ev_file_helpers_init ();
301         ev_debug_init ();
302         ev_stock_icons_init ();
303         gtk_window_set_default_icon_name ("evince");
304
305         load_files (poptGetArgs (context));
306
307         gtk_main ();
308
309         gnome_accelerators_sync ();
310         poptFreeContext (context);
311         ev_file_helpers_shutdown ();
312
313         if (enable_metadata) {
314                 ev_metadata_manager_shutdown ();
315         }
316
317         return 0;
318 }