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 static GHashTable *s_pFilterTable = NULL;
31 static GList *s_pFilterList = NULL;
32 
cairo_dock_get_session_connection(void)33 DBusGConnection *cairo_dock_get_session_connection (void)
34 {
35 	if (s_pSessionConnexion == NULL)
36 	{
37 		GError *erreur = NULL;
38 		s_pSessionConnexion = dbus_g_bus_get (DBUS_BUS_SESSION, &erreur);
39 		if (erreur != NULL)
40 		{
41 			cd_warning (erreur->message);
42 			g_error_free (erreur);
43 			s_pSessionConnexion = NULL;
44 		}
45 	}
46 	return s_pSessionConnexion;
47 }
48 
cairo_dock_get_system_connection(void)49 DBusGConnection *cairo_dock_get_system_connection (void)
50 {
51 	if (s_pSystemConnexion == NULL)
52 	{
53 		GError *erreur = NULL;
54 		s_pSystemConnexion = dbus_g_bus_get (DBUS_BUS_SYSTEM, &erreur);
55 		if (erreur != NULL)
56 		{
57 			cd_warning (erreur->message);
58 			g_error_free (erreur);
59 			s_pSystemConnexion = NULL;
60 		}
61 	}
62 	return s_pSystemConnexion;
63 }
64 
cairo_dock_get_main_proxy(void)65 DBusGProxy *cairo_dock_get_main_proxy (void)
66 {
67 	if (s_pDBusSessionProxy == NULL)
68 	{
69 		s_pDBusSessionProxy = cairo_dock_create_new_session_proxy (DBUS_SERVICE_DBUS,
70 			DBUS_PATH_DBUS,
71 			DBUS_INTERFACE_DBUS);
72 	}
73 	return s_pDBusSessionProxy;
74 }
75 
cairo_dock_get_main_system_proxy(void)76 DBusGProxy *cairo_dock_get_main_system_proxy (void)
77 {
78 	if (s_pDBusSystemProxy == NULL)
79 	{
80 		s_pDBusSystemProxy = cairo_dock_create_new_system_proxy (DBUS_SERVICE_DBUS,
81 			DBUS_PATH_DBUS,
82 			DBUS_INTERFACE_DBUS);
83 	}
84 	return s_pDBusSystemProxy;
85 }
86 
cairo_dock_register_service_name(const gchar * cServiceName)87 gboolean cairo_dock_register_service_name (const gchar *cServiceName)
88 {
89 	DBusGProxy *pProxy = cairo_dock_get_main_proxy ();
90 	if (pProxy == NULL)
91 		return FALSE;
92 	GError *erreur = NULL;
93 	guint request_ret;
94 	org_freedesktop_DBus_request_name (pProxy, cServiceName, 0, &request_ret, &erreur);
95 	if (erreur != NULL)
96 	{
97 		cd_warning ("Unable to register service: %s", erreur->message);
98 		g_error_free (erreur);
99 		return FALSE;
100 	}
101 	return TRUE;
102 }
103 
104 
cairo_dock_dbus_is_enabled(void)105 gboolean cairo_dock_dbus_is_enabled (void)
106 {
107 	return (cairo_dock_get_session_connection () != NULL && cairo_dock_get_system_connection () != NULL);
108 }
109 
on_name_owner_changed(G_GNUC_UNUSED DBusGProxy * pProxy,const gchar * cName,G_GNUC_UNUSED const gchar * cPrevOwner,const gchar * cNewOwner,G_GNUC_UNUSED gpointer data)110 static void on_name_owner_changed (G_GNUC_UNUSED DBusGProxy *pProxy, const gchar *cName, G_GNUC_UNUSED const gchar *cPrevOwner, const gchar *cNewOwner, G_GNUC_UNUSED gpointer data)
111 {
112 	//g_print ("%s (%s)\n", __func__, cName);
113 	gboolean bOwned = (cNewOwner != NULL && *cNewOwner != '\0');
114 	GList *pFilter = g_hash_table_lookup (s_pFilterTable, cName);
115 	GList *f;
116 	for (f = pFilter; f != NULL; f = f->next)
117 	{
118 		gpointer *p = f->data;
119 		CairoDockDbusNameOwnerChangedFunc pCallback = p[0];
120 		pCallback (cName, bOwned, p[1]);
121 	}
122 
123 	for (f = s_pFilterList; f != NULL; f = f->next)
124 	{
125 		gpointer *p = f->data;
126 		if (strncmp (cName, p[2], GPOINTER_TO_INT (p[3])) == 0)
127 		{
128 			CairoDockDbusNameOwnerChangedFunc pCallback = p[0];
129 			pCallback (cName, bOwned, p[1]);
130 		}
131 	}
132 }
cairo_dock_watch_dbus_name_owner(const char * cName,CairoDockDbusNameOwnerChangedFunc pCallback,gpointer data)133 void cairo_dock_watch_dbus_name_owner (const char *cName, CairoDockDbusNameOwnerChangedFunc pCallback, gpointer data)
134 {
135 	if (cName == NULL)
136 		return;
137 	if (s_pFilterTable == NULL)  // first call to this function.
138 	{
139 		// create the table
140 		s_pFilterTable = g_hash_table_new_full (g_str_hash,
141 			g_str_equal,
142 			g_free,
143 			NULL);
144 
145 		// and register to the 'NameOwnerChanged' signal
146 		DBusGProxy *pProxy = cairo_dock_get_main_proxy ();
147 		g_return_if_fail (pProxy != NULL);
148 
149 		dbus_g_proxy_add_signal (pProxy, "NameOwnerChanged",
150 			G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING,
151 			G_TYPE_INVALID);
152 		dbus_g_proxy_connect_signal (pProxy, "NameOwnerChanged",
153 			G_CALLBACK (on_name_owner_changed),
154 			NULL, NULL);
155 	}
156 
157 	// record the callback.
158 	gpointer *p = g_new0 (gpointer, 4);
159 	p[0] = pCallback;
160 	p[1] = data;
161 	int n = strlen(cName);
162 	if (cName[n-1] == '*')
163 	{
164 		p[2] = g_strdup (cName);
165 		p[3] = GINT_TO_POINTER (n-1);
166 		s_pFilterList = g_list_prepend (s_pFilterList, p);
167 	}
168 	else
169 	{
170 		GList *pFilter = g_hash_table_lookup (s_pFilterTable, cName);
171 		pFilter = g_list_prepend (pFilter, p);
172 		g_hash_table_insert (s_pFilterTable, g_strdup (cName), pFilter);
173 	}
174 }
175 
cairo_dock_stop_watching_dbus_name_owner(const char * cName,CairoDockDbusNameOwnerChangedFunc pCallback)176 void cairo_dock_stop_watching_dbus_name_owner (const char *cName, CairoDockDbusNameOwnerChangedFunc pCallback)
177 {
178 	if (cName == NULL || *cName == '\0')
179 		return;
180 	int n = strlen(cName);
181 	if (cName[n-1] == '*')
182 	{
183 		GList *f;
184 		for (f = s_pFilterList; f != NULL; f = f->next)
185 		{
186 			gpointer *p = f->data;
187 			if (strncmp (cName, p[2], strlen (cName)-1) == 0 && pCallback == p[0])
188 			{
189 				g_free (p[2]);
190 				g_free (p);
191 				s_pFilterList = g_list_delete_link (s_pFilterList, f);
192 			}
193 		}
194 	}
195 	else
196 	{
197 		GList *pFilter = g_hash_table_lookup (s_pFilterTable, cName);
198 		if (pFilter != NULL)
199 		{
200 			GList *f;
201 			for (f = pFilter; f != NULL; f = f->next)
202 			{
203 				gpointer *p = f->data;
204 				if (pCallback == p[0])
205 				{
206 					g_free (p);
207 					pFilter = g_list_delete_link (pFilter, f);
208 					g_hash_table_insert (s_pFilterTable, g_strdup (cName), pFilter);
209 					break;
210 				}
211 			}
212 		}
213 	}
214 }
215 
216 
cairo_dock_create_new_session_proxy(const char * name,const char * path,const char * interface)217 DBusGProxy *cairo_dock_create_new_session_proxy (const char *name, const char *path, const char *interface)
218 {
219 	DBusGConnection *pConnection = cairo_dock_get_session_connection ();
220 	if (pConnection != NULL)
221 		return dbus_g_proxy_new_for_name (
222 			pConnection,
223 			name,
224 			path,
225 			interface);
226 	else
227 		return NULL;
228 }
229 
cairo_dock_create_new_system_proxy(const char * name,const char * path,const char * interface)230 DBusGProxy *cairo_dock_create_new_system_proxy (const char *name, const char *path, const char *interface)
231 {
232 	DBusGConnection *pConnection = cairo_dock_get_system_connection ();
233 	if (pConnection != NULL)
234 		return dbus_g_proxy_new_for_name (
235 			pConnection,
236 			name,
237 			path,
238 			interface);
239 	else
240 		return NULL;
241 }
242 
_on_detect_application(DBusGProxy * proxy,DBusGProxyCall * call_id,gpointer * data)243 static void _on_detect_application (DBusGProxy *proxy, DBusGProxyCall *call_id, gpointer *data)
244 {
245 	CairoDockOnAppliPresentOnDbus pCallback = data[0];
246 	gpointer user_data = data[1];
247 	gchar *cName = data[2];
248 	gchar **name_list = NULL;
249 	gboolean bSuccess = dbus_g_proxy_end_call (proxy,
250 		call_id,
251 		NULL,
252 		G_TYPE_STRV,
253 		&name_list,
254 		G_TYPE_INVALID);
255 
256 	gboolean bPresent = FALSE;
257 	cd_message ("detection du service %s (%d)...", cName, bSuccess);
258 	if (bSuccess && name_list)
259 	{
260 		int i;
261 		int n = strlen(cName);
262 		if (cName[n-1] == '*')
263 		{
264 			for (i = 0; name_list[i] != NULL; i ++)
265 			{
266 				if (strncmp (name_list[i], cName, n) == 0)
267 				{
268 					bPresent = TRUE;
269 					break;
270 				}
271 			}
272 		}
273 		else
274 		{
275 			for (i = 0; name_list[i] != NULL; i ++)
276 			{
277 				if (strcmp (name_list[i], cName) == 0)
278 				{
279 					bPresent = TRUE;
280 					break;
281 				}
282 			}
283 		}
284 	}
285 
286 	pCallback (bPresent, user_data);
287 
288 	g_strfreev (name_list);
289 	g_free (cName);
290 	data[2] = NULL;
291 }
_free_detect_application(gpointer * data)292 static void _free_detect_application (gpointer *data)
293 {
294 	cd_debug ("free detection data\n");
295 	g_free (data[2]);
296 	g_free (data);
297 }
_dbus_detect_application_async(const gchar * cName,DBusGProxy * pProxy,CairoDockOnAppliPresentOnDbus pCallback,gpointer user_data)298 static inline DBusGProxyCall *_dbus_detect_application_async (const gchar *cName, DBusGProxy *pProxy, CairoDockOnAppliPresentOnDbus pCallback, gpointer user_data)
299 {
300 	g_return_val_if_fail (cName != NULL && pProxy != NULL, FALSE);
301 	gpointer *data = g_new0 (gpointer, 3);
302 	data[0] = pCallback;
303 	data[1] = user_data;
304 	data[2] = g_strdup (cName);
305 	DBusGProxyCall* pCall= dbus_g_proxy_begin_call (pProxy, "ListNames",
306 		(DBusGProxyCallNotify)_on_detect_application,
307 		data,
308 		(GDestroyNotify) _free_detect_application,
309 		G_TYPE_INVALID);
310 	return pCall;
311 }
312 
cairo_dock_dbus_detect_application_async(const gchar * cName,CairoDockOnAppliPresentOnDbus pCallback,gpointer user_data)313 DBusGProxyCall *cairo_dock_dbus_detect_application_async (const gchar *cName, CairoDockOnAppliPresentOnDbus pCallback, gpointer user_data)
314 {
315 	cd_message ("%s (%s)", __func__, cName);
316 	DBusGProxy *pProxy = cairo_dock_get_main_proxy ();
317 	return _dbus_detect_application_async (cName, pProxy, pCallback, user_data);
318 }
319 
cairo_dock_dbus_detect_system_application_async(const gchar * cName,CairoDockOnAppliPresentOnDbus pCallback,gpointer user_data)320 DBusGProxyCall *cairo_dock_dbus_detect_system_application_async (const gchar *cName, CairoDockOnAppliPresentOnDbus pCallback, gpointer user_data)
321 {
322 	cd_message ("%s (%s)", __func__, cName);
323 	DBusGProxy *pProxy = cairo_dock_get_main_system_proxy ();
324 	return _dbus_detect_application_async (cName, pProxy, pCallback, user_data);
325 }
326 
_dbus_detect_application(const gchar * cName,DBusGProxy * pProxy)327 static inline gboolean _dbus_detect_application (const gchar *cName, DBusGProxy *pProxy)
328 {
329 	g_return_val_if_fail (cName != NULL && pProxy != NULL, FALSE);
330 
331 	gchar **name_list = NULL;
332 	gboolean bPresent = FALSE;
333 
334 	if(dbus_g_proxy_call (pProxy, "ListNames", NULL,
335 		G_TYPE_INVALID,
336 		G_TYPE_STRV,
337 		&name_list,
338 		G_TYPE_INVALID))
339 	{
340 		cd_message ("detection du service %s ...", cName);
341 		int i;
342 		for (i = 0; name_list[i] != NULL; i ++)
343 		{
344 			if (strcmp (name_list[i], cName) == 0)
345 			{
346 				bPresent = TRUE;
347 				break;
348 			}
349 		}
350 	}
351 	g_strfreev (name_list);
352 	return bPresent;
353 }
354 
cairo_dock_dbus_detect_application(const gchar * cName)355 gboolean cairo_dock_dbus_detect_application (const gchar *cName)
356 {
357 	cd_message ("%s (%s)", __func__, cName);
358 	DBusGProxy *pProxy = cairo_dock_get_main_proxy ();
359 	return _dbus_detect_application (cName, pProxy);
360 }
361 
cairo_dock_dbus_detect_system_application(const gchar * cName)362 gboolean cairo_dock_dbus_detect_system_application (const gchar *cName)
363 {
364 	cd_message ("%s (%s)", __func__, cName);
365 	DBusGProxy *pProxy = cairo_dock_get_main_system_proxy ();
366 	return _dbus_detect_application (cName, pProxy);
367 }
368 
369 
cairo_dock_dbus_get_services(void)370 gchar **cairo_dock_dbus_get_services (void)
371 {
372 	DBusGProxy *pProxy = cairo_dock_get_main_proxy ();
373 	gchar **name_list = NULL;
374 	if(dbus_g_proxy_call (pProxy, "ListNames", NULL,
375 		G_TYPE_INVALID,
376 		G_TYPE_STRV,
377 		&name_list,
378 		G_TYPE_INVALID))
379 		return name_list;
380 	else
381 		return NULL;
382 }
383 
384 
385 
cairo_dock_dbus_get_boolean(DBusGProxy * pDbusProxy,const gchar * cAccessor)386 gboolean cairo_dock_dbus_get_boolean (DBusGProxy *pDbusProxy, const gchar *cAccessor)
387 {
388 	GError *erreur = NULL;
389 	gboolean bValue = FALSE;
390 	dbus_g_proxy_call (pDbusProxy, cAccessor, &erreur,
391 		G_TYPE_INVALID,
392 		G_TYPE_BOOLEAN, &bValue,
393 		G_TYPE_INVALID);
394 	if (erreur != NULL)
395 	{
396 		cd_warning (erreur->message);
397 		g_error_free (erreur);
398 	}
399 	return bValue;
400 }
401 
cairo_dock_dbus_get_integer(DBusGProxy * pDbusProxy,const gchar * cAccessor)402 int cairo_dock_dbus_get_integer (DBusGProxy *pDbusProxy, const gchar *cAccessor)
403 {
404 	GError *erreur = NULL;
405 	int iValue = -1;
406 	dbus_g_proxy_call (pDbusProxy, cAccessor, &erreur,
407 		G_TYPE_INVALID,
408 		G_TYPE_INT, &iValue,
409 		G_TYPE_INVALID);
410 	if (erreur != NULL)
411 	{
412 		cd_warning (erreur->message);
413 		g_error_free (erreur);
414 		iValue = -1;
415 	}
416 	return iValue;
417 }
418 
cairo_dock_dbus_get_uinteger(DBusGProxy * pDbusProxy,const gchar * cAccessor)419 guint cairo_dock_dbus_get_uinteger (DBusGProxy *pDbusProxy, const gchar *cAccessor)
420 {
421 	GError *erreur = NULL;
422 	guint iValue = -1;
423 	dbus_g_proxy_call (pDbusProxy, cAccessor, &erreur,
424 		G_TYPE_INVALID,
425 		G_TYPE_UINT, &iValue,
426 		G_TYPE_INVALID);
427 	if (erreur != NULL)
428 	{
429 		cd_warning (erreur->message);
430 		g_error_free (erreur);
431 		iValue = -1;
432 	}
433 	return iValue;
434 }
435 
cairo_dock_dbus_get_string(DBusGProxy * pDbusProxy,const gchar * cAccessor)436 gchar *cairo_dock_dbus_get_string (DBusGProxy *pDbusProxy, const gchar *cAccessor)
437 {
438 	GError *erreur = NULL;
439 	gchar *cValue = NULL;
440 	dbus_g_proxy_call (pDbusProxy, cAccessor, &erreur,
441 		G_TYPE_INVALID,
442 		G_TYPE_STRING, &cValue,
443 		G_TYPE_INVALID);
444 	if (erreur != NULL)
445 	{
446 		cd_warning (erreur->message);
447 		g_error_free (erreur);
448 	}
449 	return cValue;
450 }
451 
cairo_dock_dbus_get_uchar(DBusGProxy * pDbusProxy,const gchar * cAccessor)452 guchar *cairo_dock_dbus_get_uchar (DBusGProxy *pDbusProxy, const gchar *cAccessor)
453 {
454 	GError *erreur = NULL;
455 	guchar* uValue = NULL;
456 
457 	dbus_g_proxy_call (pDbusProxy, cAccessor, &erreur,
458 		G_TYPE_INVALID,
459 		G_TYPE_UCHAR, &uValue,
460 		G_TYPE_INVALID);
461 	if (erreur != NULL)
462 	{
463 		cd_warning (erreur->message);
464 		g_error_free (erreur);
465 	}
466 
467 	return uValue;
468 }
469 
cairo_dock_dbus_get_double(DBusGProxy * pDbusProxy,const gchar * cAccessor)470 gdouble cairo_dock_dbus_get_double (DBusGProxy *pDbusProxy, const gchar *cAccessor)
471 {
472 	GError *erreur = NULL;
473 	gdouble fValue = 0.;
474 
475 	dbus_g_proxy_call (pDbusProxy, cAccessor, &erreur,
476 		G_TYPE_INVALID,
477 		G_TYPE_DOUBLE, &fValue,
478 		G_TYPE_INVALID);
479 	if (erreur != NULL)
480 	{
481 		cd_warning (erreur->message);
482 		g_error_free (erreur);
483 	}
484 
485 	return fValue;
486 }
487 
cairo_dock_dbus_get_string_list(DBusGProxy * pDbusProxy,const gchar * cAccessor)488 gchar **cairo_dock_dbus_get_string_list (DBusGProxy *pDbusProxy, const gchar *cAccessor)
489 {
490 	GError *erreur = NULL;
491 	gchar **cValues = NULL;
492 	dbus_g_proxy_call (pDbusProxy, cAccessor, &erreur,
493 		G_TYPE_INVALID,
494 		G_TYPE_STRV, &cValues,
495 		G_TYPE_INVALID);
496 	if (erreur != NULL)
497 	{
498 		cd_warning (erreur->message);
499 		g_error_free (erreur);
500 	}
501 	return cValues;
502 }
503 
cairo_dock_dbus_get_array(DBusGProxy * pDbusProxy,const gchar * cAccessor)504 GPtrArray *cairo_dock_dbus_get_array (DBusGProxy *pDbusProxy, const gchar *cAccessor)
505 {
506 	GError *erreur = NULL;
507 	GPtrArray *pArray = NULL;
508 	dbus_g_proxy_call (pDbusProxy, cAccessor, &erreur,
509 		G_TYPE_INVALID,
510 		dbus_g_type_get_collection ("GPtrArray", DBUS_TYPE_G_OBJECT_PATH), &pArray,
511 		G_TYPE_INVALID);
512 	if (erreur != NULL)
513 	{
514 		cd_warning (erreur->message);
515 		g_error_free (erreur);
516 	}
517 	return pArray;
518 }
519 
520 
521 
cairo_dock_dbus_call(DBusGProxy * pDbusProxy,const gchar * cCommand)522 void cairo_dock_dbus_call (DBusGProxy *pDbusProxy, const gchar *cCommand)
523 {
524 	dbus_g_proxy_call_no_reply (pDbusProxy, cCommand,
525 		G_TYPE_INVALID,
526 		G_TYPE_INVALID);
527 }
528 
529 
cairo_dock_dbus_get_properties(DBusGProxy * pDbusProxy,const gchar * cCommand,const gchar * cInterface,const gchar * cProperty,GValue * vProperties)530 void cairo_dock_dbus_get_properties (DBusGProxy *pDbusProxy, const gchar *cCommand, const gchar *cInterface, const gchar *cProperty, GValue *vProperties)
531 {
532 	GError *erreur=NULL;
533 
534 	dbus_g_proxy_call(pDbusProxy, cCommand, &erreur,
535 		G_TYPE_STRING, cInterface,
536 		G_TYPE_STRING, cProperty,
537 		G_TYPE_INVALID,
538 		G_TYPE_VALUE, vProperties,
539 		G_TYPE_INVALID);
540 
541 	if (erreur != NULL)
542 	{
543 		cd_warning (erreur->message);
544 		g_error_free (erreur);
545 	}
546 }
547 
548 
549 
cairo_dock_dbus_get_property_in_value_with_timeout(DBusGProxy * pDbusProxy,const gchar * cInterface,const gchar * cProperty,GValue * pProperty,gint iTimeOut)550 void cairo_dock_dbus_get_property_in_value_with_timeout (DBusGProxy *pDbusProxy, const gchar *cInterface, const gchar *cProperty, GValue *pProperty, gint iTimeOut)
551 {
552 	GError *erreur=NULL;
553 
554 	dbus_g_proxy_call_with_timeout (pDbusProxy, "Get", iTimeOut, &erreur,
555 		G_TYPE_STRING, cInterface,
556 		G_TYPE_STRING, cProperty,
557 		G_TYPE_INVALID,
558 		G_TYPE_VALUE, pProperty,
559 		G_TYPE_INVALID);
560 
561 	if (erreur != NULL)
562 	{
563 		cd_warning (erreur->message);
564 		g_error_free (erreur);
565 	}
566 }
567 
cairo_dock_dbus_get_property_as_boolean_with_timeout(DBusGProxy * pDbusProxy,const gchar * cInterface,const gchar * cProperty,gint iTimeOut)568 gboolean cairo_dock_dbus_get_property_as_boolean_with_timeout (DBusGProxy *pDbusProxy, const gchar *cInterface, const gchar *cProperty, gint iTimeOut)
569 {
570 	GValue v = G_VALUE_INIT;
571 	cairo_dock_dbus_get_property_in_value_with_timeout (pDbusProxy, cInterface, cProperty, &v, iTimeOut);
572 	if (G_VALUE_HOLDS_BOOLEAN (&v))
573 		return g_value_get_boolean (&v);
574 	else
575 		return FALSE;
576 }
577 
cairo_dock_dbus_get_property_as_int_with_timeout(DBusGProxy * pDbusProxy,const gchar * cInterface,const gchar * cProperty,gint iTimeOut)578 gint cairo_dock_dbus_get_property_as_int_with_timeout (DBusGProxy *pDbusProxy, const gchar *cInterface, const gchar *cProperty, gint iTimeOut)
579 {
580 	GValue v = G_VALUE_INIT;
581 	cairo_dock_dbus_get_property_in_value_with_timeout (pDbusProxy, cInterface, cProperty, &v, iTimeOut);
582 	if (G_VALUE_HOLDS_INT (&v))
583 		return g_value_get_int (&v);
584 	else
585 		return 0;
586 }
587 
cairo_dock_dbus_get_property_as_uint_with_timeout(DBusGProxy * pDbusProxy,const gchar * cInterface,const gchar * cProperty,gint iTimeOut)588 guint cairo_dock_dbus_get_property_as_uint_with_timeout (DBusGProxy *pDbusProxy, const gchar *cInterface, const gchar *cProperty, gint iTimeOut)
589 {
590 	GValue v = G_VALUE_INIT;
591 	cairo_dock_dbus_get_property_in_value_with_timeout (pDbusProxy, cInterface, cProperty, &v, iTimeOut);
592 	if (G_VALUE_HOLDS_UINT (&v))
593 		return g_value_get_uint (&v);
594 	else
595 		return 0;
596 }
597 
cairo_dock_dbus_get_property_as_uchar_with_timeout(DBusGProxy * pDbusProxy,const gchar * cInterface,const gchar * cProperty,gint iTimeOut)598 guchar cairo_dock_dbus_get_property_as_uchar_with_timeout (DBusGProxy *pDbusProxy, const gchar *cInterface, const gchar *cProperty, gint iTimeOut)
599 {
600 	GValue v = G_VALUE_INIT;
601 	cairo_dock_dbus_get_property_in_value_with_timeout (pDbusProxy, cInterface, cProperty, &v, iTimeOut);
602 	if (G_VALUE_HOLDS_UCHAR (&v))
603 		return g_value_get_uchar (&v);
604 	else
605 		return 0;
606 }
607 
cairo_dock_dbus_get_property_as_double_with_timeout(DBusGProxy * pDbusProxy,const gchar * cInterface,const gchar * cProperty,gint iTimeOut)608 gdouble cairo_dock_dbus_get_property_as_double_with_timeout (DBusGProxy *pDbusProxy, const gchar *cInterface, const gchar *cProperty, gint iTimeOut)
609 {
610 	GValue v = G_VALUE_INIT;
611 	cairo_dock_dbus_get_property_in_value_with_timeout (pDbusProxy, cInterface, cProperty, &v, iTimeOut);
612 	if (G_VALUE_HOLDS_DOUBLE (&v))
613 		return g_value_get_double (&v);
614 	else
615 		return 0.;
616 }
617 
cairo_dock_dbus_get_property_as_string_with_timeout(DBusGProxy * pDbusProxy,const gchar * cInterface,const gchar * cProperty,gint iTimeOut)618 gchar *cairo_dock_dbus_get_property_as_string_with_timeout (DBusGProxy *pDbusProxy, const gchar *cInterface, const gchar *cProperty, gint iTimeOut)
619 {
620 	GValue v = G_VALUE_INIT;
621 	cairo_dock_dbus_get_property_in_value_with_timeout (pDbusProxy, cInterface, cProperty, &v, iTimeOut);
622 	if (G_VALUE_HOLDS_STRING (&v))
623 	{
624 		gchar *s = (gchar*)g_value_get_string (&v);  // on recupere directement le contenu de la GValue. Comme on ne libere pas la GValue, la chaine est a liberer soi-meme quand on en n'a plus besoin.
625 		return s;
626 	}
627 	else
628 		return NULL;
629 }
630 
cairo_dock_dbus_get_property_as_object_path_with_timeout(DBusGProxy * pDbusProxy,const gchar * cInterface,const gchar * cProperty,gint iTimeOut)631 gchar *cairo_dock_dbus_get_property_as_object_path_with_timeout (DBusGProxy *pDbusProxy, const gchar *cInterface, const gchar *cProperty, gint iTimeOut)
632 {
633 	GValue v = G_VALUE_INIT;
634 	cairo_dock_dbus_get_property_in_value_with_timeout (pDbusProxy, cInterface, cProperty, &v, iTimeOut);
635 	if (G_VALUE_HOLDS (&v, DBUS_TYPE_G_OBJECT_PATH))
636 	{
637 		gchar *s = (gchar*)g_value_get_string (&v);  // meme remarque.
638 		return s;
639 	}
640 	else
641 		return NULL;
642 }
643 
cairo_dock_dbus_get_property_as_boxed_with_timeout(DBusGProxy * pDbusProxy,const gchar * cInterface,const gchar * cProperty,gint iTimeOut)644 gpointer cairo_dock_dbus_get_property_as_boxed_with_timeout (DBusGProxy *pDbusProxy, const gchar *cInterface, const gchar *cProperty, gint iTimeOut)
645 {
646 	GValue v = G_VALUE_INIT;
647 	cairo_dock_dbus_get_property_in_value_with_timeout (pDbusProxy, cInterface, cProperty, &v, iTimeOut);
648 	if (G_VALUE_HOLDS_BOXED (&v))
649 	{
650 		gpointer p = g_value_get_boxed (&v);  // meme remarque.
651 		return p;
652 	}
653 	else
654 		return NULL;
655 }
656 
cairo_dock_dbus_get_property_as_string_list_with_timeout(DBusGProxy * pDbusProxy,const gchar * cInterface,const gchar * cProperty,gint iTimeOut)657 gchar **cairo_dock_dbus_get_property_as_string_list_with_timeout (DBusGProxy *pDbusProxy, const gchar *cInterface, const gchar *cProperty, gint iTimeOut)
658 {
659 	GValue v = G_VALUE_INIT;
660 	cairo_dock_dbus_get_property_in_value_with_timeout (pDbusProxy, cInterface, cProperty, &v, iTimeOut);
661 	if (G_VALUE_HOLDS_BOXED(&v))
662 	{
663 		gchar **s = (gchar**)g_value_get_boxed (&v);  // meme remarque.
664 		return s;
665 	}
666 	else
667 		return NULL;
668 }
669 
cairo_dock_dbus_get_all_properties_with_timeout(DBusGProxy * pDbusProxy,const gchar * cInterface,gint iTimeOut)670 GHashTable *cairo_dock_dbus_get_all_properties_with_timeout (DBusGProxy *pDbusProxy, const gchar *cInterface, gint iTimeOut)
671 {
672 	GError *erreur=NULL;
673 	GHashTable *hProperties = NULL;
674 
675 	dbus_g_proxy_call_with_timeout (pDbusProxy, "GetAll", iTimeOut, &erreur,
676 		G_TYPE_STRING, cInterface,
677 		G_TYPE_INVALID,
678 		(dbus_g_type_get_map("GHashTable", G_TYPE_STRING, G_TYPE_VALUE)), &hProperties,
679 		G_TYPE_INVALID);
680 
681 	if (erreur != NULL)
682 	{
683 		cd_warning (erreur->message);
684 		g_error_free (erreur);
685 		return NULL;
686 	}
687 	else
688 	{
689 		return hProperties;
690 	}
691 }
692 
693 
cairo_dock_dbus_set_property_with_timeout(DBusGProxy * pDbusProxy,const gchar * cInterface,const gchar * cProperty,GValue * pProperty,gint iTimeOut)694 void cairo_dock_dbus_set_property_with_timeout (DBusGProxy *pDbusProxy, const gchar *cInterface, const gchar *cProperty, GValue *pProperty, gint iTimeOut)
695 {
696 	GError *erreur=NULL;
697 
698 	dbus_g_proxy_call_with_timeout (pDbusProxy, "Set", iTimeOut, &erreur,
699 		G_TYPE_STRING, cInterface,
700 		G_TYPE_STRING, cProperty,
701 		G_TYPE_VALUE, pProperty,
702 		G_TYPE_INVALID,
703 		G_TYPE_INVALID);
704 
705 	if (erreur != NULL)
706 	{
707 		cd_warning (erreur->message);
708 		g_error_free (erreur);
709 	}
710 }
711 
cairo_dock_dbus_set_boolean_property_with_timeout(DBusGProxy * pDbusProxy,const gchar * cInterface,const gchar * cProperty,gboolean bValue,gint iTimeOut)712 void cairo_dock_dbus_set_boolean_property_with_timeout (DBusGProxy *pDbusProxy, const gchar *cInterface, const gchar *cProperty, gboolean bValue, gint iTimeOut)
713 {
714 	GValue v = G_VALUE_INIT;
715 	g_value_init (&v, G_TYPE_BOOLEAN);
716 	g_value_set_boolean (&v, bValue);
717 	cairo_dock_dbus_set_property_with_timeout (pDbusProxy, cInterface, cProperty, &v, iTimeOut);
718 }
719 
720