1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; c-indent-level: 8 -*- */
3 * Copyright (C) 2005, Red Hat, Inc.
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)
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.
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.
25 #include "ev-document-factory.h"
27 /* The various document type backends: */
28 #include "ev-poppler.h"
29 #include "pixbuf-document.h"
30 #include "tiff-document.h"
32 #include "ps-document.h"
35 #include "dvi-document.h"
38 #include "djvu-document.h"
41 #include "comics-document.h"
46 typedef struct _EvDocumentType EvDocumentType;
47 struct _EvDocumentType
49 const char *mime_type;
51 GType (*document_type_factory_callback)();
54 const EvDocumentType document_types[] = {
56 {"application/pdf", EV_BACKEND_PDF, pdf_document_get_type},
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},
67 {"image/tiff", EV_BACKEND_TIFF, tiff_document_get_type},
72 {"image/vnd.djvu", EV_BACKEND_DJVU, djvu_document_get_type},
77 {"application/x-dvi", EV_BACKEND_DVI, dvi_document_get_type},
82 {"application/x-cbr", EV_BACKEND_COMICS, comics_document_get_type},
83 {"application/x-cbz", EV_BACKEND_COMICS, comics_document_get_type},
90 gdk_pixbuf_mime_type_list ()
92 GSList *formats, *list;
95 formats = gdk_pixbuf_get_formats ();
98 for (list = formats; list != NULL; list = list->next) {
99 GdkPixbufFormat *format = list->data;
103 if (gdk_pixbuf_format_is_disabled (format))
106 mime_types = gdk_pixbuf_format_get_mime_types (format);
108 for (i = 0; mime_types[i] != NULL; i++) {
109 result = g_list_append (result, mime_types[i]);
112 g_slist_free (formats);
117 /* Would be nice to have this in gdk-pixbuf */
119 mime_type_supported_by_gdk_pixbuf (const gchar *mime_type)
123 gboolean retval = FALSE;
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) {
133 g_list_foreach (mime_types, (GFunc)g_free, NULL);
134 g_list_free (mime_types);
141 ev_document_type_get_from_mime (const char *mime_type)
145 g_return_val_if_fail (mime_type, G_TYPE_INVALID);
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();
154 if (mime_type_supported_by_gdk_pixbuf (mime_type)) {
155 return pixbuf_document_get_type ();
159 return G_TYPE_INVALID;
163 ev_document_factory_get_document (const char *mime_type)
165 GType type = G_TYPE_INVALID;
167 type = ev_document_type_get_from_mime (mime_type);
169 if (type != G_TYPE_INVALID) {
170 return g_object_new (type, NULL);
177 ev_document_factory_get_backend (EvDocument *document)
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;
189 if (G_TYPE_FROM_INSTANCE (document) == pixbuf_document_get_type ())
190 return EV_BACKEND_PIXBUF;
192 g_assert_not_reached ();
198 ev_document_factory_get_mime_types (EvBackend backend)
204 if (backend == EV_BACKEND_PIXBUF) {
205 return gdk_pixbuf_mime_type_list ();
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));
219 ev_document_factory_get_all_mime_types (void)
224 for (i = 0; i < G_N_ELEMENTS (document_types); i++) {
225 types = g_list_append (types, g_strdup (document_types[i].mime_type));
229 types = g_list_concat (types, gdk_pixbuf_mime_type_list ());