]> www.fi.muni.cz Git - evince.git/blob - shell/main.c
43fca484baf5d36662f17850ff28bc732fbeb924
[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 #include "eggsmclient.h"
41 #ifndef G_OS_WIN32
42 #include "eggdesktopfile.h"
43 #endif
44
45 static gchar   *ev_page_label;
46 static gchar   *ev_find_string;
47 static gboolean preview_mode = FALSE;
48 static gboolean fullscreen_mode = FALSE;
49 static gboolean presentation_mode = FALSE;
50 static gboolean unlink_temp_file = FALSE;
51 static gchar   *print_settings;
52 static const char **file_arguments = NULL;
53
54 static gboolean
55 option_version_cb (const gchar *option_name,
56                    const gchar *value,
57                    gpointer     data,
58                    GError     **error)
59 {
60   g_print ("%s %s\n", _("GNOME Document Viewer"), VERSION);
61
62   exit (0);
63   return FALSE;
64 }
65
66 static const GOptionEntry goption_options[] =
67 {
68         { "page-label", 'p', 0, G_OPTION_ARG_STRING, &ev_page_label, N_("The page of the document to display."), N_("PAGE")},
69         { "fullscreen", 'f', 0, G_OPTION_ARG_NONE, &fullscreen_mode, N_("Run evince in fullscreen mode"), NULL },
70         { "presentation", 's', 0, G_OPTION_ARG_NONE, &presentation_mode, N_("Run evince in presentation mode"), NULL },
71         { "preview", 'w', 0, G_OPTION_ARG_NONE, &preview_mode, N_("Run evince as a previewer"), NULL },
72         { "find", 'l', 0, G_OPTION_ARG_STRING, &ev_find_string, N_("The word or phrase to find in the document"), N_("STRING")},
73         { "unlink-tempfile", 'u', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_NONE, &unlink_temp_file, NULL, NULL },
74         { "print-settings", 't', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_FILENAME, &print_settings, NULL, NULL },
75         { "version", 0, G_OPTION_FLAG_NO_ARG | G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_CALLBACK, option_version_cb, NULL, NULL },
76         { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &file_arguments, NULL, N_("[FILE...]") },
77         { NULL }
78 };
79
80 static gboolean
81 launch_previewer (void)
82 {
83         GString *cmd_str;
84         gchar   *cmd;
85         gint     argc;
86         gchar  **argv;
87         gboolean retval = FALSE;
88         GError  *error = NULL;
89
90         /* Rebuild the command line, ignoring options
91          * not supported by the previewer and taking only
92          * the first path given
93          */
94         cmd_str = g_string_new ("evince-previewer");
95                 
96         if (print_settings) {
97                 gchar *quoted;
98
99                 quoted = g_shell_quote (print_settings);
100                 g_string_append_printf (cmd_str, " --print-settings %s", quoted);
101                 g_free (quoted);
102         }
103
104         if (unlink_temp_file)
105                 g_string_append (cmd_str, " --unlink-tempfile");
106
107         if (file_arguments) {
108                 gchar *quoted;
109                 
110                 quoted = g_shell_quote (file_arguments[0]);
111                 g_string_append_printf (cmd_str, " %s", quoted);
112                 g_free (quoted);
113         }
114
115         cmd = g_string_free (cmd_str, FALSE);
116         g_shell_parse_argv (cmd, &argc, &argv, &error);
117         g_free (cmd);
118         
119         if (!error) {
120                 retval = gdk_spawn_on_screen (gdk_screen_get_default (),
121                                               NULL, argv, NULL,
122                                               G_SPAWN_SEARCH_PATH,
123                                               NULL, NULL, NULL,
124                                               &error);
125                 g_strfreev (argv);
126         }
127
128         if (error) {
129                 g_warning ("Error launching previewer: %s\n", error->message);
130                 g_error_free (error);
131         }
132
133         return retval;
134 }
135
136 static void
137 value_free (GValue *value)
138 {
139         g_value_unset (value);
140         g_free (value);
141 }
142
143 /**
144  * arguments_parse:
145  *
146  * Parses the arguments and creates a #GHashTable with this data.
147  *
148  *  key                 ->  value
149  *
150  *  dislay              ->  display at the default screen.
151  *  screen              ->  screen number.
152  *  page-label          ->  only if the page label argument has been passed,
153  *                          the page of the document to display.
154  *  mode                ->  only if the view mode is one of the availables,
155  *                          the view mode.
156  *
157  * Returns: a pointer into #GHashTable with data from the arguments.
158  */
159 static GHashTable *
160 arguments_parse (void)
161 {
162         GHashTable      *args;
163         GValue          *value;
164         EvWindowRunMode  mode;
165         GdkScreen       *screen;
166         GdkDisplay      *display;
167         const gchar     *display_name;
168         gint             screen_number;
169
170         args = g_hash_table_new_full (g_str_hash,
171                                       g_str_equal,
172                                       (GDestroyNotify)g_free,
173                                       (GDestroyNotify)value_free);
174         
175         screen = gdk_screen_get_default ();
176         display = gdk_screen_get_display (screen);
177
178         display_name = gdk_display_get_name (display);
179         screen_number = gdk_screen_get_number (screen);
180
181         value = g_new0 (GValue, 1);
182         g_value_init (value, G_TYPE_STRING);
183         g_value_set_string (value, display_name);
184         g_hash_table_insert (args, g_strdup ("display"), value);
185
186         value = g_new0 (GValue, 1);
187         g_value_init (value, G_TYPE_INT);
188         g_value_set_int (value, screen_number);
189         g_hash_table_insert (args, g_strdup ("screen"), value);
190
191         if (ev_page_label) {
192                 value = g_new0 (GValue, 1);
193                 g_value_init (value, G_TYPE_STRING);
194                 g_value_set_string (value, ev_page_label);
195
196                 g_hash_table_insert (args, g_strdup ("page-label"), value);
197
198                 g_free (ev_page_label);
199                 ev_page_label = NULL;
200         }
201
202         if (ev_find_string) {
203                 value = g_new0 (GValue, 1);
204                 g_value_init (value, G_TYPE_STRING);
205                 g_value_set_string (value, ev_find_string);
206
207                 g_hash_table_insert (args, g_strdup ("find-string"), value);
208
209                 g_free (ev_find_string);
210                 ev_page_label = NULL;
211         }
212
213         if (fullscreen_mode)
214                 mode = EV_WINDOW_MODE_FULLSCREEN;
215         else if (presentation_mode)
216                 mode = EV_WINDOW_MODE_PRESENTATION;
217         else
218                 return args;
219
220         value = g_new0 (GValue, 1);
221         g_value_init (value, G_TYPE_UINT);
222         g_value_set_uint (value, mode);
223
224         g_hash_table_insert (args, g_strdup ("mode"), value);
225
226         return args;
227 }
228
229 static gint
230 find_window_list (EvWindow    *window,
231                   const gchar *uri)
232 {
233         return g_ascii_strcasecmp (uri, ev_window_get_uri (window));
234 }
235
236 static void
237 load_files (const char **files,
238             GHashTable  *args)
239 {
240         int    i;
241         GList *windows;
242
243         windows = ev_application_get_windows (EV_APP);
244
245         if (!files) {
246                 if (!windows)
247                         ev_application_open_window (EV_APP, args, GDK_CURRENT_TIME, NULL);
248                 else
249                         g_list_free (windows);
250                 return;
251         }
252
253         for (i = 0; files[i]; i++) {
254                 char   *uri;
255                 char   *label;
256                 GValue *old = NULL;
257                 GFile  *file;
258
259                 file = g_file_new_for_commandline_arg (files[i]);
260                 uri = g_file_get_uri (file);
261                 g_object_unref (file);
262
263                 if (g_list_find_custom (windows, uri, (GCompareFunc) find_window_list)) {
264                         g_free (uri);
265                         continue;
266                 }
267
268                 label = strchr (uri, '#');
269
270                 if (label) {
271                         GValue *new;
272
273                         *label = 0; label++;
274                         
275                         old = g_hash_table_lookup (args, "page-label");
276                         
277                         new = g_new0 (GValue, 1);
278                         g_value_init (new, G_TYPE_STRING);
279                         g_value_set_string (new, label);
280
281                         g_hash_table_insert (args, g_strdup ("page-label"), new);
282
283                 }
284
285                 ev_application_open_uri (EV_APP, uri, args,
286                                          GDK_CURRENT_TIME, NULL);
287
288                 if (old)
289                         g_hash_table_insert (args, g_strdup ("page-label"), old);
290                 
291                 g_free (uri);
292         }
293
294         g_list_free (windows);
295 }
296
297 #ifdef ENABLE_DBUS
298 static gboolean
299 load_files_remote (const char **files,
300                    GHashTable  *args)
301 {
302         int i;
303         GError *error = NULL;
304         DBusGConnection *connection;
305         gboolean result = FALSE;
306         DBusGProxy *remote_object;
307         GdkDisplay *display;
308         guint32 timestamp;
309
310         display = gdk_display_get_default ();
311         timestamp = gdk_x11_display_get_user_time (display);
312         connection = dbus_g_bus_get (DBUS_BUS_STARTER, &error);
313
314         if (connection == NULL) {
315                 g_warning ("%s", error->message);
316                 g_error_free (error);   
317
318                 return FALSE;
319         }
320
321         remote_object = dbus_g_proxy_new_for_name (connection,
322                                                    "org.gnome.evince.ApplicationService",
323                                                    "/org/gnome/evince/Evince",
324                                                    "org.gnome.evince.Application");
325         if (!files) {
326                 if (!dbus_g_proxy_call (remote_object, "OpenWindow", &error,
327                                         dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE), args,
328                                         G_TYPE_UINT, timestamp,
329                                         G_TYPE_INVALID,
330                                         G_TYPE_INVALID)) {
331                         g_warning ("%s", error->message);
332                         g_clear_error (&error);
333                         g_object_unref (remote_object);
334                         dbus_g_connection_unref (connection);
335                         return FALSE;
336                 }
337
338                 g_object_unref (remote_object);
339                 dbus_g_connection_unref (connection);
340                 
341                 return TRUE;
342         }
343
344         for (i = 0; files[i]; i++) {
345                 const char *page_label;
346                 GFile *file;
347                 char *uri;
348
349                 file = g_file_new_for_commandline_arg (files[i]);
350                 uri = g_file_get_uri (file);
351                 g_object_unref (file);
352
353                 page_label = ev_page_label ? ev_page_label : "";
354
355                 if (!dbus_g_proxy_call (remote_object, "OpenURI", &error,
356                                         G_TYPE_STRING, uri,
357                                         dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE), args,
358                                         G_TYPE_UINT, timestamp,
359                                         G_TYPE_INVALID,
360                                         G_TYPE_INVALID)) {
361                         g_warning ("%s", error->message);
362                         g_clear_error (&error);
363                         g_free (uri);
364                         continue;
365                 }
366
367                 g_free (uri);
368                 result = TRUE;
369         }
370
371         g_object_unref (remote_object);
372         dbus_g_connection_unref (connection);
373
374         gdk_notify_startup_complete ();
375
376         return result;
377 }
378 #endif /* ENABLE_DBUS */
379
380 int
381 main (int argc, char *argv[])
382 {
383         GOptionContext *context;
384         GHashTable *args;
385         GError *error = NULL;
386
387         /* Init glib threads asap */
388         if (!g_thread_supported ())
389                 g_thread_init (NULL);
390
391 #ifdef ENABLE_NLS
392         /* Initialize the i18n stuff */
393         bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR);
394         bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
395         textdomain (GETTEXT_PACKAGE);
396 #endif
397
398         context = g_option_context_new (N_("GNOME Document Viewer"));
399         g_option_context_set_translation_domain(context, GETTEXT_PACKAGE);
400         g_option_context_add_main_entries (context, goption_options, GETTEXT_PACKAGE);
401         
402         g_option_context_add_group (context, egg_sm_client_get_option_group ());
403         g_option_context_add_group (context, gtk_get_option_group (TRUE));
404
405         if (!g_option_context_parse (context, &argc, &argv, &error)) {
406                 g_printerr ("Cannot parse arguments: %s", error->message);
407                 g_error_free (error);
408                 g_option_context_free (context);
409                 
410                 return 1;
411         }
412         g_option_context_free (context);
413
414         if (preview_mode) {
415                 gboolean retval;
416                 
417                 retval = launch_previewer ();
418                 
419                 return retval ? 0 : 1;
420         }
421
422         args = arguments_parse ();
423
424 #ifdef ENABLE_DBUS
425         if (!ev_application_register_service (EV_APP)) {
426                 if (load_files_remote (file_arguments, args)) {
427                         g_hash_table_destroy (args);
428
429                         return 0;
430                 }
431         }
432 #endif /* ENABLE_DBUS */
433         
434         if (!ev_init ())
435                 return 1;
436
437         ev_stock_icons_init ();
438
439 #ifdef G_OS_WIN32
440         /* Manually set name and icon in win32 */
441         g_set_application_name (_("Evince"));
442         gtk_window_set_default_icon_name ("evince");
443 #else
444         egg_set_desktop_file (GNOMEDATADIR "/applications/evince.desktop");
445 #endif /* G_OS_WIN32 */
446
447         ev_application_load_session (EV_APP);
448         load_files (file_arguments, args);
449         g_hash_table_destroy (args);
450
451         /* Change directory so we don't prevent unmounting in case the initial cwd
452          * is on an external device (see bug #575436)
453          */
454         g_chdir (g_get_home_dir ());    
455
456         gtk_main ();
457
458         ev_shutdown ();
459         ev_stock_icons_shutdown ();
460
461         return 0;
462 }