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