program p02_get_all;
uses dbus;
var
err : DBusError;
conn : PDBusConnection;
msg, replymsg : PDBusMessage;
args, replyargs, subargs : DBusMessageIter;
arg1 : PChar;
argType : Integer;
arry, dict : DBusMessageIter;
dictKey, dictVal : PChar;
begin
// initialise the error value
dbus_error_init(@err);
// connect to the DBUS system bus and check for errors
conn := dbus_bus_get(DBUS_BUS_SYSTEM, @err);
if dbus_error_is_set(@err) <> 0 then begin
writeln('Connection Error :', err.message);
dbus_error_free(@err);
end;
if conn = nil then exit;
// create message and check for errors
msg := dbus_message_new_method_call(
'org.freedesktop.hostname1',
'/org/freedesktop/hostname1',
'org.freedesktop.DBus.Properties',
'GetAll');
if msg = nil then begin
writeLn('Could not construct message: ' + err.message);
dbus_error_free(@err);
end;
// initialise parameter list for appending
arg1 := pchar('org.freedesktop.hostname1');
// add parameter arguments
dbus_message_iter_init_append(msg, @args);
if dbus_message_iter_append_basic(@args, DBUS_TYPE_STRING, @arg1) = 0 then begin
writeln('Out Of Memory on arg1!');
exit;
end;
// send message and block while waiting for reply
replymsg := dbus_connection_send_with_reply_and_block(conn, msg, 1000, @err);
if dbus_error_is_set(@err) <> 0 then begin
writeLn('Error waiting for reply: ' + err.message);
dbus_error_free(@err);
end;
// initialise reply argument list then check type of return value
// before retrieving value
repeat
if dbus_message_iter_init(replymsg, @replyargs) = 0 then begin
writeln('No return value');
break;
end;
if dbus_message_iter_get_arg_type(@replyargs) <> DBUS_TYPE_ARRAY then begin
writeln('Return value is not an array: ' + chr(dbus_message_iter_get_arg_type(@replyargs)));
break;
end;
// check first item is a dictionary entry
dbus_message_iter_recurse(@replyargs, @arry);
argType := dbus_message_iter_get_arg_type(@arry);
if argType <> DBUS_TYPE_DICT_ENTRY then begin
writeln('Array is not a dictionary: ' + chr(argType));
break;
end;
// loop through all array entries getting the key name
// and the assiciated value
while argType <> DBUS_TYPE_INVALID do begin
dbus_message_iter_recurse(@arry, @dict);
if dbus_message_iter_get_arg_type(@dict) <> DBUS_TYPE_STRING then begin
writeln('Entry1 is not a string: ' + chr(dbus_message_iter_get_arg_type(@dict)));
break;
end;
dbus_message_iter_get_basic(@dict, @dictKey);
dbus_message_iter_next(@dict);
if dbus_message_iter_get_arg_type(@dict) <> DBUS_TYPE_VARIANT then begin
writeln('Entry2 is not a variant: ' + chr(dbus_message_iter_get_arg_type(@dict)));
break;
end;
dbus_message_iter_recurse(@dict, @subargs);
if dbus_message_iter_get_arg_type(@subargs) <> DBUS_TYPE_STRING then begin
writeln('Entry2 value is not a string: ' + chr(dbus_message_iter_get_arg_type(@subargs)));
break;
end;
dbus_message_iter_get_basic(@subargs, @dictVal);
writeln(dictkey + ': ' + dictVal);
dbus_message_iter_next(@arry);
argType := dbus_message_iter_get_arg_type(@arry);
end;
until true;
// free the messages and finish using the connection
dbus_message_unref(replymsg);
dbus_message_unref(msg);
dbus_connection_unref(conn);
end.