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