]> www.fi.muni.cz Git - evince.git/blob - libdocument/ev-document.c
5c63b25f9d86bd7f2d84b0bbd1ff796c449d6cab
[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) 2009 Carlos Garcia Campos
4  *  Copyright (C) 2004 Marco Pesenti Gritti
5  *
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)
9  *  any later version.
10  *
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.
15  *
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.
19  *
20  */
21
22 #include "config.h"
23
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include "ev-document.h"
28
29 #define EV_DOCUMENT_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), EV_TYPE_DOCUMENT, EvDocumentPrivate))
30
31 typedef struct _EvPageSize
32 {
33         gdouble width;
34         gdouble height;
35 } EvPageSize;
36
37 struct _EvDocumentPrivate
38 {
39         gint            n_pages;
40
41         gboolean        uniform;
42         gdouble         uniform_width;
43         gdouble         uniform_height;
44
45         gdouble         max_width;
46         gdouble         max_height;
47         gint            max_label;
48
49         gchar         **page_labels;
50         EvPageSize     *page_sizes;
51         EvDocumentInfo *info;
52 };
53
54 static gint            _ev_document_get_n_pages   (EvDocument  *document);
55 static void            _ev_document_get_page_size (EvDocument *document,
56                                                    EvPage     *page,
57                                                    double     *width,
58                                                    double     *height);
59 static gchar          *_ev_document_get_page_label (EvDocument *document,
60                                                     EvPage     *page);
61 static EvDocumentInfo *_ev_document_get_info       (EvDocument *document);
62
63 GMutex *ev_doc_mutex = NULL;
64 GMutex *ev_fc_mutex = NULL;
65
66 G_DEFINE_ABSTRACT_TYPE (EvDocument, ev_document, G_TYPE_OBJECT)
67
68 GQuark
69 ev_document_error_quark (void)
70 {
71   static GQuark q = 0;
72   if (q == 0)
73     q = g_quark_from_static_string ("ev-document-error-quark");
74
75   return q;
76 }
77
78 static EvPage *
79 ev_document_impl_get_page (EvDocument *document,
80                            gint        index)
81 {
82         return ev_page_new (index);
83 }
84
85 static EvDocumentInfo *
86 ev_document_impl_get_info (EvDocument *document)
87 {
88         return g_new0 (EvDocumentInfo, 1);
89 }
90
91 static void
92 ev_document_finalize (GObject *object)
93 {
94         EvDocument *document = EV_DOCUMENT (object);
95
96         if (document->priv->page_sizes) {
97                 g_free (document->priv->page_sizes);
98                 document->priv->page_sizes = NULL;
99         }
100
101         if (document->priv->page_labels) {
102                 gint i;
103
104                 for (i = 0; i < document->priv->n_pages; i++) {
105                         g_free (document->priv->page_labels[i]);
106                 }
107                 g_free (document->priv->page_labels);
108                 document->priv->page_labels = NULL;
109         }
110
111         if (document->priv->info) {
112                 ev_document_info_free (document->priv->info);
113                 document->priv->info = NULL;
114         }
115
116         G_OBJECT_CLASS (ev_document_parent_class)->finalize (object);
117 }
118
119 static void
120 ev_document_init (EvDocument *document)
121 {
122         document->priv = EV_DOCUMENT_GET_PRIVATE (document);
123
124         /* Assume all pages are the same size until proven otherwise */
125         document->priv->uniform = TRUE;
126 }
127
128 static void
129 ev_document_class_init (EvDocumentClass *klass)
130 {
131         GObjectClass *g_object_class = G_OBJECT_CLASS (klass);
132
133         g_type_class_add_private (g_object_class, sizeof (EvDocumentPrivate));
134
135         klass->get_page = ev_document_impl_get_page;
136         klass->get_info = ev_document_impl_get_info;
137
138         g_object_class->finalize = ev_document_finalize;
139 }
140
141 GMutex *
142 ev_document_get_doc_mutex (void)
143 {
144         if (ev_doc_mutex == NULL) {
145                 ev_doc_mutex = g_mutex_new ();
146         }
147         return ev_doc_mutex;
148 }
149
150 void
151 ev_document_doc_mutex_lock (void)
152 {
153         g_mutex_lock (ev_document_get_doc_mutex ());
154 }
155
156 void
157 ev_document_doc_mutex_unlock (void)
158 {
159         g_mutex_unlock (ev_document_get_doc_mutex ());
160 }
161
162 gboolean
163 ev_document_doc_mutex_trylock (void)
164 {
165         return g_mutex_trylock (ev_document_get_doc_mutex ());
166 }
167
168 GMutex *
169 ev_document_get_fc_mutex (void)
170 {
171         if (ev_fc_mutex == NULL) {
172                 ev_fc_mutex = g_mutex_new ();
173         }
174         return ev_fc_mutex;
175 }
176
177 void
178 ev_document_fc_mutex_lock (void)
179 {
180         g_mutex_lock (ev_document_get_fc_mutex ());
181 }
182
183 void
184 ev_document_fc_mutex_unlock (void)
185 {
186         g_mutex_unlock (ev_document_get_fc_mutex ());
187 }
188
189 gboolean
190 ev_document_fc_mutex_trylock (void)
191 {
192         return g_mutex_trylock (ev_document_get_fc_mutex ());
193 }
194
195 /**
196  * ev_document_load:
197  * @document: a #EvDocument
198  * @uri: the document's URI
199  * @error: a #GError location to store an error, or %NULL
200  *
201  * Loads @document from @uri.
202  * 
203  * On failure, %FALSE is returned and @error is filled in.
204  * If the document is encrypted, EV_DEFINE_ERROR_ENCRYPTED is returned.
205  * If the backend cannot load the specific document, EV_DOCUMENT_ERROR_INVALID
206  * is returned. Other errors are possible too, depending on the backend
207  * used to load the document and the URI, e.g. #GIOError, #GFileError, and
208  * #GConvertError.
209  *
210  * Returns: %TRUE on success, or %FALSE on failure.
211  */
212 gboolean
213 ev_document_load (EvDocument  *document,
214                   const char  *uri,
215                   GError     **error)
216 {
217         EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
218         gboolean retval;
219         GError *err = NULL;
220
221         retval = klass->load (document, uri, &err);
222         if (!retval) {
223                 if (err) {
224                         g_propagate_error (error, err);
225                 } else {
226                         g_warning ("%s::EvDocument::load returned FALSE but did not fill in @error; fix the backend!\n",
227                                    G_OBJECT_TYPE_NAME (document));
228
229                         /* So upper layers don't crash */
230                         g_set_error_literal (error,
231                                              EV_DOCUMENT_ERROR,
232                                              EV_DOCUMENT_ERROR_INVALID,
233                                              "Internal error in backend");
234                 }
235         } else {
236                 gint i;
237                 EvDocumentPrivate *priv = document->priv;
238
239                 /* Cache some info about the document to avoid
240                  * going to the backends since it requires locks
241                  */
242                 priv->n_pages = _ev_document_get_n_pages (document);
243                 priv->info = _ev_document_get_info (document);
244
245                 for (i = 0; i < priv->n_pages; i++) {
246                         EvPage     *page = ev_document_get_page (document, i);
247                         gdouble     page_width = 0;
248                         gdouble     page_height = 0;
249                         EvPageSize *page_size;
250                         gchar      *page_label;
251
252                         _ev_document_get_page_size (document, page, &page_width, &page_height);
253
254                         if (i == 0) {
255                                 priv->uniform_width = page_width;
256                                 priv->uniform_height = page_height;
257                         } else if (priv->uniform &&
258                                    (priv->uniform_width != page_width ||
259                                     priv->uniform_height != page_height)) {
260                                 /* It's a different page size.  Backfill the array. */
261                                 int j;
262
263                                 priv->page_sizes = g_new0 (EvPageSize, priv->n_pages);
264
265                                 for (j = 0; j < i; j++) {
266                                         page_size = &(priv->page_sizes[j]);
267                                         page_size->width = priv->uniform_width;
268                                         page_size->height = priv->uniform_height;
269                                 }
270                                 priv->uniform = FALSE;
271                         }
272                         if (!priv->uniform) {
273                                 page_size = &(priv->page_sizes[i]);
274
275                                 page_size->width = page_width;
276                                 page_size->height = page_height;
277
278                                 if (page_width > priv->max_width)
279                                         priv->max_width = page_width;
280
281                                 if (page_height > priv->max_height)
282                                         priv->max_height = page_height;
283                         }
284
285                         page_label = _ev_document_get_page_label (document, page);
286                         if (page_label) {
287                                 if (priv->page_labels) {
288                                         priv->page_labels[i] = page_label;
289                                 } else {
290                                         gchar *numeric_label;
291
292                                         numeric_label = g_strdup_printf ("%d", i + 1);
293                                         if (strcmp (numeric_label, page_label) != 0) {
294                                                 priv->page_labels = g_new0 (gchar *, priv->n_pages);
295                                                 priv->page_labels[i] = page_label;
296                                         }
297                                         g_free (numeric_label);
298                                 }
299                                 priv->max_label = MAX (priv->max_label,
300                                                        g_utf8_strlen (page_label, 256));
301                         }
302
303                         g_object_unref (page);
304                 }
305
306                 if (priv->uniform) {
307                         priv->max_width = priv->uniform_width;
308                         priv->max_height = priv->uniform_height;
309                 }
310         }
311
312         return retval;
313 }
314
315 /**
316  * ev_document_save:
317  * @document:
318  * @uri: the target URI
319  * @error: a #GError location to store an error, or %NULL
320  *
321  * Saves @document to @uri.
322  * 
323  * Returns: %TRUE on success, or %FALSE on error with @error filled in
324  */
325 gboolean
326 ev_document_save (EvDocument  *document,
327                   const char  *uri,
328                   GError     **error)
329 {
330         EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
331
332         return klass->save (document, uri, error);
333 }
334
335 EvPage *
336 ev_document_get_page (EvDocument *document,
337                       gint        index)
338 {
339         EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
340
341         return klass->get_page (document, index);
342 }
343
344 static gint
345 _ev_document_get_n_pages (EvDocument  *document)
346 {
347         EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
348
349         return klass->get_n_pages (document);
350 }
351
352 gint
353 ev_document_get_n_pages (EvDocument  *document)
354 {
355         g_return_val_if_fail (EV_IS_DOCUMENT (document), 0);
356
357         return document->priv->n_pages;
358 }
359
360 static void
361 _ev_document_get_page_size (EvDocument *document,
362                             EvPage     *page,
363                             double     *width,
364                             double     *height)
365 {
366         EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
367
368         klass->get_page_size (document, page, width, height);
369 }
370
371 void
372 ev_document_get_page_size (EvDocument *document,
373                            gint        page_index,
374                            double     *width,
375                            double     *height)
376 {
377         g_return_if_fail (EV_IS_DOCUMENT (document));
378         g_return_if_fail (page_index >= 0 || page_index < document->priv->n_pages);
379
380         if (width)
381                 *width = document->priv->uniform ?
382                         document->priv->uniform_width :
383                         document->priv->page_sizes[page_index].width;
384         if (height)
385                 *height = document->priv->uniform ?
386                         document->priv->uniform_height :
387                         document->priv->page_sizes[page_index].height;
388 }
389
390 static gchar *
391 _ev_document_get_page_label (EvDocument *document,
392                              EvPage     *page)
393 {
394         EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
395
396         return klass->get_page_label ?
397                 klass->get_page_label (document, page) : NULL;
398 }
399
400 gchar *
401 ev_document_get_page_label (EvDocument *document,
402                             gint        page_index)
403 {
404         g_return_val_if_fail (EV_IS_DOCUMENT (document), NULL);
405         g_return_val_if_fail (page_index >= 0 || page_index < document->priv->n_pages, NULL);
406
407         return (document->priv->page_labels && document->priv->page_labels[page_index]) ?
408                 g_strdup (document->priv->page_labels[page_index]) :
409                 g_strdup_printf ("%d", page_index + 1);
410 }
411
412 static EvDocumentInfo *
413 _ev_document_get_info (EvDocument *document)
414 {
415         EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
416
417         return klass->get_info (document);
418 }
419
420 EvDocumentInfo *
421 ev_document_get_info (EvDocument *document)
422 {
423         g_return_val_if_fail (EV_IS_DOCUMENT (document), NULL);
424
425         return document->priv->info;
426 }
427
428 cairo_surface_t *
429 ev_document_render (EvDocument      *document,
430                     EvRenderContext *rc)
431 {
432         EvDocumentClass *klass = EV_DOCUMENT_GET_CLASS (document);
433
434         return klass->render (document, rc);
435 }
436
437 const gchar *
438 ev_document_get_title (EvDocument *document)
439 {
440         g_return_val_if_fail (EV_IS_DOCUMENT (document), NULL);
441
442         return (document->priv->info->fields_mask & EV_DOCUMENT_INFO_TITLE) ?
443                 document->priv->info->title : NULL;
444 }
445
446 gboolean
447 ev_document_is_page_size_uniform (EvDocument *document)
448 {
449         g_return_val_if_fail (EV_IS_DOCUMENT (document), TRUE);
450
451         return document->priv->uniform;
452 }
453
454 void
455 ev_document_get_max_page_size (EvDocument *document,
456                                gdouble    *width,
457                                gdouble    *height)
458 {
459         g_return_if_fail (EV_IS_DOCUMENT (document));
460
461         if (width)
462                 *width = document->priv->max_width;
463         if (height)
464                 *height = document->priv->max_height;
465 }
466
467 gboolean
468 ev_document_check_dimensions (EvDocument *document)
469 {
470         g_return_val_if_fail (EV_IS_DOCUMENT (document), FALSE);
471
472         return (document->priv->max_width > 0 && document->priv->max_height > 0);
473 }
474
475 gint
476 ev_document_get_max_label_len (EvDocument *document)
477 {
478         g_return_val_if_fail (EV_IS_DOCUMENT (document), -1);
479
480         return document->priv->max_label;
481 }
482
483 gboolean
484 ev_document_has_text_page_labels (EvDocument *document)
485 {
486         g_return_val_if_fail (EV_IS_DOCUMENT (document), FALSE);
487
488         return document->priv->page_labels != NULL;
489 }
490
491 gboolean
492 ev_document_find_page_by_label (EvDocument  *document,
493                                 const gchar *page_label,
494                                 gint        *page_index)
495 {
496         gint i, page;
497         glong value;
498         gchar *endptr = NULL;
499         EvDocumentPrivate *priv = document->priv;
500
501         g_return_val_if_fail (EV_IS_DOCUMENT (document), FALSE);
502         g_return_val_if_fail (page_label != NULL, FALSE);
503         g_return_val_if_fail (page_index != NULL, FALSE);
504
505         /* First, look for a literal label match */
506         for (i = 0; priv->page_labels && i < priv->n_pages; i ++) {
507                 if (priv->page_labels[i] != NULL &&
508                     ! strcmp (page_label, priv->page_labels[i])) {
509                         *page_index = i;
510                         return TRUE;
511                 }
512         }
513
514         /* Second, look for a match with case insensitively */
515         for (i = 0; priv->page_labels && i < priv->n_pages; i++) {
516                 if (priv->page_labels[i] != NULL &&
517                     ! strcasecmp (page_label, priv->page_labels[i])) {
518                         *page_index = i;
519                         return TRUE;
520                 }
521         }
522
523         /* Next, parse the label, and see if the number fits */
524         value = strtol (page_label, &endptr, 10);
525         if (endptr[0] == '\0') {
526                 /* Page number is an integer */
527                 page = MIN (G_MAXINT, value);
528
529                 /* convert from a page label to a page offset */
530                 page --;
531                 if (page >= 0 && page < priv->n_pages) {
532                         *page_index = page;
533                         return TRUE;
534                 }
535         }
536
537         return FALSE;
538 }
539
540 /* EvDocumentInfo */
541 EV_DEFINE_BOXED_TYPE (EvDocumentInfo, ev_document_info, ev_document_info_copy, ev_document_info_free)
542
543 EvDocumentInfo *
544 ev_document_info_copy (EvDocumentInfo *info)
545 {
546         EvDocumentInfo *copy;
547         
548         g_return_val_if_fail (info != NULL, NULL);
549
550         copy = g_new0 (EvDocumentInfo, 1);
551         copy->title = g_strdup (info->title);
552         copy->format = g_strdup (info->format);
553         copy->author = g_strdup (info->author);
554         copy->subject = g_strdup (info->subject);
555         copy->keywords = g_strdup (info->keywords);
556         copy->security = g_strdup (info->security);
557         copy->creator = g_strdup (info->creator);
558         copy->producer = g_strdup (info->producer);
559         copy->linearized = g_strdup (info->linearized);
560         
561         copy->creation_date = info->creation_date;
562         copy->modified_date = info->modified_date;
563         copy->layout = info->layout;
564         copy->mode = info->mode;
565         copy->ui_hints = info->ui_hints;
566         copy->permissions = info->permissions;
567         copy->n_pages = info->n_pages;
568         copy->fields_mask = info->fields_mask;
569
570         return copy;
571 }
572
573 void
574 ev_document_info_free (EvDocumentInfo *info)
575 {
576         if (info == NULL)
577                 return;
578
579         g_free (info->title);
580         g_free (info->format);
581         g_free (info->author);
582         g_free (info->subject);
583         g_free (info->keywords);
584         g_free (info->creator);
585         g_free (info->producer);
586         g_free (info->linearized);
587         g_free (info->security);
588         
589         g_free (info);
590 }
591
592 /* EvRectangle */
593 EV_DEFINE_BOXED_TYPE (EvRectangle, ev_rectangle, ev_rectangle_copy, ev_rectangle_free)
594
595 EvRectangle *
596 ev_rectangle_new (void)
597 {
598         return g_new0 (EvRectangle, 1);
599 }
600
601 EvRectangle *
602 ev_rectangle_copy (EvRectangle *rectangle)
603 {
604         EvRectangle *new_rectangle;
605
606         g_return_val_if_fail (rectangle != NULL, NULL);
607
608         new_rectangle = g_new (EvRectangle, 1);
609         *new_rectangle = *rectangle;
610
611         return new_rectangle;
612 }
613
614 void
615 ev_rectangle_free (EvRectangle *rectangle)
616 {
617         g_free (rectangle);
618 }
619
620 /* Compares two rects.  returns 0 if they're equal */
621 #define EPSILON 0.0000001
622
623 gint
624 ev_rect_cmp (EvRectangle *a,
625              EvRectangle *b)
626 {
627         if (a == b)
628                 return 0;
629         if (a == NULL || b == NULL)
630                 return 1;
631
632         return ! ((ABS (a->x1 - b->x1) < EPSILON) &&
633                   (ABS (a->y1 - b->y1) < EPSILON) &&
634                   (ABS (a->x2 - b->x2) < EPSILON) &&
635                   (ABS (a->y2 - b->y2) < EPSILON));
636 }