Add a more sophisticated GetProperties reply parser

This commit is contained in:
Denis Kenzior 2010-02-12 15:05:27 -06:00
parent d5577a393d
commit 7a48b56ebb
1 changed files with 84 additions and 0 deletions

View File

@ -252,6 +252,90 @@ static int send_method_call_with_reply(const char *dest, const char *path,
return 0;
}
typedef void (*PropertyHandler)(DBusMessageIter *iter, gpointer user_data);
struct property_handler {
const char *property;
PropertyHandler callback;
gpointer user_data;
};
static gint property_handler_compare(gconstpointer a, gconstpointer b)
{
const struct property_handler *handler = a;
const char *property = b;
return strcmp(handler->property, property);
}
static void parse_properties_reply(DBusMessage *reply,
const char *property, ...)
{
va_list args;
GSList *prop_handlers = NULL;
DBusMessageIter array, dict;
va_start(args, property);
while (property != NULL) {
struct property_handler *handler =
g_new0(struct property_handler, 1);
handler->property = property;
handler->callback = va_arg(args, PropertyHandler);
handler->user_data = va_arg(args, gpointer);
property = va_arg(args, const char *);
prop_handlers = g_slist_prepend(prop_handlers, handler);
}
va_end(args);
if (dbus_message_iter_init(reply, &array) == FALSE)
goto done;
if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY)
goto done;
dbus_message_iter_recurse(&array, &dict);
while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
DBusMessageIter entry, value;
const char *key;
GSList *l;
dbus_message_iter_recurse(&dict, &entry);
if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
goto done;
dbus_message_iter_get_basic(&entry, &key);
dbus_message_iter_next(&entry);
if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_VARIANT)
goto done;
dbus_message_iter_recurse(&entry, &value);
l = g_slist_find_custom(prop_handlers, key,
property_handler_compare);
if (l) {
struct property_handler *handler = l->data;
handler->callback(&value, handler->user_data);
}
dbus_message_iter_next(&dict);
}
done:
g_slist_foreach(prop_handlers, (GFunc)g_free, NULL);
g_slist_free(prop_handlers);
}
static void cind_status_cb(gboolean ok, GAtResult *result,
gpointer user_data)
{