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