QT DBUS挂载文件系统

3

我希望使用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个回答

5

好的,在挣扎了至少两天后,我终于做到了!我查看了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)。也许有人也可以用它。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接