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