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