]> www.fi.muni.cz Git - evince.git/blob - shell/main.c
Change process working directory to $HOME so that we don't prevent
[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 void
230 load_files (const char **files,
231             GHashTable  *args)
232 {
233         int i;
234
235         if (!files) {
236                 ev_application_open_window (EV_APP, args, GDK_CURRENT_TIME, NULL);
237                 return;
238         }
239
240         for (i = 0; files[i]; i++) {
241                 char   *uri;
242                 char   *label;
243                 GValue *old = NULL;
244                 GFile  *file;
245
246                 file = g_file_new_for_commandline_arg (files[i]);
247                 uri = g_file_get_uri (file);
248                 g_object_unref (file);
249                 
250                 label = strchr (uri, '#');
251
252                 if (label) {
253                         GValue *new;
254
255                         *label = 0; label++;
256                         
257                         old = g_hash_table_lookup (args, "page-label");
258                         
259                         new = g_new0 (GValue, 1);
260                         g_value_init (new, G_TYPE_STRING);
261                         g_value_set_string (new, label);
262
263                         g_hash_table_insert (args, g_strdup ("page-label"), new);
264
265                 }
266
267                 ev_application_open_uri (EV_APP, uri, args,
268                                          GDK_CURRENT_TIME, NULL);
269
270                 if (old)
271                         g_hash_table_insert (args, g_strdup ("page-label"), old);
272                 
273                 g_free (uri);
274         }
275 }
276
277 #ifdef ENABLE_DBUS
278 static gboolean
279 load_files_remote (const char **files,
280                    GHashTable  *args)
281 {
282         int i;
283         GError *error = NULL;
284         DBusGConnection *connection;
285         gboolean result = FALSE;
286         DBusGProxy *remote_object;
287         GdkDisplay *display;
288         guint32 timestamp;
289
290         display = gdk_display_get_default ();
291         timestamp = gdk_x11_display_get_user_time (display);
292         connection = dbus_g_bus_get (DBUS_BUS_STARTER, &error);
293
294         if (connection == NULL) {
295                 g_warning ("%s", error->message);
296                 g_error_free (error);   
297
298                 return FALSE;
299         }
300
301         remote_object = dbus_g_proxy_new_for_name (connection,
302                                                    "org.gnome.evince.ApplicationService",
303                                                    "/org/gnome/evince/Evince",
304                                                    "org.gnome.evince.Application");
305         if (!files) {
306                 if (!dbus_g_proxy_call (remote_object, "OpenWindow", &error,
307                                         dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE), args,
308                                         G_TYPE_UINT, timestamp,
309                                         G_TYPE_INVALID,
310                                         G_TYPE_INVALID)) {
311                         g_warning ("%s", error->message);
312                         g_clear_error (&error);
313                         g_object_unref (remote_object);
314                         dbus_g_connection_unref (connection);
315                         return FALSE;
316                 }
317
318                 g_object_unref (remote_object);
319                 dbus_g_connection_unref (connection);
320                 
321                 return TRUE;
322         }
323
324         for (i = 0; files[i]; i++) {
325                 const char *page_label;
326                 GFile *file;
327                 char *uri;
328
329                 file = g_file_new_for_commandline_arg (files[i]);
330                 uri = g_file_get_uri (file);
331                 g_object_unref (file);
332
333                 page_label = ev_page_label ? ev_page_label : "";
334
335                 if (!dbus_g_proxy_call (remote_object, "OpenURI", &error,
336                                         G_TYPE_STRING, uri,
337                                         dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE), args,
338                                         G_TYPE_UINT, timestamp,
339                                         G_TYPE_INVALID,
340                                         G_TYPE_INVALID)) {
341                         g_warning ("%s", error->message);
342                         g_clear_error (&error);
343                         g_free (uri);
344                         continue;
345                 }
346
347                 g_free (uri);
348                 result = TRUE;
349         }
350
351         g_object_unref (remote_object);
352         dbus_g_connection_unref (connection);
353
354         gdk_notify_startup_complete ();
355
356         return result;
357 }
358 #endif /* ENABLE_DBUS */
359
360 int
361 main (int argc, char *argv[])
362 {
363         GOptionContext *context;
364         GHashTable *args;
365         GError *error = NULL;
366
367         /* Init glib threads asap */
368         if (!g_thread_supported ())
369                 g_thread_init (NULL);
370
371 #ifdef ENABLE_NLS
372         /* Initialize the i18n stuff */
373         bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR);
374         bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
375         textdomain (GETTEXT_PACKAGE);
376 #endif
377
378         context = g_option_context_new (N_("GNOME Document Viewer"));
379         g_option_context_set_translation_domain(context, GETTEXT_PACKAGE);
380         g_option_context_add_main_entries (context, goption_options, GETTEXT_PACKAGE);
381         
382         g_option_context_add_group (context, egg_sm_client_get_option_group ());
383         g_option_context_add_group (context, gtk_get_option_group (TRUE));
384
385         if (!g_option_context_parse (context, &argc, &argv, &error)) {
386                 g_printerr ("Cannot parse arguments: %s", error->message);
387                 g_error_free (error);
388                 g_option_context_free (context);
389                 
390                 return 1;
391         }
392         g_option_context_free (context);
393
394         if (preview_mode) {
395                 gboolean retval;
396                 
397                 retval = launch_previewer ();
398                 
399                 return retval ? 0 : 1;
400         }
401
402         args = arguments_parse ();
403
404 #ifdef ENABLE_DBUS
405         if (!ev_application_register_service (EV_APP)) {
406                 if (load_files_remote (file_arguments, args)) {
407                         g_hash_table_destroy (args);
408
409                         return 0;
410                 }
411         }
412 #endif /* ENABLE_DBUS */
413         
414         if (!ev_init ())
415                 return 1;
416
417         ev_stock_icons_init ();
418
419 #ifdef G_OS_WIN32
420         /* Manually set name and icon in win32 */
421         g_set_application_name (_("Evince"));
422         gtk_window_set_default_icon_name ("evince");
423 #else
424         egg_set_desktop_file (GNOMEDATADIR "/applications/evince.desktop");
425 #endif /* G_OS_WIN32 */
426
427         if (!ev_application_load_session (EV_APP))
428                 load_files (file_arguments, args);
429         g_hash_table_destroy (args);
430
431         /* Change directory so we don't prevent unmounting in case the initial cwd
432          * is on an external device (see bug #575436)
433          */
434         g_chdir (g_get_home_dir ());    
435
436         gtk_main ();
437
438         ev_shutdown ();
439         ev_stock_icons_shutdown ();
440
441         return 0;
442 }