]> www.fi.muni.cz Git - evince.git/blob - libdocument/ev-document-factory.c
Use the pixbuf backend only when the mime type is not supported by any of
[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 #include <gio/gio.h>
27 #include <glib/gstdio.h>
28 #include <glib/gi18n.h>
29 #include <gtk/gtkfilechooserdialog.h>
30
31 #include "ev-backends-manager.h"
32 #include "ev-document-factory.h"
33 #include "ev-file-helpers.h"
34
35 #ifdef ENABLE_PIXBUF
36 static GList*
37 gdk_pixbuf_mime_type_list ()
38 {
39         GSList *formats, *list;
40         GList *result = NULL;
41
42         formats = gdk_pixbuf_get_formats ();
43         for (list = formats; list != NULL; list = list->next) {
44                 GdkPixbufFormat *format = list->data;
45                 gchar          **mime_types;
46
47                 if (gdk_pixbuf_format_is_disabled (format))
48                         continue;
49
50                 mime_types = gdk_pixbuf_format_get_mime_types (format);
51                 result = g_list_prepend (result, mime_types); 
52         }
53         g_slist_free (formats);
54
55         return result;
56 }
57
58 /* Would be nice to have this in gdk-pixbuf */
59 static gboolean
60 mime_type_supported_by_gdk_pixbuf (const gchar *mime_type)
61 {
62         GList *mime_types;
63         GList *list;
64         gboolean retval = FALSE;
65
66         mime_types = gdk_pixbuf_mime_type_list ();
67         for (list = mime_types; list; list = list->next) {
68                 gchar      **mtypes = (gchar **)list->data;
69                 const gchar *mtype;
70                 gint         i = 0;
71
72                 while ((mtype = mtypes[i++])) {
73                         if (strcmp (mtype, mime_type) == 0) {
74                                 retval = TRUE;
75                                 break;
76                         }
77                 }
78         }
79
80         g_list_foreach (mime_types, (GFunc)g_strfreev, NULL);
81         g_list_free (mime_types);
82
83         return retval;
84 }
85 #endif /* ENABLE_PIXBUF */
86
87 static EvCompressionType
88 get_compression_from_mime_type (const gchar *mime_type)
89 {
90         gchar type[3];
91         gchar *p;
92
93         if (!(p = g_strrstr (mime_type, "/")))
94                 return EV_COMPRESSION_NONE;
95
96         if (sscanf (++p, "x-%2s%*s", type) == 1) {
97                 if (g_ascii_strcasecmp (type, "gz") == 0)
98                         return EV_COMPRESSION_GZIP;
99                 else if (g_ascii_strcasecmp (type, "bz") == 0)
100                         return EV_COMPRESSION_BZIP2;
101         }
102
103         return EV_COMPRESSION_NONE;
104 }
105
106 static gchar *
107 get_mime_type_from_uri (const gchar *uri)
108 {
109         GFile     *file;
110         GFileInfo *file_info;
111         gchar     *mime_type;
112
113         file = g_file_new_for_uri (uri);
114         file_info = g_file_query_info (file,
115                                        G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
116                                        0, NULL, NULL);
117         g_object_unref (file);
118
119         if (file_info == NULL)
120                 return NULL;
121
122         mime_type = g_strdup (g_file_info_get_content_type (file_info));
123         g_object_unref (file_info);
124
125         return mime_type;
126 }
127
128 static gchar *
129 get_mime_type_from_data (const gchar *uri)
130 {
131         GFile            *file;
132         GFileInputStream *input_stream;
133         gssize            size_read;
134         guchar            buffer[1024];
135
136         file = g_file_new_for_uri (uri);
137         
138         input_stream = g_file_read (file, NULL, NULL);
139         if (!input_stream) {
140                 g_object_unref (file);
141                 return NULL;
142         }
143
144         size_read = g_input_stream_read (G_INPUT_STREAM (input_stream),
145                                          buffer, 1024, NULL, NULL);
146         g_input_stream_close (G_INPUT_STREAM (input_stream), NULL, NULL);
147
148         g_object_unref (file);
149
150         if (size_read == -1)
151                 return NULL;
152
153         return g_content_type_guess (NULL, /* no filename */
154                                      buffer, 1024,
155                                      NULL);
156 }
157
158 static EvDocument *
159 get_document_from_uri (const char        *uri,
160                        gboolean           slow,
161                        EvCompressionType *compression,
162                        GError           **error)
163 {
164         EvDocument *document = NULL;
165         gchar      *mime_type = NULL;
166
167         *compression = EV_COMPRESSION_NONE;
168
169         mime_type = slow ?
170                 get_mime_type_from_data (uri) :
171                 get_mime_type_from_uri (uri);
172
173         if (mime_type == NULL) {
174                 g_set_error (error,
175                              EV_DOCUMENT_ERROR, 
176                              0,
177                              _("Unknown MIME Type"));
178                 g_free (mime_type);
179                 
180                 return NULL;
181         }
182
183         document = ev_backends_manager_get_document (mime_type);
184         
185 #ifdef ENABLE_PIXBUF
186         if (!document && mime_type_supported_by_gdk_pixbuf (mime_type))
187                 document = ev_backends_manager_get_document ("image/*");
188 #endif /* ENABLE_PIXBUF */
189
190         if (document == NULL) {
191                 g_set_error (error,
192                              EV_DOCUMENT_ERROR, 
193                              0,
194                              _("Unhandled MIME type: ā€œ%sā€"), mime_type);
195                 g_free (mime_type);
196                 
197                 return NULL;
198         }
199
200         *compression = get_compression_from_mime_type (mime_type);
201
202         g_free (mime_type);
203         
204         return document;
205 }
206
207 static void
208 free_uncompressed_uri (gchar *uri_unc)
209 {
210         if (!uri_unc)
211                 return;
212
213         ev_tmp_uri_unlink (uri_unc);
214         g_free (uri_unc);
215 }
216
217 EvDocument *
218 ev_document_factory_get_document (const char *uri, GError **error)
219 {
220         EvDocument *document;
221         int result;
222         EvCompressionType compression;
223         gchar *uri_unc = NULL;
224
225         document = get_document_from_uri (uri, FALSE, &compression, error);
226         if (*error == NULL) {
227                 uri_unc = ev_file_uncompress (uri, compression, error);
228                 if (uri_unc) {
229                         g_object_set_data_full (G_OBJECT (document),
230                                                 "uri-uncompressed",
231                                                 uri_unc,
232                                                 (GDestroyNotify) free_uncompressed_uri);
233                 }
234
235                 if (*error != NULL) {
236                         /* Error uncompressing file */
237                         if (document)
238                                 g_object_unref (document);
239                         return NULL;
240                 }
241
242                 result = ev_document_load (document, uri_unc ? uri_unc : uri, error);
243
244                 if (result == FALSE || *error) {
245                         if (*error &&
246                             (*error)->domain == EV_DOCUMENT_ERROR &&
247                             (*error)->code == EV_DOCUMENT_ERROR_ENCRYPTED)
248                                 return document;
249                 } else {
250                         return document;
251                 }
252         }
253         
254         /* Try again with slow mime detection */
255         if (document)
256                 g_object_unref (document);
257         document = NULL;
258
259         if (*error)
260                 g_error_free (*error);
261         *error = NULL;
262
263         uri_unc = NULL;
264
265         document = get_document_from_uri (uri, TRUE, &compression, error);
266
267         if (*error != NULL) {
268                 return NULL;
269         }
270
271         uri_unc = ev_file_uncompress (uri, compression, error);
272         if (uri_unc) {
273                 g_object_set_data_full (G_OBJECT (document),
274                                         "uri-uncompressed",
275                                         uri_unc,
276                                         (GDestroyNotify) free_uncompressed_uri);
277         }
278
279         if (*error != NULL) {
280                 /* Error uncompressing file */
281                 if (document)
282                         g_object_unref (document);
283                 return NULL;
284         }
285         
286         result = ev_document_load (document, uri_unc ? uri_unc : uri, error);
287
288         if (result == FALSE) {
289                 if (*error == NULL) {
290                         g_set_error (error,
291                                      EV_DOCUMENT_ERROR,
292                                      0,
293                                      _("Unknown MIME Type"));
294                 } else if ((*error)->domain == EV_DOCUMENT_ERROR &&
295                            (*error)->code == EV_DOCUMENT_ERROR_ENCRYPTED) {
296                         return document;
297                 }
298
299                 if (document)
300                         g_object_unref (document);
301                 document = NULL;
302         }
303         
304         return document;
305 }
306
307 static void
308 file_filter_add_mime_types (EvTypeInfo *info, GtkFileFilter *filter)
309 {
310         const gchar *mime_type;
311         gint         i = 0;
312
313 #ifdef ENABLE_PIXBUF
314         if (g_ascii_strcasecmp (info->mime_types[0], "image/*") == 0) {
315                 GList *pixbuf_types, *l;
316
317                 pixbuf_types = gdk_pixbuf_mime_type_list ();
318                 for (l = pixbuf_types; l; l = g_list_next (l)) {
319                         gchar **mime_types = (gchar **)l->data;
320                         gint    j = 0;
321                         
322                         while ((mime_type = mime_types[j++]))
323                                 gtk_file_filter_add_mime_type (filter, mime_type);
324                         
325                         g_strfreev (mime_types);
326                 }
327                 g_list_free (pixbuf_types);
328
329                 return;
330         }
331 #endif /* ENABLE_PIXBUF */
332         
333         while ((mime_type = info->mime_types[i++]))
334                 gtk_file_filter_add_mime_type (filter, mime_type);
335 }
336
337 void 
338 ev_document_factory_add_filters (GtkWidget *chooser, EvDocument *document)
339 {
340         GList         *all_types;
341         GtkFileFilter *filter;
342         GtkFileFilter *default_filter;
343         GtkFileFilter *document_filter;
344
345         all_types = ev_backends_manager_get_all_types_info ();
346         
347         default_filter = document_filter = filter = gtk_file_filter_new ();
348         gtk_file_filter_set_name (filter, _("All Documents"));
349         g_list_foreach (all_types, (GFunc)file_filter_add_mime_types, filter);
350         gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
351
352         if (document) {
353                 EvTypeInfo *info;
354
355                 info = ev_backends_manager_get_document_type_info (document);
356                 default_filter = filter = gtk_file_filter_new ();
357                 gtk_file_filter_set_name (filter, info->desc);
358                 file_filter_add_mime_types (info, filter);
359                 g_free (info);
360                 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
361         } else {
362                 GList *l;
363
364                 for (l = all_types; l; l = g_list_next (l)){
365                         EvTypeInfo *info;
366
367                         info = (EvTypeInfo *)l->data;
368
369                         default_filter = filter = gtk_file_filter_new ();
370                         gtk_file_filter_set_name (filter, info->desc);
371                         file_filter_add_mime_types (info, filter);
372                         gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
373                 }
374         }
375
376         g_list_foreach (all_types, (GFunc)g_free, NULL);
377         g_list_free (all_types);
378
379         filter = gtk_file_filter_new ();
380         gtk_file_filter_set_name (filter, _("All Files"));
381         gtk_file_filter_add_pattern (filter, "*");
382         gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
383
384         gtk_file_chooser_set_filter (GTK_FILE_CHOOSER (chooser),
385                                      document == NULL ? document_filter : default_filter);
386 }