Search code examples
c++qtfilesystemsmountdbus

QT DBUS mount filesystem


I would like to mount a filesystem using QT and DBUS. I subscribed to signal "DeviceAdded" using this small snippet:

 void DBusWatcher::deviceAdded(const QDBusObjectPath &o) {
    QDBusMessage call = QDBusMessage::createMethodCall("org.freedesktop.UDisks", o.path(), "org.freedesktop.DBus.Properties", "GetAll");

    QList<QVariant> args;
    args.append("org.freedesktop.UDisks.Device");
    call.setArguments(args);

    QDBusPendingReply<QVariantMap> reply = DBusConnection::systemBus().asyncCall(call);
    reply.waitForFinished();

    QVariantMap map = reply.value();

    // ...
}

That works pretty fine. My question is, how do I mount this thing? All I have is something like this - and it does not work at all - and with no errors.

QDBusMessage call = QDBusMessage::createMethodCall("org.freedesktop.UDisks", "dont know what to put here!", "org.freedesktop.UDisks.Device", "FilesystemMount");

And now, what action should I use on QDBusConnection::systemBus(): call, asyncCall, callWithCallback? What has to be put as second argument into createMethodCall? Nothing works! Really fustrating!


Solution

  • OK, after struggling for at least 2 days I finally got it! I looked into razer-qt sources, I looked into kdelibs sources but somehow all their dbus stuff did not work. So here is the snippet I'm pretty happy with:

    void DBusWatcher::deviceAdded(const QDBusObjectPath &o) {
        QDBusMessage call = QDBusMessage::createMethodCall("org.freedesktop.UDisks", o.path(), "org.freedesktop.DBus.Properties", "GetAll");
    
        QList<QVariant> args;
        args.append("org.freedesktop.UDisks.Device");
        call.setArguments(args);
    
        QDBusPendingReply<QVariantMap> reply = QDBusConnection::systemBus().asyncCall(call);
        reply.waitForFinished();
    
        QVariantMap map = reply.value();
        // now do what you want with the map ;)
        // You will find all available information to the device attached
    }
    
    // a class wide pointer to the systembus
    // initialized within the constructor of the class
    // and deleted in the destructor
    dbus = new QDBusInterface(
        "org.freedesktop.UDisks",
        "here comes the path from the QDBusObjectPath.path() object",
        "org.freedesktop.UDisks.Device",
        QDBusConnection::systemBus(),
        this
    );
    
    void DbusAction::mountFilesystem() {
        if(dbus->isValid()) {
    
            QList<QVariant> args;
            args << QVariant(QString()) << QVariant(QStringList());
    
            QDBusMessage msg = dbus->callWithArgumentList(QDBus::AutoDetect, "FilesystemMount", args);
            if(msg.type() == QDBusMessage::ReplyMessage) {
                QString path = msg.arguments().at(0).toString();
                if(!path.isEmpty()) {
                    emit deviceMounted(path);
                } else {
                    qDebug() << "sorry, but the path returned is empty";
                }
            } else {
                qDebug() << msg.errorMessage();
            }
        }
    }
    

    I'm using Openbox and the latest Udisk(2) stuff running on x64-ArchLinux. Maybe someone can use it too.