1
/*
2
 * rtm-glib-example.c: Example
3
 *
4
 * Copyright (C) 2009 Manuel Rego Casasnovas <mrego@igalia.com>
5
 *
6
 * This library is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2.1 of the License, or (at your option) any later version.
10
 *
11
 * This library is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with this library; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
19
 */
20
21
#include <stdio.h>
22
#include <rtm-glib/rtm-glib.h>
23
#include <rtm-glib/rtm-task.h>
24
#include <rtm-glib/rtm-error.h>
25
#include <rtm-glib/rtm-list.h>
26
#include <rtm-glib/rtm-location.h>
27
#include <rtm-glib/rtm-time-zone.h>
28
#include <rtm-glib/rtm-contact.h>
29
30
31
#define RTM_API_KEY "de4eacaea750baff02b70e6ec57fcf22"
32
#define RTM_SHARED_SECRET "6aedd71e2f4f9c94"
33
34
35
gint
36
main (gint argc, gchar **argv)
37
{
38
        RtmGlib *rtm;
39
        gchar *frob;
40
        gchar *url;
41
        gchar *auth_token;
42
        gchar *username;
43
        GList *glist;
44
        GList *item;
45
        RtmTask *task;
46
        GError *error = NULL;
47
        RtmList *rtm_list;
48
        gchar *timeline;
49
        gchar *transaction_id;
50
        RtmLocation *location;
51
        gchar *list_id_sent = NULL;
52
        RtmTimeZone *time_zone;
53
        gchar *time;
54
        RtmContact *contact;
55
56
        g_thread_init (NULL);
57
        g_type_init();
58
59
        rtm = rtm_glib_new (RTM_API_KEY, RTM_SHARED_SECRET);
60
61
        if (rtm_glib_test_echo (rtm, &error)) {
62
                g_print ("Test echo OK!\n");
63
        } else {
64
                g_print ("Test echo FAIL!\n");
65
        }
66
        if (error != NULL) {
67
                g_error ("%s", rtm_error_get_message (error));
68
        }
69
70
        glist = rtm_glib_time_zones_get_list (rtm, &error);
71
        if (error != NULL) {
72
                g_error ("%s", rtm_error_get_message (error));
73
        }
74
        for (item = glist; item; item = g_list_next (item)) {
75
                time_zone = (RtmTimeZone *) item->data;
76
                g_print ("%s", rtm_time_zone_to_string (time_zone));
77
        }
78
        g_list_free (glist);
79
80
        time = rtm_glib_time_parse (rtm, "02/10/2009 10:25", NULL, FALSE, &error);
81
        if (error != NULL) {
82
                g_error ("%s", rtm_error_get_message (error));
83
        }
84
        g_print ("Time: %s\n", time);
85
        g_free (time);
86
87
        time = rtm_glib_time_convert (rtm, "Europe/Madrid", NULL, NULL, &error);
88
        if (error != NULL) {
89
                g_error ("%s", rtm_error_get_message (error));
90
        }
91
        g_print ("Time: %s\n", time);
92
        g_free (time);
93
94
        frob = rtm_glib_auth_get_frob (rtm, &error);
95
        if (error != NULL) {
96
                g_error ("%s", rtm_error_get_message (error));
97
        }
98
        g_print ("Frob: %s\n", frob);
99
100
        url = rtm_glib_auth_get_login_url (rtm, frob, "delete");
101
        g_print ("URL: %s\n", url);
102
103
        getchar ();
104
105
        auth_token = rtm_glib_auth_get_token (rtm, frob, &error);
106
        if (error != NULL) {
107
                g_error ("%s", rtm_error_get_message (error));
108
        }
109
        g_print ("auth_token: %s\n", auth_token);
110
111
        if (rtm_glib_auth_check_token (rtm, auth_token, NULL)) {
112
                g_print ("auth_token valid!\n");
113
        } else {
114
                g_print ("auth_token not valid!\n");
115
        }
116
        if (error != NULL) {
117
                g_error ("%s", rtm_error_get_message (error));
118
        }
119
120
        username = rtm_glib_test_login (rtm, auth_token, &error);
121
122
        g_free (auth_token);
123
124
        if (error != NULL) {
125
                g_error ("%s", rtm_error_get_message (error));
126
        }
127
        g_print ("username: %s\n", username);
128
129
        glist = rtm_glib_tasks_get_list (rtm, NULL, NULL, NULL, &error);
130
        if (error != NULL) {
131
                g_error ("%s", rtm_error_get_message (error));
132
        }
133
        for (item = glist; item; item = g_list_next (item)) {
134
                task = (RtmTask *) item->data;
135
                g_print ("%s", rtm_task_to_string (task));
136
        }
137
        g_list_free (glist);
138
139
        glist = rtm_glib_lists_get_list (rtm, &error);
140
        if (error != NULL) {
141
                g_error ("%s", rtm_error_get_message (error));
142
        }
143
        for (item = glist; item; item = g_list_next (item)) {
144
                rtm_list = (RtmList *) item->data;
145
                if (g_strcmp0 (rtm_list_get_name (rtm_list), "Sent") == 0) {
146
                        list_id_sent = rtm_list_get_id (rtm_list);
147
                }
148
                g_print ("%s", rtm_list_to_string (rtm_list));
149
        }
150
        g_list_free (glist);
151
152
        timeline = rtm_glib_timelines_create (rtm, &error);
153
        if (error != NULL) {
154
                g_error ("%s", rtm_error_get_message (error));
155
        }
156
        g_print ("timeline: %s", timeline);
157
158
        task = rtm_glib_tasks_add (rtm, timeline, "test-rtm-glib", NULL, FALSE, &error);
159
        if (error != NULL) {
160
                g_error ("%s", rtm_error_get_message (error));
161
        }
162
        if (task != NULL) {
163
                g_print ("First task added! task_id: %s\n", rtm_task_get_id (task));
164
        } else {
165
                g_print ("First task NOT added!\n");
166
        }
167
168
        transaction_id = rtm_glib_tasks_delete (rtm, timeline, task, &error);
169
        if (error != NULL) {
170
                g_error ("%s", rtm_error_get_message (error));
171
        }
172
        if (transaction_id != NULL) {
173
                g_print ("Task deleted! transaction_id: %s\n", transaction_id);
174
        } else {
175
                g_print ("Task NOT deleted!\n");
176
        }
177
178
        if (rtm_glib_transactions_undo (rtm, timeline, transaction_id, &error)) {
179
                g_print ("Undo!\n");
180
        } else {
181
                g_print ("NOT undo!\n");
182
        }
183
        if (error != NULL) {
184
                g_error ("%s", rtm_error_get_message (error));
185
        }
186
187
        transaction_id = rtm_glib_tasks_set_name (rtm, timeline, task, "test-rtm-glib-renamed", &error);
188
        if (error != NULL) {
189
                g_error ("%s", rtm_error_get_message (error));
190
        }
191
        if (transaction_id != NULL) {
192
                g_print ("Task renamed! transaction_id: %s\n", transaction_id);
193
        } else {
194
                g_print ("Task NOT renamed!\n");
195
        }
196
197
        transaction_id = rtm_glib_tasks_set_url (rtm, timeline, task, "http://http://gitorious.org/rtm-glib/", &error);
198
        if (error != NULL) {
199
                g_error ("%s", rtm_error_get_message (error));
200
        }
201
        if (transaction_id != NULL) {
202
                g_print ("Task URL set! transaction_id: %s\n", transaction_id);
203
        } else {
204
                g_print ("Task URL NOT set!\n");
205
        }
206
207
        transaction_id = rtm_glib_tasks_set_priority (rtm, timeline, task, "2", &error);
208
        if (error != NULL) {
209
                g_error ("%s", rtm_error_get_message (error));
210
        }
211
        if (transaction_id != NULL) {
212
                g_print ("Task priority set! transaction_id: %s\n", transaction_id);
213
        } else {
214
                g_print ("Task priority NOT set!\n");
215
        }
216
217
        transaction_id = rtm_glib_tasks_move_priority (rtm, timeline, task, "down", &error);
218
        if (error != NULL) {
219
                g_error ("%s", rtm_error_get_message (error));
220
        }
221
        if (transaction_id != NULL) {
222
                g_print ("Task priority moved! transaction_id: %s\n", transaction_id);
223
        } else {
224
                g_print ("Task priority NOT moved!\n");
225
        }
226
227
        transaction_id = rtm_glib_tasks_complete (rtm, timeline, task, &error);
228
        if (error != NULL) {
229
                g_error ("%s", rtm_error_get_message (error));
230
        }
231
        if (transaction_id != NULL) {
232
                g_print ("Task completed! transaction_id: %s\n", transaction_id);
233
        } else {
234
                g_print ("Task NOT completed!\n");
235
        }
236
237
        transaction_id = rtm_glib_tasks_uncomplete (rtm, timeline, task, &error);
238
        if (error != NULL) {
239
                g_error ("%s", rtm_error_get_message (error));
240
        }
241
        if (transaction_id != NULL) {
242
                g_print ("Task uncompleted! transaction_id: %s\n", transaction_id);
243
        } else {
244
                g_print ("Task NOT uncompleted!\n");
245
        }
246
247
        transaction_id = rtm_glib_tasks_postpone (rtm, timeline, task, &error);
248
        if (error != NULL) {
249
                g_error ("%s", rtm_error_get_message (error));
250
        }
251
        if (transaction_id != NULL) {
252
                g_print ("Task postponed! transaction_id: %s\n", transaction_id);
253
        } else {
254
                g_print ("Task NOT postponed!\n");
255
        }
256
257
        transaction_id = rtm_glib_tasks_set_recurrence (rtm, timeline, task, "every week", &error);
258
        if (error != NULL) {
259
                g_error ("%s", rtm_error_get_message (error));
260
        }
261
        if (transaction_id != NULL) {
262
                g_print ("Task recurrence set! transaction_id: %s\n", transaction_id);
263
        } else {
264
                g_print ("Task recurrence NOT set!\n");
265
        }
266
267
        transaction_id = rtm_glib_tasks_set_estimate (rtm, timeline, task, "5 hours", &error);
268
        if (error != NULL) {
269
                g_error ("%s", rtm_error_get_message (error));
270
        }
271
        if (transaction_id != NULL) {
272
                g_print ("Task estimate set! transaction_id: %s\n", transaction_id);
273
        } else {
274
                g_print ("Task estimate NOT set!\n");
275
        }
276
277
        transaction_id = rtm_glib_tasks_set_due_date (rtm, timeline, task, "25/10/2009", FALSE, TRUE, &error);
278
        if (error != NULL) {
279
                g_error ("%s", rtm_error_get_message (error));
280
        }
281
        if (transaction_id != NULL) {
282
                g_print ("Task due date set! transaction_id: %s\n", transaction_id);
283
        } else {
284
                g_print ("Task due date NOT set!\n");
285
        }
286
287
        glist = rtm_glib_locations_get_list (rtm, &error);
288
        if (error != NULL) {
289
                g_error ("%s", rtm_error_get_message (error));
290
        }
291
        for (item = glist; item; item = g_list_next (item)) {
292
                location = (RtmLocation *) item->data;
293
                g_print ("%s", rtm_location_to_string (location));
294
        }
295
296
        item = g_list_first (glist);
297
        if (item != NULL) {
298
                location = (RtmLocation *) item->data;
299
                transaction_id = rtm_glib_tasks_set_location (rtm, timeline, task, rtm_location_get_id(location), &error);
300
                if (error != NULL) {
301
                        g_error ("%s", rtm_error_get_message (error));
302
                }
303
                if (transaction_id != NULL) {
304
                        g_print ("Task location set! transaction_id: %s\n", transaction_id);
305
                } else {
306
                        g_print ("Task location NOT set!\n");
307
                }
308
                g_object_unref (location);
309
        }
310
311
        g_list_free (glist);
312
313
        transaction_id = rtm_glib_tasks_set_tags (rtm, timeline, task, "rtm,glib", &error);
314
        if (error != NULL) {
315
                g_error ("%s", rtm_error_get_message (error));
316
        }
317
        if (transaction_id != NULL) {
318
                g_print ("Task tags set! transaction_id: %s\n", transaction_id);
319
        } else {
320
                g_print ("Task tags NOT set!\n");
321
        }
322
323
        transaction_id = rtm_glib_tasks_add_tags (rtm, timeline, task, "gnome,library", &error);
324
        if (error != NULL) {
325
                g_error ("%s", rtm_error_get_message (error));
326
        }
327
        if (transaction_id != NULL) {
328
                g_print ("Task tags added! transaction_id: %s\n", transaction_id);
329
        } else {
330
                g_print ("Task tags NOT added!\n");
331
        }
332
333
        transaction_id = rtm_glib_tasks_remove_tags (rtm, timeline, task, "glib,gnome", &error);
334
        if (error != NULL) {
335
                g_error ("%s", rtm_error_get_message (error));
336
        }
337
        if (transaction_id != NULL) {
338
                g_print ("Task tags removed! transaction_id: %s\n", transaction_id);
339
        } else {
340
                g_print ("Task tags NOT removed!\n");
341
        }
342
343
        if (list_id_sent) {
344
                transaction_id = rtm_glib_tasks_move_to (rtm, timeline, task, list_id_sent, &error);
345
                if (error != NULL) {
346
                        g_error ("%s", rtm_error_get_message (error));
347
                }
348
                if (transaction_id != NULL) {
349
                        g_print ("Task moved! transaction_id: %s\n", transaction_id);
350
                } else {
351
                        g_print ("Task NOT moved!\n");
352
                }
353
                g_free (list_id_sent);
354
        }
355
356
        task = rtm_glib_tasks_add (rtm, timeline, "test-rtm-glib2", NULL, FALSE, &error);
357
        if (error != NULL) {
358
                g_error ("%s", rtm_error_get_message (error));
359
        }
360
        if (task != NULL) {
361
                g_print ("Second task added! task_id: %s\n", rtm_task_get_id (task));
362
        } else {
363
                g_print ("Second task NOT added!\n");
364
        }
365
366
        transaction_id = rtm_glib_tasks_delete (rtm, timeline, task, &error);
367
        if (error != NULL) {
368
                g_error ("%s", rtm_error_get_message (error));
369
        }
370
        if (transaction_id != NULL) {
371
                g_print ("Task deleted! transaction_id: %s\n", transaction_id);
372
        } else {
373
                g_print ("Task NOT deleted!\n");
374
        }
375
376
        rtm_list = rtm_glib_lists_add (rtm, timeline, "test-list-glib", NULL, &error);
377
        if (error != NULL) {
378
                g_error ("%s", rtm_error_get_message (error));
379
        }
380
        if (task != NULL) {
381
                g_print ("List added! list_id: %s\n", rtm_list_get_id (rtm_list));
382
        } else {
383
                g_print ("List NOT added!\n");
384
        }
385
386
        transaction_id = rtm_glib_lists_set_name (rtm, timeline, rtm_list, "test-rtm-glib-renamed", &error);
387
        if (error != NULL) {
388
                g_error ("%s", rtm_error_get_message (error));
389
        }
390
        if (transaction_id != NULL) {
391
                g_print ("List renamed! transaction_id: %s\n", transaction_id);
392
        } else {
393
                g_print ("List NOT renamed!\n");
394
        }
395
396
        transaction_id = rtm_glib_lists_archive (rtm, timeline, rtm_list, &error);
397
        if (error != NULL) {
398
                g_error ("%s", rtm_error_get_message (error));
399
        }
400
        if (transaction_id != NULL) {
401
                g_print ("List archived! transaction_id: %s\n", transaction_id);
402
        } else {
403
                g_print ("List NOT archived!\n");
404
        }
405
406
        transaction_id = rtm_glib_lists_unarchive (rtm, timeline, rtm_list, &error);
407
        if (error != NULL) {
408
                g_error ("%s", rtm_error_get_message (error));
409
        }
410
        if (transaction_id != NULL) {
411
                g_print ("List unarchived! transaction_id: %s\n", transaction_id);
412
        } else {
413
                g_print ("List NOT unarchived!\n");
414
        }
415
416
        if (rtm_glib_lists_set_default_list (rtm, timeline, rtm_list, &error)) {
417
                g_print ("List \"%s\" set as default!\n", rtm_list_get_id (rtm_list));
418
        } else {
419
                g_print ("List \"%s\" NOT set as default!\n", rtm_list_get_id (rtm_list));
420
        }
421
        if (error != NULL) {
422
                g_error ("%s", rtm_error_get_message (error));
423
        }
424
425
        transaction_id = rtm_glib_lists_delete (rtm, timeline, rtm_list, &error);
426
        if (error != NULL) {
427
                g_error ("%s", rtm_error_get_message (error));
428
        }
429
        if (transaction_id != NULL) {
430
                g_print ("List deleted! transaction_id: %s\n", transaction_id);
431
        } else {
432
                g_print ("List NOT deleted!\n");
433
        }
434
435
        glist = rtm_glib_contacts_get_list (rtm, &error);
436
        if (error != NULL) {
437
                g_error ("%s", rtm_error_get_message (error));
438
        }
439
        for (item = glist; item; item = g_list_next (item)) {
440
                contact = (RtmContact *) item->data;
441
                g_print ("%s", rtm_contact_to_string (contact));
442
        }
443
        g_list_free (glist);
444
445
        contact = rtm_glib_contacts_add (rtm, timeline, "rtmglib", &error);
446
        if (error != NULL) {
447
                g_error ("%s", rtm_error_get_message (error));
448
        }
449
        if (task != NULL) {
450
                g_print ("Contact added! contact_id: %s\n", rtm_contact_get_id (contact));
451
        } else {
452
                g_print ("Contact NOT added!\n");
453
        }
454
455
        transaction_id = rtm_glib_contacts_delete (rtm, timeline, contact, &error);
456
        if (error != NULL) {
457
                g_error ("%s", rtm_error_get_message (error));
458
        }
459
        if (transaction_id != NULL) {
460
                g_print ("Contact deleted! transaction_id: %s\n", transaction_id);
461
        } else {
462
                g_print ("Contact NOT deleted!\n");
463
        }
464
465
        g_free (frob);
466
        g_free (url);
467
        g_free (username);
468
        g_free (timeline);
469
        g_free (transaction_id);
470
471
        g_object_unref (task);
472
        g_object_unref (rtm_list);
473
        g_object_unref (contact);
474
        g_object_unref (rtm);
475
476
        return 0;
477
}