]> www.fi.muni.cz Git - evince.git/blob - shell/main.c
Last minute GTK-2.6 fixes. 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 <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_SESSION, &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                         return FALSE;
162                 }
163 #elif DBUS_VERSION == 34
164                 call = dbus_g_proxy_begin_call (remote_object, "OpenWindow",
165                                                 G_TYPE_UINT, timestamp,
166                                                 G_TYPE_INVALID);
167
168                 if (!dbus_g_proxy_end_call (remote_object, call, &error, G_TYPE_INVALID)) {
169                         g_warning (error->message);
170                         g_clear_error (&error);
171                         return FALSE;
172                 }
173 #else
174                 if (!dbus_g_proxy_call (remote_object, "OpenWindow", &error,
175                                         G_TYPE_UINT, timestamp,
176                                         G_TYPE_INVALID,
177                                         G_TYPE_INVALID)) {
178                         g_warning (error->message);
179                         g_clear_error (&error);
180                         return FALSE;
181                 }
182 #endif
183                 return TRUE;
184         }
185
186         for (i = 0; files[i]; i++) {
187                 const char *page_label;
188                 char *uri;
189
190                 uri = gnome_vfs_make_uri_from_shell_arg (files[i]);
191                 page_label = ev_page_label ? ev_page_label : ""; 
192 #if DBUS_VERSION <= 33
193                 call = dbus_g_proxy_begin_call (remote_object, "OpenURI",
194                                                 DBUS_TYPE_STRING, &uri,
195                                                 DBUS_TYPE_STRING, &page_label,
196                                                 DBUS_TYPE_UINT32, timestamp,
197                                                 DBUS_TYPE_INVALID);
198
199                 if (!dbus_g_proxy_end_call (remote_object, call, &error, DBUS_TYPE_INVALID)) {
200                         g_warning (error->message);
201                         g_clear_error (&error);
202                         g_free (uri);
203                         continue;
204                 }
205 #elif DBUS_VERSION == 34
206                 call = dbus_g_proxy_begin_call (remote_object, "OpenURI",
207                                                 G_TYPE_STRING, uri,
208                                                 G_TYPE_STRING, page_label,
209                                                 G_TYPE_UINT, timestamp,
210                                                 G_TYPE_INVALID);
211
212                 if (!dbus_g_proxy_end_call (remote_object, call, &error, G_TYPE_INVALID)) {
213                         g_warning (error->message);
214                         g_clear_error (&error);
215                         g_free (uri);
216                         continue;
217                 }
218 #else
219                 if (!dbus_g_proxy_call (remote_object, "OpenURI", &error,
220                                         G_TYPE_STRING, uri,
221                                         G_TYPE_STRING, page_label,
222                                         G_TYPE_UINT, timestamp,
223                                         G_TYPE_INVALID,
224                                         G_TYPE_INVALID)) {
225                         g_warning (error->message);
226                         g_clear_error (&error);
227                         g_free (uri);
228                         continue;
229                 }
230 #endif
231                 g_free (uri);
232                 result = TRUE;
233         }
234
235         gdk_notify_startup_complete ();
236
237         return result;
238 }
239 #endif /* ENABLE_DBUS */
240
241 int
242 main (int argc, char *argv[])
243 {
244         gboolean enable_metadata = FALSE;
245         poptContext context;
246         GValue context_as_value = { 0 };
247         GnomeProgram *program;
248
249 #ifdef ENABLE_NLS
250         /* Initialize the i18n stuff */
251         bindtextdomain(GETTEXT_PACKAGE, GNOMELOCALEDIR);
252         bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
253         textdomain(GETTEXT_PACKAGE);
254 #endif
255
256         program = gnome_program_init (PACKAGE, VERSION,
257                                       LIBGNOMEUI_MODULE, argc, argv,
258                                       GNOME_PARAM_POPT_TABLE, popt_options,
259                                       GNOME_PARAM_HUMAN_READABLE_NAME, _("Evince"),
260                                       GNOME_PARAM_APP_DATADIR, GNOMEDATADIR,
261                                       NULL);
262         g_object_get_property (G_OBJECT (program),
263                                GNOME_PARAM_POPT_CONTEXT,
264                                g_value_init (&context_as_value, G_TYPE_POINTER));
265         context = g_value_get_pointer (&context_as_value);
266
267
268 #ifdef ENABLE_DBUS
269         if (!ev_application_register_service (EV_APP)) {
270                 if (load_files_remote (poptGetArgs (context))) {
271                         return 0;
272                 }
273         } else {
274                 enable_metadata = TRUE;
275         }
276 #endif
277
278         gnome_authentication_manager_init ();
279
280
281         if (enable_metadata) {
282                 ev_metadata_manager_init ();
283         }
284
285         ev_job_queue_init ();
286         g_set_application_name (_("Evince Document Viewer"));
287
288         ev_file_helpers_init ();
289         ev_debug_init ();
290         ev_stock_icons_init ();
291         gtk_window_set_default_icon_name ("postscript-viewer");
292
293         load_files (poptGetArgs (context));
294
295         gtk_main ();
296
297         gnome_accelerators_sync ();
298         poptFreeContext (context);
299         ev_file_helpers_shutdown ();
300
301         if (enable_metadata) {
302                 ev_metadata_manager_shutdown ();
303         }
304
305         return 0;
306 }