]> www.fi.muni.cz Git - evince.git/blob - shell/ev-document-types.c
Change api to lookup from uri. Do fast lookup first, if the type is
[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 "ps-document.h"
31 #ifdef ENABLE_DVI
32 #include "dvi-document.h"
33 #endif
34 #ifdef ENABLE_DJVU
35 #include "djvu-document.h"
36 #endif
37
38 #include <string.h>
39 #include <libgnomevfs/gnome-vfs-mime-utils.h>
40 #include <libgnomevfs/gnome-vfs-file-info.h>
41 #include <libgnomevfs/gnome-vfs-ops.h>
42
43 typedef struct _EvDocumentType EvDocumentType;
44 struct _EvDocumentType
45 {
46         const char *mime_type;
47         GType (*document_type_factory_callback)();
48 };
49
50 const EvDocumentType document_types[] = {
51         /* PDF: */
52         {"application/pdf",            pdf_document_get_type},
53
54         /* Postscript: */
55         {"application/postscript",     ps_document_get_type},
56         {"application/x-gzpostscript", ps_document_get_type},
57         {"image/x-eps",                ps_document_get_type},
58
59 #ifdef ENABLE_DJVU
60         /* djvu: */
61         {"image/vnd.djvu",             djvu_document_get_type},
62 #endif          
63
64 #ifdef ENABLE_DVI
65         /* dvi: */
66         {"application/x-dvi",          dvi_document_get_type},
67 #endif
68 };
69
70 /* Would be nice to have this in gdk-pixbuf */
71 static gboolean
72 mime_type_supported_by_gdk_pixbuf (const gchar *mime_type)
73 {
74         GSList *formats, *list;
75         gboolean retval = FALSE;
76
77         formats = gdk_pixbuf_get_formats ();
78
79         list = formats;
80         while (list) {
81                 GdkPixbufFormat *format = list->data;
82                 int i;
83                 gchar **mime_types;
84
85                 if (gdk_pixbuf_format_is_disabled (format))
86                         continue;
87
88                 mime_types = gdk_pixbuf_format_get_mime_types (format);
89
90                 for (i = 0; mime_types[i] != NULL; i++) {
91                         if (strcmp (mime_types[i], mime_type) == 0) {
92                                 retval = TRUE;
93                                 break;
94                         }
95                 }
96
97                 if (retval)
98                         break;
99
100                 list = list->next;
101         }
102
103         g_slist_free (formats);
104
105         return retval;
106 }
107
108 static char *
109 get_slow_mime_type (const char *uri)
110 {
111         GnomeVFSFileInfo *info;
112         char *mime_type;
113         GnomeVFSResult result;
114
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) {
121                 mime_type = NULL;
122         } else {
123                 mime_type = g_strdup (info->mime_type);
124         }
125         gnome_vfs_file_info_unref (info);
126
127         return mime_type;
128 }
129
130 static GType
131 get_document_type_from_mime (const char *mime_type)
132 {
133         int i;
134
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();
139                 }
140         }
141
142         if (mime_type_supported_by_gdk_pixbuf (mime_type)) {
143                 return pixbuf_document_get_type ();
144         }
145
146         return G_TYPE_INVALID;
147 }
148
149 GType
150 ev_document_type_lookup (const char *uri, char **mime_type)
151 {
152         GType type = G_TYPE_INVALID;
153         char *mime;
154
155         g_return_val_if_fail (uri, G_TYPE_INVALID);
156
157         mime = gnome_vfs_get_mime_type (uri);
158         if (mime) {
159                 type = get_document_type_from_mime (mime);
160         }
161
162         if (type == G_TYPE_INVALID) {
163                 g_free (mime);
164                 mime = get_slow_mime_type (uri);
165                 if (mime) {
166                         type = get_document_type_from_mime (mime);
167                 }
168         }
169
170         if (mime_type) {
171                 *mime_type = mime;
172         } else {
173                 g_free (mime);
174         }
175
176         return type;
177 }