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.
22 #include "ev-application.h"
23 #include "ev-metadata-manager.h"
25 #include <glib/gi18n.h>
27 #include <gtk/gtkmain.h>
30 #include <libgnome/gnome-program.h>
31 #include <libgnomeui/gnome-ui-init.h>
32 #include <libgnomeui/gnome-app-helper.h>
33 #include <libgnomeui/gnome-authentication-manager.h>
34 #include <libgnomevfs/gnome-vfs-utils.h>
37 #include <dbus/dbus-glib-bindings.h>
40 #include "ev-stock-icons.h"
41 #include "ev-job-queue.h"
42 #include "ev-file-helpers.h"
44 static gchar *ev_page_label;
45 static gboolean preview_mode = FALSE;
46 static gboolean fullscren_mode = FALSE;
47 static gboolean presentation_mode = FALSE;
48 static gboolean unlink_temp_file = FALSE;
49 static const char **file_arguments = NULL;
51 static const GOptionEntry goption_options[] =
53 { "page-label", 'p', 0, G_OPTION_ARG_STRING, &ev_page_label, N_("The page of the document to display."), N_("PAGE")},
54 { "fullscreen", 'f', 0, G_OPTION_ARG_NONE, &fullscren_mode, N_("Run evince in fullscreen mode"), NULL },
55 { "presentation", 's', 0, G_OPTION_ARG_NONE, &presentation_mode, N_("Run evince in presentation mode"), NULL },
56 { "preview", 'w', 0, G_OPTION_ARG_NONE, &preview_mode, N_("Run evince as a previewer"), NULL },
57 { "unlink-temp-file", 'u', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_NONE, &unlink_temp_file, NULL, NULL },
58 { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &file_arguments, NULL, N_("[FILE...]") },
63 value_free (GValue *value)
65 g_value_unset (value);
70 arguments_parse (void)
77 const gchar *display_name;
80 args = g_hash_table_new_full (g_str_hash,
82 (GDestroyNotify)g_free,
83 (GDestroyNotify)value_free);
85 screen = gdk_screen_get_default ();
86 display = gdk_screen_get_display (screen);
88 display_name = gdk_display_get_name (display);
89 screen_number = gdk_screen_get_number (screen);
91 value = g_new0 (GValue, 1);
92 g_value_init (value, G_TYPE_STRING);
93 g_value_set_string (value, display_name);
94 g_hash_table_insert (args, g_strdup ("display"), value);
96 value = g_new0 (GValue, 1);
97 g_value_init (value, G_TYPE_INT);
98 g_value_set_int (value, screen_number);
99 g_hash_table_insert (args, g_strdup ("screen"), value);
102 value = g_new0 (GValue, 1);
103 g_value_init (value, G_TYPE_STRING);
104 g_value_set_string (value, ev_page_label);
106 g_hash_table_insert (args, g_strdup ("page-label"), value);
110 mode = EV_WINDOW_MODE_FULLSCREEN;
111 else if (presentation_mode)
112 mode = EV_WINDOW_MODE_PRESENTATION;
113 else if (preview_mode)
114 mode = EV_WINDOW_MODE_PREVIEW;
118 value = g_new0 (GValue, 1);
119 g_value_init (value, G_TYPE_UINT);
120 g_value_set_uint (value, mode);
122 g_hash_table_insert (args, g_strdup ("mode"), value);
124 if (mode == EV_WINDOW_MODE_PREVIEW && unlink_temp_file) {
125 value = g_new0 (GValue, 1);
126 g_value_init (value, G_TYPE_BOOLEAN);
127 g_value_set_boolean (value, unlink_temp_file);
129 g_hash_table_insert (args,
130 g_strdup ("unlink-temp-file"),
138 load_files (const char **files,
144 ev_application_open_window (EV_APP, args, GDK_CURRENT_TIME, NULL);
148 for (i = 0; files[i]; i++) {
153 uri = gnome_vfs_make_uri_from_shell_arg (files[i]);
155 label = strchr (uri, GNOME_VFS_URI_MAGIC_CHR);
162 old = g_hash_table_lookup (args, "page-label");
164 new = g_new0 (GValue, 1);
165 g_value_init (new, G_TYPE_STRING);
166 g_value_set_string (new, label);
168 g_hash_table_insert (args, g_strdup ("page-label"), new);
172 ev_application_open_uri (EV_APP, uri, args,
173 GDK_CURRENT_TIME, NULL);
176 g_hash_table_insert (args, g_strdup ("page-label"), old);
185 load_files_remote (const char **files,
189 GError *error = NULL;
190 DBusGConnection *connection;
191 gboolean result = FALSE;
192 #if DBUS_VERSION < 35
193 DBusGPendingCall *call;
195 DBusGProxy *remote_object;
199 display = gdk_display_get_default ();
200 timestamp = gdk_x11_display_get_user_time (display);
201 connection = dbus_g_bus_get (DBUS_BUS_STARTER, &error);
203 if (connection == NULL) {
204 g_warning (error->message);
205 g_error_free (error);
210 remote_object = dbus_g_proxy_new_for_name (connection,
211 "org.gnome.evince.ApplicationService",
212 "/org/gnome/evince/Evince",
213 "org.gnome.evince.Application");
215 #if DBUS_VERSION <= 33
216 call = dbus_g_proxy_begin_call (remote_object, "OpenWindow",
217 DBUS_TYPE_UINT32, ×tamp,
220 if (!dbus_g_proxy_end_call (remote_object, call, &error, DBUS_TYPE_INVALID)) {
221 g_warning (error->message);
222 g_clear_error (&error);
223 g_object_unref (remote_object);
224 dbus_g_connection_unref (connection);
227 #elif DBUS_VERSION == 34
228 call = dbus_g_proxy_begin_call (remote_object, "OpenWindow",
229 G_TYPE_UINT, timestamp,
232 if (!dbus_g_proxy_end_call (remote_object, call, &error, G_TYPE_INVALID)) {
233 g_warning (error->message);
234 g_clear_error (&error);
235 g_object_unref (remote_object);
236 dbus_g_connection_unref (connection);
240 if (!dbus_g_proxy_call (remote_object, "OpenWindow", &error,
241 dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE), args,
242 G_TYPE_UINT, timestamp,
245 g_warning (error->message);
246 g_clear_error (&error);
247 g_object_unref (remote_object);
248 dbus_g_connection_unref (connection);
252 g_object_unref (remote_object);
253 dbus_g_connection_unref (connection);
258 for (i = 0; files[i]; i++) {
259 const char *page_label;
262 uri = gnome_vfs_make_uri_from_shell_arg (files[i]);
263 page_label = ev_page_label ? ev_page_label : "";
264 #if DBUS_VERSION <= 33
265 call = dbus_g_proxy_begin_call (remote_object, "OpenURI",
266 DBUS_TYPE_STRING, &uri,
267 DBUS_TYPE_STRING, &page_label,
268 DBUS_TYPE_UINT32, ×tamp,
271 if (!dbus_g_proxy_end_call (remote_object, call, &error, DBUS_TYPE_INVALID)) {
272 g_warning (error->message);
273 g_clear_error (&error);
277 #elif DBUS_VERSION == 34
278 call = dbus_g_proxy_begin_call (remote_object, "OpenURI",
280 G_TYPE_STRING, page_label,
281 G_TYPE_UINT, timestamp,
284 if (!dbus_g_proxy_end_call (remote_object, call, &error, G_TYPE_INVALID)) {
285 g_warning (error->message);
286 g_clear_error (&error);
291 if (!dbus_g_proxy_call (remote_object, "OpenURI", &error,
293 dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE), args,
294 G_TYPE_UINT, timestamp,
297 g_warning (error->message);
298 g_clear_error (&error);
307 g_object_unref (remote_object);
308 dbus_g_connection_unref (connection);
310 gdk_notify_startup_complete ();
314 #endif /* ENABLE_DBUS */
317 main (int argc, char *argv[])
319 gboolean enable_metadata = FALSE;
320 GOptionContext *context;
322 GnomeProgram *program;
324 context = g_option_context_new (_("GNOME Document Viewer"));
327 /* Initialize the i18n stuff */
328 bindtextdomain(GETTEXT_PACKAGE, GNOMELOCALEDIR);
329 bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
330 textdomain(GETTEXT_PACKAGE);
331 g_option_context_add_main_entries (context, goption_options, GETTEXT_PACKAGE);
333 g_option_context_add_main_entries (context, goption_options, NULL);
336 program = gnome_program_init (PACKAGE, VERSION,
337 LIBGNOMEUI_MODULE, argc, argv,
338 GNOME_PARAM_GOPTION_CONTEXT, context,
339 GNOME_PARAM_HUMAN_READABLE_NAME, _("Evince"),
340 GNOME_PARAM_APP_DATADIR, GNOMEDATADIR,
343 args = arguments_parse ();
346 if (!ev_application_register_service (EV_APP)) {
347 if (load_files_remote (file_arguments, args)) {
348 g_hash_table_destroy (args);
349 g_object_unref (program);
354 enable_metadata = TRUE;
359 gnome_authentication_manager_init ();
361 if (enable_metadata) {
362 ev_metadata_manager_init ();
365 ev_job_queue_init ();
366 g_set_application_name (_("Evince Document Viewer"));
368 ev_file_helpers_init ();
369 ev_stock_icons_init ();
370 gtk_window_set_default_icon_name ("evince");
372 load_files (file_arguments, args);
373 g_hash_table_destroy (args);
377 gnome_accelerators_sync ();
378 ev_file_helpers_shutdown ();
380 if (enable_metadata) {
381 ev_metadata_manager_shutdown ();
383 g_object_unref (program);