]> www.fi.muni.cz Git - evince.git/blob - backend/ev-document-factory.c
0f85c7c5ee4bc649fce94f16ac0a9ec7a2dfa850
[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 #ifdef ENABLE_PIXBUF
75 /* Would be nice to have this in gdk-pixbuf */
76 static gboolean
77 mime_type_supported_by_gdk_pixbuf (const gchar *mime_type)
78 {
79         GSList *formats, *list;
80         gboolean retval = FALSE;
81
82         formats = gdk_pixbuf_get_formats ();
83
84         list = formats;
85         while (list) {
86                 GdkPixbufFormat *format = list->data;
87                 int i;
88                 gchar **mime_types;
89
90                 if (gdk_pixbuf_format_is_disabled (format))
91                         continue;
92
93                 mime_types = gdk_pixbuf_format_get_mime_types (format);
94
95                 for (i = 0; mime_types[i] != NULL; i++) {
96                         if (strcmp (mime_types[i], mime_type) == 0) {
97                                 retval = TRUE;
98                                 break;
99                         }
100                 }
101
102                 if (retval)
103                         break;
104
105                 list = list->next;
106         }
107
108         g_slist_free (formats);
109
110         return retval;
111 }
112 #endif
113
114 static GType
115 ev_document_type_get_from_mime (const char *mime_type)
116 {
117         int i;
118         
119         g_return_val_if_fail (mime_type, G_TYPE_INVALID);
120
121         for (i = 0; i < G_N_ELEMENTS (document_types); i++) {
122                 if (strcmp (mime_type, document_types[i].mime_type) == 0) {
123                         g_assert (document_types[i].document_type_factory_callback != NULL);
124                         return document_types[i].document_type_factory_callback();
125                 }
126         }
127 #ifdef ENABLE_PIXBUF
128         if (mime_type_supported_by_gdk_pixbuf (mime_type)) {
129                 return pixbuf_document_get_type ();
130         }
131 #endif
132
133         return G_TYPE_INVALID;
134 }
135
136 EvDocument *
137 ev_document_factory_get_document (const char *mime_type)
138 {
139         GType type = G_TYPE_INVALID;
140         
141         type = ev_document_type_get_from_mime (mime_type);
142
143         if (type != G_TYPE_INVALID) {
144                 return g_object_new (type, NULL);
145         }
146                 
147         return NULL;
148 }
149
150 EvBackend
151 ev_document_factory_get_backend (EvDocument *document)
152 {
153         int i;
154
155         for (i = 0; i < G_N_ELEMENTS (document_types); i++) {
156                 GType type = document_types[i].document_type_factory_callback ();
157                 if (type == G_TYPE_FROM_INSTANCE (document)) {
158                         return  document_types[i].backend;
159                 }
160         }
161
162         g_assert_not_reached ();
163         
164         return 0;
165 }
166
167 GList *
168 ev_document_factory_get_mime_types (EvBackend backend)
169 {
170         GList *types = NULL;
171         int i;
172         
173         for (i = 0; i < G_N_ELEMENTS (document_types); i++) {
174                 if (document_types[i].backend == backend) {
175                         types = g_list_append (types, g_strdup (document_types[i].mime_type));
176                 }
177         }
178
179         return types;
180 }
181
182 GList *
183 ev_document_factory_get_all_mime_types (void)
184 {
185         GList *types = NULL;
186         int i;
187         
188         for (i = 0; i < G_N_ELEMENTS (document_types); i++) {
189                 types = g_list_append (types, g_strdup (document_types[i].mime_type));
190         }
191
192         return types;
193 }