summaryrefslogtreecommitdiff
path: root/gdata/media/gdata-media-credit.c
blob: 896693b942b6c51055f278a7d193de1d611d292a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/*
 * GData Client
 * Copyright (C) Philip Withnall 2009–2010 <philip@tecnocode.co.uk>
 *
 * GData Client 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.
 *
 * GData Client 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 GData Client.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * SECTION:gdata-media-credit
 * @short_description: Media RSS credit element
 * @stability: Stable
 * @include: gdata/media/gdata-media-credit.h
 *
 * #GDataMediaCredit represents a "credit" element from the
 * <ulink type="http" url="http://video.search.yahoo.com/mrss">Media RSS specification</ulink>.
 *
 * The class only implements parsing, not XML output, at the moment.
 *
 * Since: 0.4.0
 **/

#include <glib.h>
#include <libxml/parser.h>

#include "gdata-media-credit.h"
#include "gdata-parsable.h"
#include "gdata-parser.h"
#include "gdata-types.h"

static void gdata_media_credit_finalize (GObject *object);
static void gdata_media_credit_get_property (GObject *object, guint property_id, GValue *value, GParamSpec *pspec);
static gboolean pre_parse_xml (GDataParsable *parsable, xmlDoc *doc, xmlNode *root_node, gpointer user_data, GError **error);
static gboolean parse_xml (GDataParsable *parsable, xmlDoc *doc, xmlNode *node, gpointer user_data, GError **error);
static void get_namespaces (GDataParsable *parsable, GHashTable *namespaces);

struct _GDataMediaCreditPrivate {
	gchar *credit;
	gchar *scheme;
	gchar *role;
};

enum {
	PROP_CREDIT = 1,
	PROP_SCHEME,
	PROP_ROLE
};

G_DEFINE_TYPE (GDataMediaCredit, gdata_media_credit, GDATA_TYPE_PARSABLE)

static void
gdata_media_credit_class_init (GDataMediaCreditClass *klass)
{
	GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
	GDataParsableClass *parsable_class = GDATA_PARSABLE_CLASS (klass);

	g_type_class_add_private (klass, sizeof (GDataMediaCreditPrivate));

	gobject_class->get_property = gdata_media_credit_get_property;
	gobject_class->finalize = gdata_media_credit_finalize;

	parsable_class->pre_parse_xml = pre_parse_xml;
	parsable_class->parse_xml = parse_xml;
	parsable_class->get_namespaces = get_namespaces;
	parsable_class->element_name = "credit";
	parsable_class->element_namespace = "media";

	/**
	 * GDataMediaCredit:credit:
	 *
	 * The credited entity's name.
	 *
	 * For more information, see the <ulink type="http" url="http://video.search.yahoo.com/mrss">Media RSS specification</ulink>.
	 *
	 * Since: 0.4.0
	 **/
	g_object_class_install_property (gobject_class, PROP_CREDIT,
	                                 g_param_spec_string ("credit",
	                                                      "Credit", "The credited entity's name.",
	                                                      NULL,
	                                                      G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));

	/**
	 * GDataMediaCredit:scheme:
	 *
	 * A URI that identifies the role scheme.
	 *
	 * For more information, see the <ulink type="http" url="http://video.search.yahoo.com/mrss">Media RSS specification</ulink>.
	 *
	 * Since: 0.4.0
	 **/
	g_object_class_install_property (gobject_class, PROP_SCHEME,
	                                 g_param_spec_string ("scheme",
	                                                      "Scheme", "A URI that identifies the role scheme.",
	                                                      NULL,
	                                                      G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));

	/**
	 * GDataMediaCredit:role:
	 *
	 * The role the credited entity played in the production of the media.
	 *
	 * For more information, see the <ulink type="http" url="http://video.search.yahoo.com/mrss">Media RSS specification</ulink>.
	 *
	 * Since: 0.4.0
	 **/
	g_object_class_install_property (gobject_class, PROP_ROLE,
	                                 g_param_spec_string ("role",
	                                                      "Role", "The role the credited entity played in the production of the media.",
	                                                      NULL,
	                                                      G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
}

static void
gdata_media_credit_init (GDataMediaCredit *self)
{
	self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, GDATA_TYPE_MEDIA_CREDIT, GDataMediaCreditPrivate);
}

static void
gdata_media_credit_finalize (GObject *object)
{
	GDataMediaCreditPrivate *priv = GDATA_MEDIA_CREDIT (object)->priv;

	g_free (priv->credit);
	g_free (priv->scheme);
	g_free (priv->role);

	/* Chain up to the parent class */
	G_OBJECT_CLASS (gdata_media_credit_parent_class)->finalize (object);
}

static void
gdata_media_credit_get_property (GObject *object, guint property_id, GValue *value, GParamSpec *pspec)
{
	GDataMediaCreditPrivate *priv = GDATA_MEDIA_CREDIT (object)->priv;

	switch (property_id) {
		case PROP_CREDIT:
			g_value_set_string (value, priv->credit);
			break;
		case PROP_SCHEME:
			g_value_set_string (value, priv->scheme);
			break;
		case PROP_ROLE:
			g_value_set_string (value, priv->role);
			break;
		default:
			/* We don't have any other property... */
			G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
			break;
	}
}

static gboolean
pre_parse_xml (GDataParsable *parsable, xmlDoc *doc, xmlNode *root_node, gpointer user_data, GError **error)
{
	GDataMediaCreditPrivate *priv = GDATA_MEDIA_CREDIT (parsable)->priv;
	xmlChar *credit;
	gchar *scheme;
	guint i;

	credit = xmlNodeListGetString (doc, root_node->children, TRUE);
	if (credit == NULL || *credit == '\0') {
		xmlFree (credit);
		return gdata_parser_error_required_content_missing (root_node, error);
	}

	scheme = (gchar*) xmlGetProp (root_node, (xmlChar*) "scheme");
	if (scheme != NULL && *scheme == '\0') {
		g_free (scheme);
		xmlFree (credit);
		return gdata_parser_error_required_property_missing (root_node, "scheme", error);
	} else if (scheme == NULL) {
		/* Default */
		scheme = g_strdup ("urn:ebu");
	}

	priv->credit = (gchar*) credit;
	priv->scheme = scheme;
	priv->role = (gchar*) xmlGetProp (root_node, (xmlChar*) "role");

	/* Convert the role to lower case */
	if (priv->role != NULL) {
		for (i = 0; priv->role[i] != '\0'; i++)
			priv->role[i] = g_ascii_tolower (priv->role[i]);
	}

	return TRUE;
}

static gboolean
parse_xml (GDataParsable *parsable, xmlDoc *doc, xmlNode *node, gpointer user_data, GError **error)
{
	/* Textual content's handled in pre_parse_xml */
	if (node->type != XML_ELEMENT_NODE)
		return TRUE;

	return GDATA_PARSABLE_CLASS (gdata_media_credit_parent_class)->parse_xml (parsable, doc, node, user_data, error);
}

static void
get_namespaces (GDataParsable *parsable, GHashTable *namespaces)
{
	g_hash_table_insert (namespaces, (gchar*) "media", (gchar*) "http://search.yahoo.com/mrss/");
}

/**
 * gdata_media_credit_get_credit:
 * @self: a #GDataMediaCredit
 *
 * Gets the #GDataMediaCredit:credit property.
 *
 * Return value: the name of the credited entity
 *
 * Since: 0.4.0
 **/
const gchar *
gdata_media_credit_get_credit (GDataMediaCredit *self)
{
	g_return_val_if_fail (GDATA_IS_MEDIA_CREDIT (self), NULL);
	return self->priv->credit;
}

/**
 * gdata_media_credit_get_scheme:
 * @self: a #GDataMediaCredit
 *
 * Gets the #GDataMediaCredit:scheme property.
 *
 * Return value: the credit's role scheme, or %NULL
 *
 * Since: 0.4.0
 **/
const gchar *
gdata_media_credit_get_scheme (GDataMediaCredit *self)
{
	g_return_val_if_fail (GDATA_IS_MEDIA_CREDIT (self), NULL);
	return self->priv->scheme;
}

/**
 * gdata_media_credit_get_role:
 * @self: a #GDataMediaCredit
 *
 * Gets the #GDataMediaCredit:role property.
 *
 * Return value: the credited entity's role, or %NULL
 *
 * Since: 0.4.0
 **/
const gchar *
gdata_media_credit_get_role (GDataMediaCredit *self)
{
	g_return_val_if_fail (GDATA_IS_MEDIA_CREDIT (self), NULL);
	return self->priv->role;
}