diff options
author | Kitsune Ral <Kitsune-Ral@users.sf.net> | 2018-02-25 19:04:03 +0900 |
---|---|---|
committer | Kitsune Ral <Kitsune-Ral@users.sf.net> | 2018-02-26 09:08:04 +0900 |
commit | 41171cc181c4bc48ba2bdc39b582d0f6c2fdde0d (patch) | |
tree | 9aa33574c6aecfcffd3e85b8efc29b4fad907174 /examples/qmc-example.cpp | |
parent | 91cc0e8db0006beeb91b9e007cd21343984dfb6a (diff) | |
download | libquotient-41171cc181c4bc48ba2bdc39b582d0f6c2fdde0d.tar.gz libquotient-41171cc181c4bc48ba2bdc39b582d0f6c2fdde0d.zip |
qmc-example: Single-shot sync instead of continuous; room tags; code cleanup
Single-shot sync is now used because with that qmc-example can be used as a crude auto-testing tool.
Diffstat (limited to 'examples/qmc-example.cpp')
-rw-r--r-- | examples/qmc-example.cpp | 43 |
1 files changed, 29 insertions, 14 deletions
diff --git a/examples/qmc-example.cpp b/examples/qmc-example.cpp index dbb9912b..857c1190 100644 --- a/examples/qmc-example.cpp +++ b/examples/qmc-example.cpp @@ -4,12 +4,10 @@ #include <QCoreApplication> #include <iostream> -#include <string> using namespace QMatrixClient; using std::cout; using std::endl; -using std::string; void onNewRoom(Room* r) { @@ -17,39 +15,56 @@ void onNewRoom(Room* r) QObject::connect(r, &Room::namesChanged, [=] { cout << "Room " << r->id().toStdString() << ", name(s) changed:" << endl << " Name: " << r->name().toStdString() << endl - << " Canonical alias: " << r->canonicalAlias().toStdString() + << " Canonical alias: " << r->canonicalAlias().toStdString() << endl << endl << endl; }); + QObject::connect(r, &Room::tagsChanged, [=] { + cout << "Room " << r->id().toStdString() << ", tag(s) changed:" << endl + << " " << r->tagNames().join(", ").toStdString() << endl << endl; + }); QObject::connect(r, &Room::aboutToAddNewMessages, [=] (RoomEventsRange timeline) { cout << timeline.size() << " new event(s) in room " - << r->id().toStdString() << ":" << endl; + << r->id().toStdString() << ":" + << endl; for (const auto& item: timeline) { cout << "From: " << r->roomMembername(item->senderId()).toStdString() << endl << "Timestamp:" << item->timestamp().toString().toStdString() << endl - << "JSON:" << endl << string(item->originalJson()) << endl; + << "JSON:" << endl << item->originalJson().toStdString() << endl; } }); } +void finalize(Connection* conn) +{ + cout << "Logging out" << endl; + conn->logout(); + QObject::connect(conn, &Connection::loggedOut, QCoreApplication::instance(), + [conn] { + conn->deleteLater(); + QCoreApplication::instance()->processEvents(); + QCoreApplication::instance()->quit(); + }); +} + int main(int argc, char* argv[]) { QCoreApplication app(argc, argv); - if (argc < 2) + if (argc < 3) return -1; - auto conn = new Connection(QUrl("https://matrix.org")); + cout << "Connecting to the server as " << argv[1] << endl; + auto conn = new Connection; conn->connectToServer(argv[1], argv[2], "QMatrixClient example application"); - auto c = QObject::connect(conn, &Connection::connected, [=] { - cout << "Connected" << endl; - conn->sync(); - }); - QObject::connect(conn, &Connection::syncDone, [=] { - cout << "Sync done" << endl; - conn->sync(30000); + QObject::connect(conn, &Connection::connected, [=] { + cout << "Connected, server: " + << conn->homeserver().toDisplayString().toStdString() << endl; + cout << "Access token: " << conn->accessToken().toStdString() << endl; + conn->sync(); }); QObject::connect(conn, &Connection::newRoom, onNewRoom); + QObject::connect(conn, &Connection::syncDone, std::bind(finalize, conn)); return app.exec(); } |