]> www.fi.muni.cz Git - evince.git/blob - libdocument/ev-document.c
66d7d46e001da3811abe30455876db00f9f5cae0
[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 void
49 ev_document_init (EvDocument *document)
50 {
51 }
52
53 static void
54 ev_document_class_init (EvDocumentClass *klass)
55 {
56         klass->get_page = ev_document_impl_get_page;
57 }
58
59 GMutex *
60 ev_document_get_doc_mutex (void)
61 {
62         if (ev_doc_mutex == NULL) {
63                 ev_doc_mutex = g_mutex_new ();
64         }
65         return ev_doc_mutex;
66 }
67
68 void
69 ev_document_doc_mutex_lock (void)
70 {
71         g_mutex_lock (ev_document_get_doc_mutex ());
72 }
73
74 void
75 ev_document_doc_mutex_unlock (void)
76 {
77         g_mutex_unlock (ev_document_get_doc_mutex ());
78 }
79
80 gboolean
81 ev_document_doc_mutex_trylock (void)
82 {
83         return g_mutex_trylock (ev_document_get_doc_mutex ());
84 }
85
86 GMutex *
87 ev_document_get_fc_mutex (void)
88 {
89         if (ev_fc_mutex == NULL) {
90                 ev_fc_mutex = g_mutex_new ();
91         }
92         return ev_fc_mutex;
93 }
94
95 void
96 ev_document_fc_mutex_lock (void)
97 {
98         g_mutex_lock (ev_document_get_fc_mutex ());
99 }
100
101 void
102 ev_document_fc_mutex_unlock (void)
103 {
104         g_mutex_unlock (ev_document_get_fc_mutex ());
105 }
106
107 gboolean
108 ev_document_fc_mutex_trylock (void)
109 {
110         return g_mutex_trylock (ev_document_get_fc_mutex ());
111 }
112
113 /**
114  * ev_document_load:
115  * @document: a #EvDocument
116  * @uri: the document's URI
117  * @error: a #GError location to store an error, or %NULL
118  *
119  * Loads @document from @uri.
120  * 
121  * On failure, %FALSE is returned and @error is filled in.
122  * If the document is encrypted, EV_DEFINE_ERROR_ENCRYPTED is returned.
123  * If the backend cannot load the specific document, EV_DOCUMENT_ERROR_INVALID
124  * is returned. Other errors are possible too, depending on the backend
125  * used to load the document and the URI, e.g. #GIOError, #GFileError, and
126  * #GConvertError.
127  *
128  * Returns: %TRUE on success, or %FALSE on failure.
129  */
130 gboolean
131 ev_document_load (EvDocument  *document,
132                   const char  *uri,
133                   GError     **error)
134 {
135         EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
136         gboolean retval;
137         GError *err = NULL;
138
139         retval = klass->load (document, uri, &err);
140         if (!retval) {
141                 if (err) {
142                         g_propagate_error (error, err);
143                 } else {
144                         g_warning ("%s::EvDocument::load returned FALSE but did not fill in @error; fix the backend!\n",
145                                    G_OBJECT_TYPE_NAME (document));
146
147                         /* So upper layers don't crash */
148                         g_set_error_literal (error,
149                                              EV_DOCUMENT_ERROR,
150                                              EV_DOCUMENT_ERROR_INVALID,
151                                              "Internal error in backend");
152                 }
153         }
154
155         return retval;
156 }
157
158 /**
159  * ev_document_save:
160  * @document:
161  * @uri: the target URI
162  * @error: a #GError location to store an error, or %NULL
163  *
164  * Saves @document to @uri.
165  * 
166  * Returns: %TRUE on success, or %FALSE on error with @error filled in
167  */
168 gboolean
169 ev_document_save (EvDocument  *document,
170                   const char  *uri,
171                   GError     **error)
172 {
173         EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
174
175         return klass->save (document, uri, error);
176 }
177
178 int
179 ev_document_get_n_pages (EvDocument  *document)
180 {
181         EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
182
183         return klass->get_n_pages (document);
184 }
185
186 EvPage *
187 ev_document_get_page (EvDocument *document,
188                       gint        index)
189 {
190         EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
191
192         return klass->get_page (document, index);
193 }
194
195 void
196 ev_document_get_page_size (EvDocument *document,
197                            EvPage     *page,
198                            double     *width,
199                            double     *height)
200 {
201         EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
202
203         klass->get_page_size (document, page, width, height);
204 }
205
206 gchar *
207 ev_document_get_page_label (EvDocument *document,
208                             EvPage     *page)
209 {
210         EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
211
212         return klass->get_page_label ?
213                 klass->get_page_label (document, page) : NULL;
214 }
215
216 EvDocumentInfo *
217 ev_document_get_info (EvDocument *document)
218 {
219         EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
220
221         return klass->get_info (document);
222 }
223
224 cairo_surface_t *
225 ev_document_render (EvDocument      *document,
226                     EvRenderContext *rc)
227 {
228         EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
229
230         return klass->render (document, rc);
231 }
232
233 /* EvDocumentInfo */
234 EV_DEFINE_BOXED_TYPE (EvDocumentInfo, ev_document_info, ev_document_info_copy, ev_document_info_free)
235
236 EvDocumentInfo *
237 ev_document_info_copy (EvDocumentInfo *info)
238 {
239         EvDocumentInfo *copy;
240         
241         g_return_val_if_fail (info != NULL, NULL);
242
243         copy = g_new0 (EvDocumentInfo, 1);
244         copy->title = g_strdup (info->title);
245         copy->format = g_strdup (info->format);
246         copy->author = g_strdup (info->author);
247         copy->subject = g_strdup (info->subject);
248         copy->keywords = g_strdup (info->keywords);
249         copy->security = g_strdup (info->security);
250         copy->creator = g_strdup (info->creator);
251         copy->producer = g_strdup (info->producer);
252         copy->linearized = g_strdup (info->linearized);
253         
254         copy->creation_date = info->creation_date;
255         copy->modified_date = info->modified_date;
256         copy->layout = info->layout;
257         copy->mode = info->mode;
258         copy->ui_hints = info->ui_hints;
259         copy->permissions = info->permissions;
260         copy->n_pages = info->n_pages;
261         copy->fields_mask = info->fields_mask;
262
263         return copy;
264 }
265
266 void
267 ev_document_info_free (EvDocumentInfo *info)
268 {
269         if (info == NULL)
270                 return;
271
272         g_free (info->title);
273         g_free (info->format);
274         g_free (info->author);
275         g_free (info->subject);
276         g_free (info->keywords);
277         g_free (info->creator);
278         g_free (info->producer);
279         g_free (info->linearized);
280         g_free (info->security);
281         
282         g_free (info);
283 }
284
285 /* EvRectangle */
286 EV_DEFINE_BOXED_TYPE (EvRectangle, ev_rectangle, ev_rectangle_copy, ev_rectangle_free)
287
288 EvRectangle *
289 ev_rectangle_new (void)
290 {
291         return g_new0 (EvRectangle, 1);
292 }
293
294 EvRectangle *
295 ev_rectangle_copy (EvRectangle *rectangle)
296 {
297         EvRectangle *new_rectangle;
298
299         g_return_val_if_fail (rectangle != NULL, NULL);
300
301         new_rectangle = g_new (EvRectangle, 1);
302         *new_rectangle = *rectangle;
303
304         return new_rectangle;
305 }
306
307 void
308 ev_rectangle_free (EvRectangle *rectangle)
309 {
310         g_free (rectangle);
311 }
312
313 /* Compares two rects.  returns 0 if they're equal */
314 #define EPSILON 0.0000001
315
316 gint
317 ev_rect_cmp (EvRectangle *a,
318              EvRectangle *b)
319 {
320         if (a == b)
321                 return 0;
322         if (a == NULL || b == NULL)
323                 return 1;
324
325         return ! ((ABS (a->x1 - b->x1) < EPSILON) &&
326                   (ABS (a->y1 - b->y1) < EPSILON) &&
327                   (ABS (a->x2 - b->x2) < EPSILON) &&
328                   (ABS (a->y2 - b->y2) < EPSILON));
329 }