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