]> www.fi.muni.cz Git - evince.git/blob - shell/ev-application.c
[shell] Remove fontconfig lock/unlock when showing the main window
[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         gtk_window_present (GTK_WINDOW (ev_window));
639 #else
640         gtk_window_present_with_time (GTK_WINDOW (ev_window), timestamp);
641 #endif /* GDK_WINDOWING_X11 */
642 }
643
644 /**
645  * ev_application_open_uri_at_dest:
646  * @application: The instance of the application.
647  * @uri: The uri to be opened.
648  * @screen: Thee screen where the link will be shown.
649  * @dest: The #EvLinkDest of the document.
650  * @mode: The run mode of the window.
651  * @timestamp: Current time value.
652  */
653 void
654 ev_application_open_uri_at_dest (EvApplication  *application,
655                                  const char     *uri,
656                                  GdkScreen      *screen,
657                                  EvLinkDest     *dest,
658                                  EvWindowRunMode mode,
659                                  const gchar    *search_string,
660                                  guint           timestamp)
661 {
662         EvWindow *ev_window;
663
664         g_return_if_fail (uri != NULL);
665
666         if (application->uri && strcmp (application->uri, uri) != 0) {
667                 /* spawn a new evince process */
668                 ev_spawn (uri, screen, dest, mode, search_string, timestamp);
669                 return;
670         } else {
671 #ifdef ENABLE_DBUS
672                 GHashTable *args = build_args (screen, dest, mode, search_string);
673                 gboolean    ret;
674
675                 /* Register the uri or send OpenURI to
676                  * remote instance if already registered
677                  */
678                 ret = ev_application_register_uri (application, uri, args, timestamp);
679                 g_hash_table_destroy (args);
680                 if (!ret)
681                         return;
682 #endif /* ENABLE_DBUS */
683
684                 ev_window = ev_application_get_empty_window (application, screen);
685                 if (!ev_window)
686                         ev_window = EV_WINDOW (ev_window_new ());
687         }
688
689         application->uri = g_strdup (uri);
690
691         ev_application_open_uri_in_window (application, uri, ev_window,
692                                            screen, dest, mode,
693                                            search_string,
694                                            timestamp);
695 }
696
697 /**
698  * ev_application_open_window:
699  * @application: The instance of the application.
700  * @timestamp: Current time value.
701  *
702  * Creates a new window
703  */
704 void
705 ev_application_open_window (EvApplication *application,
706                             GdkScreen     *screen,
707                             guint32        timestamp)
708 {
709         GtkWidget *new_window = ev_window_new ();
710 #ifdef GDK_WINDOWING_X11
711         GdkWindow *gdk_window;
712 #endif
713
714         if (screen) {
715                 ev_stock_icons_set_screen (screen);
716                 gtk_window_set_screen (GTK_WINDOW (new_window), screen);
717         }
718
719         if (!gtk_widget_get_realized (new_window))
720                 gtk_widget_realize (new_window);
721
722 #ifdef GDK_WINDOWING_X11
723         gdk_window = gtk_widget_get_window (GTK_WIDGET (new_window));
724
725         if (timestamp <= 0)
726                 timestamp = gdk_x11_get_server_time (gdk_window);
727         gdk_x11_window_set_user_time (gdk_window, timestamp);
728
729         gtk_window_present (GTK_WINDOW (new_window));
730 #else
731         gtk_window_present_with_time (GTK_WINDOW (new_window), timestamp);
732 #endif /* GDK_WINDOWING_X11 */
733 }
734
735 /**
736  * ev_application_open_uri:
737  * @application: The instance of the application.
738  * @uri: The uri to be opened
739  * @args: A #GHashTable with the arguments data.
740  * @timestamp: Current time value.
741  * @error: The #GError facility.
742  */
743 static gboolean
744 ev_application_open_uri (EvApplication  *application,
745                          const char     *uri,
746                          GHashTable     *args,
747                          guint           timestamp,
748                          GError        **error)
749 {
750         GList           *windows, *l;
751         EvLinkDest      *dest = NULL;
752         EvWindowRunMode  mode = EV_WINDOW_MODE_NORMAL;
753         const gchar     *search_string = NULL;
754         GdkScreen       *screen = NULL;
755
756         g_assert (application->uri != NULL);
757
758         /* FIXME: we don't need uri anymore,
759          * maybe this method should be renamed
760          * as reload, refresh or something like that
761          */
762         if (!application->uri || strcmp (application->uri, uri)) {
763                 g_warning ("Invalid uri: %s, expected %s\n",
764                            uri, application->uri);
765                 return TRUE;
766         }
767
768         if (args) {
769                 screen = get_screen_from_args (args);
770                 dest = get_destination_from_args (args);
771                 mode = get_window_run_mode_from_args (args);
772                 search_string = get_find_string_from_args (args);
773         }
774
775         windows = ev_application_get_windows (application);
776         for (l = windows; l != NULL; l = g_list_next (l)) {
777                 EvWindow *ev_window = EV_WINDOW (l->data);
778
779                 ev_application_open_uri_in_window (application, uri, ev_window,
780                                                    screen, dest, mode,
781                                                    search_string,
782                                                    timestamp);
783         }
784         g_list_free (windows);
785
786         if (dest)
787                 g_object_unref (dest);
788
789         return TRUE;
790 }
791
792 void
793 ev_application_open_uri_list (EvApplication *application,
794                               GSList        *uri_list,
795                               GdkScreen     *screen,
796                               guint          timestamp)
797 {
798         GSList *l;
799
800         for (l = uri_list; l != NULL; l = l->next) {
801                 ev_application_open_uri_at_dest (application, (char *)l->data,
802                                                  screen, NULL, 0, NULL,
803                                                  timestamp);
804         }
805 }
806
807 static void
808 ev_application_accel_map_save (EvApplication *application)
809 {
810         gchar *accel_map_file;
811         gchar *tmp_filename;
812         gint   fd;
813
814         if (userdir) {
815                 accel_map_file = g_build_filename (userdir, "accels",
816                                                    "evince", NULL);
817         } else {
818                 accel_map_file = g_build_filename (g_get_home_dir (),
819                                                    ".gnome2", "accels",
820                                                    "evince", NULL);
821         }
822
823         tmp_filename = g_strdup_printf ("%s.XXXXXX", accel_map_file);
824
825         fd = g_mkstemp (tmp_filename);
826         if (fd == -1) {
827                 g_free (accel_map_file);
828                 g_free (tmp_filename);
829
830                 return;
831         }
832         gtk_accel_map_save_fd (fd);
833         close (fd);
834
835         if (g_rename (tmp_filename, accel_map_file) == -1) {
836                 /* FIXME: win32? */
837                 g_unlink (tmp_filename);
838         }
839
840         g_free (accel_map_file);
841         g_free (tmp_filename);
842 }
843
844 static void
845 ev_application_accel_map_load (EvApplication *application)
846 {
847         gchar *accel_map_file;
848
849         if (userdir) {
850                 accel_map_file = g_build_filename (userdir, "accels",
851                                                    "evince", NULL);
852         } else {
853                 accel_map_file = g_build_filename (g_get_home_dir (),
854                                                    ".gnome2", "accels",
855                                                    "evince", NULL);
856         }
857
858         gtk_accel_map_load (accel_map_file);
859         g_free (accel_map_file);
860 }
861
862 void
863 ev_application_shutdown (EvApplication *application)
864 {
865         if (application->uri) {
866 #ifdef ENABLE_DBUS
867                 ev_application_unregister_uri (application,
868                                                application->uri);
869 #endif
870                 g_free (application->uri);
871                 application->uri = NULL;
872         }
873
874         ev_application_accel_map_save (application);
875
876         g_object_unref (application->scr_saver);
877         application->scr_saver = NULL;
878
879 #ifdef ENABLE_DBUS
880         if (application->keys) {
881                 g_object_unref (application->keys);
882                 application->keys = NULL;
883         }
884 #endif /* ENABLE_DBUS */
885         
886         g_free (application->dot_dir);
887         application->dot_dir = NULL;
888         g_free (application->data_dir);
889         application->data_dir = NULL;
890         g_free (application->filechooser_open_uri);
891         application->filechooser_open_uri = NULL;
892         g_free (application->filechooser_save_uri);
893         application->filechooser_save_uri = NULL;
894
895         g_object_unref (application);
896         instance = NULL;
897         
898         gtk_main_quit ();
899 }
900
901 static void
902 ev_application_class_init (EvApplicationClass *ev_application_class)
903 {
904 #ifdef ENABLE_DBUS
905         dbus_g_object_type_install_info (EV_TYPE_APPLICATION,
906                                          &dbus_glib_ev_application_object_info);
907 #endif
908 }
909
910 static void
911 ev_application_init (EvApplication *ev_application)
912 {
913         GError *error = NULL;
914
915         userdir = g_getenv ("GNOME22_USER_DIR");
916         if (userdir)
917                 ev_application->dot_dir = g_build_filename (userdir, "evince", NULL);
918         else
919                 ev_application->dot_dir = g_build_filename (g_get_home_dir (),
920                                                             ".gnome2",
921                                                             "evince",
922                                                             NULL);
923
924 #ifdef G_OS_WIN32
925 {
926         gchar *dir;
927
928         dir = g_win32_get_package_installation_directory_of_module (NULL);
929         ev_application->data_dir = g_build_filename (dir, "share", "evince", NULL);
930         g_free (dir);
931 }
932 #else
933         ev_application->data_dir = g_strdup (DATADIR);
934 #endif
935
936         ev_application_init_session (ev_application);
937
938         ev_application_accel_map_load (ev_application);
939
940         ev_application->scr_saver = totem_scrsaver_new ();
941
942 #ifdef ENABLE_DBUS
943         ev_application->connection = dbus_g_bus_get (DBUS_BUS_STARTER, &error);
944         if (ev_application->connection) {
945                 dbus_g_connection_register_g_object (ev_application->connection,
946                                                      APPLICATION_DBUS_OBJECT_PATH,
947                                                      G_OBJECT (ev_application));
948         } else {
949                 g_warning ("Error connection to DBus: %s\n", error->message);
950                 g_error_free (error);
951         }
952         ev_application->keys = ev_media_player_keys_new ();
953 #endif /* ENABLE_DBUS */
954 }
955
956 gboolean
957 ev_application_has_window (EvApplication *application)
958 {
959         GList   *windows = ev_application_get_windows (application);
960         gboolean retval = windows != NULL;
961
962         g_list_free (windows);
963
964         return retval;
965 }
966
967 const gchar *
968 ev_application_get_uri (EvApplication *application)
969 {
970         return application->uri;
971 }
972
973 /**
974  * ev_application_get_media_keys:
975  * @application: The instance of the application.
976  *
977  * It gives you access to the media player keys handler object.
978  *
979  * Returns: A #EvMediaPlayerKeys.
980  */
981 GObject *
982 ev_application_get_media_keys (EvApplication *application)
983 {
984 #ifdef ENABLE_DBUS
985         return G_OBJECT (application->keys);
986 #else
987         return NULL;
988 #endif /* ENABLE_DBUS */
989 }
990
991 void
992 ev_application_set_filechooser_uri (EvApplication       *application,
993                                     GtkFileChooserAction action,
994                                     const gchar         *uri)
995 {
996         if (action == GTK_FILE_CHOOSER_ACTION_OPEN) {
997                 g_free (application->filechooser_open_uri);
998                 application->filechooser_open_uri = g_strdup (uri);
999         } else if (action == GTK_FILE_CHOOSER_ACTION_SAVE) {
1000                 g_free (application->filechooser_save_uri);
1001                 application->filechooser_save_uri = g_strdup (uri);
1002         }
1003 }
1004
1005 const gchar *
1006 ev_application_get_filechooser_uri (EvApplication       *application,
1007                                     GtkFileChooserAction action)
1008 {
1009         if (action == GTK_FILE_CHOOSER_ACTION_OPEN) {
1010                 if (application->filechooser_open_uri)
1011                         return application->filechooser_open_uri;
1012         } else if (action == GTK_FILE_CHOOSER_ACTION_SAVE) {
1013                 if (application->filechooser_save_uri)
1014                         return application->filechooser_save_uri;
1015         }
1016
1017         return NULL;
1018 }
1019
1020 void
1021 ev_application_screensaver_enable (EvApplication *application)
1022 {
1023         totem_scrsaver_enable (application->scr_saver);
1024 }
1025
1026 void
1027 ev_application_screensaver_disable (EvApplication *application)
1028 {
1029         totem_scrsaver_disable (application->scr_saver);
1030 }
1031
1032 const gchar *
1033 ev_application_get_dot_dir (EvApplication *application,
1034                             gboolean create)
1035 {
1036         if (create)
1037                 g_mkdir_with_parents (application->dot_dir, 0700);
1038
1039         return application->dot_dir;
1040 }
1041
1042 const gchar *
1043 ev_application_get_data_dir (EvApplication   *application)
1044 {
1045         return application->data_dir;
1046 }