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>
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>
38 #include <dbus/dbus-glib-bindings.h>
41 #include "ev-stock-icons.h"
43 #include "ev-job-queue.h"
44 #include "ev-file-helpers.h"
46 static char *ev_page_label;
48 static struct poptOption popt_options[] =
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 }
55 load_files (const char **files)
60 ev_application_open_window (EV_APP, GDK_CURRENT_TIME, NULL);
64 for (i = 0; files[i]; i++) {
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);
76 #ifndef HAVE_GTK_WINDOW_PRESENT_WITH_TIME
78 get_startup_time (void)
80 const char *envvar, *timestamp;
84 envvar = getenv ("DESKTOP_STARTUP_ID");
89 /* DESKTOP_STARTUP_ID is of form "<unique>_TIME<timestamp>".
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".
94 timestamp = rindex (envvar, 'T');
96 /* Maybe the word "TIME" was not found... */
97 if (timestamp == NULL || strncmp (timestamp, "TIME", 4))
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.
107 value = strtoul (timestamp, &end, 10);
109 /* unsigned long might be 64bit, so double-check! */
110 if (errno != 0 || *end != '\0' || value > G_MAXINT32)
118 load_files_remote (const char **files)
121 GError *error = NULL;
122 DBusGConnection *connection;
123 gboolean result = FALSE;
124 #if DBUS_VERSION < 35
125 DBusGPendingCall *call;
127 DBusGProxy *remote_object;
128 #ifdef HAVE_GTK_WINDOW_PRESENT_WITH_TIME
133 #ifdef HAVE_GTK_WINDOW_PRESENT_WITH_TIME
134 display = gdk_display_get_default();
135 timestamp = gdk_x11_display_get_user_time (display);
137 /* Fake it for GTK+2.6 */
138 timestamp = get_startup_time ();
140 connection = dbus_g_bus_get (DBUS_BUS_STARTER, &error);
141 if (connection == NULL) {
142 g_warning (error->message);
143 g_error_free (error);
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");
153 #if DBUS_VERSION <= 33
154 call = dbus_g_proxy_begin_call (remote_object, "OpenWindow",
155 DBUS_TYPE_UINT32, ×tamp,
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);
165 #elif DBUS_VERSION == 34
166 call = dbus_g_proxy_begin_call (remote_object, "OpenWindow",
167 G_TYPE_UINT, timestamp,
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);
178 if (!dbus_g_proxy_call (remote_object, "OpenWindow", &error,
179 G_TYPE_UINT, timestamp,
182 g_warning (error->message);
183 g_clear_error (&error);
184 g_object_unref (remote_object);
185 dbus_g_connection_unref (connection);
189 g_object_unref (remote_object);
190 dbus_g_connection_unref (connection);
195 for (i = 0; files[i]; i++) {
196 const char *page_label;
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, ×tamp,
208 if (!dbus_g_proxy_end_call (remote_object, call, &error, DBUS_TYPE_INVALID)) {
209 g_warning (error->message);
210 g_clear_error (&error);
214 #elif DBUS_VERSION == 34
215 call = dbus_g_proxy_begin_call (remote_object, "OpenURI",
217 G_TYPE_STRING, page_label,
218 G_TYPE_UINT, timestamp,
221 if (!dbus_g_proxy_end_call (remote_object, call, &error, G_TYPE_INVALID)) {
222 g_warning (error->message);
223 g_clear_error (&error);
228 if (!dbus_g_proxy_call (remote_object, "OpenURI", &error,
230 G_TYPE_STRING, page_label,
231 G_TYPE_UINT, timestamp,
234 g_warning (error->message);
235 g_clear_error (&error);
244 g_object_unref (remote_object);
245 dbus_g_connection_unref (connection);
247 gdk_notify_startup_complete ();
251 #endif /* ENABLE_DBUS */
254 main (int argc, char *argv[])
256 gboolean enable_metadata = FALSE;
258 GValue context_as_value = { 0 };
259 GnomeProgram *program;
262 /* Initialize the i18n stuff */
263 bindtextdomain(GETTEXT_PACKAGE, GNOMELOCALEDIR);
264 bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
265 textdomain(GETTEXT_PACKAGE);
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,
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);
281 if (!ev_application_register_service (EV_APP)) {
282 if (load_files_remote (poptGetArgs (context))) {
286 enable_metadata = TRUE;
290 gnome_authentication_manager_init ();
293 if (enable_metadata) {
294 ev_metadata_manager_init ();
297 ev_job_queue_init ();
298 g_set_application_name (_("Evince Document Viewer"));
300 ev_file_helpers_init ();
302 ev_stock_icons_init ();
303 gtk_window_set_default_icon_name ("evince");
305 load_files (poptGetArgs (context));
309 gnome_accelerators_sync ();
310 poptFreeContext (context);
311 ev_file_helpers_shutdown ();
313 if (enable_metadata) {
314 ev_metadata_manager_shutdown ();