]> www.fi.muni.cz Git - evince.git/blob - shell/ev-application.c
New CBR/CBZ backend for comic books.
[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-document-types.h"
30 #include "ev-file-helpers.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 (!org_freedesktop_DBus_request_name (driver_proxy,
77                                                 APPLICATION_SERVICE_NAME,
78                                                 0, &request_name_result, &err)) {
79                 g_warning ("Service registration failed.");
80                 g_clear_error (&err);
81         }
82
83         if (request_name_result == DBUS_REQUEST_NAME_REPLY_EXISTS) {
84                 return FALSE;
85         }
86
87 #if DBUS_VERSION == 33
88         dbus_g_object_class_install_info (G_OBJECT_GET_CLASS (application),
89                                           &dbus_glib_ev_application_object_info);
90 #else
91         dbus_g_object_type_install_info (EV_TYPE_APPLICATION,
92                                          &dbus_glib_ev_application_object_info);
93 #endif
94 #ifdef ENABLE_COMICS
95         GtkFileFilter *comics_filter;
96 #endif
97
98         dbus_g_connection_register_g_object (connection,
99                                              "/org/gnome/evince/Evince",
100                                              G_OBJECT (application));
101
102         return TRUE;
103 }
104 #endif /* ENABLE_DBUS */
105
106 EvApplication *
107 ev_application_get_instance (void)
108 {
109         static EvApplication *instance;
110
111         if (!instance) {
112                 instance = EV_APPLICATION (g_object_new (EV_TYPE_APPLICATION, NULL));
113         }
114
115         return instance;
116 }
117
118 static void
119 removed_from_session (GnomeClient *client, EvApplication *application)
120 {
121         ev_application_shutdown (application);
122 }
123
124 static gint
125 save_session (GnomeClient *client, gint phase, GnomeSaveStyle save_style, gint shutdown,
126               GnomeInteractStyle interact_style, gint fast, EvApplication *application)
127 {
128         GList *windows, *l;
129         char **restart_argv;
130         int argc = 0, k;
131
132         windows = ev_application_get_windows (application);
133         restart_argv = g_new (char *, g_list_length (windows) + 1);
134         restart_argv[argc++] = g_strdup ("evince");
135
136         for (l = windows; l != NULL; l = l->next) {
137                 EvWindow *window = EV_WINDOW (l->data);
138                 restart_argv[argc++] = g_strdup (ev_window_get_uri (window));
139         }
140
141         gnome_client_set_restart_command (client, argc, restart_argv);
142
143         for (k = 0; k < argc; k++) {
144                 g_free (restart_argv[k]);
145         }
146
147         g_list_free (windows);
148         g_free (restart_argv);
149         
150         return TRUE;
151 }
152
153 static void
154 init_session (EvApplication *application)
155 {
156         GnomeClient *client;
157
158         client = gnome_master_client ();
159
160         g_signal_connect (client, "save_yourself",
161                           G_CALLBACK (save_session), application);      
162         g_signal_connect (client, "die",
163                           G_CALLBACK (removed_from_session), application);
164 }
165
166 gboolean
167 ev_application_open_window (EvApplication  *application,
168                             guint32         timestamp,
169                             GError        **error)
170 {
171         GtkWidget *new_window = ev_window_new ();
172
173         gtk_widget_show (new_window);
174         
175 #ifdef HAVE_GTK_WINDOW_PRESENT_WITH_TIME
176         gtk_window_present_with_time (GTK_WINDOW (new_window),
177                                       timestamp);
178 #else
179         gtk_window_present (GTK_WINDOW (new_window));
180 #endif
181
182         return TRUE;
183 }
184
185 static EvWindow *
186 ev_application_get_empty_window (EvApplication *application)
187 {
188         EvWindow *empty_window = NULL;
189         GList *windows = ev_application_get_windows (application);
190         GList *l;
191
192         for (l = windows; l != NULL; l = l->next) {
193                 EvWindow *window = EV_WINDOW (l->data);
194
195                 if (ev_window_is_empty (window)) {
196                         empty_window = window;
197                         break;
198                 }
199         }
200
201         g_list_free (windows);
202         
203         return empty_window;
204 }
205
206 static EvWindow *
207 ev_application_get_uri_window (EvApplication *application, const char *uri)
208 {
209         EvWindow *uri_window = NULL;
210         GList *windows = gtk_window_list_toplevels ();
211         GList *l;
212
213         g_return_val_if_fail (uri != NULL, NULL);
214
215         for (l = windows; l != NULL; l = l->next) {
216                 if (EV_IS_WINDOW (l->data)) {
217                         EvWindow *window = EV_WINDOW (l->data);
218                         const char *window_uri = ev_window_get_uri (window);
219
220                         if (window_uri && strcmp (window_uri, uri) == 0) {
221                                 uri_window = window;
222                                 break;
223                         }
224                 }
225         }
226
227         g_list_free (windows);
228         
229         return uri_window;
230 }
231
232 gboolean
233 ev_application_open_uri (EvApplication  *application,
234                          const char     *uri,
235                          const char     *page_label,
236                          guint           timestamp,
237                          GError        **error)
238 {
239         EvWindow *new_window;
240
241         g_return_val_if_fail (uri != NULL, FALSE);
242
243         new_window = ev_application_get_uri_window (application, uri);
244         if (new_window != NULL) {
245 #ifdef HAVE_GTK_WINDOW_PRESENT_WITH_TIME
246                 gtk_window_present_with_time (GTK_WINDOW (new_window),
247                                               timestamp);
248 #else
249                 gtk_window_present (GTK_WINDOW (new_window));
250 #endif  
251                 return TRUE;
252         }
253
254         new_window = ev_application_get_empty_window (application);
255
256         if (new_window == NULL) {
257                 new_window = EV_WINDOW (ev_window_new ());
258         }
259
260         /* We need to load uri before showing the window, so
261            we can restore window size without flickering */     
262         ev_window_open_uri (new_window, uri);
263
264         gtk_widget_show (GTK_WIDGET (new_window));
265
266 #ifdef HAVE_GTK_WINDOW_PRESENT_WITH_TIME
267         gtk_window_present_with_time (GTK_WINDOW (new_window),
268                                       timestamp);
269 #else
270         gtk_window_present (GTK_WINDOW (new_window));
271 #endif
272
273         if (page_label != NULL) {
274                 ev_window_open_page_label (new_window, page_label);
275         }
276
277         return TRUE;
278 }
279
280 void
281 ev_application_open_uri_list (EvApplication *application,
282                               GSList        *uri_list,
283                               guint          timestamp)
284 {
285         GSList *l;
286
287         for (l = uri_list; l != NULL; l = l->next) {
288                 ev_application_open_uri (application, (char *)l->data,
289                                          NULL,
290                                          timestamp,
291                                          NULL);
292         }
293 }
294
295 void
296 ev_application_shutdown (EvApplication *application)
297 {
298         if (application->toolbars_model) {
299                 g_object_unref (application->toolbars_model);
300                 g_free (application->toolbars_file);
301                 application->toolbars_model = NULL;
302                 application->toolbars_file = NULL;
303         }
304
305         if (application->recent_model) {
306                 g_object_unref (application->recent_model);
307                 application->recent_model = NULL;
308         }
309
310         g_object_unref (application);
311         gtk_main_quit ();
312 }
313
314 static void
315 ev_application_class_init (EvApplicationClass *ev_application_class)
316 {
317 }
318
319 static void
320 ev_application_init (EvApplication *ev_application)
321 {
322         init_session (ev_application);
323
324         ev_application->toolbars_model = egg_toolbars_model_new ();
325
326         ev_application->toolbars_file = g_build_filename
327                         (ev_dot_dir (), "evince_toolbar.xml", NULL);
328
329         if (!egg_toolbars_model_load (ev_application->toolbars_model,
330                                       ev_application->toolbars_file)) {
331                 egg_toolbars_model_load (ev_application->toolbars_model,
332                                          DATADIR"/evince-toolbar.xml");
333         }
334
335         egg_toolbars_model_set_flags (ev_application->toolbars_model, 0,
336                                       EGG_TB_MODEL_NOT_REMOVABLE); 
337                                       
338         ev_application->recent_model = egg_recent_model_new (EGG_RECENT_MODEL_SORT_MRU);
339         /* FIXME we should add a mime type filter but current eggrecent
340            has only a varargs style api which does not work well when
341            the list of mime types is dynamic */
342         egg_recent_model_set_limit (ev_application->recent_model, 5);   
343         egg_recent_model_set_filter_groups (ev_application->recent_model,
344                                             "Evince", NULL);
345 }
346
347 GList *
348 ev_application_get_windows (EvApplication *application)
349 {
350         GList *l, *toplevels;
351         GList *windows = NULL;
352
353         toplevels = gtk_window_list_toplevels ();
354
355         for (l = toplevels; l != NULL; l = l->next) {
356                 if (EV_IS_WINDOW (l->data)) {
357                         windows = g_list_append (windows, l->data);
358                 }
359         }
360
361         g_list_free (toplevels);
362
363         return windows;
364 }
365
366 EggToolbarsModel *ev_application_get_toolbars_model (EvApplication *application)
367 {
368         return application->toolbars_model;
369 }
370
371 EggRecentModel *ev_application_get_recent_model (EvApplication *application)
372 {
373         return application->recent_model;
374 }
375
376 void ev_application_save_toolbars_model (EvApplication *application)
377 {
378         egg_toolbars_model_save (application->toolbars_model,
379                                  application->toolbars_file, "1.0");
380 }
381
382