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