Fixed function name
[maevies] / src / mvs-tmdb-movie-service.c
1 /*
2  * mvs-tmdb-movie-service.c
3  *
4  * This file is part of maevies
5  * Copyright (C) 2010 Simón Pena <spenap@gmail.com>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License as
9  * published by the Free Software Foundation; either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  *
17  */
18
19 #include <dbus/dbus-glib-bindings.h>
20
21 #include "mvs-tmdb-movie-service.h"
22 #include "mvs-tmdb-image.h"
23
24 #define TMDB_MOVIE_SERVICE_OBJECT_PATH "/TMDBMovie"
25 #define TMDB_MOVIE_SERVICE_NAME "com.simonpena.maevies.tmdbmovie"
26
27 G_DEFINE_TYPE (MvsTmdbMovieService, mvs_tmdb_movie_service, G_TYPE_OBJECT)
28
29 enum {
30         PROP_0,
31         PROP_ID,
32         PROP_DBUSGCONN,
33 };
34
35 #define GET_PRIVATE(o) \
36         (G_TYPE_INSTANCE_GET_PRIVATE ((o), MVS_TYPE_TMDB_MOVIE_SERVICE, MvsTmdbMovieServicePrivate))
37
38 struct _MvsTmdbMovieServicePrivate {
39         MvsTmdbMovie *movie;
40         DBusGConnection *connection;
41         gchar *suffix;
42 };
43
44 gboolean
45 mvs_tmdb_movie_service_get_title (MvsTmdbMovieService *self, gchar **title,
46                                   GError **error)
47 {
48         *title = g_strdup (mvs_tmdb_movie_get_name (self->priv->movie));
49         return *title != NULL;
50 }
51
52 gboolean
53 mvs_tmdb_movie_service_get_popularity (MvsTmdbMovieService *self, gchar **popularity,
54                                        GError **error)
55 {
56         *popularity = g_strdup (mvs_tmdb_movie_get_popularity (self->priv->movie));
57         return *popularity != NULL;
58 }
59
60 gboolean
61 mvs_tmdb_movie_service_get_rating (MvsTmdbMovieService *self, gchar **rating)
62 {
63         *rating = g_strdup (mvs_tmdb_movie_get_rating (self->priv->movie));
64         return *rating != NULL;
65 }
66
67 gboolean
68 mvs_tmdb_movie_service_get_released (MvsTmdbMovieService *self, gchar **released)
69 {
70         *released = g_strdup (mvs_tmdb_movie_get_released (self->priv->movie));
71         return *released != NULL;
72 }
73
74 gboolean
75 mvs_tmdb_movie_service_get_overview (MvsTmdbMovieService *self, gchar **overview)
76 {
77         *overview = g_strdup (mvs_tmdb_movie_get_overview (self->priv->movie));
78         return *overview != NULL;
79 }
80
81 gboolean
82 mvs_tmdb_movie_service_get_images (MvsTmdbMovieService *self, GPtrArray **image_array, GError **error)
83 {
84         GList *iter = NULL;
85         *image_array = g_ptr_array_new ();
86
87         GList *movie_images = mvs_tmdb_movie_get_images (self->priv->movie);
88         for (iter = movie_images; iter; iter = iter->next) {
89
90                 GValueArray *image_properties = g_value_array_new (4);
91                 MvsTmdbImage *movie_image = MVS_TMDB_IMAGE (iter->data);
92
93                 GValue value = { 0 };
94                 g_value_init (&value, G_TYPE_STRING);
95
96                 g_value_set_string (&value,
97                                     mvs_tmdb_image_get_imagetype (movie_image));
98
99                 g_value_array_append (image_properties,
100                                       &value);
101
102                 g_value_set_string (&value,
103                                     mvs_tmdb_image_get_url (movie_image));
104
105                 g_value_array_append (image_properties,
106                                       &value);
107
108                 g_value_set_string (&value,
109                                     mvs_tmdb_image_get_size (movie_image));
110
111                 g_value_array_append (image_properties,
112                                       &value);
113
114                 g_value_set_string (&value,
115                                     mvs_tmdb_image_get_id (movie_image));
116
117                 g_value_array_append (image_properties,
118                                       &value);
119
120                 g_ptr_array_add (*image_array, image_properties);
121         }
122
123         return image_array != NULL;
124 }
125
126 #include "mvs-tmdb-movie-service-glue.h"
127
128 static void
129 setup_dbus (MvsTmdbMovieService *self)
130 {
131         DBusGProxy *proxy;
132         guint request_name_result;
133         GError *error = NULL;
134         gchar *object_path = NULL;
135
136         proxy = dbus_g_proxy_new_for_name (self->priv->connection,
137                                            DBUS_SERVICE_DBUS,
138                                            DBUS_PATH_DBUS,
139                                            DBUS_INTERFACE_DBUS);
140
141         if (!org_freedesktop_DBus_request_name (proxy,
142                                                 TMDB_MOVIE_SERVICE_NAME,
143                                                 0, &request_name_result,
144                                                 &error)) {
145                 g_warning ("Unable to register service: %s", error->message);
146                 g_error_free (error);
147         }
148
149         object_path = g_strdup_printf (TMDB_MOVIE_SERVICE_OBJECT_PATH "/%s",
150                         self->priv->suffix);
151
152         dbus_g_connection_register_g_object (self->priv->connection,
153                                              object_path,
154                                              G_OBJECT (self));
155
156         g_free (object_path);
157         g_object_unref (proxy);
158 }
159
160 static void
161 mvs_tmdb_movie_service_set_property (GObject *object, guint property_id,
162                                   const GValue *value, GParamSpec *pspec)
163 {
164         MvsTmdbMovieService *self = MVS_TMDB_MOVIE_SERVICE (object);
165
166         switch (property_id) {
167         case PROP_DBUSGCONN:
168                 if (!self->priv->connection) {
169                         DBusGConnection *tmp = g_value_get_pointer (value);
170                         if (tmp) {
171                                 self->priv->connection =
172                                         dbus_g_connection_ref (tmp);
173                                 setup_dbus (self);
174                         }
175                 }
176                 g_assert (self->priv->connection);
177                 break;
178         default:
179                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
180         }
181 }
182
183 static void
184 mvs_tmdb_movie_service_finalize (GObject *object)
185 {
186         MvsTmdbMovieService *self = MVS_TMDB_MOVIE_SERVICE (object);
187
188         if (self->priv->connection) {
189                 dbus_g_connection_unref (self->priv->connection);
190         }
191         g_free (self->priv->suffix);
192         g_object_unref (self->priv->movie);
193         G_OBJECT_CLASS (mvs_tmdb_movie_service_parent_class)->finalize (object);
194 }
195
196 static void
197 mvs_tmdb_movie_service_class_init (MvsTmdbMovieServiceClass *klass)
198 {
199         GObjectClass *object_class = G_OBJECT_CLASS (klass);
200
201         g_type_class_add_private (klass, sizeof (MvsTmdbMovieServicePrivate));
202
203         object_class->set_property = mvs_tmdb_movie_service_set_property;
204         object_class->finalize = mvs_tmdb_movie_service_finalize;
205
206         g_object_class_install_property
207                 (object_class, PROP_DBUSGCONN,
208                  g_param_spec_pointer ("connection", "DBusGConnection",
209                                        "DBus GConnection",
210                                        G_PARAM_WRITABLE));
211
212          dbus_g_object_type_install_info (MVS_TYPE_TMDB_MOVIE_SERVICE,
213                                           &dbus_glib_mvs_tmdb_movie_service_object_info);
214 }
215
216 static void
217 mvs_tmdb_movie_service_init (MvsTmdbMovieService *self)
218 {
219         self->priv = GET_PRIVATE (self);
220         self->priv->movie = NULL;
221         self->priv->connection = NULL;
222         self->priv->suffix = NULL;
223 }
224
225 MvsTmdbMovieService*
226 mvs_tmdb_movie_service_new (DBusGConnection *connection,
227                 MvsTmdbMovie *movie, const gchar *suffix)
228 {
229         MvsTmdbMovieService *instance = g_object_new (MVS_TYPE_TMDB_MOVIE_SERVICE, NULL);
230         instance->priv->suffix = g_strdup(suffix);
231         g_object_set (instance, "connection", connection, NULL);
232         instance->priv->movie = movie;
233         return instance;
234 }