]> www.fi.muni.cz Git - evince.git/blob - backend/ev-document-factory.c
Allow to enable/disable the pixbuf backend. Default to off for now. If we
[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 #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
41 typedef struct _EvDocumentType EvDocumentType;
42 struct _EvDocumentType
43 {
44         const char *mime_type;
45         EvBackend backend;
46         GType (*document_type_factory_callback)();
47 };
48
49 const EvDocumentType document_types[] = {
50         /* PDF: */
51         {"application/pdf",            EV_BACKEND_PDF,  pdf_document_get_type},
52
53         /* Postscript: */
54         {"application/postscript",     EV_BACKEND_PS,   ps_document_get_type},
55         {"application/x-gzpostscript", EV_BACKEND_PS,   ps_document_get_type},
56         {"image/x-eps",                EV_BACKEND_PS,   ps_document_get_type},
57
58 #ifdef ENABLE_TIFF
59         /* Tiff: */
60         {"image/tiff",                 EV_BACKEND_TIFF, tiff_document_get_type},
61 #endif
62
63 #ifdef ENABLE_DJVU
64         /* djvu: */
65         {"image/vnd.djvu",             EV_BACKEND_DJVU, djvu_document_get_type},
66 #endif          
67
68 #ifdef ENABLE_DVI
69         /* dvi: */
70         {"application/x-dvi",          EV_BACKEND_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
113 static GType
114 ev_document_type_get_from_mime (const char *mime_type)
115 {
116         int i;
117         
118         g_return_val_if_fail (mime_type, G_TYPE_INVALID);
119
120         for (i = 0; i < G_N_ELEMENTS (document_types); i++) {
121                 if (strcmp (mime_type, document_types[i].mime_type) == 0) {
122                         g_assert (document_types[i].document_type_factory_callback != NULL);
123                         return document_types[i].document_type_factory_callback();
124                 }
125         }
126 #ifdef ENABLE_PIXBUF
127         if (mime_type_supported_by_gdk_pixbuf (mime_type)) {
128                 return pixbuf_document_get_type ();
129         }
130 #endif
131
132         return G_TYPE_INVALID;
133 }
134
135 EvDocument *
136 ev_document_factory_get_document (const char *mime_type)
137 {
138         GType type = G_TYPE_INVALID;
139         
140         type = ev_document_type_get_from_mime (mime_type);
141
142         if (type != G_TYPE_INVALID) {
143                 return g_object_new (type, NULL);
144         }
145                 
146         return NULL;
147 }
148
149 EvBackend
150 ev_document_factory_get_backend (EvDocument *document)
151 {
152         int i;
153
154         for (i = 0; i < G_N_ELEMENTS (document_types); i++) {
155                 GType type = document_types[i].document_type_factory_callback ();
156                 if (type == G_TYPE_FROM_INSTANCE (document)) {
157                         return  document_types[i].backend;
158                 }
159         }
160
161         g_assert_not_reached ();
162         
163         return 0;
164 }
165
166 GList *
167 ev_document_factory_get_mime_types (EvBackend backend)
168 {
169         GList *types = NULL;
170         int i;
171         
172         for (i = 0; i < G_N_ELEMENTS (document_types); i++) {
173                 if (document_types[i].backend == backend) {
174                         types = g_list_append (types, g_strdup (document_types[i].mime_type));
175                 }
176         }
177
178         return types;
179 }
180
181 GList *
182 ev_document_factory_get_all_mime_types (void)
183 {
184         GList *types = NULL;
185         int i;
186         
187         for (i = 0; i < G_N_ELEMENTS (document_types); i++) {
188                 types = g_list_append (types, g_strdup (document_types[i].mime_type));
189         }
190
191         return types;
192 }