]> www.fi.muni.cz Git - evince.git/blob - shell/ev-application.c
Use icon theme associated with the window screen rather than default.
[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 EvApplication *
105 ev_application_get_instance (void)
106 {
107         static EvApplication *instance;
108
109         if (!instance) {
110                 instance = EV_APPLICATION (g_object_new (EV_TYPE_APPLICATION, NULL));
111         }
112
113         return instance;
114 }
115
116 static void
117 removed_from_session (GnomeClient *client, EvApplication *application)
118 {
119         ev_application_shutdown (application);
120 }
121
122 static gint
123 save_session (GnomeClient *client, gint phase, GnomeSaveStyle save_style, gint shutdown,
124               GnomeInteractStyle interact_style, gint fast, EvApplication *application)
125 {
126         GList *windows, *l;
127         char **restart_argv;
128         int argc = 0, k;
129
130         windows = ev_application_get_windows (application);
131         restart_argv = g_new (char *, g_list_length (windows) + 1);
132         restart_argv[argc++] = g_strdup ("evince");
133
134         for (l = windows; l != NULL; l = l->next) {
135                 EvWindow *window = EV_WINDOW (l->data);
136                 restart_argv[argc++] = g_strdup (ev_window_get_uri (window));
137         }
138
139         gnome_client_set_restart_command (client, argc, restart_argv);
140
141         for (k = 0; k < argc; k++) {
142                 g_free (restart_argv[k]);
143         }
144
145         g_list_free (windows);
146         g_free (restart_argv);
147         
148         return TRUE;
149 }
150
151 static void
152 init_session (EvApplication *application)
153 {
154         GnomeClient *client;
155
156         client = gnome_master_client ();
157
158         g_signal_connect (client, "save_yourself",
159                           G_CALLBACK (save_session), application);      
160         g_signal_connect (client, "die",
161                           G_CALLBACK (removed_from_session), application);
162 }
163
164 static GdkDisplay *
165 ev_display_open_if_needed (const gchar *name)
166 {
167         GSList     *displays;
168         GSList     *l;
169         GdkDisplay *display = NULL;
170
171         displays = gdk_display_manager_list_displays (gdk_display_manager_get ());
172
173         for (l = displays; l != NULL; l = l->next) {
174                 const gchar *display_name = gdk_display_get_name ((GdkDisplay *) l->data);
175
176                 if (g_ascii_strcasecmp (display_name, name) == 0) {
177                         display = l->data;
178                         break;
179                 }
180         }
181
182         g_slist_free (displays);
183
184         return display != NULL ? display : gdk_display_open (name);
185 }
186
187 static GdkScreen *
188 get_screen_from_args (GHashTable *args)
189 {
190         GValue     *value = NULL;
191         GdkDisplay *display = NULL;
192         GdkScreen  *screen = NULL;
193
194         g_assert (args != NULL);
195         
196         value = g_hash_table_lookup (args, "display");
197         if (value) {
198                 const gchar *display_name;
199                 
200                 display_name = g_value_get_string (value);
201                 display = ev_display_open_if_needed (display_name);
202         }
203         
204         value = g_hash_table_lookup (args, "screen");
205         if (value) {
206                 gint screen_number;
207                 
208                 screen_number = g_value_get_int (value);
209                 screen = gdk_display_get_screen (display, screen_number);
210         }
211
212         return screen;
213 }
214
215 static EvWindowRunMode
216 get_window_run_mode_from_args (GHashTable *args)
217 {
218         EvWindowRunMode  mode = EV_WINDOW_MODE_NORMAL;
219         GValue          *value = NULL;
220
221         g_assert (args != NULL);
222
223         value = g_hash_table_lookup (args, "mode");
224         if (value) {
225                 mode = g_value_get_uint (value);
226         }
227
228         return mode;
229 }
230
231 static EvLinkDest *
232 get_destination_from_args (GHashTable *args)
233 {
234         EvLinkDest *dest = NULL;
235         GValue     *value = NULL;
236         
237         g_assert (args != NULL);
238         
239         value = g_hash_table_lookup (args, "page-label");
240         if (value) {
241                 const gchar *page_label;
242
243                 page_label = g_value_get_string (value);
244                 dest = ev_link_dest_new_page_label (page_label);
245         }
246
247         return dest;
248 }
249
250 static gboolean
251 get_unlink_temp_file_from_args (GHashTable *args)
252 {
253         gboolean unlink_temp_file = FALSE;
254         GValue  *value = NULL;
255
256         g_assert (args != NULL);
257
258         value = g_hash_table_lookup (args, "unlink-temp-file");
259         if (value) {
260                 unlink_temp_file = g_value_get_boolean (value);
261         }
262         
263         return unlink_temp_file;
264 }
265
266 gboolean
267 ev_application_open_window (EvApplication  *application,
268                             GHashTable     *args,
269                             guint32         timestamp,
270                             GError        **error)
271 {
272         GtkWidget *new_window = ev_window_new ();
273         GdkScreen *screen = NULL;
274
275         if (args) {
276                 screen = get_screen_from_args (args);
277         }
278         
279         if (screen) {
280                 gtk_window_set_screen (GTK_WINDOW (new_window), screen);
281         }
282         
283         gtk_widget_show (new_window);
284         
285         gtk_window_present_with_time (GTK_WINDOW (new_window),
286                                       timestamp);
287         return TRUE;
288 }
289
290 static EvWindow *
291 ev_application_get_empty_window (EvApplication *application,
292                                  GdkScreen     *screen)
293 {
294         EvWindow *empty_window = NULL;
295         GList *windows = ev_application_get_windows (application);
296         GList *l;
297
298         for (l = windows; l != NULL; l = l->next) {
299                 EvWindow *window = EV_WINDOW (l->data);
300
301                 if (ev_window_is_empty (window) &&
302                     gtk_window_get_screen (GTK_WINDOW (window)) == screen) {
303                         empty_window = window;
304                         break;
305                 }
306         }
307
308         g_list_free (windows);
309         
310         return empty_window;
311 }
312
313 static EvWindow *
314 ev_application_get_uri_window (EvApplication *application, const char *uri)
315 {
316         EvWindow *uri_window = NULL;
317         GList *windows = gtk_window_list_toplevels ();
318         GList *l;
319
320         g_return_val_if_fail (uri != NULL, NULL);
321
322         for (l = windows; l != NULL; l = l->next) {
323                 if (EV_IS_WINDOW (l->data)) {
324                         EvWindow *window = EV_WINDOW (l->data);
325                         const char *window_uri = ev_window_get_uri (window);
326
327                         if (window_uri && strcmp (window_uri, uri) == 0 && !ev_window_is_empty (window)) {
328                                 uri_window = window;
329                                 break;
330                         }
331                 }
332         }
333
334         g_list_free (windows);
335         
336         return uri_window;
337 }
338
339 void
340 ev_application_open_uri_at_dest (EvApplication  *application,
341                                  const char     *uri,
342                                  GdkScreen      *screen,
343                                  EvLinkDest     *dest,
344                                  EvWindowRunMode mode,
345                                  gboolean        unlink_temp_file,
346                                  guint           timestamp)
347 {
348         EvWindow     *new_window;
349         GtkIconTheme *icon_theme;
350
351         g_return_if_fail (uri != NULL);
352
353         icon_theme = gtk_icon_theme_get_for_screen (screen);
354         if (icon_theme) {
355                 gchar **path = NULL;
356                 gint    n_paths;
357                 gint    i;
358                 gchar  *ev_icons_path;
359
360                 /* GtkIconTheme will then look in Evince custom hicolor dir
361                  * for icons as well as the standard search paths
362                  */
363                 ev_icons_path = g_build_filename (DATADIR, "icons", NULL);
364                 gtk_icon_theme_get_search_path (icon_theme, &path, &n_paths);
365                 for (i = n_paths - 1; i >= 0; i--) {
366                         if (g_ascii_strcasecmp (ev_icons_path, path[i]) == 0)
367                                 break;
368                 }
369
370                 if (i < 0)
371                         gtk_icon_theme_append_search_path (icon_theme,
372                                                            ev_icons_path);
373
374                 g_free (ev_icons_path);
375                 g_strfreev (path);
376         }       
377
378         new_window = ev_application_get_uri_window (application, uri);
379         
380         if (new_window == NULL) {
381                 new_window = ev_application_get_empty_window (application, screen);
382         }
383
384         if (new_window == NULL) {
385                 new_window = EV_WINDOW (ev_window_new ());
386         }
387
388         if (screen)
389                 gtk_window_set_screen (GTK_WINDOW (new_window), screen);
390
391         /* We need to load uri before showing the window, so
392            we can restore window size without flickering */     
393         ev_window_open_uri (new_window, uri, dest, mode, unlink_temp_file);
394
395         ev_document_fc_mutex_lock ();
396         gtk_widget_show (GTK_WIDGET (new_window));
397         ev_document_fc_mutex_unlock ();
398
399         gtk_window_present_with_time (GTK_WINDOW (new_window),
400                                       timestamp);
401 }
402
403 gboolean
404 ev_application_open_uri (EvApplication  *application,
405                          const char     *uri,
406                          GHashTable     *args,
407                          guint           timestamp,
408                          GError        **error)
409 {
410         EvLinkDest      *dest = NULL;
411         EvWindowRunMode  mode = EV_WINDOW_MODE_NORMAL;
412         gboolean         unlink_temp_file = FALSE;
413         GdkScreen       *screen = NULL;
414
415         if (args) {
416                 screen = get_screen_from_args (args);
417                 dest = get_destination_from_args (args);
418                 mode = get_window_run_mode_from_args (args);
419                 unlink_temp_file = (mode == EV_WINDOW_MODE_PREVIEW &&
420                                     get_unlink_temp_file_from_args (args));
421         }
422         
423         ev_application_open_uri_at_dest (application, uri, screen,
424                                          dest, mode, unlink_temp_file, 
425                                          timestamp);
426
427         if (dest)
428                 g_object_unref (dest);
429
430         return TRUE;
431 }
432
433 void
434 ev_application_open_uri_list (EvApplication *application,
435                               GSList        *uri_list,
436                               GdkScreen     *screen,
437                               guint          timestamp)
438 {
439         GSList *l;
440
441         for (l = uri_list; l != NULL; l = l->next) {
442                 ev_application_open_uri_at_dest (application, (char *)l->data,
443                                                  screen, NULL, 0, FALSE, 
444                                                  timestamp);
445         }
446 }
447
448 void
449 ev_application_shutdown (EvApplication *application)
450 {
451         if (application->toolbars_model) {
452                 g_object_unref (application->toolbars_model);
453                 g_object_unref (application->preview_toolbars_model);
454                 g_free (application->toolbars_file);
455                 application->toolbars_model = NULL;
456                 application->preview_toolbars_model = NULL;
457                 application->toolbars_file = NULL;
458         }
459
460 #ifndef HAVE_GTK_RECENT
461         if (application->recent_model) {
462                 g_object_unref (application->recent_model);
463                 application->recent_model = NULL;
464         }
465 #endif
466         
467         g_free (application->last_chooser_uri);
468         g_object_unref (application);
469         
470         gtk_main_quit ();
471 }
472
473 static void
474 ev_application_class_init (EvApplicationClass *ev_application_class)
475 {
476 }
477
478 static void
479 ev_application_init (EvApplication *ev_application)
480 {
481         init_session (ev_application);
482
483         ev_application->toolbars_model = egg_toolbars_model_new ();
484
485         ev_application->toolbars_file = g_build_filename
486                         (ev_dot_dir (), "evince_toolbar.xml", NULL);
487
488         egg_toolbars_model_load_names (ev_application->toolbars_model,
489                                        DATADIR "/evince-toolbar.xml");
490
491         if (!egg_toolbars_model_load_toolbars (ev_application->toolbars_model,
492                                                ev_application->toolbars_file)) {
493                 egg_toolbars_model_load_toolbars (ev_application->toolbars_model,
494                                                   DATADIR"/evince-toolbar.xml");
495         }
496
497         egg_toolbars_model_set_flags (ev_application->toolbars_model, 0,
498                                       EGG_TB_MODEL_NOT_REMOVABLE); 
499
500         ev_application->preview_toolbars_model = egg_toolbars_model_new ();
501
502         egg_toolbars_model_load_toolbars (ev_application->preview_toolbars_model,
503                                           DATADIR"/evince-preview-toolbar.xml");
504
505         egg_toolbars_model_set_flags (ev_application->preview_toolbars_model, 0,
506                                       EGG_TB_MODEL_NOT_REMOVABLE); 
507
508 #ifndef HAVE_GTK_RECENT
509         ev_application->recent_model = egg_recent_model_new (EGG_RECENT_MODEL_SORT_MRU);
510         /* FIXME we should add a mime type filter but current eggrecent
511            has only a varargs style api which does not work well when
512            the list of mime types is dynamic */
513         egg_recent_model_set_limit (ev_application->recent_model, 5);   
514         egg_recent_model_set_filter_groups (ev_application->recent_model,
515                                             "Evince", NULL);
516 #endif /* HAVE_GTK_RECENT */
517 }
518
519 GList *
520 ev_application_get_windows (EvApplication *application)
521 {
522         GList *l, *toplevels;
523         GList *windows = NULL;
524
525         toplevels = gtk_window_list_toplevels ();
526
527         for (l = toplevels; l != NULL; l = l->next) {
528                 if (EV_IS_WINDOW (l->data)) {
529                         windows = g_list_append (windows, l->data);
530                 }
531         }
532
533         g_list_free (toplevels);
534
535         return windows;
536 }
537
538 EggToolbarsModel *ev_application_get_toolbars_model (EvApplication *application,
539                                                      gboolean preview)
540 {
541         return preview ? 
542             application->preview_toolbars_model : application->toolbars_model;
543 }
544
545 #ifndef HAVE_GTK_RECENT
546 EggRecentModel *ev_application_get_recent_model (EvApplication *application)
547 {
548         return application->recent_model;
549 }
550 #endif
551
552 void ev_application_save_toolbars_model (EvApplication *application)
553 {
554         egg_toolbars_model_save_toolbars (application->toolbars_model,
555                                           application->toolbars_file, "1.0");
556 }
557
558 void ev_application_set_chooser_uri (EvApplication *application, const gchar *uri)
559 {
560         g_free (application->last_chooser_uri);
561         application->last_chooser_uri = g_strdup (uri);
562 }
563
564 const gchar* ev_application_get_chooser_uri (EvApplication *application)
565 {
566         return application->last_chooser_uri;
567 }
568
569 void ev_application_screensaver_enable  (EvApplication   *application)
570 {
571         if (application->scr_saver)
572                 totem_scrsaver_enable (application->scr_saver); 
573 }
574
575 void ev_application_screensaver_disable (EvApplication   *application)
576 {
577         if (application->scr_saver)
578                 totem_scrsaver_disable (application->scr_saver);        
579 }