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