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);
49 ev_document_init (EvDocument *document)
54 ev_document_class_init (EvDocumentClass *klass)
56 klass->get_page = ev_document_impl_get_page;
60 ev_document_get_doc_mutex (void)
62 if (ev_doc_mutex == NULL) {
63 ev_doc_mutex = g_mutex_new ();
69 ev_document_doc_mutex_lock (void)
71 g_mutex_lock (ev_document_get_doc_mutex ());
75 ev_document_doc_mutex_unlock (void)
77 g_mutex_unlock (ev_document_get_doc_mutex ());
81 ev_document_doc_mutex_trylock (void)
83 return g_mutex_trylock (ev_document_get_doc_mutex ());
87 ev_document_get_fc_mutex (void)
89 if (ev_fc_mutex == NULL) {
90 ev_fc_mutex = g_mutex_new ();
96 ev_document_fc_mutex_lock (void)
98 g_mutex_lock (ev_document_get_fc_mutex ());
102 ev_document_fc_mutex_unlock (void)
104 g_mutex_unlock (ev_document_get_fc_mutex ());
108 ev_document_fc_mutex_trylock (void)
110 return g_mutex_trylock (ev_document_get_fc_mutex ());
115 * @document: a #EvDocument
116 * @uri: the document's URI
117 * @error: a #GError location to store an error, or %NULL
119 * Loads @document from @uri.
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
128 * Returns: %TRUE on success, or %FALSE on failure.
131 ev_document_load (EvDocument *document,
135 EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
139 retval = klass->load (document, uri, &err);
142 g_propagate_error (error, err);
144 g_warning ("%s::EvDocument::load returned FALSE but did not fill in @error; fix the backend!\n",
145 G_OBJECT_TYPE_NAME (document));
147 /* So upper layers don't crash */
148 g_set_error_literal (error,
150 EV_DOCUMENT_ERROR_INVALID,
151 "Internal error in backend");
161 * @uri: the target URI
162 * @error: a #GError location to store an error, or %NULL
164 * Saves @document to @uri.
166 * Returns: %TRUE on success, or %FALSE on error with @error filled in
169 ev_document_save (EvDocument *document,
173 EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
175 return klass->save (document, uri, error);
179 ev_document_get_n_pages (EvDocument *document)
181 EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
183 return klass->get_n_pages (document);
187 ev_document_get_page (EvDocument *document,
190 EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
192 return klass->get_page (document, index);
196 ev_document_get_page_size (EvDocument *document,
201 EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
203 klass->get_page_size (document, page, width, height);
207 ev_document_get_page_label (EvDocument *document,
210 EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
212 return klass->get_page_label ?
213 klass->get_page_label (document, page) : NULL;
217 ev_document_get_info (EvDocument *document)
219 EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
221 return klass->get_info (document);
225 ev_document_render (EvDocument *document,
228 EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
230 return klass->render (document, rc);
234 EV_DEFINE_BOXED_TYPE (EvDocumentInfo, ev_document_info, ev_document_info_copy, ev_document_info_free)
237 ev_document_info_copy (EvDocumentInfo *info)
239 EvDocumentInfo *copy;
241 g_return_val_if_fail (info != NULL, NULL);
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);
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;
267 ev_document_info_free (EvDocumentInfo *info)
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);
286 EV_DEFINE_BOXED_TYPE (EvRectangle, ev_rectangle, ev_rectangle_copy, ev_rectangle_free)
289 ev_rectangle_new (void)
291 return g_new0 (EvRectangle, 1);
295 ev_rectangle_copy (EvRectangle *rectangle)
297 EvRectangle *new_rectangle;
299 g_return_val_if_fail (rectangle != NULL, NULL);
301 new_rectangle = g_new (EvRectangle, 1);
302 *new_rectangle = *rectangle;
304 return new_rectangle;
308 ev_rectangle_free (EvRectangle *rectangle)
313 /* Compares two rects. returns 0 if they're equal */
314 #define EPSILON 0.0000001
317 ev_rect_cmp (EvRectangle *a,
322 if (a == NULL || b == NULL)
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));