Thread: [SailfishOS+Harmattan] QDBusConnection: receive/send message
View Single Post
Posts: 133 | Thanked: 405 times | Joined on Mar 2012 @ Europe
#9
Finally, it works!

If someone wants to know, how to send an XMMP-Message using Qt and DBus on Harmattan, here it is:

First declare needed typedefs

Code:
typedef QMap<QString, QVariant> KeyVarPair;
typedef QList<KeyVarPair> KeyVarPairList;
Q_DECLARE_METATYPE(KeyVarPair)
Q_DECLARE_METATYPE(KeyVarPairList)
And register them as DBus types:

Code:
qDBusRegisterMetaType<KeyVarPair>();
qDBusRegisterMetaType<KeyVarPairList>();
Now create a sendMessage function:

Code:
bool MyClass::telepathySendDBusMessage(QString _receiver, QString _account, QString _content)
{
    QString dbus_service = "org.freedesktop.Telepathy.ChannelDispatcher";
    QString dbus_path = "/org/freedesktop/Telepathy/ChannelDispatcher";
    QString dbus_interface = "org.freedesktop.Telepathy.ChannelDispatcher.Interface.Messages.DRAFT";
  
    QDBusMessage m = QDBusMessage::createMethodCall(
                                       dbus_service,
                                       dbus_path, 
                                       dbus_interface, 
                                       "SendMessage");
    

    QList<QVariant> args;

    // Path and targetID
    QDBusObjectPath path(_account);
    QString targetID = _receiver;
    args << qVariantFromValue(path);
    args << targetID;

    // Message Parts
    KeyVarPair message1;

    int type = 0;
    message1["message-type"] = qVariantFromValue(type);

    KeyVarPair message2;

    message2["content"] = qVariantFromValue(_content);
    message2["content-type"] = qVariantFromValue(QString("text/plain"));

    KeyVarPairList messageData;
    messageData << message1;
    messageData << message2;

    args << qVariantFromValue(messageData);

    // Flags
    uint flags = 0;
    args << flags;

    m.setArguments(args);
   
    QDBusMessage retVal = QDBusConnection::sessionBus().call(m);

    return true;
}
Call the message:

Code:
myClassObj->telepathySendDBusMessage(
  "to.jabber.account@jabber.server.tld",
  "/org/freedesktop/Telepathy/Account/gabble/jabber/ACCOUNT_NAME",
  "It works!");
Finally I have everything to start with the OTR implementation!
 

The Following 4 Users Say Thank You to chrm For This Useful Post: