1 /*
2  * example.c -- Random example to test the flicksoup files
3  *
4  * Copyright (C) 2010-2015 Mario Sanchez Prada
5  * Authors: Mario Sanchez Prada <msanchez@gnome.org>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of version 3 of the GNU General Public
9  * License as published by the Free Software Foundation.
10  *
11  * This program 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  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see <http://www.gnu.org/licenses/>
18  *
19  */
20 
21 #include <stdio.h>
22 #include <glib.h>
23 #include <errno.h>
24 #include <gcrypt.h>
25 #include <pthread.h>
26 
27 #include <config.h>
28 #include <flicksoup/flicksoup.h>
29 #include <libsoup/soup.h>
30 
31 #if GCRYPT_VERSION_NUMBER < 0x010600
32 GCRY_THREAD_OPTION_PTHREAD_IMPL;
33 #endif
34 
35 #define API_KEY "18861766601de84f0921ce6be729f925"
36 #define SHARED_SECRET "6233fbefd85f733a"
37 
38 #define TEST_PHOTO "testphoto.png"
39 
40 static gchar *uploaded_photo_id = NULL;
41 static gchar *created_photoset_id = NULL;
42 static gchar *first_group_id = NULL;
43 static gchar *test_photo_path = NULL;
44 
45 /* Prototypes */
46 
47 static gchar *encode_uri (const gchar *uri);
48 void upload_cb (GObject *object, GAsyncResult *res, gpointer source_func);
49 void added_to_group_cb (GObject *object, GAsyncResult *res, gpointer unused);
50 void get_groups_cb (GObject *object, GAsyncResult *res, gpointer unused);
51 void added_to_photoset_cb (GObject *object, GAsyncResult *res, gpointer unused);
52 void photoset_created_cb (GObject *object, GAsyncResult *res, gpointer unused);
53 void get_photosets_cb (GObject *object, GAsyncResult *res, gpointer unused);
54 void photo_get_info_cb (GObject *object, GAsyncResult *res, gpointer unused);
55 void set_date_posted_cb (GObject *object, GAsyncResult *res, gpointer user_data);
56 void get_location_cb (GObject *object, GAsyncResult *res, gpointer unused);
57 void set_location_cb (GObject *object, GAsyncResult *res, gpointer unused);
58 void set_license_cb (GObject *object, GAsyncResult *res, gpointer unused);
59 void get_tags_list_cb (GObject *object, GAsyncResult *res, gpointer unused);
60 void get_upload_status_cb (GObject *object, GAsyncResult *res, gpointer unused);
61 void check_auth_info_cb (GObject *object, GAsyncResult *res, gpointer unused);
62 void complete_auth_cb (GObject *object, GAsyncResult *res, gpointer unused);
63 void get_auth_url_cb (GObject *object, GAsyncResult *res, gpointer unused);
64 gboolean do_work (gpointer unused);
65 
66 /* Implementations */
67 
68 static gchar *
encode_uri(const gchar * uri)69 encode_uri                             (const gchar *uri)
70 {
71   return soup_uri_encode (uri, "%!*'();:@&=+$,/?#[] ");
72 }
73 
74 void
upload_cb(GObject * object,GAsyncResult * res,gpointer source_func)75 upload_cb                               (GObject      *object,
76                                          GAsyncResult *res,
77                                          gpointer      source_func)
78 {
79   FspSession *session = FSP_SESSION (object);
80   g_autoptr(GError) error = NULL;
81 
82   g_free (uploaded_photo_id);
83   uploaded_photo_id = fsp_session_upload_finish (session, res, &error);
84 
85   if (error != NULL)
86     {
87       g_print ("Error uploading picture: %s\n", error->message);
88     }
89   else
90     {
91       g_print ("[upload_cb]::Success! Photo ID: %s\n\n", uploaded_photo_id);
92 
93       /* Make a pause before continuing */
94       g_print ("Press ENTER to continue...\n\n");
95       getchar ();
96 
97       if (source_func == get_tags_list_cb)
98         {
99           /* Continue setting a license for the picture */
100           g_print ("Setting license for photo %s...\n", uploaded_photo_id);
101           fsp_session_set_license (session, uploaded_photo_id,
102                                    FSP_LICENSE_AT_NC_ND, NULL,
103                                    set_license_cb, NULL);
104         }
105       else if (source_func == photoset_created_cb)
106         {
107           /* Continue adding the picture to the photoset */
108           g_print ("Adding picture to photoset...\n");
109           fsp_session_add_to_photoset (session,
110                                        uploaded_photo_id,
111                                        created_photoset_id,
112                                        NULL, added_to_photoset_cb, NULL);
113         }
114       else if (source_func == get_groups_cb)
115         {
116           /* Continue adding the picture to the group */
117           g_print ("Adding picture to group...\n");
118           fsp_session_add_to_group (session,
119                                     uploaded_photo_id,
120                                     first_group_id,
121                                     NULL, added_to_group_cb, NULL);
122         }
123     }
124 }
125 
126 void
added_to_group_cb(GObject * object,GAsyncResult * res,gpointer user_data)127 added_to_group_cb                       (GObject      *object,
128                                          GAsyncResult *res,
129                                          gpointer      user_data)
130 {
131   FspSession *session = FSP_SESSION (object);
132   g_autoptr(GError) error = NULL;
133   gboolean result = FALSE;
134 
135   result = fsp_session_add_to_group_finish (session, res, &error);
136   if (error != NULL)
137     {
138       g_print ("Error creating group: %s\n", error->message);
139     }
140   else
141     {
142       g_print ("[added_to_groups_cb]::Success! (%s)\n\n",
143                result ? "OK" : "FAIL");
144     }
145 }
146 
147 void
get_groups_cb(GObject * object,GAsyncResult * res,gpointer user_data)148 get_groups_cb                           (GObject      *object,
149                                          GAsyncResult *res,
150                                          gpointer      user_data)
151 {
152   FspSession *session = FSP_SESSION (object);
153   g_autoptr(GError) error = NULL;
154   g_autoptr(GSList) groups_list = NULL;
155 
156   groups_list = fsp_session_get_groups_finish (session, res, &error);
157   if (error != NULL)
158     {
159       g_print ("Error getting groups: %s\n", error->message);
160     }
161   else
162     {
163       gint i = 0;
164       GSList *item = NULL;
165       FspDataGroup *group = NULL;
166 
167       g_print ("[get_groups_cb]::Success! Number of groups found: %d\n",
168                g_slist_length (groups_list));
169 
170       for (item = groups_list; item; item = g_slist_next (item))
171         {
172           group = FSP_DATA_GROUP (item->data);
173           g_print ("[get_groups_cb]::\tGroup #%d\n", i++);
174           g_print ("[get_groups_cb]::\t\tGroup id: %s\n", group->id);
175           g_print ("[get_groups_cb]::\t\tGroup name: %s\n", group->name);
176           g_print ("[get_groups_cb]::\t\tGroup privacy: %d\n", group->privacy);
177           g_print ("[get_groups_cb]::\t\tGroup number of photos: %d\n", group->n_photos);
178 
179           /* Store the ID of the first group to add a picture later */
180           if (item == groups_list)
181             first_group_id = g_strdup (group->id);
182 
183           fsp_data_free (FSP_DATA (group));
184         }
185 
186       getchar ();
187 
188       /* Continue adding a picture to the group, but first upload a new one */
189       g_print ("Uploading a new picture to be added to the group...");
190       fsp_session_upload (session,
191                           test_photo_path,
192                           "Yet another title",
193                           "Yet another description ",
194                           "yet some other tags",
195                           FSP_VISIBILITY_NO,
196                           FSP_VISIBILITY_YES,
197                           FSP_VISIBILITY_NONE,
198                           FSP_SAFETY_LEVEL_NONE,
199                           FSP_CONTENT_TYPE_PHOTO,
200                           FSP_SEARCH_SCOPE_NONE,
201                           NULL, upload_cb,
202                           (gpointer) get_groups_cb);
203     }
204 }
205 
206 void
added_to_photoset_cb(GObject * object,GAsyncResult * res,gpointer user_data)207 added_to_photoset_cb                    (GObject      *object,
208                                          GAsyncResult *res,
209                                          gpointer      user_data)
210 {
211   FspSession *session = FSP_SESSION (object);
212   g_autoptr(GError) error = NULL;
213   gboolean result = FALSE;
214 
215   result = fsp_session_add_to_photoset_finish (session, res, &error);
216   if (error != NULL)
217     {
218       g_print ("Error adding to photoset: %s\n", error->message);
219     }
220   else
221     {
222       g_print ("[added_to_photosets_cb]::Success! (%s)\n\n",
223                result ? "OK" : "FAIL");
224 
225       /* Make a pause before continuing */
226       g_print ("Press ENTER to continue...\n\n");
227       getchar ();
228 
229       /* Continue getting the list of groups */
230       g_print ("Getting list of groups...\n");
231       fsp_session_get_groups (session, NULL,
232                               get_groups_cb, NULL);
233     }
234 }
235 
236 void
photoset_created_cb(GObject * object,GAsyncResult * res,gpointer user_data)237 photoset_created_cb                     (GObject      *object,
238                                          GAsyncResult *res,
239                                          gpointer      user_data)
240 {
241   FspSession *session = FSP_SESSION (object);
242   g_autoptr(GError) error = NULL;
243   created_photoset_id =
244     fsp_session_create_photoset_finish (session, res, &error);
245 
246   if (error != NULL)
247     {
248       g_print ("Error creating photoset: %s\n", error->message);
249     }
250   else
251     {
252       g_print ("[photosets_created_cb]::Success! Photo set Id: %s\n\n",
253                created_photoset_id);
254 
255       getchar ();
256 
257       /* Continue adding a picture to the photoset, but first upload a new one */
258       g_print ("Uploading a new picture to be added to the photoset...");
259       fsp_session_upload (session,
260                           test_photo_path,
261                           "Yet another title",
262                           "Yet another description ",
263                           "yet some other tags",
264                           FSP_VISIBILITY_NO,
265                           FSP_VISIBILITY_YES,
266                           FSP_VISIBILITY_NONE,
267                           FSP_SAFETY_LEVEL_NONE,
268                           FSP_CONTENT_TYPE_PHOTO,
269                           FSP_SEARCH_SCOPE_NONE,
270                           NULL, upload_cb,
271                           (gpointer) photoset_created_cb);
272     }
273 }
274 
275 void
get_photosets_cb(GObject * object,GAsyncResult * res,gpointer user_data)276 get_photosets_cb                        (GObject      *object,
277                                          GAsyncResult *res,
278                                          gpointer      user_data)
279 {
280   FspSession *session = FSP_SESSION (object);
281   g_autoptr(GError) error = NULL;
282   g_autoptr(GSList) photosets_list =
283     fsp_session_get_photosets_finish (session, res, &error);
284 
285   if (error != NULL)
286     {
287       g_print ("Error getting photosets: %s\n", error->message);
288     }
289   else
290     {
291       gint i = 0;
292       GSList *item = NULL;
293       FspDataPhotoSet *photoset = NULL;
294 
295       g_print ("[get_photosets_cb]::Success! Number of photosets found: %d\n",
296                g_slist_length (photosets_list));
297 
298       for (item = photosets_list; item; item = g_slist_next (item))
299         {
300           photoset = FSP_DATA_PHOTO_SET (item->data);
301           g_print ("[get_photosets_cb]::\tPhotoset #%d\n", i++);
302           g_print ("[get_photosets_cb]::\t\tPhotoset id: %s\n", photoset->id);
303           g_print ("[get_photosets_cb]::\t\tPhotoset title: %s\n", photoset->title);
304           g_print ("[get_photosets_cb]::\t\tPhotoset description: %s\n", photoset->description);
305           g_print ("[get_photosets_cb]::\t\tPhotoset primary photo id: %s\n", photoset->primary_photo_id);
306           g_print ("[get_photosets_cb]::\t\tPhotoset number of photos: %d\n", photoset->n_photos);
307 
308           fsp_data_free (FSP_DATA (photoset));
309         }
310 
311       getchar ();
312 
313       /* Continue creating a new photoset */
314       g_print ("Creating a new photoset...\n");
315       fsp_session_create_photoset (session,
316                                    "Photoset's title",
317                                    "Photoset's description\nasdasda",
318                                    uploaded_photo_id,
319                                    NULL, photoset_created_cb, NULL);
320     }
321 }
322 
323 void
photo_get_info_cb(GObject * object,GAsyncResult * res,gpointer user_data)324 photo_get_info_cb                       (GObject      *object,
325                                          GAsyncResult *res,
326                                          gpointer      user_data)
327 {
328   FspSession *session = FSP_SESSION (object);
329   g_autoptr(GError) error = NULL;
330   FspDataPhotoInfo *photo_info =
331     fsp_session_get_info_finish (session, res, &error);
332 
333   if (error != NULL)
334     {
335       g_print ("Error uploading picture: %s\n", error->message);
336     }
337   else
338     {
339       g_print ("[photo_get_info_cb]::Success! Photo Info:\n");
340       g_print ("[photo_get_info_cb]::\tPhoto id: %s\n", photo_info->id);
341       g_print ("[photo_get_info_cb]::\tPhoto secret: %s\n", photo_info->secret);
342       g_print ("[photo_get_info_cb]::\tPhoto server: %s\n", photo_info->server);
343       g_print ("[photo_get_info_cb]::\tPhoto isfavorite: %s\n",
344                photo_info->is_favorite ? "Yes" : "No");
345       g_print ("[photo_get_info_cb]::\tPhoto license: %d\n", photo_info->license);
346       g_print ("[photo_get_info_cb]::\tPhoto rotation: %d\n", photo_info->rotation);
347       g_print ("[photo_get_info_cb]::\tPhoto orig_secret: %s\n", photo_info->orig_secret);
348       g_print ("[photo_get_info_cb]::\tPhoto orig_format: %s\n", photo_info->orig_format);
349       g_print ("[photo_get_info_cb]::\tPhoto title: %s\n", photo_info->title);
350       g_print ("[photo_get_info_cb]::\tPhoto description: %s\n", photo_info->description);
351       g_print ("[photo_get_info_cb]::\tPhoto ispublic: %s\n",
352                photo_info->is_public ? "Yes" : "No");
353       g_print ("[photo_get_info_cb]::\tPhoto isfamily: %s\n",
354                photo_info->is_family ? "Yes" : "No");
355       g_print ("[photo_get_info_cb]::\tPhoto isfriend: %s\n",
356                photo_info->is_friend ? "Yes" : "No");
357       g_print ("[photo_get_info_cb]::\tPhoto permcomment: %d\n", photo_info->perm_comment);
358       g_print ("[photo_get_info_cb]::\tPhoto permaddmeta: %d\n", photo_info->perm_add_meta);
359       g_print ("[photo_get_info_cb]::\tPhoto cancomment: %d\n", photo_info->can_comment);
360       g_print ("[photo_get_info_cb]::\tPhoto canaddmeta: %d\n", photo_info->can_add_meta);
361 
362       /* Make a pause before continuing */
363       g_print ("Press ENTER to continue...\n\n");
364       getchar ();
365 
366       /* Continue getting the list of photosets */
367       g_print ("Getting list of photosets...\n");
368       fsp_session_get_photosets (session, NULL, get_photosets_cb, NULL);
369 
370       fsp_data_free (FSP_DATA (photo_info));
371     }
372 }
373 
374 void
set_date_posted_cb(GObject * object,GAsyncResult * res,gpointer user_data)375 set_date_posted_cb                      (GObject      *object,
376                                          GAsyncResult *res,
377                                          gpointer      user_data)
378 {
379   FspSession *session = FSP_SESSION (object);
380   g_autoptr(GError) error = NULL;
381   gboolean result = FALSE;
382 
383   result = fsp_session_set_date_posted_finish (session, res, &error);
384   if (error != NULL)
385     {
386       g_print ("Error setting the date posted: %s\n", error->message);
387     }
388   else
389     {
390       g_print ("[set_date_posted_cb]::Success! (%s)\n\n",
391                result ? "OK" : "FAIL");
392 
393       /* Make a pause before continuing */
394       g_print ("Press ENTER to continue...\n\n");
395       getchar ();
396 
397       /* Continue getting info about the picture */
398       g_print ("Getting info for photo %s...\n", uploaded_photo_id);
399       fsp_session_get_info (session, uploaded_photo_id, NULL,
400                             photo_get_info_cb, NULL);
401     }
402 }
403 
404 void
get_location_cb(GObject * object,GAsyncResult * res,gpointer user_data)405 get_location_cb                         (GObject      *object,
406                                          GAsyncResult *res,
407                                          gpointer      user_data)
408 {
409   FspSession *session = FSP_SESSION (object);
410   g_autoptr(GError) error = NULL;
411   FspDataLocation *location = NULL;
412 
413   location = fsp_session_get_location_finish (session, res, &error);
414   if (error != NULL)
415     {
416       g_print ("Error getting location: %s\n", error->message);
417     }
418   else
419     {
420       GDateTime *date_now = NULL;
421       GDateTime *date = NULL;
422       g_autofree gchar* date_str = NULL;
423 
424       g_print ("[get_location_cb]::Success! Location got:\n");
425       g_print ("[get_location_cb]::\tLatitude: %g\n", location->latitude);
426       g_print ("[get_location_cb]::\tLongitude: %g\n", location->longitude);
427       g_print ("[get_location_cb]::\tAccuracy: %d\n", location->accuracy);
428 
429       /* Make a pause before continuing */
430       g_print ("Press ENTER to continue...\n\n");
431       getchar ();
432 
433       /* Continue setting the date posted to one year ago */
434       date_now = g_date_time_new_now_local ();
435       date = g_date_time_add_years (date_now, -1);
436       date_str = g_date_time_format (date, "%Y-%m-%d %H:%M:%S");
437       g_print ("Setting date posted for photo %s to %s...\n", uploaded_photo_id, date_str);
438       fsp_session_set_date_posted (session, uploaded_photo_id, date, NULL,
439                                    set_date_posted_cb, NULL);
440 
441       g_date_time_unref (date);
442       g_date_time_unref (date_now);
443 
444       fsp_data_free (FSP_DATA (location));
445     }
446 }
447 
448 
449 void
set_location_cb(GObject * object,GAsyncResult * res,gpointer user_data)450 set_location_cb                         (GObject      *object,
451                                          GAsyncResult *res,
452                                          gpointer      user_data)
453 {
454   FspSession *session = FSP_SESSION (object);
455   g_autoptr(GError) error = NULL;
456   gboolean result = FALSE;
457 
458   result = fsp_session_set_location_finish (session, res, &error);
459   if (error != NULL)
460     {
461       g_print ("Error setting location: %s\n", error->message);
462     }
463   else
464     {
465       g_print ("[set_location_cb]::Success! (%s)\n\n",
466                result ? "OK" : "FAIL");
467 
468       /* Make a pause before continuing */
469       g_print ("Press ENTER to continue...\n\n");
470       getchar ();
471 
472       /* Continue getting the location for the picture (should match) */
473       g_print ("Getting location for photo %s...\n", uploaded_photo_id);
474       fsp_session_get_location (session, uploaded_photo_id, NULL,
475                                 get_location_cb, NULL);
476     }
477 }
478 
479 void
set_license_cb(GObject * object,GAsyncResult * res,gpointer user_data)480 set_license_cb                          (GObject      *object,
481                                          GAsyncResult *res,
482                                          gpointer      user_data)
483 {
484   FspSession *session = FSP_SESSION (object);
485   g_autoptr(GError) error = NULL;
486   gboolean result = FALSE;
487 
488   result = fsp_session_set_license_finish (session, res, &error);
489   if (error != NULL)
490     {
491       g_print ("Error setting license: %s\n", error->message);
492     }
493   else
494     {
495       FspDataLocation *location = NULL;
496 
497       g_print ("[set_license_cb]::Success! (%s)\n\n",
498                result ? "OK" : "FAIL");
499 
500       /* Make a pause before continuing */
501       g_print ("Press ENTER to continue...\n\n");
502       getchar ();
503 
504       /* Continue setting a location for the picture */
505       location = FSP_DATA_LOCATION (fsp_data_new (FSP_LOCATION));
506       location->latitude = 42.166448;
507       location->longitude = -7.182521;
508       location->accuracy = 10;
509 
510       g_print ("Setting location for photo %s to (%g, %g, %d)...\n",
511                uploaded_photo_id,
512                location->latitude,
513                location->longitude,
514                location->accuracy);
515 
516       fsp_session_set_location (session, uploaded_photo_id, location,
517                                 NULL, set_location_cb, NULL);
518       fsp_data_free (FSP_DATA (location));
519     }
520 }
521 
522 void
get_tags_list_cb(GObject * object,GAsyncResult * res,gpointer unused)523 get_tags_list_cb (GObject *object, GAsyncResult *res, gpointer unused)
524 {
525   FspSession* session = FSP_SESSION (object);
526   g_autoptr(GSList) tags_list = NULL;
527   g_autoptr(GError) error = NULL;
528 
529   tags_list = fsp_session_get_tags_list_finish (session, res, &error);
530   if (error != NULL)
531     {
532       g_print ("Error retrieving tags: %s\n", error->message);
533     }
534   else
535     {
536       GSList *item = NULL;
537       gchar *tag = NULL;
538 
539       g_print ("[get_tags_list_cb]::Success! Number of tags found: %d\n",
540                g_slist_length (tags_list));
541 
542       for (item = tags_list; item; item = g_slist_next (item))
543         {
544           tag = (gchar *) item->data;
545           g_print ("[get_tags_list_cb]::\tTag: %s\n", tag);
546           g_free (tag);
547         }
548 
549       /* Make a pause before continuing */
550       g_print ("Press ENTER to continue...\n\n");
551       getchar ();
552 
553       /* Continue uploading a picture */
554       g_print ("Uploading a picture...\n");
555       fsp_session_upload (session,
556                           test_photo_path,
557                           "title with spaces and wéïrd characters!",
558                           "description with\nmultiple lines",
559                           "áèïôu "
560                           "çÇ*+[]{} "
561                           "qwerty "
562                           "!·$%&/(@#~^*+ "
563                           "\"Tag With Spaces\"",
564                           FSP_VISIBILITY_NO,
565                           FSP_VISIBILITY_YES,
566                           FSP_VISIBILITY_NONE,
567                           FSP_SAFETY_LEVEL_NONE,
568                           FSP_CONTENT_TYPE_PHOTO,
569                           FSP_SEARCH_SCOPE_NONE,
570                           NULL, upload_cb,
571                           (gpointer) get_tags_list_cb);
572     }
573 }
574 
575 void
get_upload_status_cb(GObject * object,GAsyncResult * res,gpointer unused)576 get_upload_status_cb (GObject *object, GAsyncResult *res, gpointer unused)
577 {
578   FspSession* session = FSP_SESSION (object);
579   FspDataUploadStatus *upload_status = NULL;
580   g_autoptr(GError) error = NULL;
581 
582   upload_status = fsp_session_get_upload_status_finish (session, res, &error);
583   if (error != NULL)
584     {
585       g_print ("Error retrieving upload status: %s\n", error->message);
586     }
587   else
588     {
589       g_print ("[get_upload_status_cb]::Success! Upload status:\n");
590       g_print ("[get_upload_status_cb]::\tUser id: %s\n", upload_status->id);
591       g_print ("[get_upload_status_cb]::\tUser is pro?: %s\n",
592                upload_status->pro_user ? "YES" : "NO");
593       g_print ("[get_upload_status_cb]::\tBandwitdh Max KB: %lu\n",
594                upload_status->bw_max_kb);
595       g_print ("[get_upload_status_cb]::\tBandwitdh Used KB: %lu\n",
596                upload_status->bw_used_kb);
597       g_print ("[get_upload_status_cb]::\tBandwitdh Remaining KB: %lu\n",
598                upload_status->bw_remaining_kb);
599       g_print ("[get_upload_status_cb]::\tBandwitdh Used videos: %u\n",
600                upload_status->bw_used_videos);
601       g_print ("[get_upload_status_cb]::\tBandwitdh Remaining videos: %u\n",
602                upload_status->bw_remaining_videos);
603       g_print ("[get_upload_status_cb]::\tPicture filesize Max KB: %lu\n",
604                upload_status->picture_fs_max_kb);
605       g_print ("[get_upload_status_cb]::\tVideo filesize Max KB: %lu\n",
606                upload_status->video_fs_max_kb);
607 
608       /* Make a pause before continuing */
609       g_print ("Press ENTER to continue...\n\n");
610       getchar ();
611 
612       /* Continue getting the list of tags */
613       g_print ("Getting the list of tags...\n");
614       fsp_session_get_tags_list (session, NULL, get_tags_list_cb, NULL);
615       fsp_data_free (FSP_DATA (upload_status));
616     }
617 }
618 
619 void
check_auth_info_cb(GObject * object,GAsyncResult * res,gpointer unused)620 check_auth_info_cb (GObject *object, GAsyncResult *res, gpointer unused)
621 {
622   FspSession* session = FSP_SESSION (object);
623   FspDataAuthToken *auth_token = NULL;
624   g_autoptr(GError) error = NULL;
625 
626   auth_token = fsp_session_check_auth_info_finish (session, res, &error);
627   if (error != NULL)
628     {
629       g_print ("Error checking authorization information: %s\n", error->message);
630     }
631   else
632     {
633       g_print ("[check_auth_info_cb]::Result: Success!\n\n");
634 
635       g_print ("[check_auth_info_cb]::Auth token\n");
636       g_print ("[check_auth_info_cb]::\ttoken = %s\n", auth_token->token);
637       g_print ("[check_auth_info_cb]::\tpermissions = %s\n", auth_token->permissions);
638       g_print ("[check_auth_info_cb]::\tnsid = %s\n", auth_token->nsid);
639       g_print ("[check_auth_info_cb]::\tusername = %s\n", auth_token->username);
640       g_print ("[check_auth_info_cb]::\tfullname = %s\n", auth_token->fullname);
641 
642       /* Make a pause before continuing */
643       g_print ("Press ENTER to continue...\n\n");
644       getchar ();
645 
646       /* Continue getting the upload status */
647       g_print ("Retrieving upload status...\n");
648       fsp_session_get_upload_status (session, NULL, get_upload_status_cb, NULL);
649 
650       fsp_data_free (FSP_DATA (auth_token));
651     }
652 }
653 
654 void
complete_auth_cb(GObject * object,GAsyncResult * res,gpointer user_data)655 complete_auth_cb                        (GObject      *object,
656                                          GAsyncResult *res,
657                                          gpointer      user_data)
658 {
659   FspSession* session = FSP_SESSION (object);
660   FspDataAuthToken *auth_token = NULL;
661   g_autoptr(GError) error = NULL;
662 
663   auth_token = fsp_session_complete_auth_finish (session, res, &error);
664   if (error != NULL)
665     {
666       g_print ("Error completing authorization: %s\n", error->message);
667     }
668   else
669     {
670       g_print ("[complete_auth_cb]::Result: Success!\n\n");
671 
672       g_print ("[complete_auth_cb]::Auth token\n");
673       g_print ("[complete_auth_cb]::\ttoken = %s\n", auth_token->token);
674       g_print ("[complete_auth_cb]::\ttoken_secret = %s\n", auth_token->token_secret);
675       g_print ("[complete_auth_cb]::\tpermissions = %s\n", auth_token->permissions);
676       g_print ("[complete_auth_cb]::\tnsid = %s\n", auth_token->nsid);
677       g_print ("[complete_auth_cb]::\tusername = %s\n", auth_token->username);
678       g_print ("[complete_auth_cb]::\tfullname = %s\n", auth_token->fullname);
679 
680       /* Make a pause before continuing */
681       g_print ("Press ENTER to continue...\n\n");
682       getchar ();
683 
684       /* Continue checking the authorization information */
685       g_print ("Checking the authorization information...\n");
686       fsp_session_check_auth_info (session, NULL, check_auth_info_cb, NULL);
687 
688       fsp_data_free (FSP_DATA (auth_token));
689     }
690 }
691 
692 void
get_auth_url_cb(GObject * object,GAsyncResult * res,gpointer user_data)693 get_auth_url_cb                         (GObject      *object,
694                                          GAsyncResult *res,
695                                          gpointer      user_data)
696 {
697   FspSession* session = FSP_SESSION (object);
698   g_autoptr(GError) error = NULL;
699   g_autofree gchar *auth_url = fsp_session_get_auth_url_finish (session, res, &error);
700 
701   if (error != NULL)
702     {
703       g_print ("Error getting auth URL: %s\n", error->message);
704     }
705   else
706     {
707       char buffer[12];
708 
709       g_print ("[get_auth_url_cb]::Result: %s&perms=write\n\n",
710                auth_url ? auth_url : "No URL got");
711 
712       /* Make a pause before continuing */
713       g_print ("\nEnter the verification code and press ENTER to continue: ");
714       if (fgets(buffer, 12, stdin))
715         {
716           g_autofree gchar *verifier = NULL;
717 
718           verifier = encode_uri (buffer);
719 
720           /* Continue finishing the authorization */
721           g_print ("Finishing authorization...\n");
722           fsp_session_complete_auth (session, verifier, NULL, complete_auth_cb, NULL);
723         }
724       else
725         g_print ("Authorization failed. Can't continue.\n");
726     }
727 }
728 
729 gboolean
do_work(gpointer unused)730 do_work (gpointer unused)
731 {
732   FspSession *session = NULL;
733   const gchar *api_key = NULL;
734   const gchar *secret = NULL;
735 
736   session = fsp_session_new (API_KEY, SHARED_SECRET, NULL);
737   g_print ("Created FspSession:\n");
738 
739   api_key = fsp_session_get_api_key (session);
740   secret = fsp_session_get_secret (session);
741 
742   g_print ("\tAPI key: %s\n\tSecret: %s\n\n", api_key, secret);
743 
744   g_print ("Getting authorization URL...\n");
745   fsp_session_get_auth_url (session, NULL, get_auth_url_cb, NULL);
746 
747   return FALSE;
748 }
749 
750 int
main(int argc,char ** args)751 main                                    (int    argc,
752                                          char **args)
753 {
754   GMainLoop *mainloop;
755 
756   g_print ("Running flicksoup example...\n\n");
757 
758 #if GCRYPT_VERSION_NUMBER < 0x010600
759   /* Initialize gcrypt at the very beginning */
760   gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
761 #endif
762 
763   /* Version check should be almost the very first call because it
764      makes sure that important subsystems are initialized. */
765   g_assert (gcry_check_version (LIBGCRYPT_MIN_VERSION));
766   /* Allocate a pool of 16k secure memory.  This make the secure
767      memory available and also drops privileges where needed. */
768   gcry_control (GCRYCTL_INIT_SECMEM, 16384, 0);
769   /* Tell Libgcrypt that initialization has completed. */
770   gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
771 
772   /* Find full path to the testing photo */
773   test_photo_path = g_strdup_printf ("file://%s/%s",
774                                      g_get_current_dir (),
775                                      TEST_PHOTO);
776 
777   /* Queue the work in the main context */
778   g_idle_add (do_work, NULL);
779 
780   /* Run the main loop */
781   mainloop = g_main_loop_new (NULL, FALSE);
782   g_main_loop_run (mainloop);
783 
784   return 0;
785 }
786 
787