summaryrefslogtreecommitdiff
path: root/gfbgraph/gfbgraph-album.c
blob: 8f8cec402126e715282b56c8ea9b11d8821623a6 (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
/* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 2; tab-width: 2 -*-  */
/*
 * libgfbgraph - GObject library for Facebook Graph API
 * Copyright (C) 2013 Álvaro Peña <alvaropg@gmail.com>
 *               2020 Leesoo Ahn <yisooan@fedoraproject.org>
 *
 * 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-album
 * @short_description: GFBGraph Photo album node object
 * @stability: Unstable
 * @include: gfbgraph/gfbgraph.h
 *
 * #GFGraphAlbum represents the <ulink url="https://developers.facebook.com/docs/reference/api/album/">
 * photo album node in the Graph API</ulink>.
 *
 * This node is connectable to:
 *  - #GFBGraphUser
 **/

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

enum {
  PROP_O,
  PROP_NAME,
  PROP_DESCRIPTION,
  PROP_COVER_PHOTO,
  PROP_COUNT
};

struct _GFBGraphAlbumPrivate {
  gchar *name;
  gchar *description;
  gchar *cover_photo;
  guint  count;
};

#define GFBGRAPH_ALBUM_GET_PRIVATE(o) \
  (G_TYPE_INSTANCE_GET_PRIVATE((o), GFBGRAPH_TYPE_ALBUM, GFBGraphAlbumPrivate))

static GFBGraphNodeClass *parent_class = NULL;

static void connectable_iface_init (GFBGraphConnectableInterface *iface);

G_DEFINE_TYPE_WITH_CODE (GFBGraphAlbum, gfbgraph_album, GFBGRAPH_TYPE_NODE,
  G_IMPLEMENT_INTERFACE (GFBGRAPH_TYPE_CONNECTABLE, connectable_iface_init));

static void
gfbgraph_album_finalize (GObject *obj)
{
  GFBGraphAlbumPrivate *priv = GFBGRAPH_ALBUM_GET_PRIVATE (obj);

  if (priv->name)
    g_free (priv->name);
  if (priv->description)
    g_free (priv->description);
  if (priv->cover_photo)
    g_free (priv->cover_photo);

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

static void
gfbgraph_album_set_property (GObject      *object,
                             guint         prop_id,
                             const GValue *value,
                             GParamSpec   *pspec)
{
  GFBGraphAlbumPrivate *priv = GFBGRAPH_ALBUM_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_DESCRIPTION:
      if (priv->description)
        g_free (priv->description);
      priv->description = g_strdup (g_value_get_string (value));
      break;
    case PROP_COVER_PHOTO:
      if (priv->cover_photo)
        g_free (priv->cover_photo);
      priv->cover_photo = g_strdup (g_value_get_string (value));
      break;
    case PROP_COUNT:
      priv->count = g_value_get_uint (value);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static void
gfbgraph_album_get_property (GObject    *object,
                             guint       prop_id,
                             GValue     *value,
                             GParamSpec *pspec)
{
  GFBGraphAlbumPrivate *priv = GFBGRAPH_ALBUM_GET_PRIVATE (object);

  switch (prop_id) {
    case PROP_NAME:
      g_value_set_string (value, priv->name);
      break;
    case PROP_DESCRIPTION:
      g_value_set_string (value, priv->description);
      break;
    case PROP_COVER_PHOTO:
      g_value_set_string (value, priv->cover_photo);
      break;
    case PROP_COUNT:
      g_value_set_uint (value, priv->count);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static void
gfbgraph_album_class_init (GFBGraphAlbumClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);

  parent_class            = g_type_class_peek_parent (klass);

  gobject_class->finalize = gfbgraph_album_finalize;
  gobject_class->set_property = gfbgraph_album_set_property;
  gobject_class->get_property = gfbgraph_album_get_property;

  g_type_class_add_private (gobject_class, sizeof(GFBGraphAlbumPrivate));

  /**
   * GFBGraphAlbum:name:
   *
   * The album name.
   **/
  g_object_class_install_property (gobject_class,
                                   PROP_NAME,
                                   g_param_spec_string ("name",
                                                        "The title",
                                                        "The name of the album",
                                                        "",
                                                        G_PARAM_READABLE | G_PARAM_WRITABLE));

  /**
   * GFBGraphAlbum:description:
   *
   * The album description given by the owner.
   **/
  g_object_class_install_property (gobject_class,
                                   PROP_DESCRIPTION,
                                   g_param_spec_string ("description",
                                                        "The description",
                                                        "The description of the album",
                                                        "",
                                                        G_PARAM_READABLE | G_PARAM_WRITABLE));

  /**
   * GFBGraphAlbum:cover_photo:
   *
   * The node ID for the album cover photo. It's an ID for a #GFBGraphPhoto node.
   **/
  g_object_class_install_property (gobject_class,
                                   PROP_COVER_PHOTO,
                                   g_param_spec_string ("cover_photo",
                                                        "Cover photo",
                                                        "The ID for the cover photo of the album",
                                                        "",
                                                        G_PARAM_READABLE | G_PARAM_WRITABLE));

  /**
   * GFBGraphAlbum:count:
   *
   * The number of photos in the album.
   **/
  g_object_class_install_property (gobject_class,
                                   PROP_COUNT,
                                   g_param_spec_uint ("count",
                                                      "Number of photos",
                                                      "The number of photos in the album",
                                                      0, G_MAXUINT, 0,
                                                      G_PARAM_READABLE | G_PARAM_WRITABLE));

}

static void
gfbgraph_album_init (GFBGraphAlbum *obj)
{
  obj->priv = GFBGRAPH_ALBUM_GET_PRIVATE(obj);
}

static GHashTable *
get_connection_post_params (GFBGraphConnectable *self,
                            GType                node_type)
{
  GHashTable *params;
  GFBGraphAlbumPrivate *priv = GFBGRAPH_ALBUM_GET_PRIVATE (self);

  params = g_hash_table_new (g_str_hash, g_str_equal);
  g_hash_table_insert (params, "name", priv->name);
  if (priv->description != NULL)
    g_hash_table_insert (params, "message", priv->description);
  /* TODO: Incorpate the "privacy" param */

  return params;
}

static void
connectable_iface_init (GFBGraphConnectableInterface *iface)
{
  GHashTable *connections = g_hash_table_new (g_str_hash, g_str_equal);

  g_hash_table_insert (connections,
                       (gpointer) g_type_name (GFBGRAPH_TYPE_USER),
                       (gpointer) "albums");

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

/**
 * gfbgraph_album_new:
 *
 * Creates a new #GFBGraphAlbum.
 *
 * Returns: (transfer full): a new #GFBGraphAlbum; unref with g_object_unref()
 **/
GFBGraphAlbum *
gfbgraph_album_new (void)
{
  return GFBGRAPH_ALBUM (g_object_new (GFBGRAPH_TYPE_ALBUM, NULL));
}

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

/**
 * gfbgraph_album_get_name:
 * @album: a #GFBGraphAlbum.
 *
 * Returns: (transfer none): the @album name, or %NULL.
 **/
const gchar *
gfbgraph_album_get_name (GFBGraphAlbum *album)
{
  g_return_val_if_fail (GFBGRAPH_IS_ALBUM (album), NULL);

  return album->priv->name;
}

/**
 * gfbgraph_album_get_description:
 * @album: a #GFBGraphAlbum.
 *
 * Returns: (transfer none): the @album description, or %NULL.
 **/
const gchar *
gfbgraph_album_get_description (GFBGraphAlbum *album)
{
  g_return_val_if_fail (GFBGRAPH_IS_ALBUM (album), NULL);

  return album->priv->description;
}

/**
 * gfbgraph_album_get_cover_photo:
 * @album: a #GFBGraphAlbum.
 *
 * Returns: (transfer none): the cover photo node ID or %NULL.
 **/
const gchar *
gfbgraph_album_get_cover_photo_id (GFBGraphAlbum *album)
{
  g_return_val_if_fail (GFBGRAPH_IS_ALBUM (album), NULL);

  return album->priv->cover_photo;
}

/**
 * gfbgraph_album_get_count:
 * @album: a #GFBGraphAlbum.
 *
 * Returns: (transfer none): the number of photos into the @album or -1 in case of error.
 **/
guint
gfbgraph_album_get_count (GFBGraphAlbum *album)
{
  g_return_val_if_fail (GFBGRAPH_IS_ALBUM (album), -1);

  return album->priv->count;
}

/**
 * gfbgraph_album_set_name:
 * @album: a #GFBGraphAlbum.
 * @name: a const pointer to a #gchar.
 *
 * Sets the name for the @album.
 **/
void
gfbgraph_album_set_name (GFBGraphAlbum *album,
                         const gchar   *name)
{
  g_return_if_fail (GFBGRAPH_IS_ALBUM (album));
  g_return_if_fail (name != NULL);

  g_object_set (G_OBJECT (album),
                "name", name,
                NULL);
}

/**
 * gfbgraph_album_set_description:
 * @album: a #GFBGraphAlbum.
 * @description: a const pointer to a #gchar.
 *
 * Sets the description for the @album.
 **/
void
gfbgraph_album_set_description (GFBGraphAlbum *album,
                                const gchar   *description)
{
  g_return_if_fail (GFBGRAPH_IS_ALBUM (album));
  g_return_if_fail (description != NULL);

  g_object_set (G_OBJECT (album),
                "description", description,
                NULL);
}