]> www.fi.muni.cz Git - evince.git/blob - shell/main.c
Add support for multiscreen systems. Fixes bug #316206.
[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         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         return args;
125 }
126
127 static void
128 load_files (const char **files,
129             GHashTable  *args)
130 {
131         int i;
132
133         if (!files) {
134                 ev_application_open_window (EV_APP, args, GDK_CURRENT_TIME, NULL);
135                 return;
136         }
137
138         for (i = 0; files[i]; i++) {
139                 char   *uri;
140                 char   *label;
141                 GValue *old = NULL;
142
143                 uri = gnome_vfs_make_uri_from_shell_arg (files[i]);
144                 
145                 label = strchr (uri, GNOME_VFS_URI_MAGIC_CHR);
146
147                 if (label) {
148                         GValue *new;
149
150                         *label = 0; label++;
151                         
152                         old = g_hash_table_lookup (args, "page-label");
153                         
154                         new = g_new0 (GValue, 1);
155                         g_value_init (new, G_TYPE_STRING);
156                         g_value_set_string (new, label);
157
158                         g_hash_table_insert (args, g_strdup ("page-label"), new);
159
160                 }
161
162                 ev_application_open_uri (EV_APP, uri, args,
163                                          GDK_CURRENT_TIME, NULL);
164
165                 if (old)
166                         g_hash_table_insert (args, g_strdup ("page-label"), old);
167                 
168                 g_free (uri);
169         }
170 }
171
172 #ifdef ENABLE_DBUS
173
174 static gboolean
175 load_files_remote (const char **files,
176                    GHashTable  *args)
177 {
178         int i;
179         GError *error = NULL;
180         DBusGConnection *connection;
181         gboolean result = FALSE;
182 #if DBUS_VERSION < 35
183         DBusGPendingCall *call;
184 #endif
185         DBusGProxy *remote_object;
186         GdkDisplay *display;
187         guint32 timestamp;
188
189         display = gdk_display_get_default ();
190         timestamp = gdk_x11_display_get_user_time (display);
191         connection = dbus_g_bus_get (DBUS_BUS_STARTER, &error);
192
193         if (connection == NULL) {
194                 g_warning (error->message);
195                 g_error_free (error);   
196
197                 return FALSE;
198         }
199
200         remote_object = dbus_g_proxy_new_for_name (connection,
201                                                    "org.gnome.evince.ApplicationService",
202                                                    "/org/gnome/evince/Evince",
203                                                    "org.gnome.evince.Application");
204         if (!files) {
205 #if DBUS_VERSION <= 33
206                 call = dbus_g_proxy_begin_call (remote_object, "OpenWindow",
207                                                 DBUS_TYPE_UINT32, &timestamp,
208                                                 DBUS_TYPE_INVALID);
209
210                 if (!dbus_g_proxy_end_call (remote_object, call, &error, DBUS_TYPE_INVALID)) {
211                         g_warning (error->message);
212                         g_clear_error (&error);
213                         g_object_unref (remote_object);
214                         dbus_g_connection_unref (connection);
215                         return FALSE;
216                 }
217 #elif DBUS_VERSION == 34
218                 call = dbus_g_proxy_begin_call (remote_object, "OpenWindow",
219                                                 G_TYPE_UINT, timestamp,
220                                                 G_TYPE_INVALID);
221
222                 if (!dbus_g_proxy_end_call (remote_object, call, &error, G_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 #else
230                 if (!dbus_g_proxy_call (remote_object, "OpenWindow", &error,
231                                         dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE), args,
232                                         G_TYPE_UINT, timestamp,
233                                         G_TYPE_INVALID,
234                                         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 #endif
242                 g_object_unref (remote_object);
243                 dbus_g_connection_unref (connection);
244                 
245                 return TRUE;
246         }
247
248         for (i = 0; files[i]; i++) {
249                 const char *page_label;
250                 char *uri;
251
252                 uri = gnome_vfs_make_uri_from_shell_arg (files[i]);
253                 page_label = ev_page_label ? ev_page_label : "";
254 #if DBUS_VERSION <= 33
255                 call = dbus_g_proxy_begin_call (remote_object, "OpenURI",
256                                                 DBUS_TYPE_STRING, &uri,
257                                                 DBUS_TYPE_STRING, &page_label,
258                                                 DBUS_TYPE_UINT32, &timestamp,
259                                                 DBUS_TYPE_INVALID);
260
261                 if (!dbus_g_proxy_end_call (remote_object, call, &error, DBUS_TYPE_INVALID)) {
262                         g_warning (error->message);
263                         g_clear_error (&error);
264                         g_free (uri);
265                         continue;
266                 }
267 #elif DBUS_VERSION == 34
268                 call = dbus_g_proxy_begin_call (remote_object, "OpenURI",
269                                                 G_TYPE_STRING, uri,
270                                                 G_TYPE_STRING, page_label,
271                                                 G_TYPE_UINT, timestamp,
272                                                 G_TYPE_INVALID);
273
274                 if (!dbus_g_proxy_end_call (remote_object, call, &error, G_TYPE_INVALID)) {
275                         g_warning (error->message);
276                         g_clear_error (&error);
277                         g_free (uri);
278                         continue;
279                 }
280 #else
281                 if (!dbus_g_proxy_call (remote_object, "OpenURI", &error,
282                                         G_TYPE_STRING, uri,
283                                         dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE), args,
284                                         G_TYPE_UINT, timestamp,
285                                         G_TYPE_INVALID,
286                                         G_TYPE_INVALID)) {
287                         g_warning (error->message);
288                         g_clear_error (&error);
289                         g_free (uri);
290                         continue;
291                 }
292 #endif
293                 g_free (uri);
294                 result = TRUE;
295         }
296
297         g_object_unref (remote_object);
298         dbus_g_connection_unref (connection);
299
300         gdk_notify_startup_complete ();
301
302         return result;
303 }
304 #endif /* ENABLE_DBUS */
305
306 int
307 main (int argc, char *argv[])
308 {
309         gboolean enable_metadata = FALSE;
310         GOptionContext *context;
311         GHashTable *args;
312         GnomeProgram *program;
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         program = gnome_program_init (PACKAGE, VERSION,
327                                       LIBGNOMEUI_MODULE, argc, argv,
328                                       GNOME_PARAM_GOPTION_CONTEXT, context,
329                                       GNOME_PARAM_HUMAN_READABLE_NAME, _("Evince"),
330                                       GNOME_PARAM_APP_DATADIR, GNOMEDATADIR,
331                                       NULL);
332
333         args = arguments_parse ();
334
335 #ifdef ENABLE_DBUS
336         if (!ev_application_register_service (EV_APP)) {
337                 if (load_files_remote (file_arguments, args)) {
338                         g_hash_table_destroy (args);
339                         g_object_unref (program);
340                         
341                         return 0;
342                 }
343         } else {
344                 enable_metadata = TRUE;
345         }
346 #endif
347         
348         gdk_threads_init ();
349         gnome_authentication_manager_init ();
350
351         if (enable_metadata) {
352                 ev_metadata_manager_init ();
353         }
354
355         ev_job_queue_init ();
356         g_set_application_name (_("Evince Document Viewer"));
357
358         ev_file_helpers_init ();
359         ev_debug_init ();
360         ev_stock_icons_init ();
361         gtk_window_set_default_icon_name ("evince");
362
363         load_files (file_arguments, args);
364         g_hash_table_destroy (args);
365
366         gtk_main ();
367
368         gnome_accelerators_sync ();
369         ev_file_helpers_shutdown ();
370
371         if (enable_metadata) {
372                 ev_metadata_manager_shutdown ();
373         }
374         g_object_unref (program);
375
376         return 0;
377 }