]> www.fi.muni.cz Git - evince.git/blob - shell/main.c
EggSMClient copied from libegg
[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 <stdlib.h>
23 #include <string.h>
24
25 #include <glib/gi18n.h>
26 #include <gtk/gtk.h>
27 #include <gdk/gdkx.h>
28
29 #ifdef ENABLE_DBUS
30 #include <dbus/dbus-glib-bindings.h>
31 #endif
32
33 #include "ev-application.h"
34 #include "ev-backends-manager.h"
35 #include "ev-debug.h"
36 #include "ev-file-helpers.h"
37 #include "ev-metadata-manager.h"
38 #include "ev-stock-icons.h"
39 #include "eggsmclient.h"
40
41 static gchar   *ev_page_label;
42 static gchar   *ev_find_string;
43 static gboolean preview_mode = FALSE;
44 static gboolean fullscren_mode = FALSE;
45 static gboolean presentation_mode = FALSE;
46 static gboolean unlink_temp_file = FALSE;
47 static gchar   *print_settings;
48 static const char **file_arguments = NULL;
49
50 static const GOptionEntry goption_options[] =
51 {
52         { "page-label", 'p', 0, G_OPTION_ARG_STRING, &ev_page_label, N_("The page of the document to display."), N_("PAGE")},
53         { "fullscreen", 'f', 0, G_OPTION_ARG_NONE, &fullscren_mode, N_("Run evince in fullscreen mode"), NULL },
54         { "presentation", 's', 0, G_OPTION_ARG_NONE, &presentation_mode, N_("Run evince in presentation mode"), NULL },
55         { "preview", 'w', 0, G_OPTION_ARG_NONE, &preview_mode, N_("Run evince as a previewer"), NULL },
56         { "find", 'l', 0, G_OPTION_ARG_STRING, &ev_find_string, N_("The word or phrase to find in the document"), N_("STRING")},
57         { "unlink-tempfile", 'u', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_NONE, &unlink_temp_file, NULL, NULL },
58         { "print-settings", 't', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_FILENAME, &print_settings, 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 /**
71  * arguments_parse:
72  *
73  * Parses the arguments and creates a #GHashTable with this data.
74  *
75  *  key                 ->  value
76  *
77  *  dislay              ->  display at the default screen.
78  *  screen              ->  screen number.
79  *  page-label          ->  only if the page label argument has been passed,
80  *                          the page of the document to display.
81  *  mode                ->  only if the view mode is one of the availables,
82  *                          the view mode.
83  *  unlink-temp-file    ->  only if the view mode is preview mode and
84  *                          unlink-temp-file has been passed, unlink-temp-file.
85  *
86  * Returns: a pointer into #GHashTable with data from the arguments.
87  */
88 static GHashTable *
89 arguments_parse (void)
90 {
91         GHashTable      *args;
92         GValue          *value;
93         EvWindowRunMode  mode;
94         GdkScreen       *screen;
95         GdkDisplay      *display;
96         const gchar     *display_name;
97         gint             screen_number;
98
99         args = g_hash_table_new_full (g_str_hash,
100                                       g_str_equal,
101                                       (GDestroyNotify)g_free,
102                                       (GDestroyNotify)value_free);
103         
104         screen = gdk_screen_get_default ();
105         display = gdk_screen_get_display (screen);
106
107         display_name = gdk_display_get_name (display);
108         screen_number = gdk_screen_get_number (screen);
109
110         value = g_new0 (GValue, 1);
111         g_value_init (value, G_TYPE_STRING);
112         g_value_set_string (value, display_name);
113         g_hash_table_insert (args, g_strdup ("display"), value);
114
115         value = g_new0 (GValue, 1);
116         g_value_init (value, G_TYPE_INT);
117         g_value_set_int (value, screen_number);
118         g_hash_table_insert (args, g_strdup ("screen"), value);
119
120         if (ev_page_label) {
121                 value = g_new0 (GValue, 1);
122                 g_value_init (value, G_TYPE_STRING);
123                 g_value_set_string (value, ev_page_label);
124
125                 g_hash_table_insert (args, g_strdup ("page-label"), value);
126
127                 g_free (ev_page_label);
128                 ev_page_label = NULL;
129         }
130
131         if (ev_find_string) {
132                 value = g_new0 (GValue, 1);
133                 g_value_init (value, G_TYPE_STRING);
134                 g_value_set_string (value, ev_find_string);
135
136                 g_hash_table_insert (args, g_strdup ("find-string"), value);
137
138                 g_free (ev_find_string);
139                 ev_page_label = NULL;
140         }
141
142         if (fullscren_mode)
143                 mode = EV_WINDOW_MODE_FULLSCREEN;
144         else if (presentation_mode)
145                 mode = EV_WINDOW_MODE_PRESENTATION;
146         else if (preview_mode)
147                 mode = EV_WINDOW_MODE_PREVIEW;
148         else
149                 return args;
150
151         value = g_new0 (GValue, 1);
152         g_value_init (value, G_TYPE_UINT);
153         g_value_set_uint (value, mode);
154
155         g_hash_table_insert (args, g_strdup ("mode"), value);
156
157         if (mode == EV_WINDOW_MODE_PREVIEW && unlink_temp_file) {
158                 value = g_new0 (GValue, 1);
159                 g_value_init (value, G_TYPE_BOOLEAN);
160                 g_value_set_boolean (value, unlink_temp_file);
161
162                 g_hash_table_insert (args,
163                                      g_strdup ("unlink-temp-file"),
164                                      value);
165         }
166
167         if (mode == EV_WINDOW_MODE_PREVIEW && print_settings) {
168                 value = g_new0 (GValue, 1);
169                 g_value_init (value, G_TYPE_STRING);
170                 g_value_set_string (value, print_settings);
171
172                 g_hash_table_insert (args,
173                                      g_strdup ("print-settings"),
174                                      value);
175                 g_free (print_settings);
176                 print_settings = NULL;
177         }
178
179         return args;
180 }
181
182 static void
183 load_files (const char **files,
184             GHashTable  *args)
185 {
186         int i;
187
188         if (!files) {
189                 ev_application_open_window (EV_APP, args, GDK_CURRENT_TIME, NULL);
190                 return;
191         }
192
193         for (i = 0; files[i]; i++) {
194                 char   *uri;
195                 char   *label;
196                 GValue *old = NULL;
197                 GFile  *file;
198
199                 file = g_file_new_for_commandline_arg (files[i]);
200                 uri = g_file_get_uri (file);
201                 g_object_unref (file);
202                 
203                 label = strchr (uri, '#');
204
205                 if (label) {
206                         GValue *new;
207
208                         *label = 0; label++;
209                         
210                         old = g_hash_table_lookup (args, "page-label");
211                         
212                         new = g_new0 (GValue, 1);
213                         g_value_init (new, G_TYPE_STRING);
214                         g_value_set_string (new, label);
215
216                         g_hash_table_insert (args, g_strdup ("page-label"), new);
217
218                 }
219
220                 ev_application_open_uri (EV_APP, uri, args,
221                                          GDK_CURRENT_TIME, NULL);
222
223                 if (old)
224                         g_hash_table_insert (args, g_strdup ("page-label"), old);
225                 
226                 g_free (uri);
227         }
228 }
229
230 #ifdef ENABLE_DBUS
231 static gboolean
232 load_files_remote (const char **files,
233                    GHashTable  *args)
234 {
235         int i;
236         GError *error = NULL;
237         DBusGConnection *connection;
238         gboolean result = FALSE;
239         DBusGProxy *remote_object;
240         GdkDisplay *display;
241         guint32 timestamp;
242
243         display = gdk_display_get_default ();
244         timestamp = gdk_x11_display_get_user_time (display);
245         connection = dbus_g_bus_get (DBUS_BUS_STARTER, &error);
246
247         if (connection == NULL) {
248                 g_warning ("%s", error->message);
249                 g_error_free (error);   
250
251                 return FALSE;
252         }
253
254         remote_object = dbus_g_proxy_new_for_name (connection,
255                                                    "org.gnome.evince.ApplicationService",
256                                                    "/org/gnome/evince/Evince",
257                                                    "org.gnome.evince.Application");
258         if (!files) {
259                 if (!dbus_g_proxy_call (remote_object, "OpenWindow", &error,
260                                         dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE), args,
261                                         G_TYPE_UINT, timestamp,
262                                         G_TYPE_INVALID,
263                                         G_TYPE_INVALID)) {
264                         g_warning ("%s", error->message);
265                         g_clear_error (&error);
266                         g_object_unref (remote_object);
267                         dbus_g_connection_unref (connection);
268                         return FALSE;
269                 }
270
271                 g_object_unref (remote_object);
272                 dbus_g_connection_unref (connection);
273                 
274                 return TRUE;
275         }
276
277         for (i = 0; files[i]; i++) {
278                 const char *page_label;
279                 GFile *file;
280                 char *uri;
281
282                 file = g_file_new_for_commandline_arg (files[i]);
283                 uri = g_file_get_uri (file);
284                 g_object_unref (file);
285
286                 page_label = ev_page_label ? ev_page_label : "";
287
288                 if (!dbus_g_proxy_call (remote_object, "OpenURI", &error,
289                                         G_TYPE_STRING, uri,
290                                         dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE), args,
291                                         G_TYPE_UINT, timestamp,
292                                         G_TYPE_INVALID,
293                                         G_TYPE_INVALID)) {
294                         g_warning ("%s", error->message);
295                         g_clear_error (&error);
296                         g_free (uri);
297                         continue;
298                 }
299
300                 g_free (uri);
301                 result = TRUE;
302         }
303
304         g_object_unref (remote_object);
305         dbus_g_connection_unref (connection);
306
307         gdk_notify_startup_complete ();
308
309         return result;
310 }
311 #endif /* ENABLE_DBUS */
312
313 int
314 main (int argc, char *argv[])
315 {
316         GOptionContext *context;
317         GHashTable *args;
318         gboolean enable_metadata = FALSE;
319         GError *error = NULL;
320
321         /* Init glib threads asap */
322         if (!g_thread_supported ())
323                 g_thread_init (NULL);
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         g_option_context_set_translation_domain(context, GETTEXT_PACKAGE);
334 #else
335         g_option_context_add_main_entries (context, goption_options, NULL);
336 #endif /* ENABLE_NLS */
337         
338         g_option_context_add_group (context, egg_sm_client_get_option_group ());
339         g_option_context_add_group (context, gtk_get_option_group (TRUE));
340
341         gtk_init (&argc, &argv);
342
343         if (!g_option_context_parse (context, &argc, &argv, &error)) {
344                 g_warning ("Cannot parse arguments: %s", error->message);
345                 g_error_free (error);
346                 g_option_context_free (context);
347                 
348                 return 1;
349         }
350         g_option_context_free (context);
351
352         args = arguments_parse ();
353
354 #ifdef ENABLE_DBUS
355         if (!ev_application_register_service (EV_APP)) {
356                 if (load_files_remote (file_arguments, args)) {
357                         g_hash_table_destroy (args);
358
359                         return 0;
360                 }
361         } else {
362                 enable_metadata = TRUE;
363         }
364 #endif /* ENABLE_DBUS */
365
366         ev_debug_init ();
367         ev_backends_manager_init ();
368         
369         if (enable_metadata) {
370                 ev_metadata_manager_init ();
371         }
372
373         ev_file_helpers_init ();
374         ev_stock_icons_init ();
375         gtk_window_set_default_icon_name ("evince");
376
377         if (!ev_application_load_session (EV_APP))
378                 load_files (file_arguments, args);
379         g_hash_table_destroy (args);
380
381         gtk_main ();
382
383         ev_file_helpers_shutdown ();
384
385         if (enable_metadata) {
386                 ev_metadata_manager_shutdown ();
387         }
388
389         ev_backends_manager_shutdown ();
390
391         ev_debug_shutdown ();
392
393         return 0;
394 }