]> www.fi.muni.cz Git - evince.git/blob - libdocument/ev-document.c
Remove unnecessary NULL check; g_strdup() is NULL-safe.
[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) 2004 Marco Pesenti Gritti
4  *
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)
8  *  any later version.
9  *
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.
14  *
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.
18  *
19  */
20
21 #include "config.h"
22
23 #include "ev-document.h"
24
25 GMutex *ev_doc_mutex = NULL;
26 GMutex *ev_fc_mutex = NULL;
27
28 EV_DEFINE_INTERFACE (EvDocument, ev_document, G_TYPE_OBJECT)
29
30 GQuark
31 ev_document_error_quark (void)
32 {
33   static GQuark q = 0;
34   if (q == 0)
35     q = g_quark_from_static_string ("ev-document-error-quark");
36
37   return q;
38 }
39
40 static void
41 ev_document_class_init (EvDocumentIface *klass)
42 {
43 }
44
45 GMutex *
46 ev_document_get_doc_mutex (void)
47 {
48         if (ev_doc_mutex == NULL) {
49                 ev_doc_mutex = g_mutex_new ();
50         }
51         return ev_doc_mutex;
52 }
53
54 void
55 ev_document_doc_mutex_lock (void)
56 {
57         g_mutex_lock (ev_document_get_doc_mutex ());
58 }
59
60 void
61 ev_document_doc_mutex_unlock (void)
62 {
63         g_mutex_unlock (ev_document_get_doc_mutex ());
64 }
65
66 gboolean
67 ev_document_doc_mutex_trylock (void)
68 {
69         return g_mutex_trylock (ev_document_get_doc_mutex ());
70 }
71
72 GMutex *
73 ev_document_get_fc_mutex (void)
74 {
75         if (ev_fc_mutex == NULL) {
76                 ev_fc_mutex = g_mutex_new ();
77         }
78         return ev_fc_mutex;
79 }
80
81 void
82 ev_document_fc_mutex_lock (void)
83 {
84         g_mutex_lock (ev_document_get_fc_mutex ());
85 }
86
87 void
88 ev_document_fc_mutex_unlock (void)
89 {
90         g_mutex_unlock (ev_document_get_fc_mutex ());
91 }
92
93 gboolean
94 ev_document_fc_mutex_trylock (void)
95 {
96         return g_mutex_trylock (ev_document_get_fc_mutex ());
97 }
98
99 /**
100  * ev_document_load:
101  * @document: a #EvDocument
102  * @uri: the document's URI
103  * @error: a #GError location to store an error, or %NULL
104  *
105  * Loads @document from @uri.
106  * On failure, @error is filled in.
107  *
108  * Returns: %TRUE on success, or %FALSE on failure.
109  */
110 gboolean
111 ev_document_load (EvDocument  *document,
112                   const char  *uri,
113                   GError     **error)
114 {
115         EvDocumentIface *iface = EV_DOCUMENT_GET_IFACE (document);
116         gboolean retval;
117
118         retval = iface->load (document, uri, error);
119
120         return retval;
121 }
122
123 gboolean
124 ev_document_save (EvDocument  *document,
125                   const char  *uri,
126                   GError     **error)
127 {
128         EvDocumentIface *iface = EV_DOCUMENT_GET_IFACE (document);
129         gboolean retval;
130
131         retval = iface->save (document, uri, error);
132
133         return retval;
134 }
135
136 int
137 ev_document_get_n_pages (EvDocument  *document)
138 {
139         EvDocumentIface *iface = EV_DOCUMENT_GET_IFACE (document);
140         gint retval;
141
142         retval = iface->get_n_pages (document);
143
144         return retval;
145 }
146
147 EvPage *
148 ev_document_get_page (EvDocument *document,
149                       gint        index)
150 {
151         EvDocumentIface *iface = EV_DOCUMENT_GET_IFACE (document);
152         EvPage *retval;
153
154         if (iface->get_page)
155                 retval = iface->get_page (document, index);
156         else
157                 retval = ev_page_new (index);
158
159         return retval;
160 }
161
162 void
163 ev_document_get_page_size (EvDocument *document,
164                            EvPage     *page,
165                            double     *width,
166                            double     *height)
167 {
168         EvDocumentIface *iface = EV_DOCUMENT_GET_IFACE (document);
169
170         iface->get_page_size (document, page, width, height);
171 }
172
173 char *
174 ev_document_get_page_label (EvDocument *document,
175                             EvPage     *page)
176 {
177         EvDocumentIface *iface = EV_DOCUMENT_GET_IFACE (document);
178
179         if (iface->get_page_label == NULL)
180                 return NULL;
181
182         return iface->get_page_label (document, page);
183 }
184
185 EvDocumentInfo *
186 ev_document_get_info (EvDocument *document)
187 {
188         EvDocumentIface *iface = EV_DOCUMENT_GET_IFACE (document);
189
190         return iface->get_info (document);
191 }
192
193 gboolean
194 ev_document_has_attachments (EvDocument *document)
195 {
196         EvDocumentIface *iface = EV_DOCUMENT_GET_IFACE (document);
197
198         if (iface->has_attachments == NULL)
199                 return FALSE;
200         
201         return iface->has_attachments (document);
202 }
203
204 GList *
205 ev_document_get_attachments (EvDocument *document)
206 {
207         EvDocumentIface *iface = EV_DOCUMENT_GET_IFACE (document);
208         GList *retval;
209
210         if (iface->get_attachments == NULL)
211                 return NULL;
212         retval = iface->get_attachments (document);
213
214         return retval;
215 }
216
217 cairo_surface_t *
218 ev_document_render (EvDocument      *document,
219                     EvRenderContext *rc)
220 {
221         EvDocumentIface *iface = EV_DOCUMENT_GET_IFACE (document);
222         cairo_surface_t *retval;
223
224         g_assert (iface->render);
225
226         retval = iface->render (document, rc);
227
228         return retval;
229 }
230
231 /* EvDocumentInfo */
232 EV_DEFINE_BOXED_TYPE (EvDocumentInfo, ev_document_info, ev_document_info_copy, ev_document_info_free)
233
234 EvDocumentInfo *
235 ev_document_info_copy (EvDocumentInfo *info)
236 {
237         EvDocumentInfo *copy;
238         
239         g_return_val_if_fail (info != NULL, NULL);
240
241         copy = g_new0 (EvDocumentInfo, 1);
242         copy->title = g_strdup (info->title);
243         copy->format = g_strdup (info->format);
244         copy->author = g_strdup (info->author);
245         copy->subject = g_strdup (info->subject);
246         copy->keywords = g_strdup (info->keywords);
247         copy->security = g_strdup (info->security);
248         copy->creator = g_strdup (info->creator);
249         copy->producer = g_strdup (info->producer);
250         copy->linearized = g_strdup (info->linearized);
251         
252         copy->creation_date = info->creation_date;
253         copy->modified_date = info->modified_date;
254         copy->layout = info->layout;
255         copy->mode = info->mode;
256         copy->ui_hints = info->ui_hints;
257         copy->permissions = info->permissions;
258         copy->n_pages = info->n_pages;
259         copy->fields_mask = info->fields_mask;
260
261         return copy;
262 }
263
264 void
265 ev_document_info_free (EvDocumentInfo *info)
266 {
267         if (info == NULL)
268                 return;
269
270         g_free (info->title);
271         g_free (info->format);
272         g_free (info->author);
273         g_free (info->subject);
274         g_free (info->keywords);
275         g_free (info->creator);
276         g_free (info->producer);
277         g_free (info->linearized);
278         g_free (info->security);
279         
280         g_free (info);
281 }
282
283
284 /* Compares two rects.  returns 0 if they're equal */
285 #define EPSILON 0.0000001
286
287 gint
288 ev_rect_cmp (EvRectangle *a,
289              EvRectangle *b)
290 {
291         if (a == b)
292                 return 0;
293         if (a == NULL || b == NULL)
294                 return 1;
295
296         return ! ((ABS (a->x1 - b->x1) < EPSILON) &&
297                   (ABS (a->y1 - b->y1) < EPSILON) &&
298                   (ABS (a->x2 - b->x2) < EPSILON) &&
299                   (ABS (a->y2 - b->y2) < EPSILON));
300 }