]> www.fi.muni.cz Git - evince.git/blob - shell/main.c
b7362e06e0255363d3fdb226ca56923a53b56e79
[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
49 static gchar   *ev_page_label;
50 static gchar   *ev_find_string;
51 static gboolean preview_mode = FALSE;
52 static gboolean fullscren_mode = FALSE;
53 static gboolean presentation_mode = FALSE;
54 static gboolean unlink_temp_file = FALSE;
55 static gchar   *print_settings;
56 static const char **file_arguments = NULL;
57
58 static const GOptionEntry goption_options[] =
59 {
60         { "page-label", 'p', 0, G_OPTION_ARG_STRING, &ev_page_label, N_("The page of the document to display."), N_("PAGE")},
61         { "fullscreen", 'f', 0, G_OPTION_ARG_NONE, &fullscren_mode, N_("Run evince in fullscreen mode"), NULL },
62         { "presentation", 's', 0, G_OPTION_ARG_NONE, &presentation_mode, N_("Run evince in presentation mode"), NULL },
63         { "preview", 'w', 0, G_OPTION_ARG_NONE, &preview_mode, N_("Run evince as a previewer"), NULL },
64         { "find", 'l', 0, G_OPTION_ARG_STRING, &ev_find_string, N_("The word or phrase to find in the document"), N_("STRING")},
65         { "unlink-tempfile", 'u', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_NONE, &unlink_temp_file, NULL, NULL },
66         { "print-settings", 't', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_FILENAME, &print_settings, NULL, NULL },
67         { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &file_arguments, NULL, N_("[FILE...]") },
68         { NULL }
69 };
70
71 static void
72 value_free (GValue *value)
73 {
74         g_value_unset (value);
75         g_free (value);
76 }
77
78 /**
79  * arguments_parse:
80  *
81  * Parses the arguments and creates a #GHashTable with this data.
82  *
83  *  key                 ->  value
84  *
85  *  dislay              ->  display at the default screen.
86  *  screen              ->  screen number.
87  *  page-label          ->  only if the page label argument has been passed,
88  *                          the page of the document to display.
89  *  mode                ->  only if the view mode is one of the availables,
90  *                          the view mode.
91  *  unlink-temp-file    ->  only if the view mode is preview mode and
92  *                          unlink-temp-file has been passed, unlink-temp-file.
93  *
94  * Returns: a pointer into #GHashTable with data from the arguments.
95  */
96 static GHashTable *
97 arguments_parse (void)
98 {
99         GHashTable      *args;
100         GValue          *value;
101         EvWindowRunMode  mode;
102         GdkScreen       *screen;
103         GdkDisplay      *display;
104         const gchar     *display_name;
105         gint             screen_number;
106
107         args = g_hash_table_new_full (g_str_hash,
108                                       g_str_equal,
109                                       (GDestroyNotify)g_free,
110                                       (GDestroyNotify)value_free);
111         
112         screen = gdk_screen_get_default ();
113         display = gdk_screen_get_display (screen);
114
115         display_name = gdk_display_get_name (display);
116         screen_number = gdk_screen_get_number (screen);
117
118         value = g_new0 (GValue, 1);
119         g_value_init (value, G_TYPE_STRING);
120         g_value_set_string (value, display_name);
121         g_hash_table_insert (args, g_strdup ("display"), value);
122
123         value = g_new0 (GValue, 1);
124         g_value_init (value, G_TYPE_INT);
125         g_value_set_int (value, screen_number);
126         g_hash_table_insert (args, g_strdup ("screen"), value);
127
128         if (ev_page_label) {
129                 value = g_new0 (GValue, 1);
130                 g_value_init (value, G_TYPE_STRING);
131                 g_value_set_string (value, ev_page_label);
132
133                 g_hash_table_insert (args, g_strdup ("page-label"), value);
134
135                 g_free (ev_page_label);
136                 ev_page_label = NULL;
137         }
138
139         if (ev_find_string) {
140                 value = g_new0 (GValue, 1);
141                 g_value_init (value, G_TYPE_STRING);
142                 g_value_set_string (value, ev_find_string);
143
144                 g_hash_table_insert (args, g_strdup ("find-string"), value);
145
146                 g_free (ev_find_string);
147                 ev_page_label = NULL;
148         }
149
150         if (fullscren_mode)
151                 mode = EV_WINDOW_MODE_FULLSCREEN;
152         else if (presentation_mode)
153                 mode = EV_WINDOW_MODE_PRESENTATION;
154         else if (preview_mode)
155                 mode = EV_WINDOW_MODE_PREVIEW;
156         else
157                 return args;
158
159         value = g_new0 (GValue, 1);
160         g_value_init (value, G_TYPE_UINT);
161         g_value_set_uint (value, mode);
162
163         g_hash_table_insert (args, g_strdup ("mode"), value);
164
165         if (mode == EV_WINDOW_MODE_PREVIEW && unlink_temp_file) {
166                 value = g_new0 (GValue, 1);
167                 g_value_init (value, G_TYPE_BOOLEAN);
168                 g_value_set_boolean (value, unlink_temp_file);
169
170                 g_hash_table_insert (args,
171                                      g_strdup ("unlink-temp-file"),
172                                      value);
173         }
174
175         if (mode == EV_WINDOW_MODE_PREVIEW && print_settings) {
176                 value = g_new0 (GValue, 1);
177                 g_value_init (value, G_TYPE_STRING);
178                 g_value_set_string (value, print_settings);
179
180                 g_hash_table_insert (args,
181                                      g_strdup ("print-settings"),
182                                      value);
183                 g_free (print_settings);
184                 print_settings = NULL;
185         }
186
187         return args;
188 }
189
190 static void
191 load_files (const char **files,
192             GHashTable  *args)
193 {
194         int i;
195
196         if (!files) {
197                 ev_application_open_window (EV_APP, args, GDK_CURRENT_TIME, NULL);
198                 return;
199         }
200
201         for (i = 0; files[i]; i++) {
202                 char   *uri;
203                 char   *label;
204                 GValue *old = NULL;
205
206                 uri = gnome_vfs_make_uri_from_shell_arg (files[i]);
207                 
208                 label = strchr (uri, GNOME_VFS_URI_MAGIC_CHR);
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 (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 (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                 char *uri;
285
286                 uri = gnome_vfs_make_uri_from_shell_arg (files[i]);
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 (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         gboolean enable_metadata = FALSE;
318         GOptionContext *context;
319         GHashTable *args;
320 #if WITH_GNOME
321         GnomeProgram *program;
322 #else
323         char *accel_filename;
324         GError *error = NULL;
325 #endif
326
327         context = g_option_context_new (_("GNOME Document Viewer"));
328
329 #ifdef ENABLE_NLS
330         /* Initialize the i18n stuff */
331         bindtextdomain(GETTEXT_PACKAGE, GNOMELOCALEDIR);
332         bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
333         textdomain(GETTEXT_PACKAGE);
334         g_option_context_add_main_entries (context, goption_options, GETTEXT_PACKAGE);
335         g_option_context_set_translation_domain(context, GETTEXT_PACKAGE);
336 #else
337         g_option_context_add_main_entries (context, goption_options, NULL);
338 #endif
339
340 #if WITH_GNOME
341         program = gnome_program_init (PACKAGE, VERSION,
342                                       LIBGNOMEUI_MODULE, argc, argv,
343                                       GNOME_PARAM_GOPTION_CONTEXT, context,
344                                       GNOME_PARAM_HUMAN_READABLE_NAME, _("Evince"),
345                                       GNOME_PARAM_APP_DATADIR, GNOMEDATADIR,
346                                       NULL);
347 #else
348         g_option_context_add_group (context, gtk_get_option_group (TRUE));
349         if (!g_option_context_parse (context, &argc, &argv, &error)) {
350                 g_warning ("Cannot parse arguments: %s", error->message);
351                 g_error_free (error);
352                 return 1;
353         }
354         g_option_context_free (context);
355         
356         gnome_vfs_init ();
357
358         accel_filename = g_build_filename (ev_dot_dir (), "accels", NULL);
359         gtk_accel_map_load (accel_filename);
360 #endif
361
362         args = arguments_parse ();
363
364 #ifdef ENABLE_DBUS
365         if (!ev_application_register_service (EV_APP)) {
366                 if (load_files_remote (file_arguments, args)) {
367                         g_hash_table_destroy (args);
368 #if WITH_GNOME
369                         g_object_unref (program);
370 #endif
371                         return 0;
372                 }
373         } else {
374                 enable_metadata = TRUE;
375         }
376 #endif
377
378 #if WITH_GNOME  
379         gnome_authentication_manager_init ();
380 #endif
381
382         if (enable_metadata) {
383                 ev_metadata_manager_init ();
384         }
385
386         ev_job_queue_init ();
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 #if WITH_GNOME
412         g_object_unref (program);
413 #endif
414         
415         return 0;
416 }