]> www.fi.muni.cz Git - evince.git/blob - libdocument/ev-document-factory.c
[libdocument] Add EvAnnotationAttachment to support attachment annotations
[evince.git] / libdocument / ev-document-factory.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; c-indent-level: 8 -*- */
2 /*
3  *  Copyright (C) 2005, Red Hat, Inc. 
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 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include <string.h>
26
27 #include <gio/gio.h>
28 #include <glib/gstdio.h>
29 #include <glib/gi18n-lib.h>
30 #include <gtk/gtk.h>
31
32 #include "ev-backends-manager.h"
33 #include "ev-document-factory.h"
34 #include "ev-file-helpers.h"
35
36 #ifdef ENABLE_PIXBUF
37 static GList*
38 gdk_pixbuf_mime_type_list ()
39 {
40         GSList *formats, *list;
41         GList *result = NULL;
42
43         formats = gdk_pixbuf_get_formats ();
44         for (list = formats; list != NULL; list = list->next) {
45                 GdkPixbufFormat *format = list->data;
46                 gchar          **mime_types;
47
48                 if (gdk_pixbuf_format_is_disabled (format))
49                         continue;
50
51                 mime_types = gdk_pixbuf_format_get_mime_types (format);
52                 result = g_list_prepend (result, mime_types); 
53         }
54         g_slist_free (formats);
55
56         return result;
57 }
58
59 /* Would be nice to have this in gdk-pixbuf */
60 static gboolean
61 mime_type_supported_by_gdk_pixbuf (const gchar *mime_type)
62 {
63         GList *mime_types;
64         GList *list;
65         gboolean retval = FALSE;
66
67         mime_types = gdk_pixbuf_mime_type_list ();
68         for (list = mime_types; list; list = list->next) {
69                 gchar      **mtypes = (gchar **)list->data;
70                 const gchar *mtype;
71                 gint         i = 0;
72
73                 while ((mtype = mtypes[i++])) {
74                         if (strcmp (mtype, mime_type) == 0) {
75                                 retval = TRUE;
76                                 break;
77                         }
78                 }
79         }
80
81         g_list_foreach (mime_types, (GFunc)g_strfreev, NULL);
82         g_list_free (mime_types);
83
84         return retval;
85 }
86 #endif /* ENABLE_PIXBUF */
87
88 static EvCompressionType
89 get_compression_from_mime_type (const gchar *mime_type)
90 {
91         gchar type[3];
92         gchar *p;
93
94         if (!(p = g_strrstr (mime_type, "/")))
95                 return EV_COMPRESSION_NONE;
96
97         if (sscanf (++p, "x-%2s%*s", type) == 1) {
98                 if (g_ascii_strcasecmp (type, "gz") == 0)
99                         return EV_COMPRESSION_GZIP;
100                 else if (g_ascii_strcasecmp (type, "bz") == 0)
101                         return EV_COMPRESSION_BZIP2;
102         }
103
104         return EV_COMPRESSION_NONE;
105 }
106
107
108 /*
109  * get_document_from_uri:
110  * @uri: the document URI
111  * @fast: whether to use fast MIME type detection
112  * @compression: a location to store the document's compression type
113  * @error: a #GError location to store an error, or %NULL
114  *
115  * Creates a #EvDocument instance for the document at @uri, using either
116  * fast or slow MIME type detection. If a document could be created,
117  * @compression is filled in with the document's compression type.
118  * On error, %NULL is returned and @error filled in.
119  * 
120  * Returns: a new #EvDocument instance, or %NULL on error with @error filled in
121  */
122 static EvDocument *
123 get_document_from_uri (const char        *uri,
124                        gboolean           fast,
125                        EvCompressionType *compression,
126                        GError           **error)
127 {
128         EvDocument *document = NULL;
129         gchar      *mime_type = NULL;
130         GError     *err = NULL;
131
132         *compression = EV_COMPRESSION_NONE;
133
134         mime_type = ev_file_get_mime_type (uri, fast, &err);
135
136         if (mime_type == NULL) {
137                 g_free (mime_type);
138
139                 if (err == NULL) {
140                         g_set_error_literal (error,
141                                              EV_DOCUMENT_ERROR,
142                                              EV_DOCUMENT_ERROR_INVALID,
143                                              _("Unknown MIME Type"));
144                 } else {
145                         g_propagate_error (error, err);
146                 }
147                 
148                 return NULL;
149         }
150
151         document = ev_backends_manager_get_document (mime_type);
152         
153 #ifdef ENABLE_PIXBUF
154         if (!document && mime_type_supported_by_gdk_pixbuf (mime_type))
155                 document = ev_backends_manager_get_document ("image/*");
156 #endif /* ENABLE_PIXBUF */
157
158         if (document == NULL) {
159                 gchar *content_type, *mime_desc = NULL;
160
161                 content_type = g_content_type_from_mime_type (mime_type);
162                 if (content_type)
163                         mime_desc = g_content_type_get_description (content_type);
164
165                 g_set_error (error,
166                              EV_DOCUMENT_ERROR, 
167                              EV_DOCUMENT_ERROR_INVALID,
168                              _("File type %s (%s) is not supported"),
169                              mime_desc ? mime_desc : "-", mime_type);
170                 g_free (mime_desc);
171                 g_free (content_type);
172                 g_free (mime_type);
173
174                 return NULL;
175         }
176
177         *compression = get_compression_from_mime_type (mime_type);
178
179         g_free (mime_type);
180         
181         return document;
182 }
183
184 static void
185 free_uncompressed_uri (gchar *uri_unc)
186 {
187         if (!uri_unc)
188                 return;
189
190         ev_tmp_uri_unlink (uri_unc);
191         g_free (uri_unc);
192 }
193
194 /**
195  * ev_document_factory_get_document:
196  * @uri: an URI
197  * @error: a #GError location to store an error, or %NULL
198  *
199  * Creates a #EvDocument for the document at @uri; or, if no backend handling
200  * the document's type is found, or an error occurred on opening the document,
201  * returns %NULL and fills in @error.
202  * If the document is encrypted, it is returned but also @error is set to
203  * %EV_DOCUMENT_ERROR_ENCRYPTED.
204  *
205  * Returns: a new #EvDocument, or %NULL.
206  */
207 EvDocument *
208 ev_document_factory_get_document (const char *uri, GError **error)
209 {
210         EvDocument *document;
211         int result;
212         EvCompressionType compression;
213         gchar *uri_unc = NULL;
214         GError *err = NULL;
215
216         g_return_val_if_fail (uri != NULL, NULL);
217
218         document = get_document_from_uri (uri, TRUE, &compression, &err);
219         g_assert (document != NULL || err != NULL);
220
221         if (document != NULL) {
222                 uri_unc = ev_file_uncompress (uri, compression, &err);
223                 if (uri_unc) {
224                         g_object_set_data_full (G_OBJECT (document),
225                                                 "uri-uncompressed",
226                                                 uri_unc,
227                                                 (GDestroyNotify) free_uncompressed_uri);
228                 } else if (err != NULL) {
229                         /* Error uncompressing file */
230                         g_object_unref (document);
231                         g_propagate_error (error, err);
232                         return NULL;
233                 }
234
235                 result = ev_document_load (document, uri_unc ? uri_unc : uri, &err);
236
237                 if (result == FALSE || err) {
238                         if (err &&
239                             g_error_matches (err, EV_DOCUMENT_ERROR, EV_DOCUMENT_ERROR_ENCRYPTED)) {
240                                 g_propagate_error (error, err);
241                                 return document;
242                             }
243                         /* else fall through to slow mime code section below */
244                 } else {
245                         return document;
246                 }
247
248                 g_object_unref (document);
249                 document = NULL;
250         }
251         
252         /* Try again with slow mime detection */
253         g_clear_error (&err);
254         uri_unc = NULL;
255
256         document = get_document_from_uri (uri, FALSE, &compression, &err);
257         if (document == NULL) {
258                 g_assert (err != NULL);
259                 g_propagate_error (error, err);
260                 return NULL;
261         }
262
263         uri_unc = ev_file_uncompress (uri, compression, &err);
264         if (uri_unc) {
265                 g_object_set_data_full (G_OBJECT (document),
266                                         "uri-uncompressed",
267                                         uri_unc,
268                                         (GDestroyNotify) free_uncompressed_uri);
269         } else if (err != NULL) {
270                 /* Error uncompressing file */
271                 g_propagate_error (error, err);
272
273                 g_object_unref (document);
274                 return NULL;
275         }
276         
277         result = ev_document_load (document, uri_unc ? uri_unc : uri, &err);
278         if (result == FALSE) {
279                 if (err == NULL) {
280                         /* FIXME: this really should not happen; the backend should
281                          * always return a meaningful error.
282                          */
283                         g_set_error_literal (&err,
284                                              EV_DOCUMENT_ERROR,
285                                              EV_DOCUMENT_ERROR_INVALID,
286                                              _("Unknown MIME Type"));
287                 } else if (g_error_matches (err, EV_DOCUMENT_ERROR, EV_DOCUMENT_ERROR_ENCRYPTED)) {
288                         g_propagate_error (error, err);
289                         return document;
290                 }
291
292                 g_object_unref (document);
293                 document = NULL;
294
295                 g_propagate_error (error, err);
296         }
297         
298         return document;
299 }
300
301 static void
302 file_filter_add_mime_types (EvTypeInfo *info, GtkFileFilter *filter)
303 {
304         const gchar *mime_type;
305         gint         i = 0;
306
307 #ifdef ENABLE_PIXBUF
308         if (g_ascii_strcasecmp (info->mime_types[0], "image/*") == 0) {
309                 GList *pixbuf_types, *l;
310
311                 pixbuf_types = gdk_pixbuf_mime_type_list ();
312                 for (l = pixbuf_types; l; l = g_list_next (l)) {
313                         gchar **mime_types = (gchar **)l->data;
314                         gint    j = 0;
315                         
316                         while ((mime_type = mime_types[j++]))
317                                 gtk_file_filter_add_mime_type (filter, mime_type);
318                         
319                         g_strfreev (mime_types);
320                 }
321                 g_list_free (pixbuf_types);
322
323                 return;
324         }
325 #endif /* ENABLE_PIXBUF */
326         
327         while ((mime_type = info->mime_types[i++]))
328                 gtk_file_filter_add_mime_type (filter, mime_type);
329 }
330
331 /**
332  * ev_document_factory_add_filters:
333  * @chooser: a #GtkFileChooser
334  * @document: a #EvDocument, or %NULL
335  *
336  * Adds some file filters to @chooser.
337  
338  * Always add a "All documents" format.
339  * 
340  * If @document is not %NULL, adds a #GtkFileFilter for @document's MIME type.
341  *
342  * If @document is %NULL, adds a #GtkFileFilter for each document type that evince
343  * can handle.
344  */
345 void
346 ev_document_factory_add_filters (GtkWidget *chooser, EvDocument *document)
347 {
348         GList         *all_types;
349         GtkFileFilter *filter;
350         GtkFileFilter *default_filter;
351         GtkFileFilter *document_filter;
352
353         g_return_if_fail (GTK_IS_FILE_CHOOSER (chooser));
354         g_return_if_fail (document == NULL || EV_IS_DOCUMENT (document));
355
356         all_types = ev_backends_manager_get_all_types_info ();
357         
358         default_filter = document_filter = filter = gtk_file_filter_new ();
359         gtk_file_filter_set_name (filter, _("All Documents"));
360         g_list_foreach (all_types, (GFunc)file_filter_add_mime_types, filter);
361         gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
362
363         if (document) {
364                 EvTypeInfo *info;
365
366                 info = ev_backends_manager_get_document_type_info (document);
367                 default_filter = filter = gtk_file_filter_new ();
368                 gtk_file_filter_set_name (filter, info->desc);
369                 file_filter_add_mime_types (info, filter);
370                 g_free (info);
371                 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
372         } else {
373                 GList *l;
374
375                 for (l = all_types; l; l = g_list_next (l)){
376                         EvTypeInfo *info;
377
378                         info = (EvTypeInfo *)l->data;
379
380                         default_filter = filter = gtk_file_filter_new ();
381                         gtk_file_filter_set_name (filter, info->desc);
382                         file_filter_add_mime_types (info, filter);
383                         gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
384                 }
385         }
386
387         g_list_foreach (all_types, (GFunc)g_free, NULL);
388         g_list_free (all_types);
389
390         filter = gtk_file_filter_new ();
391         gtk_file_filter_set_name (filter, _("All Files"));
392         gtk_file_filter_add_pattern (filter, "*");
393         gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
394
395         gtk_file_chooser_set_filter (GTK_FILE_CHOOSER (chooser),
396                                      document == NULL ? document_filter : default_filter);
397 }