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