With a Qt DBus proxy built on QDbusAbstractInterface (via qdbusxml2cpp), what's the best way to handle the service/object you want to interface to not being available when you start? Note: I'm not interested in simply knowing it (you can use BlahService.isValid() to find that out); I want to be able to know if it's valid, and know when it becomes valid so I can change state (and broadcast that state change with a signal), and on that state change do other stuff. Conversely, I want to know when it's no longer valid for similar reasons.
Without tracking the state of the service:
#define CONNECT_DBUS_SIG(x,y) connect(blah,SIGNAL(x),this,SLOT(y))
// FIX - should watch for service, and also handle it going away and
// coming back
blah = new BlahService("com.xyzzy.BlahService", "/com/xyzzy/BlahService",
QDBusConnection::sessionBus(), this);
if (!blah)
return 0;
if (blah.isValid())
{
CONNECT_DBUS_SIG(foo(),Event_foo());
}
else
{
// Since we aren't watching for registration, what can we do but exit?
}
Probably we need to watch for NameOwnerChanged on the DBus connection object - unless QT's dbus code does this for us - and then when we get that signal change state, and if needed connect or disconnect the signals from the object.
All the examples I find either ignore the issue or simply exit if the server object doesn't exist, and don't deal with it going away. The Car/Controller Qt example at least notices if the server goes away and prints "Disconnected" if isValid() becomes false during use, but it's polling isValid().
Added:
Note that QtDbusAbtractInterface registers for changes of ownership of the server (NameOwnerChanged), and updates isValid() when changes occur. So I suspect you can connect to that serverOwnerChanged signal directly to find out about changes to ownership and use that as an indicator to try again - though you won't be able to trust isValid since it may be updated before or after you get signaled.
Alternatively (ugly) you can set up a timer and poll for isValid().