2 * Copyright (C) 2004 Marco Pesenti Gritti
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)
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.
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.
23 #include "ev-application.h"
24 #include "ev-metadata-manager.h"
26 #include <glib/gi18n.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>
36 #include <dbus/dbus-glib-bindings.h>
39 #include "ev-stock-icons.h"
41 #include "ev-job-queue.h"
42 #include "ev-file-helpers.h"
44 static char *ev_page_label;
46 static struct poptOption popt_options[] =
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 }
53 load_files (const char **files)
58 ev_application_open_window (EV_APP, GDK_CURRENT_TIME, NULL);
62 for (i = 0; files[i]; i++) {
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);
74 #ifndef HAVE_GTK_WINDOW_PRESENT_WITH_TIME
76 get_startup_time (void)
78 const char *envvar, *timestamp;
82 envvar = getenv ("DESKTOP_STARTUP_ID");
87 /* DESKTOP_STARTUP_ID is of form "<unique>_TIME<timestamp>".
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".
92 timestamp = rindex (envvar, 'T');
94 /* Maybe the word "TIME" was not found... */
95 if (timestamp == NULL || strncmp (timestamp, "TIME", 4))
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.
105 value = strtoul (timestamp, &end, 10);
107 /* unsigned long might be 64bit, so double-check! */
108 if (errno != 0 || *end != '\0' || value > G_MAXINT32)
116 load_files_remote (const char **files)
119 GError *error = NULL;
120 DBusGConnection *connection;
121 gboolean result = FALSE;
122 #if DBUS_VERSION < 35
123 DBusGPendingCall *call;
125 DBusGProxy *remote_object;
129 #ifdef HAVE_GTK_WINDOW_PRESENT_WITH_TIME
130 display = gdk_display_get_default();
131 timestamp = gdk_x11_display_get_user_time (display);
133 /* Fake it for GTK+2.6 */
134 timestamp = get_startup_time ();
136 connection = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
137 if (connection == NULL) {
138 g_warning (error->message);
139 g_error_free (error);
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");
149 #if DBUS_VERSION <= 33
150 call = dbus_g_proxy_begin_call (remote_object, "OpenWindow",
151 DBUS_TYPE_UINT32, timestamp,
154 if (!dbus_g_proxy_end_call (remote_object, call, &error, DBUS_TYPE_INVALID)) {
155 g_warning (error->message);
156 g_clear_error (&error);
159 #elif DBUS_VERSION == 34
160 call = dbus_g_proxy_begin_call (remote_object, "OpenWindow",
161 G_TYPE_UINT, timestamp,
164 if (!dbus_g_proxy_end_call (remote_object, call, &error, G_TYPE_INVALID)) {
165 g_warning (error->message);
166 g_clear_error (&error);
170 if (!dbus_g_proxy_call (remote_object, "OpenWindow", &error,
171 G_TYPE_UINT, timestamp,
174 g_warning (error->message);
175 g_clear_error (&error);
182 for (i = 0; files[i]; i++) {
183 const char *page_label;
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,
195 if (!dbus_g_proxy_end_call (remote_object, call, &error, DBUS_TYPE_INVALID)) {
196 g_warning (error->message);
197 g_clear_error (&error);
201 #elif DBUS_VERSION == 34
202 call = dbus_g_proxy_begin_call (remote_object, "OpenURI",
204 G_TYPE_STRING, page_label,
205 G_TYPE_UINT, timestamp,
208 if (!dbus_g_proxy_end_call (remote_object, call, &error, G_TYPE_INVALID)) {
209 g_warning (error->message);
210 g_clear_error (&error);
215 if (!dbus_g_proxy_call (remote_object, "OpenURI", &error,
217 G_TYPE_STRING, page_label,
218 G_TYPE_UINT, timestamp,
221 g_warning (error->message);
222 g_clear_error (&error);
231 gdk_notify_startup_complete ();
235 #endif /* ENABLE_DBUS */
238 main (int argc, char *argv[])
240 gboolean enable_metadata = FALSE;
242 GValue context_as_value = { 0 };
243 GnomeProgram *program;
246 /* Initialize the i18n stuff */
247 bindtextdomain(GETTEXT_PACKAGE, GNOMELOCALEDIR);
248 bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
249 textdomain(GETTEXT_PACKAGE);
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,
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);
265 if (!ev_application_register_service (EV_APP)) {
266 if (load_files_remote (poptGetArgs (context))) {
270 enable_metadata = TRUE;
274 gnome_authentication_manager_init ();
277 if (enable_metadata) {
278 ev_metadata_manager_init ();
281 ev_job_queue_init ();
282 g_set_application_name (_("Evince Document Viewer"));
284 ev_file_helpers_init ();
286 ev_stock_icons_init ();
287 gtk_window_set_default_icon_name ("postscript-viewer");
289 load_files (poptGetArgs (context));
293 gnome_accelerators_sync ();
294 poptFreeContext (context);
295 ev_file_helpers_shutdown ();
297 if (enable_metadata) {
298 ev_metadata_manager_shutdown ();