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.
25 #include <glib/gstdio.h>
26 #include <glib/gi18n.h>
31 #include <dbus/dbus-glib-bindings.h>
34 #include "ev-application.h"
35 #include "ev-backends-manager.h"
38 #include "ev-file-helpers.h"
39 #include "ev-stock-icons.h"
42 #include "eggsmclient.h"
44 #include "eggdesktopfile.h"
46 #endif /* WITH_SMCLIENT */
48 static gchar *ev_page_label;
49 static gchar *ev_find_string;
50 static gboolean preview_mode = FALSE;
51 static gboolean fullscreen_mode = FALSE;
52 static gboolean presentation_mode = FALSE;
53 static gboolean unlink_temp_file = FALSE;
54 static gchar *print_settings;
55 static const char **file_arguments = NULL;
58 option_version_cb (const gchar *option_name,
63 g_print ("%s %s\n", _("GNOME Document Viewer"), VERSION);
69 static const GOptionEntry goption_options[] =
71 { "page-label", 'p', 0, G_OPTION_ARG_STRING, &ev_page_label, N_("The page of the document to display."), N_("PAGE")},
72 { "fullscreen", 'f', 0, G_OPTION_ARG_NONE, &fullscreen_mode, N_("Run evince in fullscreen mode"), NULL },
73 { "presentation", 's', 0, G_OPTION_ARG_NONE, &presentation_mode, N_("Run evince in presentation mode"), NULL },
74 { "preview", 'w', 0, G_OPTION_ARG_NONE, &preview_mode, N_("Run evince as a previewer"), NULL },
75 { "find", 'l', 0, G_OPTION_ARG_STRING, &ev_find_string, N_("The word or phrase to find in the document"), N_("STRING")},
76 { "unlink-tempfile", 'u', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_NONE, &unlink_temp_file, NULL, NULL },
77 { "print-settings", 't', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_FILENAME, &print_settings, NULL, NULL },
78 { "version", 0, G_OPTION_FLAG_NO_ARG | G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_CALLBACK, option_version_cb, NULL, NULL },
79 { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &file_arguments, NULL, N_("[FILE...]") },
84 launch_previewer (void)
90 gboolean retval = FALSE;
93 /* Rebuild the command line, ignoring options
94 * not supported by the previewer and taking only
95 * the first path given
97 cmd_str = g_string_new ("evince-previewer");
102 quoted = g_shell_quote (print_settings);
103 g_string_append_printf (cmd_str, " --print-settings %s", quoted);
107 if (unlink_temp_file)
108 g_string_append (cmd_str, " --unlink-tempfile");
110 if (file_arguments) {
113 quoted = g_shell_quote (file_arguments[0]);
114 g_string_append_printf (cmd_str, " %s", quoted);
118 cmd = g_string_free (cmd_str, FALSE);
119 g_shell_parse_argv (cmd, &argc, &argv, &error);
123 retval = gdk_spawn_on_screen (gdk_screen_get_default (),
132 g_warning ("Error launching previewer: %s\n", error->message);
133 g_error_free (error);
140 value_free (GValue *value)
142 g_value_unset (value);
149 * Parses the arguments and creates a #GHashTable with this data.
153 * dislay -> display at the default screen.
154 * screen -> screen number.
155 * page-label -> only if the page label argument has been passed,
156 * the page of the document to display.
157 * mode -> only if the view mode is one of the availables,
160 * Returns: a pointer into #GHashTable with data from the arguments.
163 arguments_parse (void)
167 EvWindowRunMode mode;
170 const gchar *display_name;
173 args = g_hash_table_new_full (g_str_hash,
175 (GDestroyNotify)g_free,
176 (GDestroyNotify)value_free);
178 screen = gdk_screen_get_default ();
179 display = gdk_screen_get_display (screen);
181 display_name = gdk_display_get_name (display);
182 screen_number = gdk_screen_get_number (screen);
184 value = g_new0 (GValue, 1);
185 g_value_init (value, G_TYPE_STRING);
186 g_value_set_string (value, display_name);
187 g_hash_table_insert (args, g_strdup ("display"), value);
189 value = g_new0 (GValue, 1);
190 g_value_init (value, G_TYPE_INT);
191 g_value_set_int (value, screen_number);
192 g_hash_table_insert (args, g_strdup ("screen"), value);
195 value = g_new0 (GValue, 1);
196 g_value_init (value, G_TYPE_STRING);
197 g_value_set_string (value, ev_page_label);
199 g_hash_table_insert (args, g_strdup ("page-label"), value);
201 g_free (ev_page_label);
202 ev_page_label = NULL;
205 if (ev_find_string) {
206 value = g_new0 (GValue, 1);
207 g_value_init (value, G_TYPE_STRING);
208 g_value_set_string (value, ev_find_string);
210 g_hash_table_insert (args, g_strdup ("find-string"), value);
212 g_free (ev_find_string);
213 ev_page_label = NULL;
217 mode = EV_WINDOW_MODE_FULLSCREEN;
218 else if (presentation_mode)
219 mode = EV_WINDOW_MODE_PRESENTATION;
223 value = g_new0 (GValue, 1);
224 g_value_init (value, G_TYPE_UINT);
225 g_value_set_uint (value, mode);
227 g_hash_table_insert (args, g_strdup ("mode"), value);
233 find_window_list (EvWindow *window,
236 return g_ascii_strcasecmp (uri, ev_window_get_uri (window));
240 load_files (const char **files,
246 windows = ev_application_get_windows (EV_APP);
250 ev_application_open_window (EV_APP, args, GDK_CURRENT_TIME, NULL);
252 g_list_free (windows);
256 for (i = 0; files[i]; i++) {
262 file = g_file_new_for_commandline_arg (files[i]);
263 uri = g_file_get_uri (file);
264 g_object_unref (file);
266 if (g_list_find_custom (windows, uri, (GCompareFunc) find_window_list)) {
271 label = strchr (uri, '#');
278 old = g_hash_table_lookup (args, "page-label");
280 new = g_new0 (GValue, 1);
281 g_value_init (new, G_TYPE_STRING);
282 g_value_set_string (new, label);
284 g_hash_table_insert (args, g_strdup ("page-label"), new);
288 ev_application_open_uri (EV_APP, uri, args,
289 GDK_CURRENT_TIME, NULL);
292 g_hash_table_insert (args, g_strdup ("page-label"), old);
297 g_list_free (windows);
302 load_files_remote (const char **files,
306 GError *error = NULL;
307 DBusGConnection *connection;
308 gboolean result = FALSE;
309 DBusGProxy *remote_object;
313 display = gdk_display_get_default ();
314 timestamp = gdk_x11_display_get_user_time (display);
315 connection = dbus_g_bus_get (DBUS_BUS_STARTER, &error);
317 if (connection == NULL) {
318 g_warning ("%s", error->message);
319 g_error_free (error);
324 remote_object = dbus_g_proxy_new_for_name (connection,
325 "org.gnome.evince.ApplicationService",
326 "/org/gnome/evince/Evince",
327 "org.gnome.evince.Application");
329 if (!dbus_g_proxy_call (remote_object, "OpenWindow", &error,
330 dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE), args,
331 G_TYPE_UINT, timestamp,
334 g_warning ("%s", error->message);
335 g_clear_error (&error);
336 g_object_unref (remote_object);
337 dbus_g_connection_unref (connection);
341 g_object_unref (remote_object);
342 dbus_g_connection_unref (connection);
347 for (i = 0; files[i]; i++) {
348 const char *page_label;
352 file = g_file_new_for_commandline_arg (files[i]);
353 uri = g_file_get_uri (file);
354 g_object_unref (file);
356 page_label = ev_page_label ? ev_page_label : "";
358 if (!dbus_g_proxy_call (remote_object, "OpenURI", &error,
360 dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE), args,
361 G_TYPE_UINT, timestamp,
364 g_warning ("%s", error->message);
365 g_clear_error (&error);
374 g_object_unref (remote_object);
375 dbus_g_connection_unref (connection);
377 gdk_notify_startup_complete ();
381 #endif /* ENABLE_DBUS */
384 main (int argc, char *argv[])
386 GOptionContext *context;
388 GError *error = NULL;
390 /* Init glib threads asap */
391 if (!g_thread_supported ())
392 g_thread_init (NULL);
395 /* Initialize the i18n stuff */
396 bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR);
397 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
398 textdomain (GETTEXT_PACKAGE);
401 context = g_option_context_new (N_("GNOME Document Viewer"));
402 g_option_context_set_translation_domain(context, GETTEXT_PACKAGE);
403 g_option_context_add_main_entries (context, goption_options, GETTEXT_PACKAGE);
406 g_option_context_add_group (context, egg_sm_client_get_option_group ());
409 g_option_context_add_group (context, gtk_get_option_group (TRUE));
411 if (!g_option_context_parse (context, &argc, &argv, &error)) {
412 g_printerr ("Cannot parse arguments: %s", error->message);
413 g_error_free (error);
414 g_option_context_free (context);
418 g_option_context_free (context);
423 retval = launch_previewer ();
425 return retval ? 0 : 1;
428 args = arguments_parse ();
431 if (!ev_application_register_service (EV_APP)) {
432 if (load_files_remote (file_arguments, args)) {
433 g_hash_table_destroy (args);
438 #endif /* ENABLE_DBUS */
443 ev_stock_icons_init ();
445 #if defined(WITH_SMCLIENT) && defined(GDK_WINDOWING_X11)
446 egg_set_desktop_file (GNOMEDATADIR "/applications/evince.desktop");
448 /* Manually set name and icon */
449 g_set_application_name (_("Document Viewer"));
450 gtk_window_set_default_icon_name ("evince");
451 #endif /* WITH_SMCLIENT && GDK_WINDOWING_X11 */
453 ev_application_load_session (EV_APP);
454 load_files (file_arguments, args);
455 g_hash_table_destroy (args);
457 /* Change directory so we don't prevent unmounting in case the initial cwd
458 * is on an external device (see bug #575436)
460 g_chdir (g_get_home_dir ());
465 ev_stock_icons_shutdown ();