~ubuntu-branches/ubuntu/oneiric/cairo-dock/oneiric

« back to all changes in this revision

Viewing changes to src/cairo-dock-dbus.c

  • Committer: Bazaar Package Importer
  • Author(s): Matthieu Baerts (matttbe)
  • Date: 2010-08-09 23:26:12 UTC
  • mto: (18.1.1 cairo-dock) (19.1.1 cairo-dock)
  • mto: This revision was merged to the branch mainline in revision 13.
  • Revision ID: james.westby@ubuntu.com-20100809232612-pocdxliaxjdetm37
Tags: upstream-2.2.0~0beta4
ImportĀ upstreamĀ versionĀ 2.2.0~0beta4

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/**
2
 
* This file is a part of the Cairo-Dock project
3
 
*
4
 
* Copyright : (C) see the 'copyright' file.
5
 
* E-mail    : see the 'copyright' file.
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
9
 
* as published by the Free Software Foundation; either version 3
10
 
* of the License, or (at your option) any later version.
11
 
*
12
 
* This program is distributed in the hope that it will be useful,
13
 
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 
* GNU General Public License for more details.
16
 
* You should have received a copy of the GNU General Public License
17
 
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
 
*/
19
 
 
20
 
#include <string.h>
21
 
#include <glib.h>
22
 
 
23
 
#include "cairo-dock-log.h"
24
 
#include "cairo-dock-dbus.h"
25
 
 
26
 
static DBusGConnection *s_pSessionConnexion = NULL;
27
 
static DBusGConnection *s_pSystemConnexion = NULL;
28
 
static DBusGProxy *s_pDBusSessionProxy = NULL;
29
 
static DBusGProxy *s_pDBusSystemProxy = NULL;
30
 
 
31
 
 
32
 
DBusGConnection *cairo_dock_get_session_connection (void)
33
 
{
34
 
        if (s_pSessionConnexion == NULL)
35
 
        {
36
 
                GError *erreur = NULL;
37
 
                s_pSessionConnexion = dbus_g_bus_get (DBUS_BUS_SESSION, &erreur);
38
 
                if (erreur != NULL)
39
 
                {
40
 
                        cd_warning (erreur->message);
41
 
                        g_error_free (erreur);
42
 
                        s_pSessionConnexion = NULL;
43
 
                }
44
 
        }
45
 
        return s_pSessionConnexion;
46
 
}
47
 
 
48
 
DBusGConnection *cairo_dock_get_system_connection (void)
49
 
{
50
 
        if (s_pSystemConnexion == NULL)
51
 
        {
52
 
                GError *erreur = NULL;
53
 
                s_pSystemConnexion = dbus_g_bus_get (DBUS_BUS_SYSTEM, &erreur);
54
 
                if (erreur != NULL)
55
 
                {
56
 
                        cd_warning (erreur->message);
57
 
                        g_error_free (erreur);
58
 
                        s_pSystemConnexion = NULL;
59
 
                }
60
 
        }
61
 
        return s_pSystemConnexion;
62
 
}
63
 
 
64
 
DBusGProxy *cairo_dock_get_main_proxy (void)
65
 
{
66
 
        if (s_pDBusSessionProxy == NULL)
67
 
        {
68
 
                s_pDBusSessionProxy = cairo_dock_create_new_session_proxy (DBUS_SERVICE_DBUS,
69
 
                        DBUS_PATH_DBUS,
70
 
                        DBUS_INTERFACE_DBUS);
71
 
        }
72
 
        return s_pDBusSessionProxy;
73
 
}
74
 
 
75
 
DBusGProxy *cairo_dock_get_main_system_proxy (void)
76
 
{
77
 
        if (s_pDBusSystemProxy == NULL)
78
 
        {
79
 
                s_pDBusSystemProxy = cairo_dock_create_new_system_proxy (DBUS_SERVICE_DBUS,
80
 
                        DBUS_PATH_DBUS,
81
 
                        DBUS_INTERFACE_DBUS);
82
 
        } 
83
 
        return s_pDBusSystemProxy;
84
 
}
85
 
 
86
 
void cairo_dock_register_service_name (const gchar *cServiceName)
87
 
{
88
 
        DBusGProxy *pProxy = cairo_dock_get_main_proxy ();
89
 
        if (pProxy == NULL)
90
 
                return ;
91
 
        GError *erreur = NULL;
92
 
        int request_ret;
93
 
        org_freedesktop_DBus_request_name (pProxy, cServiceName, 0, &request_ret, &erreur);
94
 
        if (erreur != NULL)
95
 
        {
96
 
                cd_warning ("Unable to register service: %s", erreur->message);
97
 
                g_error_free (erreur);
98
 
        }
99
 
}
100
 
 
101
 
 
102
 
gboolean cairo_dock_bdus_is_enabled (void)
103
 
{
104
 
        return (cairo_dock_get_session_connection () != NULL && cairo_dock_get_system_connection () != NULL);
105
 
}
106
 
 
107
 
 
108
 
DBusGProxy *cairo_dock_create_new_session_proxy (const char *name, const char *path, const char *interface)
109
 
{
110
 
        DBusGConnection *pConnection = cairo_dock_get_session_connection ();
111
 
        if (pConnection != NULL)
112
 
                return dbus_g_proxy_new_for_name (
113
 
                        pConnection,
114
 
                        name,
115
 
                        path,
116
 
                        interface);
117
 
        else
118
 
                return NULL;
119
 
}
120
 
 
121
 
DBusGProxy *cairo_dock_create_new_system_proxy (const char *name, const char *path, const char *interface)
122
 
{
123
 
        DBusGConnection *pConnection = cairo_dock_get_system_connection ();
124
 
        if (pConnection != NULL)
125
 
                return dbus_g_proxy_new_for_name (
126
 
                        pConnection,
127
 
                        name,
128
 
                        path,
129
 
                        interface);
130
 
        else
131
 
                return NULL;
132
 
}
133
 
 
134
 
 
135
 
static inline gboolean _dbus_detect_application (const gchar *cName, DBusGProxy *pProxy)
136
 
{
137
 
        g_return_val_if_fail (cName != NULL && pProxy != NULL, FALSE);
138
 
        
139
 
        gchar **name_list = NULL;
140
 
        gboolean bPresent = FALSE;
141
 
        
142
 
        if(dbus_g_proxy_call (pProxy, "ListNames", NULL,
143
 
                G_TYPE_INVALID,
144
 
                G_TYPE_STRV,
145
 
                &name_list,
146
 
                G_TYPE_INVALID))
147
 
        {
148
 
                cd_message ("detection du service %s ...", cName);
149
 
                int i;
150
 
                for (i = 0; name_list[i] != NULL; i ++)
151
 
                {
152
 
                        if (strcmp (name_list[i], cName) == 0)
153
 
                        {
154
 
                                bPresent = TRUE;
155
 
                                break;
156
 
                        }
157
 
                }
158
 
        }
159
 
        g_strfreev (name_list);
160
 
        return bPresent;
161
 
}
162
 
 
163
 
gboolean cairo_dock_dbus_detect_application (const gchar *cName)
164
 
{
165
 
        cd_message ("%s (%s)", __func__, cName);
166
 
        DBusGProxy *pProxy = cairo_dock_get_main_proxy ();
167
 
        return _dbus_detect_application (cName, pProxy);
168
 
}
169
 
 
170
 
gboolean cairo_dock_dbus_detect_system_application (const gchar *cName)
171
 
{
172
 
        cd_message ("%s (%s)", __func__, cName);
173
 
        DBusGProxy *pProxy = cairo_dock_get_main_system_proxy ();
174
 
        return _dbus_detect_application (cName, pProxy);
175
 
}
176
 
 
177
 
 
178
 
gchar **cairo_dock_dbus_get_services (void)
179
 
{
180
 
        DBusGProxy *pProxy = cairo_dock_get_main_proxy ();
181
 
        gchar **name_list = NULL;
182
 
        if(dbus_g_proxy_call (pProxy, "ListNames", NULL,
183
 
                G_TYPE_INVALID,
184
 
                G_TYPE_STRV,
185
 
                &name_list,
186
 
                G_TYPE_INVALID))
187
 
                return name_list;
188
 
        else
189
 
                return NULL;
190
 
}
191
 
 
192
 
 
193
 
 
194
 
gboolean cairo_dock_dbus_get_boolean (DBusGProxy *pDbusProxy, const gchar *cAccessor)
195
 
{
196
 
        GError *erreur = NULL;
197
 
        gboolean bValue = FALSE;
198
 
        dbus_g_proxy_call (pDbusProxy, cAccessor, &erreur,
199
 
                G_TYPE_INVALID,
200
 
                G_TYPE_BOOLEAN, &bValue,
201
 
                G_TYPE_INVALID);
202
 
        if (erreur != NULL)
203
 
        {
204
 
                cd_warning (erreur->message);
205
 
                g_error_free (erreur);
206
 
        }
207
 
        return bValue;
208
 
}
209
 
 
210
 
int cairo_dock_dbus_get_integer (DBusGProxy *pDbusProxy, const gchar *cAccessor)
211
 
{
212
 
        GError *erreur = NULL;
213
 
        int iValue = -1;
214
 
        dbus_g_proxy_call (pDbusProxy, cAccessor, &erreur,
215
 
                G_TYPE_INVALID,
216
 
                G_TYPE_INT, &iValue,
217
 
                G_TYPE_INVALID);
218
 
        if (erreur != NULL)
219
 
        {
220
 
                cd_warning (erreur->message);
221
 
                g_error_free (erreur);
222
 
                iValue = -1;
223
 
        }
224
 
        return iValue;
225
 
}
226
 
 
227
 
guint cairo_dock_dbus_get_uinteger (DBusGProxy *pDbusProxy, const gchar *cAccessor)
228
 
{
229
 
        GError *erreur = NULL;
230
 
        guint iValue = -1;
231
 
        dbus_g_proxy_call (pDbusProxy, cAccessor, &erreur,
232
 
                G_TYPE_INVALID,
233
 
                G_TYPE_UINT, &iValue,
234
 
                G_TYPE_INVALID);
235
 
        if (erreur != NULL)
236
 
        {
237
 
                cd_warning (erreur->message);
238
 
                g_error_free (erreur);
239
 
                iValue = -1;
240
 
        }
241
 
        return iValue;
242
 
}
243
 
 
244
 
gchar *cairo_dock_dbus_get_string (DBusGProxy *pDbusProxy, const gchar *cAccessor)
245
 
{
246
 
        GError *erreur = NULL;
247
 
        gchar *cValue = NULL;
248
 
        dbus_g_proxy_call (pDbusProxy, cAccessor, &erreur,
249
 
                G_TYPE_INVALID,
250
 
                G_TYPE_STRING, &cValue,
251
 
                G_TYPE_INVALID);
252
 
        if (erreur != NULL)
253
 
        {
254
 
                cd_warning (erreur->message);
255
 
                g_error_free (erreur);
256
 
        }
257
 
        return cValue;
258
 
}
259
 
 
260
 
guchar *cairo_dock_dbus_get_uchar (DBusGProxy *pDbusProxy, const gchar *cAccessor)
261
 
{
262
 
        GError *erreur = NULL;
263
 
        guchar* uValue = NULL;
264
 
        
265
 
        dbus_g_proxy_call (pDbusProxy, cAccessor, &erreur,
266
 
                G_TYPE_INVALID,
267
 
                G_TYPE_UCHAR, &uValue,
268
 
                G_TYPE_INVALID);
269
 
        if (erreur != NULL)
270
 
        {
271
 
                cd_warning (erreur->message);
272
 
                g_error_free (erreur);
273
 
        }
274
 
        
275
 
        return uValue;
276
 
}
277
 
 
278
 
gdouble cairo_dock_dbus_get_double (DBusGProxy *pDbusProxy, const gchar *cAccessor)
279
 
{
280
 
        GError *erreur = NULL;
281
 
        gdouble fValue = 0.;
282
 
        
283
 
        dbus_g_proxy_call (pDbusProxy, cAccessor, &erreur,
284
 
                G_TYPE_INVALID,
285
 
                G_TYPE_DOUBLE, &fValue,
286
 
                G_TYPE_INVALID);
287
 
        if (erreur != NULL)
288
 
        {
289
 
                cd_warning (erreur->message);
290
 
                g_error_free (erreur);
291
 
        }
292
 
        
293
 
        return fValue;
294
 
}
295
 
 
296
 
gchar **cairo_dock_dbus_get_string_list (DBusGProxy *pDbusProxy, const gchar *cAccessor)
297
 
{
298
 
        GError *erreur = NULL;
299
 
        gchar **cValues = NULL;
300
 
        dbus_g_proxy_call (pDbusProxy, cAccessor, &erreur,
301
 
                G_TYPE_INVALID,
302
 
                G_TYPE_POINTER, &cValues,
303
 
                G_TYPE_INVALID);
304
 
        if (erreur != NULL)
305
 
        {
306
 
                cd_warning (erreur->message);
307
 
                g_error_free (erreur);
308
 
        }
309
 
        return cValues;
310
 
}
311
 
 
312
 
GPtrArray *cairo_dock_dbus_get_array (DBusGProxy *pDbusProxy, const gchar *cAccessor)
313
 
{
314
 
        GError *erreur = NULL;
315
 
        GPtrArray *pArray = NULL;
316
 
        dbus_g_proxy_call (pDbusProxy, cAccessor, &erreur,
317
 
                G_TYPE_INVALID,
318
 
                dbus_g_type_get_collection ("GPtrArray", DBUS_TYPE_G_OBJECT_PATH), &pArray,
319
 
                G_TYPE_INVALID);
320
 
        if (erreur != NULL)
321
 
        {
322
 
                cd_warning (erreur->message);
323
 
                g_error_free (erreur);
324
 
        }
325
 
        return pArray;
326
 
}
327
 
 
328
 
 
329
 
 
330
 
void cairo_dock_dbus_call (DBusGProxy *pDbusProxy, const gchar *cCommand)
331
 
{
332
 
        dbus_g_proxy_call_no_reply (pDbusProxy, cCommand,
333
 
                G_TYPE_INVALID,
334
 
                G_TYPE_INVALID);
335
 
}
336
 
 
337
 
 
338
 
void cairo_dock_dbus_get_properties (DBusGProxy *pDbusProxy, const gchar *cCommand, const gchar *cInterface, const gchar *cProperty, GValue *vProperties)
339
 
{
340
 
        GError *erreur=NULL;
341
 
        
342
 
        dbus_g_proxy_call(pDbusProxy, cCommand, &erreur,
343
 
                G_TYPE_STRING, cInterface,
344
 
                G_TYPE_STRING, cProperty,
345
 
                G_TYPE_INVALID,
346
 
                G_TYPE_VALUE, vProperties,
347
 
                G_TYPE_INVALID);
348
 
        
349
 
        if (erreur != NULL)
350
 
        {
351
 
                cd_warning (erreur->message);
352
 
                g_error_free (erreur);
353
 
        }
354
 
}
355
 
 
356
 
 
357
 
 
358
 
void cairo_dock_dbus_get_property_in_value (DBusGProxy *pDbusProxy, const gchar *cInterface, const gchar *cProperty, GValue *pProperty)
359
 
{
360
 
        GError *erreur=NULL;
361
 
        
362
 
        dbus_g_proxy_call(pDbusProxy, "Get", &erreur,
363
 
                G_TYPE_STRING, cInterface,
364
 
                G_TYPE_STRING, cProperty,
365
 
                G_TYPE_INVALID,
366
 
                G_TYPE_VALUE, pProperty,
367
 
                G_TYPE_INVALID);
368
 
        
369
 
        if (erreur != NULL)
370
 
        {
371
 
                cd_warning (erreur->message);
372
 
                g_error_free (erreur);
373
 
        }
374
 
}
375
 
 
376
 
gboolean cairo_dock_dbus_get_property_as_boolean (DBusGProxy *pDbusProxy, const gchar *cInterface, const gchar *cProperty)
377
 
{
378
 
        GValue v = {0};
379
 
        cairo_dock_dbus_get_property_in_value (pDbusProxy, cInterface, cProperty, &v);
380
 
        if (G_VALUE_HOLDS_BOOLEAN (&v))
381
 
                return g_value_get_boolean (&v);
382
 
        else
383
 
                return FALSE;
384
 
}
385
 
 
386
 
gint cairo_dock_dbus_get_property_as_int (DBusGProxy *pDbusProxy, const gchar *cInterface, const gchar *cProperty)
387
 
{
388
 
        GValue v = {0};
389
 
        cairo_dock_dbus_get_property_in_value (pDbusProxy, cInterface, cProperty, &v);
390
 
        if (G_VALUE_HOLDS_INT (&v))
391
 
                return g_value_get_int (&v);
392
 
        else
393
 
                return 0;
394
 
}
395
 
 
396
 
guint cairo_dock_dbus_get_property_as_uint (DBusGProxy *pDbusProxy, const gchar *cInterface, const gchar *cProperty)
397
 
{
398
 
        GValue v = {0};
399
 
        cairo_dock_dbus_get_property_in_value (pDbusProxy, cInterface, cProperty, &v);
400
 
        if (G_VALUE_HOLDS_UINT (&v))
401
 
                return g_value_get_uint (&v);
402
 
        else
403
 
                return 0;
404
 
}
405
 
 
406
 
guchar cairo_dock_dbus_get_property_as_uchar (DBusGProxy *pDbusProxy, const gchar *cInterface, const gchar *cProperty)
407
 
{
408
 
        GValue v = {0};
409
 
        cairo_dock_dbus_get_property_in_value (pDbusProxy, cInterface, cProperty, &v);
410
 
        if (G_VALUE_HOLDS_UCHAR (&v))
411
 
                return g_value_get_uchar (&v);
412
 
        else
413
 
                return 0;
414
 
}
415
 
 
416
 
gdouble cairo_dock_dbus_get_property_as_double (DBusGProxy *pDbusProxy, const gchar *cInterface, const gchar *cProperty)
417
 
{
418
 
        GValue v = {0};
419
 
        cairo_dock_dbus_get_property_in_value (pDbusProxy, cInterface, cProperty, &v);
420
 
        if (G_VALUE_HOLDS_DOUBLE (&v))
421
 
                return g_value_get_double (&v);
422
 
        else
423
 
                return 0.;
424
 
}
425
 
 
426
 
gchar *cairo_dock_dbus_get_property_as_string (DBusGProxy *pDbusProxy, const gchar *cInterface, const gchar *cProperty)
427
 
{
428
 
        GValue v = {0};
429
 
        cairo_dock_dbus_get_property_in_value (pDbusProxy, cInterface, cProperty, &v);
430
 
        if (G_VALUE_HOLDS_STRING (&v))
431
 
        {
432
 
                gchar *s = g_value_get_string (&v);
433
 
                return s;
434
 
        }
435
 
        else
436
 
                return NULL;
437
 
}
438
 
 
439
 
gchar *cairo_dock_dbus_get_property_as_object_path (DBusGProxy *pDbusProxy, const gchar *cInterface, const gchar *cProperty)
440
 
{
441
 
        GValue v = {0};
442
 
        cairo_dock_dbus_get_property_in_value (pDbusProxy, cInterface, cProperty, &v);
443
 
        if (G_VALUE_HOLDS (&v, DBUS_TYPE_G_OBJECT_PATH))
444
 
        {
445
 
                gchar *s = g_value_get_string (&v);
446
 
                return s;
447
 
        }
448
 
        else
449
 
                return NULL;
450
 
}
451
 
 
452
 
gpointer cairo_dock_dbus_get_property_as_boxed (DBusGProxy *pDbusProxy, const gchar *cInterface, const gchar *cProperty)
453
 
{
454
 
        GValue v = {0};
455
 
        cairo_dock_dbus_get_property_in_value (pDbusProxy, cInterface, cProperty, &v);
456
 
        if (G_VALUE_HOLDS_BOXED (&v))
457
 
        {
458
 
                gpointer p = g_value_get_boxed (&v);
459
 
                return p;
460
 
        }
461
 
        else
462
 
                return NULL;
463
 
}
464
 
 
465
 
GHashTable *cairo_dock_dbus_get_all_properties (DBusGProxy *pDbusProxy, const gchar *cInterface)
466
 
{
467
 
        GError *erreur=NULL;
468
 
        GHashTable *hProperties = NULL;
469
 
        
470
 
        dbus_g_proxy_call(pDbusProxy, "GetAll", &erreur,
471
 
                G_TYPE_STRING, cInterface,
472
 
                G_TYPE_INVALID,
473
 
                (dbus_g_type_get_map("GHashTable", G_TYPE_STRING, G_TYPE_VALUE)), &hProperties,
474
 
                G_TYPE_INVALID);
475
 
                                        
476
 
        if (erreur != NULL)
477
 
        {
478
 
                cd_warning (erreur->message);
479
 
                g_error_free (erreur);
480
 
                return NULL;
481
 
        }
482
 
        else
483
 
        {
484
 
                return hProperties;
485
 
        }
486
 
}
487
 
 
488
 
 
489
 
void cairo_dock_dbus_set_property (DBusGProxy *pDbusProxy, const gchar *cInterface, const gchar *cProperty, GValue *pProperty)
490
 
{
491
 
        GError *erreur=NULL;
492
 
        
493
 
        dbus_g_proxy_call(pDbusProxy, "Set", &erreur,
494
 
                G_TYPE_STRING, cInterface,
495
 
                G_TYPE_STRING, cProperty,
496
 
                G_TYPE_VALUE, pProperty,
497
 
                G_TYPE_INVALID,
498
 
                G_TYPE_INVALID);
499
 
        
500
 
        if (erreur != NULL)
501
 
        {
502
 
                cd_warning (erreur->message);
503
 
                g_error_free (erreur);
504
 
        }
505
 
}
506
 
 
507
 
void cairo_dock_dbus_set_boolean_property (DBusGProxy *pDbusProxy, const gchar *cInterface, const gchar *cProperty, gboolean bValue)
508
 
{
509
 
        GValue v = {0};
510
 
        g_value_init (&v, G_TYPE_BOOLEAN);
511
 
        g_value_set_boolean (&v, bValue);
512
 
        cairo_dock_dbus_set_property (pDbusProxy, cInterface, cProperty, &v);
513
 
}
514