summaryrefslogtreecommitdiff
path: root/gdata/services/contacts/gdata-contacts-query.c
blob: 23f40bbf07d4027dd2b82a605313f1a7b37b2d3a (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
473
474
475
476
477
478
479
480
/* -*- 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-contacts-query
 * @short_description: GData Contacts query object
 * @stability: Stable
 * @include: gdata/services/contacts/gdata-contacts-query.h
 *
 * #GDataContactsQuery represents a collection of query parameters specific to the Google Contacts service, which go above and beyond
 * those catered for by #GDataQuery.
 *
 * For more information on the custom GData query parameters supported by #GDataContactsQuery, see the <ulink type="http"
 * url="http://code.google.com/apis/contacts/docs/2.0/reference.html#Parameters">online documentation</ulink>.
 *
 * <example>
 * 	<title>Querying for Contacts</title>
 * 	<programlisting>
 *	GDataContactsService *service;
 *	gchar *group_id;
 *	GDataContactsQuery *query;
 *	GDataFeed *feed;
 *	GList *i;
 *	GError *error = NULL;
 *
 *	/<!-- -->* Create a service and return the group we're querying within. *<!-- -->/
 *	service = create_contacts_service ();
 *	group_id = query_user_for_group (service);
 *
 *	/<!-- -->* Create the query to use. We're going to query for contacts which match the search term "John" within a given group,
 *	 * including deleted contacts. The group is specified by its ID. *<!-- -->/
 *	query = gdata_contacts_query_new ("John");
 *	gdata_contacts_query_set_show_deleted (query, TRUE);
 *	gdata_contacts_query_set_group (query, group_id);
 *
 *	g_free (group_id);
 *
 *	/<!-- -->* Execute the query *<!-- -->/
 *	feed = gdata_contacts_service_query_contacts (service, query, NULL, NULL, NULL, &error);
 *
 *	g_object_unref (query);
 *	g_object_unref (service);
 *
 *	if (error != NULL) {
 *		g_error ("Error querying for contacts: %s", error->message);
 *		g_error_free (error);
 *		return;
 *	}
 *
 *	/<!-- -->* Iterate through the returned contacts and do something with them *<!-- -->/
 *	for (i = gdata_feed_get_entries (feed); i != NULL; i = i->next) {
 *		GDataContactsContact *contact = GDATA_CONTACTS_CONTACT (i->data);
 *
 *		/<!-- -->* Do something with the contact here, such as insert it into a UI *<!-- -->/
 *	}
 *
 *	g_object_unref (feed);
 * 	</programlisting>
 * </example>
 *
 * Since: 0.2.0
 */

#include <config.h>
#include <glib.h>
#include <glib/gi18n-lib.h>
#include <string.h>

#include "gdata-contacts-query.h"
#include "gdata-query.h"

static void gdata_contacts_query_finalize (GObject *object);
static void gdata_contacts_query_get_property (GObject *object, guint property_id, GValue *value, GParamSpec *pspec);
static void gdata_contacts_query_set_property (GObject *object, guint property_id, const GValue *value, GParamSpec *pspec);
static void get_query_uri (GDataQuery *self, const gchar *feed_uri, GString *query_uri, gboolean *params_started);

struct _GDataContactsQueryPrivate {
	gchar *order_by; /* TODO: #defined values */
	gboolean show_deleted;
	gchar *sort_order; /* TODO: #defined values */
	gchar *group;
};

enum {
	PROP_ORDER_BY = 1,
	PROP_SHOW_DELETED,
	PROP_SORT_ORDER,
	PROP_GROUP
};

G_DEFINE_TYPE (GDataContactsQuery, gdata_contacts_query, GDATA_TYPE_QUERY)

static void
gdata_contacts_query_class_init (GDataContactsQueryClass *klass)
{
	GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
	GDataQueryClass *query_class = GDATA_QUERY_CLASS (klass);

	g_type_class_add_private (klass, sizeof (GDataContactsQueryPrivate));

	gobject_class->set_property = gdata_contacts_query_set_property;
	gobject_class->get_property = gdata_contacts_query_get_property;
	gobject_class->finalize = gdata_contacts_query_finalize;

	query_class->get_query_uri = get_query_uri;

	/**
	 * GDataContactsQuery:order-by:
	 *
	 * Sorting criterion. The only supported value is <literal>lastmodified</literal>.
	 *
	 * Since: 0.2.0
	 */
	g_object_class_install_property (gobject_class, PROP_ORDER_BY,
	                                 g_param_spec_string ("order-by",
	                                                      "Order by", "Sorting criterion.",
	                                                      NULL,
	                                                      G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));

	/**
	 * GDataContactsQuery:show-deleted:
	 *
	 * Whether to include deleted contacts in the query feed. Deleted contacts return %TRUE
	 * from gdata_contacts_contact_is_deleted(), and have no other information. They do not
	 * normally appear in query results.
	 *
	 * Since: 0.2.0
	 */
	g_object_class_install_property (gobject_class, PROP_SHOW_DELETED,
	                                 g_param_spec_boolean ("show-deleted",
	                                                       "Show deleted?", "Whether to include deleted contacts in the query feed.",
	                                                       FALSE,
	                                                       G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));

	/**
	 * GDataContactsQuery:sort-order:
	 *
	 * Sorting order direction. Can be either <literal>ascending</literal> or <literal>descending</literal>.
	 *
	 * Since: 0.2.0
	 */
	g_object_class_install_property (gobject_class, PROP_SORT_ORDER,
	                                 g_param_spec_string ("sort-order",
	                                                      "Sort order", "Sorting order direction.",
	                                                      NULL,
	                                                      G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));

	/**
	 * GDataContactsQuery:group:
	 *
	 * Constrains the results to only those belonging to the group specified. The value of this parameter
	 * should be a group ID URI.
	 *
	 * Since: 0.2.0
	 */
	g_object_class_install_property (gobject_class, PROP_GROUP,
	                                 g_param_spec_string ("group",
	                                                      "Group", "Constrains the results to only those belonging to the group specified.",
	                                                      NULL,
	                                                      G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
}

static void
gdata_contacts_query_init (GDataContactsQuery *self)
{
	self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, GDATA_TYPE_CONTACTS_QUERY, GDataContactsQueryPrivate);
}

static void
gdata_contacts_query_finalize (GObject *object)
{
	GDataContactsQueryPrivate *priv = GDATA_CONTACTS_QUERY (object)->priv;

	g_free (priv->order_by);
	g_free (priv->sort_order);
	g_free (priv->group);

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

static void
gdata_contacts_query_get_property (GObject *object, guint property_id, GValue *value, GParamSpec *pspec)
{
	GDataContactsQueryPrivate *priv = GDATA_CONTACTS_QUERY (object)->priv;

	switch (property_id) {
		case PROP_ORDER_BY:
			g_value_set_string (value, priv->order_by);
			break;
		case PROP_SHOW_DELETED:
			g_value_set_boolean (value, priv->show_deleted);
			break;
		case PROP_SORT_ORDER:
			g_value_set_string (value, priv->sort_order);
			break;
		case PROP_GROUP:
			g_value_set_string (value, priv->group);
			break;
		default:
			/* We don't have any other property... */
			G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
			break;
	}
}

static void
gdata_contacts_query_set_property (GObject *object, guint property_id, const GValue *value, GParamSpec *pspec)
{
	GDataContactsQuery *self = GDATA_CONTACTS_QUERY (object);

	switch (property_id) {
		case PROP_ORDER_BY:
			gdata_contacts_query_set_order_by (self, g_value_get_string (value));
			break;
		case PROP_SHOW_DELETED:
			gdata_contacts_query_set_show_deleted (self, g_value_get_boolean (value));
			break;
		case PROP_SORT_ORDER:
			gdata_contacts_query_set_sort_order (self, g_value_get_string (value));
			break;
		case PROP_GROUP:
			gdata_contacts_query_set_group (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;
	}
}

static void
get_query_uri (GDataQuery *self, const gchar *feed_uri, GString *query_uri, gboolean *params_started)
{
	GDataContactsQueryPrivate *priv = GDATA_CONTACTS_QUERY (self)->priv;

	#define APPEND_SEP g_string_append_c (query_uri, (*params_started == FALSE) ? '?' : '&'); *params_started = TRUE;

	/* Chain up to the parent class */
	GDATA_QUERY_CLASS (gdata_contacts_query_parent_class)->get_query_uri (self, feed_uri, query_uri, params_started);

	if (priv->order_by != NULL) {
		APPEND_SEP
		g_string_append (query_uri, "orderby=");
		g_string_append_uri_escaped (query_uri, priv->order_by, NULL, FALSE);
	}

	APPEND_SEP
	if (priv->show_deleted == TRUE)
		g_string_append (query_uri, "showdeleted=true");
	else
		g_string_append (query_uri, "showdeleted=false");

	if (priv->sort_order != NULL) {
		APPEND_SEP
		g_string_append (query_uri, "sortorder=");
		g_string_append_uri_escaped (query_uri, priv->sort_order, NULL, FALSE);
	}

	if (priv->group != NULL) {
		APPEND_SEP
		g_string_append (query_uri, "group=");
		g_string_append_uri_escaped (query_uri, priv->group, NULL, FALSE);
	}
}

/**
 * gdata_contacts_query_new:
 * @q: (allow-none): a query string, or %NULL
 *
 * Creates a new #GDataContactsQuery with its #GDataQuery:q property set to @q.
 *
 * Return value: a new #GDataContactsQuery
 *
 * Since: 0.2.0
 */
GDataContactsQuery *
gdata_contacts_query_new (const gchar *q)
{
	return g_object_new (GDATA_TYPE_CONTACTS_QUERY, "q", q, NULL);
}

/**
 * gdata_contacts_query_new_with_limits:
 * @q: (allow-none): a query string, or %NULL
 * @start_index: a one-based start index for the results, or <code class="literal">0</code>
 * @max_results: the maximum number of results to return, or <code class="literal">0</code>
 *
 * Creates a new #GDataContactsQuery with its #GDataQuery:q property set to @q, and the limits @start_index and @max_results
 * applied.
 *
 * Return value: a new #GDataContactsQuery
 *
 * Since: 0.2.0
 */
GDataContactsQuery *
gdata_contacts_query_new_with_limits (const gchar *q, guint start_index, guint max_results)
{
	return g_object_new (GDATA_TYPE_CONTACTS_QUERY,
	                     "q", q,
	                     "start-index", start_index,
	                     "max-results", max_results,
	                     NULL);
}

/**
 * gdata_contacts_query_get_order_by:
 * @self: a #GDataContactsQuery
 *
 * Gets the #GDataContactsQuery:order-by property.
 *
 * Return value: the order by property, or %NULL if it is unset
 *
 * Since: 0.2.0
 */
const gchar *
gdata_contacts_query_get_order_by (GDataContactsQuery *self)
{
	g_return_val_if_fail (GDATA_IS_CONTACTS_QUERY (self), NULL);
	return self->priv->order_by;
}

/**
 * gdata_contacts_query_set_order_by:
 * @self: a #GDataContactsQuery
 * @order_by: (allow-none): a new order by string, or %NULL
 *
 * Sets the #GDataContactsQuery:order-by property of the #GDataContactsQuery to the new order by string, @order_by.
 *
 * Set @order_by to %NULL to unset the property in the query URI.
 *
 * Since: 0.2.0
 */
void
gdata_contacts_query_set_order_by (GDataContactsQuery *self, const gchar *order_by)
{
	g_return_if_fail (GDATA_IS_CONTACTS_QUERY (self));

	g_free (self->priv->order_by);
	self->priv->order_by = g_strdup (order_by);
	g_object_notify (G_OBJECT (self), "order-by");

	/* Our current ETag will no longer be relevant */
	gdata_query_set_etag (GDATA_QUERY (self), NULL);
}

/**
 * gdata_contacts_query_show_deleted:
 * @self: a #GDataContactsQuery
 *
 * Gets the #GDataContactsQuery:show-deleted property.
 *
 * Return value: %TRUE if deleted contacts should be shown, %FALSE otherwise
 *
 * Since: 0.2.0
 */
gboolean
gdata_contacts_query_show_deleted (GDataContactsQuery *self)
{
	g_return_val_if_fail (GDATA_IS_CONTACTS_QUERY (self), FALSE);
	return self->priv->show_deleted;
}

/**
 * gdata_contacts_query_set_show_deleted:
 * @self: a #GDataContactsQuery
 * @show_deleted: %TRUE to show deleted contacts, %FALSE otherwise
 *
 * Sets the #GDataContactsQuery:show-deleted property of the #GDataContactsQuery.
 *
 * Since: 0.2.0
 */
void
gdata_contacts_query_set_show_deleted (GDataContactsQuery *self, gboolean show_deleted)
{
	g_return_if_fail (GDATA_IS_CONTACTS_QUERY (self));
	self->priv->show_deleted = show_deleted;
	g_object_notify (G_OBJECT (self), "show-deleted");

	/* Our current ETag will no longer be relevant */
	gdata_query_set_etag (GDATA_QUERY (self), NULL);
}

/**
 * gdata_contacts_query_get_sort_order:
 * @self: a #GDataContactsQuery
 *
 * Gets the #GDataContactsQuery:sort-order property.
 *
 * Return value: the sort order property, or %NULL if it is unset
 *
 * Since: 0.2.0
 */
const gchar *
gdata_contacts_query_get_sort_order (GDataContactsQuery *self)
{
	g_return_val_if_fail (GDATA_IS_CONTACTS_QUERY (self), NULL);
	return self->priv->sort_order;
}

/**
 * gdata_contacts_query_set_sort_order:
 * @self: a #GDataContactsQuery
 * @sort_order: (allow-none): a new sort order string, or %NULL
 *
 * Sets the #GDataContactsQuery:sort-order property of the #GDataContactsQuery to the new sort order string, @sort_order.
 *
 * Set @sort_order to %NULL to unset the property in the query URI.
 *
 * Since: 0.2.0
 */
void
gdata_contacts_query_set_sort_order (GDataContactsQuery *self, const gchar *sort_order)
{
	g_return_if_fail (GDATA_IS_CONTACTS_QUERY (self));

	g_free (self->priv->sort_order);
	self->priv->sort_order = g_strdup (sort_order);
	g_object_notify (G_OBJECT (self), "sort-order");

	/* Our current ETag will no longer be relevant */
	gdata_query_set_etag (GDATA_QUERY (self), NULL);
}

/**
 * gdata_contacts_query_get_group:
 * @self: a #GDataContactsQuery
 *
 * Gets the #GDataContactsQuery:group property.
 *
 * Return value: the group property, or %NULL if it is unset
 *
 * Since: 0.2.0
 */
const gchar *
gdata_contacts_query_get_group (GDataContactsQuery *self)
{
	g_return_val_if_fail (GDATA_IS_CONTACTS_QUERY (self), NULL);
	return self->priv->group;
}

/**
 * gdata_contacts_query_set_group:
 * @self: a #GDataContactsQuery
 * @group: (allow-none): a new group ID URI, or %NULL
 *
 * Sets the #GDataContactsQuery:group property of the #GDataContactsQuery to the new group ID URI, @group.
 *
 * Set @group to %NULL to unset the property in the query URI.
 *
 * Since: 0.2.0
 */
void
gdata_contacts_query_set_group (GDataContactsQuery *self, const gchar *group)
{
	g_return_if_fail (GDATA_IS_CONTACTS_QUERY (self));

	g_free (self->priv->group);
	self->priv->group = g_strdup (group);
	g_object_notify (G_OBJECT (self), "group");

	/* Our current ETag will no longer be relevant */
	gdata_query_set_etag (GDATA_QUERY (self), NULL);
}