1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; c-indent-level: 8 -*- */
3 * Copyright (C) 2005, Red Hat, Inc.
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)
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.
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.
28 #include <glib/gstdio.h>
29 #include <glib/gi18n-lib.h>
32 #include "ev-backends-manager.h"
33 #include "ev-document-factory.h"
34 #include "ev-file-helpers.h"
38 gdk_pixbuf_mime_type_list ()
40 GSList *formats, *list;
43 formats = gdk_pixbuf_get_formats ();
44 for (list = formats; list != NULL; list = list->next) {
45 GdkPixbufFormat *format = list->data;
48 if (gdk_pixbuf_format_is_disabled (format))
51 mime_types = gdk_pixbuf_format_get_mime_types (format);
52 result = g_list_prepend (result, mime_types);
54 g_slist_free (formats);
59 /* Would be nice to have this in gdk-pixbuf */
61 mime_type_supported_by_gdk_pixbuf (const gchar *mime_type)
65 gboolean retval = FALSE;
67 mime_types = gdk_pixbuf_mime_type_list ();
68 for (list = mime_types; list; list = list->next) {
69 gchar **mtypes = (gchar **)list->data;
73 while ((mtype = mtypes[i++])) {
74 if (strcmp (mtype, mime_type) == 0) {
81 g_list_foreach (mime_types, (GFunc)g_strfreev, NULL);
82 g_list_free (mime_types);
86 #endif /* ENABLE_PIXBUF */
88 static EvCompressionType
89 get_compression_from_mime_type (const gchar *mime_type)
94 if (!(p = g_strrstr (mime_type, "/")))
95 return EV_COMPRESSION_NONE;
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;
104 return EV_COMPRESSION_NONE;
109 * get_document_from_uri:
110 * @uri: the document URI
111 * @fast: whether to use fast MIME type detection
112 * @compression: a location to store the document's compression type
113 * @error: a #GError location to store an error, or %NULL
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.
120 * Returns: a new #EvDocument instance, or %NULL on error with @error filled in
123 get_document_from_uri (const char *uri,
125 EvCompressionType *compression,
128 EvDocument *document = NULL;
129 gchar *mime_type = NULL;
132 *compression = EV_COMPRESSION_NONE;
134 mime_type = ev_file_get_mime_type (uri, fast, &err);
136 if (mime_type == NULL) {
140 g_set_error_literal (error,
142 EV_DOCUMENT_ERROR_INVALID,
143 _("Unknown MIME Type"));
145 g_propagate_error (error, err);
151 document = ev_backends_manager_get_document (mime_type);
154 if (!document && mime_type_supported_by_gdk_pixbuf (mime_type))
155 document = ev_backends_manager_get_document ("image/*");
156 #endif /* ENABLE_PIXBUF */
158 if (document == NULL) {
159 gchar *content_type, *mime_desc = NULL;
161 content_type = g_content_type_from_mime_type (mime_type);
163 mime_desc = g_content_type_get_description (content_type);
167 EV_DOCUMENT_ERROR_INVALID,
168 _("File type %s (%s) is not supported"),
169 mime_desc ? mime_desc : "-", mime_type);
171 g_free (content_type);
177 *compression = get_compression_from_mime_type (mime_type);
185 free_uncompressed_uri (gchar *uri_unc)
190 ev_tmp_uri_unlink (uri_unc);
195 * ev_document_factory_get_document:
197 * @error: a #GError location to store an error, or %NULL
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 * If the document is encrypted, it is returned but also @error is set to
203 * %EV_DOCUMENT_ERROR_ENCRYPTED.
205 * Returns: a new #EvDocument, or %NULL.
208 ev_document_factory_get_document (const char *uri, GError **error)
210 EvDocument *document;
212 EvCompressionType compression;
213 gchar *uri_unc = NULL;
216 g_return_val_if_fail (uri != NULL, NULL);
218 document = get_document_from_uri (uri, TRUE, &compression, &err);
219 g_assert (document != NULL || err != NULL);
221 if (document != NULL) {
222 uri_unc = ev_file_uncompress (uri, compression, &err);
224 g_object_set_data_full (G_OBJECT (document),
227 (GDestroyNotify) free_uncompressed_uri);
228 } else if (err != NULL) {
229 /* Error uncompressing file */
230 g_object_unref (document);
231 g_propagate_error (error, err);
235 result = ev_document_load (document, uri_unc ? uri_unc : uri, &err);
237 if (result == FALSE || err) {
239 g_error_matches (err, EV_DOCUMENT_ERROR, EV_DOCUMENT_ERROR_ENCRYPTED)) {
240 g_propagate_error (error, err);
243 /* else fall through to slow mime code section below */
248 g_object_unref (document);
252 /* Try again with slow mime detection */
253 g_clear_error (&err);
256 document = get_document_from_uri (uri, FALSE, &compression, &err);
257 if (document == NULL) {
258 g_assert (err != NULL);
259 g_propagate_error (error, err);
263 uri_unc = ev_file_uncompress (uri, compression, &err);
265 g_object_set_data_full (G_OBJECT (document),
268 (GDestroyNotify) free_uncompressed_uri);
269 } else if (err != NULL) {
270 /* Error uncompressing file */
271 g_propagate_error (error, err);
273 g_object_unref (document);
277 result = ev_document_load (document, uri_unc ? uri_unc : uri, &err);
278 if (result == FALSE) {
280 /* FIXME: this really should not happen; the backend should
281 * always return a meaningful error.
283 g_set_error_literal (&err,
285 EV_DOCUMENT_ERROR_INVALID,
286 _("Unknown MIME Type"));
287 } else if (g_error_matches (err, EV_DOCUMENT_ERROR, EV_DOCUMENT_ERROR_ENCRYPTED)) {
288 g_propagate_error (error, err);
292 g_object_unref (document);
295 g_propagate_error (error, err);
302 file_filter_add_mime_types (EvTypeInfo *info, GtkFileFilter *filter)
304 const gchar *mime_type;
308 if (g_ascii_strcasecmp (info->mime_types[0], "image/*") == 0) {
309 GList *pixbuf_types, *l;
311 pixbuf_types = gdk_pixbuf_mime_type_list ();
312 for (l = pixbuf_types; l; l = g_list_next (l)) {
313 gchar **mime_types = (gchar **)l->data;
316 while ((mime_type = mime_types[j++]))
317 gtk_file_filter_add_mime_type (filter, mime_type);
319 g_strfreev (mime_types);
321 g_list_free (pixbuf_types);
325 #endif /* ENABLE_PIXBUF */
327 while ((mime_type = info->mime_types[i++]))
328 gtk_file_filter_add_mime_type (filter, mime_type);
332 * ev_document_factory_add_filters:
333 * @chooser: a #GtkFileChooser
334 * @document: a #EvDocument, or %NULL
336 * Adds some file filters to @chooser.
338 * Always add a "All documents" format.
340 * If @document is not %NULL, adds a #GtkFileFilter for @document's MIME type.
342 * If @document is %NULL, adds a #GtkFileFilter for each document type that evince
346 ev_document_factory_add_filters (GtkWidget *chooser, EvDocument *document)
349 GtkFileFilter *filter;
350 GtkFileFilter *default_filter;
351 GtkFileFilter *document_filter;
353 g_return_if_fail (GTK_IS_FILE_CHOOSER (chooser));
354 g_return_if_fail (document == NULL || EV_IS_DOCUMENT (document));
356 all_types = ev_backends_manager_get_all_types_info ();
358 default_filter = document_filter = filter = gtk_file_filter_new ();
359 gtk_file_filter_set_name (filter, _("All Documents"));
360 g_list_foreach (all_types, (GFunc)file_filter_add_mime_types, filter);
361 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
366 info = ev_backends_manager_get_document_type_info (document);
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);
371 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
375 for (l = all_types; l; l = g_list_next (l)){
378 info = (EvTypeInfo *)l->data;
380 default_filter = filter = gtk_file_filter_new ();
381 gtk_file_filter_set_name (filter, info->desc);
382 file_filter_add_mime_types (info, filter);
383 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
387 g_list_foreach (all_types, (GFunc)g_free, NULL);
388 g_list_free (all_types);
390 filter = gtk_file_filter_new ();
391 gtk_file_filter_set_name (filter, _("All Files"));
392 gtk_file_filter_add_pattern (filter, "*");
393 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
395 gtk_file_chooser_set_filter (GTK_FILE_CHOOSER (chooser),
396 document == NULL ? document_filter : default_filter);