]> www.fi.muni.cz Git - evince.git/blob - shell/main.c
On Windows, when called from a console, get console output (works only
[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 #ifdef ENABLE_DBUS
30 #include <gdk/gdkx.h>
31 #include <dbus/dbus-glib-bindings.h>
32 #endif
33
34 #include "ev-application.h"
35 #include "ev-backends-manager.h"
36 #include "ev-debug.h"
37 #include "ev-init.h"
38 #include "ev-file-helpers.h"
39 #include "ev-stock-icons.h"
40
41 #ifdef WITH_SMCLIENT
42 #include "eggsmclient.h"
43 #ifdef GDK_WINDOWING_X11
44 #include "eggdesktopfile.h"
45 #endif
46 #endif /* WITH_SMCLIENT */
47
48 #ifdef G_OS_WIN32
49 #ifdef DATADIR
50 #undef DATADIR
51 #endif
52 #include <io.h>
53 #include <conio.h>
54 #define _WIN32_WINNT 0x0500
55 #include <windows.h>
56 #endif
57
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;
66
67 static gboolean
68 option_version_cb (const gchar *option_name,
69                    const gchar *value,
70                    gpointer     data,
71                    GError     **error)
72 {
73   g_print ("%s %s\n", _("GNOME Document Viewer"), VERSION);
74
75   exit (0);
76   return FALSE;
77 }
78
79 static const GOptionEntry goption_options[] =
80 {
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...]") },
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 void
150 value_free (GValue *value)
151 {
152         g_value_unset (value);
153         g_free (value);
154 }
155
156 /**
157  * arguments_parse:
158  *
159  * Parses the arguments and creates a #GHashTable with this data.
160  *
161  *  key                 ->  value
162  *
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,
168  *                          the view mode.
169  *
170  * Returns: a pointer into #GHashTable with data from the arguments.
171  */
172 static GHashTable *
173 arguments_parse (void)
174 {
175         GHashTable      *args;
176         GValue          *value;
177         EvWindowRunMode  mode;
178         GdkScreen       *screen;
179         GdkDisplay      *display;
180         const gchar     *display_name;
181         gint             screen_number;
182
183         args = g_hash_table_new_full (g_str_hash,
184                                       g_str_equal,
185                                       (GDestroyNotify)g_free,
186                                       (GDestroyNotify)value_free);
187         
188         screen = gdk_screen_get_default ();
189         display = gdk_screen_get_display (screen);
190
191         display_name = gdk_display_get_name (display);
192         screen_number = gdk_screen_get_number (screen);
193
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);
198
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);
203
204         if (ev_page_label) {
205                 value = g_new0 (GValue, 1);
206                 g_value_init (value, G_TYPE_STRING);
207                 g_value_set_string (value, ev_page_label);
208
209                 g_hash_table_insert (args, g_strdup ("page-label"), value);
210
211                 g_free (ev_page_label);
212                 ev_page_label = NULL;
213         }
214
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);
219
220                 g_hash_table_insert (args, g_strdup ("find-string"), value);
221
222                 g_free (ev_find_string);
223                 ev_page_label = NULL;
224         }
225
226         if (fullscreen_mode)
227                 mode = EV_WINDOW_MODE_FULLSCREEN;
228         else if (presentation_mode)
229                 mode = EV_WINDOW_MODE_PRESENTATION;
230         else
231                 return args;
232
233         value = g_new0 (GValue, 1);
234         g_value_init (value, G_TYPE_UINT);
235         g_value_set_uint (value, mode);
236
237         g_hash_table_insert (args, g_strdup ("mode"), value);
238
239         return args;
240 }
241
242 static gint
243 find_window_list (EvWindow    *window,
244                   const gchar *uri)
245 {
246         return g_ascii_strcasecmp (uri, ev_window_get_uri (window));
247 }
248
249 static void
250 load_files (const char **files,
251             GHashTable  *args)
252 {
253         int    i;
254         GList *windows;
255
256         windows = ev_application_get_windows (EV_APP);
257
258         if (!files) {
259                 if (!windows)
260                         ev_application_open_window (EV_APP, args, GDK_CURRENT_TIME, NULL);
261                 else
262                         g_list_free (windows);
263                 return;
264         }
265
266         for (i = 0; files[i]; i++) {
267                 char   *uri;
268                 char   *label;
269                 GValue *old = NULL;
270                 GFile  *file;
271
272                 file = g_file_new_for_commandline_arg (files[i]);
273                 uri = g_file_get_uri (file);
274                 g_object_unref (file);
275
276                 if (g_list_find_custom (windows, uri, (GCompareFunc) find_window_list)) {
277                         g_free (uri);
278                         continue;
279                 }
280
281                 label = strchr (uri, '#');
282
283                 if (label) {
284                         GValue *new;
285
286                         *label = 0; label++;
287                         
288                         old = g_hash_table_lookup (args, "page-label");
289                         
290                         new = g_new0 (GValue, 1);
291                         g_value_init (new, G_TYPE_STRING);
292                         g_value_set_string (new, label);
293
294                         g_hash_table_insert (args, g_strdup ("page-label"), new);
295
296                 }
297
298                 ev_application_open_uri (EV_APP, uri, args,
299                                          GDK_CURRENT_TIME, NULL);
300
301                 if (old)
302                         g_hash_table_insert (args, g_strdup ("page-label"), old);
303                 
304                 g_free (uri);
305         }
306
307         g_list_free (windows);
308 }
309
310 #ifdef ENABLE_DBUS
311 static gboolean
312 load_files_remote (const char **files,
313                    GHashTable  *args)
314 {
315         int i;
316         GError *error = NULL;
317         DBusGConnection *connection;
318         gboolean result = FALSE;
319         DBusGProxy *remote_object;
320         GdkDisplay *display;
321         guint32 timestamp;
322
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);
326
327         if (connection == NULL) {
328                 g_warning ("%s", error->message);
329                 g_error_free (error);   
330
331                 return FALSE;
332         }
333
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");
338         if (!files) {
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,
342                                         G_TYPE_INVALID,
343                                         G_TYPE_INVALID)) {
344                         g_warning ("%s", error->message);
345                         g_clear_error (&error);
346                         g_object_unref (remote_object);
347                         dbus_g_connection_unref (connection);
348                         return FALSE;
349                 }
350
351                 g_object_unref (remote_object);
352                 dbus_g_connection_unref (connection);
353                 
354                 return TRUE;
355         }
356
357         for (i = 0; files[i]; i++) {
358                 const char *page_label;
359                 GFile *file;
360                 char *uri;
361
362                 file = g_file_new_for_commandline_arg (files[i]);
363                 uri = g_file_get_uri (file);
364                 g_object_unref (file);
365
366                 page_label = ev_page_label ? ev_page_label : "";
367
368                 if (!dbus_g_proxy_call (remote_object, "OpenURI", &error,
369                                         G_TYPE_STRING, uri,
370                                         dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE), args,
371                                         G_TYPE_UINT, timestamp,
372                                         G_TYPE_INVALID,
373                                         G_TYPE_INVALID)) {
374                         g_warning ("%s", error->message);
375                         g_clear_error (&error);
376                         g_free (uri);
377                         continue;
378                 }
379
380                 g_free (uri);
381                 result = TRUE;
382         }
383
384         g_object_unref (remote_object);
385         dbus_g_connection_unref (connection);
386
387         gdk_notify_startup_complete ();
388
389         return result;
390 }
391 #endif /* ENABLE_DBUS */
392
393 int
394 main (int argc, char *argv[])
395 {
396         GOptionContext *context;
397         GHashTable *args;
398         GError *error = NULL;
399
400 #ifdef G_OS_WIN32
401
402     if (fileno (stdout) != -1 &&
403           _get_osfhandle (fileno (stdout)) != -1)
404         {
405           /* stdout is fine, presumably redirected to a file or pipe */
406         }
407     else
408     {
409           typedef BOOL (* WINAPI AttachConsole_t) (DWORD);
410
411           AttachConsole_t p_AttachConsole =
412             (AttachConsole_t) GetProcAddress (GetModuleHandle ("kernel32.dll"), "AttachConsole");
413
414           if (p_AttachConsole != NULL && p_AttachConsole (ATTACH_PARENT_PROCESS))
415       {
416               freopen ("CONOUT$", "w", stdout);
417               dup2 (fileno (stdout), 1);
418               freopen ("CONOUT$", "w", stderr);
419               dup2 (fileno (stderr), 2);
420
421       }
422         }
423 #endif
424
425         /* Init glib threads asap */
426         if (!g_thread_supported ())
427                 g_thread_init (NULL);
428
429 #ifdef ENABLE_NLS
430         /* Initialize the i18n stuff */
431         bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR);
432         bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
433         textdomain (GETTEXT_PACKAGE);
434 #endif
435
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);
439
440 #ifdef WITH_SMCLIENT
441         g_option_context_add_group (context, egg_sm_client_get_option_group ());
442 #endif
443
444         g_option_context_add_group (context, gtk_get_option_group (TRUE));
445
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);
450                 
451                 return 1;
452         }
453         g_option_context_free (context);
454
455         if (preview_mode) {
456                 gboolean retval;
457                 
458                 retval = launch_previewer ();
459                 
460                 return retval ? 0 : 1;
461         }
462
463         args = arguments_parse ();
464
465 #ifdef ENABLE_DBUS
466         if (!ev_application_register_service (EV_APP)) {
467                 if (load_files_remote (file_arguments, args)) {
468                         g_hash_table_destroy (args);
469
470                         return 0;
471                 }
472         }
473 #endif /* ENABLE_DBUS */
474         
475         if (!ev_init ())
476                 return 1;
477
478         ev_stock_icons_init ();
479
480 #if defined(WITH_SMCLIENT) && defined(GDK_WINDOWING_X11)
481         egg_set_desktop_file (GNOMEDATADIR "/applications/evince.desktop");
482 #else
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 */
487
488         ev_application_load_session (EV_APP);
489         load_files (file_arguments, args);
490         g_hash_table_destroy (args);
491
492         /* Change directory so we don't prevent unmounting in case the initial cwd
493          * is on an external device (see bug #575436)
494          */
495         g_chdir (g_get_home_dir ());    
496
497         gtk_main ();
498
499         ev_shutdown ();
500         ev_stock_icons_shutdown ();
501
502         return 0;
503 }