]> www.fi.muni.cz Git - evince.git/blob - shell/main.c
c7005cfe205d4e0061e0f7c72dd97b0b71046c8f
[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 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         { "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 (fullscren_mode)
138                 mode = EV_WINDOW_MODE_FULLSCREEN;
139         else if (presentation_mode)
140                 mode = EV_WINDOW_MODE_PRESENTATION;
141         else if (preview_mode)
142                 mode = EV_WINDOW_MODE_PREVIEW;
143         else
144                 return args;
145
146         value = g_new0 (GValue, 1);
147         g_value_init (value, G_TYPE_UINT);
148         g_value_set_uint (value, mode);
149
150         g_hash_table_insert (args, g_strdup ("mode"), value);
151
152         if (mode == EV_WINDOW_MODE_PREVIEW && unlink_temp_file) {
153                 value = g_new0 (GValue, 1);
154                 g_value_init (value, G_TYPE_BOOLEAN);
155                 g_value_set_boolean (value, unlink_temp_file);
156
157                 g_hash_table_insert (args,
158                                      g_strdup ("unlink-temp-file"),
159                                      value);
160         }
161
162         if (mode == EV_WINDOW_MODE_PREVIEW && print_settings) {
163                 value = g_new0 (GValue, 1);
164                 g_value_init (value, G_TYPE_STRING);
165                 g_value_set_string (value, print_settings);
166
167                 g_hash_table_insert (args,
168                                      g_strdup ("print-settings"),
169                                      value);
170                 g_free (print_settings);
171                 print_settings = NULL;
172         }
173
174         return args;
175 }
176
177 static void
178 load_files (const char **files,
179             GHashTable  *args)
180 {
181         int i;
182
183         if (!files) {
184                 ev_application_open_window (EV_APP, args, GDK_CURRENT_TIME, NULL);
185                 return;
186         }
187
188         for (i = 0; files[i]; i++) {
189                 char   *uri;
190                 char   *label;
191                 GValue *old = NULL;
192
193                 uri = gnome_vfs_make_uri_from_shell_arg (files[i]);
194                 
195                 label = strchr (uri, GNOME_VFS_URI_MAGIC_CHR);
196
197                 if (label) {
198                         GValue *new;
199
200                         *label = 0; label++;
201                         
202                         old = g_hash_table_lookup (args, "page-label");
203                         
204                         new = g_new0 (GValue, 1);
205                         g_value_init (new, G_TYPE_STRING);
206                         g_value_set_string (new, label);
207
208                         g_hash_table_insert (args, g_strdup ("page-label"), new);
209
210                 }
211
212                 ev_application_open_uri (EV_APP, uri, args,
213                                          GDK_CURRENT_TIME, NULL);
214
215                 if (old)
216                         g_hash_table_insert (args, g_strdup ("page-label"), old);
217                 
218                 g_free (uri);
219         }
220 }
221
222 #ifdef ENABLE_DBUS
223 static gboolean
224 load_files_remote (const char **files,
225                    GHashTable  *args)
226 {
227         int i;
228         GError *error = NULL;
229         DBusGConnection *connection;
230         gboolean result = FALSE;
231         DBusGProxy *remote_object;
232         GdkDisplay *display;
233         guint32 timestamp;
234
235         display = gdk_display_get_default ();
236         timestamp = gdk_x11_display_get_user_time (display);
237         connection = dbus_g_bus_get (DBUS_BUS_STARTER, &error);
238
239         if (connection == NULL) {
240                 g_warning (error->message);
241                 g_error_free (error);   
242
243                 return FALSE;
244         }
245
246         remote_object = dbus_g_proxy_new_for_name (connection,
247                                                    "org.gnome.evince.ApplicationService",
248                                                    "/org/gnome/evince/Evince",
249                                                    "org.gnome.evince.Application");
250         if (!files) {
251                 if (!dbus_g_proxy_call (remote_object, "OpenWindow", &error,
252                                         dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE), args,
253                                         G_TYPE_UINT, timestamp,
254                                         G_TYPE_INVALID,
255                                         G_TYPE_INVALID)) {
256                         g_warning (error->message);
257                         g_clear_error (&error);
258                         g_object_unref (remote_object);
259                         dbus_g_connection_unref (connection);
260                         return FALSE;
261                 }
262
263                 g_object_unref (remote_object);
264                 dbus_g_connection_unref (connection);
265                 
266                 return TRUE;
267         }
268
269         for (i = 0; files[i]; i++) {
270                 const char *page_label;
271                 char *uri;
272
273                 uri = gnome_vfs_make_uri_from_shell_arg (files[i]);
274                 page_label = ev_page_label ? ev_page_label : "";
275
276                 if (!dbus_g_proxy_call (remote_object, "OpenURI", &error,
277                                         G_TYPE_STRING, uri,
278                                         dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE), args,
279                                         G_TYPE_UINT, timestamp,
280                                         G_TYPE_INVALID,
281                                         G_TYPE_INVALID)) {
282                         g_warning (error->message);
283                         g_clear_error (&error);
284                         g_free (uri);
285                         continue;
286                 }
287
288                 g_free (uri);
289                 result = TRUE;
290         }
291
292         g_object_unref (remote_object);
293         dbus_g_connection_unref (connection);
294
295         gdk_notify_startup_complete ();
296
297         return result;
298 }
299 #endif /* ENABLE_DBUS */
300
301 int
302 main (int argc, char *argv[])
303 {
304         gboolean enable_metadata = FALSE;
305         GOptionContext *context;
306         GHashTable *args;
307 #if WITH_GNOME
308         GnomeProgram *program;
309 #else
310         char *accel_filename;
311         GError *error = NULL;
312 #endif
313
314         context = g_option_context_new (_("GNOME Document Viewer"));
315
316 #ifdef ENABLE_NLS
317         /* Initialize the i18n stuff */
318         bindtextdomain(GETTEXT_PACKAGE, GNOMELOCALEDIR);
319         bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
320         textdomain(GETTEXT_PACKAGE);
321         g_option_context_add_main_entries (context, goption_options, GETTEXT_PACKAGE);
322 #else
323         g_option_context_add_main_entries (context, goption_options, NULL);
324 #endif
325
326 #if WITH_GNOME
327         program = gnome_program_init (PACKAGE, VERSION,
328                                       LIBGNOMEUI_MODULE, argc, argv,
329                                       GNOME_PARAM_GOPTION_CONTEXT, context,
330                                       GNOME_PARAM_HUMAN_READABLE_NAME, _("Evince"),
331                                       GNOME_PARAM_APP_DATADIR, GNOMEDATADIR,
332                                       NULL);
333 #else
334         g_option_context_add_group (context, gtk_get_option_group (TRUE));
335         if (!g_option_context_parse (context, &argc, &argv, &error)) {
336                 g_warning ("Cannot parse arguments: %s", error->message);
337                 g_error_free (error);
338                 return 1;
339         }
340         g_option_context_free (context);
341         
342         gnome_vfs_init ();
343
344         accel_filename = g_build_filename (ev_dot_dir (), "accels", NULL);
345         gtk_accel_map_load (accel_filename);
346 #endif
347
348         args = arguments_parse ();
349
350 #ifdef ENABLE_DBUS
351         if (!ev_application_register_service (EV_APP)) {
352                 if (load_files_remote (file_arguments, args)) {
353                         g_hash_table_destroy (args);
354 #if WITH_GNOME
355                         g_object_unref (program);
356 #endif
357                         return 0;
358                 }
359         } else {
360                 enable_metadata = TRUE;
361         }
362 #endif
363
364 #if WITH_GNOME  
365         gnome_authentication_manager_init ();
366 #endif
367
368         if (enable_metadata) {
369                 ev_metadata_manager_init ();
370         }
371
372         ev_job_queue_init ();
373         g_set_application_name (_("Evince Document Viewer"));
374
375         ev_file_helpers_init ();
376         ev_stock_icons_init ();
377         gtk_window_set_default_icon_name ("evince");
378
379         load_files (file_arguments, args);
380         g_hash_table_destroy (args);
381
382         gtk_main ();
383
384 #if WITH_GNOME
385         gnome_accelerators_sync ();
386 #else
387         gtk_accel_map_save (accel_filename);
388         g_free (accel_filename);
389 #endif
390
391         ev_file_helpers_shutdown ();
392
393         if (enable_metadata) {
394                 ev_metadata_manager_shutdown ();
395         }
396
397 #if WITH_GNOME
398         g_object_unref (program);
399 #endif
400         
401         return 0;
402 }