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