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