Change from HAL to udisk
Hello everyone,
I’m developing a program that should detect whenever a USB storage device is plugged/unplugged. After extensive search I’ve found an example that worked ok for me:
- "org.freedesktop.Hal",
- "/org/freedesktop/Hal/Manager",
- "org.freedesktop.Hal.Manager",
- "DeviceAdded",
- this, SLOT(deviceAdded());
- "org.freedesktop.Hal",
- "/org/freedesktop/Hal/Manager",
- "org.freedesktop.Hal.Manager",
- "DeviceRemoved",
- this, SLOT(deviceRemoved());
- void deviceAdded(){
- qDebug() << "device added!";
- }
- void deviceRemoved(){
- qDebug() << "device removed!";
- }
However, reading more about HAL I’ve discovered that it is now obsolete and it is being replaced by udev (my Fedora 15 didn’t even come with HAL installed anymore). So instead using HAL, I’ve tried to change the example to use udisks by doing the following:
- "org.freedesktop.UDisks", /*Changed Hal to UDisks*/
- "/org/freedesktop/UDisks",
- "org.freedesktop.UDisks",
- "DeviceAdded",
- this, SLOT(deviceAdded());
- "org.freedesktop.UDisks",
- "/org/freedesktop/UDisks",
- "org.freedesktop.UDisks",
- "DeviceRemoved",
- this, SLOT(deviceRemoved());
- void deviceAdded(){
- qDebug() << "device added!";
- }
- void deviceRemoved(){
- qDebug() << "device removed!";
- }
However this new code won’t work. Does anyone have any idea what I might be doing incorrectly?
9 replies
So, now I got a solution:
change the slots argument to the type: QDBusObjectPath, then it will be handled correctly
Example:
- "org.freedesktop.UDisks",
- "/org/freedesktop/UDisks",
- "org.freedesktop.UDisks",
- "DeviceAdded",
- qDebug() << "device added!"<<dev.path();
- }
In future cases, when the typing is unclear (I did not find the mapping from the “o” type in dbus to the corresponding type in qt, which is the QDBusObjectPath) just try first to receive the message as a QDBusMessage. You can print it out with qDebug and get more information about the type.
Another hint is to use the introspection with the qdbusviewer application. It will at least give you the arg type string “o” in this case.
Please tell, if it works..
It’s my way, no dbus, only QtCore: http://developer.qt.nokia.com/forums/viewthread/9605/
So, solution for my next last question:
“How can I access properties as they exist for each device?”
As mentioned in the code, I am not sure if there are way more elegant ways to deal with the variant types, but it works.
dbuspath is the dbus.path() from the previous question.
first, check if the device is really mounted:
- "org.freedesktop.DBus.Properties", "Get");
- args.append("org.freedesktop.UDisks.Device");
- args.append("DeviceIsMounted");
- msg.setArguments(args);
- QDBusMessage reply;
- reply = cnx.call(msg);
- QString sig;
- sig = 'v';
- if (!reply.signature().compare(sig)) {
- if (reply.arguments().length() == 1) {
- bool ismounted;
- qDebug() << "elem 1:" << reply.arguments().at(0);
- //not sure, if its the best way to handle those variants
- ismounted = result.toBool();
- return ismounted;
- }
- }
And then get the mountpath just alike:
- "org.freedesktop.DBus.Properties", "Get");
- args.append("org.freedesktop.UDisks.Device");
- args.append("DeviceMountPaths");
- msg.setArguments(args);
- QString sig;
- sig = 'v';
- if (!reply.signature().compare(sig)) {
- if (reply.arguments().length() == 1) {
- QString mount_path;
- qDebug() << "elem 1:" << reply.arguments().at(0);
- mount_path = result.toString();
- return mount_path;
- }
- }
Hi, I am new here, and also have this problem. I add this code on my ubuntu 12.04 project.
Then it works! Thank you for your share.
So, now I got a solution:
change the slots argument to the type: QDBusObjectPath, then it will be handled correctlyExample:
"org.freedesktop.UDisks", "/org/freedesktop/UDisks", "org.freedesktop.UDisks", "DeviceAdded", qDebug() << "device added!"<<dev.path(); }In future cases, when the typing is unclear (I did not find the mapping from the “o” type in dbus to the corresponding type in qt, which is the QDBusObjectPath) just try first to receive the message as a QDBusMessage. You can print it out with qDebug and get more information about the type.
Another hint is to use the introspection with the qdbusviewer application. It will at least give you the arg type string “o” in this case.Please tell, if it works..
You must log in to post a reply. Not a member yet? Register here!

