From: Andrea Zagli Date: Sat, 5 Jan 2013 14:40:10 +0000 (+0100) Subject: Added parsing of collection. X-Git-Url: https://saetta.ns0.it/gitweb?a=commitdiff_plain;h=543e01d216e6c893638289f6faf99a7eab014fad;p=libgapp Added parsing of collection. Added anjuta project files. --- diff --git a/.anjuta/default.profile b/.anjuta/default.profile new file mode 100644 index 0000000..cf999f6 --- /dev/null +++ b/.anjuta/default.profile @@ -0,0 +1,18 @@ + + + + + + + + + + + + diff --git a/.anjuta_sym_db.db b/.anjuta_sym_db.db new file mode 100644 index 0000000..354436d Binary files /dev/null and b/.anjuta_sym_db.db differ diff --git a/libgapp.anjuta b/libgapp.anjuta new file mode 100644 index 0000000..5e1cf44 --- /dev/null +++ b/libgapp.anjuta @@ -0,0 +1,47 @@ + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/Makefile.am b/src/Makefile.am index b456e8a..3f13284 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -14,13 +14,16 @@ AM_CPPFLAGS = $(GAPP_CFLAGS) \ lib_LTLIBRARIES = libgapp.la -libgapp_la_SOURCES = service.c \ - workspace.c +libgapp_la_SOURCES = \ + collection.c \ + service.c \ + workspace.c libgapp_la_LDFLAGS = -no-undefined libgapp_include_HEADERS = gapp.h \ - service.h \ - workspace.h + collection.h \ + service.h \ + workspace.h libgapp_includedir = $(includedir)/libgapp diff --git a/src/collection.c b/src/collection.c new file mode 100644 index 0000000..88e2797 --- /dev/null +++ b/src/collection.c @@ -0,0 +1,292 @@ +/* libgfeed + * + * Copyright (C) 2012 Andrea Zagli + * + * 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 Street, Fifth Floor, Boston, MA 02110-1301 USA + * + * Andrea Zagli + */ + +#include + +#include + +#include "collection.h" + +enum +{ + PROP_0, + PROP_HREF, + PROP_TITLE +}; + +static void gapp_collection_class_init (GappCollectionClass *klass); +static void gapp_collection_init (GappCollection *collection); + +static void gapp_collection_set_property (GObject *object, + guint property_id, + const GValue *value, + GParamSpec *pspec); +static void gapp_collection_get_property (GObject *object, + guint property_id, + GValue *value, + GParamSpec *pspec); + +#define GAPP_COLLECTION_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), TYPE_GAPP_COLLECTION, GappCollectionPrivate)) + +typedef struct _GappCollectionPrivate GappCollectionPrivate; +struct _GappCollectionPrivate + { + gchar *href; + AtomText *title; + }; + +G_DEFINE_TYPE (GappCollection, gapp_collection, TYPE_ATOM_COMMON) + +static void +gapp_collection_class_init (GappCollectionClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + + g_type_class_add_private (object_class, sizeof (GappCollectionPrivate)); + + object_class->set_property = gapp_collection_set_property; + object_class->get_property = gapp_collection_get_property; + + g_object_class_install_property (object_class, PROP_HREF, + g_param_spec_string ("href", + "HRef", + "HRef.", + "", + G_PARAM_READWRITE | G_PARAM_CONSTRUCT)); + + g_object_class_install_property (object_class, PROP_TITLE, + g_param_spec_object ("title", + "Title", + "A human-readable title for the collection.", + TYPE_ATOM_TEXT, + G_PARAM_READWRITE)); +} + +static void +gapp_collection_init (GappCollection *collection) +{ +} + +/** + * gapp_collection_new: + * @href: + * + * Returns: the newly created #GappCollection object. + */ +GappCollection +*gapp_collection_new (const gchar *href) +{ + GappCollection *gapp_collection = GAPP_COLLECTION (g_object_new (gapp_collection_get_type (), NULL)); + + g_object_set (G_OBJECT (gapp_collection), + "href", href, + NULL); + + return gapp_collection; +} + +/** + * gapp_collection_new_from_xml: + * @xnode: an #xmlNode. + * + * Returns: the newly created #GappCollection object. + */ +GappCollection +*gapp_collection_new_from_xml (xmlNode *xnode) +{ + GappCollection *gapp_collection; + GappCollectionPrivate *priv; + + gchar *href; + AtomText *title; + + xmlXPathObject *xpresult; + xmlXPathContext *xpcontext; + + guint i; + + gapp_collection = NULL; + + href = xmlGetProp (xnode, "href"); + if (href == NULL) + { + g_critical ("Collection must have an attribute href."); + } + else + { + if (g_strcmp0 (g_strstrip (href), "") == 0) + { + g_critical ("Collection must have a not empty attribute href."); + } + } + + gapp_collection = gapp_collection_new (href); + + title = NULL; + xpcontext = xmlXPathNewContext (xnode->doc); + + xmlXPathRegisterNs (xpcontext, (const xmlChar *)"xmlns", (const xmlChar *)"http://www.w3.org/2007/app"); + xmlXPathRegisterNs (xpcontext, (const xmlChar *)"atom", (const xmlChar *)"http://www.w3.org/2005/Atom"); + + /* searching for title */ + xpcontext->node = xnode; + xpresult = xmlXPathEvalExpression ((const xmlChar *)"child::atom:title", xpcontext); + if (xpresult != NULL + && xpresult->nodesetval != NULL + && xpresult->nodesetval->nodeNr > 0) + { + title = atom_text_new_from_xml (xpresult->nodesetval->nodeTab[0]); + if (title == NULL) + { + g_warning ("Invalid title element."); + } + else + { + g_object_set (G_OBJECT (gapp_collection), + "title", title, + NULL); + } + } + + /* searching for accept */ + xpcontext->node = xnode; + xpresult = xmlXPathEvalExpression ((const xmlChar *)"child::xmlns:accept", xpcontext); + if (xpresult != NULL + && xpresult->nodesetval != NULL) + { + for (i = 0; i < xpresult->nodesetval->nodeNr; i++) + { + } + } + + return gapp_collection; +} + +/** + * atgapp_collection_get_xml: + * @gapp_collection: an #GappCollection object. + * @xnode: an #xmlNode. + * + */ +void +gapp_collection_get_xml (GappCollection *gapp_collection, xmlNode *xnode) +{ + xmlNode *xnodenew; + GList *lst; + + GappCollectionPrivate *priv = GAPP_COLLECTION_GET_PRIVATE (gapp_collection); + + atom_common_get_xml (ATOM_COMMON (gapp_collection), xnode); + + if (priv->title != NULL) + { + xnodenew = xmlNewNode (NULL, (const xmlChar *)"title"); + xmlAddChild (xnode, xnodenew); + atom_text_get_xml (priv->title, xnodenew); + } +} + +const gchar +*gapp_collection_get_href (GappCollection *gapp_collection) +{ + GappCollectionPrivate *priv = GAPP_COLLECTION_GET_PRIVATE (gapp_collection); + + return (const gchar *)priv->href; +} + +const gchar +*gapp_collection_get_title (GappCollection *gapp_collection) +{ + const gchar *ret; + AtomText *title; + + ret = NULL; + + g_object_get (G_OBJECT (gapp_collection), + "title", &title, + NULL); + + if (title != NULL) + { + g_object_get (G_OBJECT (title), + "text", &ret, + NULL); + } + + return ret; +} + +static void +gapp_collection_set_property (GObject *object, guint property_id, const GValue *value, GParamSpec *pspec) +{ + GObject *obj; + + GappCollection *collection = GAPP_COLLECTION (object); + GappCollectionPrivate *priv = GAPP_COLLECTION_GET_PRIVATE (collection); + + switch (property_id) + { + case PROP_HREF: + if (g_strcmp0 (g_value_get_string (value), "") != 0) + { + if (priv->href != NULL) + { + g_free (priv->href); + } + + priv->href = g_strdup (g_value_get_string (value)); + } + break; + + case PROP_TITLE: + if (g_value_get_object (value) != NULL && IS_ATOM_TEXT (g_value_get_object (value))) + { + priv->title = (AtomText *)g_value_dup_object (value); + } + break; + + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); + break; + } +} + +static void +gapp_collection_get_property (GObject *object, guint property_id, GValue *value, GParamSpec *pspec) +{ + GappCollection *collection = GAPP_COLLECTION (object); + GappCollectionPrivate *priv = GAPP_COLLECTION_GET_PRIVATE (collection); + + switch (property_id) + { + case PROP_HREF: + g_value_set_string (value, priv->href); + break; + + case PROP_TITLE: + g_value_set_object (value, priv->title); + break; + + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); + break; + } +} diff --git a/src/collection.h b/src/collection.h new file mode 100644 index 0000000..634821a --- /dev/null +++ b/src/collection.h @@ -0,0 +1,70 @@ +/* libgfeed + * + * Copyright (C) 2012 Andrea Zagli + * + * 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 Street, Fifth Floor, Boston, MA 02110-1301 USA + * + * Andrea Zagli + */ + +#ifndef __GAPP_COLLECTION_H__ +#define __GAPP_COLLECTION_H__ + +#include +#include +#include + +#include + +#include "collection.h" + +G_BEGIN_DECLS + + +#define TYPE_GAPP_COLLECTION (gapp_collection_get_type ()) +#define GAPP_COLLECTION(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_GAPP_COLLECTION, GappCollection)) +#define GAPP_COLLECTION_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_GAPP_COLLECTION, GappCollectionClass)) +#define IS_GAPP_COLLECTION(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_GAPP_COLLECTION)) +#define IS_GAPP_COLLECTION_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_GAPP_COLLECTION)) +#define GAPP_COLLECTION_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_GAPP_COLLECTION, GappCollectionClass)) + + +typedef struct _GappCollection GappCollection; +typedef struct _GappCollectionClass GappCollectionClass; + +struct _GappCollection + { + GObject parent; + }; + +struct _GappCollectionClass + { + GObjectClass parent_class; + }; + +GType gapp_collection_get_type (void) G_GNUC_CONST; + +GappCollection *gapp_collection_new (const gchar *href); +GappCollection *gapp_collection_new_from_xml (xmlNode *xnode); + +void gapp_collection_get_xml (GappCollection *gapp_collection, xmlNode *xnode); + +const gchar *gapp_collection_get_href (GappCollection *gapp_collection); +const gchar *gapp_collection_get_title (GappCollection *gapp_collection); + + +G_END_DECLS + +#endif /* __GAPP_COLLECTION_H__ */ diff --git a/src/workspace.c b/src/workspace.c index dc4c031..ebc82a0 100644 --- a/src/workspace.c +++ b/src/workspace.c @@ -24,6 +24,7 @@ #include #include "workspace.h" +#include "collection.h" enum { @@ -49,6 +50,8 @@ typedef struct _GappWorkspacePrivate GappWorkspacePrivate; struct _GappWorkspacePrivate { AtomText *title; + + GSList *collection; }; G_DEFINE_TYPE (GappWorkspace, gapp_workspace, TYPE_ATOM_COMMON) @@ -72,8 +75,9 @@ gapp_workspace_class_init (GappWorkspaceClass *klass) } static void -gapp_workspace_init (GappWorkspace *atome) +gapp_workspace_init (GappWorkspace *workspace) { + } /** @@ -110,6 +114,8 @@ GappWorkspace xmlXPathObject *xpresult; xmlXPathContext *xpcontext; + GappCollection *collection; + title = NULL; xpcontext = xmlXPathNewContext (xnode->doc); @@ -143,6 +149,14 @@ GappWorkspace { if (xmlStrcmp (cur->name, (const xmlChar *)"collection") == 0) { + collection = gapp_collection_new_from_xml (cur); + if (collection != NULL) + { + g_message ("Collection found: \"%s\" - \"%s\"", + gapp_collection_get_href (collection), + gapp_collection_get_title (collection)); + priv->collection = g_slist_append (priv->collection, collection); + } } cur = cur->next;