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.
27 #include <glib/gstdio.h>
28 #include <glib/gi18n.h>
29 #include <gtk/gtkfilechooserdialog.h>
31 #include "ev-backends-manager.h"
32 #include "ev-document-factory.h"
33 #include "ev-file-helpers.h"
37 gdk_pixbuf_mime_type_list ()
39 GSList *formats, *list;
42 formats = gdk_pixbuf_get_formats ();
43 for (list = formats; list != NULL; list = list->next) {
44 GdkPixbufFormat *format = list->data;
47 if (gdk_pixbuf_format_is_disabled (format))
50 mime_types = gdk_pixbuf_format_get_mime_types (format);
51 result = g_list_prepend (result, mime_types);
53 g_slist_free (formats);
58 /* Would be nice to have this in gdk-pixbuf */
60 mime_type_supported_by_gdk_pixbuf (const gchar *mime_type)
64 gboolean retval = FALSE;
66 mime_types = gdk_pixbuf_mime_type_list ();
67 for (list = mime_types; list; list = list->next) {
68 gchar **mtypes = (gchar **)list->data;
72 while ((mtype = mtypes[i++])) {
73 if (strcmp (mtype, mime_type) == 0) {
80 g_list_foreach (mime_types, (GFunc)g_strfreev, NULL);
81 g_list_free (mime_types);
85 #endif /* ENABLE_PIXBUF */
87 static EvCompressionType
88 get_compression_from_mime_type (const gchar *mime_type)
93 if (!(p = g_strrstr (mime_type, "/")))
94 return EV_COMPRESSION_NONE;
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;
103 return EV_COMPRESSION_NONE;
107 get_mime_type_from_uri (const gchar *uri, GError **error)
110 GFileInfo *file_info;
113 file = g_file_new_for_uri (uri);
114 file_info = g_file_query_info (file,
115 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
117 g_object_unref (file);
119 if (file_info == NULL)
122 mime_type = g_strdup (g_file_info_get_content_type (file_info));
123 g_object_unref (file_info);
129 get_mime_type_from_data (const gchar *uri, GError **error)
132 GFileInputStream *input_stream;
136 file = g_file_new_for_uri (uri);
138 input_stream = g_file_read (file, NULL, error);
140 g_object_unref (file);
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, error);
148 g_object_unref (file);
153 return g_content_type_guess (NULL, /* no filename */
159 get_document_from_uri (const char *uri,
161 EvCompressionType *compression,
164 EvDocument *document = NULL;
165 gchar *mime_type = NULL;
167 *compression = EV_COMPRESSION_NONE;
170 get_mime_type_from_data (uri, error) :
171 get_mime_type_from_uri (uri, error);
173 if (mime_type == NULL) {
176 if (*error == NULL) {
180 _("Unknown MIME Type"));
186 document = ev_backends_manager_get_document (mime_type);
189 if (!document && mime_type_supported_by_gdk_pixbuf (mime_type))
190 document = ev_backends_manager_get_document ("image/*");
191 #endif /* ENABLE_PIXBUF */
193 if (document == NULL) {
196 mime_desc = g_content_type_get_description (mime_type);
200 _("File type %s (%s) is not supported"),
201 mime_desc, mime_type);
208 *compression = get_compression_from_mime_type (mime_type);
216 free_uncompressed_uri (gchar *uri_unc)
221 ev_tmp_uri_unlink (uri_unc);
226 ev_document_factory_get_document (const char *uri, GError **error)
228 EvDocument *document;
230 EvCompressionType compression;
231 gchar *uri_unc = NULL;
233 document = get_document_from_uri (uri, FALSE, &compression, error);
234 if (*error == NULL) {
235 uri_unc = ev_file_uncompress (uri, compression, error);
237 g_object_set_data_full (G_OBJECT (document),
240 (GDestroyNotify) free_uncompressed_uri);
243 if (*error != NULL) {
244 /* Error uncompressing file */
246 g_object_unref (document);
250 result = ev_document_load (document, uri_unc ? uri_unc : uri, error);
252 if (result == FALSE || *error) {
254 (*error)->domain == EV_DOCUMENT_ERROR &&
255 (*error)->code == EV_DOCUMENT_ERROR_ENCRYPTED)
262 /* Try again with slow mime detection */
264 g_object_unref (document);
268 g_error_free (*error);
273 document = get_document_from_uri (uri, TRUE, &compression, error);
275 if (*error != NULL) {
279 uri_unc = ev_file_uncompress (uri, compression, error);
281 g_object_set_data_full (G_OBJECT (document),
284 (GDestroyNotify) free_uncompressed_uri);
287 if (*error != NULL) {
288 /* Error uncompressing file */
290 g_object_unref (document);
294 result = ev_document_load (document, uri_unc ? uri_unc : uri, error);
296 if (result == FALSE) {
297 if (*error == NULL) {
301 _("Unknown MIME Type"));
302 } else if ((*error)->domain == EV_DOCUMENT_ERROR &&
303 (*error)->code == EV_DOCUMENT_ERROR_ENCRYPTED) {
308 g_object_unref (document);
316 file_filter_add_mime_types (EvTypeInfo *info, GtkFileFilter *filter)
318 const gchar *mime_type;
322 if (g_ascii_strcasecmp (info->mime_types[0], "image/*") == 0) {
323 GList *pixbuf_types, *l;
325 pixbuf_types = gdk_pixbuf_mime_type_list ();
326 for (l = pixbuf_types; l; l = g_list_next (l)) {
327 gchar **mime_types = (gchar **)l->data;
330 while ((mime_type = mime_types[j++]))
331 gtk_file_filter_add_mime_type (filter, mime_type);
333 g_strfreev (mime_types);
335 g_list_free (pixbuf_types);
339 #endif /* ENABLE_PIXBUF */
341 while ((mime_type = info->mime_types[i++]))
342 gtk_file_filter_add_mime_type (filter, mime_type);
346 ev_document_factory_add_filters (GtkWidget *chooser, EvDocument *document)
349 GtkFileFilter *filter;
350 GtkFileFilter *default_filter;
351 GtkFileFilter *document_filter;
353 all_types = ev_backends_manager_get_all_types_info ();
355 default_filter = document_filter = filter = gtk_file_filter_new ();
356 gtk_file_filter_set_name (filter, _("All Documents"));
357 g_list_foreach (all_types, (GFunc)file_filter_add_mime_types, filter);
358 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
363 info = ev_backends_manager_get_document_type_info (document);
364 default_filter = filter = gtk_file_filter_new ();
365 gtk_file_filter_set_name (filter, info->desc);
366 file_filter_add_mime_types (info, filter);
368 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
372 for (l = all_types; l; l = g_list_next (l)){
375 info = (EvTypeInfo *)l->data;
377 default_filter = filter = gtk_file_filter_new ();
378 gtk_file_filter_set_name (filter, info->desc);
379 file_filter_add_mime_types (info, filter);
380 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
384 g_list_foreach (all_types, (GFunc)g_free, NULL);
385 g_list_free (all_types);
387 filter = gtk_file_filter_new ();
388 gtk_file_filter_set_name (filter, _("All Files"));
389 gtk_file_filter_add_pattern (filter, "*");
390 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
392 gtk_file_chooser_set_filter (GTK_FILE_CHOOSER (chooser),
393 document == NULL ? document_filter : default_filter);