summaryrefslogtreecommitdiff
path: root/gfbgraph/gfbgraph-photo.c
blob: b366d7bd0249508fd14c77e70b36d1842098113f (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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
/* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 8; tab-width: 8 -*-  */
/*
 * libgfbgraph - GObject library for Facebook Graph API
 * Copyright (C) 2013 Álvaro Peña <alvaropg@gmail.com>
 *
 * GFBGraph is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * GFBGraph 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with GFBGraph.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * SECTION:gfbgraph-photo
 * @short_description: GFBGraph Photo node
 * @stability: Unstable
 * @include: gfbgraph/gfbgraph.h
 *
 * #GFBGraphPhoto represents a <ulink url="https://developers.facebook.com/docs/reference/api/photo/">photo in the Facebook Graph API</ulink>.
 **/

#include "gfbgraph-photo.h"
#include "gfbgraph-connectable.h"
#include "gfbgraph-album.h"

#include <json-glib/json-glib.h>
#include <libsoup/soup.h>
#include <libsoup/soup-request.h>
#include <libsoup/soup-request-http.h>
#include <libsoup/soup-requester.h>

enum {
        PROP_0,

        PROP_NAME,
        PROP_SOURCE,
        PROP_HEIGHT,
        PROP_WIDTH,
        PROP_IMAGES
};

struct _GFBGraphPhotoPrivate {
        gchar              *name;
        gchar              *source;
        guint               width;
        guint               height;
        GList              *images;
        GFBGraphPhotoImage *hires_image;
};

static void gfbgraph_photo_init         (GFBGraphPhoto *obj);
static void gfbgraph_photo_class_init   (GFBGraphPhotoClass *klass);
static void gfbgraph_photo_finalize     (GObject *obj);
static void gfbgraph_photo_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
static void gfbgraph_photo_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);

static void gfbgraph_photo_connectable_iface_init     (GFBGraphConnectableInterface *iface);
GHashTable* gfbgraph_photo_get_connection_post_params (GFBGraphConnectable *self, GType node_type);

static void  gfbgraph_photo_serializable_iface_init           (JsonSerializableIface *iface);
JsonNode    *gfbgraph_photo_serializable_serialize_property   (JsonSerializable *serializable, const gchar *property_name, const GValue *value, GParamSpec *pspec);
gboolean     gfbgraph_photo_serializable_deserialize_property (JsonSerializable *serializable, const gchar *property_name, GValue *value, GParamSpec *pspec, JsonNode *property_node);
GParamSpec  *gfbgraph_photo_serializable_find_property        (JsonSerializable *serializable, const char *name);
GParamSpec **gfbgraph_photo_serializable_list_properties      (JsonSerializable *serializable, guint *n_pspecs);
void         gfbgraph_photo_serializable_set_property         (JsonSerializable *serializable, GParamSpec *pspec, const GValue *value);
void         gfbgraph_photo_serializable_get_property         (JsonSerializable *serializable, GParamSpec *pspec, GValue *value);

#define GFBGRAPH_PHOTO_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), GFBGRAPH_TYPE_PHOTO, GFBGraphPhotoPrivate))

static GFBGraphNodeClass *parent_class = NULL;

G_DEFINE_TYPE_WITH_CODE (GFBGraphPhoto, gfbgraph_photo, GFBGRAPH_TYPE_NODE,
                         G_IMPLEMENT_INTERFACE (GFBGRAPH_TYPE_CONNECTABLE, gfbgraph_photo_connectable_iface_init);
                         G_IMPLEMENT_INTERFACE (JSON_TYPE_SERIALIZABLE, gfbgraph_photo_serializable_iface_init););

static void
gfbgraph_photo_init (GFBGraphPhoto *obj)
{
        obj->priv = GFBGRAPH_PHOTO_GET_PRIVATE(obj);

        obj->priv->images = NULL;
}

static void
gfbgraph_photo_class_init (GFBGraphPhotoClass *klass)
{
        GObjectClass *gobject_class = G_OBJECT_CLASS (klass);

        parent_class            = g_type_class_peek_parent (klass);
        gobject_class->finalize = gfbgraph_photo_finalize;
        gobject_class->set_property = gfbgraph_photo_set_property;
        gobject_class->get_property = gfbgraph_photo_get_property;

        g_type_class_add_private (gobject_class, sizeof(GFBGraphPhotoPrivate));

        /**
         * GFBGraphPhoto:name:
         *
         * The name of the photo given by his owner.
         **/
        g_object_class_install_property (gobject_class,
                                         PROP_NAME,
                                         g_param_spec_string ("name",
                                                              "The photo name", "The name given by the user to the photo",
                                                              "",
                                                              G_PARAM_READABLE | G_PARAM_WRITABLE));

        /**
         * GFBGraphPhoto:source:
         *
         * An URI for the photo, with a maximum width or height of 720px.
         **/
        g_object_class_install_property (gobject_class,
                                         PROP_SOURCE,
                                         g_param_spec_string ("source",
                                                              "The URI for the photo", "The URI for the photo, with a maximum width or height of 720px",
                                                              "",
                                                              G_PARAM_READABLE | G_PARAM_WRITABLE));

        /**
         * GFBGraphPhoto:width:
         *
         * The default photo width, up to 720px.
         **/
        g_object_class_install_property (gobject_class,
                                         PROP_WIDTH,
                                         g_param_spec_uint ("width",
                                                            "Photo width", "The photo width",
                                                            0, G_MAXUINT, 0,
                                                            G_PARAM_READABLE | G_PARAM_WRITABLE));

        /**
         * GFBGraphPhoto:height:
         *
         * The default photo height, up to 720px.
         **/
        g_object_class_install_property (gobject_class,
                                         PROP_HEIGHT,
                                         g_param_spec_uint ("height",
                                                            "Photo height", "The photo height",
                                                            0, G_MAXUINT, 0,
                                                            G_PARAM_READABLE | G_PARAM_WRITABLE));

        /**
         * GFBGraphPhoto:images:
         *
         * A list with the available representations of the photo, in differents sizes
         **/
        g_object_class_install_property (gobject_class,
                                         PROP_IMAGES,
                                         g_param_spec_pointer ("images",
                                                               "Sizes of the photo", "The diffents sizes available of the photo",
                                                               G_PARAM_READABLE | G_PARAM_WRITABLE));
}

static void
gfbgraph_photo_finalize (GObject *obj)
{
        GFBGraphPhotoPrivate *priv;
        GList *images;
        GFBGraphPhotoImage *photo_image;

        priv = GFBGRAPH_PHOTO_GET_PRIVATE (obj);

        images = priv->images;
        while (images) {
                photo_image = (GFBGraphPhotoImage *) images->data;

                g_free (photo_image->source);
                g_free (photo_image);

                images = g_list_next (images);
        }

        g_free (priv->name);
        g_free (priv->source);
        g_list_free (priv->images);

        G_OBJECT_CLASS(parent_class)->finalize (obj);
}

static void
gfbgraph_photo_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
{
        GFBGraphPhotoPrivate *priv;

        priv = GFBGRAPH_PHOTO_GET_PRIVATE (object);

        switch (prop_id) {
                case PROP_NAME:
                        if (priv->name)
                                g_free (priv->name);
                        priv->name = g_strdup (g_value_get_string (value));
                        break;
                case PROP_SOURCE:
                        if (priv->source)
                                g_free (priv->source);
                        priv->source = g_strdup (g_value_get_string (value));
                        break;
                case PROP_WIDTH:
                        priv->width = g_value_get_uint (value);
                        break;
                case PROP_HEIGHT:
                        priv->height = g_value_get_uint (value);
                        break;
                case PROP_IMAGES:
                        /* TODO: Free GList memory with g_list_free_full */
                        priv->images = g_value_get_pointer (value);
                        break;
                default:
                        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
                        break;
        }
}

static void
gfbgraph_photo_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
{
        GFBGraphPhotoPrivate *priv;

        priv = GFBGRAPH_PHOTO_GET_PRIVATE (object);

        switch (prop_id) {
                case PROP_NAME:
                        g_value_set_string (value, priv->name);
                        break;
                case PROP_SOURCE:
                        g_value_set_string (value, priv->source);
                        break;
                case PROP_WIDTH:
                        g_value_set_uint (value, priv->width);
                        break;
                case PROP_HEIGHT:
                        g_value_set_uint (value, priv->height);
                        break;
                case PROP_IMAGES:
                        g_value_set_pointer (value, priv->images);
                default:
                        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
                        break;
        }
}

static void
gfbgraph_photo_connectable_iface_init (GFBGraphConnectableInterface *iface)
{
        GHashTable *connections;

        connections = g_hash_table_new (g_str_hash, g_str_equal);
        g_hash_table_insert (connections, (gpointer) g_type_name (GFBGRAPH_TYPE_ALBUM), (gpointer) "photos");

        iface->connections = connections;
        iface->get_connection_post_params = gfbgraph_photo_get_connection_post_params;
        iface->parse_connected_data = gfbgraph_connectable_default_parse_connected_data;
}

GHashTable*
gfbgraph_photo_get_connection_post_params (GFBGraphConnectable *self, GType node_type)
{
        GHashTable *params;
        GFBGraphPhotoPrivate *priv;

        priv = GFBGRAPH_PHOTO_GET_PRIVATE (self);

        params = g_hash_table_new (g_str_hash, g_str_equal);
        g_hash_table_insert (params, "message", priv->name);
        /* TODO: Incorpate the "source" param (multipart/form-data) */

        return params;
}

static void
gfbgraph_photo_serializable_iface_init (JsonSerializableIface *iface)
{
        iface->serialize_property   = gfbgraph_photo_serializable_serialize_property;
        iface->deserialize_property = gfbgraph_photo_serializable_deserialize_property;
        iface->find_property        = gfbgraph_photo_serializable_find_property;
        iface->list_properties      = gfbgraph_photo_serializable_list_properties;
        iface->set_property         = gfbgraph_photo_serializable_set_property;
        iface->get_property         = gfbgraph_photo_serializable_get_property;
}

JsonNode *
gfbgraph_photo_serializable_serialize_property (JsonSerializable *serializable, const gchar *property_name, const GValue *value, GParamSpec *pspec)
{
        JsonNode *node = NULL;

        g_print ("Serializing %s\n", property_name);

        if (g_strcmp0 ("images", property_name) == 0) {
        } else {
                node = json_serializable_default_serialize_property (serializable, property_name, value, pspec);
        }

        return node;
}

gboolean
gfbgraph_photo_serializable_deserialize_property (JsonSerializable *serializable, const gchar *property_name, GValue *value, GParamSpec *pspec, JsonNode *property_node)
{
        gboolean res;

        if (g_strcmp0 ("images", property_name) == 0) {
                if (JSON_NODE_HOLDS_ARRAY (property_node)) {
                        guint i, num_images;
                        JsonArray *jarray;
                        GList *images;

                        images = NULL;
                        jarray = json_node_get_array (property_node);
                        num_images = json_array_get_length (jarray);
                        for (i = 0; i < num_images; i++) {
                                JsonObject *image_object;
                                GFBGraphPhotoImage *photo_image;

                                image_object = json_array_get_object_element (jarray, i);
                                photo_image = g_new0 (GFBGraphPhotoImage, 1);
                                photo_image->width = json_object_get_int_member (image_object, "width");
                                photo_image->height = json_object_get_int_member (image_object, "height");
                                photo_image->source = g_strdup (json_object_get_string_member (image_object, "source"));

                                images = g_list_append (images, photo_image);
                        }

                        g_value_set_pointer (value, (gpointer *) images);
                        res = TRUE;
                } else {
                        g_warning ("The 'images' node retrieved from the Facebook Graph API isn't an array, it's holding a %s\n", json_node_type_name (property_node));
                        res = FALSE;
                }
        } else {
                res = json_serializable_default_deserialize_property (serializable, property_name, value, pspec, property_node);
        }

        return res;
}

GParamSpec*
gfbgraph_photo_serializable_find_property (JsonSerializable *serializable, const char *name)
{
        return g_object_class_find_property (G_OBJECT_GET_CLASS (GFBGRAPH_PHOTO (serializable)), name);
}

GParamSpec**
gfbgraph_photo_serializable_list_properties (JsonSerializable *serializable, guint *n_pspecs)
{
        return g_object_class_list_properties (G_OBJECT_GET_CLASS (GFBGRAPH_PHOTO (serializable)), n_pspecs);
}

void
gfbgraph_photo_serializable_set_property (JsonSerializable *serializable, GParamSpec *pspec, const GValue *value)
{
        g_object_set_property (G_OBJECT (serializable), g_param_spec_get_name (pspec), value);
}

void
gfbgraph_photo_serializable_get_property (JsonSerializable *serializable, GParamSpec *pspec, GValue *value)
{
        g_object_get_property (G_OBJECT (serializable), g_param_spec_get_name (pspec), value);
}

/**
 * gfbgraph_photo_new:
 *
 * Creates a new #GFBGraphPhoto.
 *
 * Returns: (transfer full): a new #GFBGraphPhoto; unref with g_object_unref()
 **/
GFBGraphPhoto*
gfbgraph_photo_new (void)
{
        return GFBGRAPH_PHOTO(g_object_new(GFBGRAPH_TYPE_PHOTO, NULL));
}

/**
 * gfbgraph_photo_new_from_id:
 * @authorizer: a #GFBGraphAuthorizer.
 * @id: a const #gchar with the photo ID.
 * @error: (allow-none): a #GError or %NULL.
 *
 * Retrieves an photo node from the Facebook Graph with the give ID.
 *
 * Returns: (transfer full): a new #GFBGraphPhoto; unref with g_object_unref()
 **/
GFBGraphPhoto*
gfbgraph_photo_new_from_id (GFBGraphAuthorizer *authorizer, const gchar *id, GError **error)
{
        return GFBGRAPH_PHOTO (gfbgraph_node_new_from_id (authorizer, id, GFBGRAPH_TYPE_PHOTO, error));
}


/**
 * gfbgraph_photo_download_default_size:
 * @photo: a #GFBGraphPhoto.
 * @authorizer: a #GFBGraphAuthorizer.
 * @error: (allow-none) a #GError or %NULL.
 *
 * Download the default sized photo pointed by @photo, with a maximum width or height of 720px.
 * The photo always is a JPEG.
 *
 * Returns: (transfer full): a #GInputStream with the photo content or %NULL in case of error.
 **/
GInputStream*
gfbgraph_photo_download_default_size (GFBGraphPhoto *photo, GFBGraphAuthorizer *authorizer, GError **error)
{
        GInputStream *stream = NULL;
        SoupSession *session;
        SoupRequester *requester;
        SoupRequest *request;
        SoupMessage *message;
        GFBGraphPhotoPrivate *priv;

        g_return_val_if_fail (GFBGRAPH_IS_PHOTO (photo), NULL);
        g_return_val_if_fail (GFBGRAPH_IS_AUTHORIZER (authorizer), NULL);

        priv = GFBGRAPH_PHOTO_GET_PRIVATE (photo);

        session = soup_session_sync_new ();
        requester = soup_requester_new ();
        soup_session_add_feature (session, SOUP_SESSION_FEATURE (requester));

        request = soup_requester_request (requester, priv->source, error);
        if (request != NULL) {
                message = soup_request_http_get_message (SOUP_REQUEST_HTTP (request));

                stream = soup_request_send (request, NULL, error);
                if (stream != NULL) {
                        g_object_weak_ref (G_OBJECT (stream), (GWeakNotify) g_object_unref, session);
                }

                g_clear_object (&message);
                g_clear_object (&request);
        }

        g_clear_object (&requester);

        return stream;
}

/**
 * gfbgraph_photo_get_name:
 * @photo: a #GFBGraphPhoto.
 *
 * Returns: (transfer none): the @photo name, which is the comment given by the user so it would by larger, or %NULL.
 **/
const gchar*
gfbgraph_photo_get_name (GFBGraphPhoto *photo)
{
        g_return_val_if_fail (GFBGRAPH_IS_PHOTO (photo), NULL);

        return photo->priv->name;
}

/**
 * gfbgraph_photo_get_source_uri:
 * @photo: a #GFBGraphPhoto.
 *
 * Returns: (transfer none): the image link with a maximun widht or height of 720px
 **/
const gchar*
gfbgraph_photo_get_default_source_uri (GFBGraphPhoto *photo)
{
        g_return_val_if_fail (GFBGRAPH_IS_PHOTO (photo), NULL);

        return photo->priv->source;
}

/**
 * gfbgraph_pohto_get_default_width:
 * @photo: a #GFBGraphPhoto.
 *
 * Returns: (transfer none): the default photo width, up to 720px.
 **/
guint
gfbgraph_photo_get_default_width (GFBGraphPhoto *photo)
{
        g_return_val_if_fail (GFBGRAPH_IS_PHOTO (photo), 0);

        return photo->priv->width;
}

/**
 * gfbgraph_pohto_get_default_height:
 * @photo: a #GFBGraphPhoto.
 *
 * Returns: (transfer none): the default photo height, up to 720px.
 **/
guint
gfbgraph_photo_get_default_height (GFBGraphPhoto *photo)
{
        g_return_val_if_fail (GFBGRAPH_IS_PHOTO (photo), 0);

        return photo->priv->height;
}

/**
 * gfbgraph_photo_get_images:
 * @photo: a #GFBGraphPhoto.
 *
 * Returns: (transfer none): a #GList of #GFBGraphPhotoImage with the available photo sizes
 **/
GList*
gfbgraph_photo_get_images (GFBGraphPhoto *photo)
{
        g_return_val_if_fail (GFBGRAPH_IS_PHOTO (photo), NULL);

        return photo->priv->images;
}

/**
 * gfbgraph_photo_get_image_hires:
 * @photo: a #GFBGraphPhoto.
 *
 * Returns: (transfer none): a #GFBGraphPhotoImage with the higher resolution available of the photo
 **/
GFBGraphPhotoImage*
gfbgraph_photo_get_image_hires (GFBGraphPhoto *photo)
{
        g_return_val_if_fail (GFBGRAPH_IS_PHOTO (photo), NULL);

        if (photo->priv->hires_image == NULL) {
                GList *images_list;
                guint bigger_width;
                GFBGraphPhotoImage *photo_image;

                bigger_width = 0;
                images_list = photo->priv->images;
                while (images_list) {
                        photo_image = (GFBGraphPhotoImage *) images_list->data;
                        if (photo_image->width > bigger_width) {
                                photo->priv->hires_image = photo_image;
                                bigger_width = photo_image->width;
                        }

                        images_list = g_list_next (images_list);
                }
        }

        return photo->priv->hires_image;
}

GFBGraphPhotoImage*
gfbgraph_photo_get_image_near_width (GFBGraphPhoto *photo, guint width)
{
        GList *images_list;
        GFBGraphPhotoImage *tmp_photo_image;
        GFBGraphPhotoImage *photo_image;
        gint tmp_w_dif, w_dif;

        g_return_val_if_fail (GFBGRAPH_IS_PHOTO (photo), NULL);

        photo_image = NULL;
        images_list = photo->priv->images;
        while (images_list) {
                tmp_photo_image = (GFBGraphPhotoImage *) images_list->data;
                tmp_w_dif = tmp_photo_image->width - width;
                tmp_w_dif = (tmp_w_dif > 0) ? tmp_w_dif : (tmp_w_dif * -1);

                if (photo_image == NULL
                    || tmp_w_dif < w_dif) {
                        w_dif = tmp_w_dif;
                        photo_image = tmp_photo_image;
                } else {
                }

                images_list = g_list_next (images_list);
        }

        return photo_image;
}

GFBGraphPhotoImage*
gfbgraph_photo_get_image_near_height (GFBGraphPhoto *photo, guint height)
{
        GList *images_list;
        GFBGraphPhotoImage *tmp_photo_image;
        GFBGraphPhotoImage *photo_image;
        gint tmp_h_dif, h_dif;

        g_return_val_if_fail (GFBGRAPH_IS_PHOTO (photo), NULL);

        photo_image = NULL;
        images_list = photo->priv->images;
        while (images_list) {
                tmp_photo_image = (GFBGraphPhotoImage *) images_list->data;
                tmp_h_dif = ABS(tmp_photo_image->height - height);

                if (photo_image == NULL
                    || tmp_h_dif < h_dif) {
                        h_dif = tmp_h_dif;
                        photo_image = tmp_photo_image;
                }

                images_list = g_list_next (images_list);
        }

        return photo_image;
}