summaryrefslogtreecommitdiff
path: root/gdata/services/youtube/gdata-youtube-category.c
blob: c28ab614170a1e70aaf731a807952918911433fd (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
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/*
 * GData Client
 * Copyright (C) Philip Withnall 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-youtube-category
 * @short_description: YouTube category element
 * @stability: Stable
 * @include: gdata/services/youtube/gdata-youtube-category.h
 *
 * #GDataYouTubeCategory represents the YouTube-specific customizations to #GDataCategory. For more information,
 * see the <ulink type="http" url="http://code.google.com/apis/youtube/2.0/reference.html#YouTube_Category_List">
 * online documentation</ulink>.
 *
 * Since: 0.7.0
 **/

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

#include "gdata-youtube-category.h"
#include "gdata-parsable.h"
#include "gdata-parser.h"

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

struct _GDataYouTubeCategoryPrivate {
	gboolean assignable;
	gchar **browsable_regions;
	/*gboolean deprecated; --- "Categories that are neither assignable or browsable are deprecated and are identified as such using the
	 * <yt:deprecated> tag." */
};

enum {
	PROP_IS_ASSIGNABLE = 1,
	PROP_IS_DEPRECATED
};

G_DEFINE_TYPE (GDataYouTubeCategory, gdata_youtube_category, GDATA_TYPE_CATEGORY)

static void
gdata_youtube_category_class_init (GDataYouTubeCategoryClass *klass)
{
	GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
	GDataParsableClass *parsable_class = GDATA_PARSABLE_CLASS (klass);

	g_type_class_add_private (klass, sizeof (GDataYouTubeCategoryPrivate));

	gobject_class->get_property = gdata_youtube_category_get_property;
	gobject_class->finalize = gdata_youtube_category_finalize;

	parsable_class->parse_xml = parse_xml;
	parsable_class->get_namespaces = get_namespaces;

	/**
	 * GDataYouTubeCategory:is-assignable:
	 *
	 * Whether new videos can be added to the category.
	 *
	 * Since: 0.7.0
	 **/
	g_object_class_install_property (gobject_class, PROP_IS_ASSIGNABLE,
	                                 g_param_spec_boolean ("is-assignable",
	                                                       "Assignable?", "Whether new videos can be added to the category.",
	                                                       FALSE,
	                                                       G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));

	/**
	 * GDataYouTubeCategory:is-deprecated:
	 *
	 * Whether the category is deprecated.
	 *
	 * Since: 0.7.0
	 **/
	g_object_class_install_property (gobject_class, PROP_IS_DEPRECATED,
	                                 g_param_spec_boolean ("is-deprecated",
	                                                       "Deprecated?", "Whether the category is deprecated.",
	                                                       TRUE,
	                                                       G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
}

static void
gdata_youtube_category_init (GDataYouTubeCategory *self)
{
	self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, GDATA_TYPE_YOUTUBE_CATEGORY, GDataYouTubeCategoryPrivate);
}

static void
gdata_youtube_category_finalize (GObject *object)
{
	GDataYouTubeCategoryPrivate *priv = GDATA_YOUTUBE_CATEGORY (object)->priv;

	g_strfreev (priv->browsable_regions);

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

static void
gdata_youtube_category_get_property (GObject *object, guint property_id, GValue *value, GParamSpec *pspec)
{
	GDataYouTubeCategoryPrivate *priv = GDATA_YOUTUBE_CATEGORY (object)->priv;

	switch (property_id) {
		case PROP_IS_ASSIGNABLE:
			g_value_set_boolean (value, priv->assignable);
			break;
		case PROP_IS_DEPRECATED:
			g_value_set_boolean (value, (priv->assignable == FALSE && priv->browsable_regions == NULL) ? TRUE : FALSE);
			break;
		default:
			/* We don't have any other property... */
			G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
			break;
	}
}

static gboolean
parse_xml (GDataParsable *parsable, xmlDoc *doc, xmlNode *node, gpointer user_data, GError **error)
{
	GDataYouTubeCategory *self = GDATA_YOUTUBE_CATEGORY (parsable);

	if (gdata_parser_is_namespace (node, "http://gdata.youtube.com/schemas/2007") == TRUE) {
		if (xmlStrcmp (node->name, (xmlChar*) "assignable") == 0) {
			/* yt:assignable */
			self->priv->assignable = TRUE;
		} else if (xmlStrcmp (node->name, (xmlChar*) "deprecated") == 0) {
			/* yt:deprecated */
			self->priv->assignable = FALSE;
			g_strfreev (self->priv->browsable_regions);
			self->priv->browsable_regions = NULL;
		} else if (xmlStrcmp (node->name, (xmlChar*) "browsable") == 0) {
			/* yt:browsable */
			xmlChar *regions;

			regions = xmlGetProp (node, (xmlChar*) "regions");
			if (regions == NULL)
				return gdata_parser_error_required_property_missing (node, "regions", error);

			self->priv->browsable_regions = g_strsplit ((gchar*) regions, " ", -1);
			xmlFree (regions);
		} else {
			return GDATA_PARSABLE_CLASS (gdata_youtube_category_parent_class)->parse_xml (parsable, doc, node, user_data, error);
		}
	} else {
		return GDATA_PARSABLE_CLASS (gdata_youtube_category_parent_class)->parse_xml (parsable, doc, node, user_data, error);
	}

	return TRUE;
}

static void
get_namespaces (GDataParsable *parsable, GHashTable *namespaces)
{
	/* Chain up to the parent class */
	GDATA_PARSABLE_CLASS (gdata_youtube_category_parent_class)->get_namespaces (parsable, namespaces);

	g_hash_table_insert (namespaces, (gchar*) "youtube", (gchar*) "http://gdata.youtube.com/schemas/2007");
}

/**
 * gdata_youtube_category_is_assignable:
 * @self: a #GDataYouTubeCategory
 *
 * Gets the #GDataYouTubeCategory:is-assignable property.
 *
 * Return value: whether new videos can be assigned to the category
 *
 * Since: 0.7.0
 **/
gboolean
gdata_youtube_category_is_assignable (GDataYouTubeCategory *self)
{
	g_return_val_if_fail (GDATA_IS_YOUTUBE_CATEGORY (self), FALSE);
	return self->priv->assignable;
}

/**
 * gdata_youtube_category_is_browsable:
 * @self: a #GDataYouTubeCategory
 * @region: a two-letter region ID
 *
 * Returns whether the category is browsable in the given @region. The list of supported region IDs is
 * <ulink type="http" url="http://code.google.com/apis/youtube/2.0/reference.html#Region_specific_feeds">available online</ulink>.
 *
 * Return value: whether the category is browsable in @region
 *
 * Since: 0.7.0
 **/
gboolean
gdata_youtube_category_is_browsable (GDataYouTubeCategory *self, const gchar *region)
{
	g_return_val_if_fail (GDATA_IS_YOUTUBE_CATEGORY (self), FALSE);
	g_return_val_if_fail (region != NULL && *region != '\0', FALSE);

	if (self->priv->browsable_regions != NULL) {
		guint i;

		for (i = 0; self->priv->browsable_regions[i] != NULL; i++) {
			if (strcmp (self->priv->browsable_regions[i], region) == 0)
				return TRUE;
		}
	}

	return FALSE;
}

/**
 * gdata_youtube_category_is_deprecated:
 * @self: a #GDataYouTubeCategory
 *
 * Gets the #GDataYouTubeCategory:is-deprecated property.
 *
 * Return value: whether the category is deprecated
 *
 * Since: 0.7.0
 **/
gboolean
gdata_youtube_category_is_deprecated (GDataYouTubeCategory *self)
{
	g_return_val_if_fail (GDATA_IS_YOUTUBE_CATEGORY (self), TRUE);
	return (self->priv->assignable == FALSE && self->priv->browsable_regions == NULL) ? TRUE : FALSE;
}