]> www.fi.muni.cz Git - evince.git/blob - shell/ev-application.c
Use always a different process for every document
[evince.git] / shell / ev-application.c
1 /* this file is part of evince, a gnome document viewer
2  *
3  *  Copyright (C) 2004 Martin Kretzschmar
4  *
5  *  Author:
6  *    Martin Kretzschmar <martink@gnome.org>
7  *
8  * Evince is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * Evince is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
21  */
22
23
24 #include <config.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include <glib.h>
29 #include <glib/gi18n.h>
30 #include <gtk/gtk.h>
31 #ifdef GDK_WINDOWING_X11
32 #include <gdk/gdkx.h>
33 #endif
34
35 #include "totem-scrsaver.h"
36
37 #ifdef WITH_SMCLIENT
38 #include "eggsmclient.h"
39 #endif
40
41 #include "ev-application.h"
42 #include "ev-document-factory.h"
43 #include "ev-file-helpers.h"
44 #include "ev-utils.h"
45 #include "ev-stock-icons.h"
46
47 #ifdef ENABLE_DBUS
48 #include "ev-media-player-keys.h"
49 #endif /* ENABLE_DBUS */
50
51 #ifdef ENABLE_DBUS
52 #include <dbus/dbus-glib-bindings.h>
53 static gboolean ev_application_open_uri (EvApplication  *application,
54                                          const char     *uri,
55                                          GHashTable     *args,
56                                          guint           timestamp,
57                                          GError        **error);
58 #include "ev-application-service.h"
59 #endif
60
61 static void ev_application_save_print_settings (EvApplication *application);
62
63 struct _EvApplication {
64         GObject base_instance;
65
66         EvWindow *window;
67         gchar    *uri;
68
69         gchar *dot_dir;
70         gchar *data_dir;
71         gchar *accel_map_file;
72         gchar *toolbars_file;
73
74 #ifdef ENABLE_DBUS
75         DBusGConnection *connection;
76         EvMediaPlayerKeys *keys;
77 #endif
78
79         EggToolbarsModel *toolbars_model;
80
81         TotemScrsaver *scr_saver;
82
83 #ifdef WITH_SMCLIENT
84         EggSMClient *smclient;
85 #endif
86
87         gchar *filechooser_open_uri;
88         gchar *filechooser_save_uri;
89
90         GtkPrintSettings *print_settings;
91         GtkPageSetup     *page_setup;
92         GKeyFile         *print_settings_file;
93 };
94
95 struct _EvApplicationClass {
96         GObjectClass base_class;
97 };
98
99 static EvApplication *instance;
100
101 G_DEFINE_TYPE (EvApplication, ev_application, G_TYPE_OBJECT);
102
103 #ifdef ENABLE_DBUS
104 #define APPLICATION_DBUS_OBJECT_PATH "/org/gnome/evince/Evince"
105 #define APPLICATION_DBUS_INTERFACE   "org.gnome.evince.Application"
106 #endif
107
108 #define EV_PRINT_SETTINGS_FILE "print-settings"
109 #define EV_PRINT_SETTINGS_GROUP "Print Settings"
110 #define EV_PAGE_SETUP_GROUP "Page Setup"
111
112 /**
113  * ev_application_get_instance:
114  *
115  * Checks for #EvApplication instance, if it doesn't exist it does create it.
116  *
117  * Returns: an instance of the #EvApplication data.
118  */
119 EvApplication *
120 ev_application_get_instance (void)
121 {
122         if (!instance) {
123                 instance = EV_APPLICATION (g_object_new (EV_TYPE_APPLICATION, NULL));
124         }
125
126         return instance;
127 }
128
129 /* Session */
130 gboolean
131 ev_application_load_session (EvApplication *application)
132 {
133         GKeyFile *state_file;
134         gchar    *uri;
135
136 #ifdef WITH_SMCLIENT
137         if (egg_sm_client_is_resumed (application->smclient)) {
138                 state_file = egg_sm_client_get_state_file (application->smclient);
139                 if (!state_file)
140                         return FALSE;
141         } else
142 #endif /* WITH_SMCLIENT */
143                 return FALSE;
144
145         uri = g_key_file_get_string (state_file, "Evince", "uri", NULL);
146         if (!uri)
147                 return FALSE;
148
149         ev_application_open_uri_at_dest (application, uri,
150                                          gdk_screen_get_default (),
151                                          NULL, 0, NULL,
152                                          GDK_CURRENT_TIME);
153         g_free (uri);
154         g_key_file_free (state_file);
155
156         return TRUE;
157 }
158
159 #ifdef WITH_SMCLIENT
160
161 static void
162 smclient_save_state_cb (EggSMClient   *client,
163                         GKeyFile      *state_file,
164                         EvApplication *application)
165 {
166         if (!application->uri)
167                 return;
168
169         g_key_file_set_string (state_file, "Evince", "uri", application->uri);
170 }
171
172 static void
173 smclient_quit_cb (EggSMClient   *client,
174                   EvApplication *application)
175 {
176         ev_application_shutdown (application);
177 }
178 #endif /* WITH_SMCLIENT */
179
180 static void
181 ev_application_init_session (EvApplication *application)
182 {
183 #ifdef WITH_SMCLIENT
184         application->smclient = egg_sm_client_get ();
185         g_signal_connect (application->smclient, "save_state",
186                           G_CALLBACK (smclient_save_state_cb),
187                           application);
188         g_signal_connect (application->smclient, "quit",
189                           G_CALLBACK (smclient_quit_cb),
190                           application);
191 #endif
192 }
193
194 /**
195  * ev_display_open_if_needed:
196  * @name: the name of the display to be open if it's needed.
197  *
198  * Search among all the open displays if any of them have the same name as the
199  * passed name. If the display isn't found it tries the open it.
200  *
201  * Returns: a #GdkDisplay of the display with the passed name.
202  */
203 static GdkDisplay *
204 ev_display_open_if_needed (const gchar *name)
205 {
206         GSList     *displays;
207         GSList     *l;
208         GdkDisplay *display = NULL;
209
210         displays = gdk_display_manager_list_displays (gdk_display_manager_get ());
211
212         for (l = displays; l != NULL; l = l->next) {
213                 const gchar *display_name = gdk_display_get_name ((GdkDisplay *) l->data);
214
215                 if (g_ascii_strcasecmp (display_name, name) == 0) {
216                         display = l->data;
217                         break;
218                 }
219         }
220
221         g_slist_free (displays);
222
223         return display != NULL ? display : gdk_display_open (name);
224 }
225
226 /**
227  * get_screen_from_args:
228  * @args: a #GHashTable with data passed to the application.
229  *
230  * Looks for the screen in the display available in the hash table passed to the
231  * application. If the display isn't opened, it's opened and the #GdkScreen
232  * assigned to the screen in that display returned.
233  *
234  * Returns: the #GdkScreen assigned to the screen on the display indicated by
235  *          the data on the #GHashTable.
236  */
237 static GdkScreen *
238 get_screen_from_args (GHashTable *args)
239 {
240         GValue     *value = NULL;
241         GdkDisplay *display = NULL;
242         GdkScreen  *screen = NULL;
243
244         g_assert (args != NULL);
245         
246         value = g_hash_table_lookup (args, "display");
247         if (value) {
248                 const gchar *display_name;
249                 
250                 display_name = g_value_get_string (value);
251                 display = ev_display_open_if_needed (display_name);
252         }
253         
254         value = g_hash_table_lookup (args, "screen");
255         if (value) {
256                 gint screen_number;
257                 
258                 screen_number = g_value_get_int (value);
259                 screen = gdk_display_get_screen (display, screen_number);
260         }
261
262         return screen;
263 }
264
265 /**
266  * get_window_run_mode_from_args:
267  * @args: a #GHashTable with data passed to the application.
268  *
269  * It does look if the mode option has been passed from command line, using it
270  * as the window run mode, otherwise the run mode will be the normal mode.
271  *
272  * Returns: The window run mode passed from command line or
273  *          EV_WINDOW_MODE_NORMAL in other case.
274  */
275 static EvWindowRunMode
276 get_window_run_mode_from_args (GHashTable *args)
277 {
278         EvWindowRunMode  mode = EV_WINDOW_MODE_NORMAL;
279         GValue          *value = NULL;
280
281         g_assert (args != NULL);
282
283         value = g_hash_table_lookup (args, "mode");
284         if (value) {
285                 mode = g_value_get_uint (value);
286         }
287
288         return mode;
289 }
290
291 /**
292  * get_destination_from_args:
293  * @args: a #GHashTable with data passed to the application.
294  *
295  * It does look for the page-label argument parsed from the command line and
296  * if it does exist, it returns an #EvLinkDest.
297  *
298  * Returns: An #EvLinkDest to page-label if it has been passed from the command
299  *          line, NULL in other case.
300  */
301 static EvLinkDest *
302 get_destination_from_args (GHashTable *args)
303 {
304         EvLinkDest *dest = NULL;
305         GValue     *value = NULL;
306         
307         g_assert (args != NULL);
308         
309         value = g_hash_table_lookup (args, "page-label");
310         if (value) {
311                 const gchar *page_label;
312
313                 page_label = g_value_get_string (value);
314                 dest = ev_link_dest_new_page_label (page_label);
315         }
316
317         return dest;
318 }
319
320 static const gchar *
321 get_find_string_from_args (GHashTable *args)
322 {
323         GValue *value = NULL;
324
325         g_assert (args != NULL);
326
327         value = g_hash_table_lookup (args, "find-string");
328         
329         return value ? g_value_get_string (value) : NULL;
330 }
331
332 static void
333 value_free (GValue *value)
334 {
335         g_value_unset (value);
336         g_free (value);
337 }
338
339 static GHashTable *
340 build_args (GdkScreen      *screen,
341             EvLinkDest     *dest,
342             EvWindowRunMode mode,
343             const gchar    *search_string)
344 {
345         GHashTable  *args;
346         GValue      *value;
347         GdkDisplay  *display;
348         const gchar *display_name;
349         gint         screen_number;
350
351         args = g_hash_table_new_full (g_str_hash,
352                                       g_str_equal,
353                                       (GDestroyNotify)g_free,
354                                       (GDestroyNotify)value_free);
355
356         /* Display */
357         display = gdk_screen_get_display (screen);
358         display_name = gdk_display_get_name (display);
359         value = g_new0 (GValue, 1);
360         g_value_init (value, G_TYPE_STRING);
361         g_value_set_string (value, display_name);
362         g_hash_table_insert (args, g_strdup ("display"), value);
363
364         /* Screen */
365         screen_number = gdk_screen_get_number (screen);
366         value = g_new0 (GValue, 1);
367         g_value_init (value, G_TYPE_INT);
368         g_value_set_int (value, screen_number);
369         g_hash_table_insert (args, g_strdup ("screen"), value);
370
371         /* Page label */
372         if (dest) {
373                 value = g_new0 (GValue, 1);
374                 g_value_init (value, G_TYPE_STRING);
375                 g_value_set_string (value, ev_link_dest_get_page_label (dest));
376
377                 g_hash_table_insert (args, g_strdup ("page-label"), value);
378         }
379
380         /* Find string */
381         if (search_string) {
382                 value = g_new0 (GValue, 1);
383                 g_value_init (value, G_TYPE_STRING);
384                 g_value_set_string (value, search_string);
385
386                 g_hash_table_insert (args, g_strdup ("find-string"), value);
387         }
388
389         /* Mode */
390         if (mode != EV_WINDOW_MODE_NORMAL) {
391                 value = g_new0 (GValue, 1);
392                 g_value_init (value, G_TYPE_UINT);
393                 g_value_set_uint (value, mode);
394
395                 g_hash_table_insert (args, g_strdup ("mode"), value);
396         }
397
398         return args;
399 }
400
401 static void
402 ev_spawn (const char     *uri,
403           GdkScreen      *screen,
404           EvLinkDest     *dest,
405           EvWindowRunMode mode,
406           const gchar    *search_string)
407 {
408         gchar   *argv[6];
409         guint    arg = 0;
410         gint     i;
411         gboolean res;
412         GError  *error = NULL;
413
414         argv[arg++] = g_build_filename (BINDIR, "evince", NULL);
415
416         /* Page label */
417         if (dest) {
418                 const gchar *page_label;
419
420                 page_label = ev_link_dest_get_page_label (dest);
421                 if (page_label)
422                         argv[arg++] = g_strdup_printf ("--page-label=%s", page_label);
423                 else
424                         argv[arg++] = g_strdup_printf ("--page-label=%d",
425                                                        ev_link_dest_get_page (dest));
426         }
427
428         /* Find string */
429         if (search_string) {
430                 argv[arg++] = g_strdup_printf ("--find=%s", search_string);
431         }
432
433         /* Mode */
434         switch (mode) {
435         case EV_WINDOW_MODE_FULLSCREEN:
436                 argv[arg++] = g_strdup ("-f");
437                 break;
438         case EV_WINDOW_MODE_PRESENTATION:
439                 argv[arg++] = g_strdup ("-s");
440                 break;
441         default:
442                 break;
443         }
444
445         argv[arg++] = (gchar *)uri;
446         argv[arg] = NULL;
447
448         res = gdk_spawn_on_screen (screen, NULL /* wd */, argv, NULL /* env */,
449                                    0, NULL, NULL, NULL, &error);
450         if (!res) {
451                 g_warning ("Error launching evince %s: %s\n", uri, error->message);
452                 g_error_free (error);
453         }
454
455         for (i = 0; i < arg - 1; i++) {
456                 g_free (argv[i]);
457         }
458 }
459
460 #ifdef ENABLE_DBUS
461 static gboolean
462 ev_application_register_uri (EvApplication *application,
463                              const gchar   *uri,
464                              GHashTable    *args,
465                              guint          timestamp)
466 {
467         DBusGProxy *proxy;
468         gchar      *owner;
469         gboolean    retval = TRUE;
470         GError     *error = NULL;
471
472         if (!application->connection)
473                 return TRUE;
474
475         proxy = dbus_g_proxy_new_for_name (application->connection,
476                                            "org.gnome.evince.Daemon",
477                                            "/org/gnome/evince/Daemon",
478                                            "org.gnome.evince.Daemon");
479         if (!dbus_g_proxy_call (proxy, "RegisterDocument", &error,
480                                 G_TYPE_STRING, uri,
481                                 G_TYPE_INVALID,
482                                 G_TYPE_STRING, &owner,
483                                 G_TYPE_INVALID)) {
484                 g_warning ("Error registering document: %s\n", error->message);
485                 g_error_free (error);
486                 g_object_unref (proxy);
487
488                 return TRUE;
489         }
490         g_object_unref (proxy);
491
492         if (*owner == ':') {
493                 /* Already registered */
494                 proxy = dbus_g_proxy_new_for_name_owner (application->connection,
495                                                          owner,
496                                                          APPLICATION_DBUS_OBJECT_PATH,
497                                                          APPLICATION_DBUS_INTERFACE,
498                                                          &error);
499                 if (proxy) {
500                         if (!dbus_g_proxy_call (proxy, "OpenURI", &error,
501                                                 G_TYPE_STRING, uri,
502                                                 dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE), args,
503                                                 G_TYPE_UINT, timestamp,
504                                                 G_TYPE_INVALID,
505                                                 G_TYPE_INVALID)) {
506                                 g_warning ("%s", error->message);
507                                 g_error_free (error);
508                         }
509                         g_object_unref (proxy);
510                 } else {
511                         g_warning ("Error creating proxy: %s\n", error->message);
512                         g_error_free (error);
513                 }
514
515                 /* Do not continue opening this document */
516                 retval = FALSE;
517         }
518
519         g_free (owner);
520
521         return retval;
522 }
523
524 static void
525 ev_application_unregister_uri (EvApplication *application,
526                                const gchar   *uri)
527 {
528         DBusGProxy *proxy;
529         GError     *error = NULL;
530
531         if (!application->connection)
532                 return;
533
534         proxy = dbus_g_proxy_new_for_name (application->connection,
535                                            "org.gnome.evince.Daemon",
536                                            "/org/gnome/evince/Daemon",
537                                            "org.gnome.evince.Daemon");
538         if (!dbus_g_proxy_call (proxy, "UnregisterDocument", &error,
539                                 G_TYPE_STRING, uri,
540                                 G_TYPE_INVALID,
541                                 G_TYPE_INVALID)) {
542                 g_warning ("Error unregistering document: %s\n", error->message);
543                 g_error_free (error);
544         }
545
546         g_object_unref (proxy);
547 }
548 #endif /* ENABLE_DBUS */
549
550 static void
551 ev_application_open_uri_in_window (EvApplication  *application,
552                                    const char     *uri,
553                                    GdkScreen      *screen,
554                                    EvLinkDest     *dest,
555                                    EvWindowRunMode mode,
556                                    const gchar    *search_string,
557                                    guint           timestamp)
558 {
559         EvWindow *ev_window = application->window;
560
561         if (screen) {
562                 ev_stock_icons_set_screen (screen);
563                 gtk_window_set_screen (GTK_WINDOW (ev_window), screen);
564         }
565
566         /* We need to load uri before showing the window, so
567            we can restore window size without flickering */
568         ev_window_open_uri (ev_window, uri, dest, mode, search_string);
569
570         if (!GTK_WIDGET_REALIZED (GTK_WIDGET (ev_window)))
571                 gtk_widget_realize (GTK_WIDGET (ev_window));
572
573 #ifdef GDK_WINDOWING_X11
574         if (timestamp <= 0)
575                 timestamp = gdk_x11_get_server_time (GTK_WIDGET (ev_window)->window);
576         gdk_x11_window_set_user_time (GTK_WIDGET (ev_window)->window, timestamp);
577
578         ev_document_fc_mutex_lock ();
579         gtk_window_present (GTK_WINDOW (ev_window));
580         ev_document_fc_mutex_unlock ();
581 #else
582         ev_document_fc_mutex_lock ();
583         gtk_window_present_with_time (GTK_WINDOW (ev_window), timestamp);
584         ev_document_fc_mutex_unlock ();
585 #endif /* GDK_WINDOWING_X11 */
586 }
587
588 /**
589  * ev_application_open_uri_at_dest:
590  * @application: The instance of the application.
591  * @uri: The uri to be opened.
592  * @screen: Thee screen where the link will be shown.
593  * @dest: The #EvLinkDest of the document.
594  * @mode: The run mode of the window.
595  * @timestamp: Current time value.
596  */
597 void
598 ev_application_open_uri_at_dest (EvApplication  *application,
599                                  const char     *uri,
600                                  GdkScreen      *screen,
601                                  EvLinkDest     *dest,
602                                  EvWindowRunMode mode,
603                                  const gchar    *search_string,
604                                  guint           timestamp)
605 {
606         g_return_if_fail (uri != NULL);
607
608         if (application->window && !ev_window_is_empty (application->window)) {
609                 if (application->uri && strcmp (application->uri, uri) != 0) {
610                         /* spawn a new evince process */
611                         ev_spawn (uri, screen, dest, mode, search_string);
612                         return;
613                 }
614         } else {
615 #ifdef ENABLE_DBUS
616                 GHashTable *args = build_args (screen, dest, mode, search_string);
617                 gboolean    ret;
618
619                 /* Register the uri or send OpenURI to
620                  * remote instance if already registered
621                  */
622                 ret = ev_application_register_uri (application, uri, args, timestamp);
623                 g_hash_table_destroy (args);
624                 if (!ret)
625                         return;
626 #endif /* ENABLE_DBUS */
627
628                 if (!application->window)
629                         application->window = EV_WINDOW (ev_window_new ());
630         }
631
632         application->uri = g_strdup (uri);
633
634         ev_application_open_uri_in_window (application, uri,
635                                            screen, dest, mode,
636                                            search_string,
637                                            timestamp);
638 }
639
640 /**
641  * ev_application_open_window:
642  * @application: The instance of the application.
643  * @timestamp: Current time value.
644  *
645  * Creates a new window
646  */
647 void
648 ev_application_open_window (EvApplication *application,
649                             GdkScreen     *screen,
650                             guint32        timestamp)
651 {
652         GtkWidget *new_window = ev_window_new ();
653
654         application->window = EV_WINDOW (new_window);
655
656         if (screen) {
657                 ev_stock_icons_set_screen (screen);
658                 gtk_window_set_screen (GTK_WINDOW (new_window), screen);
659         }
660
661         if (!GTK_WIDGET_REALIZED (new_window))
662                 gtk_widget_realize (new_window);
663
664 #ifdef GDK_WINDOWING_X11
665         if (timestamp <= 0)
666                 timestamp = gdk_x11_get_server_time (new_window->window);
667         gdk_x11_window_set_user_time (new_window->window, timestamp);
668
669         gtk_window_present (GTK_WINDOW (new_window));
670 #else
671         gtk_window_present_with_time (GTK_WINDOW (new_window), timestamp);
672 #endif /* GDK_WINDOWING_X11 */
673 }
674
675 /**
676  * ev_application_open_uri:
677  * @application: The instance of the application.
678  * @uri: The uri to be opened
679  * @args: A #GHashTable with the arguments data.
680  * @timestamp: Current time value.
681  * @error: The #GError facility.
682  */
683 static gboolean
684 ev_application_open_uri (EvApplication  *application,
685                          const char     *uri,
686                          GHashTable     *args,
687                          guint           timestamp,
688                          GError        **error)
689 {
690         EvLinkDest      *dest = NULL;
691         EvWindowRunMode  mode = EV_WINDOW_MODE_NORMAL;
692         const gchar     *search_string = NULL;
693         GdkScreen       *screen = NULL;
694
695         g_assert (application->window != NULL);
696
697         /* FIXME: we don't need uri anymore,
698          * maybe this method should be renamed
699          * as reload, refresh or something like that
700          */
701         if (!application->uri || strcmp (application->uri, uri)) {
702                 g_warning ("Invalid uri: %s, expected %s\n",
703                            uri, application->uri);
704                 return TRUE;
705         }
706
707         if (args) {
708                 screen = get_screen_from_args (args);
709                 dest = get_destination_from_args (args);
710                 mode = get_window_run_mode_from_args (args);
711                 search_string = get_find_string_from_args (args);
712         }
713
714         ev_application_open_uri_in_window (application, uri,
715                                            screen, dest, mode,
716                                            search_string,
717                                            timestamp);
718
719         if (dest)
720                 g_object_unref (dest);
721
722         return TRUE;
723 }
724
725 void
726 ev_application_open_uri_list (EvApplication *application,
727                               GSList        *uri_list,
728                               GdkScreen     *screen,
729                               guint          timestamp)
730 {
731         GSList *l;
732
733         for (l = uri_list; l != NULL; l = l->next) {
734                 ev_application_open_uri_at_dest (application, (char *)l->data,
735                                                  screen, NULL, 0, NULL,
736                                                  timestamp);
737         }
738 }
739
740 void
741 ev_application_shutdown (EvApplication *application)
742 {
743         if (application->uri) {
744 #ifdef ENABLE_DBUS
745                 ev_application_unregister_uri (application,
746                                                application->uri);
747 #endif
748                 g_free (application->uri);
749                 application->uri = NULL;
750         }
751
752         if (application->accel_map_file) {
753                 gtk_accel_map_save (application->accel_map_file);
754                 g_free (application->accel_map_file);
755                 application->accel_map_file = NULL;
756         }
757         
758         if (application->toolbars_model) {
759                 g_object_unref (application->toolbars_model);
760                 g_free (application->toolbars_file);
761                 application->toolbars_model = NULL;
762                 application->toolbars_file = NULL;
763         }
764
765         ev_application_save_print_settings (application);
766         
767         if (application->print_settings_file) {
768                 g_key_file_free (application->print_settings_file);
769                 application->print_settings_file = NULL;
770         }
771
772         if (application->print_settings) {
773                 g_object_unref (application->print_settings);
774                 application->print_settings = NULL;
775         }
776
777         if (application->page_setup) {
778                 g_object_unref (application->page_setup);
779                 application->page_setup = NULL;
780         }
781
782 #ifdef ENABLE_DBUS
783         if (application->keys) {
784                 g_object_unref (application->keys);
785                 application->keys = NULL;
786         }
787 #endif /* ENABLE_DBUS */
788         
789         g_free (application->dot_dir);
790         application->dot_dir = NULL;
791         g_free (application->data_dir);
792         application->data_dir = NULL;
793         g_free (application->filechooser_open_uri);
794         application->filechooser_open_uri = NULL;
795         g_free (application->filechooser_save_uri);
796         application->filechooser_save_uri = NULL;
797
798         g_object_unref (application);
799         instance = NULL;
800         
801         gtk_main_quit ();
802 }
803
804 static void
805 ev_application_class_init (EvApplicationClass *ev_application_class)
806 {
807 #ifdef ENABLE_DBUS
808         dbus_g_object_type_install_info (EV_TYPE_APPLICATION,
809                                          &dbus_glib_ev_application_object_info);
810 #endif
811 }
812
813 static void
814 ev_application_init (EvApplication *ev_application)
815 {
816         gint i;
817         const gchar *home_dir;
818         gchar *toolbar_path;
819         GError *error = NULL;
820
821         ev_application->dot_dir = g_build_filename (g_get_home_dir (),
822                                                     ".gnome2",
823                                                     "evince",
824                                                     NULL);
825
826         /* FIXME: why make this fatal? */
827         if (!ev_dir_ensure_exists (ev_application->dot_dir, 0700))
828                 exit (1);
829
830 #ifdef G_OS_WIN32
831 {
832         gchar *dir;
833
834         dir = g_win32_get_package_installation_directory_of_module (NULL);
835         ev_application->data_dir = g_build_filename (dir, "share", "evince", NULL);
836         g_free (dir);
837 }
838 #else
839         ev_application->data_dir = g_strdup (DATADIR);
840 #endif
841
842         ev_application_init_session (ev_application);
843
844         home_dir = g_get_home_dir ();
845         if (home_dir) {
846                 ev_application->accel_map_file = g_build_filename (home_dir,
847                                                                    ".gnome2",
848                                                                    "accels",
849                                                                    "evince",
850                                                                    NULL);
851                 gtk_accel_map_load (ev_application->accel_map_file);
852         }
853         
854         ev_application->toolbars_model = egg_toolbars_model_new ();
855
856         ev_application->toolbars_file = g_build_filename
857                         (ev_application->dot_dir, "evince_toolbar.xml", NULL);
858
859         toolbar_path = g_build_filename (ev_application->data_dir,
860                                          "evince-toolbar.xml", NULL);
861         egg_toolbars_model_load_names (ev_application->toolbars_model,
862                                        toolbar_path);
863
864         if (!egg_toolbars_model_load_toolbars (ev_application->toolbars_model,
865                                                ev_application->toolbars_file)) {
866                 egg_toolbars_model_load_toolbars (ev_application->toolbars_model,
867                                                   toolbar_path);
868         }
869         g_free (toolbar_path);
870
871         /* Open item doesn't exist anymore,
872          * convert it to OpenRecent for compatibility
873          */
874         for (i = 0; i < egg_toolbars_model_n_items (ev_application->toolbars_model, 0); i++) {
875                 const gchar *item;
876                 
877                 item = egg_toolbars_model_item_nth (ev_application->toolbars_model, 0, i);
878                 if (g_ascii_strcasecmp (item, "FileOpen") == 0) {
879                         egg_toolbars_model_remove_item (ev_application->toolbars_model, 0, i);
880                         egg_toolbars_model_add_item (ev_application->toolbars_model, 0, i,
881                                                      "FileOpenRecent");
882                         ev_application_save_toolbars_model (ev_application);
883                         break;
884                 }
885         }
886
887         egg_toolbars_model_set_flags (ev_application->toolbars_model, 0,
888                                       EGG_TB_MODEL_NOT_REMOVABLE);
889
890 #ifdef ENABLE_DBUS
891         ev_application->connection = dbus_g_bus_get (DBUS_BUS_STARTER, &error);
892         if (ev_application->connection) {
893                 dbus_g_connection_register_g_object (ev_application->connection,
894                                                      APPLICATION_DBUS_OBJECT_PATH,
895                                                      G_OBJECT (ev_application));
896                 ev_application->scr_saver = totem_scrsaver_new (ev_application->connection);
897         } else {
898                 g_warning ("Error connection to DBus: %s\n", error->message);
899                 g_error_free (error);
900         }
901         ev_application->keys = ev_media_player_keys_new ();
902 #endif /* ENABLE_DBUS */
903 }
904
905 gboolean
906 ev_application_has_window (EvApplication *application)
907 {
908         return application->window != NULL;
909 }
910
911 const gchar *
912 ev_application_get_uri (EvApplication *application)
913 {
914         return application->uri;
915 }
916
917 /**
918  * ev_application_get_media_keys:
919  * @application: The instance of the application.
920  *
921  * It gives you access to the media player keys handler object.
922  *
923  * Returns: A #EvMediaPlayerKeys.
924  */
925 GObject *
926 ev_application_get_media_keys (EvApplication *application)
927 {
928 #ifdef ENABLE_DBUS
929         return G_OBJECT (application->keys);
930 #else
931         return NULL;
932 #endif /* ENABLE_DBUS */
933 }
934
935 EggToolbarsModel *
936 ev_application_get_toolbars_model (EvApplication *application)
937 {
938         return application->toolbars_model;
939 }
940
941 void
942 ev_application_save_toolbars_model (EvApplication *application)
943 {
944         egg_toolbars_model_save_toolbars (application->toolbars_model,
945                                           application->toolbars_file, "1.0");
946 }
947
948 void
949 ev_application_set_filechooser_uri (EvApplication       *application,
950                                     GtkFileChooserAction action,
951                                     const gchar         *uri)
952 {
953         if (action == GTK_FILE_CHOOSER_ACTION_OPEN) {
954                 g_free (application->filechooser_open_uri);
955                 application->filechooser_open_uri = g_strdup (uri);
956         } else if (action == GTK_FILE_CHOOSER_ACTION_SAVE) {
957                 g_free (application->filechooser_save_uri);
958                 application->filechooser_save_uri = g_strdup (uri);
959         }
960 }
961
962 const gchar *
963 ev_application_get_filechooser_uri (EvApplication       *application,
964                                     GtkFileChooserAction action)
965 {
966         if (action == GTK_FILE_CHOOSER_ACTION_OPEN) {
967                 if (application->filechooser_open_uri)
968                         return application->filechooser_open_uri;
969         } else if (action == GTK_FILE_CHOOSER_ACTION_SAVE) {
970                 if (application->filechooser_save_uri)
971                         return application->filechooser_save_uri;
972         }
973
974         return NULL;
975 }
976
977 void
978 ev_application_screensaver_enable (EvApplication *application)
979 {
980         if (application->scr_saver)
981                 totem_scrsaver_enable (application->scr_saver); 
982 }
983
984 void
985 ev_application_screensaver_disable (EvApplication *application)
986 {
987         if (application->scr_saver)
988                 totem_scrsaver_disable (application->scr_saver);        
989 }
990
991 static GKeyFile *
992 ev_application_get_print_settings_file (EvApplication *application)
993 {
994         gchar *filename;
995         
996         if (application->print_settings_file)
997                 return application->print_settings_file;
998
999         application->print_settings_file = g_key_file_new ();
1000         
1001         filename = g_build_filename (ev_application_get_dot_dir (application), EV_PRINT_SETTINGS_FILE, NULL);
1002         if (g_file_test (filename, G_FILE_TEST_IS_REGULAR)) {
1003                 GError *error = NULL;
1004
1005                 g_key_file_load_from_file (application->print_settings_file,
1006                                            filename,
1007                                            G_KEY_FILE_KEEP_COMMENTS |
1008                                            G_KEY_FILE_KEEP_TRANSLATIONS,
1009                                            &error);
1010                 if (error) {
1011                         g_warning ("%s", error->message);
1012                         g_error_free (error);
1013                 }
1014         }
1015         g_free (filename);
1016
1017         return application->print_settings_file;
1018 }
1019
1020 static void
1021 ev_application_save_print_settings (EvApplication *application)
1022 {
1023         GKeyFile *key_file;
1024         gchar    *filename;
1025         gchar    *data;
1026         gssize    data_length;
1027         GError   *error = NULL;
1028
1029         if (!application->print_settings && !application->page_setup)
1030                 return;
1031         
1032         key_file = ev_application_get_print_settings_file (application);
1033         if (application->print_settings)
1034                 gtk_print_settings_to_key_file (application->print_settings,
1035                                                 key_file,
1036                                                 EV_PRINT_SETTINGS_GROUP);
1037         if (application->page_setup)
1038                 gtk_page_setup_to_key_file (application->page_setup,
1039                                             key_file,
1040                                             EV_PAGE_SETUP_GROUP);
1041         
1042         filename = g_build_filename (ev_application_get_dot_dir (application), EV_PRINT_SETTINGS_FILE, NULL);
1043         data = g_key_file_to_data (key_file, (gsize *)&data_length, NULL);
1044         g_file_set_contents (filename, data, data_length, &error);
1045         if (error) {
1046                 g_warning ("%s", error->message);
1047                 g_error_free (error);
1048         }
1049         g_free (data);
1050         g_free (filename);
1051 }
1052
1053 GtkPrintSettings *
1054 ev_application_get_print_settings (EvApplication *application)
1055 {
1056         GKeyFile         *key_file;
1057         GtkPrintSettings *print_settings;
1058         
1059         if (application->print_settings)
1060                 return application->print_settings;
1061
1062         key_file = ev_application_get_print_settings_file (application);
1063         print_settings = g_key_file_has_group (key_file, EV_PRINT_SETTINGS_GROUP) ? 
1064                 gtk_print_settings_new_from_key_file (key_file, EV_PRINT_SETTINGS_GROUP, NULL) :
1065                 gtk_print_settings_new ();
1066
1067         application->print_settings = print_settings ? print_settings : gtk_print_settings_new ();
1068
1069         return application->print_settings;
1070 }
1071
1072 void
1073 ev_application_set_print_settings (EvApplication    *application,
1074                                    GtkPrintSettings *settings)
1075 {
1076         GKeyFile *key_file;
1077         
1078         g_return_if_fail (GTK_IS_PRINT_SETTINGS (settings));
1079         
1080         if (settings == application->print_settings)
1081                 return;
1082
1083         key_file = ev_application_get_print_settings_file (application);
1084         
1085         if (application->print_settings)
1086                 g_object_unref (application->print_settings);
1087         
1088         application->print_settings = g_object_ref (settings);
1089         gtk_print_settings_to_key_file (settings, key_file, EV_PRINT_SETTINGS_GROUP);
1090 }
1091
1092 GtkPageSetup *
1093 ev_application_get_page_setup (EvApplication *application)
1094 {
1095         GKeyFile     *key_file;
1096         GtkPageSetup *page_setup;
1097         
1098         if (application->page_setup)
1099                 return application->page_setup;
1100
1101         key_file = ev_application_get_print_settings_file (application);
1102         page_setup = g_key_file_has_group (key_file, EV_PAGE_SETUP_GROUP) ? 
1103                 gtk_page_setup_new_from_key_file (key_file, EV_PAGE_SETUP_GROUP, NULL) :
1104                 gtk_page_setup_new ();
1105
1106         application->page_setup = page_setup ? page_setup : gtk_page_setup_new ();
1107
1108         return application->page_setup;
1109 }
1110
1111 void
1112 ev_application_set_page_setup (EvApplication *application,
1113                                GtkPageSetup  *page_setup)
1114 {
1115         GKeyFile *key_file;
1116         
1117         g_return_if_fail (GTK_IS_PAGE_SETUP (page_setup));
1118         
1119         if (page_setup == application->page_setup)
1120                 return;
1121
1122         key_file = ev_application_get_print_settings_file (application);
1123         
1124         if (application->page_setup)
1125                 g_object_unref (application->page_setup);
1126         
1127         application->page_setup = g_object_ref (page_setup);
1128         gtk_page_setup_to_key_file (page_setup, key_file, EV_PAGE_SETUP_GROUP);
1129 }
1130
1131 const gchar *
1132 ev_application_get_dot_dir (EvApplication   *application)
1133 {
1134         return application->dot_dir;
1135 }
1136
1137 const gchar *
1138 ev_application_get_data_dir (EvApplication   *application)
1139 {
1140         return application->data_dir;
1141 }