]> www.fi.muni.cz Git - evince.git/blob - backend/ev-document-factory.c
Included option in configure to make ps backend compilation optional with
[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 /* Would be nice to have this in gdk-pixbuf */
89 static gboolean
90 mime_type_supported_by_gdk_pixbuf (const gchar *mime_type)
91 {
92         GSList *formats, *list;
93         gboolean retval = FALSE;
94
95         formats = gdk_pixbuf_get_formats ();
96
97         list = formats;
98         while (list) {
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                         if (strcmp (mime_types[i], mime_type) == 0) {
110                                 retval = TRUE;
111                                 break;
112                         }
113                 }
114
115                 if (retval)
116                         break;
117
118                 list = list->next;
119         }
120
121         g_slist_free (formats);
122
123         return retval;
124 }
125 #endif
126
127 static GType
128 ev_document_type_get_from_mime (const char *mime_type)
129 {
130         int i;
131         
132         g_return_val_if_fail (mime_type, G_TYPE_INVALID);
133
134         for (i = 0; i < G_N_ELEMENTS (document_types); i++) {
135                 if (strcmp (mime_type, document_types[i].mime_type) == 0) {
136                         g_assert (document_types[i].document_type_factory_callback != NULL);
137                         return document_types[i].document_type_factory_callback();
138                 }
139         }
140 #ifdef ENABLE_PIXBUF
141         if (mime_type_supported_by_gdk_pixbuf (mime_type)) {
142                 return pixbuf_document_get_type ();
143         }
144 #endif
145
146         return G_TYPE_INVALID;
147 }
148
149 EvDocument *
150 ev_document_factory_get_document (const char *mime_type)
151 {
152         GType type = G_TYPE_INVALID;
153         
154         type = ev_document_type_get_from_mime (mime_type);
155
156         if (type != G_TYPE_INVALID) {
157                 return g_object_new (type, NULL);
158         }
159                 
160         return NULL;
161 }
162
163 EvBackend
164 ev_document_factory_get_backend (EvDocument *document)
165 {
166         int i;
167
168         for (i = 0; i < G_N_ELEMENTS (document_types); i++) {
169                 GType type = document_types[i].document_type_factory_callback ();
170                 if (type == G_TYPE_FROM_INSTANCE (document)) {
171                         return  document_types[i].backend;
172                 }
173         }
174
175         g_assert_not_reached ();
176         
177         return 0;
178 }
179
180 GList *
181 ev_document_factory_get_mime_types (EvBackend backend)
182 {
183         GList *types = NULL;
184         int i;
185         
186         for (i = 0; i < G_N_ELEMENTS (document_types); i++) {
187                 if (document_types[i].backend == backend) {
188                         types = g_list_append (types, g_strdup (document_types[i].mime_type));
189                 }
190         }
191
192         return types;
193 }
194
195 GList *
196 ev_document_factory_get_all_mime_types (void)
197 {
198         GList *types = NULL;
199         int i;
200         
201         for (i = 0; i < G_N_ELEMENTS (document_types); i++) {
202                 types = g_list_append (types, g_strdup (document_types[i].mime_type));
203         }
204
205         return types;
206 }