1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; c-indent-level: 8 -*- */
3 * Copyright (C) 2009 Carlos Garcia Campos
4 * Copyright (C) 2004 Marco Pesenti Gritti
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)
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.
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.
24 #include "ev-document.h"
26 GMutex *ev_doc_mutex = NULL;
27 GMutex *ev_fc_mutex = NULL;
29 G_DEFINE_ABSTRACT_TYPE (EvDocument, ev_document, G_TYPE_OBJECT)
32 ev_document_error_quark (void)
36 q = g_quark_from_static_string ("ev-document-error-quark");
42 ev_document_impl_get_page (EvDocument *document,
45 return ev_page_new (index);
48 static EvDocumentInfo *
49 ev_document_impl_get_info (EvDocument *document)
51 return g_new0 (EvDocumentInfo, 1);
55 ev_document_init (EvDocument *document)
60 ev_document_class_init (EvDocumentClass *klass)
62 klass->get_page = ev_document_impl_get_page;
63 klass->get_info = ev_document_impl_get_info;
67 ev_document_get_doc_mutex (void)
69 if (ev_doc_mutex == NULL) {
70 ev_doc_mutex = g_mutex_new ();
76 ev_document_doc_mutex_lock (void)
78 g_mutex_lock (ev_document_get_doc_mutex ());
82 ev_document_doc_mutex_unlock (void)
84 g_mutex_unlock (ev_document_get_doc_mutex ());
88 ev_document_doc_mutex_trylock (void)
90 return g_mutex_trylock (ev_document_get_doc_mutex ());
94 ev_document_get_fc_mutex (void)
96 if (ev_fc_mutex == NULL) {
97 ev_fc_mutex = g_mutex_new ();
103 ev_document_fc_mutex_lock (void)
105 g_mutex_lock (ev_document_get_fc_mutex ());
109 ev_document_fc_mutex_unlock (void)
111 g_mutex_unlock (ev_document_get_fc_mutex ());
115 ev_document_fc_mutex_trylock (void)
117 return g_mutex_trylock (ev_document_get_fc_mutex ());
122 * @document: a #EvDocument
123 * @uri: the document's URI
124 * @error: a #GError location to store an error, or %NULL
126 * Loads @document from @uri.
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
135 * Returns: %TRUE on success, or %FALSE on failure.
138 ev_document_load (EvDocument *document,
142 EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
146 retval = klass->load (document, uri, &err);
149 g_propagate_error (error, err);
151 g_warning ("%s::EvDocument::load returned FALSE but did not fill in @error; fix the backend!\n",
152 G_OBJECT_TYPE_NAME (document));
154 /* So upper layers don't crash */
155 g_set_error_literal (error,
157 EV_DOCUMENT_ERROR_INVALID,
158 "Internal error in backend");
168 * @uri: the target URI
169 * @error: a #GError location to store an error, or %NULL
171 * Saves @document to @uri.
173 * Returns: %TRUE on success, or %FALSE on error with @error filled in
176 ev_document_save (EvDocument *document,
180 EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
182 return klass->save (document, uri, error);
186 ev_document_get_n_pages (EvDocument *document)
188 EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
190 return klass->get_n_pages (document);
194 ev_document_get_page (EvDocument *document,
197 EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
199 return klass->get_page (document, index);
203 ev_document_get_page_size (EvDocument *document,
208 EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
210 klass->get_page_size (document, page, width, height);
214 ev_document_get_page_label (EvDocument *document,
217 EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
219 return klass->get_page_label ?
220 klass->get_page_label (document, page) : NULL;
224 ev_document_get_info (EvDocument *document)
226 EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
228 return klass->get_info (document);
232 ev_document_render (EvDocument *document,
235 EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
237 return klass->render (document, rc);
241 EV_DEFINE_BOXED_TYPE (EvDocumentInfo, ev_document_info, ev_document_info_copy, ev_document_info_free)
244 ev_document_info_copy (EvDocumentInfo *info)
246 EvDocumentInfo *copy;
248 g_return_val_if_fail (info != NULL, NULL);
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);
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;
274 ev_document_info_free (EvDocumentInfo *info)
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);
293 EV_DEFINE_BOXED_TYPE (EvRectangle, ev_rectangle, ev_rectangle_copy, ev_rectangle_free)
296 ev_rectangle_new (void)
298 return g_new0 (EvRectangle, 1);
302 ev_rectangle_copy (EvRectangle *rectangle)
304 EvRectangle *new_rectangle;
306 g_return_val_if_fail (rectangle != NULL, NULL);
308 new_rectangle = g_new (EvRectangle, 1);
309 *new_rectangle = *rectangle;
311 return new_rectangle;
315 ev_rectangle_free (EvRectangle *rectangle)
320 /* Compares two rects. returns 0 if they're equal */
321 #define EPSILON 0.0000001
324 ev_rect_cmp (EvRectangle *a,
329 if (a == NULL || b == NULL)
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));