Commit 251e1192 authored by Daniel García Moreno's avatar Daniel García Moreno

Revert "Using rust epub lib instead C code"

We'll maintain the C code until Rust is stable enough to integrate in
Gnome.
parent 14cc7348
*.o
*.lo
*.swp
Version 0.5
===========
- Replaced gepub-archive / gepub-doc C source with Rust epub lib
Version 0.4
===========
......
AC_PREREQ(2.60)
AC_INIT([libgepub],[0.5],[danigm@wadobo.com])
AC_INIT([libgepub],[0.4],[danigm@wadobo.com])
AC_CONFIG_AUX_DIR([build])
AM_INIT_AUTOMAKE([1.10 dist-bzip2])
AM_INIT_AUTOMAKE([1.10 -Wall -Werror dist-bzip2])
AM_SILENT_RULES([yes])
GNOME_COMMON_INIT
......@@ -16,34 +16,6 @@ AM_PROG_CC_C_O
AC_PROG_INSTALL
AC_PROG_LIBTOOL
AC_CHECK_PROG(CARGO, [cargo], [yes], [no])
AS_IF(test x$CARGO = xno,
AC_MSG_ERROR([cargo is required. Please install the Rust toolchain from https://www.rust-lang.org/])
)
AC_CHECK_PROG(RUSTC, [rustc], [yes], [no])
AS_IF(test x$RUSTC = xno,
AC_MSG_ERROR([rustc is required. Please install the Rust toolchain from https://www.rust-lang.org/])
)
AC_ARG_ENABLE(debug,
AC_HELP_STRING([--enable-debug],
[Build Rust code with debugging information [default=no]]),
[debug_release=$enableval],
[debug_release=no])
AC_MSG_CHECKING(whether to build Rust code with debugging information)
if test "x$debug_release" = "xyes" ; then
AC_MSG_RESULT(yes)
RUST_TARGET_SUBDIR=debug
else
AC_MSG_RESULT(no)
RUST_TARGET_SUBDIR=release
fi
AM_CONDITIONAL([DEBUG_RELEASE], [test "x$debug_release" = "xyes"])
AC_SUBST([RUST_TARGET_SUBDIR])
PKG_CHECK_MODULES(GEPUB,
webkit2gtk-4.0
libsoup-2.4
......
lib_LTLIBRARIES = libgepub.la
NOINST_H_FILES = \
gepub-utils.h
INST_H_FILES = \
gepub-widget.h \
gepub-archive.h \
gepub-text-chunk.h \
gepub-doc.h \
gepub.h
......@@ -10,35 +15,13 @@ libgepubinclude_HEADERS = $(INST_H_FILES)
libgepub_la_SOURCES = \
gepub-widget.c \
gepub-archive.c \
gepub-text-chunk.c \
gepub-doc.c \
gepub-utils.c \
$(NOINST_H_FILES) \
$(INST_H_FILES)
RUST_SOURCES = \
rust/Cargo.toml \
rust/src/lib.rs
if DEBUG_RELEASE
CARGO_RELEASE_ARGS=
else
CARGO_RELEASE_ARGS=--release
endif
RUST_LIB=@abs_top_builddir@/target/@RUST_TARGET_SUBDIR@/libepub_internals.a
check-local:
cd $(srcdir)/rust && \
CARGO_TARGET_DIR=@abs_top_builddir@/target cargo test
clean-local:
cd $(top_srcdir)/rust && \
CARGO_TARGET_DIR=@abs_top_builddir@/target cargo clean
@abs_top_builddir@/target/@RUST_TARGET_SUBDIR@/libepub_internals.a: $(RUST_SOURCES)
cd $(top_srcdir)/libgepub/rust && \
CARGO_TARGET_DIR=@abs_top_builddir@/target cargo build --verbose $(CARGO_RELEASE_ARGS)
libgepub_la_CPPFLAGS = \
-I$(top_builddir) \
-I$(srcdir) \
......@@ -56,8 +39,7 @@ libgepub_la_LDFLAGS = \
$(AM_LDFLAGS)
libgepub_la_LIBADD = \
$(GEPUB_LIBS) \
$(RUST_LIB)
$(GEPUB_LIBS)
-include $(INTROSPECTION_MAKEFILE)
INTROSPECTION_GIRS =
......@@ -67,13 +49,13 @@ INTROSPECTION_COMPILER_ARGS = --includedir=$(srcdir)
if HAVE_INTROSPECTION
introspection_sources = $(libgepub_la_SOURCES)
Gepub-0.5.gir: libgepub.la
Gepub_0_5_gir_INCLUDES = GObject-2.0 libxml2-2.0 WebKit2-4.0
Gepub_0_5_gir_CFLAGS = $(INCLUDES)
Gepub_0_5_gir_LIBS = libgepub.la
Gepub_0_5_gir_FILES = $(introspection_sources)
Gepub_0_5_gir_SCANNERFLAGS = --warn-all
INTROSPECTION_GIRS += Gepub-0.5.gir
Gepub-0.4.gir: libgepub.la
Gepub_0_4_gir_INCLUDES = GObject-2.0 libxml2-2.0 WebKit2-4.0
Gepub_0_4_gir_CFLAGS = $(INCLUDES)
Gepub_0_4_gir_LIBS = libgepub.la
Gepub_0_4_gir_FILES = $(introspection_sources)
Gepub_0_4_gir_SCANNERFLAGS = --warn-all
INTROSPECTION_GIRS += Gepub-0.4.gir
girdir = $(datadir)/gir-1.0
gir_DATA = $(INTROSPECTION_GIRS)
......
/* GepubArchive
*
* Copyright (C) 2011 Daniel Garcia <danigm@wadobo.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <config.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <archive.h>
#include <archive_entry.h>
#include "gepub-archive.h"
#include "gepub-utils.h"
#define BUFZISE 1024
struct _GepubArchive {
GObject parent;
struct archive *archive;
gchar *path;
};
struct _GepubArchiveClass {
GObjectClass parent_class;
};
G_DEFINE_TYPE (GepubArchive, gepub_archive, G_TYPE_OBJECT)
static gboolean
gepub_archive_open (GepubArchive *archive)
{
archive->archive = archive_read_new ();
archive_read_support_format_zip (archive->archive);
int r;
r = archive_read_open_filename (archive->archive, archive->path, 10240);
if (r != ARCHIVE_OK) {
archive_read_free (archive->archive);
return FALSE;
}
return TRUE;
}
static void
gepub_archive_close (GepubArchive *archive)
{
if (!archive->archive)
return;
archive_read_free (archive->archive);
archive->archive = NULL;
}
static void
gepub_archive_finalize (GObject *object)
{
GepubArchive *archive = GEPUB_ARCHIVE (object);
g_clear_pointer (&archive->path, g_free);
gepub_archive_close (archive);
G_OBJECT_CLASS (gepub_archive_parent_class)->finalize (object);
}
static void
gepub_archive_init (GepubArchive *archive)
{
}
static void
gepub_archive_class_init (GepubArchiveClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = gepub_archive_finalize;
}
GepubArchive *
gepub_archive_new (const gchar *path)
{
GepubArchive *archive;
archive = GEPUB_ARCHIVE (g_object_new (GEPUB_TYPE_ARCHIVE, NULL));
archive->path = g_strdup (path);
archive->archive = NULL;
return archive;
}
/**
* gepub_archive_list_files:
* @archive: a #GepubArchive
*
* Returns: (element-type utf8) (transfer full): list of files in the archive
*/
GList *
gepub_archive_list_files (GepubArchive *archive)
{
struct archive_entry *entry;
GList *file_list = NULL;
if (!gepub_archive_open (archive))
return NULL;
while (archive_read_next_header (archive->archive, &entry) == ARCHIVE_OK) {
file_list = g_list_prepend (file_list, g_strdup (archive_entry_pathname (entry)));
archive_read_data_skip (archive->archive);
}
gepub_archive_close (archive);
return file_list;
}
GBytes *
gepub_archive_read_entry (GepubArchive *archive,
const gchar *path)
{
struct archive_entry *entry;
guchar *buffer;
gint size;
if (!gepub_archive_open (archive))
return NULL;
while (archive_read_next_header (archive->archive, &entry) == ARCHIVE_OK) {
if (g_ascii_strcasecmp (path, archive_entry_pathname (entry)) == 0)
break;
archive_read_data_skip (archive->archive);
}
size = archive_entry_size (entry);
buffer = g_malloc0 (size);
archive_read_data (archive->archive, buffer, size);
gepub_archive_close (archive);
return g_bytes_new_take (buffer, size);
}
gchar *
gepub_archive_get_root_file (GepubArchive *archive)
{
xmlDoc *doc = NULL;
xmlNode *root_element = NULL;
xmlNode *root_node = NULL;
GBytes *bytes;
const guchar *buffer;
gsize bufsize;
gchar *root_file = NULL;
// root file is in META-INF/container.xml
bytes = gepub_archive_read_entry (archive, "META-INF/container.xml");
if (!bytes)
return NULL;
buffer = g_bytes_get_data (bytes, &bufsize);
doc = xmlRecoverMemory (buffer, bufsize);
root_element = xmlDocGetRootElement (doc);
root_node = gepub_utils_get_element_by_tag (root_element, "rootfile");
root_file = xmlGetProp (root_node, "full-path");
xmlFreeDoc (doc);
g_bytes_unref (bytes);
return root_file;
}
/* GepubArchive
*
* Copyright (C) 2011 Daniel Garcia <danigm@wadobo.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef __GEPUB_ARCHIVE_H__
#define __GEPUB_ARCHIVE_H__
#include <glib-object.h>
#include <gio/gio.h>
#include <glib.h>
G_BEGIN_DECLS
#define GEPUB_TYPE_ARCHIVE (gepub_archive_get_type ())
#define GEPUB_ARCHIVE(obj) (G_TYPE_CHECK_INSTANCE_CAST (obj, GEPUB_TYPE_ARCHIVE, GepubArchive))
#define GEPUB_ARCHIVE_CLASS(cls) (G_TYPE_CHECK_CLASS_CAST (cls, GEPUB_TYPE_ARCHIVE, GepubArchiveClass))
#define GEPUB_IS_ARCHIVE(obj) (G_TYPE_CHECK_INSTANCE_TYPE (obj, GEPUB_TYPE_ARCHIVE))
#define GEPUB_IS_ARCHIVE_CLASS(obj) (G_TYPE_CHECK_CLASS_TYPE (obj, GEPUB_TYPE_ARCHIVE))
#define GEPUB_ARCHIVE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GEPUB_TYPE_ARCHIVE, GepubArchiveClass))
typedef struct _GepubArchive GepubArchive;
typedef struct _GepubArchiveClass GepubArchiveClass;
GType gepub_archive_get_type (void) G_GNUC_CONST;
GepubArchive *gepub_archive_new (const gchar *path);
GList *gepub_archive_list_files (GepubArchive *archive);
GBytes *gepub_archive_read_entry (GepubArchive *archive,
const gchar *path);
gchar *gepub_archive_get_root_file (GepubArchive *archive);
G_END_DECLS
#endif /* __GEPUB_ARCHIVE_H__ */
......@@ -18,42 +18,30 @@
*/
#include <config.h>
#include <gio/gio.h>
#include <libxml/tree.h>
#include <libxml/HTMLparser.h>
#include <string.h>
#include "gepub-utils.h"
#include "gepub-doc.h"
#include "gepub-archive.h"
#include "gepub-text-chunk.h"
// Rust
void *epub_new(char *path);
void epub_destroy(void *doc);
void *epub_get_resource(void *doc, const char *path, int *size);
void *epub_get_resource_by_id(void *doc, const char *id, int *size);
void *epub_get_metadata(void *doc, const char *mdata);
void *epub_get_resource_mime(void *doc, const char *path);
void *epub_get_resource_mime_by_id(void *doc, const char *id);
void *epub_get_current_mime(void *doc);
void *epub_get_current(void *doc, int *size);
void *epub_get_current_with_epub_uris(void *doc, int *size);
void epub_set_page(void *doc, guint page);
guint epub_get_num_pages(void *doc);
guint epub_get_page(void *doc);
gboolean epub_next_page(void *doc);
gboolean epub_prev_page(void *doc);
void *epub_get_cover(void *doc);
void *epub_resource_path(void *doc, const char *id);
void *epub_current_path(void *doc);
void *epub_current_id(void *doc);
static void gepub_doc_fill_resources (GepubDoc *doc);
static void gepub_doc_fill_spine (GepubDoc *doc);
static void gepub_doc_initable_iface_init (GInitableIface *iface);
struct _GepubDoc {
GObject parent;
GepubArchive *archive;
GBytes *content;
gchar *content_base;
gchar *path;
void *rust_epub_doc;
GHashTable *resources;
GList *spine;
GList *page;
};
struct _GepubDocClass {
......@@ -64,7 +52,6 @@ enum {
PROP_0,
PROP_PATH,
PROP_PAGE,
PROP_FILE,
NUM_PROPS
};
......@@ -73,12 +60,28 @@ static GParamSpec *properties[NUM_PROPS] = { NULL, };
G_DEFINE_TYPE_WITH_CODE (GepubDoc, gepub_doc, G_TYPE_OBJECT,
G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, gepub_doc_initable_iface_init))
static void
gepub_resource_free (GepubResource *res)
{
g_free (res->mime);
g_free (res->uri);
g_free (res);
}
static void
gepub_doc_finalize (GObject *object)
{
GepubDoc *doc = GEPUB_DOC (object);
epub_destroy (doc->rust_epub_doc);
g_clear_object (&doc->archive);
g_clear_pointer (&doc->content, g_bytes_unref);
g_clear_pointer (&doc->path, g_free);
g_clear_pointer (&doc->resources, g_hash_table_destroy);
if (doc->spine) {
g_list_foreach (doc->spine, (GFunc)g_free, NULL);
g_clear_pointer (&doc->spine, g_list_free);
}
G_OBJECT_CLASS (gepub_doc_parent_class)->finalize (object);
}
......@@ -98,13 +101,6 @@ gepub_doc_set_property (GObject *object,
case PROP_PAGE:
gepub_doc_set_page (doc, g_value_get_int (value));
break;
case PROP_FILE: {
GFile *f = G_FILE (g_value_get_object (value));
if (f) {
doc->path = g_file_get_path (G_FILE (g_value_get_object (value)));
}
break;
}
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
......@@ -126,11 +122,6 @@ gepub_doc_get_property (GObject *object,
case PROP_PAGE:
g_value_set_int (value, gepub_doc_get_page (doc));
break;
case PROP_FILE: {
GFile *f = g_file_new_for_path (doc->path);
g_value_set_object (value, f);
break;
}
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
......@@ -140,6 +131,13 @@ gepub_doc_get_property (GObject *object,
static void
gepub_doc_init (GepubDoc *doc)
{
/* doc resources hashtable:
* id : (mime, path)
*/
doc->resources = g_hash_table_new_full (g_str_hash,
g_str_equal,
(GDestroyNotify)g_free,
(GDestroyNotify)gepub_resource_free);
}
static void
......@@ -167,15 +165,6 @@ gepub_doc_class_init (GepubDocClass *klass)
G_PARAM_READWRITE |
G_PARAM_STATIC_STRINGS);
properties[PROP_FILE] =
g_param_spec_object ("file",
"File",
"GFile to the EPUB document",
G_TYPE_FILE,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_STRINGS);
g_object_class_install_properties (object_class, NUM_PROPS, properties);
}
......@@ -185,12 +174,35 @@ gepub_doc_initable_init (GInitable *initable,
GError **error)
{
GepubDoc *doc = GEPUB_DOC (initable);
gchar *file;
gsize bufsize = 0;
gint i = 0, len;
g_assert (doc->path != NULL);
doc->rust_epub_doc = epub_new (doc->path);
if (!doc->rust_epub_doc) {
doc->archive = gepub_archive_new (doc->path);
file = gepub_archive_get_root_file (doc->archive);
if (!file)
return FALSE;
doc->content = gepub_archive_read_entry (doc->archive, file);
if (!doc->content)
return FALSE;
len = strlen (file);
doc->content_base = g_strdup ("");
for (i=0; i<len; i++) {
if (file[i] == '/') {
g_free (doc->content_base);
doc->content_base = g_strndup (file, i+1);
break;
}
}
gepub_doc_fill_resources (doc);
gepub_doc_fill_spine (doc);
g_free (file);
return TRUE;
}
......@@ -215,6 +227,93 @@ gepub_doc_new (const gchar *path)
NULL);
}
static void
gepub_doc_fill_resources (GepubDoc *doc)
{
xmlDoc *xdoc = NULL;
xmlNode *root_element = NULL;
xmlNode *mnode = NULL;
xmlNode *item = NULL;
gchar *id, *tmpuri, *uri;
GepubResource *res;
const char *data;
gsize size;
data = g_bytes_get_data (doc->content, &size);
xdoc = xmlRecoverMemory (data, size);
root_element = xmlDocGetRootElement (xdoc);
mnode = gepub_utils_get_element_by_tag (root_element, "manifest");
item = mnode->children;
while (item) {
if (item->type != XML_ELEMENT_NODE ) {
item = item->next;
continue;
}
id = xmlGetProp (item, "id");
tmpuri = xmlGetProp (item, "href");
uri = g_strdup_printf ("%s%s", doc->content_base, tmpuri);
g_free (tmpuri);
res = g_malloc (sizeof (GepubResource));
res->mime = xmlGetProp (item, "media-type");
res->uri = uri;
g_hash_table_insert (doc->resources, id, res);
item = item->next;
}
xmlFreeDoc (xdoc);
}
static void
gepub_doc_fill_spine (GepubDoc *doc)
{
xmlDoc *xdoc = NULL;
xmlNode *root_element = NULL;
xmlNode *snode = NULL;
xmlNode *item = NULL;
gchar *id;
const char *data;
gsize size;
GList *spine = NULL;
data = g_bytes_get_data (doc->content, &size);
xdoc = xmlRecoverMemory (data, size);
root_element = xmlDocGetRootElement (xdoc);
snode = gepub_utils_get_element_by_tag (root_element, "spine");
item = snode->children;
while (item) {
if (item->type != XML_ELEMENT_NODE ) {
item = item->next;
continue;
}
id = xmlGetProp (item, "idref");
spine = g_list_prepend (spine, id);
item = item->next;
}
doc->spine = g_list_reverse (spine);
doc->page = doc->spine;
xmlFreeDoc (xdoc);
}
/**
* gepub_doc_get_content:
* @doc: a #GepubDoc
*
* Returns: (transfer none): the document content
*/
GBytes *
gepub_doc_get_content (GepubDoc *doc)
{
return doc->content;
}
/**
* gepub_doc_get_metadata:
* @doc: a #GepubDoc
......@@ -225,7 +324,40 @@ gepub_doc_new (const gchar *path)
gchar *
gepub_doc_get_metadata (GepubDoc *doc, const gchar *mdata)
{
return epub_get_metadata (doc->rust_epub_doc, mdata);
xmlDoc *xdoc = NULL;
xmlNode *root_element = NULL;
xmlNode *mnode = NULL;
xmlNode *mdata_node = NULL;
gchar *ret;
xmlChar *text;
const char *data;
gsize size;
data = g_bytes_get_data (doc->content, &size);
xdoc = xmlRecoverMemory (data, size);
root_element = xmlDocGetRootElement (xdoc);
mnode = gepub_utils_get_element_by_tag (root_element, "metadata");
mdata_node = gepub_utils_get_element_by_tag (mnode, mdata);
text = xmlNodeGetContent (mdata_node);
ret = g_strdup (text);
xmlFree (text);
xmlFreeDoc (xdoc);
return ret;
}
/**
* gepub_doc_get_resources:
* @doc: a #GepubDoc
*
* Returns: (element-type utf8 Gepub.Resource) (transfer none): doc resource table
*/
GHashTable *
gepub_doc_get_resources (GepubDoc *doc)
{
return doc->resources;
}
/**
......@@ -238,9 +370,13 @@ gepub_doc_get_metadata (GepubDoc *doc, const gchar *mdata)
GBytes *
gepub_doc_get_resource_by_id (GepubDoc *doc, const gchar *id)
{
int size = 0;
guint8 *data = epub_get_resource_by_id (doc->rust_epub_doc, id, &size);
return g_bytes_new_take (data, size);
GepubResource *gres = g_hash_table_lookup (doc->resources, id);
if (!gres) {
// not found
return NULL;
}
return gepub_archive_read_entry (doc->archive, gres->uri);
}
/**
......@@ -253,9 +389,7 @@ gepub_doc_get_resource_by_id (GepubDoc *doc, const gchar *id)
GBytes *
gepub_doc_get_resource (GepubDoc *doc, const gchar *path)
{
int size = 0;
guint8 *data = epub_get_resource (doc->rust_epub_doc, path, &size);
return g_bytes_new_take (data, size);
return gepub_archive_read_entry (doc->archive, path);
}
/**
......@@ -268,7 +402,17 @@ gepub_doc_get_resource (GepubDoc *doc, const gchar *path)
gchar *
gepub_doc_get_resource_mime_by_id (GepubDoc *doc, const gchar *id)
{
return epub_get_resource_mime_by_id (doc->rust_epub_doc, id);
GepubResource *gres;
g_return_val_if_fail (id != NULL, NULL);
gres = g_hash_table_lookup (doc->resources, id);
if (!gres) {
// not found
return NULL;
}
return g_strdup (gres->mime);
}
/**
......@@ -281,7 +425,20 @@ gepub_doc_get_resource_mime_by_id (GepubDoc *doc, const gchar *id)
gchar *
gepub_doc_get_resource_mime (GepubDoc *doc, const gchar *path)
{
return epub_get_resource_mime (doc->rust_epub_doc, path);
GepubResource *gres;
GList *keys = g_hash_table_get_keys (doc->resources);
while (keys) {
gres = ((GepubResource*)g_hash_table_lookup (doc->resources, keys->data));
if (!strcmp (gres->uri, path))
break;
keys = keys->next;
}
if (keys)
return g_strdup (gres->mime);
else