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-photo-list-worker.h"
25 #include "nflick-photo-list-worker-private.h"
27 GType nflick_photo_list_worker_get_type (void)
29 static GType objecttype = 0;
33 static const GTypeInfo objectinfo = {
34 sizeof (NFlickPhotoListWorkerClass),
37 (GClassInitFunc) nflick_photo_list_worker_class_init,
40 sizeof (NFlickPhotoListWorker),
42 (GInstanceInitFunc) nflick_photo_list_worker_init,
44 objecttype = g_type_register_static (NFLICK_TYPE_WORKER, "NFlickPhotoListWorker",
50 static void nflick_photo_list_worker_class_init (NFlickPhotoListWorkerClass *klass)
52 GObjectClass *gobjectclass = (GObjectClass *) klass;
53 NFlickWorkerClass *workerclass = (NFlickWorkerClass *) klass;
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;
59 workerclass->ThreadFunc = (NFlickWorkerThreadFunc) thread_func;
61 ParentClass = g_type_class_ref (NFLICK_TYPE_WORKER);
64 static void nflick_photo_list_worker_init (NFlickPhotoListWorker *self)
66 g_return_if_fail (NFLICK_IS_PHOTO_LIST_WORKER (self));
70 NFlickPhotoListWorkerPrivate *priv = g_new0 (NFlickPhotoListWorkerPrivate, 1);
71 g_return_if_fail (priv != NULL);
73 if (private_init (self, priv) == TRUE) {
75 nflick_worker_set_message ((NFlickWorker *) self, gettext ("Loading photoset data..."));
77 private_dispose (priv);
83 static gboolean private_init (NFlickPhotoListWorker *self, NFlickPhotoListWorkerPrivate *private)
85 g_return_val_if_fail (NFLICK_IS_PHOTO_LIST_WORKER (self), FALSE);
86 g_return_val_if_fail (private != NULL, FALSE);
89 private->Token = NULL;
90 private->PhotoDataList = NULL;
95 static void private_dispose (NFlickPhotoListWorkerPrivate *private)
97 g_return_if_fail (private != NULL);
99 if (private->Id != NULL) {
100 g_free (private->Id);
104 if (private->Token != NULL) {
105 g_free (private->Token);
106 private->Token = NULL;
109 if (private->PhotoDataList != NULL) {
113 for (iterator = private->PhotoDataList; iterator; iterator = g_list_next (iterator))
114 if (iterator->data != NULL)
115 nflick_photo_data_free ((NFlickPhotoData *) iterator->data);
117 g_list_free (private->PhotoDataList);
118 private->PhotoDataList = NULL;
122 static void nflick_photo_list_worker_dispose (NFlickPhotoListWorker *self)
124 g_return_if_fail (NFLICK_IS_PHOTO_LIST_WORKER (self));
126 if (self->Private != NULL)
127 private_dispose (self->Private);
129 G_OBJECT_CLASS (ParentClass)->dispose (G_OBJECT (self));
132 static void nflick_photo_list_worker_finalize (NFlickPhotoListWorker *self)
134 g_return_if_fail (NFLICK_IS_PHOTO_LIST_WORKER (self));
136 if (self->Private != NULL) {
137 g_free (self->Private);
138 self->Private = NULL;
141 G_OBJECT_CLASS (ParentClass)->finalize (G_OBJECT (self));
144 static NFlickWorkerStatus thread_func (NFlickPhotoListWorker *self)
146 NFlickApiRequest *get_photolist_request = NULL;
147 NFlickWorkerStatus status = NFLICK_WORKER_STATUS_OK;
148 NFlickApiResponse *photo_list_response = NULL;
150 get_photolist_request = nflick_api_request_new (NFLICK_FLICKR_API_METHOD_PHOTOSETS_GET_PHOTOS);
151 if (get_photolist_request == NULL)
154 nflick_api_request_add_parameter (get_photolist_request,
155 NFLICK_FLICKR_API_PARAM_TOKEN,
156 self->Private->Token);
158 nflick_api_request_add_parameter (get_photolist_request,
159 NFLICK_FLICKR_API_PARAM_PHOTOSET_ID,
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);
168 if (nflick_worker_is_aborted ((NFlickWorker *) self) == TRUE)
171 photo_list_response = nflick_api_response_new_from_request (NFLICK_TYPE_PHOTO_LIST_RESPONSE, get_photolist_request);
172 if (photo_list_response == NULL)
175 if (nflick_worker_parse_api_response ((NFlickWorker*) self, photo_list_response) == FALSE)
178 self->Private->PhotoDataList = nflick_photo_list_response_take_list ((NFlickPhotoListResponse *) photo_list_response);
184 status = NFLICK_WORKER_STATUS_ABORTED;
188 status = NFLICK_WORKER_STATUS_ERROR;
191 if (get_photolist_request != NULL)
192 g_object_unref (get_photolist_request);
194 if (photo_list_response != NULL)
195 g_object_unref (photo_list_response);
200 NFlickPhotoListWorker* nflick_photo_list_worker_new (const gchar *id, const gchar *token)
202 g_return_val_if_fail (id != NULL, NULL);
204 NFlickPhotoListWorker *self = g_object_new (NFLICK_TYPE_PHOTO_LIST_WORKER, NULL);
205 g_return_val_if_fail (self != NULL, NULL);
207 if (self->Private == NULL) {
208 g_object_unref (self);
212 self->Private->Id = g_strdup (id);
213 self->Private->Token = g_strdup (token);
218 static void nflick_photo_list_worker_get_property (NFlickPhotoListWorker *self, guint propid,
219 GValue *value, GParamSpec *pspec)
221 g_return_if_fail (NFLICK_IS_PHOTO_LIST_WORKER (self));
222 g_assert (self->Private != NULL);
227 G_OBJECT_WARN_INVALID_PROPERTY_ID (G_OBJECT (self), propid, pspec);
232 GList* nflick_photo_list_worker_take_list (NFlickPhotoListWorker *self)
234 g_return_val_if_fail (NFLICK_IS_PHOTO_LIST_WORKER (self), NULL);
236 GList *lst = self->Private->PhotoDataList;
237 self->Private->PhotoDataList = NULL;