]> www.fi.muni.cz Git - evince.git/blob - libdocument/ev-document.c
Add code to catch backends incorrectly implementing the load vfunc.
[evince.git] / libdocument / ev-document.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; c-indent-level: 8 -*- */
2 /*
3  *  Copyright (C) 2004 Marco Pesenti Gritti
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2, or (at your option)
8  *  any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  *
19  */
20
21 #include "config.h"
22
23 #include "ev-document.h"
24
25 GMutex *ev_doc_mutex = NULL;
26 GMutex *ev_fc_mutex = NULL;
27
28 EV_DEFINE_INTERFACE (EvDocument, ev_document, G_TYPE_OBJECT)
29
30 GQuark
31 ev_document_error_quark (void)
32 {
33   static GQuark q = 0;
34   if (q == 0)
35     q = g_quark_from_static_string ("ev-document-error-quark");
36
37   return q;
38 }
39
40 static void
41 ev_document_class_init (EvDocumentIface *klass)
42 {
43 }
44
45 GMutex *
46 ev_document_get_doc_mutex (void)
47 {
48         if (ev_doc_mutex == NULL) {
49                 ev_doc_mutex = g_mutex_new ();
50         }
51         return ev_doc_mutex;
52 }
53
54 void
55 ev_document_doc_mutex_lock (void)
56 {
57         g_mutex_lock (ev_document_get_doc_mutex ());
58 }
59
60 void
61 ev_document_doc_mutex_unlock (void)
62 {
63         g_mutex_unlock (ev_document_get_doc_mutex ());
64 }
65
66 gboolean
67 ev_document_doc_mutex_trylock (void)
68 {
69         return g_mutex_trylock (ev_document_get_doc_mutex ());
70 }
71
72 GMutex *
73 ev_document_get_fc_mutex (void)
74 {
75         if (ev_fc_mutex == NULL) {
76                 ev_fc_mutex = g_mutex_new ();
77         }
78         return ev_fc_mutex;
79 }
80
81 void
82 ev_document_fc_mutex_lock (void)
83 {
84         g_mutex_lock (ev_document_get_fc_mutex ());
85 }
86
87 void
88 ev_document_fc_mutex_unlock (void)
89 {
90         g_mutex_unlock (ev_document_get_fc_mutex ());
91 }
92
93 gboolean
94 ev_document_fc_mutex_trylock (void)
95 {
96         return g_mutex_trylock (ev_document_get_fc_mutex ());
97 }
98
99 /**
100  * ev_document_load:
101  * @document: a #EvDocument
102  * @uri: the document's URI
103  * @error: a #GError location to store an error, or %NULL
104  *
105  * Loads @document from @uri.
106  * 
107  * On failure, %FALSE is returned and @error is filled in.
108  * If the document is encrypted, EV_DEFINE_ERROR_ENCRYPTED is returned.
109  * If the backend cannot load the specific document, EV_DOCUMENT_ERROR_INVALID
110  * is returned. Other errors are possible too, depending on the backend
111  * used to load the document and the URI, e.g. #GIOError, #GFileError, and
112  * #GConvertError.
113  *
114  * Returns: %TRUE on success, or %FALSE on failure.
115  */
116 gboolean
117 ev_document_load (EvDocument  *document,
118                   const char  *uri,
119                   GError     **error)
120 {
121         EvDocumentIface *iface = EV_DOCUMENT_GET_IFACE (document);
122         gboolean retval;
123         GError *err = NULL;
124
125         retval = iface->load (document, uri, &err);
126         if (!retval) {
127                 if (err) {
128                         g_propagate_error (error, err);
129                 } else {
130                         g_warning ("%s::EvDocumentIface::load returned FALSE but did not fill in @error; fix the backend!\n",
131                                    G_OBJECT_TYPE_NAME (document));
132
133                         /* So upper layers don't crash */
134                         g_set_error_literal (error,
135                                              EV_DOCUMENT_ERROR,
136                                              EV_DOCUMENT_ERROR_INVALID,
137                                              "Internal error in backend");
138                 }
139         }
140
141         return retval;
142 }
143
144 gboolean
145 ev_document_save (EvDocument  *document,
146                   const char  *uri,
147                   GError     **error)
148 {
149         EvDocumentIface *iface = EV_DOCUMENT_GET_IFACE (document);
150         gboolean retval;
151
152         retval = iface->save (document, uri, error);
153
154         return retval;
155 }
156
157 int
158 ev_document_get_n_pages (EvDocument  *document)
159 {
160         EvDocumentIface *iface = EV_DOCUMENT_GET_IFACE (document);
161         gint retval;
162
163         retval = iface->get_n_pages (document);
164
165         return retval;
166 }
167
168 EvPage *
169 ev_document_get_page (EvDocument *document,
170                       gint        index)
171 {
172         EvDocumentIface *iface = EV_DOCUMENT_GET_IFACE (document);
173         EvPage *retval;
174
175         if (iface->get_page)
176                 retval = iface->get_page (document, index);
177         else
178                 retval = ev_page_new (index);
179
180         return retval;
181 }
182
183 void
184 ev_document_get_page_size (EvDocument *document,
185                            EvPage     *page,
186                            double     *width,
187                            double     *height)
188 {
189         EvDocumentIface *iface = EV_DOCUMENT_GET_IFACE (document);
190
191         iface->get_page_size (document, page, width, height);
192 }
193
194 char *
195 ev_document_get_page_label (EvDocument *document,
196                             EvPage     *page)
197 {
198         EvDocumentIface *iface = EV_DOCUMENT_GET_IFACE (document);
199
200         if (iface->get_page_label == NULL)
201                 return NULL;
202
203         return iface->get_page_label (document, page);
204 }
205
206 EvDocumentInfo *
207 ev_document_get_info (EvDocument *document)
208 {
209         EvDocumentIface *iface = EV_DOCUMENT_GET_IFACE (document);
210
211         return iface->get_info (document);
212 }
213
214 gboolean
215 ev_document_has_attachments (EvDocument *document)
216 {
217         EvDocumentIface *iface = EV_DOCUMENT_GET_IFACE (document);
218
219         if (iface->has_attachments == NULL)
220                 return FALSE;
221         
222         return iface->has_attachments (document);
223 }
224
225 GList *
226 ev_document_get_attachments (EvDocument *document)
227 {
228         EvDocumentIface *iface = EV_DOCUMENT_GET_IFACE (document);
229         GList *retval;
230
231         if (iface->get_attachments == NULL)
232                 return NULL;
233         retval = iface->get_attachments (document);
234
235         return retval;
236 }
237
238 cairo_surface_t *
239 ev_document_render (EvDocument      *document,
240                     EvRenderContext *rc)
241 {
242         EvDocumentIface *iface = EV_DOCUMENT_GET_IFACE (document);
243         cairo_surface_t *retval;
244
245         g_assert (iface->render);
246
247         retval = iface->render (document, rc);
248
249         return retval;
250 }
251
252 /* EvDocumentInfo */
253 EV_DEFINE_BOXED_TYPE (EvDocumentInfo, ev_document_info, ev_document_info_copy, ev_document_info_free)
254
255 EvDocumentInfo *
256 ev_document_info_copy (EvDocumentInfo *info)
257 {
258         EvDocumentInfo *copy;
259         
260         g_return_val_if_fail (info != NULL, NULL);
261
262         copy = g_new0 (EvDocumentInfo, 1);
263         copy->title = g_strdup (info->title);
264         copy->format = g_strdup (info->format);
265         copy->author = g_strdup (info->author);
266         copy->subject = g_strdup (info->subject);
267         copy->keywords = g_strdup (info->keywords);
268         copy->security = g_strdup (info->security);
269         copy->creator = g_strdup (info->creator);
270         copy->producer = g_strdup (info->producer);
271         copy->linearized = g_strdup (info->linearized);
272         
273         copy->creation_date = info->creation_date;
274         copy->modified_date = info->modified_date;
275         copy->layout = info->layout;
276         copy->mode = info->mode;
277         copy->ui_hints = info->ui_hints;
278         copy->permissions = info->permissions;
279         copy->n_pages = info->n_pages;
280         copy->fields_mask = info->fields_mask;
281
282         return copy;
283 }
284
285 void
286 ev_document_info_free (EvDocumentInfo *info)
287 {
288         if (info == NULL)
289                 return;
290
291         g_free (info->title);
292         g_free (info->format);
293         g_free (info->author);
294         g_free (info->subject);
295         g_free (info->keywords);
296         g_free (info->creator);
297         g_free (info->producer);
298         g_free (info->linearized);
299         g_free (info->security);
300         
301         g_free (info);
302 }
303
304
305 /* Compares two rects.  returns 0 if they're equal */
306 #define EPSILON 0.0000001
307
308 gint
309 ev_rect_cmp (EvRectangle *a,
310              EvRectangle *b)
311 {
312         if (a == b)
313                 return 0;
314         if (a == NULL || b == NULL)
315                 return 1;
316
317         return ! ((ABS (a->x1 - b->x1) < EPSILON) &&
318                   (ABS (a->y1 - b->y1) < EPSILON) &&
319                   (ABS (a->x2 - b->x2) < EPSILON) &&
320                   (ABS (a->y2 - b->y2) < EPSILON));
321 }