QT DBUS挂载文件系统

时间:2012-09-16 16:44:21

标签: c++ qt filesystems mount dbus

我想使用QT和DBUS挂载文件系统。我使用这个小片段订阅了“DeviceAdded”信号:

 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();

    // ...
}

这很好用。我的问题是,我该怎么装这个东西?我所拥有的就是这样 - 它根本不起作用 - 并且没有任何错误。

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

现在,我应该在QDBusConnection :: systemBus()上使用什么操作:call,asyncCall,callWithCallback?什么必须作为第二个参数放入createMethodCall?什么都行不通!真的很沮丧!

1 个答案:

答案 0 :(得分:5)

好吧,经过至少2天的挣扎,我终于明白了!我查看了razer-qt来源,我查看了kdelibs来源,但不知怎的,他们所有的dbus内容都无效。所以这是我非常满意的片段:

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();
        }
    }
}

我正在使用Openbox以及在x64-ArchLinux上运行的最新Udisk(2)内容。也许有人也可以使用它。