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.
24 #include <glib/gi18n.h>
29 #include <libgnome/gnome-program.h>
30 #include <libgnomeui/gnome-ui-init.h>
31 #include <libgnomeui/gnome-app-helper.h>
32 #include <libgnomeui/gnome-authentication-manager.h>
36 #include <dbus/dbus-glib-bindings.h>
39 #include "ev-application.h"
40 #include "ev-backends-manager.h"
42 #include "ev-file-helpers.h"
43 #include "ev-metadata-manager.h"
44 #include "ev-stock-icons.h"
46 static gchar *ev_page_label;
47 static gchar *ev_find_string;
48 static gboolean preview_mode = FALSE;
49 static gboolean fullscren_mode = FALSE;
50 static gboolean presentation_mode = FALSE;
51 static gboolean unlink_temp_file = FALSE;
52 static gchar *print_settings;
53 static const char **file_arguments = NULL;
55 static const GOptionEntry goption_options[] =
57 { "page-label", 'p', 0, G_OPTION_ARG_STRING, &ev_page_label, N_("The page of the document to display."), N_("PAGE")},
58 { "fullscreen", 'f', 0, G_OPTION_ARG_NONE, &fullscren_mode, N_("Run evince in fullscreen mode"), NULL },
59 { "presentation", 's', 0, G_OPTION_ARG_NONE, &presentation_mode, N_("Run evince in presentation mode"), NULL },
60 { "preview", 'w', 0, G_OPTION_ARG_NONE, &preview_mode, N_("Run evince as a previewer"), NULL },
61 { "find", 'l', 0, G_OPTION_ARG_STRING, &ev_find_string, N_("The word or phrase to find in the document"), N_("STRING")},
62 { "unlink-tempfile", 'u', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_NONE, &unlink_temp_file, NULL, NULL },
63 { "print-settings", 't', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_FILENAME, &print_settings, NULL, NULL },
64 { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &file_arguments, NULL, N_("[FILE...]") },
69 value_free (GValue *value)
71 g_value_unset (value);
78 * Parses the arguments and creates a #GHashTable with this data.
82 * dislay -> display at the default screen.
83 * screen -> screen number.
84 * page-label -> only if the page label argument has been passed,
85 * the page of the document to display.
86 * mode -> only if the view mode is one of the availables,
88 * unlink-temp-file -> only if the view mode is preview mode and
89 * unlink-temp-file has been passed, unlink-temp-file.
91 * Returns: a pointer into #GHashTable with data from the arguments.
94 arguments_parse (void)
101 const gchar *display_name;
104 args = g_hash_table_new_full (g_str_hash,
106 (GDestroyNotify)g_free,
107 (GDestroyNotify)value_free);
109 screen = gdk_screen_get_default ();
110 display = gdk_screen_get_display (screen);
112 display_name = gdk_display_get_name (display);
113 screen_number = gdk_screen_get_number (screen);
115 value = g_new0 (GValue, 1);
116 g_value_init (value, G_TYPE_STRING);
117 g_value_set_string (value, display_name);
118 g_hash_table_insert (args, g_strdup ("display"), value);
120 value = g_new0 (GValue, 1);
121 g_value_init (value, G_TYPE_INT);
122 g_value_set_int (value, screen_number);
123 g_hash_table_insert (args, g_strdup ("screen"), value);
126 value = g_new0 (GValue, 1);
127 g_value_init (value, G_TYPE_STRING);
128 g_value_set_string (value, ev_page_label);
130 g_hash_table_insert (args, g_strdup ("page-label"), value);
132 g_free (ev_page_label);
133 ev_page_label = NULL;
136 if (ev_find_string) {
137 value = g_new0 (GValue, 1);
138 g_value_init (value, G_TYPE_STRING);
139 g_value_set_string (value, ev_find_string);
141 g_hash_table_insert (args, g_strdup ("find-string"), value);
143 g_free (ev_find_string);
144 ev_page_label = NULL;
148 mode = EV_WINDOW_MODE_FULLSCREEN;
149 else if (presentation_mode)
150 mode = EV_WINDOW_MODE_PRESENTATION;
151 else if (preview_mode)
152 mode = EV_WINDOW_MODE_PREVIEW;
156 value = g_new0 (GValue, 1);
157 g_value_init (value, G_TYPE_UINT);
158 g_value_set_uint (value, mode);
160 g_hash_table_insert (args, g_strdup ("mode"), value);
162 if (mode == EV_WINDOW_MODE_PREVIEW && unlink_temp_file) {
163 value = g_new0 (GValue, 1);
164 g_value_init (value, G_TYPE_BOOLEAN);
165 g_value_set_boolean (value, unlink_temp_file);
167 g_hash_table_insert (args,
168 g_strdup ("unlink-temp-file"),
172 if (mode == EV_WINDOW_MODE_PREVIEW && print_settings) {
173 value = g_new0 (GValue, 1);
174 g_value_init (value, G_TYPE_STRING);
175 g_value_set_string (value, print_settings);
177 g_hash_table_insert (args,
178 g_strdup ("print-settings"),
180 g_free (print_settings);
181 print_settings = NULL;
188 load_files (const char **files,
194 ev_application_open_window (EV_APP, args, GDK_CURRENT_TIME, NULL);
198 for (i = 0; files[i]; i++) {
204 file = g_file_new_for_commandline_arg (files[i]);
205 uri = g_file_get_uri (file);
206 g_object_unref (file);
208 label = strchr (uri, '#');
215 old = g_hash_table_lookup (args, "page-label");
217 new = g_new0 (GValue, 1);
218 g_value_init (new, G_TYPE_STRING);
219 g_value_set_string (new, label);
221 g_hash_table_insert (args, g_strdup ("page-label"), new);
225 ev_application_open_uri (EV_APP, uri, args,
226 GDK_CURRENT_TIME, NULL);
229 g_hash_table_insert (args, g_strdup ("page-label"), old);
237 load_files_remote (const char **files,
241 GError *error = NULL;
242 DBusGConnection *connection;
243 gboolean result = FALSE;
244 DBusGProxy *remote_object;
248 display = gdk_display_get_default ();
249 timestamp = gdk_x11_display_get_user_time (display);
250 connection = dbus_g_bus_get (DBUS_BUS_STARTER, &error);
252 if (connection == NULL) {
253 g_warning ("%s", error->message);
254 g_error_free (error);
259 remote_object = dbus_g_proxy_new_for_name (connection,
260 "org.gnome.evince.ApplicationService",
261 "/org/gnome/evince/Evince",
262 "org.gnome.evince.Application");
264 if (!dbus_g_proxy_call (remote_object, "OpenWindow", &error,
265 dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE), args,
266 G_TYPE_UINT, timestamp,
269 g_warning ("%s", error->message);
270 g_clear_error (&error);
271 g_object_unref (remote_object);
272 dbus_g_connection_unref (connection);
276 g_object_unref (remote_object);
277 dbus_g_connection_unref (connection);
282 for (i = 0; files[i]; i++) {
283 const char *page_label;
287 file = g_file_new_for_commandline_arg (files[i]);
288 uri = g_file_get_uri (file);
289 g_object_unref (file);
291 page_label = ev_page_label ? ev_page_label : "";
293 if (!dbus_g_proxy_call (remote_object, "OpenURI", &error,
295 dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE), args,
296 G_TYPE_UINT, timestamp,
299 g_warning ("%s", error->message);
300 g_clear_error (&error);
309 g_object_unref (remote_object);
310 dbus_g_connection_unref (connection);
312 gdk_notify_startup_complete ();
316 #endif /* ENABLE_DBUS */
319 main (int argc, char *argv[])
321 gboolean enable_metadata = FALSE;
322 GOptionContext *context;
325 GnomeProgram *program;
327 char *accel_filename;
328 GError *error = NULL;
331 context = g_option_context_new (_("GNOME Document Viewer"));
334 /* Initialize the i18n stuff */
335 bindtextdomain(GETTEXT_PACKAGE, GNOMELOCALEDIR);
336 bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
337 textdomain(GETTEXT_PACKAGE);
338 g_option_context_add_main_entries (context, goption_options, GETTEXT_PACKAGE);
339 g_option_context_set_translation_domain(context, GETTEXT_PACKAGE);
341 g_option_context_add_main_entries (context, goption_options, NULL);
345 program = gnome_program_init (PACKAGE, VERSION,
346 LIBGNOMEUI_MODULE, argc, argv,
347 GNOME_PARAM_GOPTION_CONTEXT, context,
348 GNOME_PARAM_HUMAN_READABLE_NAME, _("Evince"),
349 GNOME_PARAM_APP_DATADIR, GNOMEDATADIR,
352 g_option_context_add_group (context, gtk_get_option_group (TRUE));
353 if (!g_option_context_parse (context, &argc, &argv, &error)) {
354 g_warning ("Cannot parse arguments: %s", error->message);
355 g_error_free (error);
358 g_option_context_free (context);
360 accel_filename = g_build_filename (ev_dot_dir (), "accels", NULL);
361 gtk_accel_map_load (accel_filename);
364 args = arguments_parse ();
367 if (!ev_application_register_service (EV_APP)) {
368 if (load_files_remote (file_arguments, args)) {
369 g_hash_table_destroy (args);
371 g_object_unref (program);
376 enable_metadata = TRUE;
383 gnome_authentication_manager_init ();
386 ev_backends_manager_init ();
388 if (enable_metadata) {
389 ev_metadata_manager_init ();
392 g_set_application_name (_("Evince Document Viewer"));
394 ev_file_helpers_init ();
395 ev_stock_icons_init ();
396 gtk_window_set_default_icon_name ("evince");
398 load_files (file_arguments, args);
399 g_hash_table_destroy (args);
404 gnome_accelerators_sync ();
406 gtk_accel_map_save (accel_filename);
407 g_free (accel_filename);
410 ev_file_helpers_shutdown ();
412 if (enable_metadata) {
413 ev_metadata_manager_shutdown ();
416 ev_backends_manager_shutdown ();
418 ev_debug_shutdown ();
421 g_object_unref (program);