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