]> www.fi.muni.cz Git - evince.git/blob - shell/ev-application.c
Add 'timestamp' argument to ev_application_open_window and
[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 <string.h>
40
41 #ifdef ENABLE_DBUS
42 #include "ev-application-service.h"
43 #include <dbus/dbus-glib-bindings.h>
44 #endif
45
46 G_DEFINE_TYPE (EvApplication, ev_application, G_TYPE_OBJECT);
47
48 #define EV_APPLICATION_GET_PRIVATE(object) \
49         (G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_APPLICATION, EvApplicationPrivate))
50
51 #define APPLICATION_SERVICE_NAME "org.gnome.evince.ApplicationService"
52
53 #ifdef ENABLE_DBUS
54 gboolean
55 ev_application_register_service (EvApplication *application)
56 {
57         DBusGConnection *connection;
58         DBusGProxy *driver_proxy;
59         GError *err = NULL;
60         guint request_name_result;
61
62         connection = dbus_g_bus_get (DBUS_BUS_STARTER, &err);
63         if (connection == NULL) {
64                 g_warning ("Service registration failed.");
65                 g_error_free (err);
66
67                 return FALSE;
68         }
69
70         driver_proxy = dbus_g_proxy_new_for_name (connection,
71                                                   DBUS_SERVICE_DBUS,
72                                                   DBUS_PATH_DBUS,
73                                                   DBUS_INTERFACE_DBUS);
74
75         if (!org_freedesktop_DBus_request_name (driver_proxy,
76                                                 APPLICATION_SERVICE_NAME,
77                                                 0, &request_name_result, &err)) {
78                 g_warning ("Service registration failed.");
79                 g_clear_error (&err);
80         }
81
82         if (request_name_result == DBUS_REQUEST_NAME_REPLY_EXISTS) {
83                 return FALSE;
84         }
85
86 #if DBUS_VERSION == 33
87         dbus_g_object_class_install_info (G_OBJECT_GET_CLASS (application),
88                                           &dbus_glib_ev_application_object_info);
89 #else
90         dbus_g_object_type_install_info (EV_TYPE_APPLICATION,
91                                          &dbus_glib_ev_application_object_info);
92 #endif
93
94         dbus_g_connection_register_g_object (connection,
95                                              "/org/gnome/evince/Evince",
96                                              G_OBJECT (application));
97
98         return TRUE;
99 }
100 #endif /* ENABLE_DBUS */
101
102 EvApplication *
103 ev_application_get_instance (void)
104 {
105         static EvApplication *instance;
106
107         if (!instance) {
108                 instance = EV_APPLICATION (g_object_new (EV_TYPE_APPLICATION, NULL));
109         }
110
111         return instance;
112 }
113
114 gboolean
115 ev_application_open_window (EvApplication  *application,
116                             guint32         timestamp,
117                             GError        **error)
118 {
119         GtkWidget *new_window = ev_window_new ();
120
121         gtk_widget_show (new_window);
122         gtk_window_present_with_time (GTK_WINDOW (new_window),
123                                       timestamp);
124
125         return TRUE;
126 }
127
128 static EvWindow *
129 ev_application_get_empty_window (EvApplication *application)
130 {
131         EvWindow *empty_window = NULL;
132         GList *windows = gtk_window_list_toplevels ();
133         GList *l;
134
135         for (l = windows; l != NULL; l = l->next) {
136                 if (EV_IS_WINDOW (l->data)) {
137                         EvWindow *window = EV_WINDOW (l->data);
138
139                         if (ev_window_is_empty (window)) {
140                                 empty_window = window;
141                                 break;
142                         }
143                 }
144         }
145
146         g_list_free (windows);
147         
148         return empty_window;
149 }
150
151 static EvWindow *
152 ev_application_get_uri_window (EvApplication *application, const char *uri)
153 {
154         EvWindow *uri_window = NULL;
155         GList *windows = gtk_window_list_toplevels ();
156         GList *l;
157
158         g_return_val_if_fail (uri != NULL, NULL);
159
160         for (l = windows; l != NULL; l = l->next) {
161                 if (EV_IS_WINDOW (l->data)) {
162                         EvWindow *window = EV_WINDOW (l->data);
163                         const char *window_uri = ev_window_get_uri (window);
164
165                         if (window_uri && strcmp (window_uri, uri) == 0) {
166                                 uri_window = window;
167                                 break;
168                         }
169                 }
170         }
171
172         g_list_free (windows);
173         
174         return uri_window;
175 }
176
177 gboolean
178 ev_application_open_uri (EvApplication  *application,
179                          const char     *uri,
180                          const char     *page_label,
181                          guint           timestamp,
182                          GError        **error)
183 {
184         EvWindow *new_window;
185
186         g_return_val_if_fail (uri != NULL, FALSE);
187
188         new_window = ev_application_get_uri_window (application, uri);
189         if (new_window != NULL) {
190                 gtk_window_present_with_time (GTK_WINDOW (new_window),
191                                               timestamp);
192                 
193                 return TRUE;
194         }
195
196         new_window = ev_application_get_empty_window (application);
197
198         if (new_window == NULL) {
199                 new_window = EV_WINDOW (ev_window_new ());
200                 gtk_widget_show (GTK_WIDGET (new_window));
201         }
202         
203         ev_window_open_uri (new_window, uri);
204
205         gtk_window_present_with_time (GTK_WINDOW (new_window), timestamp);
206
207         if (page_label != NULL) {
208                 ev_window_open_page_label (new_window, page_label);
209         }
210
211         return TRUE;
212 }
213
214 void
215 ev_application_open_uri_list (EvApplication *application,
216                               GSList        *uri_list,
217                               guint          timestamp)
218 {
219         GSList *l;
220
221         for (l = uri_list; l != NULL; l = l->next) {
222                 ev_application_open_uri (application, (char *)l->data,
223                                          NULL,
224                                          timestamp,
225                                          NULL);
226         }
227 }
228
229 void
230 ev_application_shutdown (EvApplication *application)
231 {
232         if (application->toolbars_model) {
233                 g_object_unref (application->toolbars_model);
234                 g_free (application->toolbars_file);
235                 application->toolbars_model = NULL;
236                 application->toolbars_file = NULL;
237         }
238
239         if (application->recent_model) {
240                 g_object_unref (application->recent_model);
241                 application->recent_model = NULL;
242         }
243
244         g_object_unref (application);
245         gtk_main_quit ();
246 }
247
248 static void
249 ev_application_class_init (EvApplicationClass *ev_application_class)
250 {
251 }
252
253 static void
254 ev_application_init (EvApplication *ev_application)
255 {
256         ev_application->toolbars_model = egg_toolbars_model_new ();
257
258         ev_application->toolbars_file = g_build_filename
259                         (ev_dot_dir (), "evince_toolbar.xml", NULL);
260
261         if (!egg_toolbars_model_load (ev_application->toolbars_model,
262                                       ev_application->toolbars_file)) {
263                 egg_toolbars_model_load (ev_application->toolbars_model,
264                                          DATADIR"/evince-toolbar.xml");
265         }
266
267         egg_toolbars_model_set_flags (ev_application->toolbars_model, 0,
268                                       EGG_TB_MODEL_NOT_REMOVABLE); 
269                                       
270         ev_application->recent_model = egg_recent_model_new (EGG_RECENT_MODEL_SORT_MRU);
271         /* FIXME we should add a mime type filter but current eggrecent
272            has only a varargs style api which does not work well when
273            the list of mime types is dynamic */
274         egg_recent_model_set_limit (ev_application->recent_model, 5);   
275         egg_recent_model_set_filter_groups (ev_application->recent_model,
276                                             "Evince", NULL);
277 }
278
279 EggToolbarsModel *ev_application_get_toolbars_model (EvApplication *application)
280 {
281         return application->toolbars_model;
282 }
283
284 EggRecentModel *ev_application_get_recent_model (EvApplication *application)
285 {
286         return application->recent_model;
287 }
288
289 void ev_application_save_toolbars_model (EvApplication *application)
290 {
291         egg_toolbars_model_save (application->toolbars_model,
292                                  application->toolbars_file, "1.0");
293 }
294
295