]> www.fi.muni.cz Git - evince.git/blob - shell/main.c
4ed0b05ebd42067d934d64b144599ba637c43df7
[evince.git] / shell / main.c
1 /*
2  *  Copyright (C) 2004 Marco Pesenti Gritti
3  *
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)
7  *  any later version.
8  *
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.
13  *
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.
17  *
18  */
19
20 #include "config.h"
21
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include <glib/gstdio.h>
26 #include <glib/gi18n.h>
27 #include <gtk/gtk.h>
28
29 #include "ev-application.h"
30 #include "ev-debug.h"
31 #include "ev-init.h"
32 #include "ev-file-helpers.h"
33 #include "ev-stock-icons.h"
34 #include "ev-metadata.h"
35
36 #ifdef WITH_SMCLIENT
37 #include "eggsmclient.h"
38 #ifdef GDK_WINDOWING_X11
39 #include "eggdesktopfile.h"
40 #endif
41 #endif /* WITH_SMCLIENT */
42
43 #ifdef G_OS_WIN32
44 #ifdef DATADIR
45 #undef DATADIR
46 #endif
47 #include <io.h>
48 #include <conio.h>
49 #if !(_WIN32_WINNT >= 0x0500)
50 #error "_WIN32_WINNT must be defined >= 0x0500"
51 #endif
52 #include <windows.h>
53 #endif
54
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;
64
65
66 static gboolean
67 option_version_cb (const gchar *option_name,
68                    const gchar *value,
69                    gpointer     data,
70                    GError     **error)
71 {
72   g_print ("%s %s\n", _("GNOME Document Viewer"), VERSION);
73
74   exit (0);
75   return FALSE;
76 }
77
78 static const GOptionEntry goption_options[] =
79 {
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ā€¦]") },
90         { NULL }
91 };
92
93 static gboolean
94 launch_previewer (void)
95 {
96         GString *cmd_str;
97         gchar   *cmd;
98         gint     argc;
99         gchar  **argv;
100         gboolean retval = FALSE;
101         GError  *error = NULL;
102
103         /* Rebuild the command line, ignoring options
104          * not supported by the previewer and taking only
105          * the first path given
106          */
107         cmd_str = g_string_new ("evince-previewer");
108                 
109         if (print_settings) {
110                 gchar *quoted;
111
112                 quoted = g_shell_quote (print_settings);
113                 g_string_append_printf (cmd_str, " --print-settings %s", quoted);
114                 g_free (quoted);
115         }
116
117         if (unlink_temp_file)
118                 g_string_append (cmd_str, " --unlink-tempfile");
119
120         if (file_arguments) {
121                 gchar *quoted;
122                 
123                 quoted = g_shell_quote (file_arguments[0]);
124                 g_string_append_printf (cmd_str, " %s", quoted);
125                 g_free (quoted);
126         }
127
128         cmd = g_string_free (cmd_str, FALSE);
129         g_shell_parse_argv (cmd, &argc, &argv, &error);
130         g_free (cmd);
131         
132         if (!error) {
133                 retval = gdk_spawn_on_screen (gdk_screen_get_default (),
134                                               NULL, argv, NULL,
135                                               G_SPAWN_SEARCH_PATH,
136                                               NULL, NULL, NULL,
137                                               &error);
138                 g_strfreev (argv);
139         }
140
141         if (error) {
142                 g_warning ("Error launching previewer: %s\n", error->message);
143                 g_error_free (error);
144         }
145
146         return retval;
147 }
148
149 static gchar *
150 get_label_from_filename (const gchar *filename)
151 {
152         GFile   *file;
153         gchar   *label;
154         gboolean exists;
155
156         label = g_strrstr (filename, "#");
157         if (!label)
158                 return NULL;
159
160         /* Filename contains a #, check
161          * whether it's part of the path
162          * or a label
163          */
164         file = g_file_new_for_commandline_arg (filename);
165         exists = g_file_query_exists (file, NULL);
166         g_object_unref (file);
167
168         return exists ? NULL : label;
169 }
170
171 static void
172 load_files (const char **files)
173 {
174         GdkScreen       *screen = gdk_screen_get_default ();
175         EvWindowRunMode  mode = EV_WINDOW_MODE_NORMAL;
176         gint             i;
177         EvLinkDest      *global_dest = NULL;
178
179         if (!files) {
180                 if (!ev_application_has_window (EV_APP))
181                         ev_application_open_window (EV_APP, screen, GDK_CURRENT_TIME);
182                 return;
183         }
184
185         if (ev_page_label)
186                 global_dest = ev_link_dest_new_page_label (ev_page_label);
187         else if (ev_page_index)
188                 global_dest = ev_link_dest_new_page (MAX (0, ev_page_index - 1));
189
190         if (fullscreen_mode)
191                 mode = EV_WINDOW_MODE_FULLSCREEN;
192         else if (presentation_mode)
193                 mode = EV_WINDOW_MODE_PRESENTATION;
194
195         for (i = 0; files[i]; i++) {
196                 const gchar *filename;
197                 gchar       *uri;
198                 gchar       *label;
199                 GFile       *file;
200                 EvLinkDest  *dest = NULL;
201                 const gchar *app_uri;
202
203                 filename = files[i];
204                 label = get_label_from_filename (filename);
205                 if (label) {
206                         *label = 0;
207                         label++;
208                         dest = ev_link_dest_new_page_label (label);
209                 } else if (global_dest) {
210                         dest = g_object_ref (global_dest);
211                 }
212
213                 file = g_file_new_for_commandline_arg (filename);
214                 uri = g_file_get_uri (file);
215                 g_object_unref (file);
216
217                 app_uri = ev_application_get_uri (EV_APP);
218                 if (app_uri && strcmp (app_uri, uri) == 0) {
219                         g_free (uri);
220                         continue;
221                 }
222
223
224
225                 ev_application_open_uri_at_dest (EV_APP, uri, screen, dest,
226                                                  mode, ev_find_string,
227                                                  GDK_CURRENT_TIME);
228
229                 if (dest)
230                         g_object_unref (dest);
231                 g_free (uri);
232         }
233 }
234
235 static gchar*
236 ev_get_locale_dir ()
237 {
238 #ifdef G_OS_WIN32
239         gchar *install_dir = NULL, *locale_dir;
240         gchar *retval = NULL;
241
242         install_dir =
243             g_win32_get_package_installation_directory_of_module (NULL);
244
245         if (install_dir) {
246                 locale_dir = g_build_filename (install_dir,
247                                         "share", "locale", NULL);
248                 retval = g_win32_locale_filename_from_utf8 (locale_dir);
249                 g_free (install_dir);
250                 g_free (locale_dir);
251         }
252
253         if (retval)
254                 return retval;
255         else
256                 return g_strdup ("");
257 #else
258         return g_strdup (GNOMELOCALEDIR);
259 #endif
260 }
261
262 int
263 main (int argc, char *argv[])
264 {
265         GOptionContext *context;
266         GError         *error = NULL;
267
268 #ifdef G_OS_WIN32
269
270     if (fileno (stdout) != -1 &&
271           _get_osfhandle (fileno (stdout)) != -1)
272         {
273           /* stdout is fine, presumably redirected to a file or pipe */
274         }
275     else
276     {
277           typedef BOOL (* WINAPI AttachConsole_t) (DWORD);
278
279           AttachConsole_t p_AttachConsole =
280             (AttachConsole_t) GetProcAddress (GetModuleHandle ("kernel32.dll"), "AttachConsole");
281
282           if (p_AttachConsole != NULL && p_AttachConsole (ATTACH_PARENT_PROCESS))
283       {
284               freopen ("CONOUT$", "w", stdout);
285               dup2 (fileno (stdout), 1);
286               freopen ("CONOUT$", "w", stderr);
287               dup2 (fileno (stderr), 2);
288
289       }
290         }
291 #endif
292
293         /* Init glib threads asap */
294         if (!g_thread_supported ())
295                 g_thread_init (NULL);
296
297 #ifdef ENABLE_NLS
298         /* Initialize the i18n stuff */
299         gchar *tmp = ev_get_locale_dir ();
300         bindtextdomain (GETTEXT_PACKAGE, tmp);
301         g_free (tmp);
302         bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
303         textdomain (GETTEXT_PACKAGE);
304 #endif
305
306         context = g_option_context_new (N_("GNOME Document Viewer"));
307         g_option_context_set_translation_domain(context, GETTEXT_PACKAGE);
308         g_option_context_add_main_entries (context, goption_options, GETTEXT_PACKAGE);
309
310 #ifdef WITH_SMCLIENT
311         g_option_context_add_group (context, egg_sm_client_get_option_group ());
312 #endif
313
314         g_option_context_add_group (context, gtk_get_option_group (TRUE));
315
316         if (!g_option_context_parse (context, &argc, &argv, &error)) {
317                 g_printerr ("Cannot parse arguments: %s\n", error->message);
318                 g_error_free (error);
319                 g_option_context_free (context);
320
321                 return 1;
322         }
323         g_option_context_free (context);
324
325         if (preview_mode) {
326                 gboolean retval;
327                 
328                 retval = launch_previewer ();
329                 
330                 return retval ? 0 : 1;
331         }
332
333         if (!ev_init ())
334                 return 1;
335
336         ev_stock_icons_init ();
337
338 #if defined(WITH_SMCLIENT) && defined(GDK_WINDOWING_X11)
339         egg_set_desktop_file (GNOMEDATADIR "/applications/evince.desktop");
340 #else
341         /* Manually set name and icon */
342         g_set_application_name (_("Document Viewer"));
343         gtk_window_set_default_icon_name ("evince");
344 #endif /* WITH_SMCLIENT && GDK_WINDOWING_X11 */
345
346         ev_application_load_session (EV_APP);
347         load_files (file_arguments);
348
349         /* Change directory so we don't prevent unmounting in case the initial cwd
350          * is on an external device (see bug #575436)
351          */
352         g_chdir (g_get_home_dir ());
353
354         gtk_main ();
355
356         ev_shutdown ();
357         ev_stock_icons_shutdown ();
358
359         return 0;
360 }