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