summaryrefslogtreecommitdiff
path: root/src/lib/properties.c
blob: 8b913d5650e3feac24bac76d0c98bbc62fc99f3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#include <gio/gio.h>
#include "dbus-common.h"
#include "properties.h"

struct _PropertiesPrivate
{
    GDBusProxy *proxy;
    
    // a{sa{sv}}
    GVariant *dict_interfaces;
    
    gchar *dbus_type;
    gchar *dbus_service_name;
    gchar *dbus_object_path;
};

G_DEFINE_TYPE_WITH_PRIVATE (Properties, properties, G_TYPE_OBJECT)

enum
{
    PROP_0,

    PROP_DBUS_TYPE,
    PROP_DBUS_SERVICE_NAME,
    PROP_DBUS_OBJECT_PATH,

    N_PROPERTIES
};

/* Keep a pointer to the properties definition */
static GParamSpec *obj_properties[N_PROPERTIES] = { NULL, };

/*
 * Private method definitions
 */
static void _properties_create_gdbus_proxy(Properties *self, GError **error);

/*
 * Methods
 */
static void _properties_set_property(GObject *object, guint property_id, const GValue *value, GParamSpec *pspec)
{
    Properties *self = PROPERTIES(object);
    GError *error = NULL;

    switch (property_id)
    {
//    case PROP_EXAMPLE:
//        g_free(self->priv->example);
//        self->priv->example = g_value_dup_string(value);
//        g_print("example: %s\n", self->priv->example);
//        break;

    case PROP_DBUS_TYPE:
        if(self->priv->dbus_type)
            g_free(self->priv->dbus_type);
        self->priv->dbus_type = g_value_dup_string(value);
        // _properties_set_dbus_object_path(self, g_value_dup_string(value), &error);
        _properties_create_gdbus_proxy(self, &error);
        break;
        
    case PROP_DBUS_SERVICE_NAME:
        if(self->priv->dbus_service_name)
            g_free(self->priv->dbus_service_name);
        self->priv->dbus_service_name = g_value_dup_string(value);
        // _properties_set_dbus_object_path(self, g_value_dup_string(value), &error);
        _properties_create_gdbus_proxy(self, &error);
        break;
        
    case PROP_DBUS_OBJECT_PATH:
        if(self->priv->dbus_object_path)
            g_free(self->priv->dbus_object_path);
        self->priv->dbus_object_path = g_value_dup_string(value);
        // _properties_set_dbus_object_path(self, g_value_dup_string(value), &error);
        _properties_create_gdbus_proxy(self, &error);
        break;

    default:
        /* We don't have any other property... */
        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
        break;
    }

    g_assert(error == NULL);
}

static void _properties_get_property(GObject *object, guint property_id, GValue *value, GParamSpec *pspec)
{
    Properties *self = PROPERTIES(object);

    switch (property_id)
    {
//    case PROP_EXAMPLE:
//        g_value_set_string(value, self->priv->example);
//        break;

    case PROP_DBUS_TYPE:
        g_value_set_string(value, properties_get_dbus_type(self));
        break;
        
    case PROP_DBUS_SERVICE_NAME:
        g_value_set_string(value, properties_get_dbus_service_name(self));
        break;
        
    case PROP_DBUS_OBJECT_PATH:
        g_value_set_string(value, properties_get_dbus_object_path(self));
        break;

    default:
        /* We don't have any other property... */
        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
        break;
    }
}

static void properties_dispose (GObject *gobject)
{
    Properties *self = PROPERTIES (gobject);

    /* In dispose(), you are supposed to free all types referenced from this
     * object which might themselves hold a reference to self. Generally,
     * the most simple solution is to unref all members on which you own a 
     * reference.
     */

    /* dispose() might be called multiple times, so we must guard against
     * calling g_object_unref() on an invalid GObject by setting the member
     * NULL; g_clear_object() does this for us, atomically.
     */
    // g_clear_object (&self->priv->an_object);
    g_clear_object (&self->priv->proxy);
    if(self->priv->dict_interfaces != NULL)
        g_variant_unref(self->priv->dict_interfaces);


    /* Always chain up to the parent class; there is no need to check if
     * the parent class implements the dispose() virtual function: it is
     * always guaranteed to do so
     */
    G_OBJECT_CLASS (properties_parent_class)->dispose (gobject);
}

static void properties_finalize (GObject *gobject)
{
    Properties *self = PROPERTIES(gobject);

    // g_free(self->priv->a_string);

    /* Always chain up to the parent class; as with dispose(), finalize()
     * is guaranteed to exist on the parent's class virtual function table
     */
    G_OBJECT_CLASS (properties_parent_class)->finalize (gobject);
}

static void properties_class_init (PropertiesClass *klass)
{
    GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
    
    gobject_class->get_property = _properties_get_property;
    gobject_class->set_property = _properties_set_property;
    
    /* object DBusType [readwrite, construct only] */
    obj_properties[PROP_DBUS_TYPE] =
    g_param_spec_string ("DBusType",
                         "dbus_type",
                         "Properties D-Bus connection type",
                         NULL /* default value */,
                         G_PARAM_CONSTRUCT_ONLY |
                         G_PARAM_READWRITE);
    
    /* object DBusServiceName [readwrite, construct only] */
    obj_properties[PROP_DBUS_SERVICE_NAME] =
    g_param_spec_string ("DBusServiceName",
                         "dbus_service_name",
                         "Properties D-Bus service name",
                         NULL /* default value */,
                         G_PARAM_CONSTRUCT_ONLY |
                         G_PARAM_READWRITE);
    
    /* object DBusObjectPath [readwrite, construct only] */
    obj_properties[PROP_DBUS_OBJECT_PATH] =
    g_param_spec_string ("DBusObjectPath",
                         "dbus_object_path",
                         "Properties D-Bus object path",
                         NULL /* default value */,
                         G_PARAM_CONSTRUCT_ONLY |
                         G_PARAM_READWRITE);

    g_object_class_install_properties (gobject_class,
                                       N_PROPERTIES,
                                       obj_properties);
}

static void properties_init (Properties *self)
{
    self->priv = properties_get_instance_private (self);
    self->priv->proxy = NULL;
    self->priv->dict_interfaces = NULL;
    self->priv->dbus_type = NULL;
    self->priv->dbus_service_name = NULL;
    self->priv->dbus_object_path = NULL;
}

static void _properties_create_gdbus_proxy(Properties *self, GError **error)
{
    if(self->priv->dbus_type && self->priv->dbus_service_name && self->priv->dbus_object_path)
    {
        if(g_ascii_strcasecmp(g_ascii_strdown(self->priv->dbus_type, -1), "system") == 0)
        {
            g_assert(system_conn != NULL);
            self->priv->proxy = g_dbus_proxy_new_sync(system_conn, G_DBUS_PROXY_FLAGS_NONE, NULL, self->priv->dbus_service_name, self->priv->dbus_object_path, PROPERTIES_DBUS_INTERFACE, NULL, error);
        }
        else if(g_ascii_strcasecmp(g_ascii_strdown(self->priv->dbus_type, -1), "session") == 0)
        {
            g_assert(session_conn != NULL);
            self->priv->proxy = g_dbus_proxy_new_sync(session_conn, G_DBUS_PROXY_FLAGS_NONE, NULL, self->priv->dbus_service_name, self->priv->dbus_object_path, PROPERTIES_DBUS_INTERFACE, NULL, error);
        }
        else
            g_error("Invalid DBus connection type: %s", self->priv->dbus_type);
    }
}

const gchar *properties_get_dbus_type(Properties *self)
{
    g_assert(PROPERTIES_IS(self));
    g_assert(self->priv->proxy != NULL);
    return self->priv->dbus_type;
}

const gchar *properties_get_dbus_service_name(Properties *self)
{
    g_assert(PROPERTIES_IS(self));
    g_assert(self->priv->proxy != NULL);
    return g_dbus_proxy_get_name(self->priv->proxy);
}

const gchar *properties_get_dbus_object_path(Properties *self)
{
    g_assert(PROPERTIES_IS(self));
    g_assert(self->priv->proxy != NULL);
    return g_dbus_proxy_get_object_path(self->priv->proxy);
}

GVariant *properties_get(Properties *self, const gchar *interface_name, const gchar *property_name, GError **error)
{
    g_assert(PROPERTIES_IS(self));
    GVariant *retVal = g_dbus_proxy_call_sync(self->priv->proxy, "Get", g_variant_new("(ss)", interface_name, property_name), G_DBUS_CALL_FLAGS_NONE, -1, NULL, error);
    if (retVal == NULL)
        return NULL;
    retVal = g_variant_get_child_value(retVal, 0);
    retVal = g_variant_get_variant(retVal);
    return retVal;
}

void properties_set(Properties *self, const gchar *interface_name, const gchar *property_name, const GVariant *value, GError **error)
{
    g_assert(PROPERTIES_IS(self));
    g_dbus_proxy_call_sync(self->priv->proxy, "Set", g_variant_new("(ssv)", interface_name, property_name, value), G_DBUS_CALL_FLAGS_NONE, -1, NULL, error);
}

GVariant *properties_get_all(Properties *self, const gchar *interface_name, GError **error)
{
    g_assert(PROPERTIES_IS(self));
    GVariant *retVal = g_dbus_proxy_call_sync(self->priv->proxy, "GetAll", g_variant_new("(s)", interface_name), G_DBUS_CALL_FLAGS_NONE, -1, NULL, error);
    if (retVal == NULL)
        return NULL;
    retVal = g_variant_get_child_value(retVal, 0);
    return retVal;
}