Initial Import
[profile/ivi/clutter-toys.git] / attic / aaina / libnflick / nflick-photo-list-worker.c
1 /******************************************************************************/
2 /*                                                                            */
3 /* GPL license, Copyright (c) 2005-2006 by:                                   */
4 /*                                                                            */
5 /* Authors:                                                                   */
6 /*      Michael Dominic K. <michaldominik@gmail.com>                          */
7 /*                                                                            */
8 /* This program is free software; you can redistribute it and/or modify it    */
9 /* under the terms of the GNU General Public License as published by the      */
10 /* Free Software Foundation; either version 2, or (at your option) any later  */
11 /* version.                                                                   */
12 /*                                                                            */
13 /* This program is distributed in the hope that it will be useful, but        */
14 /* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY */
15 /* or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License   */
16 /* for more details.                                                          */
17 /*                                                                            */
18 /* You should have received a copy of the GNU General Public License along    */
19 /* with this program; if not, write to the Free Software Foundation, Inc., 59 */
20 /* Temple Place - Suite 330, Boston, MA 02111-1307, USA.                      */
21 /*                                                                            */
22 /******************************************************************************/
23
24 #include "nflick-photo-list-worker.h"
25 #include "nflick-photo-list-worker-private.h"
26
27 GType                           nflick_photo_list_worker_get_type (void)
28 {
29         static GType objecttype = 0;
30
31         if (!objecttype) {
32
33                 static const GTypeInfo objectinfo = {
34                         sizeof (NFlickPhotoListWorkerClass), 
35                         NULL, 
36                         NULL,
37                         (GClassInitFunc) nflick_photo_list_worker_class_init,
38                         NULL,
39                         NULL, 
40                         sizeof (NFlickPhotoListWorker), 
41                         4, 
42                         (GInstanceInitFunc) nflick_photo_list_worker_init,
43                 };
44                 objecttype = g_type_register_static (NFLICK_TYPE_WORKER, "NFlickPhotoListWorker",
45                                                      &objectinfo, 0);
46         }
47         return objecttype;
48 }
49
50 static void                     nflick_photo_list_worker_class_init (NFlickPhotoListWorkerClass *klass)
51 {
52         GObjectClass *gobjectclass = (GObjectClass *) klass;
53         NFlickWorkerClass *workerclass = (NFlickWorkerClass *) klass;
54
55         gobjectclass->dispose = (gpointer) nflick_photo_list_worker_dispose;
56         gobjectclass->finalize = (gpointer) nflick_photo_list_worker_finalize;
57         gobjectclass->get_property = (gpointer) nflick_photo_list_worker_get_property;
58         
59         workerclass->ThreadFunc = (NFlickWorkerThreadFunc) thread_func;
60
61         ParentClass = g_type_class_ref (NFLICK_TYPE_WORKER);
62 }
63
64 static void                     nflick_photo_list_worker_init (NFlickPhotoListWorker *self)
65 {
66         g_return_if_fail (NFLICK_IS_PHOTO_LIST_WORKER (self));
67
68         self->Private = NULL;
69
70         NFlickPhotoListWorkerPrivate *priv = g_new0 (NFlickPhotoListWorkerPrivate, 1);
71         g_return_if_fail (priv != NULL);
72         
73         if (private_init (self, priv) == TRUE) {
74                 self->Private = priv;
75                 nflick_worker_set_message ((NFlickWorker *) self, gettext ("Loading photoset data..."));
76         } else {
77                 private_dispose (priv);
78                 g_free (priv);
79                 self->Private = NULL;
80         }
81 }
82
83 static gboolean                 private_init (NFlickPhotoListWorker *self, NFlickPhotoListWorkerPrivate *private)
84 {
85         g_return_val_if_fail (NFLICK_IS_PHOTO_LIST_WORKER (self), FALSE);
86         g_return_val_if_fail (private != NULL, FALSE);
87
88         private->Id = NULL;
89         private->Token = NULL;
90         private->PhotoDataList = NULL;
91
92         return TRUE;
93 }
94
95 static void                     private_dispose (NFlickPhotoListWorkerPrivate *private)
96 {
97         g_return_if_fail (private != NULL);
98
99         if (private->Id != NULL) {
100                 g_free (private->Id);
101                 private->Id = NULL;
102         }
103
104         if (private->Token != NULL) {
105                 g_free (private->Token);
106                 private->Token = NULL;
107         }
108
109         if (private->PhotoDataList != NULL) {
110
111                 GList *iterator;
112         
113                 for (iterator = private->PhotoDataList; iterator; iterator = g_list_next (iterator))
114                         if (iterator->data != NULL)
115                                 nflick_photo_data_free ((NFlickPhotoData *) iterator->data);
116                 
117                 g_list_free (private->PhotoDataList);
118                 private->PhotoDataList = NULL;
119         }
120 }
121
122 static void                     nflick_photo_list_worker_dispose (NFlickPhotoListWorker *self)
123 {
124         g_return_if_fail (NFLICK_IS_PHOTO_LIST_WORKER (self));
125
126         if (self->Private != NULL)
127                 private_dispose (self->Private);
128
129         G_OBJECT_CLASS (ParentClass)->dispose (G_OBJECT (self));
130 }
131
132 static void                     nflick_photo_list_worker_finalize (NFlickPhotoListWorker *self)
133 {
134         g_return_if_fail (NFLICK_IS_PHOTO_LIST_WORKER (self));
135         
136         if (self->Private != NULL) {
137                 g_free (self->Private);
138                 self->Private = NULL;
139         }
140
141         G_OBJECT_CLASS (ParentClass)->finalize (G_OBJECT (self));
142 }
143
144 static NFlickWorkerStatus       thread_func (NFlickPhotoListWorker *self)
145 {
146         NFlickApiRequest *get_photolist_request = NULL; 
147         NFlickWorkerStatus status = NFLICK_WORKER_STATUS_OK;
148         NFlickApiResponse *photo_list_response = NULL;
149
150         get_photolist_request = nflick_api_request_new (NFLICK_FLICKR_API_METHOD_PHOTOSETS_GET_PHOTOS);
151         if (get_photolist_request == NULL)
152                 goto Error;
153         
154         nflick_api_request_add_parameter (get_photolist_request, 
155                                           NFLICK_FLICKR_API_PARAM_TOKEN, 
156                                           self->Private->Token);
157
158         nflick_api_request_add_parameter (get_photolist_request, 
159                                           NFLICK_FLICKR_API_PARAM_PHOTOSET_ID, 
160                                           self->Private->Id);
161
162         nflick_api_request_sign (get_photolist_request);
163         if (nflick_api_request_exec (get_photolist_request) != TRUE) {
164                 nflick_worker_set_network_error ((NFlickWorker *) self);
165                 goto Error;
166         }
167
168         if (nflick_worker_is_aborted ((NFlickWorker *) self) == TRUE)
169                 goto Abort;
170
171         photo_list_response = nflick_api_response_new_from_request (NFLICK_TYPE_PHOTO_LIST_RESPONSE, get_photolist_request);
172         if (photo_list_response == NULL)
173                 goto Error;
174
175         if (nflick_worker_parse_api_response ((NFlickWorker*) self, photo_list_response) == FALSE)
176                 goto Error;
177
178         self->Private->PhotoDataList = nflick_photo_list_response_take_list ((NFlickPhotoListResponse *) photo_list_response);
179
180         /* All ok */
181         goto Done;
182
183 Abort:
184         status = NFLICK_WORKER_STATUS_ABORTED;
185         goto Done;
186
187 Error:
188         status = NFLICK_WORKER_STATUS_ERROR;
189
190 Done:
191         if (get_photolist_request != NULL) 
192                 g_object_unref (get_photolist_request);
193
194         if (photo_list_response != NULL) 
195                 g_object_unref (photo_list_response);
196
197         return status;
198 }
199
200 NFlickPhotoListWorker*         nflick_photo_list_worker_new (const gchar *id, const gchar *token)
201 {
202         g_return_val_if_fail (id != NULL, NULL);
203
204         NFlickPhotoListWorker *self = g_object_new (NFLICK_TYPE_PHOTO_LIST_WORKER, NULL);
205         g_return_val_if_fail (self != NULL, NULL);
206
207         if (self->Private == NULL) {
208                 g_object_unref (self);
209                 return NULL;
210         }
211
212         self->Private->Id = g_strdup (id);
213         self->Private->Token = g_strdup (token);
214         
215         return self;
216 }
217
218 static void                     nflick_photo_list_worker_get_property (NFlickPhotoListWorker *self, guint propid, 
219                                                                        GValue *value, GParamSpec *pspec)
220 {
221         g_return_if_fail (NFLICK_IS_PHOTO_LIST_WORKER (self));
222         g_assert (self->Private != NULL);
223                 
224         switch (propid) {
225                 
226                 default:
227                         G_OBJECT_WARN_INVALID_PROPERTY_ID (G_OBJECT (self), propid, pspec);
228                 break;
229         }
230 }
231
232 GList*                          nflick_photo_list_worker_take_list (NFlickPhotoListWorker *self)
233 {
234         g_return_val_if_fail (NFLICK_IS_PHOTO_LIST_WORKER (self), NULL);
235
236         GList *lst = self->Private->PhotoDataList;
237         self->Private->PhotoDataList = NULL;
238
239         return lst;
240 }