1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; c-indent-level: 8 -*- */
3 * Copyright (C) 2004 Marco Pesenti Gritti
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)
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.
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.
23 #include "ev-document.h"
25 GMutex *ev_doc_mutex = NULL;
26 GMutex *ev_fc_mutex = NULL;
28 EV_DEFINE_INTERFACE (EvDocument, ev_document, G_TYPE_OBJECT)
31 ev_document_error_quark (void)
35 q = g_quark_from_static_string ("ev-document-error-quark");
41 ev_document_class_init (EvDocumentIface *klass)
46 ev_document_get_doc_mutex (void)
48 if (ev_doc_mutex == NULL) {
49 ev_doc_mutex = g_mutex_new ();
55 ev_document_doc_mutex_lock (void)
57 g_mutex_lock (ev_document_get_doc_mutex ());
61 ev_document_doc_mutex_unlock (void)
63 g_mutex_unlock (ev_document_get_doc_mutex ());
67 ev_document_doc_mutex_trylock (void)
69 return g_mutex_trylock (ev_document_get_doc_mutex ());
73 ev_document_get_fc_mutex (void)
75 if (ev_fc_mutex == NULL) {
76 ev_fc_mutex = g_mutex_new ();
82 ev_document_fc_mutex_lock (void)
84 g_mutex_lock (ev_document_get_fc_mutex ());
88 ev_document_fc_mutex_unlock (void)
90 g_mutex_unlock (ev_document_get_fc_mutex ());
94 ev_document_fc_mutex_trylock (void)
96 return g_mutex_trylock (ev_document_get_fc_mutex ());
101 * @document: a #EvDocument
102 * @uri: the document's URI
103 * @error: a #GError location to store an error, or %NULL
105 * Loads @document from @uri.
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
114 * Returns: %TRUE on success, or %FALSE on failure.
117 ev_document_load (EvDocument *document,
121 EvDocumentIface *iface = EV_DOCUMENT_GET_IFACE (document);
125 retval = iface->load (document, uri, &err);
128 g_propagate_error (error, err);
130 g_warning ("%s::EvDocumentIface::load returned FALSE but did not fill in @error; fix the backend!\n",
131 G_OBJECT_TYPE_NAME (document));
133 /* So upper layers don't crash */
134 g_set_error_literal (error,
136 EV_DOCUMENT_ERROR_INVALID,
137 "Internal error in backend");
147 * @uri: the target URI
148 * @error: a #GError location to store an error, or %NULL
150 * Saves @document to @uri.
152 * Returns: %TRUE on success, or %FALSE on error with @error filled in
155 ev_document_save (EvDocument *document,
159 EvDocumentIface *iface = EV_DOCUMENT_GET_IFACE (document);
162 retval = iface->save (document, uri, error);
168 ev_document_get_n_pages (EvDocument *document)
170 EvDocumentIface *iface = EV_DOCUMENT_GET_IFACE (document);
173 retval = iface->get_n_pages (document);
179 ev_document_get_page (EvDocument *document,
182 EvDocumentIface *iface = EV_DOCUMENT_GET_IFACE (document);
186 retval = iface->get_page (document, index);
188 retval = ev_page_new (index);
194 ev_document_get_page_size (EvDocument *document,
199 EvDocumentIface *iface = EV_DOCUMENT_GET_IFACE (document);
201 iface->get_page_size (document, page, width, height);
205 ev_document_get_page_label (EvDocument *document,
208 EvDocumentIface *iface = EV_DOCUMENT_GET_IFACE (document);
210 if (iface->get_page_label == NULL)
213 return iface->get_page_label (document, page);
217 ev_document_get_info (EvDocument *document)
219 EvDocumentIface *iface = EV_DOCUMENT_GET_IFACE (document);
221 return iface->get_info (document);
225 ev_document_has_attachments (EvDocument *document)
227 EvDocumentIface *iface = EV_DOCUMENT_GET_IFACE (document);
229 if (iface->has_attachments == NULL)
232 return iface->has_attachments (document);
236 ev_document_get_attachments (EvDocument *document)
238 EvDocumentIface *iface = EV_DOCUMENT_GET_IFACE (document);
241 if (iface->get_attachments == NULL)
243 retval = iface->get_attachments (document);
249 ev_document_render (EvDocument *document,
252 EvDocumentIface *iface = EV_DOCUMENT_GET_IFACE (document);
253 cairo_surface_t *retval;
255 g_assert (iface->render);
257 retval = iface->render (document, rc);
263 EV_DEFINE_BOXED_TYPE (EvDocumentInfo, ev_document_info, ev_document_info_copy, ev_document_info_free)
266 ev_document_info_copy (EvDocumentInfo *info)
268 EvDocumentInfo *copy;
270 g_return_val_if_fail (info != NULL, NULL);
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);
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;
296 ev_document_info_free (EvDocumentInfo *info)
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);
315 EV_DEFINE_BOXED_TYPE (EvRectangle, ev_rectangle, ev_rectangle_copy, ev_rectangle_free)
318 ev_rectangle_new (void)
320 return g_new0 (EvRectangle, 1);
324 ev_rectangle_copy (EvRectangle *rectangle)
326 EvRectangle *new_rectangle;
328 g_return_val_if_fail (rectangle != NULL, NULL);
330 new_rectangle = g_new (EvRectangle, 1);
331 *new_rectangle = *rectangle;
333 return new_rectangle;
337 ev_rectangle_free (EvRectangle *rectangle)
342 /* Compares two rects. returns 0 if they're equal */
343 #define EPSILON 0.0000001
346 ev_rect_cmp (EvRectangle *a,
351 if (a == NULL || b == NULL)
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));