summaryrefslogtreecommitdiff
path: root/attic/aaina/libnflick/nflick-info-worker.c
blob: 9c211d226eab66355b993ecb518ecb2d9970c217 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/******************************************************************************/
/*                                                                            */
/* GPL license, Copyright (c) 2005-2006 by:                                   */
/*                                                                            */
/* Authors:                                                                   */
/*      Michael Dominic K. <michaldominik@gmail.com>                          */
/*                                                                            */
/* This program is free software; you can redistribute it and/or modify it    */
/* under the terms of the GNU General Public License as published by the      */
/* Free Software Foundation; either version 2, or (at your option) any later  */
/* version.                                                                   */
/*                                                                            */
/* This program is distributed in the hope that it will be useful, but        */
/* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY */
/* or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License   */
/* for more details.                                                          */
/*                                                                            */
/* You should have received a copy of the GNU General Public License along    */
/* with this program; if not, write to the Free Software Foundation, Inc., 59 */
/* Temple Place - Suite 330, Boston, MA 02111-1307, USA.                      */
/*                                                                            */
/******************************************************************************/

#include "nflick-info-worker.h"
#include "nflick-info-worker-private.h"

GType                           
nflick_info_worker_get_type (void)
{
        static GType objecttype = 0;

        if (!objecttype) {

                static const GTypeInfo objectinfo = {
                        sizeof (NFlickInfoWorkerClass), 
                        NULL, 
                        NULL,
                        (GClassInitFunc) nflick_info_worker_class_init,
                        NULL,
                        NULL, 
                        sizeof (NFlickInfoWorker), 
                        4, 
                        (GInstanceInitFunc) nflick_info_worker_init,
                };
                objecttype = g_type_register_static (NFLICK_TYPE_WORKER, "NFlickInfoWorker",
                                                     &objectinfo, 0);
        }
        return objecttype;
}

static void                     
nflick_info_worker_class_init (NFlickInfoWorkerClass *klass)
{
        GObjectClass *gobjectclass = (GObjectClass *) klass;
        NFlickWorkerClass *workerclass = (NFlickWorkerClass *) klass;

        gobjectclass->dispose = (gpointer) nflick_info_worker_dispose;
        gobjectclass->finalize = (gpointer) nflick_info_worker_finalize;
        gobjectclass->get_property = (gpointer) nflick_info_worker_get_property;

        g_object_class_install_property (gobjectclass, ARG_PIXBUF,
                                         g_param_spec_object 
                                         ("pixbuf", "Pixbuf", "Pixbuf",
                                         GDK_TYPE_PIXBUF, G_PARAM_READABLE));
        
        workerclass->ThreadFunc = (NFlickWorkerThreadFunc) thread_func;

        ParentClass = g_type_class_ref (NFLICK_TYPE_WORKER);
}

static void                     
nflick_info_worker_init (NFlickInfoWorker *self)
{
        g_return_if_fail (NFLICK_IS_INFO_WORKER (self));

        self->Private = NULL;

        NFlickInfoWorkerPrivate *priv = g_new0 (NFlickInfoWorkerPrivate, 1);
        g_return_if_fail (priv != NULL);
        
        if (private_init (self, priv) == TRUE) {
                self->Private = priv;
                nflick_worker_set_message ((NFlickWorker *) self, 
                			    gettext ("Loading photo..."));
        } else {
                private_dispose (priv);
                g_free (priv);
                self->Private = NULL;
        }
}

static gboolean                 
private_init (NFlickInfoWorker *self, NFlickInfoWorkerPrivate *private)
{
        g_return_val_if_fail (NFLICK_IS_INFO_WORKER (self), FALSE);
        g_return_val_if_fail (private != NULL, FALSE);

        private->PhotoId = NULL;
        private->Token = NULL;

        return TRUE;
}

static void                     
private_dispose (NFlickInfoWorkerPrivate *private)
{
        g_return_if_fail (private != NULL);

        if (private->Token != NULL) {
                g_free (private->Token);
                private->Token = NULL;
        }

        if (private->PhotoId != NULL) {
                g_free (private->PhotoId);
                private->PhotoId = NULL;
        }

        if (private->Pixbuf != NULL) {
                g_object_unref (private->Pixbuf);
                private->Pixbuf = NULL;
        }
}

static void                     
nflick_info_worker_dispose (NFlickInfoWorker *self)
{
        g_return_if_fail (NFLICK_IS_INFO_WORKER (self));

        if (self->Private != NULL)
                private_dispose (self->Private);

        G_OBJECT_CLASS (ParentClass)->dispose (G_OBJECT (self));
}

static void                    
nflick_info_worker_finalize (NFlickInfoWorker *self)
{
        g_return_if_fail (NFLICK_IS_INFO_WORKER (self));
        
        if (self->Private != NULL) {
                g_free (self->Private);
                self->Private = NULL;
        }

        G_OBJECT_CLASS (ParentClass)->finalize (G_OBJECT (self));
}

static NFlickWorkerStatus       
thread_func (NFlickInfoWorker *self)
{
        NFlickApiRequest *get_sizes_request = NULL; 
        NFlickApiResponse *get_sizes_response = NULL;
        gchar *uri = NULL;
        NFlickWorkerStatus status = NFLICK_WORKER_STATUS_OK;
        gdouble vbox_aspect = (gdouble) self->Private->Width / (gdouble) self->Private->Height;
        gdouble pixbuf_aspect = -1;
        gint32 final_width = -1;
        gint32 final_height = -1;
        gboolean rotated = FALSE;

        get_sizes_request = nflick_api_request_new ("flickr.photos.getinfo");
        if (get_sizes_request == NULL)
                goto Error;

        nflick_api_request_add_parameter (get_sizes_request, 
                                          NFLICK_FLICKR_API_PARAM_PHOTO_ID, 
                                          self->Private->PhotoId);

        nflick_api_request_sign (get_sizes_request);
        if (nflick_api_request_exec (get_sizes_request) != TRUE) 
        {
                nflick_worker_set_network_error ((NFlickWorker *) self);
                goto Error;
        }

        if (nflick_worker_is_aborted ((NFlickWorker *) self) == TRUE)
                goto Abort;

        get_sizes_response = 
              nflick_api_response_new_from_request (NFLICK_TYPE_INFO_RESPONSE, 
                                                    get_sizes_request);

        if (get_sizes_response == NULL)
                goto Error;

        if (nflick_worker_parse_api_response ((NFlickWorker*) self, 
                                              get_sizes_response) == FALSE)
                goto Error;
        
        nflick_info_response_get ((NFlickInfoResponse*)get_sizes_response,
                                  &self->Private->rotation,
                                  &self->Private->realname,
                                  &self->Private->desc);

        /* All ok */
        goto Done;

Abort:
        status = NFLICK_WORKER_STATUS_ABORTED;
        goto Done;

Error:
        status = NFLICK_WORKER_STATUS_ERROR;

Done:
        if (get_sizes_request != NULL) 
                g_object_unref (get_sizes_request);

        if (get_sizes_response != NULL) 
                g_object_unref (get_sizes_response);

        if (uri != NULL)
                g_free (uri);

        return status;
}

void
nflick_info_worker_get (NFlickInfoWorker    *self,
                        gchar              **rotation,
                        gchar              **realname,
                        gchar              **desc)
{
  g_return_if_fail (NFLICK_IS_INFO_WORKER (self));

  *rotation = self->Private->rotation;
  *realname = self->Private->realname;
  *desc = self->Private->desc;
}

NFlickInfoWorker*               
nflick_info_worker_new (const gchar *photoid, gint32 width, gint32 height, const gchar *token)
{
        g_return_val_if_fail (token != NULL, NULL);
        g_return_val_if_fail (photoid != NULL, NULL);

        NFlickInfoWorker *self = g_object_new (NFLICK_TYPE_INFO_WORKER, NULL);
        g_return_val_if_fail (self != NULL, NULL);

        if (self->Private == NULL) {
                g_object_unref (self);
                return NULL;
        }

        self->Private->Token = g_strdup (token);
        self->Private->PhotoId= g_strdup (photoid);
        self->Private->Width = width;
        self->Private->Height = height;

        return self;
}

static void                     
nflick_info_worker_get_property (NFlickInfoWorker *self, guint propid, 
                                                                 GValue *value, GParamSpec *pspec)
{
        g_return_if_fail (NFLICK_IS_INFO_WORKER (self));
        g_assert (self->Private != NULL);
                
        switch (propid) {
               
                case ARG_PIXBUF:
                        g_value_set_object (value, self->Private->Pixbuf);
                break;

                case ARG_ROTATION:

 
                default:
                        G_OBJECT_WARN_INVALID_PROPERTY_ID (G_OBJECT (self), propid, pspec);
                break;

        }
}