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"
40 #include "eggsmclient.h"
42 #include "eggdesktopfile.h"
45 static gchar *ev_page_label;
46 static gchar *ev_find_string;
47 static gboolean preview_mode = FALSE;
48 static gboolean fullscreen_mode = FALSE;
49 static gboolean presentation_mode = FALSE;
50 static gboolean unlink_temp_file = FALSE;
51 static gchar *print_settings;
52 static const char **file_arguments = NULL;
55 option_version_cb (const gchar *option_name,
60 g_print ("%s %s\n", _("GNOME Document Viewer"), VERSION);
66 static const GOptionEntry goption_options[] =
68 { "page-label", 'p', 0, G_OPTION_ARG_STRING, &ev_page_label, N_("The page of the document to display."), N_("PAGE")},
69 { "fullscreen", 'f', 0, G_OPTION_ARG_NONE, &fullscreen_mode, N_("Run evince in fullscreen mode"), NULL },
70 { "presentation", 's', 0, G_OPTION_ARG_NONE, &presentation_mode, N_("Run evince in presentation mode"), NULL },
71 { "preview", 'w', 0, G_OPTION_ARG_NONE, &preview_mode, N_("Run evince as a previewer"), NULL },
72 { "find", 'l', 0, G_OPTION_ARG_STRING, &ev_find_string, N_("The word or phrase to find in the document"), N_("STRING")},
73 { "unlink-tempfile", 'u', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_NONE, &unlink_temp_file, NULL, NULL },
74 { "print-settings", 't', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_FILENAME, &print_settings, NULL, NULL },
75 { "version", 0, G_OPTION_FLAG_NO_ARG | G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_CALLBACK, option_version_cb, NULL, NULL },
76 { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &file_arguments, NULL, N_("[FILE...]") },
81 launch_previewer (void)
87 gboolean retval = FALSE;
90 /* Rebuild the command line, ignoring options
91 * not supported by the previewer and taking only
92 * the first path given
94 cmd_str = g_string_new ("evince-previewer");
99 quoted = g_shell_quote (print_settings);
100 g_string_append_printf (cmd_str, " --print-settings %s", quoted);
104 if (unlink_temp_file)
105 g_string_append (cmd_str, " --unlink-tempfile");
107 if (file_arguments) {
110 quoted = g_shell_quote (file_arguments[0]);
111 g_string_append_printf (cmd_str, " %s", quoted);
115 cmd = g_string_free (cmd_str, FALSE);
116 g_shell_parse_argv (cmd, &argc, &argv, &error);
120 retval = gdk_spawn_on_screen (gdk_screen_get_default (),
129 g_warning ("Error launching previewer: %s\n", error->message);
130 g_error_free (error);
137 value_free (GValue *value)
139 g_value_unset (value);
146 * Parses the arguments and creates a #GHashTable with this data.
150 * dislay -> display at the default screen.
151 * screen -> screen number.
152 * page-label -> only if the page label argument has been passed,
153 * the page of the document to display.
154 * mode -> only if the view mode is one of the availables,
157 * Returns: a pointer into #GHashTable with data from the arguments.
160 arguments_parse (void)
164 EvWindowRunMode mode;
167 const gchar *display_name;
170 args = g_hash_table_new_full (g_str_hash,
172 (GDestroyNotify)g_free,
173 (GDestroyNotify)value_free);
175 screen = gdk_screen_get_default ();
176 display = gdk_screen_get_display (screen);
178 display_name = gdk_display_get_name (display);
179 screen_number = gdk_screen_get_number (screen);
181 value = g_new0 (GValue, 1);
182 g_value_init (value, G_TYPE_STRING);
183 g_value_set_string (value, display_name);
184 g_hash_table_insert (args, g_strdup ("display"), value);
186 value = g_new0 (GValue, 1);
187 g_value_init (value, G_TYPE_INT);
188 g_value_set_int (value, screen_number);
189 g_hash_table_insert (args, g_strdup ("screen"), value);
192 value = g_new0 (GValue, 1);
193 g_value_init (value, G_TYPE_STRING);
194 g_value_set_string (value, ev_page_label);
196 g_hash_table_insert (args, g_strdup ("page-label"), value);
198 g_free (ev_page_label);
199 ev_page_label = NULL;
202 if (ev_find_string) {
203 value = g_new0 (GValue, 1);
204 g_value_init (value, G_TYPE_STRING);
205 g_value_set_string (value, ev_find_string);
207 g_hash_table_insert (args, g_strdup ("find-string"), value);
209 g_free (ev_find_string);
210 ev_page_label = NULL;
214 mode = EV_WINDOW_MODE_FULLSCREEN;
215 else if (presentation_mode)
216 mode = EV_WINDOW_MODE_PRESENTATION;
220 value = g_new0 (GValue, 1);
221 g_value_init (value, G_TYPE_UINT);
222 g_value_set_uint (value, mode);
224 g_hash_table_insert (args, g_strdup ("mode"), value);
230 find_window_list (EvWindow *window,
233 return g_ascii_strcasecmp (uri, ev_window_get_uri (window));
237 load_files (const char **files,
243 windows = ev_application_get_windows (EV_APP);
247 ev_application_open_window (EV_APP, args, GDK_CURRENT_TIME, NULL);
249 g_list_free (windows);
253 for (i = 0; files[i]; i++) {
259 file = g_file_new_for_commandline_arg (files[i]);
260 uri = g_file_get_uri (file);
261 g_object_unref (file);
263 if (g_list_find_custom (windows, uri, (GCompareFunc) find_window_list)) {
268 label = strchr (uri, '#');
275 old = g_hash_table_lookup (args, "page-label");
277 new = g_new0 (GValue, 1);
278 g_value_init (new, G_TYPE_STRING);
279 g_value_set_string (new, label);
281 g_hash_table_insert (args, g_strdup ("page-label"), new);
285 ev_application_open_uri (EV_APP, uri, args,
286 GDK_CURRENT_TIME, NULL);
289 g_hash_table_insert (args, g_strdup ("page-label"), old);
294 g_list_free (windows);
299 load_files_remote (const char **files,
303 GError *error = NULL;
304 DBusGConnection *connection;
305 gboolean result = FALSE;
306 DBusGProxy *remote_object;
310 display = gdk_display_get_default ();
311 timestamp = gdk_x11_display_get_user_time (display);
312 connection = dbus_g_bus_get (DBUS_BUS_STARTER, &error);
314 if (connection == NULL) {
315 g_warning ("%s", error->message);
316 g_error_free (error);
321 remote_object = dbus_g_proxy_new_for_name (connection,
322 "org.gnome.evince.ApplicationService",
323 "/org/gnome/evince/Evince",
324 "org.gnome.evince.Application");
326 if (!dbus_g_proxy_call (remote_object, "OpenWindow", &error,
327 dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE), args,
328 G_TYPE_UINT, timestamp,
331 g_warning ("%s", error->message);
332 g_clear_error (&error);
333 g_object_unref (remote_object);
334 dbus_g_connection_unref (connection);
338 g_object_unref (remote_object);
339 dbus_g_connection_unref (connection);
344 for (i = 0; files[i]; i++) {
345 const char *page_label;
349 file = g_file_new_for_commandline_arg (files[i]);
350 uri = g_file_get_uri (file);
351 g_object_unref (file);
353 page_label = ev_page_label ? ev_page_label : "";
355 if (!dbus_g_proxy_call (remote_object, "OpenURI", &error,
357 dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE), args,
358 G_TYPE_UINT, timestamp,
361 g_warning ("%s", error->message);
362 g_clear_error (&error);
371 g_object_unref (remote_object);
372 dbus_g_connection_unref (connection);
374 gdk_notify_startup_complete ();
378 #endif /* ENABLE_DBUS */
381 main (int argc, char *argv[])
383 GOptionContext *context;
385 GError *error = NULL;
387 /* Init glib threads asap */
388 if (!g_thread_supported ())
389 g_thread_init (NULL);
392 /* Initialize the i18n stuff */
393 bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR);
394 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
395 textdomain (GETTEXT_PACKAGE);
398 context = g_option_context_new (N_("GNOME Document Viewer"));
399 g_option_context_set_translation_domain(context, GETTEXT_PACKAGE);
400 g_option_context_add_main_entries (context, goption_options, GETTEXT_PACKAGE);
402 g_option_context_add_group (context, egg_sm_client_get_option_group ());
403 g_option_context_add_group (context, gtk_get_option_group (TRUE));
405 if (!g_option_context_parse (context, &argc, &argv, &error)) {
406 g_printerr ("Cannot parse arguments: %s", error->message);
407 g_error_free (error);
408 g_option_context_free (context);
412 g_option_context_free (context);
417 retval = launch_previewer ();
419 return retval ? 0 : 1;
422 args = arguments_parse ();
425 if (!ev_application_register_service (EV_APP)) {
426 if (load_files_remote (file_arguments, args)) {
427 g_hash_table_destroy (args);
432 #endif /* ENABLE_DBUS */
437 ev_stock_icons_init ();
440 /* Manually set name and icon in win32 */
441 g_set_application_name (_("Evince"));
442 gtk_window_set_default_icon_name ("evince");
444 egg_set_desktop_file (GNOMEDATADIR "/applications/evince.desktop");
445 #endif /* G_OS_WIN32 */
447 ev_application_load_session (EV_APP);
448 load_files (file_arguments, args);
449 g_hash_table_destroy (args);
451 /* Change directory so we don't prevent unmounting in case the initial cwd
452 * is on an external device (see bug #575436)
454 g_chdir (g_get_home_dir ());
459 ev_stock_icons_shutdown ();