[evince/wip/bug654832: 7/18] [libdocument] Move backends management into ev-document-factory.c



commit 49f91a8c605508c2068214233599fcb37e7e25dd
Author: Christian Persch <chpe gnome org>
Date:   Sat Feb 13 20:21:56 2010 +0100

    [libdocument] Move backends management into ev-document-factory.c
    
    Adapt callers accordingly.

 evince-document.h                                  |    1 -
 help/reference/libdocument/Makefile.am             |    1 +
 help/reference/libdocument/libevdocument-docs.xml  |    1 -
 .../libdocument/libevdocument-sections.txt         |    6 -
 libdocument/Makefile.am                            |    3 +-
 libdocument/ev-backend-info.c                      |  149 ++++++++++
 libdocument/ev-backend-info.h                      |   56 ++++
 libdocument/ev-backends-manager.c                  |  308 --------------------
 libdocument/ev-backends-manager.h                  |   20 +-
 libdocument/ev-document-factory.c                  |  190 ++++++++++++-
 libdocument/ev-document-factory.h                  |    4 +
 libdocument/ev-init.c                              |    6 +-
 properties/ev-properties-main.c                    |    2 +-
 shell/ev-window-title.c                            |    1 -
 14 files changed, 406 insertions(+), 342 deletions(-)
---
diff --git a/evince-document.h b/evince-document.h
index 1db675b..39ce75a 100644
--- a/evince-document.h
+++ b/evince-document.h
@@ -24,7 +24,6 @@
 #include <libdocument/ev-annotation.h>
 #include <libdocument/ev-async-renderer.h>
 #include <libdocument/ev-attachment.h>
-#include <libdocument/ev-backends-manager.h>
 #include <libdocument/ev-document-attachments.h>
 #include <libdocument/ev-document-factory.h>
 #include <libdocument/ev-document-find.h>
diff --git a/help/reference/libdocument/Makefile.am b/help/reference/libdocument/Makefile.am
index e1c6ba3..40d7864 100644
--- a/help/reference/libdocument/Makefile.am
+++ b/help/reference/libdocument/Makefile.am
@@ -68,6 +68,7 @@ EXTRA_HFILES =
 # e.g. IGNORE_HFILES=gtkdebug.h gtkintl.h
 IGNORE_HFILES = \
 	config.h \
+	ev-backend-info.h \
 	ev-debug.h \
 	ev-module.h
 
diff --git a/help/reference/libdocument/libevdocument-docs.xml b/help/reference/libdocument/libevdocument-docs.xml
index 5778acf..62da795 100644
--- a/help/reference/libdocument/libevdocument-docs.xml
+++ b/help/reference/libdocument/libevdocument-docs.xml
@@ -129,7 +129,6 @@
     <xi:include href="xml/ev-version.xml"/>
     <xi:include href="xml/ev-file-helpers.xml"/>
     <xi:include href="xml/ev-document-factory.xml"/>
-    <xi:include href="xml/ev-backends-manager.xml"/>
   </part>
 
   <chapter id="object-tree">
diff --git a/help/reference/libdocument/libevdocument-sections.txt b/help/reference/libdocument/libevdocument-sections.txt
index 7ed303c..8059ae3 100644
--- a/help/reference/libdocument/libevdocument-sections.txt
+++ b/help/reference/libdocument/libevdocument-sections.txt
@@ -678,12 +678,6 @@ ev_mapping_list_free
 </SECTION>
 
 <SECTION>
-<FILE>ev-backends-manager</FILE>
-ev_backends_manager_get_document
-ev_backends_manager_get_document_module_name
-</SECTION>
-
-<SECTION>
 <FILE>ev-file-helpers</FILE>
 EvCompressionType
 ev_mkstemp
diff --git a/libdocument/Makefile.am b/libdocument/Makefile.am
index eaa1db6..dcf71b0 100644
--- a/libdocument/Makefile.am
+++ b/libdocument/Makefile.am
@@ -2,6 +2,7 @@ lib_LTLIBRARIES = libevdocument3.la
 
 NOINST_H_FILES =				\
 	ev-debug.h				\
+	ev-backend-info.h			\
 	ev-module.h
 
 INST_H_SRC_FILES = 				\
@@ -52,7 +53,7 @@ libevdocument3_la_SOURCES=			\
 	ev-annotation.c				\
 	ev-async-renderer.c			\
 	ev-attachment.c				\
-	ev-backends-manager.c			\
+	ev-backend-info.c			\
 	ev-layer.c				\
 	ev-link.c				\
 	ev-link-action.c			\
diff --git a/libdocument/ev-backend-info.c b/libdocument/ev-backend-info.c
new file mode 100644
index 0000000..3bc14e1
--- /dev/null
+++ b/libdocument/ev-backend-info.c
@@ -0,0 +1,149 @@
+/* this file is part of evince, a gnome document viewer
+ *
+ *  Copyright (C) 2007 Carlos Garcia Campos <carlosgc gnome org>
+ *
+ * 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 <config.h>
+
+#include "ev-backend-info.h"
+
+#define EV_BACKENDS_GROUP     "Evince Backend"
+#define EV_BACKENDS_EXTENSION ".evince-backend"
+
+/*
+ * _ev_backend_info_free:
+ * @info:
+ *
+ * Frees @info
+ */
+void
+_ev_backend_info_free (EvBackendInfo *info)
+{
+        if (info == NULL)
+                return;
+
+	g_free (info->module_name);
+	g_free (info->type_desc);
+	g_strfreev (info->mime_types);
+        /* Leak info->module */
+	g_slice_free (EvBackendInfo, info);
+}
+
+/**
+ * _ev_backends_manager_new_from_file:
+ * @path: path to the backends file
+ * @error: a location to store a #GError, or %NULL
+ *
+ * Loads backend information from @path.
+ *
+ * Returns: a new #EvBackendInfo, or %NULL on error with @error filled in
+ */
+EvBackendInfo *
+_ev_backend_info_new_from_file (const char *file,
+                                GError **error)
+{
+	EvBackendInfo *info = NULL;
+	GKeyFile      *backend_file = NULL;
+
+	backend_file = g_key_file_new ();
+	if (!g_key_file_load_from_file (backend_file, file, G_KEY_FILE_NONE, error))
+                goto err;
+
+	info = g_slice_new0 (EvBackendInfo);
+
+	info->module_name = g_key_file_get_string (backend_file, EV_BACKENDS_GROUP,
+						   "Module", error);
+	if (!info->module_name)
+                goto err;
+
+	info->resident = g_key_file_get_boolean (backend_file, EV_BACKENDS_GROUP,
+						 "Resident", NULL);
+	
+	info->type_desc = g_key_file_get_locale_string (backend_file, EV_BACKENDS_GROUP,
+							"TypeDescription", NULL, error);
+	if (!info->type_desc)
+                goto err;
+
+	info->mime_types = g_key_file_get_string_list (backend_file, EV_BACKENDS_GROUP,
+						       "MimeType", NULL, error);
+	if (!info->mime_types)
+                goto err;
+
+	g_key_file_free (backend_file);
+
+        info->type_id = G_TYPE_INVALID;
+
+	return info;
+
+    err:
+        g_key_file_free (backend_file);
+        _ev_backend_info_free (info);
+        return NULL;
+}
+
+/*
+ * _ev_backend_info_load_from_dir:
+ * @path: a directory name
+ *
+ * Load all backend infos from @path.
+ *
+ * Returns: a newly allocated #GList containing newly allocated
+ *   #EvBackendInfo objects
+ */
+GList
+*_ev_backend_info_load_from_dir (const char *path)
+{
+        GList       *list = NULL;
+        GDir        *dir;
+        const gchar *dirent;
+        GError      *error = NULL;
+
+        dir = g_dir_open (path, 0, &error);
+        if (!dir) {
+                g_warning ("%s", error->message);
+                g_error_free (error);
+
+                return FALSE;
+        }
+
+        while ((dirent = g_dir_read_name (dir))) {
+                EvBackendInfo *info;
+                gchar         *file;
+                GError        *error = NULL;
+
+                if (!g_str_has_suffix (dirent, EV_BACKENDS_EXTENSION))
+                        continue;
+
+                file = g_build_filename (path, dirent, NULL);
+                info = _ev_backend_info_new_from_file (file, &error);
+                if (error != NULL) {
+                        g_warning ("Failed to load backend info from '%s': %s\n",
+                                   file, error->message);
+                        g_error_free (error);
+                }
+                g_free (file);
+
+                if (info == NULL)
+                        continue;
+
+                list = g_list_prepend (list, info);
+        }
+
+        g_dir_close (dir);
+
+        return list;
+}
diff --git a/libdocument/ev-backend-info.h b/libdocument/ev-backend-info.h
new file mode 100644
index 0000000..e5200f3
--- /dev/null
+++ b/libdocument/ev-backend-info.h
@@ -0,0 +1,56 @@
+/* this file is part of evince, a gnome document viewer
+ *
+ *  Copyright (C) 2007 Carlos Garcia Campos <carlosgc gnome org>
+ *
+ * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#if !defined (EVINCE_COMPILATION)
+#error "This is a private header."
+#endif
+
+#ifndef EV_BACKEND_INFO
+#define EV_BACKEND_INFO
+
+#include <glib.h>
+
+#include "ev-module.h"
+
+G_BEGIN_DECLS
+
+typedef struct _EvBackendInfo EvBackendInfo;
+
+struct _EvBackendInfo {
+        /* These two fields must be first for API/ABI compat with EvTypeInfo */
+        gchar       *type_desc;
+        gchar      **mime_types;
+
+	gchar       *module_name;
+	GTypeModule *module;
+	gboolean     resident;
+
+	GType        type_id;
+};
+
+void            _ev_backend_info_free           (EvBackendInfo *info);
+
+EvBackendInfo  *_ev_backend_info_new_from_file  (const char *file,
+                                                 GError **error);
+
+GList          *_ev_backend_info_load_from_dir  (const char *path);
+
+G_END_DECLS
+
+#endif /* !EV_BACKEND_INFO */
diff --git a/libdocument/ev-backends-manager.h b/libdocument/ev-backends-manager.h
index fa23274..7927196 100644
--- a/libdocument/ev-backends-manager.h
+++ b/libdocument/ev-backends-manager.h
@@ -30,17 +30,19 @@
 
 G_BEGIN_DECLS
 
-typedef struct _EvBackendInfo EvBackendInfo;
-
-const char *   _ev_backend_info_get_description               (EvBackendInfo *info);
-char **        _ev_backend_info_get_mime_types                (EvBackendInfo *info);
-EvBackendInfo *_ev_backends_manager_get_backend_for_document  (EvDocument  *document);
-GList         *_ev_backends_manager_get_backends              (void);
-
-gboolean    _ev_backends_manager_init                     (void);
-void        _ev_backends_manager_shutdown                 (void);
+typedef struct _EvTypeInfo {
+	const gchar  *desc;
+	const gchar **mime_types;
+} EvTypeInfo;
 
+G_GNUC_DEPRECATED
 EvDocument  *ev_backends_manager_get_document             (const gchar *mime_type);
+G_GNUC_DEPRECATED
+const gchar *ev_backends_manager_get_document_module_name (EvDocument  *document);
+G_GNUC_DEPRECATED
+EvTypeInfo  *ev_backends_manager_get_document_type_info   (EvDocument  *document);
+G_GNUC_DEPRECATED
+GList       *ev_backends_manager_get_all_types_info       (void);
 
 G_END_DECLS
 
diff --git a/libdocument/ev-document-factory.c b/libdocument/ev-document-factory.c
index 44ac444..a8030b3 100644
--- a/libdocument/ev-document-factory.c
+++ b/libdocument/ev-document-factory.c
@@ -29,10 +29,119 @@
 #include <glib/gi18n-lib.h>
 #include <gtk/gtk.h>
 
-#include "ev-backends-manager.h"
+#include "ev-backend-info.h"
 #include "ev-document-factory.h"
 #include "ev-file-helpers.h"
 
+#include "ev-backends-manager.h"
+
+/* Backends manager */
+
+static GList *ev_backends_list = NULL;
+
+static gchar *backendsdir = NULL;
+
+static const gchar *
+backends_dir (void)
+{
+	if (!backendsdir) {
+#ifdef G_OS_WIN32
+		gchar *dir;
+
+		dir = g_win32_get_package_installation_directory_of_module (NULL);
+		backendsdir = g_build_filename (dir, "lib", "evince",
+						EV_BACKENDSBINARYVERSION,
+						"backends", NULL);
+		g_free (dir);
+#else
+		backendsdir = g_strdup (EV_BACKENDSDIR);
+#endif
+	}
+
+	return backendsdir;
+}
+
+static EvBackendInfo *
+ev_document_factory_get_backend_info_for_mime_type (const gchar *mime_type)
+{
+        GList *l;
+
+        for (l = ev_backends_list; l; l = l->next) {
+                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_document_factory_new_document_for_mime_type (const gchar *mime_type)
+{
+        EvDocument    *document;
+        EvBackendInfo *info;
+
+        info = ev_document_factory_get_backend_info_for_mime_type (mime_type);
+        if (!info)
+                return NULL;
+
+        if (!info->module) {
+                gchar *path;
+
+                path = g_module_build_path (backends_dir(), info->module_name);
+                info->module = G_TYPE_MODULE (ev_module_new (path, info->resident));
+                g_free (path);
+        }
+
+        if (!g_type_module_use (info->module)) {
+                const char *err;
+
+                err = g_module_error ();
+                g_warning ("Cannot load backend '%s': %s",
+                          info->module_name, err ? err : "unknown error");
+                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 *
+ev_document_factory_get_backend_for_document (EvDocument *document)
+{
+        GList *l;
+
+        for (l = ev_backends_list; l; l = l->next) {
+                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;
+}
+
 static EvCompressionType
 get_compression_from_mime_type (const gchar *mime_type)
 {
@@ -98,7 +207,8 @@ get_document_from_uri (const char        *uri,
 		return NULL;
 	}
 
-	document = ev_backends_manager_get_document (mime_type);
+	document = ev_document_factory_new_document_for_mime_type (mime_type);
+	
 	if (document == NULL) {
 		gchar *content_type, *mime_desc = NULL;
 
@@ -135,6 +245,39 @@ free_uncompressed_uri (gchar *uri_unc)
 	g_free (uri_unc);
 }
 
+/*
+ * _ev_document_factory_init:
+ *
+ * Initializes the evince backends manager.
+ *
+ * Returns: %TRUE if there were any backends found; %FALSE otherwise
+ */
+gboolean
+_ev_document_factory_init (void)
+{
+	if (ev_backends_list)
+		return TRUE;
+
+        ev_backends_list = _ev_backend_info_load_from_dir (backends_dir ());
+
+        return ev_backends_list != NULL;
+}
+
+/*
+ * _ev_document_factory_shutdown:
+ *
+ * Shuts the evince backends manager down.
+ */
+void
+_ev_document_factory_shutdown (void)
+{
+	g_list_foreach (ev_backends_list, (GFunc) _ev_backend_info_free, NULL);
+	g_list_free (ev_backends_list);
+	ev_backends_list = NULL;
+
+	g_free (backendsdir);
+}
+
 /**
  * ev_document_factory_get_document:
  * @uri: an URI
@@ -248,7 +391,7 @@ file_filter_add_mime_types (EvBackendInfo *info, GtkFileFilter *filter)
         char **mime_types;
 	guint i;
 
-        mime_types = _ev_backend_info_get_mime_types (info);
+        mime_types = info->mime_types;
         if (mime_types == NULL)
                 return;
 
@@ -273,7 +416,6 @@ file_filter_add_mime_types (EvBackendInfo *info, GtkFileFilter *filter)
 void
 ev_document_factory_add_filters (GtkWidget *chooser, EvDocument *document)
 {
-	GList         *all_types;
 	GtkFileFilter *filter;
 	GtkFileFilter *default_filter;
 	GtkFileFilter *document_filter;
@@ -281,30 +423,28 @@ ev_document_factory_add_filters (GtkWidget *chooser, EvDocument *document)
         g_return_if_fail (GTK_IS_FILE_CHOOSER (chooser));
         g_return_if_fail (document == NULL || EV_IS_DOCUMENT (document));
 
-	all_types = _ev_backends_manager_get_backends ();
-	
 	default_filter = document_filter = filter = gtk_file_filter_new ();
 	gtk_file_filter_set_name (filter, _("All Documents"));
-	g_list_foreach (all_types, (GFunc)file_filter_add_mime_types, filter);
+	g_list_foreach (ev_backends_list, (GFunc)file_filter_add_mime_types, filter);
 	gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
 
 	if (document) {
 		EvBackendInfo *info;
 
-		info = _ev_backends_manager_get_backend_for_document (document);
+		info = ev_document_factory_get_backend_for_document (document);
 		default_filter = filter = gtk_file_filter_new ();
-		gtk_file_filter_set_name (filter, _ev_backend_info_get_description (info));
+		gtk_file_filter_set_name (filter, info->type_desc);
 		file_filter_add_mime_types (info, filter);
 		g_free (info);
 		gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
 	} else {
 		GList *l;
 
-		for (l = all_types; l; l = g_list_next (l)){
+		for (l = ev_backends_list; l; l = l->next) {
                         EvBackendInfo *info = (EvBackendInfo *) l->data;
 
 			default_filter = filter = gtk_file_filter_new ();
-			gtk_file_filter_set_name (filter, _ev_backend_info_get_description (info));
+			gtk_file_filter_set_name (filter, info->type_desc);
 			file_filter_add_mime_types (info, filter);
 			gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
 		}
@@ -318,3 +458,31 @@ ev_document_factory_add_filters (GtkWidget *chooser, EvDocument *document)
 	gtk_file_chooser_set_filter (GTK_FILE_CHOOSER (chooser),
 				     document == NULL ? document_filter : default_filter);
 }
+
+/* Deprecated API/ABI compatibility wrappers */
+
+EvDocument  *ev_backends_manager_get_document (const gchar *mime_type)
+{
+        return ev_document_factory_new_document_for_mime_type (mime_type);
+}
+
+const gchar *
+ev_backends_manager_get_document_module_name (EvDocument  *document)
+{
+        EvBackendInfo *info = ev_document_factory_get_backend_for_document (document);
+        if (info == NULL)
+                return NULL;
+
+        return info->module_name;
+}
+
+EvTypeInfo *ev_backends_manager_get_document_type_info (EvDocument  *document)
+{
+        return (EvTypeInfo *) ev_document_factory_get_backend_for_document (document);
+}
+
+GList *
+ev_backends_manager_get_all_types_info       (void)
+{
+        return g_list_copy (ev_backends_list);
+}
diff --git a/libdocument/ev-document-factory.h b/libdocument/ev-document-factory.h
index 501eb2f..b4e088b 100644
--- a/libdocument/ev-document-factory.h
+++ b/libdocument/ev-document-factory.h
@@ -31,6 +31,10 @@
 
 G_BEGIN_DECLS
 
+gboolean   _ev_document_factory_init         (void);
+void       _ev_document_factory_shutdown     (void);
+
+EvDocument* ev_document_factory_new_document_for_mime_type (const char *mime_type);
 EvDocument* ev_document_factory_get_document (const char *uri, GError **error);
 void 	    ev_document_factory_add_filters  (GtkWidget *chooser, EvDocument *document);
 
diff --git a/libdocument/ev-init.c b/libdocument/ev-init.c
index 71c6784..ac1901c 100644
--- a/libdocument/ev-init.c
+++ b/libdocument/ev-init.c
@@ -26,7 +26,7 @@
 #endif
 
 #include "ev-init.h"
-#include "ev-backends-manager.h"
+#include "ev-document-factory.h"
 #include "ev-debug.h"
 #include "ev-file-helpers.h"
 
@@ -116,7 +116,7 @@ ev_init (void)
 
         _ev_debug_init ();
         _ev_file_helpers_init ();
-        have_backends = _ev_backends_manager_init ();
+        have_backends = _ev_document_factory_init ();
 
         return have_backends;
 }
@@ -139,7 +139,7 @@ ev_shutdown (void)
 		g_free(locale_dir);
 #endif
 
-        _ev_backends_manager_shutdown ();
+        _ev_document_factory_shutdown ();
         _ev_file_helpers_shutdown ();
         _ev_debug_shutdown ();
 }
diff --git a/properties/ev-properties-main.c b/properties/ev-properties-main.c
index 1a7cccd..7b7247b 100644
--- a/properties/ev-properties-main.c
+++ b/properties/ev-properties-main.c
@@ -103,7 +103,7 @@ ev_properties_get_pages (NautilusPropertyPageProvider *provider,
 	uri = nautilus_file_info_get_uri (file);
 	mime_type = nautilus_file_info_get_mime_type (file);
 	
-	document = ev_backends_manager_get_document (mime_type);
+	document = ev_document_factory_new_document_for_mime_type (mime_type);
 	if (!document)
 		goto end;
 
diff --git a/shell/ev-window-title.c b/shell/ev-window-title.c
index 993fb45..0bfe053 100644
--- a/shell/ev-window-title.c
+++ b/shell/ev-window-title.c
@@ -19,7 +19,6 @@
 
 #include <config.h>
 #include "ev-window-title.h"
-#include "ev-backends-manager.h"
 #include "ev-utils.h"
 
 #include <string.h>



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]