]> www.fi.muni.cz Git - evince.git/blob - shell/ev-application.c
Reopen should reload document. Fixes bug 327951.
[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 gboolean
178 ev_application_open_window (EvApplication  *application,
179                             guint32         timestamp,
180                             GError        **error)
181 {
182         GtkWidget *new_window = ev_window_new ();
183
184         gtk_widget_show (new_window);
185         
186         gtk_window_present_with_time (GTK_WINDOW (new_window),
187                                       timestamp);
188         return TRUE;
189 }
190
191 static EvWindow *
192 ev_application_get_empty_window (EvApplication *application)
193 {
194         EvWindow *empty_window = NULL;
195         GList *windows = ev_application_get_windows (application);
196         GList *l;
197
198         for (l = windows; l != NULL; l = l->next) {
199                 EvWindow *window = EV_WINDOW (l->data);
200
201                 if (ev_window_is_empty (window)) {
202                         empty_window = window;
203                         break;
204                 }
205         }
206
207         g_list_free (windows);
208         
209         return empty_window;
210 }
211
212 static EvWindow *
213 ev_application_get_uri_window (EvApplication *application, const char *uri)
214 {
215         EvWindow *uri_window = NULL;
216         GList *windows = gtk_window_list_toplevels ();
217         GList *l;
218
219         g_return_val_if_fail (uri != NULL, NULL);
220
221         for (l = windows; l != NULL; l = l->next) {
222                 if (EV_IS_WINDOW (l->data)) {
223                         EvWindow *window = EV_WINDOW (l->data);
224                         const char *window_uri = ev_window_get_uri (window);
225
226                         if (window_uri && strcmp (window_uri, uri) == 0 && !ev_window_is_empty (window)) {
227                                 uri_window = window;
228                                 break;
229                         }
230                 }
231         }
232
233         g_list_free (windows);
234         
235         return uri_window;
236 }
237
238 void
239 ev_application_open_uri_at_dest (EvApplication  *application,
240                                  const char     *uri,
241                                  EvLinkDest     *dest,
242                                  EvWindowRunMode mode,
243                                  guint           timestamp)
244 {
245         EvWindow *new_window;
246
247         g_return_if_fail (uri != NULL);
248
249         new_window = ev_application_get_uri_window (application, uri);
250         
251         if (new_window == NULL) {
252                 new_window = ev_application_get_empty_window (application);
253         }
254
255         if (new_window == NULL) {
256                 new_window = EV_WINDOW (ev_window_new ());
257         }
258
259         /* We need to load uri before showing the window, so
260            we can restore window size without flickering */     
261         ev_window_open_uri (new_window, uri, dest, mode);
262
263         gtk_widget_show (GTK_WIDGET (new_window));
264
265         gtk_window_present_with_time (GTK_WINDOW (new_window),
266                                       timestamp);
267 }
268
269 gboolean
270 ev_application_open_uri (EvApplication  *application,
271                          const char     *uri,
272                          GHashTable     *args,
273                          guint           timestamp,
274                          GError        **error)
275 {
276         EvLinkDest      *dest = NULL;
277         EvWindowRunMode  mode = EV_WINDOW_MODE_NORMAL;
278
279         if (args) {
280                 GValue *value = NULL;
281                 
282                 value = g_hash_table_lookup (args, "page-label");
283                 if (value) {
284                         const gchar *page_label;
285                         
286                         page_label = g_value_get_string (value);
287                         dest = ev_link_dest_new_page_label (page_label);
288                 }
289                 
290                 value = g_hash_table_lookup (args, "mode");
291                 if (value) {
292                         mode = g_value_get_uint (value);
293                 }
294         }
295         
296         ev_application_open_uri_at_dest (application, uri, dest, mode, timestamp);
297
298         if (dest)
299                 g_object_unref (dest);
300
301         return TRUE;
302 }
303
304 void
305 ev_application_open_uri_list (EvApplication *application,
306                               GSList        *uri_list,
307                               guint          timestamp)
308 {
309         GSList *l;
310
311         for (l = uri_list; l != NULL; l = l->next) {
312                 ev_application_open_uri (application, (char *)l->data,
313                                          NULL, timestamp, NULL);
314         }
315 }
316
317 void
318 ev_application_shutdown (EvApplication *application)
319 {
320         if (application->toolbars_model) {
321                 g_object_unref (application->toolbars_model);
322                 g_free (application->toolbars_file);
323                 application->toolbars_model = NULL;
324                 application->toolbars_file = NULL;
325         }
326
327 #ifndef HAVE_GTK_RECENT
328         if (application->recent_model) {
329                 g_object_unref (application->recent_model);
330                 application->recent_model = NULL;
331         }
332 #endif
333         
334         g_free (application->last_chooser_uri);
335         g_object_unref (application);
336         
337         gtk_main_quit ();
338 }
339
340 static void
341 ev_application_class_init (EvApplicationClass *ev_application_class)
342 {
343 }
344
345 static void
346 ev_application_init (EvApplication *ev_application)
347 {
348         init_session (ev_application);
349
350         ev_application->toolbars_model = egg_toolbars_model_new ();
351
352         ev_application->toolbars_file = g_build_filename
353                         (ev_dot_dir (), "evince_toolbar.xml", NULL);
354
355         egg_toolbars_model_load_names (ev_application->toolbars_model,
356                                        DATADIR "/evince-toolbar.xml");
357
358         if (!egg_toolbars_model_load_toolbars (ev_application->toolbars_model,
359                                                ev_application->toolbars_file)) {
360                 egg_toolbars_model_load_toolbars (ev_application->toolbars_model,
361                                                   DATADIR"/evince-toolbar.xml");
362         }
363
364         egg_toolbars_model_set_flags (ev_application->toolbars_model, 0,
365                                       EGG_TB_MODEL_NOT_REMOVABLE); 
366
367 #ifndef HAVE_GTK_RECENT
368         ev_application->recent_model = egg_recent_model_new (EGG_RECENT_MODEL_SORT_MRU);
369         /* FIXME we should add a mime type filter but current eggrecent
370            has only a varargs style api which does not work well when
371            the list of mime types is dynamic */
372         egg_recent_model_set_limit (ev_application->recent_model, 5);   
373         egg_recent_model_set_filter_groups (ev_application->recent_model,
374                                             "Evince", NULL);
375 #endif /* HAVE_GTK_RECENT */
376 }
377
378 GList *
379 ev_application_get_windows (EvApplication *application)
380 {
381         GList *l, *toplevels;
382         GList *windows = NULL;
383
384         toplevels = gtk_window_list_toplevels ();
385
386         for (l = toplevels; l != NULL; l = l->next) {
387                 if (EV_IS_WINDOW (l->data)) {
388                         windows = g_list_append (windows, l->data);
389                 }
390         }
391
392         g_list_free (toplevels);
393
394         return windows;
395 }
396
397 EggToolbarsModel *ev_application_get_toolbars_model (EvApplication *application)
398 {
399         return application->toolbars_model;
400 }
401
402 #ifndef HAVE_GTK_RECENT
403 EggRecentModel *ev_application_get_recent_model (EvApplication *application)
404 {
405         return application->recent_model;
406 }
407 #endif
408
409 void ev_application_save_toolbars_model (EvApplication *application)
410 {
411         egg_toolbars_model_save_toolbars (application->toolbars_model,
412                                           application->toolbars_file, "1.0");
413 }
414
415 void ev_application_set_chooser_uri (EvApplication *application, const gchar *uri)
416 {
417         g_free (application->last_chooser_uri);
418         application->last_chooser_uri = g_strdup (uri);
419 }
420
421 const gchar* ev_application_get_chooser_uri (EvApplication *application)
422 {
423         return application->last_chooser_uri;
424 }
425