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