Blame gdata/gcontact/gdata-gcontact-jot.c

Packit 4b6dd7
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
Packit 4b6dd7
/*
Packit 4b6dd7
 * GData Client
Packit 4b6dd7
 * Copyright (C) Philip Withnall 2010 <philip@tecnocode.co.uk>
Packit 4b6dd7
 *
Packit 4b6dd7
 * GData Client is free software; you can redistribute it and/or
Packit 4b6dd7
 * modify it under the terms of the GNU Lesser General Public
Packit 4b6dd7
 * License as published by the Free Software Foundation; either
Packit 4b6dd7
 * version 2.1 of the License, or (at your option) any later version.
Packit 4b6dd7
 *
Packit 4b6dd7
 * GData Client is distributed in the hope that it will be useful,
Packit 4b6dd7
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 4b6dd7
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 4b6dd7
 * Lesser General Public License for more details.
Packit 4b6dd7
 *
Packit 4b6dd7
 * You should have received a copy of the GNU Lesser General Public
Packit 4b6dd7
 * License along with GData Client.  If not, see <http://www.gnu.org/licenses/>.
Packit 4b6dd7
 */
Packit 4b6dd7
Packit 4b6dd7
/**
Packit 4b6dd7
 * SECTION:gdata-gcontact-jot
Packit 4b6dd7
 * @short_description: gContact jot element
Packit 4b6dd7
 * @stability: Stable
Packit 4b6dd7
 * @include: gdata/gcontact/gdata-gcontact-jot.h
Packit 4b6dd7
 *
Packit 4b6dd7
 * #GDataGContactJot represents a "jot" element from the
Packit 4b6dd7
 * <ulink type="http" url="http://code.google.com/apis/contacts/docs/3.0/reference.html#gcJot">gContact specification</ulink>.
Packit 4b6dd7
 *
Packit 4b6dd7
 * Since: 0.7.0
Packit 4b6dd7
 */
Packit 4b6dd7
Packit 4b6dd7
#include <glib.h>
Packit 4b6dd7
#include <libxml/parser.h>
Packit 4b6dd7
Packit 4b6dd7
#include "gdata-gcontact-jot.h"
Packit 4b6dd7
#include "gdata-parsable.h"
Packit 4b6dd7
#include "gdata-parser.h"
Packit 4b6dd7
Packit 4b6dd7
static void gdata_gcontact_jot_finalize (GObject *object);
Packit 4b6dd7
static void gdata_gcontact_jot_get_property (GObject *object, guint property_id, GValue *value, GParamSpec *pspec);
Packit 4b6dd7
static void gdata_gcontact_jot_set_property (GObject *object, guint property_id, const GValue *value, GParamSpec *pspec);
Packit 4b6dd7
static gboolean pre_parse_xml (GDataParsable *parsable, xmlDoc *doc, xmlNode *root_node, gpointer user_data, GError **error);
Packit 4b6dd7
static gboolean parse_xml (GDataParsable *parsable, xmlDoc *doc, xmlNode *root_node, gpointer user_data, GError **error);
Packit 4b6dd7
static void pre_get_xml (GDataParsable *parsable, GString *xml_string);
Packit 4b6dd7
static void get_xml (GDataParsable *parsable, GString *xml_string);
Packit 4b6dd7
static void get_namespaces (GDataParsable *parsable, GHashTable *namespaces);
Packit 4b6dd7
Packit 4b6dd7
struct _GDataGContactJotPrivate {
Packit 4b6dd7
	gchar *content;
Packit 4b6dd7
	gchar *relation_type;
Packit 4b6dd7
};
Packit 4b6dd7
Packit 4b6dd7
enum {
Packit 4b6dd7
	PROP_CONTENT = 1,
Packit 4b6dd7
	PROP_RELATION_TYPE
Packit 4b6dd7
};
Packit 4b6dd7
Packit 4b6dd7
G_DEFINE_TYPE (GDataGContactJot, gdata_gcontact_jot, GDATA_TYPE_PARSABLE)
Packit 4b6dd7
Packit 4b6dd7
static void
Packit 4b6dd7
gdata_gcontact_jot_class_init (GDataGContactJotClass *klass)
Packit 4b6dd7
{
Packit 4b6dd7
	GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
Packit 4b6dd7
	GDataParsableClass *parsable_class = GDATA_PARSABLE_CLASS (klass);
Packit 4b6dd7
Packit 4b6dd7
	g_type_class_add_private (klass, sizeof (GDataGContactJotPrivate));
Packit 4b6dd7
Packit 4b6dd7
	gobject_class->get_property = gdata_gcontact_jot_get_property;
Packit 4b6dd7
	gobject_class->set_property = gdata_gcontact_jot_set_property;
Packit 4b6dd7
	gobject_class->finalize = gdata_gcontact_jot_finalize;
Packit 4b6dd7
Packit 4b6dd7
	parsable_class->pre_parse_xml = pre_parse_xml;
Packit 4b6dd7
	parsable_class->parse_xml = parse_xml;
Packit 4b6dd7
	parsable_class->pre_get_xml = pre_get_xml;
Packit 4b6dd7
	parsable_class->get_xml = get_xml;
Packit 4b6dd7
	parsable_class->get_namespaces = get_namespaces;
Packit 4b6dd7
	parsable_class->element_name = "jot";
Packit 4b6dd7
	parsable_class->element_namespace = "gContact";
Packit 4b6dd7
Packit 4b6dd7
	/**
Packit 4b6dd7
	 * GDataGContactJot:content:
Packit 4b6dd7
	 *
Packit 4b6dd7
	 * The content of the jot.
Packit 4b6dd7
	 *
Packit 4b6dd7
	 * For more information, see the
Packit 4b6dd7
	 * <ulink type="http" url="http://code.google.com/apis/contacts/docs/3.0/reference.html#gcJot">gContact specification</ulink>.
Packit 4b6dd7
	 *
Packit 4b6dd7
	 * Since: 0.7.0
Packit 4b6dd7
	 */
Packit 4b6dd7
	g_object_class_install_property (gobject_class, PROP_CONTENT,
Packit 4b6dd7
	                                 g_param_spec_string ("content",
Packit 4b6dd7
	                                                      "Content", "The content of the jot.",
Packit 4b6dd7
	                                                      NULL,
Packit 4b6dd7
	                                                      G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
Packit 4b6dd7
Packit 4b6dd7
	/**
Packit 4b6dd7
	 * GDataGContactJot:relation-type:
Packit 4b6dd7
	 *
Packit 4b6dd7
	 * A programmatic value that identifies the type of jot. Examples are %GDATA_GCONTACT_JOT_HOME or %GDATA_GCONTACT_JOT_OTHER.
Packit 4b6dd7
	 *
Packit 4b6dd7
	 * For more information, see the
Packit 4b6dd7
	 * <ulink type="http" url="http://code.google.com/apis/contacts/docs/3.0/reference.html#gcJot">gContact specification</ulink>.
Packit 4b6dd7
	 *
Packit 4b6dd7
	 * Since: 0.7.0
Packit 4b6dd7
	 */
Packit 4b6dd7
	g_object_class_install_property (gobject_class, PROP_RELATION_TYPE,
Packit 4b6dd7
	                                 g_param_spec_string ("relation-type",
Packit 4b6dd7
	                                                      "Relation type", "A programmatic value that identifies the type of jot.",
Packit 4b6dd7
	                                                      NULL,
Packit 4b6dd7
	                                                      G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
Packit 4b6dd7
}
Packit 4b6dd7
Packit 4b6dd7
static void
Packit 4b6dd7
gdata_gcontact_jot_init (GDataGContactJot *self)
Packit 4b6dd7
{
Packit 4b6dd7
	self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, GDATA_TYPE_GCONTACT_JOT, GDataGContactJotPrivate);
Packit 4b6dd7
}
Packit 4b6dd7
Packit 4b6dd7
static void
Packit 4b6dd7
gdata_gcontact_jot_finalize (GObject *object)
Packit 4b6dd7
{
Packit 4b6dd7
	GDataGContactJotPrivate *priv = GDATA_GCONTACT_JOT (object)->priv;
Packit 4b6dd7
Packit 4b6dd7
	g_free (priv->content);
Packit 4b6dd7
	g_free (priv->relation_type);
Packit 4b6dd7
Packit 4b6dd7
	/* Chain up to the parent class */
Packit 4b6dd7
	G_OBJECT_CLASS (gdata_gcontact_jot_parent_class)->finalize (object);
Packit 4b6dd7
}
Packit 4b6dd7
Packit 4b6dd7
static void
Packit 4b6dd7
gdata_gcontact_jot_get_property (GObject *object, guint property_id, GValue *value, GParamSpec *pspec)
Packit 4b6dd7
{
Packit 4b6dd7
	GDataGContactJotPrivate *priv = GDATA_GCONTACT_JOT (object)->priv;
Packit 4b6dd7
Packit 4b6dd7
	switch (property_id) {
Packit 4b6dd7
		case PROP_CONTENT:
Packit 4b6dd7
			g_value_set_string (value, priv->content);
Packit 4b6dd7
			break;
Packit 4b6dd7
		case PROP_RELATION_TYPE:
Packit 4b6dd7
			g_value_set_string (value, priv->relation_type);
Packit 4b6dd7
			break;
Packit 4b6dd7
		default:
Packit 4b6dd7
			/* We don't have any other property... */
Packit 4b6dd7
			G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
Packit 4b6dd7
			break;
Packit 4b6dd7
	}
Packit 4b6dd7
}
Packit 4b6dd7
Packit 4b6dd7
static void
Packit 4b6dd7
gdata_gcontact_jot_set_property (GObject *object, guint property_id, const GValue *value, GParamSpec *pspec)
Packit 4b6dd7
{
Packit 4b6dd7
	GDataGContactJot *self = GDATA_GCONTACT_JOT (object);
Packit 4b6dd7
Packit 4b6dd7
	switch (property_id) {
Packit 4b6dd7
		case PROP_CONTENT:
Packit 4b6dd7
			gdata_gcontact_jot_set_content (self, g_value_get_string (value));
Packit 4b6dd7
			break;
Packit 4b6dd7
		case PROP_RELATION_TYPE:
Packit 4b6dd7
			gdata_gcontact_jot_set_relation_type (self, g_value_get_string (value));
Packit 4b6dd7
			break;
Packit 4b6dd7
		default:
Packit 4b6dd7
			/* We don't have any other property... */
Packit 4b6dd7
			G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
Packit 4b6dd7
			break;
Packit 4b6dd7
	}
Packit 4b6dd7
}
Packit 4b6dd7
Packit 4b6dd7
static gboolean
Packit 4b6dd7
pre_parse_xml (GDataParsable *parsable, xmlDoc *doc, xmlNode *root_node, gpointer user_data, GError **error)
Packit 4b6dd7
{
Packit 4b6dd7
	xmlChar *rel;
Packit 4b6dd7
	GDataGContactJotPrivate *priv = GDATA_GCONTACT_JOT (parsable)->priv;
Packit 4b6dd7
Packit 4b6dd7
	rel = xmlGetProp (root_node, (xmlChar*) "rel");
Packit 4b6dd7
	if (rel == NULL || *rel == '\0') {
Packit 4b6dd7
		xmlFree (rel);
Packit 4b6dd7
		return gdata_parser_error_required_property_missing (root_node, "rel", error);
Packit 4b6dd7
	}
Packit 4b6dd7
Packit 4b6dd7
	priv->content = (gchar*) xmlNodeListGetString (doc, root_node->children, TRUE);
Packit 4b6dd7
	priv->relation_type = (gchar*) rel;
Packit 4b6dd7
Packit 4b6dd7
	return TRUE;
Packit 4b6dd7
}
Packit 4b6dd7
Packit 4b6dd7
static gboolean
Packit 4b6dd7
parse_xml (GDataParsable *parsable, xmlDoc *doc, xmlNode *node, gpointer user_data, GError **error)
Packit 4b6dd7
{
Packit 4b6dd7
	/* Textual content's handled in pre_parse_xml */
Packit 4b6dd7
	if (node->type != XML_ELEMENT_NODE)
Packit 4b6dd7
		return TRUE;
Packit 4b6dd7
Packit 4b6dd7
	return GDATA_PARSABLE_CLASS (gdata_gcontact_jot_parent_class)->parse_xml (parsable, doc, node, user_data, error);
Packit 4b6dd7
}
Packit 4b6dd7
Packit 4b6dd7
static void
Packit 4b6dd7
pre_get_xml (GDataParsable *parsable, GString *xml_string)
Packit 4b6dd7
{
Packit 4b6dd7
	gdata_parser_string_append_escaped (xml_string, " rel='", GDATA_GCONTACT_JOT (parsable)->priv->relation_type, "'");
Packit 4b6dd7
}
Packit 4b6dd7
Packit 4b6dd7
static void
Packit 4b6dd7
get_xml (GDataParsable *parsable, GString *xml_string)
Packit 4b6dd7
{
Packit 4b6dd7
	gdata_parser_string_append_escaped (xml_string, NULL, GDATA_GCONTACT_JOT (parsable)->priv->content, NULL);
Packit 4b6dd7
}
Packit 4b6dd7
Packit 4b6dd7
static void
Packit 4b6dd7
get_namespaces (GDataParsable *parsable, GHashTable *namespaces)
Packit 4b6dd7
{
Packit 4b6dd7
	g_hash_table_insert (namespaces, (gchar*) "gContact", (gchar*) "http://schemas.google.com/contact/2008");
Packit 4b6dd7
}
Packit 4b6dd7
Packit 4b6dd7
/**
Packit 4b6dd7
 * gdata_gcontact_jot_new:
Packit 4b6dd7
 * @content: the content of the jot
Packit 4b6dd7
 * @relation_type: the relationship between the jot and its owner
Packit 4b6dd7
 *
Packit 4b6dd7
 * Creates a new #GDataGContactJot. More information is available in the 
Packit 4b6dd7
 * url="http://code.google.com/apis/contacts/docs/3.0/reference.html#gcJot">gContact specification</ulink>.
Packit 4b6dd7
 *
Packit 4b6dd7
 * Return value: a new #GDataGContactJot; unref with g_object_unref()
Packit 4b6dd7
 *
Packit 4b6dd7
 * Since: 0.7.0
Packit 4b6dd7
 */
Packit 4b6dd7
GDataGContactJot *
Packit 4b6dd7
gdata_gcontact_jot_new (const gchar *content, const gchar *relation_type)
Packit 4b6dd7
{
Packit 4b6dd7
	g_return_val_if_fail (content != NULL, NULL);
Packit 4b6dd7
	g_return_val_if_fail (relation_type != NULL && *relation_type != '\0', NULL);
Packit 4b6dd7
	return g_object_new (GDATA_TYPE_GCONTACT_JOT, "content", content, "relation-type", relation_type, NULL);
Packit 4b6dd7
}
Packit 4b6dd7
Packit 4b6dd7
/**
Packit 4b6dd7
 * gdata_gcontact_jot_get_content:
Packit 4b6dd7
 * @self: a #GDataGContactJot
Packit 4b6dd7
 *
Packit 4b6dd7
 * Gets the #GDataGContactJot:content property.
Packit 4b6dd7
 *
Packit 4b6dd7
 * Return value: the jot's content
Packit 4b6dd7
 *
Packit 4b6dd7
 * Since: 0.7.0
Packit 4b6dd7
 */
Packit 4b6dd7
const gchar *
Packit 4b6dd7
gdata_gcontact_jot_get_content (GDataGContactJot *self)
Packit 4b6dd7
{
Packit 4b6dd7
	g_return_val_if_fail (GDATA_IS_GCONTACT_JOT (self), NULL);
Packit 4b6dd7
	return self->priv->content;
Packit 4b6dd7
}
Packit 4b6dd7
Packit 4b6dd7
/**
Packit 4b6dd7
 * gdata_gcontact_jot_set_content:
Packit 4b6dd7
 * @self: a #GDataGContactJot
Packit 4b6dd7
 * @content: the new content
Packit 4b6dd7
 *
Packit 4b6dd7
 * Sets the #GDataGContactJot:content property to @content.
Packit 4b6dd7
 *
Packit 4b6dd7
 * Since: 0.7.0
Packit 4b6dd7
 */
Packit 4b6dd7
void
Packit 4b6dd7
gdata_gcontact_jot_set_content (GDataGContactJot *self, const gchar *content)
Packit 4b6dd7
{
Packit 4b6dd7
	g_return_if_fail (GDATA_IS_GCONTACT_JOT (self));
Packit 4b6dd7
	g_return_if_fail (content != NULL);
Packit 4b6dd7
Packit 4b6dd7
	g_free (self->priv->content);
Packit 4b6dd7
	self->priv->content = g_strdup (content);
Packit 4b6dd7
	g_object_notify (G_OBJECT (self), "content");
Packit 4b6dd7
}
Packit 4b6dd7
Packit 4b6dd7
/**
Packit 4b6dd7
 * gdata_gcontact_jot_get_relation_type:
Packit 4b6dd7
 * @self: a #GDataGContactJot
Packit 4b6dd7
 *
Packit 4b6dd7
 * Gets the #GDataGContactJot:relation-type property.
Packit 4b6dd7
 *
Packit 4b6dd7
 * Return value: the jot's relation type
Packit 4b6dd7
 *
Packit 4b6dd7
 * Since: 0.7.0
Packit 4b6dd7
 */
Packit 4b6dd7
const gchar *
Packit 4b6dd7
gdata_gcontact_jot_get_relation_type (GDataGContactJot *self)
Packit 4b6dd7
{
Packit 4b6dd7
	g_return_val_if_fail (GDATA_IS_GCONTACT_JOT (self), NULL);
Packit 4b6dd7
	return self->priv->relation_type;
Packit 4b6dd7
}
Packit 4b6dd7
Packit 4b6dd7
/**
Packit 4b6dd7
 * gdata_gcontact_jot_set_relation_type:
Packit 4b6dd7
 * @self: a #GDataGContactJot
Packit 4b6dd7
 * @relation_type: the new relation type for the jot, or %NULL
Packit 4b6dd7
 *
Packit 4b6dd7
 * Sets the #GDataGContactJot:relation-type property to @relation_type
Packit 4b6dd7
 * such as %GDATA_GCONTACT_JOT_HOME or %GDATA_GCONTACT_JOT_OTHER.
Packit 4b6dd7
 *
Packit 4b6dd7
 * Since: 0.7.0
Packit 4b6dd7
 */
Packit 4b6dd7
void
Packit 4b6dd7
gdata_gcontact_jot_set_relation_type (GDataGContactJot *self, const gchar *relation_type)
Packit 4b6dd7
{
Packit 4b6dd7
	g_return_if_fail (GDATA_IS_GCONTACT_JOT (self));
Packit 4b6dd7
	g_return_if_fail (relation_type != NULL && *relation_type != '\0');
Packit 4b6dd7
Packit 4b6dd7
	g_free (self->priv->relation_type);
Packit 4b6dd7
	self->priv->relation_type = g_strdup (relation_type);
Packit 4b6dd7
	g_object_notify (G_OBJECT (self), "relation-type");
Packit 4b6dd7
}