]> www.fi.muni.cz Git - evince.git/blob - shell/main.c
Run evince instances in different processes instead of single instance
[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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 #define _WIN32_WINNT 0x0500
50 #include <windows.h>
51 #endif
52
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;
61
62 static gboolean
63 option_version_cb (const gchar *option_name,
64                    const gchar *value,
65                    gpointer     data,
66                    GError     **error)
67 {
68   g_print ("%s %s\n", _("GNOME Document Viewer"), VERSION);
69
70   exit (0);
71   return FALSE;
72 }
73
74 static const GOptionEntry goption_options[] =
75 {
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...]") },
85         { NULL }
86 };
87
88 static gboolean
89 launch_previewer (void)
90 {
91         GString *cmd_str;
92         gchar   *cmd;
93         gint     argc;
94         gchar  **argv;
95         gboolean retval = FALSE;
96         GError  *error = NULL;
97
98         /* Rebuild the command line, ignoring options
99          * not supported by the previewer and taking only
100          * the first path given
101          */
102         cmd_str = g_string_new ("evince-previewer");
103                 
104         if (print_settings) {
105                 gchar *quoted;
106
107                 quoted = g_shell_quote (print_settings);
108                 g_string_append_printf (cmd_str, " --print-settings %s", quoted);
109                 g_free (quoted);
110         }
111
112         if (unlink_temp_file)
113                 g_string_append (cmd_str, " --unlink-tempfile");
114
115         if (file_arguments) {
116                 gchar *quoted;
117                 
118                 quoted = g_shell_quote (file_arguments[0]);
119                 g_string_append_printf (cmd_str, " %s", quoted);
120                 g_free (quoted);
121         }
122
123         cmd = g_string_free (cmd_str, FALSE);
124         g_shell_parse_argv (cmd, &argc, &argv, &error);
125         g_free (cmd);
126         
127         if (!error) {
128                 retval = gdk_spawn_on_screen (gdk_screen_get_default (),
129                                               NULL, argv, NULL,
130                                               G_SPAWN_SEARCH_PATH,
131                                               NULL, NULL, NULL,
132                                               &error);
133                 g_strfreev (argv);
134         }
135
136         if (error) {
137                 g_warning ("Error launching previewer: %s\n", error->message);
138                 g_error_free (error);
139         }
140
141         return retval;
142 }
143
144 static gint
145 find_window_list (EvWindow    *window,
146                   const gchar *uri)
147 {
148         return g_ascii_strcasecmp (uri, ev_window_get_uri (window));
149 }
150
151 static void
152 load_files (const char **files)
153 {
154         GdkScreen       *screen = gdk_screen_get_default ();
155         EvWindowRunMode  mode = EV_WINDOW_MODE_NORMAL;
156         GList           *windows;
157         gint             i;
158         EvLinkDest      *global_dest = NULL;
159
160         windows = ev_application_get_windows (EV_APP);
161
162         if (!files) {
163                 if (!windows)
164                         ev_application_open_window (EV_APP, screen, GDK_CURRENT_TIME);
165                 else
166                         g_list_free (windows);
167                 return;
168         }
169
170         if (ev_page_label)
171                 global_dest = ev_link_dest_new_page_label (ev_page_label);
172
173         if (fullscreen_mode)
174                 mode = EV_WINDOW_MODE_FULLSCREEN;
175         else if (presentation_mode)
176                 mode = EV_WINDOW_MODE_PRESENTATION;
177
178         for (i = 0; files[i]; i++) {
179                 gchar      *uri;
180                 gchar      *label;
181                 GFile      *file;
182                 EvLinkDest *dest = NULL;
183
184                 file = g_file_new_for_commandline_arg (files[i]);
185                 uri = g_file_get_uri (file);
186                 g_object_unref (file);
187
188                 if (g_list_find_custom (windows, uri, (GCompareFunc) find_window_list)) {
189                         g_free (uri);
190                         continue;
191                 }
192
193                 label = strchr (uri, '#');
194                 if (label) {
195                         *label = 0;
196                         label++;
197                         dest = ev_link_dest_new_page_label (label);
198                 } else if (global_dest) {
199                         dest = g_object_ref (global_dest);
200                 }
201
202                 ev_application_open_uri_at_dest (EV_APP, uri, screen, dest,
203                                                  mode, ev_find_string,
204                                                  GDK_CURRENT_TIME);
205
206                 if (dest)
207                         g_object_unref (dest);
208                 g_free (uri);
209         }
210
211         g_list_free (windows);
212 }
213
214 int
215 main (int argc, char *argv[])
216 {
217         GOptionContext *context;
218         GList *toplevels;
219         GError *error = NULL;
220
221 #ifdef G_OS_WIN32
222
223     if (fileno (stdout) != -1 &&
224           _get_osfhandle (fileno (stdout)) != -1)
225         {
226           /* stdout is fine, presumably redirected to a file or pipe */
227         }
228     else
229     {
230           typedef BOOL (* WINAPI AttachConsole_t) (DWORD);
231
232           AttachConsole_t p_AttachConsole =
233             (AttachConsole_t) GetProcAddress (GetModuleHandle ("kernel32.dll"), "AttachConsole");
234
235           if (p_AttachConsole != NULL && p_AttachConsole (ATTACH_PARENT_PROCESS))
236       {
237               freopen ("CONOUT$", "w", stdout);
238               dup2 (fileno (stdout), 1);
239               freopen ("CONOUT$", "w", stderr);
240               dup2 (fileno (stderr), 2);
241
242       }
243         }
244 #endif
245
246         /* Init glib threads asap */
247         if (!g_thread_supported ())
248                 g_thread_init (NULL);
249
250 #ifdef ENABLE_NLS
251         /* Initialize the i18n stuff */
252         bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR);
253         bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
254         textdomain (GETTEXT_PACKAGE);
255 #endif
256
257         context = g_option_context_new (N_("GNOME Document Viewer"));
258         g_option_context_set_translation_domain(context, GETTEXT_PACKAGE);
259         g_option_context_add_main_entries (context, goption_options, GETTEXT_PACKAGE);
260
261 #ifdef WITH_SMCLIENT
262         g_option_context_add_group (context, egg_sm_client_get_option_group ());
263 #endif
264
265         g_option_context_add_group (context, gtk_get_option_group (TRUE));
266
267         if (!g_option_context_parse (context, &argc, &argv, &error)) {
268                 g_printerr ("Cannot parse arguments: %s", error->message);
269                 g_error_free (error);
270                 g_option_context_free (context);
271                 
272                 return 1;
273         }
274         g_option_context_free (context);
275
276         if (preview_mode) {
277                 gboolean retval;
278                 
279                 retval = launch_previewer ();
280                 
281                 return retval ? 0 : 1;
282         }
283
284         if (!ev_init ())
285                 return 1;
286
287         ev_stock_icons_init ();
288
289 #if defined(WITH_SMCLIENT) && defined(GDK_WINDOWING_X11)
290         egg_set_desktop_file (GNOMEDATADIR "/applications/evince.desktop");
291 #else
292         /* Manually set name and icon */
293         g_set_application_name (_("Document Viewer"));
294         gtk_window_set_default_icon_name ("evince");
295 #endif /* WITH_SMCLIENT && GDK_WINDOWING_X11 */
296
297         ev_application_load_session (EV_APP, file_arguments);
298         load_files (file_arguments);
299         toplevels = gtk_window_list_toplevels ();
300         if (toplevels) {
301                 g_list_free (toplevels);
302                 /* Change directory so we don't prevent unmounting in case the initial cwd
303                  * is on an external device (see bug #575436)
304                  */
305                 g_chdir (g_get_home_dir ());
306
307                 gtk_main ();
308         }
309
310         ev_shutdown ();
311         ev_stock_icons_shutdown ();
312
313         return 0;
314 }