Initial Import
[profile/ivi/clutter-toys.git] / attic / aaina / libnflick / nflick-auth-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-auth-worker.h"
25 #include "nflick-auth-worker-private.h"
26
27 GType                           nflick_auth_worker_get_type (void)
28 {
29         static GType objecttype = 0;
30
31         if (!objecttype) {
32
33                 static const GTypeInfo objectinfo = {
34                         sizeof (NFlickAuthWorkerClass), 
35                         NULL, 
36                         NULL,
37                         (GClassInitFunc) nflick_auth_worker_class_init,
38                         NULL,
39                         NULL, 
40                         sizeof (NFlickAuthWorker), 
41                         4, 
42                         (GInstanceInitFunc) nflick_auth_worker_init,
43                 };
44                 objecttype = g_type_register_static (NFLICK_TYPE_WORKER, "NFlickAuthWorker",
45                                                      &objectinfo, 0);
46         }
47         return objecttype;
48 }
49
50 static void                     nflick_auth_worker_class_init (NFlickAuthWorkerClass *klass)
51 {
52         GObjectClass *gobjectclass = (GObjectClass *) klass;
53         NFlickWorkerClass *workerclass = (NFlickWorkerClass *) klass;
54
55         gobjectclass->dispose = (gpointer) nflick_auth_worker_dispose;
56         gobjectclass->finalize = (gpointer) nflick_auth_worker_finalize;
57         gobjectclass->get_property = (gpointer) nflick_auth_worker_get_property;
58         
59         g_object_class_install_property (gobjectclass, ARG_TOKEN,
60                                          g_param_spec_string
61                                          ("token", "Token", "Unique flick full token",
62                                          NULL, G_PARAM_READABLE));
63
64         g_object_class_install_property (gobjectclass, ARG_USER_NAME,
65                                          g_param_spec_string
66                                          ("username", "UserName", "Flickr user name",
67                                          NULL, G_PARAM_READABLE));
68
69         g_object_class_install_property (gobjectclass, ARG_FULL_NAME,
70                                          g_param_spec_string
71                                          ("fullname", "FullName", "Flickr full user name",
72                                          NULL, G_PARAM_READABLE));
73
74         g_object_class_install_property (gobjectclass, ARG_USER_NSID,
75                                          g_param_spec_string
76                                          ("usernsid", "UserNsid", "Unique nsid identyfying user in flickr",
77                                          NULL, G_PARAM_READABLE));
78
79         workerclass->ThreadFunc = (NFlickWorkerThreadFunc) thread_func;
80
81         ParentClass = g_type_class_ref (NFLICK_TYPE_WORKER);
82 }
83
84 static void                     nflick_auth_worker_init (NFlickAuthWorker *self)
85 {
86         g_return_if_fail (NFLICK_IS_AUTH_WORKER (self));
87
88         self->Private = NULL;
89
90         NFlickAuthWorkerPrivate *priv = g_new0 (NFlickAuthWorkerPrivate, 1);
91         g_return_if_fail (priv != NULL);
92         
93         if (private_init (self, priv) == TRUE) {
94                 self->Private = priv;
95                 nflick_worker_set_message ((NFlickWorker *) self, gettext ("Authorizing token..."));
96         } else {
97                 private_dispose (priv);
98                 g_free (priv);
99                 self->Private = NULL;
100         }
101 }
102
103 static gboolean                 private_init (NFlickAuthWorker *self, NFlickAuthWorkerPrivate *private)
104 {
105         g_return_val_if_fail (NFLICK_IS_AUTH_WORKER (self), FALSE);
106         g_return_val_if_fail (private != NULL, FALSE);
107
108         private->MiniToken = NULL;
109         private->UserName = NULL;
110         private->FullName = NULL;
111         private->UserNsid = NULL;
112         private->Token = NULL;
113
114         return TRUE;
115 }
116
117 static void                     private_dispose (NFlickAuthWorkerPrivate *private)
118 {
119         g_return_if_fail (private != NULL);
120
121         if (private->MiniToken != NULL) {
122                 g_free (private->MiniToken);
123                 private->MiniToken = NULL;
124         }
125
126         if (private->UserName != NULL) {
127                 g_free (private->UserName);
128                 private->UserName = NULL;
129         }
130
131         if (private->FullName != NULL) {
132                 g_free (private->FullName);
133                 private->FullName = NULL;
134         }
135
136         if (private->Token != NULL) {
137                 g_free (private->Token);
138                 private->Token = NULL;
139         }
140
141         if (private->UserNsid != NULL) {
142                 g_free (private->UserNsid);
143                 private->UserNsid = NULL;
144         }
145 }
146
147 static void                     nflick_auth_worker_dispose (NFlickAuthWorker *self)
148 {
149         g_return_if_fail (NFLICK_IS_AUTH_WORKER (self));
150
151         if (self->Private != NULL)
152                 private_dispose (self->Private);
153
154         G_OBJECT_CLASS (ParentClass)->dispose (G_OBJECT (self));
155 }
156
157 static void                     nflick_auth_worker_finalize (NFlickAuthWorker *self)
158 {
159         g_return_if_fail (NFLICK_IS_AUTH_WORKER (self));
160         
161         if (self->Private != NULL) {
162                 g_free (self->Private);
163                 self->Private = NULL;
164         }
165
166         G_OBJECT_CLASS (ParentClass)->finalize (G_OBJECT (self));
167 }
168
169 static NFlickWorkerStatus       thread_func (NFlickAuthWorker *self)
170 {
171         NFlickApiRequest *full_token_request = NULL; 
172         NFlickApiResponse *full_token_response = NULL;
173         NFlickWorkerStatus status = NFLICK_WORKER_STATUS_OK;
174
175         full_token_request = nflick_api_request_new (NFLICK_FLICKR_API_METHOD_GET_FULL_TOKEN);
176         if (full_token_request == NULL)
177                 goto Error;
178         
179         nflick_api_request_add_parameter (full_token_request, 
180                                           NFLICK_FLICKR_API_PARAM_MINI_TOKEN, 
181                                           self->Private->MiniToken);
182
183         nflick_api_request_sign (full_token_request);
184         
185         if (nflick_api_request_exec (full_token_request) != TRUE) {
186                 nflick_worker_set_network_error ((NFlickWorker *) self);
187                 goto Error;
188         }
189
190         if (nflick_worker_is_aborted ((NFlickWorker *) self) == TRUE)
191                 goto Abort;
192
193         full_token_response = nflick_api_response_new_from_request (NFLICK_TYPE_GFT_RESPONSE, full_token_request);
194         if (full_token_response == NULL)
195                 goto Error;
196
197         if (nflick_worker_parse_api_response ((NFlickWorker*) self, full_token_response) == FALSE)
198                 goto Error;
199
200         /* Get out variables */
201         g_object_get (G_OBJECT (full_token_response), 
202                       "username", &self->Private->UserName,
203                       "fullname", &self->Private->FullName,
204                       "usernsid", &self->Private->UserNsid,
205                       "token", &self->Private->Token, NULL);
206
207         if (self->Private->UserName == NULL ||
208             self->Private->FullName == NULL ||
209             self->Private->Token == NULL ||
210             self->Private->UserNsid == NULL)
211                 goto Error;
212
213         /* All ok */
214         goto Done;
215
216 Abort:
217         status = NFLICK_WORKER_STATUS_ABORTED;
218         goto Done;
219
220 Error:
221         status = NFLICK_WORKER_STATUS_ERROR;
222
223 Done:
224         if (full_token_request != NULL) 
225                 g_object_unref (full_token_request);
226
227         if (full_token_response != NULL) 
228                 g_object_unref (full_token_response);
229
230         return status;
231 }
232
233 NFlickAuthWorker*               nflick_auth_worker_new (const gchar *minitoken)
234 {
235         g_return_val_if_fail (minitoken != NULL, NULL);
236
237         NFlickAuthWorker *self = g_object_new (NFLICK_TYPE_AUTH_WORKER, NULL);
238         g_return_val_if_fail (self != NULL, NULL);
239
240         if (self->Private == NULL) {
241                 g_object_unref (self);
242                 return NULL;
243         }
244
245         self->Private->MiniToken = g_strdup (minitoken);
246         
247         return self;
248 }
249
250 static void                     nflick_auth_worker_get_property (NFlickAuthWorker *self, guint propid, 
251                                                                  GValue *value, GParamSpec *pspec)
252 {
253         g_return_if_fail (NFLICK_IS_AUTH_WORKER (self));
254         g_assert (self->Private != NULL);
255                 
256         switch (propid) {
257                 
258                 case ARG_USER_NAME:
259                         g_value_set_string (value, self->Private->UserName);
260                 break;
261         
262                 case ARG_FULL_NAME:
263                         g_value_set_string (value, self->Private->FullName);
264                 break;
265  
266                 case ARG_TOKEN:
267                         g_value_set_string (value, self->Private->Token);
268                 break;
269        
270                 case ARG_USER_NSID:
271                         g_value_set_string (value, self->Private->UserNsid);
272                 break;
273
274                 default:
275                         G_OBJECT_WARN_INVALID_PROPERTY_ID (G_OBJECT (self), propid, pspec);
276                 break;
277         }
278 }