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 gchar *ev_page_label;
47 static gboolean preview_mode = FALSE;
48 static gboolean fullscren_mode = FALSE;
49 static gboolean presentation_mode = FALSE;
50 static const char **file_arguments = NULL;
52 static const GOptionEntry goption_options[] =
54 { "page-label", 'p', 0, G_OPTION_ARG_STRING, &ev_page_label, N_("The page of the document to display."), N_("PAGE")},
55 { "fullscreen", 'f', 0, G_OPTION_ARG_NONE, &fullscren_mode, N_("Run evince in fullscreen mode"), NULL },
56 { "presentation", 's', 0, G_OPTION_ARG_NONE, &presentation_mode, N_("Run evince in presentation mode"), NULL },
57 { "preview", 'w', 0, G_OPTION_ARG_NONE, &preview_mode, N_("Run evince as a previewer"), 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);
128 load_files (const char **files,
134 ev_application_open_window (EV_APP, args, GDK_CURRENT_TIME, NULL);
138 for (i = 0; files[i]; i++) {
143 uri = gnome_vfs_make_uri_from_shell_arg (files[i]);
145 label = strchr (uri, GNOME_VFS_URI_MAGIC_CHR);
152 old = g_hash_table_lookup (args, "page-label");
154 new = g_new0 (GValue, 1);
155 g_value_init (new, G_TYPE_STRING);
156 g_value_set_string (new, label);
158 g_hash_table_insert (args, g_strdup ("page-label"), new);
162 ev_application_open_uri (EV_APP, uri, args,
163 GDK_CURRENT_TIME, NULL);
166 g_hash_table_insert (args, g_strdup ("page-label"), old);
175 load_files_remote (const char **files,
179 GError *error = NULL;
180 DBusGConnection *connection;
181 gboolean result = FALSE;
182 #if DBUS_VERSION < 35
183 DBusGPendingCall *call;
185 DBusGProxy *remote_object;
189 display = gdk_display_get_default ();
190 timestamp = gdk_x11_display_get_user_time (display);
191 connection = dbus_g_bus_get (DBUS_BUS_STARTER, &error);
193 if (connection == NULL) {
194 g_warning (error->message);
195 g_error_free (error);
200 remote_object = dbus_g_proxy_new_for_name (connection,
201 "org.gnome.evince.ApplicationService",
202 "/org/gnome/evince/Evince",
203 "org.gnome.evince.Application");
205 #if DBUS_VERSION <= 33
206 call = dbus_g_proxy_begin_call (remote_object, "OpenWindow",
207 DBUS_TYPE_UINT32, ×tamp,
210 if (!dbus_g_proxy_end_call (remote_object, call, &error, DBUS_TYPE_INVALID)) {
211 g_warning (error->message);
212 g_clear_error (&error);
213 g_object_unref (remote_object);
214 dbus_g_connection_unref (connection);
217 #elif DBUS_VERSION == 34
218 call = dbus_g_proxy_begin_call (remote_object, "OpenWindow",
219 G_TYPE_UINT, timestamp,
222 if (!dbus_g_proxy_end_call (remote_object, call, &error, G_TYPE_INVALID)) {
223 g_warning (error->message);
224 g_clear_error (&error);
225 g_object_unref (remote_object);
226 dbus_g_connection_unref (connection);
230 if (!dbus_g_proxy_call (remote_object, "OpenWindow", &error,
231 dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE), args,
232 G_TYPE_UINT, timestamp,
235 g_warning (error->message);
236 g_clear_error (&error);
237 g_object_unref (remote_object);
238 dbus_g_connection_unref (connection);
242 g_object_unref (remote_object);
243 dbus_g_connection_unref (connection);
248 for (i = 0; files[i]; i++) {
249 const char *page_label;
252 uri = gnome_vfs_make_uri_from_shell_arg (files[i]);
253 page_label = ev_page_label ? ev_page_label : "";
254 #if DBUS_VERSION <= 33
255 call = dbus_g_proxy_begin_call (remote_object, "OpenURI",
256 DBUS_TYPE_STRING, &uri,
257 DBUS_TYPE_STRING, &page_label,
258 DBUS_TYPE_UINT32, ×tamp,
261 if (!dbus_g_proxy_end_call (remote_object, call, &error, DBUS_TYPE_INVALID)) {
262 g_warning (error->message);
263 g_clear_error (&error);
267 #elif DBUS_VERSION == 34
268 call = dbus_g_proxy_begin_call (remote_object, "OpenURI",
270 G_TYPE_STRING, page_label,
271 G_TYPE_UINT, timestamp,
274 if (!dbus_g_proxy_end_call (remote_object, call, &error, G_TYPE_INVALID)) {
275 g_warning (error->message);
276 g_clear_error (&error);
281 if (!dbus_g_proxy_call (remote_object, "OpenURI", &error,
283 dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE), args,
284 G_TYPE_UINT, timestamp,
287 g_warning (error->message);
288 g_clear_error (&error);
297 g_object_unref (remote_object);
298 dbus_g_connection_unref (connection);
300 gdk_notify_startup_complete ();
304 #endif /* ENABLE_DBUS */
307 main (int argc, char *argv[])
309 gboolean enable_metadata = FALSE;
310 GOptionContext *context;
312 GnomeProgram *program;
314 context = g_option_context_new (_("GNOME Document Viewer"));
317 /* Initialize the i18n stuff */
318 bindtextdomain(GETTEXT_PACKAGE, GNOMELOCALEDIR);
319 bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
320 textdomain(GETTEXT_PACKAGE);
321 g_option_context_add_main_entries (context, goption_options, GETTEXT_PACKAGE);
323 g_option_context_add_main_entries (context, goption_options, NULL);
326 program = gnome_program_init (PACKAGE, VERSION,
327 LIBGNOMEUI_MODULE, argc, argv,
328 GNOME_PARAM_GOPTION_CONTEXT, context,
329 GNOME_PARAM_HUMAN_READABLE_NAME, _("Evince"),
330 GNOME_PARAM_APP_DATADIR, GNOMEDATADIR,
333 args = arguments_parse ();
336 if (!ev_application_register_service (EV_APP)) {
337 if (load_files_remote (file_arguments, args)) {
338 g_hash_table_destroy (args);
339 g_object_unref (program);
344 enable_metadata = TRUE;
349 gnome_authentication_manager_init ();
351 if (enable_metadata) {
352 ev_metadata_manager_init ();
355 ev_job_queue_init ();
356 g_set_application_name (_("Evince Document Viewer"));
358 ev_file_helpers_init ();
360 ev_stock_icons_init ();
361 gtk_window_set_default_icon_name ("evince");
363 load_files (file_arguments, args);
364 g_hash_table_destroy (args);
368 gnome_accelerators_sync ();
369 ev_file_helpers_shutdown ();
371 if (enable_metadata) {
372 ev_metadata_manager_shutdown ();
374 g_object_unref (program);