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