]> www.fi.muni.cz Git - evince.git/blob - shell/main.c
catch another gtk+-2.8ism Make work with gtk+-2.6 Release 0.4.0
[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 <libgnome/gnome-program.h>
30 #include <libgnomeui/gnome-ui-init.h>
31 #include <libgnomeui/gnome-app-helper.h>
32 #include <libgnomeui/gnome-authentication-manager.h>
33 #include <libgnomevfs/gnome-vfs-utils.h>
34
35 #ifdef ENABLE_DBUS
36 #include <dbus/dbus-glib-bindings.h>
37 #endif
38
39 #include "ev-stock-icons.h"
40 #include "ev-debug.h"
41 #include "ev-job-queue.h"
42 #include "ev-file-helpers.h"
43
44 static char *ev_page_label;
45
46 static struct poptOption popt_options[] =
47 {
48         { "page-label", 'p', POPT_ARG_STRING, &ev_page_label, 0, N_("The page of the document to display."), N_("PAGE")},
49         { NULL, 0, 0, NULL, 0, NULL, NULL }
50 };
51
52 static void
53 load_files (const char **files)
54 {
55         int i;
56
57         if (!files) {
58                 ev_application_open_window (EV_APP, GDK_CURRENT_TIME, NULL);
59                 return;
60         }
61
62         for (i = 0; files[i]; i++) {
63                 char *uri;
64
65                 uri = gnome_vfs_make_uri_from_shell_arg (files[i]);
66                 ev_application_open_uri (EV_APP, uri, ev_page_label,
67                                          GDK_CURRENT_TIME, NULL);
68                 g_free (uri);
69         }
70 }
71
72 #ifdef ENABLE_DBUS
73
74 #ifndef HAVE_GTK_WINDOW_PRESENT_WITH_TIME
75 static guint32
76 get_startup_time (void)
77 {
78         const char *envvar, *timestamp;
79         unsigned long value;
80         char *end;
81
82         envvar = getenv ("DESKTOP_STARTUP_ID");
83
84         if (envvar == NULL)
85                 return 0;
86
87 /* DESKTOP_STARTUP_ID is of form "<unique>_TIME<timestamp>".
88  *
89  * <unique> might contain a T but <timestamp> is an integer.  As such,
90  * the last 'T' in the string must be the start of "TIME".
91  */
92         timestamp = rindex (envvar, 'T');
93
94 /* Maybe the word "TIME" was not found... */
95         if (timestamp == NULL || strncmp (timestamp, "TIME", 4))
96                 return 0;
97
98         timestamp += 4;
99
100 /* strtoul sets errno = ERANGE on overflow, but it is not specified
101  * if it sets it to 0 on success.  Doing so ourselves is the only
102  * way to know for sure.
103  */
104         errno = 0;
105         value = strtoul (timestamp, &end, 10);
106
107 /* unsigned long might be 64bit, so double-check! */
108         if (errno != 0 || *end != '\0' || value > G_MAXINT32)
109                 return 0;
110
111         return value;
112 }
113 #endif
114
115 static gboolean
116 load_files_remote (const char **files)
117 {
118         int i;
119         GError *error = NULL;
120         DBusGConnection *connection;
121         gboolean result = FALSE;
122 #if DBUS_VERSION < 35
123         DBusGPendingCall *call;
124 #endif
125         DBusGProxy *remote_object;
126         GdkDisplay *display;
127         guint32 timestamp;
128
129 #ifdef HAVE_GTK_WINDOW_PRESENT_WITH_TIME
130         display = gdk_display_get_default();
131         timestamp = gdk_x11_display_get_user_time (display);
132 #else
133         /* Fake it for GTK+2.6 */
134         timestamp = get_startup_time ();
135 #endif
136         connection = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
137         if (connection == NULL) {
138                 g_warning (error->message);
139                 g_error_free (error);   
140
141                 return FALSE;
142         }
143
144         remote_object = dbus_g_proxy_new_for_name (connection,
145                                                    "org.gnome.evince.ApplicationService",
146                                                    "/org/gnome/evince/Evince",
147                                                    "org.gnome.evince.Application");
148         if (!files) {
149 #if DBUS_VERSION <= 33
150                 call = dbus_g_proxy_begin_call (remote_object, "OpenWindow",
151                                                 DBUS_TYPE_UINT32, timestamp,
152                                                 DBUS_TYPE_INVALID);
153
154                 if (!dbus_g_proxy_end_call (remote_object, call, &error, DBUS_TYPE_INVALID)) {
155                         g_warning (error->message);
156                         g_clear_error (&error);
157                         return FALSE;
158                 }
159 #elif DBUS_VERSION == 34
160                 call = dbus_g_proxy_begin_call (remote_object, "OpenWindow",
161                                                 G_TYPE_UINT, timestamp,
162                                                 G_TYPE_INVALID);
163
164                 if (!dbus_g_proxy_end_call (remote_object, call, &error, G_TYPE_INVALID)) {
165                         g_warning (error->message);
166                         g_clear_error (&error);
167                         return FALSE;
168                 }
169 #else
170                 if (!dbus_g_proxy_call (remote_object, "OpenWindow", &error,
171                                         G_TYPE_UINT, timestamp,
172                                         G_TYPE_INVALID,
173                                         G_TYPE_INVALID)) {
174                         g_warning (error->message);
175                         g_clear_error (&error);
176                         return FALSE;
177                 }
178 #endif
179                 return TRUE;
180         }
181
182         for (i = 0; files[i]; i++) {
183                 const char *page_label;
184                 char *uri;
185
186                 uri = gnome_vfs_make_uri_from_shell_arg (files[i]);
187                 page_label = ev_page_label ? ev_page_label : ""; 
188 #if DBUS_VERSION <= 33
189                 call = dbus_g_proxy_begin_call (remote_object, "OpenURI",
190                                                 DBUS_TYPE_STRING, &uri,
191                                                 DBUS_TYPE_STRING, &page_label,
192                                                 DBUS_TYPE_UINT32, timestamp,
193                                                 DBUS_TYPE_INVALID);
194
195                 if (!dbus_g_proxy_end_call (remote_object, call, &error, DBUS_TYPE_INVALID)) {
196                         g_warning (error->message);
197                         g_clear_error (&error);
198                         g_free (uri);
199                         continue;
200                 }
201 #elif DBUS_VERSION == 34
202                 call = dbus_g_proxy_begin_call (remote_object, "OpenURI",
203                                                 G_TYPE_STRING, uri,
204                                                 G_TYPE_STRING, page_label,
205                                                 G_TYPE_UINT, timestamp,
206                                                 G_TYPE_INVALID);
207
208                 if (!dbus_g_proxy_end_call (remote_object, call, &error, G_TYPE_INVALID)) {
209                         g_warning (error->message);
210                         g_clear_error (&error);
211                         g_free (uri);
212                         continue;
213                 }
214 #else
215                 if (!dbus_g_proxy_call (remote_object, "OpenURI", &error,
216                                         G_TYPE_STRING, uri,
217                                         G_TYPE_STRING, page_label,
218                                         G_TYPE_UINT, timestamp,
219                                         G_TYPE_INVALID,
220                                         G_TYPE_INVALID)) {
221                         g_warning (error->message);
222                         g_clear_error (&error);
223                         g_free (uri);
224                         continue;
225                 }
226 #endif
227                 g_free (uri);
228                 result = TRUE;
229         }
230
231         gdk_notify_startup_complete ();
232
233         return result;
234 }
235 #endif /* ENABLE_DBUS */
236
237 int
238 main (int argc, char *argv[])
239 {
240         gboolean enable_metadata = FALSE;
241         poptContext context;
242         GValue context_as_value = { 0 };
243         GnomeProgram *program;
244
245 #ifdef ENABLE_NLS
246         /* Initialize the i18n stuff */
247         bindtextdomain(GETTEXT_PACKAGE, GNOMELOCALEDIR);
248         bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
249         textdomain(GETTEXT_PACKAGE);
250 #endif
251
252         program = gnome_program_init (PACKAGE, VERSION,
253                                       LIBGNOMEUI_MODULE, argc, argv,
254                                       GNOME_PARAM_POPT_TABLE, popt_options,
255                                       GNOME_PARAM_HUMAN_READABLE_NAME, _("Evince"),
256                                       GNOME_PARAM_APP_DATADIR, GNOMEDATADIR,
257                                       NULL);
258         g_object_get_property (G_OBJECT (program),
259                                GNOME_PARAM_POPT_CONTEXT,
260                                g_value_init (&context_as_value, G_TYPE_POINTER));
261         context = g_value_get_pointer (&context_as_value);
262
263
264 #ifdef ENABLE_DBUS
265         if (!ev_application_register_service (EV_APP)) {
266                 if (load_files_remote (poptGetArgs (context))) {
267                         return 0;
268                 }
269         } else {
270                 enable_metadata = TRUE;
271         }
272 #endif
273
274         gnome_authentication_manager_init ();
275
276
277         if (enable_metadata) {
278                 ev_metadata_manager_init ();
279         }
280
281         ev_job_queue_init ();
282         g_set_application_name (_("Evince Document Viewer"));
283
284         ev_file_helpers_init ();
285         ev_debug_init ();
286         ev_stock_icons_init ();
287         gtk_window_set_default_icon_name ("postscript-viewer");
288
289         load_files (poptGetArgs (context));
290
291         gtk_main ();
292
293         gnome_accelerators_sync ();
294         poptFreeContext (context);
295         ev_file_helpers_shutdown ();
296
297         if (enable_metadata) {
298                 ev_metadata_manager_shutdown ();
299         }
300
301         return 0;
302 }