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