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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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"
36 static EvCompressionType
37 get_compression_from_mime_type (const gchar *mime_type)
42 if (!(p = g_strrstr (mime_type, "/")))
43 return EV_COMPRESSION_NONE;
45 if (sscanf (++p, "x-%2s%*s", type) == 1) {
46 if (g_ascii_strcasecmp (type, "gz") == 0)
47 return EV_COMPRESSION_GZIP;
48 else if (g_ascii_strcasecmp (type, "bz") == 0)
49 return EV_COMPRESSION_BZIP2;
52 return EV_COMPRESSION_NONE;
57 * get_document_from_uri:
58 * @uri: the document URI
59 * @fast: whether to use fast MIME type detection
60 * @compression: a location to store the document's compression type
61 * @error: a #GError location to store an error, or %NULL
63 * Creates a #EvDocument instance for the document at @uri, using either
64 * fast or slow MIME type detection. If a document could be created,
65 * @compression is filled in with the document's compression type.
66 * On error, %NULL is returned and @error filled in.
68 * Returns: a new #EvDocument instance, or %NULL on error with @error filled in
71 get_document_from_uri (const char *uri,
73 EvCompressionType *compression,
76 EvDocument *document = NULL;
77 gchar *mime_type = NULL;
80 *compression = EV_COMPRESSION_NONE;
82 mime_type = ev_file_get_mime_type (uri, fast, &err);
84 if (mime_type == NULL) {
88 g_set_error_literal (error,
90 EV_DOCUMENT_ERROR_INVALID,
91 _("Unknown MIME Type"));
93 g_propagate_error (error, err);
99 document = ev_backends_manager_get_document (mime_type);
100 if (document == NULL) {
101 gchar *content_type, *mime_desc = NULL;
103 content_type = g_content_type_from_mime_type (mime_type);
105 mime_desc = g_content_type_get_description (content_type);
109 EV_DOCUMENT_ERROR_INVALID,
110 _("File type %s (%s) is not supported"),
111 mime_desc ? mime_desc : "-", mime_type);
113 g_free (content_type);
119 *compression = get_compression_from_mime_type (mime_type);
127 free_uncompressed_uri (gchar *uri_unc)
132 ev_tmp_uri_unlink (uri_unc);
137 * ev_document_factory_get_document:
139 * @error: a #GError location to store an error, or %NULL
141 * Creates a #EvDocument for the document at @uri; or, if no backend handling
142 * the document's type is found, or an error occurred on opening the document,
143 * returns %NULL and fills in @error.
144 * If the document is encrypted, it is returned but also @error is set to
145 * %EV_DOCUMENT_ERROR_ENCRYPTED.
147 * Returns: a new #EvDocument, or %NULL.
150 ev_document_factory_get_document (const char *uri, GError **error)
152 EvDocument *document;
154 EvCompressionType compression;
155 gchar *uri_unc = NULL;
158 g_return_val_if_fail (uri != NULL, NULL);
160 document = get_document_from_uri (uri, TRUE, &compression, &err);
161 g_assert (document != NULL || err != NULL);
163 if (document != NULL) {
164 uri_unc = ev_file_uncompress (uri, compression, &err);
166 g_object_set_data_full (G_OBJECT (document),
169 (GDestroyNotify) free_uncompressed_uri);
170 } else if (err != NULL) {
171 /* Error uncompressing file */
172 g_object_unref (document);
173 g_propagate_error (error, err);
177 result = ev_document_load (document, uri_unc ? uri_unc : uri, &err);
179 if (result == FALSE || err) {
181 g_error_matches (err, EV_DOCUMENT_ERROR, EV_DOCUMENT_ERROR_ENCRYPTED)) {
182 g_propagate_error (error, err);
185 /* else fall through to slow mime code section below */
190 g_object_unref (document);
194 /* Try again with slow mime detection */
195 g_clear_error (&err);
198 document = get_document_from_uri (uri, FALSE, &compression, &err);
199 if (document == NULL) {
200 g_assert (err != NULL);
201 g_propagate_error (error, err);
205 uri_unc = ev_file_uncompress (uri, compression, &err);
207 g_object_set_data_full (G_OBJECT (document),
210 (GDestroyNotify) free_uncompressed_uri);
211 } else if (err != NULL) {
212 /* Error uncompressing file */
213 g_propagate_error (error, err);
215 g_object_unref (document);
219 result = ev_document_load (document, uri_unc ? uri_unc : uri, &err);
220 if (result == FALSE) {
222 /* FIXME: this really should not happen; the backend should
223 * always return a meaningful error.
225 g_set_error_literal (&err,
227 EV_DOCUMENT_ERROR_INVALID,
228 _("Unknown MIME Type"));
229 } else if (g_error_matches (err, EV_DOCUMENT_ERROR, EV_DOCUMENT_ERROR_ENCRYPTED)) {
230 g_propagate_error (error, err);
234 g_object_unref (document);
237 g_propagate_error (error, err);
244 file_filter_add_mime_types (EvTypeInfo *info, GtkFileFilter *filter)
246 const gchar *mime_type;
249 while ((mime_type = info->mime_types[i++]))
250 gtk_file_filter_add_mime_type (filter, mime_type);
254 * ev_document_factory_add_filters:
255 * @chooser: a #GtkFileChooser
256 * @document: a #EvDocument, or %NULL
258 * Adds some file filters to @chooser.
260 * Always add a "All documents" format.
262 * If @document is not %NULL, adds a #GtkFileFilter for @document's MIME type.
264 * If @document is %NULL, adds a #GtkFileFilter for each document type that evince
268 ev_document_factory_add_filters (GtkWidget *chooser, EvDocument *document)
271 GtkFileFilter *filter;
272 GtkFileFilter *default_filter;
273 GtkFileFilter *document_filter;
275 g_return_if_fail (GTK_IS_FILE_CHOOSER (chooser));
276 g_return_if_fail (document == NULL || EV_IS_DOCUMENT (document));
278 all_types = ev_backends_manager_get_all_types_info ();
280 default_filter = document_filter = filter = gtk_file_filter_new ();
281 gtk_file_filter_set_name (filter, _("All Documents"));
282 g_list_foreach (all_types, (GFunc)file_filter_add_mime_types, filter);
283 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
288 info = ev_backends_manager_get_document_type_info (document);
289 default_filter = filter = gtk_file_filter_new ();
290 gtk_file_filter_set_name (filter, info->desc);
291 file_filter_add_mime_types (info, filter);
293 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
297 for (l = all_types; l; l = g_list_next (l)){
300 info = (EvTypeInfo *)l->data;
302 default_filter = filter = gtk_file_filter_new ();
303 gtk_file_filter_set_name (filter, info->desc);
304 file_filter_add_mime_types (info, filter);
305 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
309 g_list_foreach (all_types, (GFunc)g_free, NULL);
310 g_list_free (all_types);
312 filter = gtk_file_filter_new ();
313 gtk_file_filter_set_name (filter, _("All Files"));
314 gtk_file_filter_add_pattern (filter, "*");
315 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
317 gtk_file_chooser_set_filter (GTK_FILE_CHOOSER (chooser),
318 document == NULL ? document_filter : default_filter);