]> www.fi.muni.cz Git - evince.git/blob - shell/ev-application.c
Marco Pesenti Gritti <mpg@redhat.com>
[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
31 #include <glib.h>
32 #include <glib/gi18n.h>
33 #include <glib-object.h>
34 #include <gtk/gtkfilechooserdialog.h>
35 #include <gtk/gtkstock.h>
36 #include <gtk/gtkwidget.h>
37 #include <gtk/gtkmain.h>
38 #include <string.h>
39
40 #ifdef ENABLE_DBUS
41 #include "ev-application-service.h"
42 #include <dbus/dbus-glib-bindings.h>
43 #endif
44
45 G_DEFINE_TYPE (EvApplication, ev_application, G_TYPE_OBJECT);
46
47 #define EV_APPLICATION_GET_PRIVATE(object) \
48         (G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_APPLICATION, EvApplicationPrivate))
49
50 #define APPLICATION_SERVICE_NAME "org.gnome.evince.ApplicationService"
51
52 #ifdef ENABLE_DBUS
53 gboolean
54 ev_application_register_service (EvApplication *application)
55 {
56         DBusGConnection *connection;
57         DBusGProxy *driver_proxy;
58         GError *err = NULL;
59         guint request_name_result;
60
61         connection = dbus_g_bus_get (DBUS_BUS_STARTER, &err);
62         if (connection == NULL) {
63                 g_warning ("Service registration failed.");
64                 g_error_free (err);
65
66                 return FALSE;
67         }
68
69         driver_proxy = dbus_g_proxy_new_for_name (connection,
70                                                   DBUS_SERVICE_DBUS,
71                                                   DBUS_PATH_DBUS,
72                                                   DBUS_INTERFACE_DBUS);
73
74         if (!org_freedesktop_DBus_request_name (driver_proxy,
75                                                 APPLICATION_SERVICE_NAME,
76                                                 0, &request_name_result, &err)) {
77                 g_warning ("Service registration failed.");
78                 g_clear_error (&err);
79         }
80
81         if (request_name_result == DBUS_REQUEST_NAME_REPLY_EXISTS) {
82                 return FALSE;
83         }
84
85 #if DBUS_VERSION == 33
86         dbus_g_object_class_install_info (G_OBJECT_GET_CLASS (application),
87                                           &dbus_glib_ev_application_object_info);
88 #else
89         dbus_g_object_type_install_info (EV_TYPE_APPLICATION,
90                                          &dbus_glib_ev_application_object_info);
91 #endif
92
93         dbus_g_connection_register_g_object (connection,
94                                              "/org/gnome/evince/Evince",
95                                              G_OBJECT (application));
96
97         return TRUE;
98 }
99 #endif /* ENABLE_DBUS */
100
101 EvApplication *
102 ev_application_get_instance (void)
103 {
104         static EvApplication *instance;
105
106         if (!instance) {
107                 instance = EV_APPLICATION (g_object_new (EV_TYPE_APPLICATION, NULL));
108         }
109
110         return instance;
111 }
112
113 gboolean
114 ev_application_open_window (EvApplication  *application,
115                             GError        **error)
116 {
117         gtk_widget_show (ev_window_new ());
118
119         return TRUE;
120 }
121
122 static EvWindow *
123 ev_application_get_empty_window (EvApplication *application)
124 {
125         EvWindow *empty_window = NULL;
126         GList *windows = gtk_window_list_toplevels ();
127         GList *l;
128
129         for (l = windows; l != NULL; l = l->next) {
130                 if (EV_IS_WINDOW (l->data)) {
131                         EvWindow *window = EV_WINDOW (l->data);
132
133                         if (ev_window_is_empty (window)) {
134                                 empty_window = window;
135                                 break;
136                         }
137                 }
138         }
139
140         g_list_free (windows);
141         
142         return empty_window;
143 }
144
145 static EvWindow *
146 ev_application_get_uri_window (EvApplication *application, const char *uri)
147 {
148         EvWindow *uri_window = NULL;
149         GList *windows = gtk_window_list_toplevels ();
150         GList *l;
151
152         g_return_val_if_fail (uri != NULL, NULL);
153
154         for (l = windows; l != NULL; l = l->next) {
155                 if (EV_IS_WINDOW (l->data)) {
156                         EvWindow *window = EV_WINDOW (l->data);
157                         const char *window_uri = ev_window_get_uri (window);
158
159                         if (window_uri && strcmp (window_uri, uri) == 0) {
160                                 uri_window = window;
161                                 break;
162                         }
163                 }
164         }
165
166         g_list_free (windows);
167         
168         return uri_window;
169 }
170
171 gboolean
172 ev_application_open_uri (EvApplication  *application,
173                          const char     *uri,
174                          const char     *page_label,
175                          GError        **error)
176 {
177         EvWindow *new_window;
178
179         g_return_val_if_fail (uri != NULL, FALSE);
180
181         new_window = ev_application_get_uri_window (application, uri);
182         if (new_window != NULL) {
183                 gtk_window_present (GTK_WINDOW (new_window));
184                 
185                 return TRUE;
186         }
187
188         new_window = ev_application_get_empty_window (application);
189
190         if (new_window == NULL) {
191                 new_window = EV_WINDOW (ev_window_new ());
192         }
193         
194         ev_window_open_uri (new_window, uri);
195
196         gtk_window_present (GTK_WINDOW (new_window));
197
198         if (page_label != NULL) {
199                 ev_window_open_page_label (new_window, page_label);
200         }
201
202         return TRUE;
203 }
204
205 void
206 ev_application_open_uri_list (EvApplication *application, GSList *uri_list)
207 {
208         GSList *l;
209
210         for (l = uri_list; l != NULL; l = l->next) {
211                 ev_application_open_uri (application, (char *)l->data, NULL, NULL);
212         }
213 }
214
215 void
216 ev_application_shutdown (EvApplication *application)
217 {
218         g_object_unref (application);
219         gtk_main_quit ();
220 }
221
222 static void
223 ev_application_class_init (EvApplicationClass *ev_application_class)
224 {
225 }
226
227 static void
228 ev_application_init (EvApplication *ev_application)
229 {
230 }
231