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