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