1 /******************************************************************************/
3 /* GPL license, Copyright (c) 2005-2006 by: */
6 /* Michael Dominic K. <michaldominik@gmail.com> */
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 */
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. */
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. */
22 /******************************************************************************/
24 #include "nflick-auth-worker.h"
25 #include "nflick-auth-worker-private.h"
27 GType nflick_auth_worker_get_type (void)
29 static GType objecttype = 0;
33 static const GTypeInfo objectinfo = {
34 sizeof (NFlickAuthWorkerClass),
37 (GClassInitFunc) nflick_auth_worker_class_init,
40 sizeof (NFlickAuthWorker),
42 (GInstanceInitFunc) nflick_auth_worker_init,
44 objecttype = g_type_register_static (NFLICK_TYPE_WORKER, "NFlickAuthWorker",
50 static void nflick_auth_worker_class_init (NFlickAuthWorkerClass *klass)
52 GObjectClass *gobjectclass = (GObjectClass *) klass;
53 NFlickWorkerClass *workerclass = (NFlickWorkerClass *) klass;
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;
59 g_object_class_install_property (gobjectclass, ARG_TOKEN,
61 ("token", "Token", "Unique flick full token",
62 NULL, G_PARAM_READABLE));
64 g_object_class_install_property (gobjectclass, ARG_USER_NAME,
66 ("username", "UserName", "Flickr user name",
67 NULL, G_PARAM_READABLE));
69 g_object_class_install_property (gobjectclass, ARG_FULL_NAME,
71 ("fullname", "FullName", "Flickr full user name",
72 NULL, G_PARAM_READABLE));
74 g_object_class_install_property (gobjectclass, ARG_USER_NSID,
76 ("usernsid", "UserNsid", "Unique nsid identyfying user in flickr",
77 NULL, G_PARAM_READABLE));
79 workerclass->ThreadFunc = (NFlickWorkerThreadFunc) thread_func;
81 ParentClass = g_type_class_ref (NFLICK_TYPE_WORKER);
84 static void nflick_auth_worker_init (NFlickAuthWorker *self)
86 g_return_if_fail (NFLICK_IS_AUTH_WORKER (self));
90 NFlickAuthWorkerPrivate *priv = g_new0 (NFlickAuthWorkerPrivate, 1);
91 g_return_if_fail (priv != NULL);
93 if (private_init (self, priv) == TRUE) {
95 nflick_worker_set_message ((NFlickWorker *) self, gettext ("Authorizing token..."));
97 private_dispose (priv);
103 static gboolean private_init (NFlickAuthWorker *self, NFlickAuthWorkerPrivate *private)
105 g_return_val_if_fail (NFLICK_IS_AUTH_WORKER (self), FALSE);
106 g_return_val_if_fail (private != NULL, FALSE);
108 private->MiniToken = NULL;
109 private->UserName = NULL;
110 private->FullName = NULL;
111 private->UserNsid = NULL;
112 private->Token = NULL;
117 static void private_dispose (NFlickAuthWorkerPrivate *private)
119 g_return_if_fail (private != NULL);
121 if (private->MiniToken != NULL) {
122 g_free (private->MiniToken);
123 private->MiniToken = NULL;
126 if (private->UserName != NULL) {
127 g_free (private->UserName);
128 private->UserName = NULL;
131 if (private->FullName != NULL) {
132 g_free (private->FullName);
133 private->FullName = NULL;
136 if (private->Token != NULL) {
137 g_free (private->Token);
138 private->Token = NULL;
141 if (private->UserNsid != NULL) {
142 g_free (private->UserNsid);
143 private->UserNsid = NULL;
147 static void nflick_auth_worker_dispose (NFlickAuthWorker *self)
149 g_return_if_fail (NFLICK_IS_AUTH_WORKER (self));
151 if (self->Private != NULL)
152 private_dispose (self->Private);
154 G_OBJECT_CLASS (ParentClass)->dispose (G_OBJECT (self));
157 static void nflick_auth_worker_finalize (NFlickAuthWorker *self)
159 g_return_if_fail (NFLICK_IS_AUTH_WORKER (self));
161 if (self->Private != NULL) {
162 g_free (self->Private);
163 self->Private = NULL;
166 G_OBJECT_CLASS (ParentClass)->finalize (G_OBJECT (self));
169 static NFlickWorkerStatus thread_func (NFlickAuthWorker *self)
171 NFlickApiRequest *full_token_request = NULL;
172 NFlickApiResponse *full_token_response = NULL;
173 NFlickWorkerStatus status = NFLICK_WORKER_STATUS_OK;
175 full_token_request = nflick_api_request_new (NFLICK_FLICKR_API_METHOD_GET_FULL_TOKEN);
176 if (full_token_request == NULL)
179 nflick_api_request_add_parameter (full_token_request,
180 NFLICK_FLICKR_API_PARAM_MINI_TOKEN,
181 self->Private->MiniToken);
183 nflick_api_request_sign (full_token_request);
185 if (nflick_api_request_exec (full_token_request) != TRUE) {
186 nflick_worker_set_network_error ((NFlickWorker *) self);
190 if (nflick_worker_is_aborted ((NFlickWorker *) self) == TRUE)
193 full_token_response = nflick_api_response_new_from_request (NFLICK_TYPE_GFT_RESPONSE, full_token_request);
194 if (full_token_response == NULL)
197 if (nflick_worker_parse_api_response ((NFlickWorker*) self, full_token_response) == FALSE)
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);
207 if (self->Private->UserName == NULL ||
208 self->Private->FullName == NULL ||
209 self->Private->Token == NULL ||
210 self->Private->UserNsid == NULL)
217 status = NFLICK_WORKER_STATUS_ABORTED;
221 status = NFLICK_WORKER_STATUS_ERROR;
224 if (full_token_request != NULL)
225 g_object_unref (full_token_request);
227 if (full_token_response != NULL)
228 g_object_unref (full_token_response);
233 NFlickAuthWorker* nflick_auth_worker_new (const gchar *minitoken)
235 g_return_val_if_fail (minitoken != NULL, NULL);
237 NFlickAuthWorker *self = g_object_new (NFLICK_TYPE_AUTH_WORKER, NULL);
238 g_return_val_if_fail (self != NULL, NULL);
240 if (self->Private == NULL) {
241 g_object_unref (self);
245 self->Private->MiniToken = g_strdup (minitoken);
250 static void nflick_auth_worker_get_property (NFlickAuthWorker *self, guint propid,
251 GValue *value, GParamSpec *pspec)
253 g_return_if_fail (NFLICK_IS_AUTH_WORKER (self));
254 g_assert (self->Private != NULL);
259 g_value_set_string (value, self->Private->UserName);
263 g_value_set_string (value, self->Private->FullName);
267 g_value_set_string (value, self->Private->Token);
271 g_value_set_string (value, self->Private->UserNsid);
275 G_OBJECT_WARN_INVALID_PROPERTY_ID (G_OBJECT (self), propid, pspec);