MeeGo 1.2 Harmattan Developer Documentation Develop for the Nokia N9
   Home · All Classes · All Namespaces · Modules · Functions · Files

contact-messenger/sender.cpp

#include "sender.h"
#include "_gen/sender.moc.hpp"

#include <TelepathyQt4/Account>
#include <TelepathyQt4/Debug>
#include <TelepathyQt4/Constants>
#include <TelepathyQt4/ContactMessenger>
#include <TelepathyQt4/PendingSendMessage>
#include <TelepathyQt4/Types>

#include <QCoreApplication>

Sender::Sender(const QString &accountPath,
        const QString &contactIdentifier, const QString &message)
{
    Tp::AccountPtr acc = Tp::Account::create(TP_QT4_ACCOUNT_MANAGER_BUS_NAME,
            accountPath);
    messenger = Tp::ContactMessenger::create(acc, contactIdentifier);
    connect(messenger->sendMessage(message),
            SIGNAL(finished(Tp::PendingOperation*)),
            SLOT(onSendMessageFinished(Tp::PendingOperation*)));
}

Sender::~Sender()
{
}

void Sender::onSendMessageFinished(Tp::PendingOperation *op)
{
    if (op->isError()) {
        qDebug() << "Error sending message:" << op->errorName() << "-" << op->errorMessage();
        QCoreApplication::exit(1);
        return;
    }

    Tp::PendingSendMessage *psm = qobject_cast<Tp::PendingSendMessage *>(op);
    qDebug() << "Message sent, token is" << psm->sentMessageToken();
    QCoreApplication::exit(0);
}

int main(int argc, char **argv)
{
    QCoreApplication app(argc, argv);

    Tp::registerTypes();
    Tp::enableDebug(true);
    Tp::enableWarnings(true);

    if (argc < 4) {
        qDebug() << "Usage: contact-messenger account_path contact_id message";
        return -1;
    }

    Sender *sender = new Sender(QLatin1String(argv[1]), QLatin1String(argv[2]),
            QLatin1String(argv[3]));

    int ret = app.exec();
    delete sender;
    return ret;
}

Copyright © 2008-2011 Collabora Ltd. and Nokia Corporation
Telepathy-Qt4 0.6.5