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