]> www.fi.muni.cz Git - evince.git/blob - shell/main.c
A libdocument/ev-init.[ch]: Add single init/shutdown method. Bug #569117.
[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/gi18n.h>
26 #include <gtk/gtk.h>
27
28 #ifdef ENABLE_DBUS
29 #include <gdk/gdkx.h>
30 #include <dbus/dbus-glib-bindings.h>
31 #endif
32
33 #include "ev-application.h"
34 #include "ev-backends-manager.h"
35 #include "ev-debug.h"
36 #include "ev-init.h"
37 #include "ev-file-helpers.h"
38 #include "ev-stock-icons.h"
39 #include "eggsmclient.h"
40 #include "eggdesktopfile.h"
41
42 static gchar   *ev_page_label;
43 static gchar   *ev_find_string;
44 static gboolean preview_mode = FALSE;
45 static gboolean fullscreen_mode = FALSE;
46 static gboolean presentation_mode = FALSE;
47 static gboolean unlink_temp_file = FALSE;
48 static gchar   *print_settings;
49 static const char **file_arguments = NULL;
50
51 static gboolean
52 option_version_cb (const gchar *option_name,
53                    const gchar *value,
54                    gpointer     data,
55                    GError     **error)
56 {
57   g_print ("%s %s\n", _("GNOME Document Viewer"), VERSION);
58
59   exit (0);
60   return FALSE;
61 }
62
63 static const GOptionEntry goption_options[] =
64 {
65         { "page-label", 'p', 0, G_OPTION_ARG_STRING, &ev_page_label, N_("The page of the document to display."), N_("PAGE")},
66         { "fullscreen", 'f', 0, G_OPTION_ARG_NONE, &fullscreen_mode, N_("Run evince in fullscreen mode"), NULL },
67         { "presentation", 's', 0, G_OPTION_ARG_NONE, &presentation_mode, N_("Run evince in presentation mode"), NULL },
68         { "preview", 'w', 0, G_OPTION_ARG_NONE, &preview_mode, N_("Run evince as a previewer"), NULL },
69         { "find", 'l', 0, G_OPTION_ARG_STRING, &ev_find_string, N_("The word or phrase to find in the document"), N_("STRING")},
70         { "unlink-tempfile", 'u', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_NONE, &unlink_temp_file, NULL, NULL },
71         { "print-settings", 't', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_FILENAME, &print_settings, NULL, NULL },
72         { "version", 0, G_OPTION_FLAG_NO_ARG | G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_CALLBACK, option_version_cb, NULL, NULL },
73         { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &file_arguments, NULL, N_("[FILE...]") },
74         { NULL }
75 };
76
77 static void
78 value_free (GValue *value)
79 {
80         g_value_unset (value);
81         g_free (value);
82 }
83
84 /**
85  * arguments_parse:
86  *
87  * Parses the arguments and creates a #GHashTable with this data.
88  *
89  *  key                 ->  value
90  *
91  *  dislay              ->  display at the default screen.
92  *  screen              ->  screen number.
93  *  page-label          ->  only if the page label argument has been passed,
94  *                          the page of the document to display.
95  *  mode                ->  only if the view mode is one of the availables,
96  *                          the view mode.
97  *  unlink-temp-file    ->  only if the view mode is preview mode and
98  *                          unlink-temp-file has been passed, unlink-temp-file.
99  *
100  * Returns: a pointer into #GHashTable with data from the arguments.
101  */
102 static GHashTable *
103 arguments_parse (void)
104 {
105         GHashTable      *args;
106         GValue          *value;
107         EvWindowRunMode  mode;
108         GdkScreen       *screen;
109         GdkDisplay      *display;
110         const gchar     *display_name;
111         gint             screen_number;
112
113         args = g_hash_table_new_full (g_str_hash,
114                                       g_str_equal,
115                                       (GDestroyNotify)g_free,
116                                       (GDestroyNotify)value_free);
117         
118         screen = gdk_screen_get_default ();
119         display = gdk_screen_get_display (screen);
120
121         display_name = gdk_display_get_name (display);
122         screen_number = gdk_screen_get_number (screen);
123
124         value = g_new0 (GValue, 1);
125         g_value_init (value, G_TYPE_STRING);
126         g_value_set_string (value, display_name);
127         g_hash_table_insert (args, g_strdup ("display"), value);
128
129         value = g_new0 (GValue, 1);
130         g_value_init (value, G_TYPE_INT);
131         g_value_set_int (value, screen_number);
132         g_hash_table_insert (args, g_strdup ("screen"), value);
133
134         if (ev_page_label) {
135                 value = g_new0 (GValue, 1);
136                 g_value_init (value, G_TYPE_STRING);
137                 g_value_set_string (value, ev_page_label);
138
139                 g_hash_table_insert (args, g_strdup ("page-label"), value);
140
141                 g_free (ev_page_label);
142                 ev_page_label = NULL;
143         }
144
145         if (ev_find_string) {
146                 value = g_new0 (GValue, 1);
147                 g_value_init (value, G_TYPE_STRING);
148                 g_value_set_string (value, ev_find_string);
149
150                 g_hash_table_insert (args, g_strdup ("find-string"), value);
151
152                 g_free (ev_find_string);
153                 ev_page_label = NULL;
154         }
155
156         if (fullscreen_mode)
157                 mode = EV_WINDOW_MODE_FULLSCREEN;
158         else if (presentation_mode)
159                 mode = EV_WINDOW_MODE_PRESENTATION;
160         else if (preview_mode)
161                 mode = EV_WINDOW_MODE_PREVIEW;
162         else
163                 return args;
164
165         value = g_new0 (GValue, 1);
166         g_value_init (value, G_TYPE_UINT);
167         g_value_set_uint (value, mode);
168
169         g_hash_table_insert (args, g_strdup ("mode"), value);
170
171         if (mode == EV_WINDOW_MODE_PREVIEW && unlink_temp_file) {
172                 value = g_new0 (GValue, 1);
173                 g_value_init (value, G_TYPE_BOOLEAN);
174                 g_value_set_boolean (value, unlink_temp_file);
175
176                 g_hash_table_insert (args,
177                                      g_strdup ("unlink-temp-file"),
178                                      value);
179         }
180
181         if (mode == EV_WINDOW_MODE_PREVIEW && print_settings) {
182                 value = g_new0 (GValue, 1);
183                 g_value_init (value, G_TYPE_STRING);
184                 g_value_set_string (value, print_settings);
185
186                 g_hash_table_insert (args,
187                                      g_strdup ("print-settings"),
188                                      value);
189                 g_free (print_settings);
190                 print_settings = NULL;
191         }
192
193         return args;
194 }
195
196 static void
197 load_files (const char **files,
198             GHashTable  *args)
199 {
200         int i;
201
202         if (!files) {
203                 ev_application_open_window (EV_APP, args, GDK_CURRENT_TIME, NULL);
204                 return;
205         }
206
207         for (i = 0; files[i]; i++) {
208                 char   *uri;
209                 char   *label;
210                 GValue *old = NULL;
211                 GFile  *file;
212
213                 file = g_file_new_for_commandline_arg (files[i]);
214                 uri = g_file_get_uri (file);
215                 g_object_unref (file);
216                 
217                 label = strchr (uri, '#');
218
219                 if (label) {
220                         GValue *new;
221
222                         *label = 0; label++;
223                         
224                         old = g_hash_table_lookup (args, "page-label");
225                         
226                         new = g_new0 (GValue, 1);
227                         g_value_init (new, G_TYPE_STRING);
228                         g_value_set_string (new, label);
229
230                         g_hash_table_insert (args, g_strdup ("page-label"), new);
231
232                 }
233
234                 ev_application_open_uri (EV_APP, uri, args,
235                                          GDK_CURRENT_TIME, NULL);
236
237                 if (old)
238                         g_hash_table_insert (args, g_strdup ("page-label"), old);
239                 
240                 g_free (uri);
241         }
242 }
243
244 #ifdef ENABLE_DBUS
245 static gboolean
246 load_files_remote (const char **files,
247                    GHashTable  *args)
248 {
249         int i;
250         GError *error = NULL;
251         DBusGConnection *connection;
252         gboolean result = FALSE;
253         DBusGProxy *remote_object;
254         GdkDisplay *display;
255         guint32 timestamp;
256
257         display = gdk_display_get_default ();
258         timestamp = gdk_x11_display_get_user_time (display);
259         connection = dbus_g_bus_get (DBUS_BUS_STARTER, &error);
260
261         if (connection == NULL) {
262                 g_warning ("%s", error->message);
263                 g_error_free (error);   
264
265                 return FALSE;
266         }
267
268         remote_object = dbus_g_proxy_new_for_name (connection,
269                                                    "org.gnome.evince.ApplicationService",
270                                                    "/org/gnome/evince/Evince",
271                                                    "org.gnome.evince.Application");
272         if (!files) {
273                 if (!dbus_g_proxy_call (remote_object, "OpenWindow", &error,
274                                         dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE), args,
275                                         G_TYPE_UINT, timestamp,
276                                         G_TYPE_INVALID,
277                                         G_TYPE_INVALID)) {
278                         g_warning ("%s", error->message);
279                         g_clear_error (&error);
280                         g_object_unref (remote_object);
281                         dbus_g_connection_unref (connection);
282                         return FALSE;
283                 }
284
285                 g_object_unref (remote_object);
286                 dbus_g_connection_unref (connection);
287                 
288                 return TRUE;
289         }
290
291         for (i = 0; files[i]; i++) {
292                 const char *page_label;
293                 GFile *file;
294                 char *uri;
295
296                 file = g_file_new_for_commandline_arg (files[i]);
297                 uri = g_file_get_uri (file);
298                 g_object_unref (file);
299
300                 page_label = ev_page_label ? ev_page_label : "";
301
302                 if (!dbus_g_proxy_call (remote_object, "OpenURI", &error,
303                                         G_TYPE_STRING, uri,
304                                         dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE), args,
305                                         G_TYPE_UINT, timestamp,
306                                         G_TYPE_INVALID,
307                                         G_TYPE_INVALID)) {
308                         g_warning ("%s", error->message);
309                         g_clear_error (&error);
310                         g_free (uri);
311                         continue;
312                 }
313
314                 g_free (uri);
315                 result = TRUE;
316         }
317
318         g_object_unref (remote_object);
319         dbus_g_connection_unref (connection);
320
321         gdk_notify_startup_complete ();
322
323         return result;
324 }
325 #endif /* ENABLE_DBUS */
326
327 int
328 main (int argc, char *argv[])
329 {
330         GOptionContext *context;
331         GHashTable *args;
332         GError *error = NULL;
333
334         /* Init glib threads asap */
335         if (!g_thread_supported ())
336                 g_thread_init (NULL);
337
338 #ifdef ENABLE_NLS
339         /* Initialize the i18n stuff */
340         bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR);
341         bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
342         textdomain (GETTEXT_PACKAGE);
343 #endif
344
345         context = g_option_context_new (N_("GNOME Document Viewer"));
346         g_option_context_set_translation_domain(context, GETTEXT_PACKAGE);
347         g_option_context_add_main_entries (context, goption_options, GETTEXT_PACKAGE);
348         
349         g_option_context_add_group (context, egg_sm_client_get_option_group ());
350         g_option_context_add_group (context, gtk_get_option_group (TRUE));
351
352         if (!g_option_context_parse (context, &argc, &argv, &error)) {
353                 g_printerr ("Cannot parse arguments: %s", error->message);
354                 g_error_free (error);
355                 g_option_context_free (context);
356                 
357                 return 1;
358         }
359         g_option_context_free (context);
360
361         args = arguments_parse ();
362
363 #ifdef ENABLE_DBUS
364         if (!ev_application_register_service (EV_APP)) {
365                 if (load_files_remote (file_arguments, args)) {
366                         g_hash_table_destroy (args);
367
368                         return 0;
369                 }
370         }
371 #endif /* ENABLE_DBUS */
372
373         if (!ev_init ())
374                 return 1;
375
376         ev_stock_icons_init ();
377         
378         egg_set_desktop_file (GNOMEDATADIR "/applications/evince.desktop");
379
380         if (!ev_application_load_session (EV_APP))
381                 load_files (file_arguments, args);
382         g_hash_table_destroy (args);
383
384         gtk_main ();
385
386         ev_shutdown ();
387
388         return 0;
389 }