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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 #include <glib/gstdio.h>
26 #include <glib/gi18n.h>
29 #include "ev-application.h"
32 #include "ev-file-helpers.h"
33 #include "ev-stock-icons.h"
34 #include "ev-metadata.h"
37 #include "eggsmclient.h"
38 #ifdef GDK_WINDOWING_X11
39 #include "eggdesktopfile.h"
41 #endif /* WITH_SMCLIENT */
49 #if !(_WIN32_WINNT >= 0x0500)
50 #error "_WIN32_WINNT must be defined >= 0x0500"
55 static gchar *ev_page_label;
56 static gchar *ev_find_string;
57 static gint ev_page_index = 0;
58 static gboolean preview_mode = FALSE;
59 static gboolean fullscreen_mode = FALSE;
60 static gboolean presentation_mode = FALSE;
61 static gboolean unlink_temp_file = FALSE;
62 static gchar *print_settings;
63 static const char **file_arguments = NULL;
67 option_version_cb (const gchar *option_name,
72 g_print ("%s %s\n", _("GNOME Document Viewer"), VERSION);
78 static const GOptionEntry goption_options[] =
80 { "page-label", 'p', 0, G_OPTION_ARG_STRING, &ev_page_label, N_("The page label of the document to display."), N_("PAGE")},
81 { "page-index", 'i', 0, G_OPTION_ARG_INT, &ev_page_index, N_("The page number of the document to display."), N_("NUMBER")},
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 load_files (const char **files)
152 GdkScreen *screen = gdk_screen_get_default ();
153 EvWindowRunMode mode = EV_WINDOW_MODE_NORMAL;
155 EvLinkDest *global_dest = NULL;
158 if (!ev_application_has_window (EV_APP))
159 ev_application_open_window (EV_APP, screen, GDK_CURRENT_TIME);
164 global_dest = ev_link_dest_new_page_label (ev_page_label);
165 else if (ev_page_index)
166 global_dest = ev_link_dest_new_page (MAX (0, ev_page_index - 1));
169 mode = EV_WINDOW_MODE_FULLSCREEN;
170 else if (presentation_mode)
171 mode = EV_WINDOW_MODE_PRESENTATION;
173 for (i = 0; files[i]; i++) {
177 EvLinkDest *dest = NULL;
178 const gchar *app_uri;
180 file = g_file_new_for_commandline_arg (files[i]);
181 uri = g_file_get_uri (file);
182 g_object_unref (file);
184 app_uri = ev_application_get_uri (EV_APP);
185 if (app_uri && strcmp (app_uri, uri) == 0) {
190 label = strchr (uri, '#');
194 dest = ev_link_dest_new_page_label (label);
195 } else if (global_dest) {
196 dest = g_object_ref (global_dest);
199 ev_application_open_uri_at_dest (EV_APP, uri, screen, dest,
200 mode, ev_find_string,
204 g_object_unref (dest);
210 main (int argc, char *argv[])
212 GOptionContext *context;
213 GError *error = NULL;
217 if (fileno (stdout) != -1 &&
218 _get_osfhandle (fileno (stdout)) != -1)
220 /* stdout is fine, presumably redirected to a file or pipe */
224 typedef BOOL (* WINAPI AttachConsole_t) (DWORD);
226 AttachConsole_t p_AttachConsole =
227 (AttachConsole_t) GetProcAddress (GetModuleHandle ("kernel32.dll"), "AttachConsole");
229 if (p_AttachConsole != NULL && p_AttachConsole (ATTACH_PARENT_PROCESS))
231 freopen ("CONOUT$", "w", stdout);
232 dup2 (fileno (stdout), 1);
233 freopen ("CONOUT$", "w", stderr);
234 dup2 (fileno (stderr), 2);
240 /* Init glib threads asap */
241 if (!g_thread_supported ())
242 g_thread_init (NULL);
245 /* Initialize the i18n stuff */
246 bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR);
247 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
248 textdomain (GETTEXT_PACKAGE);
251 context = g_option_context_new (N_("GNOME Document Viewer"));
252 g_option_context_set_translation_domain(context, GETTEXT_PACKAGE);
253 g_option_context_add_main_entries (context, goption_options, GETTEXT_PACKAGE);
256 g_option_context_add_group (context, egg_sm_client_get_option_group ());
259 g_option_context_add_group (context, gtk_get_option_group (TRUE));
261 if (!g_option_context_parse (context, &argc, &argv, &error)) {
262 g_printerr ("Cannot parse arguments: %s\n", error->message);
263 g_error_free (error);
264 g_option_context_free (context);
268 g_option_context_free (context);
273 retval = launch_previewer ();
275 return retval ? 0 : 1;
281 ev_stock_icons_init ();
283 #if defined(WITH_SMCLIENT) && defined(GDK_WINDOWING_X11)
284 egg_set_desktop_file (GNOMEDATADIR "/applications/evince.desktop");
286 /* Manually set name and icon */
287 g_set_application_name (_("Document Viewer"));
288 gtk_window_set_default_icon_name ("evince");
289 #endif /* WITH_SMCLIENT && GDK_WINDOWING_X11 */
291 ev_application_load_session (EV_APP);
292 load_files (file_arguments);
293 if (ev_application_has_window (EV_APP)) {
294 /* Change directory so we don't prevent unmounting in case the initial cwd
295 * is on an external device (see bug #575436)
297 g_chdir (g_get_home_dir ());
303 ev_stock_icons_shutdown ();