]> www.fi.muni.cz Git - evince.git/blob - shell/main.c
[metadata] Run evince-convert-metadata tool at startup when needed
[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/gstdio.h>
26 #include <glib/gi18n.h>
27 #include <gtk/gtk.h>
28
29 #ifndef G_OS_WIN32
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <fcntl.h>
33 #include <unistd.h>
34 #endif
35
36 #ifdef ENABLE_DBUS
37 #include <gdk/gdkx.h>
38 #include <dbus/dbus-glib-bindings.h>
39 #endif
40
41 #include "ev-application.h"
42 #include "ev-backends-manager.h"
43 #include "ev-debug.h"
44 #include "ev-init.h"
45 #include "ev-file-helpers.h"
46 #include "ev-stock-icons.h"
47 #include "ev-metadata.h"
48
49 #ifdef WITH_SMCLIENT
50 #include "eggsmclient.h"
51 #ifdef GDK_WINDOWING_X11
52 #include "eggdesktopfile.h"
53 #endif
54 #endif /* WITH_SMCLIENT */
55
56 #ifdef G_OS_WIN32
57 #ifdef DATADIR
58 #undef DATADIR
59 #endif
60 #include <io.h>
61 #include <conio.h>
62 #define _WIN32_WINNT 0x0500
63 #include <windows.h>
64 #endif
65
66 static gchar   *ev_page_label;
67 static gchar   *ev_find_string;
68 static gboolean preview_mode = FALSE;
69 static gboolean fullscreen_mode = FALSE;
70 static gboolean presentation_mode = FALSE;
71 static gboolean unlink_temp_file = FALSE;
72 static gchar   *print_settings;
73 static const char **file_arguments = NULL;
74
75 static gboolean
76 option_version_cb (const gchar *option_name,
77                    const gchar *value,
78                    gpointer     data,
79                    GError     **error)
80 {
81   g_print ("%s %s\n", _("GNOME Document Viewer"), VERSION);
82
83   exit (0);
84   return FALSE;
85 }
86
87 static const GOptionEntry goption_options[] =
88 {
89         { "page-label", 'p', 0, G_OPTION_ARG_STRING, &ev_page_label, N_("The page of the document to display."), N_("PAGE")},
90         { "fullscreen", 'f', 0, G_OPTION_ARG_NONE, &fullscreen_mode, N_("Run evince in fullscreen mode"), NULL },
91         { "presentation", 's', 0, G_OPTION_ARG_NONE, &presentation_mode, N_("Run evince in presentation mode"), NULL },
92         { "preview", 'w', 0, G_OPTION_ARG_NONE, &preview_mode, N_("Run evince as a previewer"), NULL },
93         { "find", 'l', 0, G_OPTION_ARG_STRING, &ev_find_string, N_("The word or phrase to find in the document"), N_("STRING")},
94         { "unlink-tempfile", 'u', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_NONE, &unlink_temp_file, NULL, NULL },
95         { "print-settings", 't', G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_FILENAME, &print_settings, NULL, NULL },
96         { "version", 0, G_OPTION_FLAG_NO_ARG | G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_CALLBACK, option_version_cb, NULL, NULL },
97         { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &file_arguments, NULL, N_("[FILE...]") },
98         { NULL }
99 };
100
101 static gboolean
102 launch_previewer (void)
103 {
104         GString *cmd_str;
105         gchar   *cmd;
106         gint     argc;
107         gchar  **argv;
108         gboolean retval = FALSE;
109         GError  *error = NULL;
110
111         /* Rebuild the command line, ignoring options
112          * not supported by the previewer and taking only
113          * the first path given
114          */
115         cmd_str = g_string_new ("evince-previewer");
116                 
117         if (print_settings) {
118                 gchar *quoted;
119
120                 quoted = g_shell_quote (print_settings);
121                 g_string_append_printf (cmd_str, " --print-settings %s", quoted);
122                 g_free (quoted);
123         }
124
125         if (unlink_temp_file)
126                 g_string_append (cmd_str, " --unlink-tempfile");
127
128         if (file_arguments) {
129                 gchar *quoted;
130                 
131                 quoted = g_shell_quote (file_arguments[0]);
132                 g_string_append_printf (cmd_str, " %s", quoted);
133                 g_free (quoted);
134         }
135
136         cmd = g_string_free (cmd_str, FALSE);
137         g_shell_parse_argv (cmd, &argc, &argv, &error);
138         g_free (cmd);
139         
140         if (!error) {
141                 retval = gdk_spawn_on_screen (gdk_screen_get_default (),
142                                               NULL, argv, NULL,
143                                               G_SPAWN_SEARCH_PATH,
144                                               NULL, NULL, NULL,
145                                               &error);
146                 g_strfreev (argv);
147         }
148
149         if (error) {
150                 g_warning ("Error launching previewer: %s\n", error->message);
151                 g_error_free (error);
152         }
153
154         return retval;
155 }
156
157 #ifndef G_OS_WIN32
158 static gboolean
159 convert_metadata (const gchar *metadata)
160 {
161         GFile   *file;
162         gchar   *cmd;
163         gint     exit_status;
164         GError  *error = NULL;
165         gboolean retval;
166
167         /* If metadata is not supported for a local file
168          * is likely because and old gvfs version is running.
169          */
170         file = g_file_new_for_path (metadata);
171         if (!ev_is_metadata_supported_for_file (file)) {
172                 g_warning ("%s\n",
173                            "GVFS metadata not supported, "
174                            "Evince will run without metadata support");
175                 g_object_unref (file);
176                 return FALSE;
177         }
178         g_object_unref (file);
179
180         cmd = g_strdup_printf ("%s %s", LIBEXECDIR"/evince-convert-metadata", metadata);
181
182         retval = g_spawn_command_line_sync (cmd, NULL, NULL, &exit_status, &error);
183         g_free (cmd);
184
185         if (!retval) {
186                 g_printerr ("Error migrating metadata: %s\n", error->message);
187                 g_error_free (error);
188         }
189
190         return retval && exit_status == 0;
191 }
192
193 static void
194 ev_migrate_metadata (void)
195 {
196         gchar *updated;
197         gchar *metadata;
198
199         updated = g_build_filename (ev_application_get_dot_dir (EV_APP),
200                                     "migrated-to-gvfs", NULL);
201         if (g_file_test (updated, G_FILE_TEST_EXISTS)) {
202                 /* Already migrated */
203                 g_free (updated);
204                 return;
205         }
206
207         metadata = g_build_filename (ev_application_get_dot_dir (EV_APP),
208                                      "ev-metadata.xml", NULL);
209         if (g_file_test (metadata, G_FILE_TEST_EXISTS)) {
210                 if (convert_metadata (metadata)) {
211                         gint fd;
212
213                         fd = g_creat (updated, 0600);
214                         if (fd != -1) {
215                                 close (fd);
216                         }
217                 }
218         }
219
220         g_free (updated);
221         g_free (metadata);
222 }
223 #endif /* !G_OS_WIN32 */
224
225 static void
226 value_free (GValue *value)
227 {
228         g_value_unset (value);
229         g_free (value);
230 }
231
232 /**
233  * arguments_parse:
234  *
235  * Parses the arguments and creates a #GHashTable with this data.
236  *
237  *  key                 ->  value
238  *
239  *  dislay              ->  display at the default screen.
240  *  screen              ->  screen number.
241  *  page-label          ->  only if the page label argument has been passed,
242  *                          the page of the document to display.
243  *  mode                ->  only if the view mode is one of the availables,
244  *                          the view mode.
245  *
246  * Returns: a pointer into #GHashTable with data from the arguments.
247  */
248 static GHashTable *
249 arguments_parse (void)
250 {
251         GHashTable      *args;
252         GValue          *value;
253         EvWindowRunMode  mode;
254         GdkScreen       *screen;
255         GdkDisplay      *display;
256         const gchar     *display_name;
257         gint             screen_number;
258
259         args = g_hash_table_new_full (g_str_hash,
260                                       g_str_equal,
261                                       (GDestroyNotify)g_free,
262                                       (GDestroyNotify)value_free);
263         
264         screen = gdk_screen_get_default ();
265         display = gdk_screen_get_display (screen);
266
267         display_name = gdk_display_get_name (display);
268         screen_number = gdk_screen_get_number (screen);
269
270         value = g_new0 (GValue, 1);
271         g_value_init (value, G_TYPE_STRING);
272         g_value_set_string (value, display_name);
273         g_hash_table_insert (args, g_strdup ("display"), value);
274
275         value = g_new0 (GValue, 1);
276         g_value_init (value, G_TYPE_INT);
277         g_value_set_int (value, screen_number);
278         g_hash_table_insert (args, g_strdup ("screen"), value);
279
280         if (ev_page_label) {
281                 value = g_new0 (GValue, 1);
282                 g_value_init (value, G_TYPE_STRING);
283                 g_value_set_string (value, ev_page_label);
284
285                 g_hash_table_insert (args, g_strdup ("page-label"), value);
286
287                 g_free (ev_page_label);
288                 ev_page_label = NULL;
289         }
290
291         if (ev_find_string) {
292                 value = g_new0 (GValue, 1);
293                 g_value_init (value, G_TYPE_STRING);
294                 g_value_set_string (value, ev_find_string);
295
296                 g_hash_table_insert (args, g_strdup ("find-string"), value);
297
298                 g_free (ev_find_string);
299                 ev_page_label = NULL;
300         }
301
302         if (fullscreen_mode)
303                 mode = EV_WINDOW_MODE_FULLSCREEN;
304         else if (presentation_mode)
305                 mode = EV_WINDOW_MODE_PRESENTATION;
306         else
307                 return args;
308
309         value = g_new0 (GValue, 1);
310         g_value_init (value, G_TYPE_UINT);
311         g_value_set_uint (value, mode);
312
313         g_hash_table_insert (args, g_strdup ("mode"), value);
314
315         return args;
316 }
317
318 static gint
319 find_window_list (EvWindow    *window,
320                   const gchar *uri)
321 {
322         return g_ascii_strcasecmp (uri, ev_window_get_uri (window));
323 }
324
325 static void
326 load_files (const char **files,
327             GHashTable  *args)
328 {
329         int    i;
330         GList *windows;
331
332         windows = ev_application_get_windows (EV_APP);
333
334         if (!files) {
335                 if (!windows)
336                         ev_application_open_window (EV_APP, args, GDK_CURRENT_TIME, NULL);
337                 else
338                         g_list_free (windows);
339                 return;
340         }
341
342         for (i = 0; files[i]; i++) {
343                 char   *uri;
344                 char   *label;
345                 GValue *old = NULL;
346                 GFile  *file;
347
348                 file = g_file_new_for_commandline_arg (files[i]);
349                 uri = g_file_get_uri (file);
350                 g_object_unref (file);
351
352                 if (g_list_find_custom (windows, uri, (GCompareFunc) find_window_list)) {
353                         g_free (uri);
354                         continue;
355                 }
356
357                 label = strchr (uri, '#');
358
359                 if (label) {
360                         GValue *new;
361
362                         *label = 0; label++;
363                         
364                         old = g_hash_table_lookup (args, "page-label");
365                         
366                         new = g_new0 (GValue, 1);
367                         g_value_init (new, G_TYPE_STRING);
368                         g_value_set_string (new, label);
369
370                         g_hash_table_insert (args, g_strdup ("page-label"), new);
371
372                 }
373
374                 ev_application_open_uri (EV_APP, uri, args,
375                                          GDK_CURRENT_TIME, NULL);
376
377                 if (old)
378                         g_hash_table_insert (args, g_strdup ("page-label"), old);
379                 
380                 g_free (uri);
381         }
382
383         g_list_free (windows);
384 }
385
386 #ifdef ENABLE_DBUS
387 static gboolean
388 load_files_remote (const char **files,
389                    GHashTable  *args)
390 {
391         int i;
392         GError *error = NULL;
393         DBusGConnection *connection;
394         gboolean result = FALSE;
395         DBusGProxy *remote_object;
396         GdkDisplay *display;
397         guint32 timestamp;
398
399         display = gdk_display_get_default ();
400         timestamp = gdk_x11_display_get_user_time (display);
401         connection = dbus_g_bus_get (DBUS_BUS_STARTER, &error);
402
403         if (connection == NULL) {
404                 g_warning ("%s", error->message);
405                 g_error_free (error);   
406
407                 return FALSE;
408         }
409
410         remote_object = dbus_g_proxy_new_for_name (connection,
411                                                    "org.gnome.evince.ApplicationService",
412                                                    "/org/gnome/evince/Evince",
413                                                    "org.gnome.evince.Application");
414         if (!files) {
415                 if (!dbus_g_proxy_call (remote_object, "OpenWindow", &error,
416                                         dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE), args,
417                                         G_TYPE_UINT, timestamp,
418                                         G_TYPE_INVALID,
419                                         G_TYPE_INVALID)) {
420                         g_warning ("%s", error->message);
421                         g_clear_error (&error);
422                         g_object_unref (remote_object);
423                         dbus_g_connection_unref (connection);
424                         return FALSE;
425                 }
426
427                 g_object_unref (remote_object);
428                 dbus_g_connection_unref (connection);
429                 
430                 return TRUE;
431         }
432
433         for (i = 0; files[i]; i++) {
434                 const char *page_label;
435                 GFile *file;
436                 char *uri;
437
438                 file = g_file_new_for_commandline_arg (files[i]);
439                 uri = g_file_get_uri (file);
440                 g_object_unref (file);
441
442                 page_label = ev_page_label ? ev_page_label : "";
443
444                 if (!dbus_g_proxy_call (remote_object, "OpenURI", &error,
445                                         G_TYPE_STRING, uri,
446                                         dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE), args,
447                                         G_TYPE_UINT, timestamp,
448                                         G_TYPE_INVALID,
449                                         G_TYPE_INVALID)) {
450                         g_warning ("%s", error->message);
451                         g_clear_error (&error);
452                         g_free (uri);
453                         continue;
454                 }
455
456                 g_free (uri);
457                 result = TRUE;
458         }
459
460         g_object_unref (remote_object);
461         dbus_g_connection_unref (connection);
462
463         gdk_notify_startup_complete ();
464
465         return result;
466 }
467 #endif /* ENABLE_DBUS */
468
469 int
470 main (int argc, char *argv[])
471 {
472         GOptionContext *context;
473         GHashTable *args;
474         GError *error = NULL;
475
476 #ifdef G_OS_WIN32
477
478     if (fileno (stdout) != -1 &&
479           _get_osfhandle (fileno (stdout)) != -1)
480         {
481           /* stdout is fine, presumably redirected to a file or pipe */
482         }
483     else
484     {
485           typedef BOOL (* WINAPI AttachConsole_t) (DWORD);
486
487           AttachConsole_t p_AttachConsole =
488             (AttachConsole_t) GetProcAddress (GetModuleHandle ("kernel32.dll"), "AttachConsole");
489
490           if (p_AttachConsole != NULL && p_AttachConsole (ATTACH_PARENT_PROCESS))
491       {
492               freopen ("CONOUT$", "w", stdout);
493               dup2 (fileno (stdout), 1);
494               freopen ("CONOUT$", "w", stderr);
495               dup2 (fileno (stderr), 2);
496
497       }
498         }
499 #endif
500
501         /* Init glib threads asap */
502         if (!g_thread_supported ())
503                 g_thread_init (NULL);
504
505 #ifdef ENABLE_NLS
506         /* Initialize the i18n stuff */
507         bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR);
508         bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
509         textdomain (GETTEXT_PACKAGE);
510 #endif
511
512         context = g_option_context_new (N_("GNOME Document Viewer"));
513         g_option_context_set_translation_domain(context, GETTEXT_PACKAGE);
514         g_option_context_add_main_entries (context, goption_options, GETTEXT_PACKAGE);
515
516 #ifdef WITH_SMCLIENT
517         g_option_context_add_group (context, egg_sm_client_get_option_group ());
518 #endif
519
520         g_option_context_add_group (context, gtk_get_option_group (TRUE));
521
522         if (!g_option_context_parse (context, &argc, &argv, &error)) {
523                 g_printerr ("Cannot parse arguments: %s", error->message);
524                 g_error_free (error);
525                 g_option_context_free (context);
526                 
527                 return 1;
528         }
529         g_option_context_free (context);
530
531         if (preview_mode) {
532                 gboolean retval;
533                 
534                 retval = launch_previewer ();
535                 
536                 return retval ? 0 : 1;
537         }
538
539         args = arguments_parse ();
540
541 #ifdef ENABLE_DBUS
542         if (!ev_application_register_service (EV_APP)) {
543                 if (load_files_remote (file_arguments, args)) {
544                         g_hash_table_destroy (args);
545
546                         return 0;
547                 }
548         }
549 #endif /* ENABLE_DBUS */
550         
551         if (!ev_init ())
552                 return 1;
553
554 #ifndef G_OS_WIN32
555         ev_migrate_metadata ();
556 #endif
557
558         ev_stock_icons_init ();
559
560 #if defined(WITH_SMCLIENT) && defined(GDK_WINDOWING_X11)
561         egg_set_desktop_file (GNOMEDATADIR "/applications/evince.desktop");
562 #else
563         /* Manually set name and icon */
564         g_set_application_name (_("Document Viewer"));
565         gtk_window_set_default_icon_name ("evince");
566 #endif /* WITH_SMCLIENT && GDK_WINDOWING_X11 */
567
568         ev_application_load_session (EV_APP, file_arguments);
569         load_files (file_arguments, args);
570         g_hash_table_destroy (args);
571
572         /* Change directory so we don't prevent unmounting in case the initial cwd
573          * is on an external device (see bug #575436)
574          */
575         g_chdir (g_get_home_dir ());    
576
577         gtk_main ();
578
579         ev_shutdown ();
580         ev_stock_icons_shutdown ();
581
582         return 0;
583 }