2012-09-16 78 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的運行。也許有人可以使用它。