Commit 4cb8ecf9 authored by Christian Persch's avatar Christian Persch Committed by Carlos Garcia Campos

libdocument: Move backends management into ev-document-factory.c

Remove EvTypeInfo, since it's redundant with EvBackendInfo. So just
make EvBackendInfo usable from ev-document-factory (internal symbols,
not exported from libevdocument).

Remove ev_backends_manager_get_document_module_name, and change
ev-window-title to use the document class type name instead.
That code should move to the backend, either directly into the document
classes' get_title method, or a new get_display_title method.
parent 57c87893
......@@ -681,9 +681,11 @@ ev_mapping_list_free
<FILE>ev-backends-manager</FILE>
EvTypeInfo
ev_backends_manager_get_document
ev_backends_manager_get_all_types_info
<SUBSECTION Deprecated>
ev_backends_manager_get_document_module_name
ev_backends_manager_get_document_type_info
ev_backends_manager_get_all_types_info
</SECTION>
<SECTION>
......
......@@ -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 \
......
/* 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_backend_info_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);
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;
}
/* 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;
};
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 */
/* 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.
*/
#include <config.h>
#include <glib/gstdio.h>
#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;
gboolean resident;
GType type_id;
gchar *type_desc;
gchar **mime_types;
};
#define EV_BACKENDS_GROUP "Evince Backend"
#define EV_BACKENDS_EXTENSION ".evince-backend"
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 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->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, 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 (backends_dir(), 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;
if (!g_str_has_suffix (dirent, EV_BACKENDS_EXTENSION))
continue;
file = g_build_filename (backends_dir(), 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);
}
g_dir_close (dir);
return ev_backends_list != NULL;
}
/*
* _ev_backends_manager_init:
*
* Initializes the evince backends manager.
*
* Returns: %TRUE if there were any backends found; %FALSE otherwise
*/
gboolean
_ev_backends_manager_init (void)
{
if (ev_backends_list)
return TRUE;
return ev_backends_manager_load ();
}
/*
* _ev_backends_manager_shutdown:
*
* Shuts the evince backends manager down.
*/
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;
g_free (backendsdir);
}
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 (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)) {
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;
}
......@@ -35,12 +35,13 @@ typedef struct _EvTypeInfo {
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);
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);
GList *ev_backends_manager_get_all_types_info (void);
G_END_DECLS
......
......@@ -29,10 +29,95 @@
#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 *ev_backends_dir = NULL;
static EvDocument* ev_document_factory_new_document_for_mime_type (const char *mime_type);
static EvBackendInfo *
get_backend_info_for_mime_type (const gchar *mime_type)
{
GList *l;
for (l = ev_backends_list; l; l = l->next) {
EvBackendInfo *info = (EvBackendInfo *) l->data;
char **mime_types = info->mime_types;
guint i;
for (i = 0; mime_types[i] != NULL; ++i) {
if (g_ascii_strcasecmp (mime_type, mime_types[i]) == 0)
return info;
}
}
return NULL;
}
static EvBackendInfo *
get_backend_info_for_document (EvDocument *document)
{
GList *l;
for (l = ev_backends_list; l; l = l->next) {
EvBackendInfo *info = (EvBackendInfo *) l->data;
GType type;
if (!info->module)
continue;
type = ev_module_get_object_type (EV_MODULE (info->module));
if (G_TYPE_CHECK_INSTANCE_TYPE (document, type))
return info;
}
return NULL;
}
static EvDocument *
ev_document_factory_new_document_for_mime_type (const gchar *mime_type)
{
EvDocument *document;
EvBackendInfo *info;
info = get_backend_info_for_mime_type (mime_type);
if (!info)
return NULL;
if (!info->module) {
gchar *path;
path = g_module_build_path (ev_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 EvCompressionType
get_compression_from_mime_type (const gchar *mime_type)
{
......@@ -56,7 +141,7 @@ get_compression_from_mime_type (const gchar *mime_type)
/*
* get_document_from_uri:
* new_document_for_uri:
* @uri: the document URI
* @fast: whether to use fast MIME type detection
* @compression: a location to store the document's compression type
......@@ -70,10 +155,10 @@ get_compression_from_mime_type (const gchar *mime_type)
* Returns: a new #EvDocument instance, or %NULL on error with @error filled in
*/
static EvDocument *
get_document_from_uri (const char *uri,
gboolean fast,
EvCompressionType *compression,
GError **error)
new_document_for_uri (const char *uri,
gboolean fast,
EvCompressionType *compression,
GError **error)
{
EvDocument *document = NULL;
gchar *mime_type = NULL;
......@@ -98,7 +183,7 @@ 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,11 +220,69 @@ free_uncompressed_uri (gchar *uri_unc)
g_free (uri_unc);
}
/* Try to get and load the document from a file, dealing with errors
* differently depending on whether we are using slow or fast mime detection.
/*
* _ev_document_factory_init:
*
* Initializes the evince document factory.
*
* Returns: %TRUE if there were any backends found; %FALSE otherwise
*/
static EvDocument *
ev_document_factory_load_uri (const char *uri, gboolean fast, GError **error)
gboolean
_ev_document_factory_init (void)
{
if (ev_backends_list)
return TRUE;
#ifdef G_OS_WIN32
{
gchar *dir;
dir = g_win32_get_package_installation_directory_of_module (NULL);
ev_backends_dir = g_build_filename (dir, "lib", "evince",
EV_BACKENDSBINARYVERSION,
"backends", NULL);
g_free (dir);
}