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)
76 args = g_hash_table_new_full (g_str_hash,
78 (GDestroyNotify)g_free,
79 (GDestroyNotify)value_free);
82 value = g_new0 (GValue, 1);
83 g_value_init (value, G_TYPE_STRING);
84 g_value_set_string (value, ev_page_label);
86 g_hash_table_insert (args, g_strdup ("page-label"), value);
90 mode = EV_WINDOW_MODE_FULLSCREEN;
91 else if (presentation_mode)
92 mode = EV_WINDOW_MODE_PRESENTATION;
93 else if (preview_mode)
94 mode = EV_WINDOW_MODE_PREVIEW;
98 value = g_new0 (GValue, 1);
99 g_value_init (value, G_TYPE_UINT);
100 g_value_set_uint (value, mode);
102 g_hash_table_insert (args, g_strdup ("mode"), value);
108 load_files (const char **files,
114 ev_application_open_window (EV_APP, GDK_CURRENT_TIME, NULL);
118 for (i = 0; files[i]; i++) {
123 uri = gnome_vfs_make_uri_from_shell_arg (files[i]);
125 label = strchr (uri, GNOME_VFS_URI_MAGIC_CHR);
132 old = g_hash_table_lookup (args, "page-label");
134 new = g_new0 (GValue, 1);
135 g_value_init (new, G_TYPE_STRING);
136 g_value_set_string (new, label);
138 g_hash_table_insert (args, g_strdup ("page-label"), new);
142 ev_application_open_uri (EV_APP, uri, args,
143 GDK_CURRENT_TIME, NULL);
146 g_hash_table_insert (args, g_strdup ("page-label"), old);
155 load_files_remote (const char **files,
159 GError *error = NULL;
160 DBusGConnection *connection;
161 gboolean result = FALSE;
162 #if DBUS_VERSION < 35
163 DBusGPendingCall *call;
165 DBusGProxy *remote_object;
169 display = gdk_display_get_default();
170 timestamp = gdk_x11_display_get_user_time (display);
171 connection = dbus_g_bus_get (DBUS_BUS_STARTER, &error);
173 if (connection == NULL) {
174 g_warning (error->message);
175 g_error_free (error);
180 remote_object = dbus_g_proxy_new_for_name (connection,
181 "org.gnome.evince.ApplicationService",
182 "/org/gnome/evince/Evince",
183 "org.gnome.evince.Application");
185 #if DBUS_VERSION <= 33
186 call = dbus_g_proxy_begin_call (remote_object, "OpenWindow",
187 DBUS_TYPE_UINT32, ×tamp,
190 if (!dbus_g_proxy_end_call (remote_object, call, &error, DBUS_TYPE_INVALID)) {
191 g_warning (error->message);
192 g_clear_error (&error);
193 g_object_unref (remote_object);
194 dbus_g_connection_unref (connection);
197 #elif DBUS_VERSION == 34
198 call = dbus_g_proxy_begin_call (remote_object, "OpenWindow",
199 G_TYPE_UINT, timestamp,
202 if (!dbus_g_proxy_end_call (remote_object, call, &error, G_TYPE_INVALID)) {
203 g_warning (error->message);
204 g_clear_error (&error);
205 g_object_unref (remote_object);
206 dbus_g_connection_unref (connection);
210 if (!dbus_g_proxy_call (remote_object, "OpenWindow", &error,
211 G_TYPE_UINT, timestamp,
214 g_warning (error->message);
215 g_clear_error (&error);
216 g_object_unref (remote_object);
217 dbus_g_connection_unref (connection);
221 g_object_unref (remote_object);
222 dbus_g_connection_unref (connection);
227 for (i = 0; files[i]; i++) {
228 const char *page_label;
231 uri = gnome_vfs_make_uri_from_shell_arg (files[i]);
232 page_label = ev_page_label ? ev_page_label : "";
233 #if DBUS_VERSION <= 33
234 call = dbus_g_proxy_begin_call (remote_object, "OpenURI",
235 DBUS_TYPE_STRING, &uri,
236 DBUS_TYPE_STRING, &page_label,
237 DBUS_TYPE_UINT32, ×tamp,
240 if (!dbus_g_proxy_end_call (remote_object, call, &error, DBUS_TYPE_INVALID)) {
241 g_warning (error->message);
242 g_clear_error (&error);
246 #elif DBUS_VERSION == 34
247 call = dbus_g_proxy_begin_call (remote_object, "OpenURI",
249 G_TYPE_STRING, page_label,
250 G_TYPE_UINT, timestamp,
253 if (!dbus_g_proxy_end_call (remote_object, call, &error, G_TYPE_INVALID)) {
254 g_warning (error->message);
255 g_clear_error (&error);
260 if (!dbus_g_proxy_call (remote_object, "OpenURI", &error,
262 dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE), args,
263 G_TYPE_UINT, timestamp,
266 g_warning (error->message);
267 g_clear_error (&error);
276 g_object_unref (remote_object);
277 dbus_g_connection_unref (connection);
279 gdk_notify_startup_complete ();
283 #endif /* ENABLE_DBUS */
286 main (int argc, char *argv[])
288 gboolean enable_metadata = FALSE;
289 GOptionContext *context;
291 GnomeProgram *program;
293 context = g_option_context_new (_("GNOME Document Viewer"));
296 /* Initialize the i18n stuff */
297 bindtextdomain(GETTEXT_PACKAGE, GNOMELOCALEDIR);
298 bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
299 textdomain(GETTEXT_PACKAGE);
300 g_option_context_add_main_entries (context, goption_options, GETTEXT_PACKAGE);
302 g_option_context_add_main_entries (context, goption_options, NULL);
305 program = gnome_program_init (PACKAGE, VERSION,
306 LIBGNOMEUI_MODULE, argc, argv,
307 GNOME_PARAM_GOPTION_CONTEXT, context,
308 GNOME_PARAM_HUMAN_READABLE_NAME, _("Evince"),
309 GNOME_PARAM_APP_DATADIR, GNOMEDATADIR,
312 args = arguments_parse ();
315 if (!ev_application_register_service (EV_APP)) {
316 if (load_files_remote (file_arguments, args)) {
317 g_hash_table_destroy (args);
322 enable_metadata = TRUE;
327 gnome_authentication_manager_init ();
329 if (enable_metadata) {
330 ev_metadata_manager_init ();
333 ev_job_queue_init ();
334 g_set_application_name (_("Evince Document Viewer"));
336 ev_file_helpers_init ();
338 ev_stock_icons_init ();
339 gtk_window_set_default_icon_name ("evince");
341 load_files (file_arguments, args);
342 g_hash_table_destroy (args);
346 gnome_accelerators_sync ();
347 ev_file_helpers_shutdown ();
349 if (enable_metadata) {
350 ev_metadata_manager_shutdown ();
352 g_object_unref (program);