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