]> www.fi.muni.cz Git - evince.git/blob - libdocument/ev-document-factory.c
Use g_content_type_guess () to get mimetype for files. Bug #510401.
[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 void
107 throw_unknown_mime_type_error (GError **error)
108 {
109         g_set_error (error,
110                      EV_DOCUMENT_ERROR, 
111                      0,
112                      _("Unknown MIME Type"));
113 }
114
115 static void
116 throw_failed_to_get_info_error (GError **error)
117 {
118         g_set_error (error,
119                      EV_DOCUMENT_ERROR,
120                      0,
121                      _("Failed to get info for document"));
122 }
123
124 static EvDocument *
125 get_document_from_uri (const char        *uri,
126                        gboolean           slow,
127                        EvCompressionType *compression,
128                        GError           **error)
129 {
130         EvDocument *document = NULL;
131         GFile *file;
132         GFileInfo *file_info;
133         const char *mime_type;
134         char *content_type = NULL;
135
136         *compression = EV_COMPRESSION_NONE;
137
138         file = g_file_new_for_uri (uri);
139         file_info = g_file_query_info (file,
140                                        G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
141                                        0, NULL, NULL);
142
143         if (file_info == NULL) {
144                 throw_failed_to_get_info_error (error); 
145                 return NULL;
146         }
147
148         mime_type = g_file_info_get_content_type (file_info);
149
150         if (mime_type == NULL) {
151                 throw_unknown_mime_type_error (error);
152                 g_object_unref (file_info);
153                 return NULL;
154         }
155
156 #ifdef ENABLE_PIXBUF
157         if (mime_type_supported_by_gdk_pixbuf (mime_type))
158                 document = ev_backends_manager_get_document ("image/*");
159         else
160                 document = ev_backends_manager_get_document (mime_type);
161 #else
162         document = ev_backends_manager_get_document (mime_type);
163 #endif /* ENABLE_PIXBUF */
164
165         if (document == NULL) {
166                 /* try to sniff mime type from the content */
167                 guchar *buffer;
168                 gssize size_read;
169                 GFileInputStream *input_stream;
170                 
171                 input_stream = g_file_read (file, NULL, NULL);
172                 buffer = g_malloc (1024);
173                 size_read = g_input_stream_read (G_INPUT_STREAM (input_stream),
174                                                  buffer,
175                                                  1024,
176                                                  NULL, NULL);
177                 g_input_stream_close (G_INPUT_STREAM (input_stream),
178                                       NULL, NULL);
179                 g_object_unref (file);
180                 if (size_read == -1) {
181                         throw_failed_to_get_info_error (error);
182                         g_object_unref (file_info);
183                         return NULL;
184                 } else  {
185                         content_type = g_content_type_guess (NULL, /* no filename */
186                                                              buffer, 1024,
187                                                              NULL);
188                         g_free (buffer);
189                         if (content_type == NULL) {
190                                 throw_unknown_mime_type_error (error);
191                                 g_object_unref (file_info);
192                                 return NULL;
193                         } else {
194                                 document = ev_backends_manager_get_document (content_type);
195                                 if (document == NULL) {
196                                         g_set_error (error,
197                                                      EV_DOCUMENT_ERROR, 
198                                                      0,
199                                                      _("Unhandled MIME type: ā€œ%sā€"), content_type);
200                                         g_object_unref (file_info);
201                                         g_free (content_type);
202                                         return NULL;
203                                 }
204                                 mime_type = content_type;
205                         }
206                 }
207         } else {
208                 g_object_unref (file);
209         }
210
211         *compression = get_compression_from_mime_type (mime_type);
212
213         g_object_unref (file_info);
214         g_free (content_type);
215         
216         return document;
217 }
218
219 static void
220 free_uncompressed_uri (gchar *uri_unc)
221 {
222         if (!uri_unc)
223                 return;
224
225         ev_tmp_uri_unlink (uri_unc);
226         g_free (uri_unc);
227 }
228
229 EvDocument *
230 ev_document_factory_get_document (const char *uri, GError **error)
231 {
232         EvDocument *document;
233         int result;
234         EvCompressionType compression;
235         gchar *uri_unc = NULL;
236
237         document = get_document_from_uri (uri, FALSE, &compression, error);
238         if (*error == NULL) {
239                 uri_unc = ev_file_uncompress (uri, compression, error);
240                 if (uri_unc) {
241                         g_object_set_data_full (G_OBJECT (document),
242                                                 "uri-uncompressed",
243                                                 uri_unc,
244                                                 (GDestroyNotify) free_uncompressed_uri);
245                 }
246
247                 if (*error != NULL) {
248                         /* Error uncompressing file */
249                         if (document)
250                                 g_object_unref (document);
251                         return NULL;
252                 }
253
254                 result = ev_document_load (document, uri_unc ? uri_unc : uri, error);
255
256                 if (result == FALSE || *error) {
257                         if (*error &&
258                             (*error)->domain == EV_DOCUMENT_ERROR &&
259                             (*error)->code == EV_DOCUMENT_ERROR_ENCRYPTED)
260                                 return document;
261                 } else {
262                         return document;
263                 }
264         }
265         
266         /* Try again with slow mime detection */
267         if (document)
268                 g_object_unref (document);
269         document = NULL;
270
271         if (*error)
272                 g_error_free (*error);
273         *error = NULL;
274
275         uri_unc = NULL;
276
277         document = get_document_from_uri (uri, TRUE, &compression, error);
278
279         if (*error != NULL) {
280                 return NULL;
281         }
282
283         uri_unc = ev_file_uncompress (uri, compression, error);
284         if (uri_unc) {
285                 g_object_set_data_full (G_OBJECT (document),
286                                         "uri-uncompressed",
287                                         uri_unc,
288                                         (GDestroyNotify) free_uncompressed_uri);
289         }
290
291         if (*error != NULL) {
292                 /* Error uncompressing file */
293                 if (document)
294                         g_object_unref (document);
295                 return NULL;
296         }
297         
298         result = ev_document_load (document, uri_unc ? uri_unc : uri, error);
299
300         if (result == FALSE) {
301                 if (*error == NULL) {
302                         g_set_error (error,
303                                      EV_DOCUMENT_ERROR,
304                                      0,
305                                      _("Unknown MIME Type"));
306                 } else if ((*error)->domain == EV_DOCUMENT_ERROR &&
307                            (*error)->code == EV_DOCUMENT_ERROR_ENCRYPTED) {
308                         return document;
309                 }
310
311                 if (document)
312                         g_object_unref (document);
313                 document = NULL;
314         }
315         
316         return document;
317 }
318
319 static void
320 file_filter_add_mime_types (EvTypeInfo *info, GtkFileFilter *filter)
321 {
322         const gchar *mime_type;
323         gint         i = 0;
324
325 #ifdef ENABLE_PIXBUF
326         if (g_ascii_strcasecmp (info->mime_types[0], "image/*") == 0) {
327                 GList *pixbuf_types, *l;
328
329                 pixbuf_types = gdk_pixbuf_mime_type_list ();
330                 for (l = pixbuf_types; l; l = g_list_next (l)) {
331                         gchar **mime_types = (gchar **)l->data;
332                         gint    j = 0;
333                         
334                         while ((mime_type = mime_types[j++]))
335                                 gtk_file_filter_add_mime_type (filter, mime_type);
336                         
337                         g_strfreev (mime_types);
338                 }
339                 g_list_free (pixbuf_types);
340
341                 return;
342         }
343 #endif /* ENABLE_PIXBUF */
344         
345         while ((mime_type = info->mime_types[i++]))
346                 gtk_file_filter_add_mime_type (filter, mime_type);
347 }
348
349 void 
350 ev_document_factory_add_filters (GtkWidget *chooser, EvDocument *document)
351 {
352         GList         *all_types;
353         GtkFileFilter *filter;
354         GtkFileFilter *default_filter;
355         GtkFileFilter *document_filter;
356
357         all_types = ev_backends_manager_get_all_types_info ();
358         
359         default_filter = document_filter = filter = gtk_file_filter_new ();
360         gtk_file_filter_set_name (filter, _("All Documents"));
361         g_list_foreach (all_types, (GFunc)file_filter_add_mime_types, filter);
362         gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
363
364         if (document) {
365                 EvTypeInfo *info;
366
367                 info = ev_backends_manager_get_document_type_info (document);
368                 default_filter = filter = gtk_file_filter_new ();
369                 gtk_file_filter_set_name (filter, info->desc);
370                 file_filter_add_mime_types (info, filter);
371                 g_free (info);
372                 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
373         } else {
374                 GList *l;
375
376                 for (l = all_types; l; l = g_list_next (l)){
377                         EvTypeInfo *info;
378
379                         info = (EvTypeInfo *)l->data;
380
381                         default_filter = filter = gtk_file_filter_new ();
382                         gtk_file_filter_set_name (filter, info->desc);
383                         file_filter_add_mime_types (info, filter);
384                         gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
385                 }
386         }
387
388         g_list_foreach (all_types, (GFunc)g_free, NULL);
389         g_list_free (all_types);
390
391         filter = gtk_file_filter_new ();
392         gtk_file_filter_set_name (filter, _("All Files"));
393         gtk_file_filter_add_pattern (filter, "*");
394         gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
395
396         gtk_file_chooser_set_filter (GTK_FILE_CHOOSER (chooser),
397                                      document == NULL ? document_filter : default_filter);
398 }