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