]> www.fi.muni.cz Git - evince.git/blob - shell/ev-document-types.c
b39cfd7d0fce24785e84bdaae82913f6122a6b12
[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         /* Tiff: */
61         {"image/tiff",                 tiff_document_get_type},
62
63 #ifdef ENABLE_DJVU
64         /* djvu: */
65         {"image/vnd.djvu",             djvu_document_get_type},
66 #endif          
67
68 #ifdef ENABLE_DVI
69         /* dvi: */
70         {"application/x-dvi",          dvi_document_get_type},
71 #endif
72 };
73
74 /* Would be nice to have this in gdk-pixbuf */
75 static gboolean
76 mime_type_supported_by_gdk_pixbuf (const gchar *mime_type)
77 {
78         GSList *formats, *list;
79         gboolean retval = FALSE;
80
81         formats = gdk_pixbuf_get_formats ();
82
83         list = formats;
84         while (list) {
85                 GdkPixbufFormat *format = list->data;
86                 int i;
87                 gchar **mime_types;
88
89                 if (gdk_pixbuf_format_is_disabled (format))
90                         continue;
91
92                 mime_types = gdk_pixbuf_format_get_mime_types (format);
93
94                 for (i = 0; mime_types[i] != NULL; i++) {
95                         if (strcmp (mime_types[i], mime_type) == 0) {
96                                 retval = TRUE;
97                                 break;
98                         }
99                 }
100
101                 if (retval)
102                         break;
103
104                 list = list->next;
105         }
106
107         g_slist_free (formats);
108
109         return retval;
110 }
111
112 static char *
113 get_slow_mime_type (const char *uri)
114 {
115         GnomeVFSFileInfo *info;
116         char *mime_type;
117         GnomeVFSResult result;
118
119         info = gnome_vfs_file_info_new ();
120         result = gnome_vfs_get_file_info (uri, info,
121                                           GNOME_VFS_FILE_INFO_GET_MIME_TYPE |
122                                           GNOME_VFS_FILE_INFO_FORCE_SLOW_MIME_TYPE |
123                                           GNOME_VFS_FILE_INFO_FOLLOW_LINKS);
124         if (info->mime_type == NULL || result != GNOME_VFS_OK) {
125                 mime_type = NULL;
126         } else {
127                 mime_type = g_strdup (info->mime_type);
128         }
129         gnome_vfs_file_info_unref (info);
130
131         return mime_type;
132 }
133
134 static GType
135 get_document_type_from_mime (const char *mime_type)
136 {
137         int i;
138
139         for (i = 0; i < G_N_ELEMENTS (document_types); i++) {
140                 if (strcmp (mime_type, document_types[i].mime_type) == 0) {
141                         g_assert (document_types[i].document_type_factory_callback != NULL);
142                         return document_types[i].document_type_factory_callback();
143                 }
144         }
145
146         if (mime_type_supported_by_gdk_pixbuf (mime_type)) {
147                 return pixbuf_document_get_type ();
148         }
149
150         return G_TYPE_INVALID;
151 }
152
153 GType
154 ev_document_type_lookup (const char *uri, char **mime_type)
155 {
156         GType type = G_TYPE_INVALID;
157         char *mime;
158
159         g_return_val_if_fail (uri, G_TYPE_INVALID);
160
161         mime = gnome_vfs_get_mime_type (uri);
162         if (mime) {
163                 type = get_document_type_from_mime (mime);
164         }
165
166         if (type == G_TYPE_INVALID) {
167                 g_free (mime);
168                 mime = get_slow_mime_type (uri);
169                 if (mime) {
170                         type = get_document_type_from_mime (mime);
171                 }
172         }
173
174         if (mime_type) {
175                 *mime_type = mime;
176         } else {
177                 g_free (mime);
178         }
179
180         return type;
181 }