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