minfo-dataprovider: Added dataprovider service
[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
24 #define MINFO_PROVIDER_SERVICE_OBJECT_PATH "/MInfoProvider"
25 #define MINFO_PROVIDER_SERVICE_NAME "com.simonpena.maevies.minfoprovider"
26
27 G_DEFINE_TYPE (MvsMInfoProviderService, mvs_minfo_provider_service, G_TYPE_OBJECT)
28
29 enum {
30         PROP_0,
31         PROP_DBUSGCONN,
32 };
33
34 enum {
35         RESPONSE_RECEIVED,
36         LAST_SIGNAL
37 };
38
39 static guint
40 mvs_minfo_provider_service_signals[LAST_SIGNAL] = { 0 };
41
42 #define GET_PRIVATE(o) \
43         (G_TYPE_INSTANCE_GET_PRIVATE ((o), MVS_TYPE_MINFO_PROVIDER_SERVICE, MvsMInfoProviderServicePrivate))
44
45 struct _MvsMInfoProviderServicePrivate {
46         MvsMInfoProvider *minfo_provider;
47         DBusGConnection *connection;
48 };
49
50 gboolean
51 mvs_minfo_provider_service_query (MvsMInfoProviderService *self,
52                                   MvsService service,
53                                   const gchar *query,
54                                   GError **error)
55 {
56         return mvs_minfo_provider_query (self->priv->minfo_provider,
57                         service, query);
58 }
59
60 #include "mvs-minfo-provider-service-glue.h"
61
62 static void
63 response_received_cb (MvsMInfoProvider *provider, gpointer response,
64                       gpointer user_data)
65 {
66         MvsMInfoProviderService *self = MVS_MINFO_PROVIDER_SERVICE (user_data);
67         g_signal_emit (self, mvs_minfo_provider_service_signals[RESPONSE_RECEIVED], 0);
68 }
69
70 static void
71 setup_dbus (MvsMInfoProviderService *self)
72 {
73         DBusGProxy *proxy;
74         guint request_name_result;
75         GError *error = NULL;
76
77         proxy = dbus_g_proxy_new_for_name (self->priv->connection,
78                                            DBUS_SERVICE_DBUS,
79                                            DBUS_PATH_DBUS,
80                                            DBUS_INTERFACE_DBUS);
81
82         if (!org_freedesktop_DBus_request_name (proxy,
83                                                 MINFO_PROVIDER_SERVICE_NAME,
84                                                 0, &request_name_result,
85                                                 &error)) {
86                 g_warning ("Unable to register service: %s", error->message);
87                 g_error_free (error);
88         }
89
90         dbus_g_connection_register_g_object (self->priv->connection,
91                                              MINFO_PROVIDER_SERVICE_OBJECT_PATH,
92                                              G_OBJECT (self));
93
94         g_object_unref (proxy);
95 }
96
97 static void
98 mvs_minfo_provider_service_set_property (GObject *object, guint property_id,
99                                   const GValue *value, GParamSpec *pspec)
100 {
101         MvsMInfoProviderService *self = MVS_MINFO_PROVIDER_SERVICE (object);
102
103         switch (property_id) {
104         case PROP_DBUSGCONN:
105                 if (!self->priv->connection) {
106                         DBusGConnection *tmp = g_value_get_pointer (value);
107                         if (tmp) {
108                                 self->priv->connection =
109                                         dbus_g_connection_ref (tmp);
110                                 setup_dbus (self);
111                         }
112                 }
113                 g_assert (self->priv->connection);
114                 break;
115         default:
116                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
117         }
118 }
119
120 static void
121 mvs_minfo_provider_service_finalize (GObject *object)
122 {
123         MvsMInfoProviderService *self = MVS_MINFO_PROVIDER_SERVICE (object);
124
125         if (self->priv->connection) {
126                 dbus_g_connection_unref (self->priv->connection);
127         }
128         g_object_unref (self->priv->minfo_provider);
129         G_OBJECT_CLASS (mvs_minfo_provider_service_parent_class)->finalize (object);
130 }
131
132 static void
133 mvs_minfo_provider_service_class_init (MvsMInfoProviderServiceClass *klass)
134 {
135         GObjectClass *object_class = G_OBJECT_CLASS (klass);
136
137         g_type_class_add_private (klass, sizeof (MvsMInfoProviderServicePrivate));
138
139         object_class->set_property = mvs_minfo_provider_service_set_property;
140         object_class->finalize = mvs_minfo_provider_service_finalize;
141
142         g_object_class_install_property
143                 (object_class, PROP_DBUSGCONN,
144                  g_param_spec_pointer ("connection", "DBusGConnection",
145                                        "DBus GConnection",
146                                        G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
147
148          dbus_g_object_type_install_info (MVS_TYPE_MINFO_PROVIDER_SERVICE,
149                                           &dbus_glib_mvs_minfo_provider_service_object_info);
150
151          mvs_minfo_provider_service_signals[RESPONSE_RECEIVED] =
152                          g_signal_new ("response-received",
153                          G_TYPE_FROM_CLASS (klass),
154                          G_SIGNAL_RUN_LAST,
155                          0,
156                          NULL,
157                          NULL,
158                          g_cclosure_marshal_VOID__VOID,
159                          G_TYPE_NONE,
160                          0,
161                          NULL);
162 }
163
164 static void
165 mvs_minfo_provider_service_init (MvsMInfoProviderService *self)
166 {
167         self->priv = GET_PRIVATE (self);
168         self->priv->minfo_provider = mvs_minfo_provider_new ();
169         self->priv->connection = NULL;
170
171         g_signal_connect (self->priv->minfo_provider, "response-received",
172                           G_CALLBACK (response_received_cb), self);
173 }
174
175 MvsMInfoProviderService*
176 mvs_minfo_provider_service_new (DBusGConnection *connection)
177 {
178         return g_object_new (MVS_TYPE_MINFO_PROVIDER_SERVICE,
179                              "connection", connection, NULL);
180 }