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_SESSION, &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, timestamp,
158 if (!dbus_g_proxy_end_call (remote_object, call, &error, DBUS_TYPE_INVALID)) {
159 g_warning (error->message);
160 g_clear_error (&error);
163 #elif DBUS_VERSION == 34
164 call = dbus_g_proxy_begin_call (remote_object, "OpenWindow",
165 G_TYPE_UINT, timestamp,
168 if (!dbus_g_proxy_end_call (remote_object, call, &error, G_TYPE_INVALID)) {
169 g_warning (error->message);
170 g_clear_error (&error);
174 if (!dbus_g_proxy_call (remote_object, "OpenWindow", &error,
175 G_TYPE_UINT, timestamp,
178 g_warning (error->message);
179 g_clear_error (&error);
186 for (i = 0; files[i]; i++) {
187 const char *page_label;
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,
199 if (!dbus_g_proxy_end_call (remote_object, call, &error, DBUS_TYPE_INVALID)) {
200 g_warning (error->message);
201 g_clear_error (&error);
205 #elif DBUS_VERSION == 34
206 call = dbus_g_proxy_begin_call (remote_object, "OpenURI",
208 G_TYPE_STRING, page_label,
209 G_TYPE_UINT, timestamp,
212 if (!dbus_g_proxy_end_call (remote_object, call, &error, G_TYPE_INVALID)) {
213 g_warning (error->message);
214 g_clear_error (&error);
219 if (!dbus_g_proxy_call (remote_object, "OpenURI", &error,
221 G_TYPE_STRING, page_label,
222 G_TYPE_UINT, timestamp,
225 g_warning (error->message);
226 g_clear_error (&error);
235 gdk_notify_startup_complete ();
239 #endif /* ENABLE_DBUS */
242 main (int argc, char *argv[])
244 gboolean enable_metadata = FALSE;
246 GValue context_as_value = { 0 };
247 GnomeProgram *program;
250 /* Initialize the i18n stuff */
251 bindtextdomain(GETTEXT_PACKAGE, GNOMELOCALEDIR);
252 bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
253 textdomain(GETTEXT_PACKAGE);
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,
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);
269 if (!ev_application_register_service (EV_APP)) {
270 if (load_files_remote (poptGetArgs (context))) {
274 enable_metadata = TRUE;
278 gnome_authentication_manager_init ();
281 if (enable_metadata) {
282 ev_metadata_manager_init ();
285 ev_job_queue_init ();
286 g_set_application_name (_("Evince Document Viewer"));
288 ev_file_helpers_init ();
290 ev_stock_icons_init ();
291 gtk_window_set_default_icon_name ("postscript-viewer");
293 load_files (poptGetArgs (context));
297 gnome_accelerators_sync ();
298 poptFreeContext (context);
299 ev_file_helpers_shutdown ();
301 if (enable_metadata) {
302 ev_metadata_manager_shutdown ();