From 31e16bd8f386dc292e60489d4a17227804bdc2e6 Mon Sep 17 00:00:00 2001 From: Carlos Garcia Campos Date: Fri, 28 Dec 2007 18:13:32 +0000 Subject: Plugin system for backends. Fixes bug #351348. 2007-12-28 Carlos Garcia Campos * configure.ac: * Makefile.am: * po/POTFILES.in: * backend/comics/Makefile.am: * backend/comics/comics-document.[ch]: * backend/comics/comicsdocument.evince-backend.in: * backend/djvu/Makefile.am: * backend/djvu/djvu-document.[ch]: * backend/djvu/djvudocument.evince-backend.in: * backend/dvi/Makefile.am: * backend/dvi/dvi-document.[c]: * backend/dvi/dvidocument.evince-backend.in: * backend/impress/Makefile.am: * backend/impress/impress-document.[ch]: * backend/impress/impressdocument.evince-backend.in: * backend/pdf/Makefile.am: * backend/pdf/ev-poppler.cc: * backend/pdf/ev-poppler.h: * backend/pdf/pdfdocument.evince-backend.in: * backend/pixbuf/Makefile.am: * backend/pixbuf/pixbuf-document.[ch]: * backend/pixbuf/pixbufdocument.evince-backend.in: * backend/ps/Makefile.am: * backend/ps/ev-spectre.[ch]: * backend/ps/ps-document.[ch]: * backend/ps/psdocument.evince-backend.in: * backend/tiff/Makefile.am: * backend/tiff/tiff-document.[ch]: * backend/tiff/tiffdocument.evince-backend.in: * libdocument/Makefile.am: * libdocument/ev-backends-manager.[ch]: * libdocument/ev-module.[ch]: * libdocument/ev-document.h: * libdocument/ev-document-factory.[ch]: (gdk_pixbuf_mime_type_list), (mime_type_supported_by_gdk_pixbuf), (get_compression_from_mime_type), (get_document_from_uri), (ev_document_factory_get_document), (file_filter_add_mime_types), (ev_document_factory_add_filters): * shell/Makefile.am: * shell/ev-window-title.c: (get_filename_from_uri): * shell/main.c: (main): * thumbnailer/Makefile.am: * thumbnailer/evince-thumbnailer.c: (main): Plugin system for backends. Fixes bug #351348. svn path=/trunk/; revision=2786 --- (limited to 'libdocument') diff --git a/libdocument/Makefile.am b/libdocument/Makefile.am index f96d019..d6e0c56 100644 --- a/libdocument/Makefile.am +++ b/libdocument/Makefile.am @@ -1,109 +1,77 @@ -INCLUDES= \ - -DEVINCE_UIDIR=\"$(pkgdatadir)\" \ - -DGNOMELOCALEDIR=\"$(datadir)/locale\" \ - -I$(top_srcdir)/backend/pdf \ - -I$(top_srcdir)/backend/pixbuf \ - -I$(top_srcdir)/backend/tiff \ - -I$(top_srcdir)/backend/ps \ - -I$(top_srcdir)/backend/djvu \ - -I$(top_srcdir)/backend/dvi \ - -I$(top_srcdir)/backend/impress \ - -I$(top_srcdir)/backend/comics \ - $(BACKEND_CFLAGS) \ - $(WARN_CFLAGS) \ +INCLUDES= \ + -DEVINCE_UIDIR=\"$(pkgdatadir)\" \ + -DGNOMELOCALEDIR=\"$(datadir)/locale\" \ + -DEV_BACKENDSDIR=\"$(libdir)/evince/backends\" \ + $(LIB_CFLAGS) \ + $(WARN_CFLAGS) \ $(DISABLE_DEPRECATED) -noinst_LTLIBRARIES = libevbackend.la +lib_LTLIBRARIES = libevbackend.la + +libevbackend_la_LDFLAGS = -export-dynamic +libevbackend_la_LIBADD = $(LIB_LIBS) + +NOINST_H_FILES = \ + ev-backend-marshalers.h \ + ev-backends-manager.h \ + ev-document-factory.h \ + ev-module.h + +INST_H_FILES = \ + ev-async-renderer.h \ + ev-attachment.h \ + ev-document-find.h \ + ev-document-fonts.h \ + ev-document-forms.h \ + ev-document.h \ + ev-document-images.h \ + ev-document-info.h \ + ev-document-links.h \ + ev-document-misc.h \ + ev-document-security.h \ + ev-document-thumbnails.h \ + ev-document-transition.h \ + ev-file-exporter.h \ + ev-file-helpers.h \ + ev-form-field.h \ + ev-image.h \ + ev-link-action.h \ + ev-link-dest.h \ + ev-link.h \ + ev-render-context.h \ + ev-selection.h + +headerdir = $(prefix)/include/evince-@EV_API_VERSION@/evince +header_DATA = $(INST_H_FILES) libevbackend_la_SOURCES= \ ev-async-renderer.c \ - ev-async-renderer.h \ ev-attachment.c \ - ev-attachment.h \ + ev-backends-manager.c \ ev-backend-marshal.c \ ev-link.c \ - ev-link.h \ ev-link-action.c \ - ev-link-action.h \ ev-link-dest.c \ - ev-link-dest.h \ ev-image.c \ - ev-image.h \ ev-document.c \ - ev-document.h \ ev-document-factory.c \ - ev-document-factory.h \ ev-document-thumbnails.c \ - ev-document-thumbnails.h \ ev-document-fonts.c \ - ev-document-fonts.h \ ev-document-links.c \ - ev-document-links.h \ ev-document-images.c \ - ev-document-images.h \ ev-document-security.c \ - ev-document-security.h \ ev-document-find.c \ - ev-document-find.h \ - ev-document-info.h \ - ev-document-transition.h \ ev-document-transition.c \ - ev-document-forms.h \ ev-document-forms.c \ - ev-form-field.h \ ev-form-field.c \ ev-file-exporter.c \ - ev-file-exporter.h \ ev-file-helpers.c \ - ev-file-helpers.h \ - ev-render-context.h \ + ev-module.c \ ev-render-context.c \ - ev-selection.h \ ev-selection.c \ - ev-document-misc.h \ - ev-document-misc.c - -libevbackend_la_LIBADD = - -if ENABLE_PDF -libevbackend_la_LIBADD += \ - $(top_builddir)/backend/pdf/libpdfdocument.la -endif - -if ENABLE_PS -libevbackend_la_LIBADD += \ - $(top_builddir)/backend/ps/libpsdocument.la -endif - -if ENABLE_PIXBUF -libevbackend_la_LIBADD += \ - $(top_builddir)/backend/pixbuf/libpixbufdocument.la -endif - -if ENABLE_DJVU -libevbackend_la_LIBADD += \ - $(top_builddir)/backend/djvu/libgtkdjvu.la -endif - -if ENABLE_TIFF -libevbackend_la_LIBADD += \ - $(top_builddir)/backend/tiff/libtiffdocument.la -endif - -if ENABLE_DVI -libevbackend_la_LIBADD += \ - $(top_builddir)/backend/dvi/libgtkdvi.la -endif - -if ENABLE_COMICS -libevbackend_la_LIBADD += \ - $(top_builddir)/backend/comics/libcomicsdocument.la -endif - -if ENABLE_IMPRESS -libevbackend_la_LIBADD += \ - $(top_builddir)/backend/impress/libimpressdocument.la -endif + ev-document-misc.c \ + $(NOINST_H_FILES) \ + $(INST_H_FILES) BUILT_SOURCES= \ ev-backend-marshalers.h \ @@ -119,8 +87,5 @@ ev-backend-marshalers.c: ev-backend-marshalers.list ev-backend-marshal.c: ev-backend-marshalers.h ev-backend-marshalers.c -noinst_HEADERS = \ - ev-backend-marshalers.h - EXTRA_DIST= \ ev-backend-marshalers.list diff --git a/libdocument/ev-backends-manager.c b/libdocument/ev-backends-manager.c new file mode 100644 index 0000000..0f501a1 --- /dev/null +++ b/libdocument/ev-backends-manager.c @@ -0,0 +1,291 @@ +/* this file is part of evince, a gnome document viewer + * + * Copyright (C) 2007 Carlos Garcia Campos + * + * Evince is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Evince is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. + */ + +#include + +#include + +#include "ev-module.h" +#include "ev-backends-manager.h" + +static GList *ev_backends_list = NULL; + +typedef struct _EvBackendInfo EvBackendInfo; +struct _EvBackendInfo { + gchar *module_name; + GTypeModule *module; + + GType type_id; + + gchar *type_desc; + gchar **mime_types; +}; + +#define EV_BACKENDS_GROUP "Evince Backend" +#define EV_BACKENDS_EXTENSION ".evince-backend" + +static void +ev_backend_info_free (EvBackendInfo *info) +{ + g_free (info->module_name); + g_free (info->type_desc); + g_strfreev (info->mime_types); + g_free (info); +} + +static EvBackendInfo * +ev_backends_manager_load_backend (const gchar *file) +{ + EvBackendInfo *info; + GKeyFile *backend_file = NULL; + GError *error = NULL; + + backend_file = g_key_file_new (); + if (!g_key_file_load_from_file (backend_file, file, G_KEY_FILE_NONE, &error)) { + g_warning ("Error opening backend file %s: %s", + file, error->message); + g_error_free (error); + g_key_file_free (backend_file); + + return NULL; + } + + info = g_new0 (EvBackendInfo, 1); + info->module_name = g_key_file_get_string (backend_file, EV_BACKENDS_GROUP, + "Module", NULL); + if (!info->module_name) { + g_warning ("Bad evince backend file %s: Could not find 'Module'", + file); + ev_backend_info_free (info); + g_key_file_free (backend_file); + + return NULL; + } + + info->type_desc = g_key_file_get_locale_string (backend_file, EV_BACKENDS_GROUP, + "TypeDescription", NULL, NULL); + if (!info->type_desc) { + g_warning ("Bad evince backend file %s: Could not find 'TypeDescription'", + file); + ev_backend_info_free (info); + g_key_file_free (backend_file); + + return NULL; + } + + info->mime_types = g_key_file_get_string_list (backend_file, EV_BACKENDS_GROUP, + "MimeType", NULL, NULL); + if (!info->mime_types) { + g_warning ("Bad evince backend file %s: Could not find 'MimeType'", + file); + ev_backend_info_free (info); + g_key_file_free (backend_file); + + return NULL; + } + + g_key_file_free (backend_file); + + return info; +} + +static gboolean +ev_backends_manager_load (void) +{ + GDir *dir; + const gchar *dirent; + GError *error = NULL; + + dir = g_dir_open (EV_BACKENDSDIR, 0, &error); + if (!dir) { + g_warning (error->message); + g_error_free (error); + + return FALSE; + } + + while ((dirent = g_dir_read_name (dir))) { + EvBackendInfo *info; + gchar *file; + + if (!g_str_has_suffix (dirent, EV_BACKENDS_EXTENSION)) + continue; + + file = g_build_filename (EV_BACKENDSDIR, dirent, NULL); + info = ev_backends_manager_load_backend (file); + g_free (file); + + if (!info) + continue; + + ev_backends_list = g_list_prepend (ev_backends_list, info); + } + + return TRUE; +} + +gboolean +ev_backends_manager_init (void) +{ + if (ev_backends_list) + return FALSE; + + return ev_backends_manager_load (); +} + +void +ev_backends_manager_shutdown (void) +{ + g_list_foreach (ev_backends_list, (GFunc)ev_backend_info_free, NULL); + g_list_free (ev_backends_list); + ev_backends_list = NULL; +} + +static EvBackendInfo * +ev_backends_manager_get_backend_info (const gchar *mime_type) +{ + GList *l; + + for (l = ev_backends_list; l; l = g_list_next (l)) { + EvBackendInfo *info; + gint i = 0; + const char *mime; + + info = (EvBackendInfo *)l->data; + + while ((mime = info->mime_types[i++])) { + if (g_ascii_strcasecmp (mime, mime_type) == 0) + return info; + } + } + + return NULL; +} + +EvDocument * +ev_backends_manager_get_document (const gchar *mime_type) +{ + EvDocument *document; + EvBackendInfo *info; + + info = ev_backends_manager_get_backend_info (mime_type); + if (!info) + return NULL; + + if (!info->module) { + gchar *path; + + path = g_module_build_path (EV_BACKENDSDIR, info->module_name); + info->module = G_TYPE_MODULE (ev_module_new (path)); + g_free (path); + } + + if (!g_type_module_use (info->module)) { + g_warning ("Cannot load backend '%s' since file '%s' cannot be read.", + info->module_name, + ev_module_get_path (EV_MODULE (info->module))); + g_object_unref (G_OBJECT (info->module)); + info->module = NULL; + + return NULL; + } + + document = EV_DOCUMENT (ev_module_new_object (EV_MODULE (info->module))); + g_type_module_unuse (info->module); + + return document; +} + +static EvBackendInfo * +get_document_backend_info (EvDocument *document) +{ + GList *l; + + for (l = ev_backends_list; l; l = g_list_next (l)) { + EvBackendInfo *info; + GType type_id; + + info = (EvBackendInfo *)l->data; + + if (!info->module) + continue; + + type_id = ev_module_get_object_type (EV_MODULE (info->module)); + + if (G_TYPE_CHECK_INSTANCE_TYPE (document, type_id)) { + return info; + } + } + + return NULL; +} + +const gchar * +ev_backends_manager_get_document_module_name (EvDocument *document) +{ + EvBackendInfo *info; + + info = get_document_backend_info (document); + return info ? info->module_name : NULL; +} + +static EvTypeInfo * +ev_type_info_new (const gchar *desc, const gchar **mime_types) +{ + EvTypeInfo *info; + + info = g_new (EvTypeInfo, 1); + + info->desc = desc; + info->mime_types = mime_types; + + return info; +} + +EvTypeInfo * +ev_backends_manager_get_document_type_info (EvDocument *document) +{ + EvBackendInfo *info; + + info = get_document_backend_info (document); + return info ? + ev_type_info_new (info->type_desc, + (const gchar **)info->mime_types) + : NULL; +} + +GList * +ev_backends_manager_get_all_types_info (void) +{ + GList *l; + GList *retval = NULL; + + for (l = ev_backends_list; l; l = g_list_next (l)) { + EvBackendInfo *info; + EvTypeInfo *type_info; + + info = (EvBackendInfo *)l->data; + + type_info = ev_type_info_new (info->type_desc, + (const gchar **)info->mime_types); + retval = g_list_prepend (retval, type_info); + } + + return retval; +} diff --git a/libdocument/ev-backends-manager.h b/libdocument/ev-backends-manager.h new file mode 100644 index 0000000..a78d6f8 --- /dev/null +++ b/libdocument/ev-backends-manager.h @@ -0,0 +1,44 @@ +/* this file is part of evince, a gnome document viewer + * + * Copyright (C) 2007 Carlos Garcia Campos + * + * Evince is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Evince is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. + */ + +#ifndef EV_BACKENDS_MANAGER +#define EV_BACKENDS_MANAGER + +#include + +#include "ev-document.h" + +G_BEGIN_DECLS + +typedef struct _EvTypeInfo { + const gchar *desc; + const gchar **mime_types; +} EvTypeInfo; + +gboolean ev_backends_manager_init (void); +void ev_backends_manager_shutdown (void); + +EvDocument *ev_backends_manager_get_document (const gchar *mime_type); +const gchar *ev_backends_manager_get_document_module_name (EvDocument *document); +EvTypeInfo *ev_backends_manager_get_document_type_info (EvDocument *document); +GList *ev_backends_manager_get_all_types_info (void); + +G_END_DECLS + +#endif /* EV_BACKENDS_MANAGER */ diff --git a/libdocument/ev-document-factory.c b/libdocument/ev-document-factory.c index b500fdf..21e0b4a 100644 --- a/libdocument/ev-document-factory.c +++ b/libdocument/ev-document-factory.c @@ -22,34 +22,6 @@ #include "config.h" #endif -#include "ev-document-factory.h" - -/* The various document type backends: */ -#ifdef ENABLE_PDF -#include "ev-poppler.h" -#endif -#ifdef ENABLE_PS -#include "ps-document.h" -#endif -#ifdef ENABLE_TIFF -#include "tiff-document.h" -#endif -#ifdef ENABLE_DVI -#include "dvi-document.h" -#endif -#ifdef ENABLE_PIXBUF -#include "pixbuf-document.h" -#endif -#ifdef ENABLE_DJVU -#include "djvu-document.h" -#endif -#ifdef ENABLE_COMICS -#include "comics-document.h" -#endif -#ifdef ENABLE_IMPRESS -#include "impress-document.h" -#endif - #include #include #include @@ -58,87 +30,27 @@ #include #include +#include "ev-backends-manager.h" +#include "ev-document-factory.h" #include "ev-file-helpers.h" -typedef struct _EvDocumentType EvDocumentType; -struct _EvDocumentType { - const char *mime_type; - EvCompressionType compression; - EvBackend backend; - GType (*document_type_factory_callback)(); -}; - -const EvDocumentType document_types[] = { -#ifdef ENABLE_PDF - /* PDF: */ - {"application/pdf", EV_COMPRESSION_NONE, EV_BACKEND_PDF, pdf_document_get_type}, - {"application/x-bzpdf", EV_COMPRESSION_BZIP2, EV_BACKEND_PDF, pdf_document_get_type}, - {"application/x-gzpdf", EV_COMPRESSION_GZIP, EV_BACKEND_PDF, pdf_document_get_type}, -#endif - -#ifdef ENABLE_PS - /* Postscript: */ - {"application/postscript", EV_COMPRESSION_NONE, EV_BACKEND_PS, ps_document_get_type}, - {"application/x-bzpostscript", EV_COMPRESSION_BZIP2, EV_BACKEND_PS, ps_document_get_type}, - {"application/x-gzpostscript", EV_COMPRESSION_GZIP, EV_BACKEND_PS, ps_document_get_type}, - {"image/x-eps", EV_COMPRESSION_NONE, EV_BACKEND_PS, ps_document_get_type}, - {"image/x-bzeps", EV_COMPRESSION_BZIP2, EV_BACKEND_PS, ps_document_get_type}, - {"image/x-gzeps", EV_COMPRESSION_GZIP, EV_BACKEND_PS, ps_document_get_type}, -#endif - -#ifdef ENABLE_TIFF - /* Tiff: */ - {"image/tiff", EV_COMPRESSION_NONE, EV_BACKEND_TIFF, tiff_document_get_type}, -#endif - -#ifdef ENABLE_DJVU - /* djvu: */ - {"image/vnd.djvu", EV_COMPRESSION_NONE, EV_BACKEND_DJVU, djvu_document_get_type}, -#endif - -#ifdef ENABLE_DVI - /* dvi: */ - {"application/x-dvi", EV_COMPRESSION_NONE, EV_BACKEND_DVI, dvi_document_get_type}, -#endif - -#ifdef ENABLE_COMICS - /* cbr/cbz: */ - {"application/x-cbr", EV_COMPRESSION_NONE, EV_BACKEND_COMICS, comics_document_get_type}, - {"application/x-cbz", EV_COMPRESSION_NONE, EV_BACKEND_COMICS, comics_document_get_type}, -#endif - -#ifdef ENABLE_IMPRESS - /* Impress slides: */ - {"application/vnd.sun.xml.impress", EV_COMPRESSION_NONE, EV_BACKEND_IMPRESS, impress_document_get_type}, - {"application/vnd.oasis.opendocument.presentation", EV_COMPRESSION_NONE, EV_BACKEND_IMPRESS, impress_document_get_type}, -#endif - -}; - #ifdef ENABLE_PIXBUF - static GList* gdk_pixbuf_mime_type_list () { GSList *formats, *list; - GList *result; + GList *result = NULL; formats = gdk_pixbuf_get_formats (); - result = NULL; - for (list = formats; list != NULL; list = list->next) { GdkPixbufFormat *format = list->data; - int i; - gchar **mime_types; + gchar **mime_types; if (gdk_pixbuf_format_is_disabled (format)) continue; mime_types = gdk_pixbuf_format_get_mime_types (format); - - for (i = 0; mime_types[i] != NULL; i++) { - result = g_list_append (result, mime_types[i]); - } + result = g_list_prepend (result, mime_types); } g_slist_free (formats); @@ -152,110 +64,45 @@ mime_type_supported_by_gdk_pixbuf (const gchar *mime_type) GList *mime_types; GList *list; gboolean retval = FALSE; - + mime_types = gdk_pixbuf_mime_type_list (); for (list = mime_types; list; list = list->next) { - if (strcmp ((char *)list->data, mime_type) == 0) { - retval = TRUE; - break; + gchar **mtypes = (gchar **)list->data; + const gchar *mtype; + gint i = 0; + + while ((mtype = mtypes[i++])) { + if (strcmp (mtype, mime_type) == 0) { + retval = TRUE; + break; + } } } - - g_list_foreach (mime_types, (GFunc)g_free, NULL); + + g_list_foreach (mime_types, (GFunc)g_strfreev, NULL); g_list_free (mime_types); return retval; } -#endif +#endif /* ENABLE_PIXBUF */ -static EvDocument * -ev_document_factory_get_from_mime (const gchar *mime_type, - EvCompressionType *compression) +static EvCompressionType +get_compression_from_mime_type (const gchar *mime_type) { - int i; - GType type = G_TYPE_INVALID; - EvDocument *document = NULL; + gchar type[3]; + gchar *p; - *compression = EV_COMPRESSION_NONE; - - for (i = 0; i < G_N_ELEMENTS (document_types); i++) { - if (strcmp (mime_type, document_types[i].mime_type) == 0) { - g_assert (document_types[i].document_type_factory_callback != NULL); - type = document_types[i].document_type_factory_callback (); - *compression = document_types[i].compression; - break; - } - } -#ifdef ENABLE_PIXBUF - if (type == G_TYPE_INVALID && mime_type_supported_by_gdk_pixbuf (mime_type)) { - type = pixbuf_document_get_type (); - } -#endif - - if (type != G_TYPE_INVALID) { - document = g_object_new (type, NULL); - } - - return document; -} - -EvBackend -ev_document_factory_get_backend (EvDocument *document) -{ - int i; + if (!(p = g_strrstr (mime_type, "/"))) + return EV_COMPRESSION_NONE; - for (i = 0; i < G_N_ELEMENTS (document_types); i++) { - GType type = document_types[i].document_type_factory_callback (); - if (type == G_TYPE_FROM_INSTANCE (document)) { - return document_types[i].backend; - } + if (sscanf (++p, "x-%2s%*s", type) == 1) { + if (g_ascii_strcasecmp (type, "gz") == 0) + return EV_COMPRESSION_GZIP; + else if (g_ascii_strcasecmp (type, "bz") == 0) + return EV_COMPRESSION_BZIP2; } -#ifdef ENABLE_PIXBUF - if (G_TYPE_FROM_INSTANCE (document) == pixbuf_document_get_type ()) - return EV_BACKEND_PIXBUF; -#endif - g_assert_not_reached (); - - return 0; -} - -static GList * -ev_document_factory_get_mime_types (EvBackend backend) -{ - GList *types = NULL; - int i; - -#ifdef ENABLE_PIXBUF - if (backend == EV_BACKEND_PIXBUF) { - return gdk_pixbuf_mime_type_list (); - } -#endif - - for (i = 0; i < G_N_ELEMENTS (document_types); i++) { - if (document_types[i].backend == backend) { - types = g_list_append (types, g_strdup (document_types[i].mime_type)); - } - } - - return types; -} - -static GList * -ev_document_factory_get_all_mime_types (void) -{ - GList *types = NULL; - int i; - - for (i = 0; i < G_N_ELEMENTS (document_types); i++) { - types = g_list_append (types, g_strdup (document_types[i].mime_type)); - } - -#ifdef ENABLE_PIXBUF - types = g_list_concat (types, gdk_pixbuf_mime_type_list ()); -#endif - - return types; + return EV_COMPRESSION_NONE; } static EvDocument * @@ -293,8 +140,15 @@ get_document_from_uri (const char *uri, return NULL; } - document = ev_document_factory_get_from_mime (info->mime_type, compression); - +#ifdef ENABLE_PIXBUF + if (mime_type_supported_by_gdk_pixbuf (info->mime_type)) { + document = ev_backends_manager_get_document ("image/*"); + } else + document = ev_backends_manager_get_document (info->mime_type); +#else + document = ev_backends_manager_get_document (info->mime_type); +#endif /* ENABLE_PIXBUF */ + if (document == NULL) { g_set_error (error, EV_DOCUMENT_ERROR, @@ -304,6 +158,8 @@ get_document_from_uri (const char *uri, return NULL; } + *compression = get_compression_from_mime_type (info->mime_type); + gnome_vfs_file_info_unref (info); return document; @@ -410,106 +266,76 @@ ev_document_factory_get_document (const char *uri, GError **error) } static void -file_filter_add_mime_list_and_free (GtkFileFilter *filter, GList *mime_types) +file_filter_add_mime_types (EvTypeInfo *info, GtkFileFilter *filter) { - GList *l; + const gchar *mime_type; + gint i = 0; - for (l = mime_types; l != NULL; l = l->next) { - gtk_file_filter_add_mime_type (filter, l->data); - } +#ifdef ENABLE_PIXBUF + if (g_ascii_strcasecmp (info->mime_types[0], "image/*") == 0) { + GList *pixbuf_types, *l; + + pixbuf_types = gdk_pixbuf_mime_type_list (); + for (l = pixbuf_types; l; l = g_list_next (l)) { + gchar **mime_types = (gchar **)l->data; + gint j = 0; + + while ((mime_type = mime_types[j++])) + gtk_file_filter_add_mime_type (filter, mime_type); + + g_strfreev (mime_types); + } + g_list_free (pixbuf_types); - g_list_foreach (mime_types, (GFunc)g_free, NULL); - g_list_free (mime_types); + return; + } +#endif /* ENABLE_PIXBUF */ + + while ((mime_type = info->mime_types[i++])) + gtk_file_filter_add_mime_type (filter, mime_type); } void ev_document_factory_add_filters (GtkWidget *chooser, EvDocument *document) { - EvBackend backend = 0; - GList *mime_types; + GList *all_types; GtkFileFilter *filter; GtkFileFilter *default_filter; GtkFileFilter *document_filter; - if (document != NULL) { - backend = ev_document_factory_get_backend (document); - } - + all_types = ev_backends_manager_get_all_types_info (); + default_filter = document_filter = filter = gtk_file_filter_new (); gtk_file_filter_set_name (filter, _("All Documents")); - mime_types = ev_document_factory_get_all_mime_types (); - file_filter_add_mime_list_and_free (filter, mime_types); + g_list_foreach (all_types, (GFunc)file_filter_add_mime_types, filter); gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter); -#ifdef ENABLE_PS - if (document == NULL || backend == EV_BACKEND_PS) { - default_filter = filter = gtk_file_filter_new (); - gtk_file_filter_set_name (filter, _("PostScript Documents")); - mime_types = ev_document_factory_get_mime_types (EV_BACKEND_PS); - file_filter_add_mime_list_and_free (filter, mime_types); - gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter); - } -#endif - -#ifdef ENABLE_PDF - if (document == NULL || backend == EV_BACKEND_PDF) { - default_filter = filter = gtk_file_filter_new (); - gtk_file_filter_set_name (filter, _("PDF Documents")); - mime_types = ev_document_factory_get_mime_types (EV_BACKEND_PDF); - file_filter_add_mime_list_and_free (filter, mime_types); - gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter); - } -#endif + if (document) { + EvTypeInfo *info; -#ifdef ENABLE_PIXBUF - if (document == NULL || backend == EV_BACKEND_PIXBUF) { + info = ev_backends_manager_get_document_type_info (document); default_filter = filter = gtk_file_filter_new (); - gtk_file_filter_set_name (filter, _("Images")); - mime_types = ev_document_factory_get_mime_types (EV_BACKEND_PIXBUF); - file_filter_add_mime_list_and_free (filter, mime_types); + gtk_file_filter_set_name (filter, info->desc); + file_filter_add_mime_types (info, filter); + g_free (info); gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter); - } -#endif + } else { + GList *l; -#ifdef ENABLE_DVI - if (document == NULL || backend == EV_BACKEND_DVI) { - default_filter = filter = gtk_file_filter_new (); - gtk_file_filter_set_name (filter, _("DVI Documents")); - mime_types = ev_document_factory_get_mime_types (EV_BACKEND_DVI); - file_filter_add_mime_list_and_free (filter, mime_types); - gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter); - } -#endif + for (l = all_types; l; l = g_list_next (l)){ + EvTypeInfo *info; -#ifdef ENABLE_DJVU - if (document == NULL || backend == EV_BACKEND_DJVU) { - default_filter = filter = gtk_file_filter_new (); - gtk_file_filter_set_name (filter, _("Djvu Documents")); - mime_types = ev_document_factory_get_mime_types (EV_BACKEND_DJVU); - file_filter_add_mime_list_and_free (filter, mime_types); - gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter); - } -#endif + info = (EvTypeInfo *)l->data; -#ifdef ENABLE_COMICS - if (document == NULL || backend == EV_BACKEND_COMICS) { - default_filter = filter = gtk_file_filter_new (); - gtk_file_filter_set_name (filter, _("Comic Books")); - mime_types = ev_document_factory_get_mime_types (EV_BACKEND_COMICS); - file_filter_add_mime_list_and_free (filter, mime_types); - gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter); + default_filter = filter = gtk_file_filter_new (); + gtk_file_filter_set_name (filter, info->desc); + file_filter_add_mime_types (info, filter); + gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter); + } } -#endif -#ifdef ENABLE_IMPRESS - if (document == NULL || backend == EV_BACKEND_IMPRESS) { - default_filter = filter = gtk_file_filter_new (); - gtk_file_filter_set_name (filter, _("Impress Slides")); - mime_types = ev_document_factory_get_mime_types (EV_BACKEND_IMPRESS); - file_filter_add_mime_list_and_free (filter, mime_types); - gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter); - } -#endif + g_list_foreach (all_types, (GFunc)g_free, NULL); + g_list_free (all_types); filter = gtk_file_filter_new (); gtk_file_filter_set_name (filter, _("All Files")); diff --git a/libdocument/ev-document-factory.h b/libdocument/ev-document-factory.h index 886be69..55c59e4 100644 --- a/libdocument/ev-document-factory.h +++ b/libdocument/ev-document-factory.h @@ -26,19 +26,7 @@ G_BEGIN_DECLS -typedef enum { - EV_BACKEND_PDF, - EV_BACKEND_PS, - EV_BACKEND_TIFF, - EV_BACKEND_PIXBUF, - EV_BACKEND_DJVU, - EV_BACKEND_DVI, - EV_BACKEND_COMICS, - EV_BACKEND_IMPRESS -} EvBackend; - EvDocument* ev_document_factory_get_document (const char *uri, GError **error); -EvBackend ev_document_factory_get_backend (EvDocument *document); void ev_document_factory_add_filters (GtkWidget *chooser, EvDocument *document); G_END_DECLS diff --git a/libdocument/ev-document.h b/libdocument/ev-document.h index 4f83553..6e7232b 100644 --- a/libdocument/ev-document.h +++ b/libdocument/ev-document.h @@ -123,8 +123,74 @@ GList *ev_document_get_attachments (EvDocument *document); cairo_surface_t *ev_document_render (EvDocument *document, EvRenderContext *rc); -gint ev_rect_cmp (EvRectangle *a, - EvRectangle *b); +gint ev_rect_cmp (EvRectangle *a, + EvRectangle *b); + +/* + * Utility macro used to register backends + * + * use: EV_BACKEND_REGISTER_WITH_CODE(BackendName, backend_name, CODE) + */ +#define EV_BACKEND_REGISTER_WITH_CODE(BackendName, backend_name, CODE) \ + \ +static GType g_define_type_id = 0; \ + \ +GType \ +backend_name##_get_type (void) \ +{ \ + return g_define_type_id; \ +} \ + \ +static void backend_name##_init (BackendName *self); \ +static void backend_name##_class_init (BackendName##Class *klass); \ +static gpointer backend_name##_parent_class = NULL; \ +static void backend_name##_class_intern_init (gpointer klass) \ +{ \ + backend_name##_parent_class = g_type_class_peek_parent (klass); \ + backend_name##_class_init ((BackendName##Class *) klass); \ +} \ + \ +G_MODULE_EXPORT GType \ +register_evince_backend (GTypeModule *module) \ +{ \ + static const GTypeInfo our_info = { \ + sizeof (BackendName##Class), \ + NULL, /* base_init */ \ + NULL, /* base_finalize */ \ + (GClassInitFunc) backend_name##_class_intern_init, \ + NULL, \ + NULL, /* class_data */ \ + sizeof (BackendName), \ + 0, /* n_preallocs */ \ + (GInstanceInitFunc) backend_name##_init \ + }; \ + \ + /* Initialise the i18n stuff */ \ + bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR); \ + bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8"); \ + \ + g_define_type_id = g_type_module_register_type (module, \ + G_TYPE_OBJECT, \ + #BackendName, \ + &our_info, \ + (GTypeFlags)0); \ + \ + G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT, \ + backend_name##_document_iface_init); \ + \ + CODE \ + \ + return g_define_type_id; \ +} + +/* + * Utility macro used to register backend + * + * use: EV_BACKEND_REGISTER(BackendName, backend_name) + */ +#define EV_BACKEND_REGISTER(BackendName, backend_name) \ + EV_BACKEND_REGISTER_WITH_CODE(BackendName, backend_name, ;) + G_END_DECLS #endif /* EV_DOCUMENT_H */ diff --git a/libdocument/ev-module.c b/libdocument/ev-module.c new file mode 100644 index 0000000..f2aebe7 --- /dev/null +++ b/libdocument/ev-module.c @@ -0,0 +1,190 @@ +/* + * ev-module.c + * This file is part of Evince + * + * Copyright (C) 2005 - Paolo Maggi + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, + * Boston, MA 02111-1307, USA. + */ + +/* This is a modified version of ephy-module.c from Epiphany source code. + * Here the original copyright assignment: + * + * Copyright (C) 2003 Marco Pesenti Gritti + * Copyright (C) 2003, 2004 Christian Persch + * + */ + +/* + * Modified by the gedit Team, 2005. See the AUTHORS file for a + * list of people on the gedit Team. + * See the ChangeLog files for a list of changes. + * + * $Id: gedit-module.c 5367 2006-12-17 14:29:49Z pborelli $ + */ + +/* Modified by evince team */ + +#include "config.h" + +#include "ev-module.h" + +#include + +typedef struct _EvModuleClass EvModuleClass; + +struct _EvModuleClass { + GTypeModuleClass parent_class; +}; + +struct _EvModule { + GTypeModule parent_instance; + + GModule *library; + + gchar *path; + GType type; +}; + +typedef GType (*EvModuleRegisterFunc) (GTypeModule *); + +static void ev_module_init (EvModule *action); +static void ev_module_class_init (EvModuleClass *class); + +G_DEFINE_TYPE (EvModule, ev_module, G_TYPE_TYPE_MODULE) + +static gboolean +ev_module_load (GTypeModule *gmodule) +{ + EvModule *module = EV_MODULE (gmodule); + EvModuleRegisterFunc register_func; + + module->library = g_module_open (module->path, 0); + + if (!module->library) { + g_warning (g_module_error ()); + + return FALSE; + } + + /* extract symbols from the lib */ + if (!g_module_symbol (module->library, "register_evince_backend", + (void *) ®ister_func)) { + g_warning (g_module_error ()); + g_module_close (module->library); + + return FALSE; + } + + /* symbol can still be NULL even though g_module_symbol + * returned TRUE */ + if (!register_func) { + g_warning ("Symbol 'register_evince_backend' should not be NULL"); + g_module_close (module->library); + + return FALSE; + } + + module->type = register_func (gmodule); + + if (module->type == 0) { + g_warning ("Invalid evince backend contained by module %s", module->path); + + return FALSE; + } + + return TRUE; +} + +static void +ev_module_unload (GTypeModule *gmodule) +{ + EvModule *module = EV_MODULE (gmodule); + + g_module_close (module->library); + + module->library = NULL; + module->type = 0; +} + +const gchar * +ev_module_get_path (EvModule *module) +{ + g_return_val_if_fail (EV_IS_MODULE (module), NULL); + + return module->path; +} + +GObject * +ev_module_new_object (EvModule *module) +{ + g_return_val_if_fail (EV_IS_MODULE (module), NULL); + + if (module->type == 0) + return NULL; + + return g_object_new (module->type, NULL); +} + +GType +ev_module_get_object_type (EvModule *module) +{ + g_return_val_if_fail (EV_IS_MODULE (module), 0); + + return module->type; +} + +static void +ev_module_init (EvModule *module) +{ +} + +static void +ev_module_finalize (GObject *object) +{ + EvModule *module = EV_MODULE (object); + + g_free (module->path); + + G_OBJECT_CLASS (ev_module_parent_class)->finalize (object); +} + +static void +ev_module_class_init (EvModuleClass *class) +{ + GObjectClass *object_class = G_OBJECT_CLASS (class); + GTypeModuleClass *module_class = G_TYPE_MODULE_CLASS (class); + + object_class->finalize = ev_module_finalize; + + module_class->load = ev_module_load; + module_class->unload = ev_module_unload; +} + +EvModule * +ev_module_new (const gchar *path) +{ + EvModule *result; + + g_return_val_if_fail (path != NULL && path[0] != '\0', NULL); + + result = g_object_new (EV_TYPE_MODULE, NULL); + + g_type_module_set_name (G_TYPE_MODULE (result), path); + result->path = g_strdup (path); + + return result; +} diff --git a/libdocument/ev-module.h b/libdocument/ev-module.h new file mode 100644 index 0000000..4239cb1 --- /dev/null +++ b/libdocument/ev-module.h @@ -0,0 +1,69 @@ +/* + * ev-module.h + * This file is part of Evince + * + * Copyright (C) 2005 - Paolo Maggi + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, + * Boston, MA 02111-1307, USA. + */ + +/* This is a modified version of gedit-module.h from Epiphany source code. + * Here the original copyright assignment: + * + * Copyright (C) 2003 Marco Pesenti Gritti + * Copyright (C) 2003, 2004 Christian Persch + * + */ + +/* + * Modified by the gedit Team, 2005. See the AUTHORS file for a + * list of people on the gedit Team. + * See the ChangeLog files for a list of changes. + * + * $Id: gedit-module.h 5263 2006-10-08 14:26:02Z pborelli $ + */ + +/* Modified by Evince Team */ + +#ifndef EV_MODULE_H +#define EV_MODULE_H + +#include + +G_BEGIN_DECLS + +#define EV_TYPE_MODULE (ev_module_get_type ()) +#define EV_MODULE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), EV_TYPE_MODULE, EvModule)) +#define EV_MODULE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), EV_TYPE_MODULE, EvModuleClass)) +#define EV_IS_MODULE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), EV_TYPE_MODULE)) +#define EV_IS_MODULE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((obj), EV_TYPE_MODULE)) +#define EV_MODULE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), EV_TYPE_MODULE, EvModuleClass)) + +typedef struct _EvModule EvModule; + +GType ev_module_get_type (void) G_GNUC_CONST; + +EvModule *ev_module_new (const gchar *path); + +const gchar *ev_module_get_path (EvModule *module); + +GObject *ev_module_new_object (EvModule *module); + +GType ev_module_get_object_type (EvModule *module); + +G_END_DECLS + +#endif /* EV_MODULE_H */ -- cgit v0.9.1