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 <libgnomevfs/gnome-vfs-mime-utils.h>
41 #include <libgnomevfs/gnome-vfs-file-info.h>
42 #include <libgnomevfs/gnome-vfs-ops.h>
44 typedef struct _EvDocumentType EvDocumentType;
45 struct _EvDocumentType
47 const char *mime_type;
48 GType (*document_type_factory_callback)();
51 const EvDocumentType document_types[] = {
53 {"application/pdf", pdf_document_get_type},
56 {"application/postscript", ps_document_get_type},
57 {"application/x-gzpostscript", ps_document_get_type},
58 {"image/x-eps", ps_document_get_type},
62 {"image/tiff", tiff_document_get_type},
67 {"image/vnd.djvu", djvu_document_get_type},
72 {"application/x-dvi", dvi_document_get_type},
76 /* Would be nice to have this in gdk-pixbuf */
78 mime_type_supported_by_gdk_pixbuf (const gchar *mime_type)
80 GSList *formats, *list;
81 gboolean retval = FALSE;
83 formats = gdk_pixbuf_get_formats ();
87 GdkPixbufFormat *format = list->data;
91 if (gdk_pixbuf_format_is_disabled (format))
94 mime_types = gdk_pixbuf_format_get_mime_types (format);
96 for (i = 0; mime_types[i] != NULL; i++) {
97 if (strcmp (mime_types[i], mime_type) == 0) {
109 g_slist_free (formats);
115 get_slow_mime_type (const char *uri)
117 GnomeVFSFileInfo *info;
119 GnomeVFSResult result;
121 info = gnome_vfs_file_info_new ();
122 result = gnome_vfs_get_file_info (uri, info,
123 GNOME_VFS_FILE_INFO_GET_MIME_TYPE |
124 GNOME_VFS_FILE_INFO_FORCE_SLOW_MIME_TYPE |
125 GNOME_VFS_FILE_INFO_FOLLOW_LINKS);
126 if (info->mime_type == NULL || result != GNOME_VFS_OK) {
129 mime_type = g_strdup (info->mime_type);
131 gnome_vfs_file_info_unref (info);
137 get_document_type_from_mime (const char *mime_type)
141 for (i = 0; i < G_N_ELEMENTS (document_types); i++) {
142 if (strcmp (mime_type, document_types[i].mime_type) == 0) {
143 g_assert (document_types[i].document_type_factory_callback != NULL);
144 return document_types[i].document_type_factory_callback();
148 if (mime_type_supported_by_gdk_pixbuf (mime_type)) {
149 return pixbuf_document_get_type ();
152 return G_TYPE_INVALID;
156 ev_document_type_lookup (const char *uri, char **mime_type)
158 GType type = G_TYPE_INVALID;
161 g_return_val_if_fail (uri, G_TYPE_INVALID);
163 mime = gnome_vfs_get_mime_type (uri);
165 type = get_document_type_from_mime (mime);
168 if (type == G_TYPE_INVALID) {
170 mime = get_slow_mime_type (uri);
172 type = get_document_type_from_mime (mime);