summaryrefslogtreecommitdiff
path: root/gdata/services/documents/gdata-documents-property.c
blob: c1d497d5b9f99ecaf89332e498f13a464e79b343 (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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/*
 * GData Client
 * Copyright (C) Mayank Sharma <mayank8019@gmail.com>
 *
 * 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-documents-property
 * @short_description: GData Documents property object
 * @stability: Unstable
 * @include: gdata/gdata-property.h
 *
 * #GDataDocumentsProperty is a subclass of #GDataParsable and represents a Google Drive Property Resource on a file object.
 *
 * It allows applications to store additional metadata on a file, such as tags, IDs from other data stores, viewing preferences etc. Properties can be used to share metadata between applications, for example, in a workflow application.
 *
 * Each #GDataDocumentsProperty is characterized by a key-value pair (where value is optional, and takes empty string "" by default) and a visibility parameter. The visibility can take values "PUBLIC" for public properties and "PRIVATE" for private properties (default). Private properties are accessible only by the application which set them, but public properties can be read/written by other applications as well.
 *
 * Since: 0.17.11
 */

#include <glib.h>
#include <gdata/gdata-parsable.h>
#include <json-glib/json-glib.h>

#include "gdata-documents-property.h"
#include "gdata-parser.h"
#include "gdata-comparable.h"

static void gdata_documents_property_comparable_init (GDataComparableIface *iface);
static void gdata_documents_property_finalize (GObject *object);
static void gdata_documents_property_get_property (GObject *object, guint property_id, GValue *value, GParamSpec *pspec);
static void gdata_documents_property_set_property (GObject *object, guint property_id, const GValue *value, GParamSpec *pspec);

static void _gdata_documents_property_set_key (GDataDocumentsProperty *self, const gchar *key);
static void _gdata_documents_property_set_etag (GDataDocumentsProperty *self, const gchar *etag);

static gboolean parse_json (GDataParsable *parsable, JsonReader *reader, gpointer user_data, GError **error);
static void get_json (GDataParsable *parsable, JsonBuilder *builder);

struct _GDataDocumentsPropertyPrivate {
	gchar *key;
	gchar *etag;
	gchar *value;		/* default - empty string ("") */
	gchar *visibility;	/* default - "PRIVATE" */
};

enum {
	PROP_KEY = 1,
	PROP_ETAG,
	PROP_VALUE,
	PROP_VISIBILITY
};

G_DEFINE_TYPE_WITH_CODE (GDataDocumentsProperty, gdata_documents_property, GDATA_TYPE_PARSABLE,
			 G_IMPLEMENT_INTERFACE (GDATA_TYPE_COMPARABLE, gdata_documents_property_comparable_init))

static void
gdata_documents_property_class_init (GDataDocumentsPropertyClass *klass)
{
	GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
	GDataParsableClass *parsable_class = GDATA_PARSABLE_CLASS (klass);

	g_type_class_add_private (klass, sizeof (GDataDocumentsPropertyPrivate));

	gobject_class->set_property = gdata_documents_property_set_property;
	gobject_class->get_property = gdata_documents_property_get_property;
	gobject_class->finalize = gdata_documents_property_finalize;

	parsable_class->parse_json = parse_json;
	parsable_class->get_json = get_json;
	parsable_class->element_name = "property";

	/**
	 * GDataDocumentsProperty:key:
	 *
	 * The key of this property.
	 *
	 * For more information, see the <ulink type="http" url="https://developers.google.com/drive/api/v2/reference/properties">Properties Resource</ulink>
	 *
	 * Since: 0.17.11
	 */
	g_object_class_install_property (gobject_class, PROP_KEY,
					 g_param_spec_string ("key",
							      "Key", "The key of this property.",
							      NULL,
							      G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));

	/**
	 * GDataDocumentsProperty:etag:
	 *
	 * ETag of the property.
	 *
	 * For more information, see the <ulink type="http" url="https://developers.google.com/drive/api/v2/reference/properties">Properties Resource</ulink>
	 *
	 * Since: 0.17.11
	 */
	g_object_class_install_property (gobject_class, PROP_ETAG,
					 g_param_spec_string ("etag",
							      "ETag", "ETag of the property.",
							      NULL,
							      G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));

	/**
	 * GDataDocumentsProperty:value:
	 *
	 * The value of this property. By default, it takes the an empty string ("").
	 *
	 * For more information, see the <ulink type="http" url="https://developers.google.com/drive/api/v2/reference/properties">Properties Resource</ulink>
	 *
	 * Since: 0.17.11
	 */
	g_object_class_install_property (gobject_class, PROP_VALUE,
					 g_param_spec_string ("value",
							      "Value", "The value of this property.",
							      NULL,
							      G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));

	/**
	 * GDataDocumentsProperty:visibility:
	 *
	 * The visibility status of this property. The default value of
	 * visibility is PRIVATE on a Drive Properties Resource object,
	 * hence #GDataDocumentsProperty:visibility is %GDATA_DOCUMENTS_PROPERTY_VISIBILITY_PRIVATE
	 * by default. A private property restricts its visibility to only the app which created it.
	 *
	 * For more information, see the <ulink type="http" url="https://developers.google.com/drive/api/v2/reference/properties">Properties Resource</ulink>
	 *
	 * Since: 0.17.11
	 */
	g_object_class_install_property (gobject_class, PROP_VISIBILITY,
					 g_param_spec_string ("visibility",
							      "Visibility", "The visibility of this property.",
							      NULL,
							      G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
}

static gint
compare_with (GDataComparable *self, GDataComparable *other)
{

	GDataDocumentsPropertyPrivate *a = ((GDataDocumentsProperty*) self)->priv, *b = ((GDataDocumentsProperty*) other)->priv;

	if (g_strcmp0 (a->key, b->key) == 0 && g_strcmp0 (a->visibility, b->visibility) == 0)
		return 0;
	return 1;
}

static gboolean
parse_json (GDataParsable *parsable, JsonReader *reader, gpointer user_data, GError **error)
{
	gchar *output_val = NULL;
	gboolean success = TRUE, is_key_parsed = FALSE;

	if (gdata_parser_string_from_json_member (reader, "key", P_DEFAULT, &output_val, &success, error) == TRUE) {
		if (success && output_val != NULL && output_val[0] != '\0') {
			_gdata_documents_property_set_key (GDATA_DOCUMENTS_PROPERTY (parsable), output_val);
		}

		is_key_parsed = TRUE;
	} else if (gdata_parser_string_from_json_member (reader, "etag", P_DEFAULT, &output_val, &success, error) == TRUE) {
		if (success && output_val != NULL && output_val[0] != '\0') {
			_gdata_documents_property_set_etag (GDATA_DOCUMENTS_PROPERTY (parsable), output_val);
		}

		is_key_parsed = TRUE;
	} else if (gdata_parser_string_from_json_member (reader, "value", P_DEFAULT, &output_val, &success, error) == TRUE) {

		/* A Property can have a value field to be an empty string, but
		 * never NULL */
		if (success && output_val != NULL) {
			gdata_documents_property_set_value (GDATA_DOCUMENTS_PROPERTY (parsable), output_val);
		}

		is_key_parsed = TRUE;
	} else if (gdata_parser_string_from_json_member (reader, "visibility", P_REQUIRED | P_NON_EMPTY, &output_val, &success, error) == TRUE) {
		gdata_documents_property_set_visibility (GDATA_DOCUMENTS_PROPERTY (parsable), output_val);

		is_key_parsed = TRUE;
	}

	if (is_key_parsed) {
		g_free (output_val);
		return success;
	}

	/* Chain up to the parent class */
	return GDATA_PARSABLE_CLASS (gdata_documents_property_parent_class)->parse_json (parsable, reader, user_data, error);
}

static void
get_json (GDataParsable *parsable, JsonBuilder *builder)
{

	GDataDocumentsPropertyPrivate *priv = GDATA_DOCUMENTS_PROPERTY (parsable)->priv;

	/* Add all the Property specific JSON members */
	g_assert (priv->key != NULL);
	json_builder_set_member_name (builder, "key");
	json_builder_add_string_value (builder, priv->key);

	if (gdata_documents_property_get_etag (GDATA_DOCUMENTS_PROPERTY (parsable)) != NULL) {
		json_builder_set_member_name (builder, "etag");
		json_builder_add_string_value (builder, priv->etag);
	}

	/* Setting the "value" field of a Property Resource deletes that Property Resource */
	json_builder_set_member_name (builder, "value");
	json_builder_add_string_value (builder, priv->value);

	json_builder_set_member_name (builder, "visibility");
	json_builder_add_string_value (builder, priv->visibility);
}

static void
gdata_documents_property_comparable_init (GDataComparableIface *iface)
{
	iface->compare_with = compare_with;
}

static void
gdata_documents_property_init (GDataDocumentsProperty *self)
{
	self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, GDATA_TYPE_DOCUMENTS_PROPERTY, GDataDocumentsPropertyPrivate);

	/* Google Drive sets the default value of a Property Resource to be an empty string (""),
	 * and visibility is %GDATA_DOCUMENTS_PROPERTY_VISIBILITY_PRIVATE by default */
	self->priv->value = g_strdup ("");
	self->priv->visibility = g_strdup (GDATA_DOCUMENTS_PROPERTY_VISIBILITY_PRIVATE);
}

static void
gdata_documents_property_finalize (GObject *object)
{
	GDataDocumentsPropertyPrivate *priv = GDATA_DOCUMENTS_PROPERTY (object)->priv;

	g_free (priv->key);
	g_free (priv->etag);
	g_free (priv->value);
	g_free (priv->visibility);

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

static void
gdata_documents_property_get_property (GObject *object, guint property_id, GValue *value, GParamSpec *pspec)
{
	GDataDocumentsPropertyPrivate *priv = GDATA_DOCUMENTS_PROPERTY (object)->priv;

	switch (property_id) {
		case PROP_KEY:
			g_value_set_string (value, priv->key);
			break;
		case PROP_ETAG:
			g_value_set_string (value, priv->etag);
			break;
		case PROP_VALUE:
			g_value_set_string (value, priv->value);
			break;
		case PROP_VISIBILITY:
			g_value_set_string (value, priv->visibility);
			break;
		default:
			/* We don't have any other property... */
			G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
			break;
	}
}

static void
gdata_documents_property_set_property (GObject *object, guint property_id, const GValue *value, GParamSpec *pspec)
{
	GDataDocumentsProperty *self = GDATA_DOCUMENTS_PROPERTY (object);

	switch (property_id) {
		case PROP_KEY:
			_gdata_documents_property_set_key (self, g_value_get_string (value));
			break;
		case PROP_ETAG:
			_gdata_documents_property_set_etag (self, g_value_get_string (value));
			break;
		case PROP_VALUE:
			gdata_documents_property_set_value (self, g_value_get_string (value));
			break;
		case PROP_VISIBILITY:
			gdata_documents_property_set_visibility (self, g_value_get_string (value));
			break;
		default:
			/* We don't have any other property... */
			G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
			break;
	}
}

/**
 * gdata_documents_property_new:
 * @key: the property's key
 *
 * Creates a new #GDataEntry with the given ID and default properties.
 *
 * Return value: (transfer full): a new #GDataDocumentsProperty; unref with g_object_unref()
 *
 * Since: 0.17.11
 */
GDataDocumentsProperty *
gdata_documents_property_new (const gchar *key)
{
	/* GDataDocumentsProperty must have a non NULL key at initialization time,
	 * rest of the properties can be NULL or take their default values. */
	g_return_val_if_fail (key != NULL && key[0] != '\0', NULL);

	return GDATA_DOCUMENTS_PROPERTY (g_object_new (GDATA_TYPE_DOCUMENTS_PROPERTY, "key", key, NULL));
}

/**
 * gdata_documents_property_get_key:
 * @self: a #GDataDocumentsProperty
 *
 * Returns the key of the property. This will never be %NULL or an empty string ("").
 *
 * Return value: (transfer none): the property's key
 *
 * Since: 0.17.11
 */
const gchar *
gdata_documents_property_get_key (GDataDocumentsProperty *self)
{
	g_return_val_if_fail (GDATA_IS_DOCUMENTS_PROPERTY (self), NULL);
	return self->priv->key;
}

static void
_gdata_documents_property_set_key (GDataDocumentsProperty *self, const gchar *key)
{
	/* This is a READ-ONLY PROPERTY */
	g_return_if_fail (GDATA_IS_DOCUMENTS_PROPERTY (self));
	g_return_if_fail (key != NULL && *key != '\0');

	g_free (self->priv->key);
	self->priv->key = g_strdup (key);

	g_object_notify (G_OBJECT (self), "key");
}

/**
 * gdata_documents_property_get_etag:
 * @self: a #GDataDocumentsProperty
 *
 * Returns the ETag of the property.
 *
 * Return value: (transfer none): the property's ETag. The ETag will never be empty; it's either %NULL or a valid ETag.
 *
 * Since: 0.17.11
 */
const gchar *
gdata_documents_property_get_etag (GDataDocumentsProperty *self)
{
	g_return_val_if_fail (GDATA_IS_DOCUMENTS_PROPERTY (self), NULL);
	return self->priv->etag;
}

static void
_gdata_documents_property_set_etag (GDataDocumentsProperty *self, const gchar *etag)
{
	g_return_if_fail (GDATA_IS_DOCUMENTS_PROPERTY (self));

	if (g_strcmp0 (self->priv->etag, etag) != 0) {
		g_free (self->priv->etag);
		self->priv->etag = g_strdup (etag);
		g_object_notify (G_OBJECT (self), "etag");
	}
}

/**
 * gdata_documents_property_get_value:
 * @self: a #GDataDocumentsProperty
 *
 * Returns the value of the property.
 *
 * In the case that this value is %NULL, the Property Resource corresponding to @self will be deleted from the properties array on a file's metadata, whereas in the case that it's empty string (""), it will be set as it is.
 *
 * Return value: (nullable): the property's value. This can be %NULL or empty.
 *
 * Since: 0.17.11
 */
const gchar *
gdata_documents_property_get_value (GDataDocumentsProperty *self)
{
	g_return_val_if_fail (GDATA_IS_DOCUMENTS_PROPERTY (self), NULL);
	return self->priv->value;
}

/**
 * gdata_documents_property_set_value:
 * @self: a #GDataDocumentsProperty
 * @value: (allow-none): the new value of the property
 *
 * Sets #GDataDocumentsProperty:value to @value, corresponding to the key.
 *
 * In the case that @value is %NULL, the Property Resource corresponding to @self will be deleted from the properties array on a file's metadata, whereas in the case that it's empty string (""), it will be set as it is.
 *
 * Since: 0.17.11
 */
void
gdata_documents_property_set_value (GDataDocumentsProperty *self, const gchar *value)
{
	g_return_if_fail (GDATA_IS_DOCUMENTS_PROPERTY (self));

	if (g_strcmp0 (self->priv->value, value) != 0) {
		g_free (self->priv->value);
		self->priv->value = g_strdup (value);
		g_object_notify (G_OBJECT (self), "value");
	}
}

/**
 * gdata_documents_property_get_visibility:
 * @self: a #GDataDocumentsProperty
 *
 * Returns the visibility status of the property.
 *
 * Return value: %GDATA_DOCUMENTS_PROPERTY_VISIBILITY_PUBLIC if the #GDataDocumentsProperty is publicly visible to other
 * apps, %GDATA_DOCUMENTS_PROPERTY_VISIBILITY_PRIVATE if the #GDataDocumentsProperty is restricted to the application which
 * created it.
 *
 * Since: 0.17.11
 */
const gchar *
gdata_documents_property_get_visibility (GDataDocumentsProperty *self)
{
	g_return_val_if_fail (GDATA_IS_DOCUMENTS_PROPERTY (self), NULL);
	return self->priv->visibility;
}

/**
 * gdata_documents_property_set_visibility:
 * @self: a #GDataDocumentsProperty
 * @visibility: the new visibility status of the property
 *
 * Sets #GDataDocumentsProperty:visibility to %GDATA_DOCUMENTS_PROPERTY_VISIBILITY_PUBLIC for
 * public properties and %GDATA_DOCUMENTS_PROPERTY_VISIBILITY_PRIVATE for
 * private properties (default).
 *
 * Since: 0.17.11
 */
void
gdata_documents_property_set_visibility (GDataDocumentsProperty *self, const gchar *visibility)
{
	g_return_if_fail (GDATA_IS_DOCUMENTS_PROPERTY (self));
	g_return_if_fail (g_strcmp0 (visibility, GDATA_DOCUMENTS_PROPERTY_VISIBILITY_PUBLIC) == 0 ||
			  g_strcmp0 (visibility, GDATA_DOCUMENTS_PROPERTY_VISIBILITY_PRIVATE) == 0);

	if (g_strcmp0 (self->priv->visibility, visibility) != 0) {
		g_free (self->priv->visibility);
		self->priv->visibility = g_strdup (visibility);
		g_object_notify (G_OBJECT (self), "visibility");
	}
}