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"
43 #ifdef GDK_WINDOWING_X11
44 #include "eggdesktopfile.h"
46 #endif /* WITH_SMCLIENT */
54 #define _WIN32_WINNT 0x0500
58 static gchar *ev_page_label;
59 static gchar *ev_find_string;
60 static gboolean preview_mode = FALSE;
61 static gboolean fullscreen_mode = FALSE;
62 static gboolean presentation_mode = FALSE;
63 static gboolean unlink_temp_file = FALSE;
64 static gchar *print_settings;
65 static const char **file_arguments = NULL;
68 option_version_cb (const gchar *option_name,
73 g_print ("%s %s\n", _("GNOME Document Viewer"), VERSION);
79 static const GOptionEntry goption_options[] =
81 { "page-label", 'p', 0, G_OPTION_ARG_STRING, &ev_page_label, N_("The page of the document to display."), N_("PAGE")},
82 { "fullscreen", 'f', 0, G_OPTION_ARG_NONE, &fullscreen_mode, N_("Run evince in fullscreen mode"), NULL },
83 { "presentation", 's', 0, G_OPTION_ARG_NONE, &presentation_mode, N_("Run evince in presentation mode"), NULL },
84 { "preview", 'w', 0, G_OPTION_ARG_NONE, &preview_mode, N_("Run evince as a previewer"), NULL },
85 { "find", 'l', 0, G_OPTION_ARG_STRING, &ev_find_string, N_("The word or phrase to find in the document"), N_("STRING")},
86 { "unlink-tempfile", 'u', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_NONE, &unlink_temp_file, NULL, NULL },
87 { "print-settings", 't', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_FILENAME, &print_settings, NULL, NULL },
88 { "version", 0, G_OPTION_FLAG_NO_ARG | G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_CALLBACK, option_version_cb, NULL, NULL },
89 { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &file_arguments, NULL, N_("[FILE...]") },
94 launch_previewer (void)
100 gboolean retval = FALSE;
101 GError *error = NULL;
103 /* Rebuild the command line, ignoring options
104 * not supported by the previewer and taking only
105 * the first path given
107 cmd_str = g_string_new ("evince-previewer");
109 if (print_settings) {
112 quoted = g_shell_quote (print_settings);
113 g_string_append_printf (cmd_str, " --print-settings %s", quoted);
117 if (unlink_temp_file)
118 g_string_append (cmd_str, " --unlink-tempfile");
120 if (file_arguments) {
123 quoted = g_shell_quote (file_arguments[0]);
124 g_string_append_printf (cmd_str, " %s", quoted);
128 cmd = g_string_free (cmd_str, FALSE);
129 g_shell_parse_argv (cmd, &argc, &argv, &error);
133 retval = gdk_spawn_on_screen (gdk_screen_get_default (),
142 g_warning ("Error launching previewer: %s\n", error->message);
143 g_error_free (error);
150 value_free (GValue *value)
152 g_value_unset (value);
159 * Parses the arguments and creates a #GHashTable with this data.
163 * dislay -> display at the default screen.
164 * screen -> screen number.
165 * page-label -> only if the page label argument has been passed,
166 * the page of the document to display.
167 * mode -> only if the view mode is one of the availables,
170 * Returns: a pointer into #GHashTable with data from the arguments.
173 arguments_parse (void)
177 EvWindowRunMode mode;
180 const gchar *display_name;
183 args = g_hash_table_new_full (g_str_hash,
185 (GDestroyNotify)g_free,
186 (GDestroyNotify)value_free);
188 screen = gdk_screen_get_default ();
189 display = gdk_screen_get_display (screen);
191 display_name = gdk_display_get_name (display);
192 screen_number = gdk_screen_get_number (screen);
194 value = g_new0 (GValue, 1);
195 g_value_init (value, G_TYPE_STRING);
196 g_value_set_string (value, display_name);
197 g_hash_table_insert (args, g_strdup ("display"), value);
199 value = g_new0 (GValue, 1);
200 g_value_init (value, G_TYPE_INT);
201 g_value_set_int (value, screen_number);
202 g_hash_table_insert (args, g_strdup ("screen"), value);
205 value = g_new0 (GValue, 1);
206 g_value_init (value, G_TYPE_STRING);
207 g_value_set_string (value, ev_page_label);
209 g_hash_table_insert (args, g_strdup ("page-label"), value);
211 g_free (ev_page_label);
212 ev_page_label = NULL;
215 if (ev_find_string) {
216 value = g_new0 (GValue, 1);
217 g_value_init (value, G_TYPE_STRING);
218 g_value_set_string (value, ev_find_string);
220 g_hash_table_insert (args, g_strdup ("find-string"), value);
222 g_free (ev_find_string);
223 ev_page_label = NULL;
227 mode = EV_WINDOW_MODE_FULLSCREEN;
228 else if (presentation_mode)
229 mode = EV_WINDOW_MODE_PRESENTATION;
233 value = g_new0 (GValue, 1);
234 g_value_init (value, G_TYPE_UINT);
235 g_value_set_uint (value, mode);
237 g_hash_table_insert (args, g_strdup ("mode"), value);
243 find_window_list (EvWindow *window,
246 return g_ascii_strcasecmp (uri, ev_window_get_uri (window));
250 load_files (const char **files,
256 windows = ev_application_get_windows (EV_APP);
260 ev_application_open_window (EV_APP, args, GDK_CURRENT_TIME, NULL);
262 g_list_free (windows);
266 for (i = 0; files[i]; i++) {
272 file = g_file_new_for_commandline_arg (files[i]);
273 uri = g_file_get_uri (file);
274 g_object_unref (file);
276 if (g_list_find_custom (windows, uri, (GCompareFunc) find_window_list)) {
281 label = strchr (uri, '#');
288 old = g_hash_table_lookup (args, "page-label");
290 new = g_new0 (GValue, 1);
291 g_value_init (new, G_TYPE_STRING);
292 g_value_set_string (new, label);
294 g_hash_table_insert (args, g_strdup ("page-label"), new);
298 ev_application_open_uri (EV_APP, uri, args,
299 GDK_CURRENT_TIME, NULL);
302 g_hash_table_insert (args, g_strdup ("page-label"), old);
307 g_list_free (windows);
312 load_files_remote (const char **files,
316 GError *error = NULL;
317 DBusGConnection *connection;
318 gboolean result = FALSE;
319 DBusGProxy *remote_object;
323 display = gdk_display_get_default ();
324 timestamp = gdk_x11_display_get_user_time (display);
325 connection = dbus_g_bus_get (DBUS_BUS_STARTER, &error);
327 if (connection == NULL) {
328 g_warning ("%s", error->message);
329 g_error_free (error);
334 remote_object = dbus_g_proxy_new_for_name (connection,
335 "org.gnome.evince.ApplicationService",
336 "/org/gnome/evince/Evince",
337 "org.gnome.evince.Application");
339 if (!dbus_g_proxy_call (remote_object, "OpenWindow", &error,
340 dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE), args,
341 G_TYPE_UINT, timestamp,
344 g_warning ("%s", error->message);
345 g_clear_error (&error);
346 g_object_unref (remote_object);
347 dbus_g_connection_unref (connection);
351 g_object_unref (remote_object);
352 dbus_g_connection_unref (connection);
357 for (i = 0; files[i]; i++) {
358 const char *page_label;
362 file = g_file_new_for_commandline_arg (files[i]);
363 uri = g_file_get_uri (file);
364 g_object_unref (file);
366 page_label = ev_page_label ? ev_page_label : "";
368 if (!dbus_g_proxy_call (remote_object, "OpenURI", &error,
370 dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE), args,
371 G_TYPE_UINT, timestamp,
374 g_warning ("%s", error->message);
375 g_clear_error (&error);
384 g_object_unref (remote_object);
385 dbus_g_connection_unref (connection);
387 gdk_notify_startup_complete ();
391 #endif /* ENABLE_DBUS */
394 main (int argc, char *argv[])
396 GOptionContext *context;
398 GError *error = NULL;
402 if (fileno (stdout) != -1 &&
403 _get_osfhandle (fileno (stdout)) != -1)
405 /* stdout is fine, presumably redirected to a file or pipe */
409 typedef BOOL (* WINAPI AttachConsole_t) (DWORD);
411 AttachConsole_t p_AttachConsole =
412 (AttachConsole_t) GetProcAddress (GetModuleHandle ("kernel32.dll"), "AttachConsole");
414 if (p_AttachConsole != NULL && p_AttachConsole (ATTACH_PARENT_PROCESS))
416 freopen ("CONOUT$", "w", stdout);
417 dup2 (fileno (stdout), 1);
418 freopen ("CONOUT$", "w", stderr);
419 dup2 (fileno (stderr), 2);
425 /* Init glib threads asap */
426 if (!g_thread_supported ())
427 g_thread_init (NULL);
430 /* Initialize the i18n stuff */
431 bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR);
432 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
433 textdomain (GETTEXT_PACKAGE);
436 context = g_option_context_new (N_("GNOME Document Viewer"));
437 g_option_context_set_translation_domain(context, GETTEXT_PACKAGE);
438 g_option_context_add_main_entries (context, goption_options, GETTEXT_PACKAGE);
441 g_option_context_add_group (context, egg_sm_client_get_option_group ());
444 g_option_context_add_group (context, gtk_get_option_group (TRUE));
446 if (!g_option_context_parse (context, &argc, &argv, &error)) {
447 g_printerr ("Cannot parse arguments: %s", error->message);
448 g_error_free (error);
449 g_option_context_free (context);
453 g_option_context_free (context);
458 retval = launch_previewer ();
460 return retval ? 0 : 1;
463 args = arguments_parse ();
466 if (!ev_application_register_service (EV_APP)) {
467 if (load_files_remote (file_arguments, args)) {
468 g_hash_table_destroy (args);
473 #endif /* ENABLE_DBUS */
478 ev_stock_icons_init ();
480 #if defined(WITH_SMCLIENT) && defined(GDK_WINDOWING_X11)
481 egg_set_desktop_file (GNOMEDATADIR "/applications/evince.desktop");
483 /* Manually set name and icon */
484 g_set_application_name (_("Document Viewer"));
485 gtk_window_set_default_icon_name ("evince");
486 #endif /* WITH_SMCLIENT && GDK_WINDOWING_X11 */
488 ev_application_load_session (EV_APP, file_arguments);
489 load_files (file_arguments, args);
490 g_hash_table_destroy (args);
492 /* Change directory so we don't prevent unmounting in case the initial cwd
493 * is on an external device (see bug #575436)
495 g_chdir (g_get_home_dir ());
500 ev_stock_icons_shutdown ();