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