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 "ps-document.h"
32 #include "dvi-document.h"
35 #include "djvu-document.h"
39 #include <libgnomevfs/gnome-vfs-mime-utils.h>
40 #include <libgnomevfs/gnome-vfs-file-info.h>
41 #include <libgnomevfs/gnome-vfs-ops.h>
43 typedef struct _EvDocumentType EvDocumentType;
44 struct _EvDocumentType
46 const char *mime_type;
47 GType (*document_type_factory_callback)();
50 const EvDocumentType document_types[] = {
52 {"application/pdf", pdf_document_get_type},
55 {"application/postscript", ps_document_get_type},
56 {"application/x-gzpostscript", ps_document_get_type},
57 {"image/x-eps", ps_document_get_type},
61 {"image/vnd.djvu", djvu_document_get_type},
66 {"application/x-dvi", dvi_document_get_type},
70 /* Would be nice to have this in gdk-pixbuf */
72 mime_type_supported_by_gdk_pixbuf (const gchar *mime_type)
74 GSList *formats, *list;
75 gboolean retval = FALSE;
77 formats = gdk_pixbuf_get_formats ();
81 GdkPixbufFormat *format = list->data;
85 if (gdk_pixbuf_format_is_disabled (format))
88 mime_types = gdk_pixbuf_format_get_mime_types (format);
90 for (i = 0; mime_types[i] != NULL; i++) {
91 if (strcmp (mime_types[i], mime_type) == 0) {
103 g_slist_free (formats);
109 get_slow_mime_type (const char *uri)
111 GnomeVFSFileInfo *info;
113 GnomeVFSResult result;
115 info = gnome_vfs_file_info_new ();
116 result = gnome_vfs_get_file_info (uri, info,
117 GNOME_VFS_FILE_INFO_GET_MIME_TYPE |
118 GNOME_VFS_FILE_INFO_FORCE_SLOW_MIME_TYPE |
119 GNOME_VFS_FILE_INFO_FOLLOW_LINKS);
120 if (info->mime_type == NULL || result != GNOME_VFS_OK) {
123 mime_type = g_strdup (info->mime_type);
125 gnome_vfs_file_info_unref (info);
131 get_document_type_from_mime (const char *mime_type)
135 for (i = 0; i < G_N_ELEMENTS (document_types); i++) {
136 if (strcmp (mime_type, document_types[i].mime_type) == 0) {
137 g_assert (document_types[i].document_type_factory_callback != NULL);
138 return document_types[i].document_type_factory_callback();
142 if (mime_type_supported_by_gdk_pixbuf (mime_type)) {
143 return pixbuf_document_get_type ();
146 return G_TYPE_INVALID;
150 ev_document_type_lookup (const char *uri, char **mime_type)
152 GType type = G_TYPE_INVALID;
155 g_return_val_if_fail (uri, G_TYPE_INVALID);
157 mime = gnome_vfs_get_mime_type (uri);
159 type = get_document_type_from_mime (mime);
162 if (type == G_TYPE_INVALID) {
164 mime = get_slow_mime_type (uri);
166 type = get_document_type_from_mime (mime);