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