summaryrefslogtreecommitdiff
path: root/gdata/app/gdata-app-categories.c
blob: 31585ff02d31603c3be93442f7bf7e6f42efcc86 (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
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/*
 * GData Client
 * Copyright (C) Philip Withnall 2010, 2015 <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-app-categories
 * @short_description: GData APP categories object
 * @stability: Stable
 * @include: gdata/app/gdata-app-categories.h
 *
 * #GDataAPPCategories is a list of categories (#GDataCategory) returned as the result of querying an
 * <ulink type="http" url="http://www.atomenabled.org/developers/protocol/#category">Atom Publishing Protocol Category Document</ulink>.
 *
 * Since: 0.7.0
 */

#include <config.h>
#include <glib.h>

#include "gdata-app-categories.h"
#include "atom/gdata-category.h"
#include "gdata-parsable.h"
#include "gdata-parser.h"

static void gdata_app_categories_dispose (GObject *object);
static void gdata_app_categories_finalize (GObject *object);
static void gdata_app_categories_get_property (GObject *object, guint property_id, GValue *value, GParamSpec *pspec);
static gboolean
parse_json (GDataParsable *parsable, JsonReader *reader, gpointer user_data,
            GError **error);
static gboolean
post_parse_json (GDataParsable *parsable, gpointer user_data, GError **error);
static const gchar *
get_content_type (void);

struct _GDataAPPCategoriesPrivate {
	GList *categories;
	gchar *scheme;
	gboolean fixed;
};

enum {
	PROP_IS_FIXED = 1
};

G_DEFINE_TYPE (GDataAPPCategories, gdata_app_categories, GDATA_TYPE_PARSABLE)

static void
gdata_app_categories_class_init (GDataAPPCategoriesClass *klass)
{
	GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
	GDataParsableClass *parsable_class = GDATA_PARSABLE_CLASS (klass);

	g_type_class_add_private (klass, sizeof (GDataAPPCategoriesPrivate));

	gobject_class->get_property = gdata_app_categories_get_property;
	gobject_class->dispose = gdata_app_categories_dispose;
	gobject_class->finalize = gdata_app_categories_finalize;

	parsable_class->parse_json = parse_json;
	parsable_class->post_parse_json = post_parse_json;
	parsable_class->get_content_type = get_content_type;

	parsable_class->element_name = "categories";
	parsable_class->element_namespace = "app";

	/**
	 * GDataAPPCategories:is-fixed:
	 *
	 * Whether entries may use categories not in this category list.
	 *
	 * API reference: <ulink type="http" url="http://www.atomenabled.org/developers/protocol/#appCategories2">app:categories</ulink>
	 *
	 * Since: 0.7.0
	 */
	g_object_class_install_property (gobject_class, PROP_IS_FIXED,
	                                 g_param_spec_boolean ("is-fixed",
	                                                       "Fixed?", "Whether entries may use categories not in this category list.",
	                                                       FALSE,
	                                                       G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
}

static void
gdata_app_categories_init (GDataAPPCategories *self)
{
	self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, GDATA_TYPE_APP_CATEGORIES, GDataAPPCategoriesPrivate);
}

static void
gdata_app_categories_dispose (GObject *object)
{
	GDataAPPCategoriesPrivate *priv = GDATA_APP_CATEGORIES (object)->priv;

	if (priv->categories != NULL) {
		g_list_foreach (priv->categories, (GFunc) g_object_unref, NULL);
		g_list_free (priv->categories);
	}
	priv->categories = NULL;

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

static void
gdata_app_categories_finalize (GObject *object)
{
	GDataAPPCategoriesPrivate *priv = GDATA_APP_CATEGORIES (object)->priv;

	g_free (priv->scheme);

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

static void
gdata_app_categories_get_property (GObject *object, guint property_id, GValue *value, GParamSpec *pspec)
{
	GDataAPPCategoriesPrivate *priv = GDATA_APP_CATEGORIES (object)->priv;

	switch (property_id) {
		case PROP_IS_FIXED:
			g_value_set_boolean (value, priv->fixed);
			break;
		default:
			/* We don't have any other property... */
			G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
			break;
	}
}

/* Reference: https://developers.google.com/youtube/v3/docs/videoCategories/list */
static gboolean
parse_json (GDataParsable *parsable, JsonReader *reader, gpointer user_data, GError **error)
{
	GDataAPPCategories *self = GDATA_APP_CATEGORIES (parsable);
	GDataAPPCategoriesPrivate *priv = self->priv;
	GType category_type;

	category_type = (user_data == NULL) ?
		GDATA_TYPE_CATEGORY : GPOINTER_TO_SIZE (user_data);

	if (g_strcmp0 (json_reader_get_member_name (reader), "items") == 0) {
		guint i, elements;

		/* Loop through the elements array. */
		for (i = 0, elements = (guint) json_reader_count_elements (reader); i < elements; i++) {
			GDataCategory *category = NULL;
			const gchar *id, *title;
			const GError *child_error = NULL;

			json_reader_read_element (reader, i);

			json_reader_read_member (reader, "id");
			id = json_reader_get_string_value (reader);
			json_reader_end_member (reader);

			json_reader_read_member (reader, "snippet");

			json_reader_read_member (reader, "title");
			title = json_reader_get_string_value (reader);
			json_reader_end_member (reader);

			child_error = json_reader_get_error (reader);

			if (child_error != NULL) {
				return gdata_parser_error_from_json_error (reader,
				                                           child_error,
				                                           error);
			}

			/* Create the category. */
			category = g_object_new (category_type,
			                         "term", id,
			                         "label", title,
			                         NULL);
			priv->categories = g_list_prepend (priv->categories,
			                                   category);

			json_reader_end_member (reader);  /* snippet */
			json_reader_end_element (reader);  /* category */
		}

		return TRUE;
	} else if (g_strcmp0 (json_reader_get_member_name (reader), "kind") == 0 ||
	           g_strcmp0 (json_reader_get_member_name (reader), "etag") == 0 ||
	           g_strcmp0 (json_reader_get_member_name (reader), "id") == 0) {
		/* Ignore. */
		return TRUE;
	} else {
		return GDATA_PARSABLE_CLASS (gdata_app_categories_parent_class)->parse_json (parsable, reader, user_data, error);
	}
}

static gboolean
post_parse_json (GDataParsable *parsable, gpointer user_data, GError **error)
{
	GDataAPPCategoriesPrivate *priv = GDATA_APP_CATEGORIES (parsable)->priv;

	/* Reverse our lists of stuff. */
	priv->categories = g_list_reverse (priv->categories);

	return TRUE;
}

static const gchar *
get_content_type (void)
{
	return "application/json";
}

/**
 * gdata_app_categories_get_categories:
 * @self: a #GDataAPPCategories
 *
 * Returns a list of the categories in this category list.
 *
 * Return value: (element-type GData.Category) (transfer none): a #GList of #GDataCategory<!-- -->s
 *
 * Since: 0.7.0
 */
GList *
gdata_app_categories_get_categories (GDataAPPCategories *self)
{
	g_return_val_if_fail (GDATA_IS_APP_CATEGORIES (self), NULL);
	return self->priv->categories;
}

/**
 * gdata_app_categories_is_fixed:
 * @self: a #GDataAPPCategories
 *
 * Returns #GDataAPPCategories:is-fixed.
 *
 * Return value: whether entries may use categories not in this category list
 *
 * Since: 0.7.0
 */
gboolean
gdata_app_categories_is_fixed (GDataAPPCategories *self)
{
	g_return_val_if_fail (GDATA_IS_APP_CATEGORIES (self), FALSE);
	return self->priv->fixed;
}