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