]> www.fi.muni.cz Git - evince.git/blob - libdocument/ev-document.c
build: Add some more $(AM_V_GEN)s
[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 /**
145  * ev_document_save:
146  * @document:
147  * @uri: the target URI
148  * @error: a #GError location to store an error, or %NULL
149  *
150  * Saves @document to @uri.
151  * 
152  * Returns: %TRUE on success, or %FALSE on error with @error filled in
153  */
154 gboolean
155 ev_document_save (EvDocument  *document,
156                   const char  *uri,
157                   GError     **error)
158 {
159         EvDocumentIface *iface = EV_DOCUMENT_GET_IFACE (document);
160         gboolean retval;
161
162         retval = iface->save (document, uri, error);
163
164         return retval;
165 }
166
167 int
168 ev_document_get_n_pages (EvDocument  *document)
169 {
170         EvDocumentIface *iface = EV_DOCUMENT_GET_IFACE (document);
171         gint retval;
172
173         retval = iface->get_n_pages (document);
174
175         return retval;
176 }
177
178 EvPage *
179 ev_document_get_page (EvDocument *document,
180                       gint        index)
181 {
182         EvDocumentIface *iface = EV_DOCUMENT_GET_IFACE (document);
183         EvPage *retval;
184
185         if (iface->get_page)
186                 retval = iface->get_page (document, index);
187         else
188                 retval = ev_page_new (index);
189
190         return retval;
191 }
192
193 void
194 ev_document_get_page_size (EvDocument *document,
195                            EvPage     *page,
196                            double     *width,
197                            double     *height)
198 {
199         EvDocumentIface *iface = EV_DOCUMENT_GET_IFACE (document);
200
201         iface->get_page_size (document, page, width, height);
202 }
203
204 char *
205 ev_document_get_page_label (EvDocument *document,
206                             EvPage     *page)
207 {
208         EvDocumentIface *iface = EV_DOCUMENT_GET_IFACE (document);
209
210         if (iface->get_page_label == NULL)
211                 return NULL;
212
213         return iface->get_page_label (document, page);
214 }
215
216 EvDocumentInfo *
217 ev_document_get_info (EvDocument *document)
218 {
219         EvDocumentIface *iface = EV_DOCUMENT_GET_IFACE (document);
220
221         return iface->get_info (document);
222 }
223
224 gboolean
225 ev_document_has_attachments (EvDocument *document)
226 {
227         EvDocumentIface *iface = EV_DOCUMENT_GET_IFACE (document);
228
229         if (iface->has_attachments == NULL)
230                 return FALSE;
231         
232         return iface->has_attachments (document);
233 }
234
235 GList *
236 ev_document_get_attachments (EvDocument *document)
237 {
238         EvDocumentIface *iface = EV_DOCUMENT_GET_IFACE (document);
239         GList *retval;
240
241         if (iface->get_attachments == NULL)
242                 return NULL;
243         retval = iface->get_attachments (document);
244
245         return retval;
246 }
247
248 cairo_surface_t *
249 ev_document_render (EvDocument      *document,
250                     EvRenderContext *rc)
251 {
252         EvDocumentIface *iface = EV_DOCUMENT_GET_IFACE (document);
253         cairo_surface_t *retval;
254
255         g_assert (iface->render);
256
257         retval = iface->render (document, rc);
258
259         return retval;
260 }
261
262 /* EvDocumentInfo */
263 EV_DEFINE_BOXED_TYPE (EvDocumentInfo, ev_document_info, ev_document_info_copy, ev_document_info_free)
264
265 EvDocumentInfo *
266 ev_document_info_copy (EvDocumentInfo *info)
267 {
268         EvDocumentInfo *copy;
269         
270         g_return_val_if_fail (info != NULL, NULL);
271
272         copy = g_new0 (EvDocumentInfo, 1);
273         copy->title = g_strdup (info->title);
274         copy->format = g_strdup (info->format);
275         copy->author = g_strdup (info->author);
276         copy->subject = g_strdup (info->subject);
277         copy->keywords = g_strdup (info->keywords);
278         copy->security = g_strdup (info->security);
279         copy->creator = g_strdup (info->creator);
280         copy->producer = g_strdup (info->producer);
281         copy->linearized = g_strdup (info->linearized);
282         
283         copy->creation_date = info->creation_date;
284         copy->modified_date = info->modified_date;
285         copy->layout = info->layout;
286         copy->mode = info->mode;
287         copy->ui_hints = info->ui_hints;
288         copy->permissions = info->permissions;
289         copy->n_pages = info->n_pages;
290         copy->fields_mask = info->fields_mask;
291
292         return copy;
293 }
294
295 void
296 ev_document_info_free (EvDocumentInfo *info)
297 {
298         if (info == NULL)
299                 return;
300
301         g_free (info->title);
302         g_free (info->format);
303         g_free (info->author);
304         g_free (info->subject);
305         g_free (info->keywords);
306         g_free (info->creator);
307         g_free (info->producer);
308         g_free (info->linearized);
309         g_free (info->security);
310         
311         g_free (info);
312 }
313
314 /* EvRectangle */
315 EV_DEFINE_BOXED_TYPE (EvRectangle, ev_rectangle, ev_rectangle_copy, ev_rectangle_free)
316
317 EvRectangle *
318 ev_rectangle_new (void)
319 {
320         return g_new0 (EvRectangle, 1);
321 }
322
323 EvRectangle *
324 ev_rectangle_copy (EvRectangle *rectangle)
325 {
326         EvRectangle *new_rectangle;
327
328         g_return_val_if_fail (rectangle != NULL, NULL);
329
330         new_rectangle = g_new (EvRectangle, 1);
331         *new_rectangle = *rectangle;
332
333         return new_rectangle;
334 }
335
336 void
337 ev_rectangle_free (EvRectangle *rectangle)
338 {
339         g_free (rectangle);
340 }
341
342 /* Compares two rects.  returns 0 if they're equal */
343 #define EPSILON 0.0000001
344
345 gint
346 ev_rect_cmp (EvRectangle *a,
347              EvRectangle *b)
348 {
349         if (a == b)
350                 return 0;
351         if (a == NULL || b == NULL)
352                 return 1;
353
354         return ! ((ABS (a->x1 - b->x1) < EPSILON) &&
355                   (ABS (a->y1 - b->y1) < EPSILON) &&
356                   (ABS (a->x2 - b->x2) < EPSILON) &&
357                   (ABS (a->y2 - b->y2) < EPSILON));
358 }