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.
25 #include "ev-document-types.h"
27 /* The various document type backends: */
28 #include "ev-poppler.h"
29 #include "pixbuf-document.h"
30 #include "tiff-document.h"
31 #include "ps-document.h"
33 #include "dvi-document.h"
36 #include "djvu-document.h"
40 #include <glib/gi18n.h>
41 #include <libgnomevfs/gnome-vfs-mime-utils.h>
42 #include <libgnomevfs/gnome-vfs-file-info.h>
43 #include <libgnomevfs/gnome-vfs-ops.h>
45 typedef struct _EvDocumentType EvDocumentType;
46 struct _EvDocumentType
48 const char *mime_type;
49 GType (*document_type_factory_callback)();
52 const EvDocumentType document_types[] = {
54 {"application/pdf", pdf_document_get_type},
57 {"application/postscript", ps_document_get_type},
58 {"application/x-gzpostscript", ps_document_get_type},
59 {"image/x-eps", ps_document_get_type},
63 {"image/tiff", tiff_document_get_type},
68 {"image/vnd.djvu", djvu_document_get_type},
73 {"application/x-dvi", dvi_document_get_type},
77 /* Would be nice to have this in gdk-pixbuf */
79 mime_type_supported_by_gdk_pixbuf (const gchar *mime_type)
81 GSList *formats, *list;
82 gboolean retval = FALSE;
84 formats = gdk_pixbuf_get_formats ();
88 GdkPixbufFormat *format = list->data;
92 if (gdk_pixbuf_format_is_disabled (format))
95 mime_types = gdk_pixbuf_format_get_mime_types (format);
97 for (i = 0; mime_types[i] != NULL; i++) {
98 if (strcmp (mime_types[i], mime_type) == 0) {
110 g_slist_free (formats);
117 ev_document_type_from_from_mime (const char *mime_type)
121 g_return_val_if_fail (mime_type, G_TYPE_INVALID);
123 for (i = 0; i < G_N_ELEMENTS (document_types); i++) {
124 if (strcmp (mime_type, document_types[i].mime_type) == 0) {
125 g_assert (document_types[i].document_type_factory_callback != NULL);
126 return document_types[i].document_type_factory_callback();
130 if (mime_type_supported_by_gdk_pixbuf (mime_type)) {
131 return pixbuf_document_get_type ();
134 return G_TYPE_INVALID;
138 * ev_document_type_get_type:
139 * @uri: String with uri
140 * @slow: Do we need to check slow gnome-vfs mime type
141 * @mime_type: If we've found handled type, the mime_type string is returned here.
142 * @error: Information about error occured
144 * Return value: G_TYPE_INVALID on error, G_TYPE_NONE when we are not sure about
145 * mime type, and type of EvDocument implementation when we've found document.
148 ev_document_type_get_type (const char *uri, gboolean slow, gchar **mime_type, GError **error)
150 GnomeVFSFileInfo *info;
151 GnomeVFSResult result;
153 GType type = G_TYPE_INVALID;
155 info = gnome_vfs_file_info_new ();
156 result = gnome_vfs_get_file_info (uri, info,
157 GNOME_VFS_FILE_INFO_GET_MIME_TYPE |
158 GNOME_VFS_FILE_INFO_FOLLOW_LINKS |
159 (slow ? GNOME_VFS_FILE_INFO_FORCE_SLOW_MIME_TYPE : 0));
160 if (result != GNOME_VFS_OK) {
164 gnome_vfs_result_to_string (result));
165 gnome_vfs_file_info_unref (info);
166 return G_TYPE_INVALID;
169 if (info->mime_type == NULL) {
173 _("Unknown MIME Type"));
174 gnome_vfs_file_info_unref (info);
175 return slow ? G_TYPE_INVALID : G_TYPE_NONE;
178 type = ev_document_type_from_from_mime (info->mime_type);
180 if (type == G_TYPE_INVALID) {
184 _("Unhandled MIME type: '%s'"), info->mime_type);
185 gnome_vfs_file_info_unref (info);
186 return slow ? G_TYPE_INVALID : G_TYPE_NONE;
189 if (mime_type != NULL) {
190 *mime_type = g_strdup (info->mime_type);
192 gnome_vfs_file_info_unref (info);
198 ev_document_type_lookup (const char *uri, gchar **mime_type, GError **error)
200 GType type = G_TYPE_INVALID;
202 type = ev_document_type_get_type (uri, FALSE, mime_type, error);
204 if (type != G_TYPE_NONE)
208 g_error_free (*error);
212 type = ev_document_type_get_type (uri, TRUE, mime_type, error);