]> www.fi.muni.cz Git - evince.git/blob - backend/ev-document-factory.c
e15f7213750c6831e94fa9cb2847a2f135dfd491
[evince.git] / backend / ev-document-factory.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-factory.h"
26
27 /* The various document type backends: */
28 #include "ev-poppler.h"
29 #include "pixbuf-document.h"
30 #include "tiff-document.h"
31 #ifdef ENABLE_PS
32 #include "ps-document.h"
33 #endif
34 #ifdef ENABLE_DVI
35 #include "dvi-document.h"
36 #endif
37 #ifdef ENABLE_DJVU
38 #include "djvu-document.h"
39 #endif
40 #ifdef ENABLE_COMICS
41 #include "comics-document.h"
42 #endif
43
44 #include <string.h>
45
46 typedef struct _EvDocumentType EvDocumentType;
47 struct _EvDocumentType
48 {
49         const char *mime_type;
50         EvBackend backend;
51         GType (*document_type_factory_callback)();
52 };
53
54 const EvDocumentType document_types[] = {
55         /* PDF: */
56         {"application/pdf",            EV_BACKEND_PDF,  pdf_document_get_type},
57
58 #ifdef ENABLE_PS
59         /* Postscript: */
60         {"application/postscript",     EV_BACKEND_PS,   ps_document_get_type},
61         {"application/x-gzpostscript", EV_BACKEND_PS,   ps_document_get_type},
62         {"image/x-eps",                EV_BACKEND_PS,   ps_document_get_type},
63 #endif
64
65 #ifdef ENABLE_TIFF
66         /* Tiff: */
67         {"image/tiff",                 EV_BACKEND_TIFF, tiff_document_get_type},
68 #endif
69
70 #ifdef ENABLE_DJVU
71         /* djvu: */
72         {"image/vnd.djvu",             EV_BACKEND_DJVU, djvu_document_get_type},
73 #endif          
74
75 #ifdef ENABLE_DVI
76         /* dvi: */
77         {"application/x-dvi",          EV_BACKEND_DVI,  dvi_document_get_type},
78 #endif
79
80 #ifdef ENABLE_COMICS
81         /* cbr/cbz: */
82         {"application/x-cbr",           EV_BACKEND_COMICS,  comics_document_get_type},
83         {"application/x-cbz",           EV_BACKEND_COMICS,  comics_document_get_type},
84 #endif
85 };
86
87 #ifdef ENABLE_PIXBUF
88
89 static GList*
90 gdk_pixbuf_mime_type_list ()
91 {
92         GSList *formats, *list;
93         GList *result;
94
95         formats = gdk_pixbuf_get_formats ();
96         result = NULL;
97
98         for (list = formats; list != NULL; list = list->next) {
99                 GdkPixbufFormat *format = list->data;
100                 int i;
101                 gchar **mime_types;
102
103                 if (gdk_pixbuf_format_is_disabled (format))
104                         continue;
105
106                 mime_types = gdk_pixbuf_format_get_mime_types (format);
107
108                 for (i = 0; mime_types[i] != NULL; i++) {
109                         result = g_list_append (result, mime_types[i]);
110                 }
111         }
112         g_slist_free (formats);
113
114         return result;
115 }
116
117 /* Would be nice to have this in gdk-pixbuf */
118 static gboolean
119 mime_type_supported_by_gdk_pixbuf (const gchar *mime_type)
120 {
121         GList *mime_types;
122         GList *list;
123         gboolean retval = FALSE;
124         
125         mime_types = gdk_pixbuf_mime_type_list ();
126         for (list = mime_types; list; list = list->next) {
127                 if (strcmp ((char *)list->data, mime_type) == 0) {
128                         retval = TRUE;
129                         break;
130                 }
131         }
132         
133         g_list_foreach (mime_types, (GFunc)g_free, NULL);
134         g_list_free (mime_types);
135
136         return retval;
137 }
138 #endif
139
140 static GType
141 ev_document_type_get_from_mime (const char *mime_type)
142 {
143         int i;
144         
145         g_return_val_if_fail (mime_type, G_TYPE_INVALID);
146
147         for (i = 0; i < G_N_ELEMENTS (document_types); i++) {
148                 if (strcmp (mime_type, document_types[i].mime_type) == 0) {
149                         g_assert (document_types[i].document_type_factory_callback != NULL);
150                         return document_types[i].document_type_factory_callback();
151                 }
152         }
153 #ifdef ENABLE_PIXBUF
154         if (mime_type_supported_by_gdk_pixbuf (mime_type)) {
155                 return pixbuf_document_get_type ();
156         }
157 #endif
158
159         return G_TYPE_INVALID;
160 }
161
162 EvDocument *
163 ev_document_factory_get_document (const char *mime_type)
164 {
165         GType type = G_TYPE_INVALID;
166         
167         type = ev_document_type_get_from_mime (mime_type);
168
169         if (type != G_TYPE_INVALID) {
170                 return g_object_new (type, NULL);
171         }
172                 
173         return NULL;
174 }
175
176 EvBackend
177 ev_document_factory_get_backend (EvDocument *document)
178 {
179         int i;
180
181         for (i = 0; i < G_N_ELEMENTS (document_types); i++) {
182                 GType type = document_types[i].document_type_factory_callback ();
183                 if (type == G_TYPE_FROM_INSTANCE (document)) {
184                         return  document_types[i].backend;
185                 }
186         }
187
188 #ifdef ENABLE_PIXBUF
189         if (G_TYPE_FROM_INSTANCE (document) == pixbuf_document_get_type ())
190                 return EV_BACKEND_PIXBUF;
191 #endif
192         g_assert_not_reached ();
193         
194         return 0;
195 }
196
197 GList *
198 ev_document_factory_get_mime_types (EvBackend backend)
199 {
200         GList *types = NULL;
201         int i;
202         
203 #ifdef ENABLE_PIXBUF
204         if (backend == EV_BACKEND_PIXBUF) {
205                 return gdk_pixbuf_mime_type_list ();
206         }
207 #endif
208         
209         for (i = 0; i < G_N_ELEMENTS (document_types); i++) {
210                 if (document_types[i].backend == backend) {
211                         types = g_list_append (types, g_strdup (document_types[i].mime_type));
212                 }
213         }
214
215         return types;
216 }
217
218 GList *
219 ev_document_factory_get_all_mime_types (void)
220 {
221         GList *types = NULL;
222         int i;
223         
224         for (i = 0; i < G_N_ELEMENTS (document_types); i++) {
225                 types = g_list_append (types, g_strdup (document_types[i].mime_type));
226         }
227         
228 #ifdef ENABLE_PIXBUF
229         types = g_list_concat (types, gdk_pixbuf_mime_type_list ());
230 #endif
231
232         return types;
233 }