]> www.fi.muni.cz Git - evince.git/blob - shell/ev-application.c
Rework links system, it adds support for remote links now and it makes
[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 EV_APPLICATION_GET_PRIVATE(object) \
50         (G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_APPLICATION, EvApplicationPrivate))
51
52 #define APPLICATION_SERVICE_NAME "org.gnome.evince.ApplicationService"
53
54 #ifdef ENABLE_DBUS
55 gboolean
56 ev_application_register_service (EvApplication *application)
57 {
58         DBusGConnection *connection;
59         DBusGProxy *driver_proxy;
60         GError *err = NULL;
61         guint request_name_result;
62
63         connection = dbus_g_bus_get (DBUS_BUS_STARTER, &err);
64         if (connection == NULL) {
65                 g_warning ("Service registration failed.");
66                 g_error_free (err);
67
68                 return FALSE;
69         }
70
71         driver_proxy = dbus_g_proxy_new_for_name (connection,
72                                                   DBUS_SERVICE_DBUS,
73                                                   DBUS_PATH_DBUS,
74                                                   DBUS_INTERFACE_DBUS);
75
76 #if DBUS_VERSION >= 60  
77         if (!org_freedesktop_DBus_request_name (driver_proxy,
78                                                 APPLICATION_SERVICE_NAME,
79                                                 DBUS_NAME_FLAG_DO_NOT_QUEUE,
80                                                 &request_name_result, &err)) {
81                 g_warning ("Service registration failed.");
82                 g_clear_error (&err);
83         }
84 #else
85         if (!org_freedesktop_DBus_request_name (driver_proxy,
86                                                 APPLICATION_SERVICE_NAME,
87                                                 0, &request_name_result, &err)) {
88                 g_warning ("Service registration failed.");
89                 g_clear_error (&err);
90         }
91 #endif  
92
93         if (request_name_result == DBUS_REQUEST_NAME_REPLY_EXISTS) {
94                 return FALSE;
95         }
96
97 #if DBUS_VERSION == 33
98         dbus_g_object_class_install_info (G_OBJECT_GET_CLASS (application),
99                                           &dbus_glib_ev_application_object_info);
100 #else
101         dbus_g_object_type_install_info (EV_TYPE_APPLICATION,
102                                          &dbus_glib_ev_application_object_info);
103 #endif
104
105         dbus_g_connection_register_g_object (connection,
106                                              "/org/gnome/evince/Evince",
107                                              G_OBJECT (application));
108
109         return TRUE;
110 }
111 #endif /* ENABLE_DBUS */
112
113 EvApplication *
114 ev_application_get_instance (void)
115 {
116         static EvApplication *instance;
117
118         if (!instance) {
119                 instance = EV_APPLICATION (g_object_new (EV_TYPE_APPLICATION, NULL));
120         }
121
122         return instance;
123 }
124
125 static void
126 removed_from_session (GnomeClient *client, EvApplication *application)
127 {
128         ev_application_shutdown (application);
129 }
130
131 static gint
132 save_session (GnomeClient *client, gint phase, GnomeSaveStyle save_style, gint shutdown,
133               GnomeInteractStyle interact_style, gint fast, EvApplication *application)
134 {
135         GList *windows, *l;
136         char **restart_argv;
137         int argc = 0, k;
138
139         windows = ev_application_get_windows (application);
140         restart_argv = g_new (char *, g_list_length (windows) + 1);
141         restart_argv[argc++] = g_strdup ("evince");
142
143         for (l = windows; l != NULL; l = l->next) {
144                 EvWindow *window = EV_WINDOW (l->data);
145                 restart_argv[argc++] = g_strdup (ev_window_get_uri (window));
146         }
147
148         gnome_client_set_restart_command (client, argc, restart_argv);
149
150         for (k = 0; k < argc; k++) {
151                 g_free (restart_argv[k]);
152         }
153
154         g_list_free (windows);
155         g_free (restart_argv);
156         
157         return TRUE;
158 }
159
160 static void
161 init_session (EvApplication *application)
162 {
163         GnomeClient *client;
164
165         client = gnome_master_client ();
166
167         g_signal_connect (client, "save_yourself",
168                           G_CALLBACK (save_session), application);      
169         g_signal_connect (client, "die",
170                           G_CALLBACK (removed_from_session), application);
171 }
172
173 gboolean
174 ev_application_open_window (EvApplication  *application,
175                             guint32         timestamp,
176                             GError        **error)
177 {
178         GtkWidget *new_window = ev_window_new ();
179
180         gtk_widget_show (new_window);
181         
182 #ifdef HAVE_GTK_WINDOW_PRESENT_WITH_TIME
183         gtk_window_present_with_time (GTK_WINDOW (new_window),
184                                       timestamp);
185 #else
186         gtk_window_present (GTK_WINDOW (new_window));
187 #endif
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                                  guint           timestamp)
243 {
244         EvWindow *new_window;
245
246         g_return_if_fail (uri != NULL);
247
248         new_window = ev_application_get_uri_window (application, uri);
249         if (new_window != NULL) {
250 #ifdef HAVE_GTK_WINDOW_PRESENT_WITH_TIME
251                 gtk_window_present_with_time (GTK_WINDOW (new_window),
252                                               timestamp);
253 #else
254                 gtk_window_present (GTK_WINDOW (new_window));
255 #endif
256                 if (dest)
257                         ev_window_goto_dest (new_window, dest);
258
259                 return;
260         }
261
262         new_window = ev_application_get_empty_window (application);
263
264         if (new_window == NULL) {
265                 new_window = EV_WINDOW (ev_window_new ());
266         }
267
268         /* We need to load uri before showing the window, so
269            we can restore window size without flickering */     
270         ev_window_open_uri (new_window, uri, dest);
271
272         gtk_widget_show (GTK_WIDGET (new_window));
273
274 #ifdef HAVE_GTK_WINDOW_PRESENT_WITH_TIME
275         gtk_window_present_with_time (GTK_WINDOW (new_window),
276                                       timestamp);
277 #else
278         gtk_window_present (GTK_WINDOW (new_window));
279 #endif
280 }
281
282 gboolean
283 ev_application_open_uri (EvApplication  *application,
284                          const char     *uri,
285                          const char     *page_label,
286                          guint           timestamp,
287                          GError        **error)
288 {
289         ev_application_open_uri_at_dest (application, uri, NULL, timestamp);
290         
291         if (page_label && strcmp (page_label, "") != 0) {
292                 EvWindow *window;
293
294                 window = ev_application_get_uri_window (application, uri);
295                 ev_window_open_page_label (window, page_label);
296         }
297
298         return TRUE;
299 }
300
301 void
302 ev_application_open_uri_list (EvApplication *application,
303                               GSList        *uri_list,
304                               guint          timestamp)
305 {
306         GSList *l;
307
308         for (l = uri_list; l != NULL; l = l->next) {
309                 ev_application_open_uri (application, (char *)l->data,
310                                          NULL, timestamp, NULL);
311         }
312 }
313
314 void
315 ev_application_shutdown (EvApplication *application)
316 {
317         if (application->toolbars_model) {
318                 g_object_unref (application->toolbars_model);
319                 g_free (application->toolbars_file);
320                 application->toolbars_model = NULL;
321                 application->toolbars_file = NULL;
322         }
323
324         if (application->recent_model) {
325                 g_object_unref (application->recent_model);
326                 application->recent_model = NULL;
327         }
328
329         g_free (application->last_chooser_uri);
330         g_object_unref (application);
331         
332         gtk_main_quit ();
333 }
334
335 static void
336 ev_application_class_init (EvApplicationClass *ev_application_class)
337 {
338 }
339
340 static void
341 ev_application_init (EvApplication *ev_application)
342 {
343         init_session (ev_application);
344
345         ev_application->toolbars_model = egg_toolbars_model_new ();
346
347         ev_application->toolbars_file = g_build_filename
348                         (ev_dot_dir (), "evince_toolbar.xml", NULL);
349
350         if (!egg_toolbars_model_load (ev_application->toolbars_model,
351                                       ev_application->toolbars_file)) {
352                 egg_toolbars_model_load (ev_application->toolbars_model,
353                                          DATADIR"/evince-toolbar.xml");
354         }
355
356         egg_toolbars_model_set_flags (ev_application->toolbars_model, 0,
357                                       EGG_TB_MODEL_NOT_REMOVABLE); 
358                                       
359         ev_application->recent_model = egg_recent_model_new (EGG_RECENT_MODEL_SORT_MRU);
360         /* FIXME we should add a mime type filter but current eggrecent
361            has only a varargs style api which does not work well when
362            the list of mime types is dynamic */
363         egg_recent_model_set_limit (ev_application->recent_model, 5);   
364         egg_recent_model_set_filter_groups (ev_application->recent_model,
365                                             "Evince", NULL);
366 }
367
368 GList *
369 ev_application_get_windows (EvApplication *application)
370 {
371         GList *l, *toplevels;
372         GList *windows = NULL;
373
374         toplevels = gtk_window_list_toplevels ();
375
376         for (l = toplevels; l != NULL; l = l->next) {
377                 if (EV_IS_WINDOW (l->data)) {
378                         windows = g_list_append (windows, l->data);
379                 }
380         }
381
382         g_list_free (toplevels);
383
384         return windows;
385 }
386
387 EggToolbarsModel *ev_application_get_toolbars_model (EvApplication *application)
388 {
389         return application->toolbars_model;
390 }
391
392 EggRecentModel *ev_application_get_recent_model (EvApplication *application)
393 {
394         return application->recent_model;
395 }
396
397 void ev_application_save_toolbars_model (EvApplication *application)
398 {
399         egg_toolbars_model_save (application->toolbars_model,
400                                  application->toolbars_file, "1.0");
401 }
402
403 void ev_application_set_chooser_uri (EvApplication *application, const gchar *uri)
404 {
405         g_free (application->last_chooser_uri);
406         application->last_chooser_uri = g_strdup (uri);
407 }
408
409 const gchar* ev_application_get_chooser_uri (EvApplication *application)
410 {
411         return application->last_chooser_uri;
412 }
413