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