2012-09-16 75 views
2

我想使用QT和DBUS安装文件系统。我订阅了信号“DeviceAdded”使用这个小片段:QT 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 = 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 ::系统总线()调用,asyncCall,callWithCallback?什么必须作为createMethodCall的第二个参数?没有用!真的很好!

回答

4

好吧,在挣扎了至少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和最新Udisk(2)东西在x64-ArchLinux的运行。也许有人可以使用它。