]> www.fi.muni.cz Git - evince.git/blob - shell/ev-document-types.c
Cleanup tiff configure checks. Fix for bug 305218.
[evince.git] / shell / ev-document-types.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; c-indent-level: 8 -*- */
2 /*
3  *  Copyright (C) 2005, Red Hat, Inc. 
4  *
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)
8  *  any later version.
9  *
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.
14  *
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.
18  *
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include "ev-document-types.h"
26
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"
32 #ifdef ENABLE_DVI
33 #include "dvi-document.h"
34 #endif
35 #ifdef ENABLE_DJVU
36 #include "djvu-document.h"
37 #endif
38
39 #include <string.h>
40 #include <libgnomevfs/gnome-vfs-mime-utils.h>
41 #include <libgnomevfs/gnome-vfs-file-info.h>
42 #include <libgnomevfs/gnome-vfs-ops.h>
43
44 typedef struct _EvDocumentType EvDocumentType;
45 struct _EvDocumentType
46 {
47         const char *mime_type;
48         GType (*document_type_factory_callback)();
49 };
50
51 const EvDocumentType document_types[] = {
52         /* PDF: */
53         {"application/pdf",            pdf_document_get_type},
54
55         /* Postscript: */
56         {"application/postscript",     ps_document_get_type},
57         {"application/x-gzpostscript", ps_document_get_type},
58         {"image/x-eps",                ps_document_get_type},
59
60 #ifdef ENABLE_TIFF
61         /* Tiff: */
62         {"image/tiff",                 tiff_document_get_type},
63 #endif
64
65 #ifdef ENABLE_DJVU
66         /* djvu: */
67         {"image/vnd.djvu",             djvu_document_get_type},
68 #endif          
69
70 #ifdef ENABLE_DVI
71         /* dvi: */
72         {"application/x-dvi",          dvi_document_get_type},
73 #endif
74 };
75
76 /* Would be nice to have this in gdk-pixbuf */
77 static gboolean
78 mime_type_supported_by_gdk_pixbuf (const gchar *mime_type)
79 {
80         GSList *formats, *list;
81         gboolean retval = FALSE;
82
83         formats = gdk_pixbuf_get_formats ();
84
85         list = formats;
86         while (list) {
87                 GdkPixbufFormat *format = list->data;
88                 int i;
89                 gchar **mime_types;
90
91                 if (gdk_pixbuf_format_is_disabled (format))
92                         continue;
93
94                 mime_types = gdk_pixbuf_format_get_mime_types (format);
95
96                 for (i = 0; mime_types[i] != NULL; i++) {
97                         if (strcmp (mime_types[i], mime_type) == 0) {
98                                 retval = TRUE;
99                                 break;
100                         }
101                 }
102
103                 if (retval)
104                         break;
105
106                 list = list->next;
107         }
108
109         g_slist_free (formats);
110
111         return retval;
112 }
113
114 static char *
115 get_slow_mime_type (const char *uri)
116 {
117         GnomeVFSFileInfo *info;
118         char *mime_type;
119         GnomeVFSResult result;
120
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) {
127                 mime_type = NULL;
128         } else {
129                 mime_type = g_strdup (info->mime_type);
130         }
131         gnome_vfs_file_info_unref (info);
132
133         return mime_type;
134 }
135
136 static GType
137 get_document_type_from_mime (const char *mime_type)
138 {
139         int i;
140
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();
145                 }
146         }
147
148         if (mime_type_supported_by_gdk_pixbuf (mime_type)) {
149                 return pixbuf_document_get_type ();
150         }
151
152         return G_TYPE_INVALID;
153 }
154
155 GType
156 ev_document_type_lookup (const char *uri, char **mime_type)
157 {
158         GType type = G_TYPE_INVALID;
159         char *mime;
160
161         g_return_val_if_fail (uri, G_TYPE_INVALID);
162
163         mime = gnome_vfs_get_mime_type (uri);
164         if (mime) {
165                 type = get_document_type_from_mime (mime);
166         }
167
168         if (type == G_TYPE_INVALID) {
169                 g_free (mime);
170                 mime = get_slow_mime_type (uri);
171                 if (mime) {
172                         type = get_document_type_from_mime (mime);
173                 }
174         }
175
176         if (mime_type) {
177                 *mime_type = mime;
178         } else {
179                 g_free (mime);
180         }
181
182         return type;
183 }