]> www.fi.muni.cz Git - evince.git/blob - shell/ev-application.c
Make fullscreen toolbar always visible. Do not use egg toolbars for
[evince.git] / shell / ev-application.c
1 /* this file is part of evince, a gnome document viewer
2  *
3  *  Copyright (C) 2004 Martin Kretzschmar
4  *
5  *  Author:
6  *    Martin Kretzschmar <martink@gnome.org>
7  *
8  * Evince is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * Evince is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
21  */
22
23 #include "ev-application.h"
24 #include "ev-utils.h"
25 #include "ev-file-helpers.h"
26 #include "ev-document-factory.h"
27 #include "totem-scrsaver.h"
28
29 #include <glib.h>
30 #include <glib/gi18n.h>
31 #include <glib-object.h>
32 #include <gtk/gtkfilechooserdialog.h>
33 #include <gtk/gtkstock.h>
34 #include <gtk/gtkwidget.h>
35 #include <gtk/gtkmain.h>
36 #include <libgnomeui/gnome-client.h>
37 #include <string.h>
38
39 #ifdef ENABLE_DBUS
40 #include "ev-application-service.h"
41 #include <dbus/dbus-glib-bindings.h>
42 #endif
43
44 struct _EvApplication {
45         GObject base_instance;
46
47         gchar *toolbars_file;
48
49         EggToolbarsModel *toolbars_model;
50
51 #ifndef HAVE_GTK_RECENT
52         EggRecentModel  *recent_model;
53 #endif
54
55         TotemScrsaver *scr_saver;
56
57         gchar *last_chooser_uri;
58 };
59
60 struct _EvApplicationClass {
61         GObjectClass base_class;
62 };
63
64 G_DEFINE_TYPE (EvApplication, ev_application, G_TYPE_OBJECT);
65
66 #define APPLICATION_SERVICE_NAME "org.gnome.evince.ApplicationService"
67
68 #ifdef ENABLE_DBUS
69 gboolean
70 ev_application_register_service (EvApplication *application)
71 {
72         static DBusGConnection *connection = NULL;
73         DBusGProxy *driver_proxy;
74         GError *err = NULL;
75         guint request_name_result;
76
77         if (connection) {
78                 g_warning ("Service already registered.");
79                 return FALSE;
80         }
81         
82         connection = dbus_g_bus_get (DBUS_BUS_STARTER, &err);
83         if (connection == NULL) {
84                 g_warning ("Service registration failed.");
85                 g_error_free (err);
86
87                 return FALSE;
88         }
89
90         driver_proxy = dbus_g_proxy_new_for_name (connection,
91                                                   DBUS_SERVICE_DBUS,
92                                                   DBUS_PATH_DBUS,
93                                                   DBUS_INTERFACE_DBUS);
94
95         if (!org_freedesktop_DBus_request_name (driver_proxy,
96                                                 APPLICATION_SERVICE_NAME,
97                                                 DBUS_NAME_FLAG_DO_NOT_QUEUE,
98                                                 &request_name_result, &err)) {
99                 g_warning ("Service registration failed.");
100                 g_clear_error (&err);
101         }
102
103         g_object_unref (driver_proxy);
104         
105         if (request_name_result == DBUS_REQUEST_NAME_REPLY_EXISTS) {
106                 return FALSE;
107         }
108
109         dbus_g_object_type_install_info (EV_TYPE_APPLICATION,
110                                          &dbus_glib_ev_application_object_info);
111         dbus_g_connection_register_g_object (connection,
112                                              "/org/gnome/evince/Evince",
113                                              G_OBJECT (application));
114         
115         application->scr_saver = totem_scrsaver_new (connection);
116         
117         return TRUE;
118 }
119 #endif /* ENABLE_DBUS */
120
121 /**
122  * ev_application_get_instance:
123  *
124  * Checks for #EvApplication instance, if it doesn't exist it does create it.
125  *
126  * Returns: an instance of the #EvApplication data.
127  */
128 EvApplication *
129 ev_application_get_instance (void)
130 {
131         static EvApplication *instance;
132
133         if (!instance) {
134                 instance = EV_APPLICATION (g_object_new (EV_TYPE_APPLICATION, NULL));
135         }
136
137         return instance;
138 }
139
140 static void
141 removed_from_session (GnomeClient *client, EvApplication *application)
142 {
143         ev_application_shutdown (application);
144 }
145
146 static gint
147 save_session (GnomeClient *client, gint phase, GnomeSaveStyle save_style, gint shutdown,
148               GnomeInteractStyle interact_style, gint fast, EvApplication *application)
149 {
150         GList *windows, *l;
151         char **restart_argv;
152         int argc = 0, k;
153
154         windows = ev_application_get_windows (application);
155         restart_argv = g_new (char *, g_list_length (windows) + 1);
156         restart_argv[argc++] = g_strdup ("evince");
157
158         for (l = windows; l != NULL; l = l->next) {
159                 EvWindow *window = EV_WINDOW (l->data);
160                 restart_argv[argc++] = g_strdup (ev_window_get_uri (window));
161         }
162
163         gnome_client_set_restart_command (client, argc, restart_argv);
164
165         for (k = 0; k < argc; k++) {
166                 g_free (restart_argv[k]);
167         }
168
169         g_list_free (windows);
170         g_free (restart_argv);
171         
172         return TRUE;
173 }
174
175 static void
176 init_session (EvApplication *application)
177 {
178         GnomeClient *client;
179
180         client = gnome_master_client ();
181
182         g_signal_connect (client, "save_yourself",
183                           G_CALLBACK (save_session), application);      
184         g_signal_connect (client, "die",
185                           G_CALLBACK (removed_from_session), application);
186 }
187
188 /**
189  * ev_display_open_if_needed:
190  * @name: the name of the display to be open if it's needed.
191  *
192  * Search among all the open displays if any of them have the same name as the
193  * passed name. If the display isn't found it tries the open it.
194  *
195  * Returns: a #GdkDisplay of the display with the passed name.
196  */
197 static GdkDisplay *
198 ev_display_open_if_needed (const gchar *name)
199 {
200         GSList     *displays;
201         GSList     *l;
202         GdkDisplay *display = NULL;
203
204         displays = gdk_display_manager_list_displays (gdk_display_manager_get ());
205
206         for (l = displays; l != NULL; l = l->next) {
207                 const gchar *display_name = gdk_display_get_name ((GdkDisplay *) l->data);
208
209                 if (g_ascii_strcasecmp (display_name, name) == 0) {
210                         display = l->data;
211                         break;
212                 }
213         }
214
215         g_slist_free (displays);
216
217         return display != NULL ? display : gdk_display_open (name);
218 }
219
220 /**
221  * get_screen_from_args:
222  * @args: a #GHashTable with data passed to the application.
223  *
224  * Looks for the screen in the display available in the hash table passed to the
225  * application. If the display isn't opened, it's opened and the #GdkScreen
226  * assigned to the screen in that display returned.
227  *
228  * Returns: the #GdkScreen assigned to the screen on the display indicated by
229  *          the data on the #GHashTable.
230  */
231 static GdkScreen *
232 get_screen_from_args (GHashTable *args)
233 {
234         GValue     *value = NULL;
235         GdkDisplay *display = NULL;
236         GdkScreen  *screen = NULL;
237
238         g_assert (args != NULL);
239         
240         value = g_hash_table_lookup (args, "display");
241         if (value) {
242                 const gchar *display_name;
243                 
244                 display_name = g_value_get_string (value);
245                 display = ev_display_open_if_needed (display_name);
246         }
247         
248         value = g_hash_table_lookup (args, "screen");
249         if (value) {
250                 gint screen_number;
251                 
252                 screen_number = g_value_get_int (value);
253                 screen = gdk_display_get_screen (display, screen_number);
254         }
255
256         return screen;
257 }
258
259 /**
260  * get_window_run_mode_from_args:
261  * @args: a #GHashTable with data passed to the application.
262  *
263  * It does look if the mode option has been passed from command line, using it
264  * as the window run mode, otherwise the run mode will be the normal mode.
265  *
266  * Returns: The window run mode passed from command line or
267  *          EV_WINDOW_MODE_NORMAL in other case.
268  */
269 static EvWindowRunMode
270 get_window_run_mode_from_args (GHashTable *args)
271 {
272         EvWindowRunMode  mode = EV_WINDOW_MODE_NORMAL;
273         GValue          *value = NULL;
274
275         g_assert (args != NULL);
276
277         value = g_hash_table_lookup (args, "mode");
278         if (value) {
279                 mode = g_value_get_uint (value);
280         }
281
282         return mode;
283 }
284
285 /**
286  * get_destination_from_args:
287  * @args: a #GHashTable with data passed to the application.
288  *
289  * It does look for the page-label argument parsed from the command line and
290  * if it does exist, it returns an #EvLinkDest.
291  *
292  * Returns: An #EvLinkDest to page-label if it has been passed from the command
293  *          line, NULL in other case.
294  */
295 static EvLinkDest *
296 get_destination_from_args (GHashTable *args)
297 {
298         EvLinkDest *dest = NULL;
299         GValue     *value = NULL;
300         
301         g_assert (args != NULL);
302         
303         value = g_hash_table_lookup (args, "page-label");
304         if (value) {
305                 const gchar *page_label;
306
307                 page_label = g_value_get_string (value);
308                 dest = ev_link_dest_new_page_label (page_label);
309         }
310
311         return dest;
312 }
313
314 /**
315  * get_unlink_temp_file_from_args:
316  * @args: a #GHashTable with data passed to the application.
317  *
318  * It does look if the unlink-temp-file option has been passed from the command
319  * line returning it's boolean representation, otherwise it does return %FALSE.
320  *
321  * Returns: the boolean representation of the unlink-temp-file value or %FALSE
322  *          in other case.
323  */
324 static gboolean
325 get_unlink_temp_file_from_args (GHashTable *args)
326 {
327         gboolean unlink_temp_file = FALSE;
328         GValue  *value = NULL;
329
330         g_assert (args != NULL);
331
332         value = g_hash_table_lookup (args, "unlink-temp-file");
333         if (value) {
334                 unlink_temp_file = g_value_get_boolean (value);
335         }
336         
337         return unlink_temp_file;
338 }
339
340 /**
341  * ev_application_open_window:
342  * @application: The instance of the application.
343  * @args: A #GHashTable with the arguments data.
344  * @timestamp: Current time value.
345  * @error: The #GError facility.
346  * 
347  * Creates a new window and if the args are available, it's not NULL, it gets
348  * the screen from them and assigns the just created window to it. At last it
349  * does show it.
350  *
351  * Returns: %TRUE.
352  */
353 gboolean
354 ev_application_open_window (EvApplication  *application,
355                             GHashTable     *args,
356                             guint32         timestamp,
357                             GError        **error)
358 {
359         GtkWidget *new_window = ev_window_new ();
360         GdkScreen *screen = NULL;
361
362         if (args) {
363                 screen = get_screen_from_args (args);
364         }
365         
366         if (screen) {
367                 gtk_window_set_screen (GTK_WINDOW (new_window), screen);
368         }
369         
370         gtk_widget_show (new_window);
371         
372         gtk_window_present_with_time (GTK_WINDOW (new_window),
373                                       timestamp);
374         return TRUE;
375 }
376
377 /**
378  * ev_application_get_empty_window:
379  * @application: The instance of the application.
380  * @screen: The screen where the empty window will be search.
381  *
382  * It does look if there is any empty window in the indicated screen.
383  *
384  * Returns: The first empty #EvWindow in the passed #GdkScreen or NULL in other
385  *          case.
386  */
387 static EvWindow *
388 ev_application_get_empty_window (EvApplication *application,
389                                  GdkScreen     *screen)
390 {
391         EvWindow *empty_window = NULL;
392         GList *windows = ev_application_get_windows (application);
393         GList *l;
394
395         for (l = windows; l != NULL; l = l->next) {
396                 EvWindow *window = EV_WINDOW (l->data);
397
398                 if (ev_window_is_empty (window) &&
399                     gtk_window_get_screen (GTK_WINDOW (window)) == screen) {
400                         empty_window = window;
401                         break;
402                 }
403         }
404
405         g_list_free (windows);
406         
407         return empty_window;
408 }
409
410 /**
411  * ev_application_get_uri_window:
412  * @application: The instance of the application.
413  * @uri: The uri to be opened.
414  *
415  * It looks in the list of the windows for the one with the document represented
416  * by the passed uri on it. If the window is empty or the document isn't present
417  * on any window, it will return NULL.
418  *
419  * Returns: The #EvWindow where the document represented by the passed uri is
420  *          shown, NULL in other case.
421  */
422 static EvWindow *
423 ev_application_get_uri_window (EvApplication *application, const char *uri)
424 {
425         EvWindow *uri_window = NULL;
426         GList *windows = gtk_window_list_toplevels ();
427         GList *l;
428
429         g_return_val_if_fail (uri != NULL, NULL);
430
431         for (l = windows; l != NULL; l = l->next) {
432                 if (EV_IS_WINDOW (l->data)) {
433                         EvWindow *window = EV_WINDOW (l->data);
434                         const char *window_uri = ev_window_get_uri (window);
435
436                         if (window_uri && strcmp (window_uri, uri) == 0 && !ev_window_is_empty (window)) {
437                                 uri_window = window;
438                                 break;
439                         }
440                 }
441         }
442
443         g_list_free (windows);
444         
445         return uri_window;
446 }
447
448 /**
449  * ev_application_open_uri_at_dest:
450  * @application: The instance of the application.
451  * @uri: The uri to be opened.
452  * @screen: Thee screen where the link will be shown.
453  * @dest: The #EvLinkDest of the document.
454  * @mode: The run mode of the window.
455  * @unlink_temp_file: The unlink_temp_file option value.
456  * @timestamp: Current time value.
457  */
458 void
459 ev_application_open_uri_at_dest (EvApplication  *application,
460                                  const char     *uri,
461                                  GdkScreen      *screen,
462                                  EvLinkDest     *dest,
463                                  EvWindowRunMode mode,
464                                  gboolean        unlink_temp_file,
465                                  guint           timestamp)
466 {
467         EvWindow     *new_window;
468         GtkIconTheme *icon_theme;
469
470         g_return_if_fail (uri != NULL);
471
472         icon_theme = gtk_icon_theme_get_for_screen (screen);
473         if (icon_theme) {
474                 gchar **path = NULL;
475                 gint    n_paths;
476                 gint    i;
477                 gchar  *ev_icons_path;
478
479                 /* GtkIconTheme will then look in Evince custom hicolor dir
480                  * for icons as well as the standard search paths
481                  */
482                 ev_icons_path = g_build_filename (DATADIR, "icons", NULL);
483                 gtk_icon_theme_get_search_path (icon_theme, &path, &n_paths);
484                 for (i = n_paths - 1; i >= 0; i--) {
485                         if (g_ascii_strcasecmp (ev_icons_path, path[i]) == 0)
486                                 break;
487                 }
488
489                 if (i < 0)
490                         gtk_icon_theme_append_search_path (icon_theme,
491                                                            ev_icons_path);
492
493                 g_free (ev_icons_path);
494                 g_strfreev (path);
495         }       
496
497         new_window = ev_application_get_uri_window (application, uri);
498         
499         if (new_window == NULL) {
500                 new_window = ev_application_get_empty_window (application, screen);
501         }
502
503         if (new_window == NULL) {
504                 new_window = EV_WINDOW (ev_window_new ());
505         }
506
507         if (screen)
508                 gtk_window_set_screen (GTK_WINDOW (new_window), screen);
509
510         /* We need to load uri before showing the window, so
511            we can restore window size without flickering */     
512         ev_window_open_uri (new_window, uri, dest, mode, unlink_temp_file);
513
514         ev_document_fc_mutex_lock ();
515         gtk_widget_show (GTK_WIDGET (new_window));
516         ev_document_fc_mutex_unlock ();
517
518         gtk_window_present_with_time (GTK_WINDOW (new_window),
519                                       timestamp);
520 }
521
522 /**
523  * ev_application_open_uri:
524  * @application: The instance of the application.
525  * @uri: The uri to be opened
526  * @args: A #GHashTable with the arguments data.
527  * @timestamp: Current time value.
528  * @error: The #GError facility.
529  */
530 gboolean
531 ev_application_open_uri (EvApplication  *application,
532                          const char     *uri,
533                          GHashTable     *args,
534                          guint           timestamp,
535                          GError        **error)
536 {
537         EvLinkDest      *dest = NULL;
538         EvWindowRunMode  mode = EV_WINDOW_MODE_NORMAL;
539         gboolean         unlink_temp_file = FALSE;
540         GdkScreen       *screen = NULL;
541
542         if (args) {
543                 screen = get_screen_from_args (args);
544                 dest = get_destination_from_args (args);
545                 mode = get_window_run_mode_from_args (args);
546                 unlink_temp_file = (mode == EV_WINDOW_MODE_PREVIEW &&
547                                     get_unlink_temp_file_from_args (args));
548         }
549         
550         ev_application_open_uri_at_dest (application, uri, screen,
551                                          dest, mode, unlink_temp_file, 
552                                          timestamp);
553
554         if (dest)
555                 g_object_unref (dest);
556
557         return TRUE;
558 }
559
560 void
561 ev_application_open_uri_list (EvApplication *application,
562                               GSList        *uri_list,
563                               GdkScreen     *screen,
564                               guint          timestamp)
565 {
566         GSList *l;
567
568         for (l = uri_list; l != NULL; l = l->next) {
569                 ev_application_open_uri_at_dest (application, (char *)l->data,
570                                                  screen, NULL, 0, FALSE, 
571                                                  timestamp);
572         }
573 }
574
575 void
576 ev_application_shutdown (EvApplication *application)
577 {
578         if (application->toolbars_model) {
579                 g_object_unref (application->toolbars_model);
580                 g_free (application->toolbars_file);
581                 application->toolbars_model = NULL;
582                 application->toolbars_file = NULL;
583         }
584
585 #ifndef HAVE_GTK_RECENT
586         if (application->recent_model) {
587                 g_object_unref (application->recent_model);
588                 application->recent_model = NULL;
589         }
590 #endif
591         
592         g_free (application->last_chooser_uri);
593         g_object_unref (application);
594         
595         gtk_main_quit ();
596 }
597
598 static void
599 ev_application_class_init (EvApplicationClass *ev_application_class)
600 {
601 }
602
603 static void
604 ev_application_init (EvApplication *ev_application)
605 {
606         init_session (ev_application);
607
608         ev_application->toolbars_model = egg_toolbars_model_new ();
609
610         ev_application->toolbars_file = g_build_filename
611                         (ev_dot_dir (), "evince_toolbar.xml", NULL);
612
613         egg_toolbars_model_load_names (ev_application->toolbars_model,
614                                        DATADIR "/evince-toolbar.xml");
615
616         if (!egg_toolbars_model_load_toolbars (ev_application->toolbars_model,
617                                                ev_application->toolbars_file)) {
618                 egg_toolbars_model_load_toolbars (ev_application->toolbars_model,
619                                                   DATADIR"/evince-toolbar.xml");
620         }
621
622         egg_toolbars_model_set_flags (ev_application->toolbars_model, 0,
623                                       EGG_TB_MODEL_NOT_REMOVABLE); 
624
625 #ifndef HAVE_GTK_RECENT
626         ev_application->recent_model = egg_recent_model_new (EGG_RECENT_MODEL_SORT_MRU);
627         /* FIXME we should add a mime type filter but current eggrecent
628            has only a varargs style api which does not work well when
629            the list of mime types is dynamic */
630         egg_recent_model_set_limit (ev_application->recent_model, 5);   
631         egg_recent_model_set_filter_groups (ev_application->recent_model,
632                                             "Evince", NULL);
633 #endif /* HAVE_GTK_RECENT */
634 }
635
636 /**
637  * ev_application_get_windows:
638  * @application: The instance of the application.
639  *
640  * It creates a list of the top level windows.
641  *
642  * Returns: A #GList of the top level windows.
643  */
644 GList *
645 ev_application_get_windows (EvApplication *application)
646 {
647         GList *l, *toplevels;
648         GList *windows = NULL;
649
650         toplevels = gtk_window_list_toplevels ();
651
652         for (l = toplevels; l != NULL; l = l->next) {
653                 if (EV_IS_WINDOW (l->data)) {
654                         windows = g_list_append (windows, l->data);
655                 }
656         }
657
658         g_list_free (toplevels);
659
660         return windows;
661 }
662
663 EggToolbarsModel *
664 ev_application_get_toolbars_model (EvApplication *application)
665 {
666         return application->toolbars_model;
667 }
668
669 #ifndef HAVE_GTK_RECENT
670 EggRecentModel *
671 ev_application_get_recent_model (EvApplication *application)
672 {
673         return application->recent_model;
674 }
675 #endif
676
677 void
678 ev_application_save_toolbars_model (EvApplication *application)
679 {
680         egg_toolbars_model_save_toolbars (application->toolbars_model,
681                                           application->toolbars_file, "1.0");
682 }
683
684 void
685 ev_application_set_chooser_uri (EvApplication *application, const gchar *uri)
686 {
687         g_free (application->last_chooser_uri);
688         application->last_chooser_uri = g_strdup (uri);
689 }
690
691 const gchar *
692 ev_application_get_chooser_uri (EvApplication *application)
693 {
694         return application->last_chooser_uri;
695 }
696
697 void
698 ev_application_screensaver_enable  (EvApplication *application)
699 {
700         if (application->scr_saver)
701                 totem_scrsaver_enable (application->scr_saver); 
702 }
703
704 void
705 ev_application_screensaver_disable (EvApplication *application)
706 {
707         if (application->scr_saver)
708                 totem_scrsaver_disable (application->scr_saver);        
709 }