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>
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 #define _WIN32_WINNT 0x0500
53 static gchar *ev_page_label;
54 static gchar *ev_find_string;
55 static gboolean preview_mode = FALSE;
56 static gboolean fullscreen_mode = FALSE;
57 static gboolean presentation_mode = FALSE;
58 static gboolean unlink_temp_file = FALSE;
59 static gchar *print_settings;
60 static const char **file_arguments = NULL;
63 option_version_cb (const gchar *option_name,
68 g_print ("%s %s\n", _("GNOME Document Viewer"), VERSION);
74 static const GOptionEntry goption_options[] =
76 { "page-label", 'p', 0, G_OPTION_ARG_STRING, &ev_page_label, N_("The page of the document to display."), N_("PAGE")},
77 { "fullscreen", 'f', 0, G_OPTION_ARG_NONE, &fullscreen_mode, N_("Run evince in fullscreen mode"), NULL },
78 { "presentation", 's', 0, G_OPTION_ARG_NONE, &presentation_mode, N_("Run evince in presentation mode"), NULL },
79 { "preview", 'w', 0, G_OPTION_ARG_NONE, &preview_mode, N_("Run evince as a previewer"), NULL },
80 { "find", 'l', 0, G_OPTION_ARG_STRING, &ev_find_string, N_("The word or phrase to find in the document"), N_("STRING")},
81 { "unlink-tempfile", 'u', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_NONE, &unlink_temp_file, NULL, NULL },
82 { "print-settings", 't', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_FILENAME, &print_settings, NULL, NULL },
83 { "version", 0, G_OPTION_FLAG_NO_ARG | G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_CALLBACK, option_version_cb, NULL, NULL },
84 { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &file_arguments, NULL, N_("[FILE...]") },
89 launch_previewer (void)
95 gboolean retval = FALSE;
98 /* Rebuild the command line, ignoring options
99 * not supported by the previewer and taking only
100 * the first path given
102 cmd_str = g_string_new ("evince-previewer");
104 if (print_settings) {
107 quoted = g_shell_quote (print_settings);
108 g_string_append_printf (cmd_str, " --print-settings %s", quoted);
112 if (unlink_temp_file)
113 g_string_append (cmd_str, " --unlink-tempfile");
115 if (file_arguments) {
118 quoted = g_shell_quote (file_arguments[0]);
119 g_string_append_printf (cmd_str, " %s", quoted);
123 cmd = g_string_free (cmd_str, FALSE);
124 g_shell_parse_argv (cmd, &argc, &argv, &error);
128 retval = gdk_spawn_on_screen (gdk_screen_get_default (),
137 g_warning ("Error launching previewer: %s\n", error->message);
138 g_error_free (error);
145 load_files (const char **files)
147 GdkScreen *screen = gdk_screen_get_default ();
148 EvWindowRunMode mode = EV_WINDOW_MODE_NORMAL;
150 EvLinkDest *global_dest = NULL;
153 if (!ev_application_has_window (EV_APP))
154 ev_application_open_window (EV_APP, screen, GDK_CURRENT_TIME);
159 global_dest = ev_link_dest_new_page_label (ev_page_label);
162 mode = EV_WINDOW_MODE_FULLSCREEN;
163 else if (presentation_mode)
164 mode = EV_WINDOW_MODE_PRESENTATION;
166 for (i = 0; files[i]; i++) {
170 EvLinkDest *dest = NULL;
171 const gchar *app_uri;
173 file = g_file_new_for_commandline_arg (files[i]);
174 uri = g_file_get_uri (file);
175 g_object_unref (file);
177 app_uri = ev_application_get_uri (EV_APP);
178 if (app_uri && strcmp (app_uri, uri) == 0) {
183 label = strchr (uri, '#');
187 dest = ev_link_dest_new_page_label (label);
188 } else if (global_dest) {
189 dest = g_object_ref (global_dest);
192 ev_application_open_uri_at_dest (EV_APP, uri, screen, dest,
193 mode, ev_find_string,
197 g_object_unref (dest);
203 main (int argc, char *argv[])
205 GOptionContext *context;
206 GError *error = NULL;
210 if (fileno (stdout) != -1 &&
211 _get_osfhandle (fileno (stdout)) != -1)
213 /* stdout is fine, presumably redirected to a file or pipe */
217 typedef BOOL (* WINAPI AttachConsole_t) (DWORD);
219 AttachConsole_t p_AttachConsole =
220 (AttachConsole_t) GetProcAddress (GetModuleHandle ("kernel32.dll"), "AttachConsole");
222 if (p_AttachConsole != NULL && p_AttachConsole (ATTACH_PARENT_PROCESS))
224 freopen ("CONOUT$", "w", stdout);
225 dup2 (fileno (stdout), 1);
226 freopen ("CONOUT$", "w", stderr);
227 dup2 (fileno (stderr), 2);
233 /* Init glib threads asap */
234 if (!g_thread_supported ())
235 g_thread_init (NULL);
238 /* Initialize the i18n stuff */
239 bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR);
240 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
241 textdomain (GETTEXT_PACKAGE);
244 context = g_option_context_new (N_("GNOME Document Viewer"));
245 g_option_context_set_translation_domain(context, GETTEXT_PACKAGE);
246 g_option_context_add_main_entries (context, goption_options, GETTEXT_PACKAGE);
249 g_option_context_add_group (context, egg_sm_client_get_option_group ());
252 g_option_context_add_group (context, gtk_get_option_group (TRUE));
254 if (!g_option_context_parse (context, &argc, &argv, &error)) {
255 g_printerr ("Cannot parse arguments: %s\n", error->message);
256 g_error_free (error);
257 g_option_context_free (context);
261 g_option_context_free (context);
266 retval = launch_previewer ();
268 return retval ? 0 : 1;
274 ev_stock_icons_init ();
276 #if defined(WITH_SMCLIENT) && defined(GDK_WINDOWING_X11)
277 egg_set_desktop_file (GNOMEDATADIR "/applications/evince.desktop");
279 /* Manually set name and icon */
280 g_set_application_name (_("Document Viewer"));
281 gtk_window_set_default_icon_name ("evince");
282 #endif /* WITH_SMCLIENT && GDK_WINDOWING_X11 */
284 ev_application_load_session (EV_APP);
285 load_files (file_arguments);
286 if (ev_application_has_window (EV_APP)) {
287 /* Change directory so we don't prevent unmounting in case the initial cwd
288 * is on an external device (see bug #575436)
290 g_chdir (g_get_home_dir ());
296 ev_stock_icons_shutdown ();