]> www.fi.muni.cz Git - evince.git/blob - shell/ev-application.c
Bump requirements to dbus-glib 0.71 and drop all ifdefs used.
[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
350         g_return_if_fail (uri != NULL);
351
352         new_window = ev_application_get_uri_window (application, uri);
353         
354         if (new_window == NULL) {
355                 new_window = ev_application_get_empty_window (application, screen);
356         }
357
358         if (new_window == NULL) {
359                 new_window = EV_WINDOW (ev_window_new ());
360         }
361
362         if (screen)
363                 gtk_window_set_screen (GTK_WINDOW (new_window), screen);
364
365         /* We need to load uri before showing the window, so
366            we can restore window size without flickering */     
367         ev_window_open_uri (new_window, uri, dest, mode, unlink_temp_file);
368
369         gtk_widget_show (GTK_WIDGET (new_window));
370
371         gtk_window_present_with_time (GTK_WINDOW (new_window),
372                                       timestamp);
373 }
374
375 gboolean
376 ev_application_open_uri (EvApplication  *application,
377                          const char     *uri,
378                          GHashTable     *args,
379                          guint           timestamp,
380                          GError        **error)
381 {
382         EvLinkDest      *dest = NULL;
383         EvWindowRunMode  mode = EV_WINDOW_MODE_NORMAL;
384         gboolean         unlink_temp_file = FALSE;
385         GdkScreen       *screen = NULL;
386
387         if (args) {
388                 screen = get_screen_from_args (args);
389                 dest = get_destination_from_args (args);
390                 mode = get_window_run_mode_from_args (args);
391                 unlink_temp_file = (mode == EV_WINDOW_MODE_PREVIEW &&
392                                     get_unlink_temp_file_from_args (args));
393         }
394         
395         ev_application_open_uri_at_dest (application, uri, screen,
396                                          dest, mode, unlink_temp_file, 
397                                          timestamp);
398
399         if (dest)
400                 g_object_unref (dest);
401
402         return TRUE;
403 }
404
405 void
406 ev_application_open_uri_list (EvApplication *application,
407                               GSList        *uri_list,
408                               GdkScreen     *screen,
409                               guint          timestamp)
410 {
411         GSList *l;
412
413         for (l = uri_list; l != NULL; l = l->next) {
414                 ev_application_open_uri_at_dest (application, (char *)l->data,
415                                                  screen, NULL, 0, FALSE, 
416                                                  timestamp);
417         }
418 }
419
420 void
421 ev_application_shutdown (EvApplication *application)
422 {
423         if (application->toolbars_model) {
424                 g_object_unref (application->toolbars_model);
425                 g_object_unref (application->preview_toolbars_model);
426                 g_free (application->toolbars_file);
427                 application->toolbars_model = NULL;
428                 application->preview_toolbars_model = NULL;
429                 application->toolbars_file = NULL;
430         }
431
432 #ifndef HAVE_GTK_RECENT
433         if (application->recent_model) {
434                 g_object_unref (application->recent_model);
435                 application->recent_model = NULL;
436         }
437 #endif
438         
439         g_free (application->last_chooser_uri);
440         g_object_unref (application);
441         
442         gtk_main_quit ();
443 }
444
445 static void
446 ev_application_class_init (EvApplicationClass *ev_application_class)
447 {
448 }
449
450 static void
451 ev_application_init (EvApplication *ev_application)
452 {
453         init_session (ev_application);
454
455         ev_application->toolbars_model = egg_toolbars_model_new ();
456
457         ev_application->toolbars_file = g_build_filename
458                         (ev_dot_dir (), "evince_toolbar.xml", NULL);
459
460         egg_toolbars_model_load_names (ev_application->toolbars_model,
461                                        DATADIR "/evince-toolbar.xml");
462
463         if (!egg_toolbars_model_load_toolbars (ev_application->toolbars_model,
464                                                ev_application->toolbars_file)) {
465                 egg_toolbars_model_load_toolbars (ev_application->toolbars_model,
466                                                   DATADIR"/evince-toolbar.xml");
467         }
468
469         egg_toolbars_model_set_flags (ev_application->toolbars_model, 0,
470                                       EGG_TB_MODEL_NOT_REMOVABLE); 
471
472         ev_application->preview_toolbars_model = egg_toolbars_model_new ();
473
474         egg_toolbars_model_load_toolbars (ev_application->preview_toolbars_model,
475                                           DATADIR"/evince-preview-toolbar.xml");
476
477         egg_toolbars_model_set_flags (ev_application->preview_toolbars_model, 0,
478                                       EGG_TB_MODEL_NOT_REMOVABLE); 
479
480 #ifndef HAVE_GTK_RECENT
481         ev_application->recent_model = egg_recent_model_new (EGG_RECENT_MODEL_SORT_MRU);
482         /* FIXME we should add a mime type filter but current eggrecent
483            has only a varargs style api which does not work well when
484            the list of mime types is dynamic */
485         egg_recent_model_set_limit (ev_application->recent_model, 5);   
486         egg_recent_model_set_filter_groups (ev_application->recent_model,
487                                             "Evince", NULL);
488 #endif /* HAVE_GTK_RECENT */
489 }
490
491 GList *
492 ev_application_get_windows (EvApplication *application)
493 {
494         GList *l, *toplevels;
495         GList *windows = NULL;
496
497         toplevels = gtk_window_list_toplevels ();
498
499         for (l = toplevels; l != NULL; l = l->next) {
500                 if (EV_IS_WINDOW (l->data)) {
501                         windows = g_list_append (windows, l->data);
502                 }
503         }
504
505         g_list_free (toplevels);
506
507         return windows;
508 }
509
510 EggToolbarsModel *ev_application_get_toolbars_model (EvApplication *application,
511                                                      gboolean preview)
512 {
513         return preview ? 
514             application->preview_toolbars_model : application->toolbars_model;
515 }
516
517 #ifndef HAVE_GTK_RECENT
518 EggRecentModel *ev_application_get_recent_model (EvApplication *application)
519 {
520         return application->recent_model;
521 }
522 #endif
523
524 void ev_application_save_toolbars_model (EvApplication *application)
525 {
526         egg_toolbars_model_save_toolbars (application->toolbars_model,
527                                           application->toolbars_file, "1.0");
528 }
529
530 void ev_application_set_chooser_uri (EvApplication *application, const gchar *uri)
531 {
532         g_free (application->last_chooser_uri);
533         application->last_chooser_uri = g_strdup (uri);
534 }
535
536 const gchar* ev_application_get_chooser_uri (EvApplication *application)
537 {
538         return application->last_chooser_uri;
539 }
540
541 void ev_application_screensaver_enable  (EvApplication   *application)
542 {
543         if (application->scr_saver)
544                 totem_scrsaver_enable (application->scr_saver); 
545 }
546
547 void ev_application_screensaver_disable (EvApplication   *application)
548 {
549         if (application->scr_saver)
550                 totem_scrsaver_disable (application->scr_saver);        
551 }