Improved project build
[maevies] / src / mvs-minfo-provider-service.c
1 /*
2  * mvs-minfo-provider-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-minfo-provider-service.h"
22 #include "mvs-minfo-provider.h"
23 #include "mvs-tmdb-movie-service.h"
24 #include "mvs-tmdb-movie.h"
25 #include "mvs-watc-movie-service.h"
26 #include "mvs-watc-movie.h"
27 #include "mvs-marshal.h"
28
29 #define MINFO_PROVIDER_SERVICE_OBJECT_PATH "/MInfoProvider"
30 #define MINFO_PROVIDER_SERVICE_NAME "com.simonpena.maevies.minfoprovider"
31 #define TMDB_MOVIE_INTERFACE "com.simonpena.maevies.tmdbmovie"
32 #define WATC_MOVIE_INTERFACE "com.simonpena.maevies.watcmovie"
33
34 G_DEFINE_TYPE (MvsMInfoProviderService, mvs_minfo_provider_service, G_TYPE_OBJECT)
35
36 enum {
37         PROP_0,
38         PROP_DBUSGCONN,
39 };
40
41 enum {
42         RESPONSE_RECEIVED,
43         LAST_SIGNAL
44 };
45
46 static guint
47 mvs_minfo_provider_service_signals[LAST_SIGNAL] = { 0 };
48
49 #define GET_PRIVATE(o) \
50         (G_TYPE_INSTANCE_GET_PRIVATE ((o), MVS_TYPE_MINFO_PROVIDER_SERVICE, MvsMInfoProviderServicePrivate))
51
52 struct _MvsMInfoProviderServicePrivate {
53         MvsMInfoProvider *minfo_provider;
54         DBusGConnection *connection;
55         guint search_id;
56 };
57
58 gboolean
59 mvs_minfo_provider_service_query (MvsMInfoProviderService *self,
60                                   MvsService service,
61                                   const gchar *query,
62                                   GError **error)
63 {
64         return mvs_minfo_provider_query (self->priv->minfo_provider,
65                         service, query);
66 }
67
68 #include "mvs-minfo-provider-service-glue.h"
69
70 static void
71 response_received_cb (MvsMInfoProvider *provider, guint service, GList *list,
72                       gpointer user_data)
73 {
74         MvsMInfoProviderService *self = MVS_MINFO_PROVIDER_SERVICE (user_data);
75         GList *iter = NULL;
76         gchar **object_paths= g_new0 (gchar*, g_list_length (list) + 1);
77         gchar *movie_interface = NULL;
78         guint i = 0;
79
80         movie_interface = service == MVS_SERVICE_TMDB ?
81                         g_strdup (TMDB_MOVIE_INTERFACE):
82                         g_strdup (WATC_MOVIE_INTERFACE);
83
84         /* TODO We left the movie var unused to keep a warning
85          * remembering that DBus objects aren't being freed
86          * when they aren't needed anymore */
87         for (iter = list; iter; iter = iter->next) {
88                 if (MVS_IS_TMDB_MOVIE (iter->data)) {
89                         MvsTmdbMovie *tmdb_movie = MVS_TMDB_MOVIE (iter->data);
90                         gchar *uid_suffix = g_strdup_printf ("%d_%s",
91                                         self->priv->search_id,
92                                         mvs_tmdb_movie_get_id (tmdb_movie));
93
94                         MvsTmdbMovieService *movie = mvs_tmdb_movie_service_new (
95                                         self->priv->connection,
96                                         tmdb_movie, uid_suffix);
97                         object_paths[i] = g_strdup_printf ("/TMDBMovie/%s",
98                                         uid_suffix);
99                         g_free (uid_suffix);
100                 }
101                 else if (MVS_IS_WATC_MOVIE (iter->data)) {
102                         MvsWatcMovie *watc_movie = MVS_WATC_MOVIE (iter->data);
103                         gchar *uid_suffix = g_strdup_printf ("%d_%d",
104                                         self->priv->search_id,
105                                         i);
106                         MvsWatcMovieService *movie = mvs_watc_movie_service_new (
107                                         self->priv->connection,
108                                         watc_movie, uid_suffix);
109                         object_paths[i] = g_strdup_printf ("/WATCMovie/%s",
110                                         uid_suffix);
111                         g_free (uid_suffix);
112                 }
113                 i++;
114         }
115         object_paths[i] = NULL;
116
117         g_signal_emit (self, mvs_minfo_provider_service_signals[RESPONSE_RECEIVED],
118                        0, movie_interface, object_paths);
119         g_strfreev (object_paths);
120         g_free (movie_interface);
121         self->priv->search_id++;
122 }
123
124 static void
125 setup_dbus (MvsMInfoProviderService *self)
126 {
127         DBusGProxy *proxy;
128         guint request_name_result;
129         GError *error = NULL;
130
131         proxy = dbus_g_proxy_new_for_name (self->priv->connection,
132                                            DBUS_SERVICE_DBUS,
133                                            DBUS_PATH_DBUS,
134                                            DBUS_INTERFACE_DBUS);
135
136         if (!org_freedesktop_DBus_request_name (proxy,
137                                                 MINFO_PROVIDER_SERVICE_NAME,
138                                                 0, &request_name_result,
139                                                 &error)) {
140                 g_warning ("Unable to register service: %s", error->message);
141                 g_error_free (error);
142         }
143
144         dbus_g_connection_register_g_object (self->priv->connection,
145                                              MINFO_PROVIDER_SERVICE_OBJECT_PATH,
146                                              G_OBJECT (self));
147
148         g_object_unref (proxy);
149 }
150
151 static void
152 mvs_minfo_provider_service_set_property (GObject *object, guint property_id,
153                                   const GValue *value, GParamSpec *pspec)
154 {
155         MvsMInfoProviderService *self = MVS_MINFO_PROVIDER_SERVICE (object);
156
157         switch (property_id) {
158         case PROP_DBUSGCONN:
159                 if (!self->priv->connection) {
160                         DBusGConnection *tmp = g_value_get_pointer (value);
161                         if (tmp) {
162                                 self->priv->connection =
163                                         dbus_g_connection_ref (tmp);
164                                 setup_dbus (self);
165                         }
166                 }
167                 g_assert (self->priv->connection);
168                 break;
169         default:
170                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
171         }
172 }
173
174 static void
175 mvs_minfo_provider_service_finalize (GObject *object)
176 {
177         MvsMInfoProviderService *self = MVS_MINFO_PROVIDER_SERVICE (object);
178
179         if (self->priv->connection) {
180                 dbus_g_connection_unref (self->priv->connection);
181         }
182         g_object_unref (self->priv->minfo_provider);
183         G_OBJECT_CLASS (mvs_minfo_provider_service_parent_class)->finalize (object);
184 }
185
186 static void
187 mvs_minfo_provider_service_class_init (MvsMInfoProviderServiceClass *klass)
188 {
189         GObjectClass *object_class = G_OBJECT_CLASS (klass);
190
191         g_type_class_add_private (klass, sizeof (MvsMInfoProviderServicePrivate));
192
193         object_class->set_property = mvs_minfo_provider_service_set_property;
194         object_class->finalize = mvs_minfo_provider_service_finalize;
195
196         g_object_class_install_property
197                 (object_class, PROP_DBUSGCONN,
198                  g_param_spec_pointer ("connection", "DBusGConnection",
199                                        "DBus GConnection",
200                                        G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
201
202          dbus_g_object_type_install_info (MVS_TYPE_MINFO_PROVIDER_SERVICE,
203                                           &dbus_glib_mvs_minfo_provider_service_object_info);
204
205          mvs_minfo_provider_service_signals[RESPONSE_RECEIVED] =
206                          g_signal_new ("response-received",
207                          G_TYPE_FROM_CLASS (klass),
208                          G_SIGNAL_RUN_LAST,
209                          0,
210                          NULL,
211                          NULL,
212                          mvs_marshal_VOID__STRING_POINTER,
213                          G_TYPE_NONE,
214                          2,
215                          G_TYPE_STRING,
216                          G_TYPE_STRV,
217                          NULL);
218 }
219
220 static void
221 mvs_minfo_provider_service_init (MvsMInfoProviderService *self)
222 {
223         self->priv = GET_PRIVATE (self);
224         self->priv->minfo_provider = mvs_minfo_provider_new ();
225         self->priv->connection = NULL;
226         self->priv->search_id = 0;
227
228         g_signal_connect (self->priv->minfo_provider, "response-received",
229                           G_CALLBACK (response_received_cb), self);
230 }
231
232 MvsMInfoProviderService*
233 mvs_minfo_provider_service_new (DBusGConnection *connection)
234 {
235         return g_object_new (MVS_TYPE_MINFO_PROVIDER_SERVICE,
236                              "connection", connection, NULL);
237 }