Added maevies service support
[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                                    GError **error)
63 {
64         *rating = g_strdup (mvs_tmdb_movie_get_rating (self->priv->movie));
65         return *rating != NULL;
66 }
67
68 gboolean
69 mvs_tmdb_movie_service_get_released (MvsTmdbMovieService *self, gchar **released,
70                                      GError **error)
71 {
72         *released = g_strdup (mvs_tmdb_movie_get_released (self->priv->movie));
73         return *released != NULL;
74 }
75
76 gboolean
77 mvs_tmdb_movie_service_get_overview (MvsTmdbMovieService *self, gchar **overview,
78                                      GError **error)
79 {
80         *overview = g_strdup (mvs_tmdb_movie_get_overview (self->priv->movie));
81         return *overview != NULL;
82 }
83
84 gboolean
85 mvs_tmdb_movie_service_get_images (MvsTmdbMovieService *self, GPtrArray **image_array,
86                                    GError **error)
87 {
88         GList *iter = NULL;
89         *image_array = g_ptr_array_new ();
90
91         GList *movie_images = mvs_tmdb_movie_get_images (self->priv->movie);
92         for (iter = movie_images; iter; iter = iter->next) {
93
94                 GValueArray *image_properties = g_value_array_new (4);
95                 MvsTmdbImage *movie_image = MVS_TMDB_IMAGE (iter->data);
96
97                 GValue value = { 0 };
98                 g_value_init (&value, G_TYPE_STRING);
99
100                 g_value_set_string (&value,
101                                     mvs_tmdb_image_get_imagetype (movie_image));
102
103                 g_value_array_append (image_properties,
104                                       &value);
105
106                 g_value_set_string (&value,
107                                     mvs_tmdb_image_get_url (movie_image));
108
109                 g_value_array_append (image_properties,
110                                       &value);
111
112                 g_value_set_string (&value,
113                                     mvs_tmdb_image_get_size (movie_image));
114
115                 g_value_array_append (image_properties,
116                                       &value);
117
118                 g_value_set_string (&value,
119                                     mvs_tmdb_image_get_id (movie_image));
120
121                 g_value_array_append (image_properties,
122                                       &value);
123
124                 g_ptr_array_add (*image_array, image_properties);
125         }
126
127         return image_array != NULL;
128 }
129
130 #include "mvs-tmdb-movie-service-glue.h"
131
132 static void
133 setup_dbus (MvsTmdbMovieService *self)
134 {
135         DBusGProxy *proxy;
136         guint request_name_result;
137         GError *error = NULL;
138         gchar *object_path = NULL;
139
140         proxy = dbus_g_proxy_new_for_name (self->priv->connection,
141                                            DBUS_SERVICE_DBUS,
142                                            DBUS_PATH_DBUS,
143                                            DBUS_INTERFACE_DBUS);
144
145         if (!org_freedesktop_DBus_request_name (proxy,
146                                                 TMDB_MOVIE_SERVICE_NAME,
147                                                 0, &request_name_result,
148                                                 &error)) {
149                 g_warning ("Unable to register service: %s", error->message);
150                 g_error_free (error);
151         }
152
153         object_path = g_strdup_printf (TMDB_MOVIE_SERVICE_OBJECT_PATH "/%s",
154                         self->priv->suffix);
155
156         dbus_g_connection_register_g_object (self->priv->connection,
157                                              object_path,
158                                              G_OBJECT (self));
159
160         g_free (object_path);
161         g_object_unref (proxy);
162 }
163
164 static void
165 mvs_tmdb_movie_service_set_property (GObject *object, guint property_id,
166                                   const GValue *value, GParamSpec *pspec)
167 {
168         MvsTmdbMovieService *self = MVS_TMDB_MOVIE_SERVICE (object);
169
170         switch (property_id) {
171         case PROP_DBUSGCONN:
172                 if (!self->priv->connection) {
173                         DBusGConnection *tmp = g_value_get_pointer (value);
174                         if (tmp) {
175                                 self->priv->connection =
176                                         dbus_g_connection_ref (tmp);
177                                 setup_dbus (self);
178                         }
179                 }
180                 g_assert (self->priv->connection);
181                 break;
182         default:
183                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
184         }
185 }
186
187 static void
188 mvs_tmdb_movie_service_finalize (GObject *object)
189 {
190         MvsTmdbMovieService *self = MVS_TMDB_MOVIE_SERVICE (object);
191
192         if (self->priv->connection) {
193                 dbus_g_connection_unref (self->priv->connection);
194         }
195         g_free (self->priv->suffix);
196         g_object_unref (self->priv->movie);
197         G_OBJECT_CLASS (mvs_tmdb_movie_service_parent_class)->finalize (object);
198 }
199
200 static void
201 mvs_tmdb_movie_service_class_init (MvsTmdbMovieServiceClass *klass)
202 {
203         GObjectClass *object_class = G_OBJECT_CLASS (klass);
204
205         g_type_class_add_private (klass, sizeof (MvsTmdbMovieServicePrivate));
206
207         object_class->set_property = mvs_tmdb_movie_service_set_property;
208         object_class->finalize = mvs_tmdb_movie_service_finalize;
209
210         g_object_class_install_property
211                 (object_class, PROP_DBUSGCONN,
212                  g_param_spec_pointer ("connection", "DBusGConnection",
213                                        "DBus GConnection",
214                                        G_PARAM_WRITABLE));
215
216          dbus_g_object_type_install_info (MVS_TYPE_TMDB_MOVIE_SERVICE,
217                                           &dbus_glib_mvs_tmdb_movie_service_object_info);
218 }
219
220 static void
221 mvs_tmdb_movie_service_init (MvsTmdbMovieService *self)
222 {
223         self->priv = GET_PRIVATE (self);
224         self->priv->movie = NULL;
225         self->priv->connection = NULL;
226         self->priv->suffix = NULL;
227 }
228
229 MvsTmdbMovieService*
230 mvs_tmdb_movie_service_new (DBusGConnection *connection,
231                 MvsTmdbMovie *movie, const gchar *suffix)
232 {
233         MvsTmdbMovieService *instance = g_object_new (MVS_TYPE_TMDB_MOVIE_SERVICE, NULL);
234         instance->priv->suffix = g_strdup(suffix);
235         g_object_set (instance, "connection", connection, NULL);
236         instance->priv->movie = movie;
237         return instance;
238 }