ee82f3b5ef17218f8b94ec9e0705f5913a5fd633
[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         GError *error = NULL;
76         GList *iter = NULL;
77         gchar  **object_paths= g_new0 (gchar*, g_list_length (list) + 1);
78         gchar *movie_interface = NULL;
79         guint i = 0;
80
81         movie_interface = service == MVS_SERVICE_TMDB ?
82                         g_strdup (TMDB_MOVIE_INTERFACE):
83                         g_strdup (WATC_MOVIE_INTERFACE);
84
85         for (iter = list; iter; iter = iter->next) {
86                 if (MVS_IS_TMDB_MOVIE (iter->data)) {
87                         MvsTmdbMovie *tmdb_movie = MVS_TMDB_MOVIE (iter->data);
88                         gchar *uid_suffix = g_strdup_printf ("%d_%s",
89                                         self->priv->search_id,
90                                         mvs_tmdb_movie_get_id (tmdb_movie));
91
92                         MvsTmdbMovieService *movie = mvs_tmdb_movie_service_new (
93                                         self->priv->connection,
94                                         tmdb_movie, uid_suffix);
95                         object_paths[i] = g_strdup_printf ("/TMDBMovie/%s",
96                                         uid_suffix);
97                         g_free (uid_suffix);
98                 }
99                 else if (MVS_IS_WATC_MOVIE (iter->data)) {
100                         MvsWatcMovie *watc_movie = MVS_WATC_MOVIE (iter->data);
101                         gchar *uid_suffix = g_strdup_printf ("%d_%d",
102                                         self->priv->search_id,
103                                         i);
104                         MvsWatcMovieService *movie = mvs_watc_movie_service_new (
105                                         self->priv->connection,
106                                         watc_movie, uid_suffix);
107                         object_paths[i] = g_strdup_printf ("/WATCMovie/%s",
108                                         uid_suffix);
109                         g_free (uid_suffix);
110                 }
111                 i++;
112         }
113         object_paths[i] = NULL;
114
115         g_signal_emit (self, mvs_minfo_provider_service_signals[RESPONSE_RECEIVED],
116                        0, movie_interface, object_paths);
117         g_strfreev (object_paths);
118         g_free (movie_interface);
119         self->priv->search_id++;
120 }
121
122 static void
123 setup_dbus (MvsMInfoProviderService *self)
124 {
125         DBusGProxy *proxy;
126         guint request_name_result;
127         GError *error = NULL;
128
129         proxy = dbus_g_proxy_new_for_name (self->priv->connection,
130                                            DBUS_SERVICE_DBUS,
131                                            DBUS_PATH_DBUS,
132                                            DBUS_INTERFACE_DBUS);
133
134         if (!org_freedesktop_DBus_request_name (proxy,
135                                                 MINFO_PROVIDER_SERVICE_NAME,
136                                                 0, &request_name_result,
137                                                 &error)) {
138                 g_warning ("Unable to register service: %s", error->message);
139                 g_error_free (error);
140         }
141
142         dbus_g_connection_register_g_object (self->priv->connection,
143                                              MINFO_PROVIDER_SERVICE_OBJECT_PATH,
144                                              G_OBJECT (self));
145
146         g_object_unref (proxy);
147 }
148
149 static void
150 mvs_minfo_provider_service_set_property (GObject *object, guint property_id,
151                                   const GValue *value, GParamSpec *pspec)
152 {
153         MvsMInfoProviderService *self = MVS_MINFO_PROVIDER_SERVICE (object);
154
155         switch (property_id) {
156         case PROP_DBUSGCONN:
157                 if (!self->priv->connection) {
158                         DBusGConnection *tmp = g_value_get_pointer (value);
159                         if (tmp) {
160                                 self->priv->connection =
161                                         dbus_g_connection_ref (tmp);
162                                 setup_dbus (self);
163                         }
164                 }
165                 g_assert (self->priv->connection);
166                 break;
167         default:
168                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
169         }
170 }
171
172 static void
173 mvs_minfo_provider_service_finalize (GObject *object)
174 {
175         MvsMInfoProviderService *self = MVS_MINFO_PROVIDER_SERVICE (object);
176
177         if (self->priv->connection) {
178                 dbus_g_connection_unref (self->priv->connection);
179         }
180         g_object_unref (self->priv->minfo_provider);
181         G_OBJECT_CLASS (mvs_minfo_provider_service_parent_class)->finalize (object);
182 }
183
184 static void
185 mvs_minfo_provider_service_class_init (MvsMInfoProviderServiceClass *klass)
186 {
187         GObjectClass *object_class = G_OBJECT_CLASS (klass);
188
189         g_type_class_add_private (klass, sizeof (MvsMInfoProviderServicePrivate));
190
191         object_class->set_property = mvs_minfo_provider_service_set_property;
192         object_class->finalize = mvs_minfo_provider_service_finalize;
193
194         g_object_class_install_property
195                 (object_class, PROP_DBUSGCONN,
196                  g_param_spec_pointer ("connection", "DBusGConnection",
197                                        "DBus GConnection",
198                                        G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
199
200          dbus_g_object_type_install_info (MVS_TYPE_MINFO_PROVIDER_SERVICE,
201                                           &dbus_glib_mvs_minfo_provider_service_object_info);
202
203          mvs_minfo_provider_service_signals[RESPONSE_RECEIVED] =
204                          g_signal_new ("response-received",
205                          G_TYPE_FROM_CLASS (klass),
206                          G_SIGNAL_RUN_LAST,
207                          0,
208                          NULL,
209                          NULL,
210                          mvs_marshal_VOID__STRING_POINTER,
211                          G_TYPE_NONE,
212                          2,
213                          G_TYPE_STRING,
214                          G_TYPE_STRV,
215                          NULL);
216 }
217
218 static void
219 mvs_minfo_provider_service_init (MvsMInfoProviderService *self)
220 {
221         self->priv = GET_PRIVATE (self);
222         self->priv->minfo_provider = mvs_minfo_provider_new ();
223         self->priv->connection = NULL;
224         self->priv->search_id = 0;
225
226         g_signal_connect (self->priv->minfo_provider, "response-received",
227                           G_CALLBACK (response_received_cb), self);
228 }
229
230 MvsMInfoProviderService*
231 mvs_minfo_provider_service_new (DBusGConnection *connection)
232 {
233         return g_object_new (MVS_TYPE_MINFO_PROVIDER_SERVICE,
234                              "connection", connection, NULL);
235 }