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