]> www.fi.muni.cz Git - evince.git/blob - libdocument/ev-document-factory.c
Use a local GError, and propagate as appropriate, making sure always to
[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  *
203  * Returns: a new #EvDocument, or %NULL.
204  */
205 EvDocument *
206 ev_document_factory_get_document (const char *uri, GError **error)
207 {
208         EvDocument *document;
209         int result;
210         EvCompressionType compression;
211         gchar *uri_unc = NULL;
212         GError *err = NULL;
213
214         g_return_val_if_fail (uri != NULL, NULL);
215
216         document = get_document_from_uri (uri, TRUE, &compression, &err);
217         g_assert (document != NULL || err != NULL);
218
219         if (document != NULL) {
220                 uri_unc = ev_file_uncompress (uri, compression, &err);
221                 if (uri_unc) {
222                         g_object_set_data_full (G_OBJECT (document),
223                                                 "uri-uncompressed",
224                                                 uri_unc,
225                                                 (GDestroyNotify) free_uncompressed_uri);
226                 } else if (err != NULL) {
227                         /* Error uncompressing file */
228                         g_object_unref (document);
229                         g_propagate_error (error, err);
230                         return NULL;
231                 }
232
233                 result = ev_document_load (document, uri_unc ? uri_unc : uri, &err);
234
235                 if (result == FALSE || err) {
236                         if (err &&
237                             g_error_matches (err, EV_DOCUMENT_ERROR, EV_DOCUMENT_ERROR_ENCRYPTED)) {
238                                 g_error_free (err);
239                                 return document;
240                             }
241                         /* else fall through to slow mime code section below */
242                 } else {
243                         return document;
244                 }
245
246                 g_object_unref (document);
247                 document = NULL;
248         }
249         
250         /* Try again with slow mime detection */
251         g_clear_error (&err);
252         uri_unc = NULL;
253
254         document = get_document_from_uri (uri, FALSE, &compression, &err);
255         if (document == NULL) {
256                 g_assert (err != NULL);
257                 g_propagate_error (error, err);
258                 return NULL;
259         }
260
261         uri_unc = ev_file_uncompress (uri, compression, &err);
262         if (uri_unc) {
263                 g_object_set_data_full (G_OBJECT (document),
264                                         "uri-uncompressed",
265                                         uri_unc,
266                                         (GDestroyNotify) free_uncompressed_uri);
267         } else if (err != NULL) {
268                 /* Error uncompressing file */
269                 g_propagate_error (error, err);
270
271                 g_object_unref (document);
272                 return NULL;
273         }
274         
275         result = ev_document_load (document, uri_unc ? uri_unc : uri, &err);
276         if (result == FALSE) {
277                 if (err == NULL) {
278                         /* FIXME: this really should not happen; the backend should
279                          * always return a meaningful error.
280                          */
281                         g_set_error_literal (&err,
282                                              EV_DOCUMENT_ERROR,
283                                              EV_DOCUMENT_ERROR_INVALID,
284                                              _("Unknown MIME Type"));
285                 } else if (g_error_matches (err, EV_DOCUMENT_ERROR, EV_DOCUMENT_ERROR_ENCRYPTED)) {
286                         g_error_free (err);
287                         return document;
288                 }
289
290                 g_object_unref (document);
291                 document = NULL;
292
293                 g_propagate_error (error, err);
294         }
295         
296         return document;
297 }
298
299 static void
300 file_filter_add_mime_types (EvTypeInfo *info, GtkFileFilter *filter)
301 {
302         const gchar *mime_type;
303         gint         i = 0;
304
305 #ifdef ENABLE_PIXBUF
306         if (g_ascii_strcasecmp (info->mime_types[0], "image/*") == 0) {
307                 GList *pixbuf_types, *l;
308
309                 pixbuf_types = gdk_pixbuf_mime_type_list ();
310                 for (l = pixbuf_types; l; l = g_list_next (l)) {
311                         gchar **mime_types = (gchar **)l->data;
312                         gint    j = 0;
313                         
314                         while ((mime_type = mime_types[j++]))
315                                 gtk_file_filter_add_mime_type (filter, mime_type);
316                         
317                         g_strfreev (mime_types);
318                 }
319                 g_list_free (pixbuf_types);
320
321                 return;
322         }
323 #endif /* ENABLE_PIXBUF */
324         
325         while ((mime_type = info->mime_types[i++]))
326                 gtk_file_filter_add_mime_type (filter, mime_type);
327 }
328
329 /**
330  * ev_document_factory_add_filters:
331  * @chooser: a #GtkFileChooser
332  * @document: a #EvDocument, or %NULL
333  *
334  * Adds some file filters to @chooser.
335  
336  * Always add a "All documents" format.
337  * 
338  * If @document is not %NULL, adds a #GtkFileFilter for @document's MIME type.
339  *
340  * If @document is %NULL, adds a #GtkFileFilter for each document type that evince
341  * can handle.
342  */
343 void
344 ev_document_factory_add_filters (GtkWidget *chooser, EvDocument *document)
345 {
346         GList         *all_types;
347         GtkFileFilter *filter;
348         GtkFileFilter *default_filter;
349         GtkFileFilter *document_filter;
350
351         g_return_if_fail (GTK_IS_FILE_CHOOSER (chooser));
352         g_return_if_fail (document == NULL || EV_IS_DOCUMENT (document));
353
354         all_types = ev_backends_manager_get_all_types_info ();
355         
356         default_filter = document_filter = filter = gtk_file_filter_new ();
357         gtk_file_filter_set_name (filter, _("All Documents"));
358         g_list_foreach (all_types, (GFunc)file_filter_add_mime_types, filter);
359         gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
360
361         if (document) {
362                 EvTypeInfo *info;
363
364                 info = ev_backends_manager_get_document_type_info (document);
365                 default_filter = filter = gtk_file_filter_new ();
366                 gtk_file_filter_set_name (filter, info->desc);
367                 file_filter_add_mime_types (info, filter);
368                 g_free (info);
369                 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
370         } else {
371                 GList *l;
372
373                 for (l = all_types; l; l = g_list_next (l)){
374                         EvTypeInfo *info;
375
376                         info = (EvTypeInfo *)l->data;
377
378                         default_filter = filter = gtk_file_filter_new ();
379                         gtk_file_filter_set_name (filter, info->desc);
380                         file_filter_add_mime_types (info, filter);
381                         gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
382                 }
383         }
384
385         g_list_foreach (all_types, (GFunc)g_free, NULL);
386         g_list_free (all_types);
387
388         filter = gtk_file_filter_new ();
389         gtk_file_filter_set_name (filter, _("All Files"));
390         gtk_file_filter_add_pattern (filter, "*");
391         gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
392
393         gtk_file_chooser_set_filter (GTK_FILE_CHOOSER (chooser),
394                                      document == NULL ? document_filter : default_filter);
395 }