]> www.fi.muni.cz Git - evince.git/blob - libdocument/ev-document.c
More docs
[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
124         retval = iface->load (document, uri, error);
125
126         return retval;
127 }
128
129 gboolean
130 ev_document_save (EvDocument  *document,
131                   const char  *uri,
132                   GError     **error)
133 {
134         EvDocumentIface *iface = EV_DOCUMENT_GET_IFACE (document);
135         gboolean retval;
136
137         retval = iface->save (document, uri, error);
138
139         return retval;
140 }
141
142 int
143 ev_document_get_n_pages (EvDocument  *document)
144 {
145         EvDocumentIface *iface = EV_DOCUMENT_GET_IFACE (document);
146         gint retval;
147
148         retval = iface->get_n_pages (document);
149
150         return retval;
151 }
152
153 EvPage *
154 ev_document_get_page (EvDocument *document,
155                       gint        index)
156 {
157         EvDocumentIface *iface = EV_DOCUMENT_GET_IFACE (document);
158         EvPage *retval;
159
160         if (iface->get_page)
161                 retval = iface->get_page (document, index);
162         else
163                 retval = ev_page_new (index);
164
165         return retval;
166 }
167
168 void
169 ev_document_get_page_size (EvDocument *document,
170                            EvPage     *page,
171                            double     *width,
172                            double     *height)
173 {
174         EvDocumentIface *iface = EV_DOCUMENT_GET_IFACE (document);
175
176         iface->get_page_size (document, page, width, height);
177 }
178
179 char *
180 ev_document_get_page_label (EvDocument *document,
181                             EvPage     *page)
182 {
183         EvDocumentIface *iface = EV_DOCUMENT_GET_IFACE (document);
184
185         if (iface->get_page_label == NULL)
186                 return NULL;
187
188         return iface->get_page_label (document, page);
189 }
190
191 EvDocumentInfo *
192 ev_document_get_info (EvDocument *document)
193 {
194         EvDocumentIface *iface = EV_DOCUMENT_GET_IFACE (document);
195
196         return iface->get_info (document);
197 }
198
199 gboolean
200 ev_document_has_attachments (EvDocument *document)
201 {
202         EvDocumentIface *iface = EV_DOCUMENT_GET_IFACE (document);
203
204         if (iface->has_attachments == NULL)
205                 return FALSE;
206         
207         return iface->has_attachments (document);
208 }
209
210 GList *
211 ev_document_get_attachments (EvDocument *document)
212 {
213         EvDocumentIface *iface = EV_DOCUMENT_GET_IFACE (document);
214         GList *retval;
215
216         if (iface->get_attachments == NULL)
217                 return NULL;
218         retval = iface->get_attachments (document);
219
220         return retval;
221 }
222
223 cairo_surface_t *
224 ev_document_render (EvDocument      *document,
225                     EvRenderContext *rc)
226 {
227         EvDocumentIface *iface = EV_DOCUMENT_GET_IFACE (document);
228         cairo_surface_t *retval;
229
230         g_assert (iface->render);
231
232         retval = iface->render (document, rc);
233
234         return retval;
235 }
236
237 /* EvDocumentInfo */
238 EV_DEFINE_BOXED_TYPE (EvDocumentInfo, ev_document_info, ev_document_info_copy, ev_document_info_free)
239
240 EvDocumentInfo *
241 ev_document_info_copy (EvDocumentInfo *info)
242 {
243         EvDocumentInfo *copy;
244         
245         g_return_val_if_fail (info != NULL, NULL);
246
247         copy = g_new0 (EvDocumentInfo, 1);
248         copy->title = g_strdup (info->title);
249         copy->format = g_strdup (info->format);
250         copy->author = g_strdup (info->author);
251         copy->subject = g_strdup (info->subject);
252         copy->keywords = g_strdup (info->keywords);
253         copy->security = g_strdup (info->security);
254         copy->creator = g_strdup (info->creator);
255         copy->producer = g_strdup (info->producer);
256         copy->linearized = g_strdup (info->linearized);
257         
258         copy->creation_date = info->creation_date;
259         copy->modified_date = info->modified_date;
260         copy->layout = info->layout;
261         copy->mode = info->mode;
262         copy->ui_hints = info->ui_hints;
263         copy->permissions = info->permissions;
264         copy->n_pages = info->n_pages;
265         copy->fields_mask = info->fields_mask;
266
267         return copy;
268 }
269
270 void
271 ev_document_info_free (EvDocumentInfo *info)
272 {
273         if (info == NULL)
274                 return;
275
276         g_free (info->title);
277         g_free (info->format);
278         g_free (info->author);
279         g_free (info->subject);
280         g_free (info->keywords);
281         g_free (info->creator);
282         g_free (info->producer);
283         g_free (info->linearized);
284         g_free (info->security);
285         
286         g_free (info);
287 }
288
289
290 /* Compares two rects.  returns 0 if they're equal */
291 #define EPSILON 0.0000001
292
293 gint
294 ev_rect_cmp (EvRectangle *a,
295              EvRectangle *b)
296 {
297         if (a == b)
298                 return 0;
299         if (a == NULL || b == NULL)
300                 return 1;
301
302         return ! ((ABS (a->x1 - b->x1) < EPSILON) &&
303                   (ABS (a->y1 - b->y1) < EPSILON) &&
304                   (ABS (a->x2 - b->x2) < EPSILON) &&
305                   (ABS (a->y2 - b->y2) < EPSILON));
306 }