]> www.fi.muni.cz Git - evince.git/blob - libdocument/ev-document-factory.c
Document that this returns either NULL and fills in error, or non-NULL.
[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: return 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
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 EvDocument *
195 ev_document_factory_get_document (const char *uri, GError **error)
196 {
197         EvDocument *document;
198         int result;
199         EvCompressionType compression;
200         gchar *uri_unc = NULL;
201
202         document = get_document_from_uri (uri, TRUE, &compression, error);
203         if (*error == NULL) {
204                 uri_unc = ev_file_uncompress (uri, compression, error);
205                 if (uri_unc) {
206                         g_object_set_data_full (G_OBJECT (document),
207                                                 "uri-uncompressed",
208                                                 uri_unc,
209                                                 (GDestroyNotify) free_uncompressed_uri);
210                 }
211
212                 if (*error != NULL) {
213                         /* Error uncompressing file */
214                         if (document)
215                                 g_object_unref (document);
216                         return NULL;
217                 }
218
219                 result = ev_document_load (document, uri_unc ? uri_unc : uri, error);
220
221                 if (result == FALSE || *error) {
222                         if (*error &&
223                             (*error)->domain == EV_DOCUMENT_ERROR &&
224                             (*error)->code == EV_DOCUMENT_ERROR_ENCRYPTED)
225                                 return document;
226                 } else {
227                         return document;
228                 }
229         }
230         
231         /* Try again with slow mime detection */
232         if (document)
233                 g_object_unref (document);
234         document = NULL;
235
236         if (*error)
237                 g_error_free (*error);
238         *error = NULL;
239
240         uri_unc = NULL;
241
242         document = get_document_from_uri (uri, FALSE, &compression, error);
243
244         if (*error != NULL) {
245                 return NULL;
246         }
247
248         uri_unc = ev_file_uncompress (uri, compression, error);
249         if (uri_unc) {
250                 g_object_set_data_full (G_OBJECT (document),
251                                         "uri-uncompressed",
252                                         uri_unc,
253                                         (GDestroyNotify) free_uncompressed_uri);
254         }
255
256         if (*error != NULL) {
257                 /* Error uncompressing file */
258                 if (document)
259                         g_object_unref (document);
260                 return NULL;
261         }
262         
263         result = ev_document_load (document, uri_unc ? uri_unc : uri, error);
264
265         if (result == FALSE) {
266                 if (*error == NULL) {
267                         g_set_error_literal (error,
268                                              EV_DOCUMENT_ERROR,
269                                              EV_DOCUMENT_ERROR_INVALID,
270                                              _("Unknown MIME Type"));
271                 } else if ((*error)->domain == EV_DOCUMENT_ERROR &&
272                            (*error)->code == EV_DOCUMENT_ERROR_ENCRYPTED) {
273                         return document;
274                 }
275
276                 if (document)
277                         g_object_unref (document);
278                 document = NULL;
279         }
280         
281         return document;
282 }
283
284 static void
285 file_filter_add_mime_types (EvTypeInfo *info, GtkFileFilter *filter)
286 {
287         const gchar *mime_type;
288         gint         i = 0;
289
290 #ifdef ENABLE_PIXBUF
291         if (g_ascii_strcasecmp (info->mime_types[0], "image/*") == 0) {
292                 GList *pixbuf_types, *l;
293
294                 pixbuf_types = gdk_pixbuf_mime_type_list ();
295                 for (l = pixbuf_types; l; l = g_list_next (l)) {
296                         gchar **mime_types = (gchar **)l->data;
297                         gint    j = 0;
298                         
299                         while ((mime_type = mime_types[j++]))
300                                 gtk_file_filter_add_mime_type (filter, mime_type);
301                         
302                         g_strfreev (mime_types);
303                 }
304                 g_list_free (pixbuf_types);
305
306                 return;
307         }
308 #endif /* ENABLE_PIXBUF */
309         
310         while ((mime_type = info->mime_types[i++]))
311                 gtk_file_filter_add_mime_type (filter, mime_type);
312 }
313
314 void 
315 ev_document_factory_add_filters (GtkWidget *chooser, EvDocument *document)
316 {
317         GList         *all_types;
318         GtkFileFilter *filter;
319         GtkFileFilter *default_filter;
320         GtkFileFilter *document_filter;
321
322         all_types = ev_backends_manager_get_all_types_info ();
323         
324         default_filter = document_filter = filter = gtk_file_filter_new ();
325         gtk_file_filter_set_name (filter, _("All Documents"));
326         g_list_foreach (all_types, (GFunc)file_filter_add_mime_types, filter);
327         gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
328
329         if (document) {
330                 EvTypeInfo *info;
331
332                 info = ev_backends_manager_get_document_type_info (document);
333                 default_filter = filter = gtk_file_filter_new ();
334                 gtk_file_filter_set_name (filter, info->desc);
335                 file_filter_add_mime_types (info, filter);
336                 g_free (info);
337                 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
338         } else {
339                 GList *l;
340
341                 for (l = all_types; l; l = g_list_next (l)){
342                         EvTypeInfo *info;
343
344                         info = (EvTypeInfo *)l->data;
345
346                         default_filter = filter = gtk_file_filter_new ();
347                         gtk_file_filter_set_name (filter, info->desc);
348                         file_filter_add_mime_types (info, filter);
349                         gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
350                 }
351         }
352
353         g_list_foreach (all_types, (GFunc)g_free, NULL);
354         g_list_free (all_types);
355
356         filter = gtk_file_filter_new ();
357         gtk_file_filter_set_name (filter, _("All Files"));
358         gtk_file_filter_add_pattern (filter, "*");
359         gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
360
361         gtk_file_chooser_set_filter (GTK_FILE_CHOOSER (chooser),
362                                      document == NULL ? document_filter : default_filter);
363 }