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