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>
30 #include <sys/types.h>
38 #include <dbus/dbus-glib-bindings.h>
41 #include "ev-application.h"
42 #include "ev-backends-manager.h"
45 #include "ev-file-helpers.h"
46 #include "ev-stock-icons.h"
47 #include "ev-metadata.h"
50 #include "eggsmclient.h"
51 #ifdef GDK_WINDOWING_X11
52 #include "eggdesktopfile.h"
54 #endif /* WITH_SMCLIENT */
62 #define _WIN32_WINNT 0x0500
66 static gchar *ev_page_label;
67 static gchar *ev_find_string;
68 static gboolean preview_mode = FALSE;
69 static gboolean fullscreen_mode = FALSE;
70 static gboolean presentation_mode = FALSE;
71 static gboolean unlink_temp_file = FALSE;
72 static gchar *print_settings;
73 static const char **file_arguments = NULL;
76 option_version_cb (const gchar *option_name,
81 g_print ("%s %s\n", _("GNOME Document Viewer"), VERSION);
87 static const GOptionEntry goption_options[] =
89 { "page-label", 'p', 0, G_OPTION_ARG_STRING, &ev_page_label, N_("The page of the document to display."), N_("PAGE")},
90 { "fullscreen", 'f', 0, G_OPTION_ARG_NONE, &fullscreen_mode, N_("Run evince in fullscreen mode"), NULL },
91 { "presentation", 's', 0, G_OPTION_ARG_NONE, &presentation_mode, N_("Run evince in presentation mode"), NULL },
92 { "preview", 'w', 0, G_OPTION_ARG_NONE, &preview_mode, N_("Run evince as a previewer"), NULL },
93 { "find", 'l', 0, G_OPTION_ARG_STRING, &ev_find_string, N_("The word or phrase to find in the document"), N_("STRING")},
94 { "unlink-tempfile", 'u', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_NONE, &unlink_temp_file, NULL, NULL },
95 { "print-settings", 't', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_FILENAME, &print_settings, NULL, NULL },
96 { "version", 0, G_OPTION_FLAG_NO_ARG | G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_CALLBACK, option_version_cb, NULL, NULL },
97 { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &file_arguments, NULL, N_("[FILE...]") },
102 launch_previewer (void)
108 gboolean retval = FALSE;
109 GError *error = NULL;
111 /* Rebuild the command line, ignoring options
112 * not supported by the previewer and taking only
113 * the first path given
115 cmd_str = g_string_new ("evince-previewer");
117 if (print_settings) {
120 quoted = g_shell_quote (print_settings);
121 g_string_append_printf (cmd_str, " --print-settings %s", quoted);
125 if (unlink_temp_file)
126 g_string_append (cmd_str, " --unlink-tempfile");
128 if (file_arguments) {
131 quoted = g_shell_quote (file_arguments[0]);
132 g_string_append_printf (cmd_str, " %s", quoted);
136 cmd = g_string_free (cmd_str, FALSE);
137 g_shell_parse_argv (cmd, &argc, &argv, &error);
141 retval = gdk_spawn_on_screen (gdk_screen_get_default (),
150 g_warning ("Error launching previewer: %s\n", error->message);
151 g_error_free (error);
159 convert_metadata (const gchar *metadata)
164 GError *error = NULL;
167 /* If metadata is not supported for a local file
168 * is likely because and old gvfs version is running.
170 file = g_file_new_for_path (metadata);
171 if (!ev_is_metadata_supported_for_file (file)) {
173 "GVFS metadata not supported, "
174 "Evince will run without metadata support");
175 g_object_unref (file);
178 g_object_unref (file);
180 cmd = g_strdup_printf ("%s %s", LIBEXECDIR"/evince-convert-metadata", metadata);
182 retval = g_spawn_command_line_sync (cmd, NULL, NULL, &exit_status, &error);
186 g_printerr ("Error migrating metadata: %s\n", error->message);
187 g_error_free (error);
190 return retval && exit_status == 0;
194 ev_migrate_metadata (void)
199 updated = g_build_filename (ev_application_get_dot_dir (EV_APP),
200 "migrated-to-gvfs", NULL);
201 if (g_file_test (updated, G_FILE_TEST_EXISTS)) {
202 /* Already migrated */
207 metadata = g_build_filename (ev_application_get_dot_dir (EV_APP),
208 "ev-metadata.xml", NULL);
209 if (g_file_test (metadata, G_FILE_TEST_EXISTS)) {
210 if (convert_metadata (metadata)) {
213 fd = g_creat (updated, 0600);
223 #endif /* !G_OS_WIN32 */
226 value_free (GValue *value)
228 g_value_unset (value);
235 * Parses the arguments and creates a #GHashTable with this data.
239 * dislay -> display at the default screen.
240 * screen -> screen number.
241 * page-label -> only if the page label argument has been passed,
242 * the page of the document to display.
243 * mode -> only if the view mode is one of the availables,
246 * Returns: a pointer into #GHashTable with data from the arguments.
249 arguments_parse (void)
253 EvWindowRunMode mode;
256 const gchar *display_name;
259 args = g_hash_table_new_full (g_str_hash,
261 (GDestroyNotify)g_free,
262 (GDestroyNotify)value_free);
264 screen = gdk_screen_get_default ();
265 display = gdk_screen_get_display (screen);
267 display_name = gdk_display_get_name (display);
268 screen_number = gdk_screen_get_number (screen);
270 value = g_new0 (GValue, 1);
271 g_value_init (value, G_TYPE_STRING);
272 g_value_set_string (value, display_name);
273 g_hash_table_insert (args, g_strdup ("display"), value);
275 value = g_new0 (GValue, 1);
276 g_value_init (value, G_TYPE_INT);
277 g_value_set_int (value, screen_number);
278 g_hash_table_insert (args, g_strdup ("screen"), value);
281 value = g_new0 (GValue, 1);
282 g_value_init (value, G_TYPE_STRING);
283 g_value_set_string (value, ev_page_label);
285 g_hash_table_insert (args, g_strdup ("page-label"), value);
287 g_free (ev_page_label);
288 ev_page_label = NULL;
291 if (ev_find_string) {
292 value = g_new0 (GValue, 1);
293 g_value_init (value, G_TYPE_STRING);
294 g_value_set_string (value, ev_find_string);
296 g_hash_table_insert (args, g_strdup ("find-string"), value);
298 g_free (ev_find_string);
299 ev_page_label = NULL;
303 mode = EV_WINDOW_MODE_FULLSCREEN;
304 else if (presentation_mode)
305 mode = EV_WINDOW_MODE_PRESENTATION;
309 value = g_new0 (GValue, 1);
310 g_value_init (value, G_TYPE_UINT);
311 g_value_set_uint (value, mode);
313 g_hash_table_insert (args, g_strdup ("mode"), value);
319 find_window_list (EvWindow *window,
322 return g_ascii_strcasecmp (uri, ev_window_get_uri (window));
326 load_files (const char **files,
332 windows = ev_application_get_windows (EV_APP);
336 ev_application_open_window (EV_APP, args, GDK_CURRENT_TIME, NULL);
338 g_list_free (windows);
342 for (i = 0; files[i]; i++) {
348 file = g_file_new_for_commandline_arg (files[i]);
349 uri = g_file_get_uri (file);
350 g_object_unref (file);
352 if (g_list_find_custom (windows, uri, (GCompareFunc) find_window_list)) {
357 label = strchr (uri, '#');
364 old = g_hash_table_lookup (args, "page-label");
366 new = g_new0 (GValue, 1);
367 g_value_init (new, G_TYPE_STRING);
368 g_value_set_string (new, label);
370 g_hash_table_insert (args, g_strdup ("page-label"), new);
374 ev_application_open_uri (EV_APP, uri, args,
375 GDK_CURRENT_TIME, NULL);
378 g_hash_table_insert (args, g_strdup ("page-label"), old);
383 g_list_free (windows);
388 load_files_remote (const char **files,
392 GError *error = NULL;
393 DBusGConnection *connection;
394 gboolean result = FALSE;
395 DBusGProxy *remote_object;
399 display = gdk_display_get_default ();
400 timestamp = gdk_x11_display_get_user_time (display);
401 connection = dbus_g_bus_get (DBUS_BUS_STARTER, &error);
403 if (connection == NULL) {
404 g_warning ("%s", error->message);
405 g_error_free (error);
410 remote_object = dbus_g_proxy_new_for_name (connection,
411 "org.gnome.evince.ApplicationService",
412 "/org/gnome/evince/Evince",
413 "org.gnome.evince.Application");
415 if (!dbus_g_proxy_call (remote_object, "OpenWindow", &error,
416 dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE), args,
417 G_TYPE_UINT, timestamp,
420 g_warning ("%s", error->message);
421 g_clear_error (&error);
422 g_object_unref (remote_object);
423 dbus_g_connection_unref (connection);
427 g_object_unref (remote_object);
428 dbus_g_connection_unref (connection);
433 for (i = 0; files[i]; i++) {
434 const char *page_label;
438 file = g_file_new_for_commandline_arg (files[i]);
439 uri = g_file_get_uri (file);
440 g_object_unref (file);
442 page_label = ev_page_label ? ev_page_label : "";
444 if (!dbus_g_proxy_call (remote_object, "OpenURI", &error,
446 dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE), args,
447 G_TYPE_UINT, timestamp,
450 g_warning ("%s", error->message);
451 g_clear_error (&error);
460 g_object_unref (remote_object);
461 dbus_g_connection_unref (connection);
463 gdk_notify_startup_complete ();
467 #endif /* ENABLE_DBUS */
470 main (int argc, char *argv[])
472 GOptionContext *context;
474 GError *error = NULL;
478 if (fileno (stdout) != -1 &&
479 _get_osfhandle (fileno (stdout)) != -1)
481 /* stdout is fine, presumably redirected to a file or pipe */
485 typedef BOOL (* WINAPI AttachConsole_t) (DWORD);
487 AttachConsole_t p_AttachConsole =
488 (AttachConsole_t) GetProcAddress (GetModuleHandle ("kernel32.dll"), "AttachConsole");
490 if (p_AttachConsole != NULL && p_AttachConsole (ATTACH_PARENT_PROCESS))
492 freopen ("CONOUT$", "w", stdout);
493 dup2 (fileno (stdout), 1);
494 freopen ("CONOUT$", "w", stderr);
495 dup2 (fileno (stderr), 2);
501 /* Init glib threads asap */
502 if (!g_thread_supported ())
503 g_thread_init (NULL);
506 /* Initialize the i18n stuff */
507 bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR);
508 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
509 textdomain (GETTEXT_PACKAGE);
512 context = g_option_context_new (N_("GNOME Document Viewer"));
513 g_option_context_set_translation_domain(context, GETTEXT_PACKAGE);
514 g_option_context_add_main_entries (context, goption_options, GETTEXT_PACKAGE);
517 g_option_context_add_group (context, egg_sm_client_get_option_group ());
520 g_option_context_add_group (context, gtk_get_option_group (TRUE));
522 if (!g_option_context_parse (context, &argc, &argv, &error)) {
523 g_printerr ("Cannot parse arguments: %s", error->message);
524 g_error_free (error);
525 g_option_context_free (context);
529 g_option_context_free (context);
534 retval = launch_previewer ();
536 return retval ? 0 : 1;
539 args = arguments_parse ();
542 if (!ev_application_register_service (EV_APP)) {
543 if (load_files_remote (file_arguments, args)) {
544 g_hash_table_destroy (args);
549 #endif /* ENABLE_DBUS */
555 ev_migrate_metadata ();
558 ev_stock_icons_init ();
560 #if defined(WITH_SMCLIENT) && defined(GDK_WINDOWING_X11)
561 egg_set_desktop_file (GNOMEDATADIR "/applications/evince.desktop");
563 /* Manually set name and icon */
564 g_set_application_name (_("Document Viewer"));
565 gtk_window_set_default_icon_name ("evince");
566 #endif /* WITH_SMCLIENT && GDK_WINDOWING_X11 */
568 ev_application_load_session (EV_APP, file_arguments);
569 load_files (file_arguments, args);
570 g_hash_table_destroy (args);
572 /* Change directory so we don't prevent unmounting in case the initial cwd
573 * is on an external device (see bug #575436)
575 g_chdir (g_get_home_dir ());
580 ev_stock_icons_shutdown ();