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