diff options
author | Kitsune Ral <KitsuneRal@users.noreply.github.com> | 2016-04-05 20:46:45 +0300 |
---|---|---|
committer | Kitsune Ral <KitsuneRal@users.noreply.github.com> | 2016-04-05 20:46:45 +0300 |
commit | 7abdc7ec386776602758d84edc6b583d6dad4ecd (patch) | |
tree | 247b6affaa7828e5122f1c338dd35a729cc4ee19 | |
download | libquotient-7abdc7ec386776602758d84edc6b583d6dad4ecd.tar.gz libquotient-7abdc7ec386776602758d84edc6b583d6dad4ecd.zip |
Imported the current source tree from Quaternion/lib.
61 files changed, 4791 insertions, 0 deletions
diff --git a/connection.cpp b/connection.cpp new file mode 100644 index 00000000..01392848 --- /dev/null +++ b/connection.cpp @@ -0,0 +1,165 @@ +/****************************************************************************** + * Copyright (C) 2015 Felix Rohrbach <kde@fxrh.de> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "connection.h" +#include "connectiondata.h" +#include "connectionprivate.h" +#include "user.h" +#include "events/event.h" +#include "room.h" +#include "jobs/passwordlogin.h" +#include "jobs/geteventsjob.h" +#include "jobs/postmessagejob.h" +#include "jobs/postreceiptjob.h" +#include "jobs/joinroomjob.h" +#include "jobs/leaveroomjob.h" +#include "jobs/roommembersjob.h" +#include "jobs/roommessagesjob.h" +#include "jobs/syncjob.h" +#include "jobs/mediathumbnailjob.h" + +#include <QtCore/QDebug> + +using namespace QMatrixClient; + +Connection::Connection(QUrl server, QObject* parent) + : QObject(parent) +{ + d = new ConnectionPrivate(this); + d->data = new ConnectionData(server); +} + +Connection::~Connection() +{ + delete d; +} + +void Connection::connectToServer(QString user, QString password) +{ + PasswordLogin* loginJob = new PasswordLogin(d->data, user, password); + connect( loginJob, &PasswordLogin::result, d, &ConnectionPrivate::connectDone ); + loginJob->start(); + d->username = user; // to be able to reconnect + d->password = password; +} + +void Connection::reconnect() +{ + PasswordLogin* loginJob = new PasswordLogin(d->data, d->username, d->password ); + connect( loginJob, &PasswordLogin::result, d, &ConnectionPrivate::reconnectDone ); + loginJob->start(); +} + +SyncJob* Connection::sync(int timeout) +{ + QString filter = "{\"room\": { \"timeline\": { \"limit\": 100 } } }"; + SyncJob* syncJob = new SyncJob(d->data, d->data->lastEvent()); + syncJob->setFilter(filter); + syncJob->setTimeout(timeout); + connect( syncJob, &SyncJob::result, d, &ConnectionPrivate::syncDone ); + syncJob->start(); + return syncJob; +} + +void Connection::postMessage(Room* room, QString type, QString message) +{ + PostMessageJob* job = new PostMessageJob(d->data, room, type, message); + job->start(); +} + +PostReceiptJob* Connection::postReceipt(Room* room, Event* event) +{ + PostReceiptJob* job = new PostReceiptJob(d->data, room->id(), event->id()); + job->start(); + return job; +} + +void Connection::joinRoom(QString roomAlias) +{ + JoinRoomJob* job = new JoinRoomJob(d->data, roomAlias); + connect( job, &JoinRoomJob::result, d, &ConnectionPrivate::gotJoinRoom ); + job->start(); +} + +void Connection::leaveRoom(Room* room) +{ + LeaveRoomJob* job = new LeaveRoomJob(d->data, room); + job->start(); +} + +void Connection::getMembers(Room* room) +{ + RoomMembersJob* job = new RoomMembersJob(d->data, room); + connect( job, &RoomMembersJob::result, d, &ConnectionPrivate::gotRoomMembers ); + job->start(); +} + +RoomMessagesJob* Connection::getMessages(Room* room, QString from) +{ + RoomMessagesJob* job = new RoomMessagesJob(d->data, room, from); + job->start(); + return job; +} + +MediaThumbnailJob* Connection::getThumbnail(QUrl url, int requestedWidth, int requestedHeight) +{ + MediaThumbnailJob* job = new MediaThumbnailJob(d->data, url, requestedWidth, requestedHeight); + job->start(); + return job; +} + +User* Connection::user(QString userId) +{ + if( d->userMap.contains(userId) ) + return d->userMap.value(userId); + User* user = createUser(userId); + d->userMap.insert(userId, user); + return user; +} + +User *Connection::user() +{ + if( d->userId.isEmpty() ) + return 0; + return user(d->userId); +} + +QHash< QString, Room* > Connection::roomMap() const +{ + return d->roomMap; +} + +bool Connection::isConnected() +{ + return d->isConnected; +} + +ConnectionData* Connection::connectionData() +{ + return d->data; +} + +User* Connection::createUser(QString userId) +{ + return new User(userId, this); +} + +Room* Connection::createRoom(QString roomId) +{ + return new Room(this, roomId); +} diff --git a/connection.h b/connection.h new file mode 100644 index 00000000..afe124f9 --- /dev/null +++ b/connection.h @@ -0,0 +1,93 @@ +/****************************************************************************** + * Copyright (C) 2015 Felix Rohrbach <kde@fxrh.de> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef QMATRIXCLIENT_CONNECTION_H +#define QMATRIXCLIENT_CONNECTION_H + +#include <QtCore/QObject> + +namespace QMatrixClient +{ + class Room; + class User; + class Event; + class ConnectionPrivate; + class ConnectionData; + + class SyncJob; + class RoomMessagesJob; + class PostReceiptJob; + class MediaThumbnailJob; + + class Connection: public QObject { + Q_OBJECT + public: + Connection(QUrl server, QObject* parent=0); + virtual ~Connection(); + + QHash<QString, Room*> roomMap() const; + virtual bool isConnected(); + + virtual void connectToServer( QString user, QString password ); + virtual void reconnect(); + virtual SyncJob* sync(int timeout=-1); + virtual void postMessage( Room* room, QString type, QString message ); + virtual PostReceiptJob* postReceipt( Room* room, Event* event ); + virtual void joinRoom( QString roomAlias ); + virtual void leaveRoom( Room* room ); + virtual void getMembers( Room* room ); + virtual RoomMessagesJob* getMessages( Room* room, QString from ); + virtual MediaThumbnailJob* getThumbnail( QUrl url, int requestedWidth, int requestedHeight ); + + virtual User* user(QString userId); + virtual User* user(); + + signals: + void connected(); + void reconnected(); + void syncDone(); + void newRoom(Room* room); + void joinedRoom(Room* room); + + void loginError(QString error); + void connectionError(QString error); + //void jobError(BaseJob* job); + + protected: + /** + * Access the underlying ConnectionData class + */ + ConnectionData* connectionData(); + + /** + * makes it possible for derived classes to have its own User class + */ + virtual User* createUser(QString userId); + + /** + * makes it possible for derived classes to have its own Room class + */ + virtual Room* createRoom(QString roomId); + + private: + friend class ConnectionPrivate; + ConnectionPrivate* d; + }; +} + +#endif // QMATRIXCLIENT_CONNECTION_H
\ No newline at end of file diff --git a/connectiondata.cpp b/connectiondata.cpp new file mode 100644 index 00000000..374484b0 --- /dev/null +++ b/connectiondata.cpp @@ -0,0 +1,83 @@ +/****************************************************************************** + * Copyright (C) 2015 Felix Rohrbach <kde@fxrh.de> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "connectiondata.h" + +#include <QtNetwork/QNetworkAccessManager> + +using namespace QMatrixClient; + +class ConnectionData::Private +{ + public: + Private() {/*isConnected=false;*/} + + QUrl baseUrl; + //bool isConnected; + QString token; + QString lastEvent; + QNetworkAccessManager* nam; +}; + +ConnectionData::ConnectionData(QUrl baseUrl) + : d(new Private) +{ + d->baseUrl = baseUrl; + d->nam = new QNetworkAccessManager(); +} + +ConnectionData::~ConnectionData() +{ + d->nam->deleteLater(); + delete d; +} + +// bool ConnectionData::isConnected() const +// { +// return d->isConnected; +// } + +QString ConnectionData::token() const +{ + return d->token; +} + +QUrl ConnectionData::baseUrl() const +{ + return d->baseUrl; +} + +QNetworkAccessManager* ConnectionData::nam() const +{ + return d->nam; +} + +void ConnectionData::setToken(QString token) +{ + d->token = token; +} + +QString ConnectionData::lastEvent() const +{ + return d->lastEvent; +} + +void ConnectionData::setLastEvent(QString identifier) +{ + d->lastEvent = identifier; +}
\ No newline at end of file diff --git a/connectiondata.h b/connectiondata.h new file mode 100644 index 00000000..e11f54b5 --- /dev/null +++ b/connectiondata.h @@ -0,0 +1,50 @@ +/****************************************************************************** + * Copyright (C) 2015 Felix Rohrbach <kde@fxrh.de> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef QMATRIXCLIENT_CONNECTIONDATA_H +#define QMATRIXCLIENT_CONNECTIONDATA_H + +#include <QtCore/QUrl> + +class QNetworkAccessManager; + +namespace QMatrixClient +{ + class ConnectionData + { + public: + ConnectionData(QUrl baseUrl); + virtual ~ConnectionData(); + + //bool isConnected() const; + QString token() const; + QUrl baseUrl() const; + + QNetworkAccessManager* nam() const; + void setToken( QString token ); + + QString lastEvent() const; + void setLastEvent( QString identifier ); + + private: + class Private; + Private* d; + }; +} + +#endif // QMATRIXCLIENT_CONNECTIONDATA_H
\ No newline at end of file diff --git a/connectionprivate.cpp b/connectionprivate.cpp new file mode 100644 index 00000000..90733ed0 --- /dev/null +++ b/connectionprivate.cpp @@ -0,0 +1,178 @@ +/****************************************************************************** + * Copyright (C) 2015 Felix Rohrbach <kde@fxrh.de> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "connectionprivate.h" +#include "connection.h" +#include "state.h" +#include "room.h" +#include "user.h" +#include "jobs/passwordlogin.h" +#include "jobs/syncjob.h" +#include "jobs/geteventsjob.h" +#include "jobs/joinroomjob.h" +#include "jobs/roommembersjob.h" +#include "events/event.h" +#include "events/roommessageevent.h" +#include "events/roommemberevent.h" + +#include <QtCore/QDebug> + +using namespace QMatrixClient; + +ConnectionPrivate::ConnectionPrivate(Connection* parent) + : q(parent) +{ + isConnected = false; + data = 0; +} + +ConnectionPrivate::~ConnectionPrivate() +{ + delete data; +} + +void ConnectionPrivate::processState(State* state) +{ + QString roomId = state->event()->roomId(); + if( state->event()->type() == QMatrixClient::EventType::RoomMember ) + { + QMatrixClient::RoomMemberEvent* e = static_cast<QMatrixClient::RoomMemberEvent*>(state->event()); + User* user = q->user(e->userId()); + user->processEvent(e); + } + if( !roomId.isEmpty() ) + { + Room* room; + if( !roomMap.contains(roomId) ) + { + room = q->createRoom(roomId); + roomMap.insert( roomId, room ); + emit q->newRoom(room); + } else { + room = roomMap.value(roomId); + } + room->addInitialState(state); + } +} + +void ConnectionPrivate::processRooms(const QList<SyncRoomData>& data) +{ + for( const SyncRoomData& roomData: data ) + { + Room* room; + if( !roomMap.contains(roomData.roomId) ) + { + room = q->createRoom(roomData.roomId); + roomMap.insert( roomData.roomId, room ); + emit q->newRoom(room); + } else { + room = roomMap.value(roomData.roomId); + } + room->updateData(roomData); + } +} + +void ConnectionPrivate::connectDone(KJob* job) +{ + PasswordLogin* realJob = static_cast<PasswordLogin*>(job); + if( !realJob->error() ) + { + isConnected = true; + userId = realJob->id(); + qDebug() << "Our user ID: " << userId; + emit q->connected(); + } + else { + emit q->loginError( job->errorString() ); + } +} + +void ConnectionPrivate::reconnectDone(KJob* job) +{ + PasswordLogin* realJob = static_cast<PasswordLogin*>(job); + if( !realJob->error() ) + { + userId = realJob->id(); + emit q->reconnected(); + } + else { + emit q->loginError( job->errorString() ); + isConnected = false; + } +} + +void ConnectionPrivate::syncDone(KJob* job) +{ + SyncJob* syncJob = static_cast<SyncJob*>(job); + if( !syncJob->error() ) + { + data->setLastEvent(syncJob->nextBatch()); + processRooms(syncJob->roomData()); + emit q->syncDone(); + } + else { + if( syncJob->error() == BaseJob::NetworkError ) + emit q->connectionError( syncJob->errorString() ); + else + qDebug() << "syncJob failed, error:" << syncJob->error(); + } +} + +void ConnectionPrivate::gotJoinRoom(KJob* job) +{ + qDebug() << "gotJoinRoom"; + JoinRoomJob* joinJob = static_cast<JoinRoomJob*>(job); + if( !joinJob->error() ) + { + QString roomId = joinJob->roomId(); + Room* room; + if( roomMap.contains(roomId) ) + { + room = roomMap.value(roomId); + } else { + room = q->createRoom(roomId); + roomMap.insert( roomId, room ); + emit q->newRoom(room); + } + emit q->joinedRoom(room); + } + else + { + if( joinJob->error() == BaseJob::NetworkError ) + emit q->connectionError( joinJob->errorString() ); + } +} + +void ConnectionPrivate::gotRoomMembers(KJob* job) +{ + RoomMembersJob* membersJob = static_cast<RoomMembersJob*>(job); + if( !membersJob->error() ) + { + for( State* state: membersJob->states() ) + { + processState(state); + } + qDebug() << membersJob->states().count() << " processed..."; + } + else + { + qDebug() << "MembersJob error: " <<membersJob->errorString(); + if( membersJob->error() == BaseJob::NetworkError ) + emit q->connectionError( membersJob->errorString() ); + } +}
\ No newline at end of file diff --git a/connectionprivate.h b/connectionprivate.h new file mode 100644 index 00000000..2aeeaf0b --- /dev/null +++ b/connectionprivate.h @@ -0,0 +1,67 @@ +/****************************************************************************** + * Copyright (C) 2015 Felix Rohrbach <kde@fxrh.de> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef QMATRIXCLIENT_CONNECTIONPRIVATE_H +#define QMATRIXCLIENT_CONNECTIONPRIVATE_H + +class KJob; + +#include <QtCore/QObject> +#include <QtCore/QHash> +#include <QtCore/QJsonObject> + +#include "connection.h" +#include "connectiondata.h" +#include "jobs/syncjob.h" + +namespace QMatrixClient +{ + class Connection; + class Event; + class State; + class User; + + class ConnectionPrivate : public QObject + { + Q_OBJECT + public: + ConnectionPrivate(Connection* parent); + ~ConnectionPrivate(); + + void processState( State* state ); + void processRooms( const QList<SyncRoomData>& data ); + + Connection* q; + ConnectionData* data; + QHash<QString, Room*> roomMap; + QHash<QString, User*> userMap; + bool isConnected; + QString username; + QString password; + QString userId; + + public slots: + void connectDone(KJob* job); + void reconnectDone(KJob* job); + void syncDone(KJob* job); + void gotJoinRoom(KJob* job); + void gotRoomMembers(KJob* job); + }; +} + +#endif // QMATRIXCLIENT_CONNECTIONPRIVATE_H
\ No newline at end of file diff --git a/events/event.cpp b/events/event.cpp new file mode 100644 index 00000000..c3e94ce1 --- /dev/null +++ b/events/event.cpp @@ -0,0 +1,148 @@ +/****************************************************************************** + * Copyright (C) 2015 Felix Rohrbach <kde@fxrh.de> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "event.h" + +#include <QtCore/QJsonObject> +#include <QtCore/QJsonDocument> +#include <QtCore/QDateTime> +#include <QtCore/QDebug> + +#include "roommessageevent.h" +#include "roomnameevent.h" +#include "roomaliasesevent.h" +#include "roomcanonicalaliasevent.h" +#include "roommemberevent.h" +#include "roomtopicevent.h" +#include "typingevent.h" +#include "receiptevent.h" +#include "unknownevent.h" + +using namespace QMatrixClient; + +class Event::Private +{ + public: + EventType type; + QString id; + QDateTime timestamp; + QString roomId; + QString originalJson; +}; + +Event::Event(EventType type) + : d(new Private) +{ + d->type = type; +} + +Event::~Event() +{ + delete d; +} + +EventType Event::type() const +{ + return d->type; +} + +QString Event::id() const +{ + return d->id; +} + +QDateTime Event::timestamp() const +{ + return d->timestamp; +} + +QString Event::roomId() const +{ + return d->roomId; +} + +QString Event::originalJson() const +{ + return d->originalJson; +} + +Event* Event::fromJson(const QJsonObject& obj) +{ + //qDebug() << obj.value("type").toString(); + if( obj.value("type").toString() == "m.room.message" ) + { + return RoomMessageEvent::fromJson(obj); + } + if( obj.value("type").toString() == "m.room.name" ) + { + return RoomNameEvent::fromJson(obj); + } + if( obj.value("type").toString() == "m.room.aliases" ) + { + return RoomAliasesEvent::fromJson(obj); + } + if( obj.value("type").toString() == "m.room.canonical_alias" ) + { + return RoomCanonicalAliasEvent::fromJson(obj); + } + if( obj.value("type").toString() == "m.room.member" ) + { + return RoomMemberEvent::fromJson(obj); + } + if( obj.value("type").toString() == "m.room.topic" ) + { + return RoomTopicEvent::fromJson(obj); + } + if( obj.value("type").toString() == "m.typing" ) + { + qDebug() << "m.typing..."; + return TypingEvent::fromJson(obj); + } + if( obj.value("type").toString() == "m.receipt" ) + { + return ReceiptEvent::fromJson(obj); + } + //qDebug() << "Unknown event"; + return UnknownEvent::fromJson(obj); +} + +bool Event::parseJson(const QJsonObject& obj) +{ + d->originalJson = QString::fromUtf8(QJsonDocument(obj).toJson()); + bool correct = true; + if( obj.contains("event_id") ) + { + d->id = obj.value("event_id").toString(); + } else { + correct = false; + qDebug() << "Event: can't find event_id"; + } + if( obj.contains("origin_server_ts") ) + { + d->timestamp = QDateTime::fromMSecsSinceEpoch( (quint64) obj.value("origin_server_ts").toDouble(), Qt::UTC ); + } else { + correct = false; + qDebug() << "Event: can't find ts"; + //qDebug() << obj; + } + if( obj.contains("room_id") ) + { + d->roomId = obj.value("room_id").toString(); + } + return correct; +} diff --git a/events/event.h b/events/event.h new file mode 100644 index 00000000..b25b1378 --- /dev/null +++ b/events/event.h @@ -0,0 +1,58 @@ +/****************************************************************************** + * Copyright (C) 2015 Felix Rohrbach <kde@fxrh.de> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef QMATRIXCLIENT_EVENT_H +#define QMATRIXCLIENT_EVENT_H + +#include <QtCore/QString> +#include <QtCore/QDateTime> +#include <QtCore/QJsonObject> + +namespace QMatrixClient +{ + enum class EventType + { + RoomMessage, RoomName, RoomAliases, RoomCanonicalAlias, + RoomMember, RoomTopic, Typing, Receipt, Unknown + }; + + class Event + { + public: + Event(EventType type); + virtual ~Event(); + + EventType type() const; + QString id() const; + QDateTime timestamp() const; + QString roomId() const; + // only for debug purposes! + QString originalJson() const; + + static Event* fromJson(const QJsonObject& obj); + + protected: + bool parseJson(const QJsonObject& obj); + + private: + class Private; + Private* d; + }; +} + +#endif // QMATRIXCLIENT_EVENT_H diff --git a/events/receiptevent.cpp b/events/receiptevent.cpp new file mode 100644 index 00000000..d2a27079 --- /dev/null +++ b/events/receiptevent.cpp @@ -0,0 +1,97 @@ +/****************************************************************************** + * Copyright (C) 2016 Felix Rohrbach <kde@fxrh.de> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/* +Example of a Receipt Event: +{ + "content": { + "$1435641916114394fHBLK:matrix.org": { + "m.read": { + "@rikj:jki.re": { + "ts": 1436451550453 + } + } + } + }, + "room_id": "!KpjVgQyZpzBwvMBsnT:matrix.org", + "type": "m.receipt" +} +*/ + +#include "receiptevent.h" + +#include <QtCore/QJsonObject> +#include <QtCore/QJsonArray> +#include <QtCore/QDebug> + +using namespace QMatrixClient; + +Receipt::Receipt(QString event, QString user, QDateTime time) + : eventId(event) + , userId(user) + , timestamp(time) +{ +} + +class ReceiptEvent::Private +{ + public: + QHash<QString, QList<Receipt>> eventToReceipts; +}; + +ReceiptEvent::ReceiptEvent() + : Event(EventType::Receipt) + , d(new Private) +{ +} + +ReceiptEvent::~ReceiptEvent() +{ + delete d; +} + +QList<Receipt> ReceiptEvent::receiptsForEvent(QString eventId) const +{ + return d->eventToReceipts.value(eventId); +} + +QStringList ReceiptEvent::events() const +{ + return d->eventToReceipts.keys(); +} + +ReceiptEvent* ReceiptEvent::fromJson(const QJsonObject& obj) +{ + ReceiptEvent* e = new ReceiptEvent(); + e->parseJson(obj); + const QJsonObject contents = obj.value("content").toObject(); + for( const QString& eventId: contents.keys() ) + { + QJsonObject reads = contents.value(eventId).toObject().value("m.read").toObject(); + QList<Receipt> receipts; + for( const QString& userId: reads.keys() ) + { + QJsonObject user = reads.value(userId).toObject(); + QDateTime time = QDateTime::fromMSecsSinceEpoch( (quint64) user.value("ts").toDouble(), Qt::UTC ); + Receipt receipt(eventId, userId, time); + receipts.append(receipt); + } + e->d->eventToReceipts.insert(eventId, receipts); + } + return e; +} diff --git a/events/receiptevent.h b/events/receiptevent.h new file mode 100644 index 00000000..aa72ba90 --- /dev/null +++ b/events/receiptevent.h @@ -0,0 +1,56 @@ +/****************************************************************************** + * Copyright (C) 2016 Felix Rohrbach <kde@fxrh.de> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef QMATRIXCLIENT_RECEIPTEVENT_H +#define QMATRIXCLIENT_RECEIPTEVENT_H + +#include "event.h" + +#include <QtCore/QStringList> + +namespace QMatrixClient +{ + class Receipt + { + public: + QString eventId; + QString userId; + QDateTime timestamp; + + Receipt(QString event, QString user, QDateTime time); + }; + + class ReceiptEvent: public Event + { + public: + ReceiptEvent(); + virtual ~ReceiptEvent(); + + QList<Receipt> receiptsForEvent(QString eventId) const; + + QStringList events() const; + + static ReceiptEvent* fromJson(const QJsonObject& obj); + + private: + class Private; + Private* d; + }; +} + +#endif // QMATRIXCLIENT_RECEIPTEVENT_H diff --git a/events/roomaliasesevent.cpp b/events/roomaliasesevent.cpp new file mode 100644 index 00000000..6bbe0aff --- /dev/null +++ b/events/roomaliasesevent.cpp @@ -0,0 +1,78 @@ +/****************************************************************************** + * Copyright (C) 2015 Felix Rohrbach <kde@fxrh.de> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +// Example of a RoomAliases Event: +// +// { +// "age":3758857346, +// "content":{ +// "aliases":["#freenode_#testest376:matrix.org"] +// }, +// "event_id":"$1439989428122drFjY:matrix.org", +// "origin_server_ts":1439989428910, +// "replaces_state":"$143613875199223YYPrN:matrix.org", +// "room_id":"!UoqtanuuSGTMvNRfDG:matrix.org", +// "state_key":"matrix.org", +// "type":"m.room.aliases", +// "user_id":"@appservice-irc:matrix.org" +// } + +#include "roomaliasesevent.h" + +#include <QtCore/QJsonObject> +#include <QtCore/QJsonArray> +#include <QtCore/QDebug> + +using namespace QMatrixClient; + +class RoomAliasesEvent::Private +{ + public: + QStringList aliases; +}; + +RoomAliasesEvent::RoomAliasesEvent() + : Event(EventType::RoomAliases) + , d(new Private) +{ +} + +RoomAliasesEvent::~RoomAliasesEvent() +{ + delete d; +} + +QStringList RoomAliasesEvent::aliases() const +{ + return d->aliases; +} + +RoomAliasesEvent* RoomAliasesEvent::fromJson(const QJsonObject& obj) +{ + qDebug() << "!!!!"; + RoomAliasesEvent* e = new RoomAliasesEvent(); + e->parseJson(obj); + const QJsonObject contents = obj.value("content").toObject(); + const QJsonArray aliases = contents.value("aliases").toArray(); + for( const QJsonValue& alias : aliases ) + { + e->d->aliases << alias.toString(); + } + qDebug() << e->d->aliases; + return e; +} diff --git a/events/roomaliasesevent.h b/events/roomaliasesevent.h new file mode 100644 index 00000000..89ea63b8 --- /dev/null +++ b/events/roomaliasesevent.h @@ -0,0 +1,44 @@ +/****************************************************************************** + * Copyright (C) 2015 Felix Rohrbach <kde@fxrh.de> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef QMATRIXCLIENT_ROOMALIASESEVENT_H +#define QMATRIXCLIENT_ROOMALIASESEVENT_H + +#include "event.h" + +#include <QtCore/QStringList> + +namespace QMatrixClient +{ + class RoomAliasesEvent: public Event + { + public: + RoomAliasesEvent(); + virtual ~RoomAliasesEvent(); + + QStringList aliases() const; + + static RoomAliasesEvent* fromJson(const QJsonObject& obj); + + private: + class Private; + Private* d; + }; +} + +#endif // QMATRIXCLIENT_ROOMALIASESEVENT_H
\ No newline at end of file diff --git a/events/roomcanonicalaliasevent.cpp b/events/roomcanonicalaliasevent.cpp new file mode 100644 index 00000000..d84c07fc --- /dev/null +++ b/events/roomcanonicalaliasevent.cpp @@ -0,0 +1,53 @@ +/****************************************************************************** + * Copyright (C) 2016 Felix Rohrbach <kde@fxrh.de> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "roomcanonicalaliasevent.h" + +using namespace QMatrixClient; + +class RoomCanonicalAliasEvent::Private +{ + public: + QString alias; +}; + +RoomCanonicalAliasEvent::RoomCanonicalAliasEvent() + : Event(EventType::RoomCanonicalAlias) + , d(new Private) +{ +} + +RoomCanonicalAliasEvent::~RoomCanonicalAliasEvent() +{ + delete d; +} + +QString RoomCanonicalAliasEvent::alias() +{ + return d->alias; +} + +RoomCanonicalAliasEvent* RoomCanonicalAliasEvent::fromJson(const QJsonObject& obj) +{ + RoomCanonicalAliasEvent* e = new RoomCanonicalAliasEvent(); + e->parseJson(obj); + const QJsonObject contents = obj.value("content").toObject(); + e->d->alias = contents.value("alias").toString(); + return e; +} + diff --git a/events/roomcanonicalaliasevent.h b/events/roomcanonicalaliasevent.h new file mode 100644 index 00000000..f3ab8e5a --- /dev/null +++ b/events/roomcanonicalaliasevent.h @@ -0,0 +1,42 @@ +/****************************************************************************** + * Copyright (C) 2016 Felix Rohrbach <kde@fxrh.de> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef QMATRIXCLIENT_ROOMCANONICALALIASEVENT_H +#define QMATRIXCLIENT_ROOMCANONICALALIASEVENT_H + +#include "event.h" + +namespace QMatrixClient +{ + class RoomCanonicalAliasEvent: public Event + { + public: + RoomCanonicalAliasEvent(); + virtual ~RoomCanonicalAliasEvent(); + + QString alias(); + + static RoomCanonicalAliasEvent* fromJson(const QJsonObject& obj); + + private: + class Private; + Private* d; + }; +} + +#endif // QMATRIXCLIENT_ROOMCANONICALALIASEVENT_H
\ No newline at end of file diff --git a/events/roommemberevent.cpp b/events/roommemberevent.cpp new file mode 100644 index 00000000..e58bda8f --- /dev/null +++ b/events/roommemberevent.cpp @@ -0,0 +1,88 @@ +/****************************************************************************** + * Copyright (C) 2015 Felix Rohrbach <kde@fxrh.de> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "roommemberevent.h" + +#include <QtCore/QDebug> +#include <QtCore/QUrl> + +using namespace QMatrixClient; + +class RoomMemberEvent::Private +{ + public: + MembershipType membership; + QString userId; + QString displayname; + QUrl avatarUrl; +}; + +RoomMemberEvent::RoomMemberEvent() + : Event(EventType::RoomMember) + , d(new Private) +{ +} + +RoomMemberEvent::~RoomMemberEvent() +{ + delete d; +} + +MembershipType RoomMemberEvent::membership() const +{ + return d->membership; +} + +QString RoomMemberEvent::userId() const +{ + return d->userId; +} + +QString RoomMemberEvent::displayName() const +{ + return d->displayname; +} + +QUrl RoomMemberEvent::avatarUrl() const +{ + return d->avatarUrl; +} + +RoomMemberEvent* RoomMemberEvent::fromJson(const QJsonObject& obj) +{ + RoomMemberEvent* e = new RoomMemberEvent(); + e->parseJson(obj); + e->d->userId = obj.value("state_key").toString(); + QJsonObject content = obj.value("content").toObject(); + e->d->displayname = content.value("displayname").toString(); + QString membershipString = content.value("membership").toString(); + if( membershipString == "invite" ) + e->d->membership = MembershipType::Invite; + else if( membershipString == "join" ) + e->d->membership = MembershipType::Join; + else if( membershipString == "knock" ) + e->d->membership = MembershipType::Knock; + else if( membershipString == "leave" ) + e->d->membership = MembershipType::Leave; + else if( membershipString == "ban" ) + e->d->membership = MembershipType::Ban; + else + qDebug() << "Unknown MembershipType: " << membershipString; + e->d->avatarUrl = QUrl(content.value("avatar_url").toString()); + return e; +} diff --git a/events/roommemberevent.h b/events/roommemberevent.h new file mode 100644 index 00000000..e47013cb --- /dev/null +++ b/events/roommemberevent.h @@ -0,0 +1,50 @@ +/****************************************************************************** + * Copyright (C) 2015 Felix Rohrbach <kde@fxrh.de> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef QMATRIXCLIENT_ROOMMEMBEREVENT_H +#define QMATRIXCLIENT_ROOMMEMBEREVENT_H + +#include <QtCore/QJsonObject> +#include <QtCore/QUrl> + +#include "event.h" + +namespace QMatrixClient +{ + enum class MembershipType {Invite, Join, Knock, Leave, Ban}; + + class RoomMemberEvent: public Event + { + public: + RoomMemberEvent(); + virtual ~RoomMemberEvent(); + + MembershipType membership() const; + QString userId() const; + QString displayName() const; + QUrl avatarUrl() const; + + static RoomMemberEvent* fromJson(const QJsonObject& obj); + + private: + class Private; + Private* d; + }; +} + +#endif // QMATRIXCLIENT_ROOMMEMBEREVENT_H diff --git a/events/roommessageevent.cpp b/events/roommessageevent.cpp new file mode 100644 index 00000000..ea03986b --- /dev/null +++ b/events/roommessageevent.cpp @@ -0,0 +1,101 @@ +/****************************************************************************** + * Copyright (C) 2015 Felix Rohrbach <kde@fxrh.de> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "roommessageevent.h" + +#include <QtCore/QJsonObject> +#include <QtCore/QDateTime> +#include <QtCore/QDebug> + +using namespace QMatrixClient; + +class RoomMessageEvent::Private +{ + public: + Private() {} + + QString userId; + QString body; + QString msgtype; + QDateTime hsob_ts; +}; + +RoomMessageEvent::RoomMessageEvent() + : Event(EventType::RoomMessage) + , d(new Private) +{ + +} + +RoomMessageEvent::~RoomMessageEvent() +{ + delete d; +} + +QString RoomMessageEvent::userId() const +{ + return d->userId; +} + +QString RoomMessageEvent::msgtype() const +{ + return d->msgtype; +} + +QString RoomMessageEvent::body() const +{ + return d->body; +} + +QDateTime RoomMessageEvent::hsob_ts() const +{ + return d->hsob_ts; +} + +RoomMessageEvent* RoomMessageEvent::fromJson(const QJsonObject& obj) +{ + RoomMessageEvent* e = new RoomMessageEvent(); + e->parseJson(obj); + if( obj.contains("sender") ) + { + e->d->userId = obj.value("sender").toString(); + } else { + qDebug() << "RoomMessageEvent: user_id not found"; + } + if( obj.contains("content") ) + { + QJsonObject content = obj.value("content").toObject(); + if( content.contains("msgtype") ) + { + e->d->msgtype = content.value("msgtype").toString(); + } else { + qDebug() << "RoomMessageEvent: msgtype not found"; + } + if( content.contains("body") ) + { + e->d->body = content.value("body").toString(); + } else { + qDebug() << "RoomMessageEvent: body not found"; + } +// e->d->hsob_ts = QDateTime::fromMSecsSinceEpoch( content.value("hsoc_ts").toInt() ); +// } else { +// qDebug() << "RoomMessageEvent: hsoc_ts not found"; +// } + } + return e; +}
\ No newline at end of file diff --git a/events/roommessageevent.h b/events/roommessageevent.h new file mode 100644 index 00000000..dcc5b547 --- /dev/null +++ b/events/roommessageevent.h @@ -0,0 +1,45 @@ +/****************************************************************************** + * Copyright (C) 2015 Felix Rohrbach <kde@fxrh.de> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef QMATRIXCLIENT_ROOMMESSAGEEVENT_H +#define QMATRIXCLIENT_ROOMMESSAGEEVENT_H + +#include "event.h" + +namespace QMatrixClient +{ + class RoomMessageEvent: public Event + { + public: + RoomMessageEvent(); + virtual ~RoomMessageEvent(); + + QString userId() const; + QString msgtype() const; + QString body() const; + QDateTime hsob_ts() const; + + static RoomMessageEvent* fromJson( const QJsonObject& obj ); + + private: + class Private; + Private* d; + }; +} + +#endif // QMATRIXCLIENT_ROOMMESSAGEEVENT_H
\ No newline at end of file diff --git a/events/roomnameevent.cpp b/events/roomnameevent.cpp new file mode 100644 index 00000000..f4c31edf --- /dev/null +++ b/events/roomnameevent.cpp @@ -0,0 +1,51 @@ +/******************************************************************************
+ * Copyright (C) 2015 Kitsune Ral <kitsune-ral@users.sf.net>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "roomnameevent.h"
+
+using namespace QMatrixClient;
+
+struct RoomNameEvent::Private
+{
+ QString name;
+};
+
+RoomNameEvent::RoomNameEvent() :
+ Event(EventType::RoomName),
+ d(new Private)
+{
+}
+
+RoomNameEvent::~RoomNameEvent()
+{
+ delete d;
+}
+
+QString RoomNameEvent::name() const
+{
+ return d->name;
+}
+
+RoomNameEvent* RoomNameEvent::fromJson(const QJsonObject& obj)
+{
+ RoomNameEvent* e = new RoomNameEvent();
+ e->parseJson(obj);
+ const QJsonObject contents = obj.value("content").toObject();
+ e->d->name = contents.value("name").toString();
+ return e;
+}
diff --git a/events/roomnameevent.h b/events/roomnameevent.h new file mode 100644 index 00000000..436a1dd4 --- /dev/null +++ b/events/roomnameevent.h @@ -0,0 +1,44 @@ +/******************************************************************************
+ * Copyright (C) 2015 Kitsune Ral <kitsune-ral@users.sf.net>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef QMATRIXCLIENT_ROOMNAMEEVENT_H
+#define QMATRIXCLIENT_ROOMNAMEEVENT_H
+
+#include "event.h"
+
+namespace QMatrixClient
+{
+
+class RoomNameEvent : public Event
+{
+public:
+ RoomNameEvent();
+ virtual ~RoomNameEvent();
+
+ QString name() const;
+
+ static RoomNameEvent* fromJson(const QJsonObject& obj);
+
+private:
+ class Private;
+ Private *d;
+};
+
+}
+
+#endif // QMATRIXCLIENT_ROOMNAMEEVENT_H
diff --git a/events/roomtopicevent.cpp b/events/roomtopicevent.cpp new file mode 100644 index 00000000..5739d85e --- /dev/null +++ b/events/roomtopicevent.cpp @@ -0,0 +1,51 @@ +/****************************************************************************** + * Copyright (C) 2015 Felix Rohrbach <kde@fxrh.de> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "roomtopicevent.h" + +using namespace QMatrixClient; + +class RoomTopicEvent::Private +{ + public: + QString topic; +}; + +RoomTopicEvent::RoomTopicEvent() + : Event(EventType::RoomTopic) + , d(new Private) +{ +} + +RoomTopicEvent::~RoomTopicEvent() +{ + delete d; +} + +QString RoomTopicEvent::topic() const +{ + return d->topic; +} + +RoomTopicEvent* RoomTopicEvent::fromJson(const QJsonObject& obj) +{ + RoomTopicEvent* e = new RoomTopicEvent(); + e->parseJson(obj); + e->d->topic = obj.value("content").toObject().value("topic").toString(); + return e; +} diff --git a/events/roomtopicevent.h b/events/roomtopicevent.h new file mode 100644 index 00000000..9c27339c --- /dev/null +++ b/events/roomtopicevent.h @@ -0,0 +1,44 @@ +/****************************************************************************** + * Copyright (C) 2015 Felix Rohrbach <kde@fxrh.de> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef QMATRIXCLIENT_ROOMTOPICEVENT_H +#define QMATRIXCLIENT_ROOMTOPICEVENT_H + +#include <QtCore/QJsonObject> + +#include "event.h" + +namespace QMatrixClient +{ + class RoomTopicEvent: public Event + { + public: + RoomTopicEvent(); + virtual ~RoomTopicEvent(); + + QString topic() const; + + static RoomTopicEvent* fromJson(const QJsonObject& obj); + + private: + class Private; + Private* d; + }; +} + +#endif // QMATRIXCLIENT_ROOMTOPICEVENT_H
\ No newline at end of file diff --git a/events/typingevent.cpp b/events/typingevent.cpp new file mode 100644 index 00000000..242094e1 --- /dev/null +++ b/events/typingevent.cpp @@ -0,0 +1,57 @@ +/****************************************************************************** + * Copyright (C) 2015 Felix Rohrbach <kde@fxrh.de> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "typingevent.h" + +#include <QtCore/QJsonArray> + +using namespace QMatrixClient; + +class TypingEvent::Private +{ + public: + QStringList users; +}; + +TypingEvent::TypingEvent() + : Event(EventType::Typing) + , d( new Private ) +{ +} + +TypingEvent::~TypingEvent() +{ + delete d; +} + +QStringList TypingEvent::users() +{ + return d->users; +} + +TypingEvent* TypingEvent::fromJson(const QJsonObject& obj) +{ + TypingEvent* e = new TypingEvent(); + e->parseJson(obj); + QJsonArray array = obj.value("content").toObject().value("user_ids").toArray(); + for( const QJsonValue& user: array ) + { + e->d->users << user.toString(); + } + return e; +} diff --git a/events/typingevent.h b/events/typingevent.h new file mode 100644 index 00000000..2625dc66 --- /dev/null +++ b/events/typingevent.h @@ -0,0 +1,44 @@ +/****************************************************************************** + * Copyright (C) 2015 Felix Rohrbach <kde@fxrh.de> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef QMATRIXCLIENT_TYPINGEVENT_H +#define QMATRIXCLIENT_TYPINGEVENT_H + +#include <QtCore/QStringList> + +#include "event.h" + +namespace QMatrixClient +{ + class TypingEvent: public Event + { + public: + TypingEvent(); + virtual ~TypingEvent(); + + QStringList users(); + + static TypingEvent* fromJson(const QJsonObject& obj); + + private: + class Private; + Private* d; + }; +} + +#endif // QMATRIXCLIENT_TYPINGEVENT_H diff --git a/events/unknownevent.cpp b/events/unknownevent.cpp new file mode 100644 index 00000000..758d0ec8 --- /dev/null +++ b/events/unknownevent.cpp @@ -0,0 +1,60 @@ +/****************************************************************************** + * Copyright (C) 2015 Felix Rohrbach <kde@fxrh.de> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "unknownevent.h" + +#include <QtCore/QJsonDocument> + +using namespace QMatrixClient; + +class UnknownEvent::Private +{ + public: + QString type; + QString content; +}; + +UnknownEvent::UnknownEvent() + : Event(EventType::Unknown) + , d(new Private) +{ +} + +UnknownEvent::~UnknownEvent() +{ + delete d; +} + +QString UnknownEvent::typeString() const +{ + return d->type; +} + +QString UnknownEvent::content() const +{ + return d->content; +} + +UnknownEvent* UnknownEvent::fromJson(const QJsonObject& obj) +{ + UnknownEvent* e = new UnknownEvent(); + e->parseJson(obj); + e->d->type = obj.value("type").toString(); + e->d->content = QString::fromUtf8(QJsonDocument(obj).toJson()); + return e; +}
\ No newline at end of file diff --git a/events/unknownevent.h b/events/unknownevent.h new file mode 100644 index 00000000..58493095 --- /dev/null +++ b/events/unknownevent.h @@ -0,0 +1,43 @@ +/****************************************************************************** + * Copyright (C) 2015 Felix Rohrbach <kde@fxrh.de> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef QMATRIXCLIENT_UNKNOWNEVENT_H +#define QMATRIXCLIENT_UNKNOWNEVENT_H + +#include "event.h" + +namespace QMatrixClient +{ + class UnknownEvent: public Event + { + public: + UnknownEvent(); + virtual ~UnknownEvent(); + + QString typeString() const; + QString content() const; + + static UnknownEvent* fromJson(const QJsonObject& obj); + + private: + class Private; + Private* d; + }; +} + +#endif // QMATRIXCLIENT_UNKNOWNEVENT_H
\ No newline at end of file diff --git a/jobs/basejob.cpp b/jobs/basejob.cpp new file mode 100644 index 00000000..b7e1b718 --- /dev/null +++ b/jobs/basejob.cpp @@ -0,0 +1,146 @@ +/****************************************************************************** + * Copyright (C) 2015 Felix Rohrbach <kde@fxrh.de> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "basejob.h" + +#include <QtNetwork/QNetworkAccessManager> +#include <QtNetwork/QNetworkReply> +#include <QtNetwork/QNetworkRequest> +#include <QtCore/QTimer> + +#include "../connectiondata.h" + +using namespace QMatrixClient; + +class BaseJob::Private +{ + public: + Private(ConnectionData* c, JobHttpType t, bool nt) + : connection(c), reply(0), type(t), needsToken(nt) {} + + ConnectionData* connection; + QNetworkReply* reply; + JobHttpType type; + bool needsToken; +}; + +BaseJob::BaseJob(ConnectionData* connection, JobHttpType type, bool needsToken) + : d(new Private(connection, type, needsToken)) +{ +} + +BaseJob::~BaseJob() +{ + if( d->reply ) + d->reply->deleteLater(); + delete d; +} + +ConnectionData* BaseJob::connection() const +{ + return d->connection; +} + +QJsonObject BaseJob::data() +{ + return QJsonObject(); +} + +QUrlQuery BaseJob::query() +{ + return QUrlQuery(); +} + +void BaseJob::parseJson(const QJsonDocument& data) +{ +} + +void BaseJob::start() +{ + QUrl url = d->connection->baseUrl(); + url.setPath( url.path() + "/" + apiPath() ); + QUrlQuery query = this->query(); + if( d->needsToken ) + query.addQueryItem("access_token", connection()->token()); + url.setQuery(query); + QNetworkRequest req = QNetworkRequest(url); + req.setHeader(QNetworkRequest::ContentTypeHeader, "application/json"); +#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)) + req.setAttribute(QNetworkRequest::FollowRedirectsAttribute, true);\ + req.setMaximumRedirectsAllowed(10); +#endif + QJsonDocument data = QJsonDocument(this->data()); + switch( d->type ) + { + case JobHttpType::GetJob: + d->reply = d->connection->nam()->get(req); + break; + case JobHttpType::PostJob: + d->reply = d->connection->nam()->post(req, data.toJson()); + break; + case JobHttpType::PutJob: + d->reply = d->connection->nam()->put(req, data.toJson()); + break; + } + connect( d->reply, &QNetworkReply::finished, this, &BaseJob::gotReply ); + QTimer::singleShot( 120*1000, this, SLOT(timeout()) ); +// connect( d->reply, static_cast<void(QNetworkReply::*)(QNetworkReply::NetworkError)>(&QNetworkReply::error), +// this, &BaseJob::networkError ); // http://doc.qt.io/qt-5/qnetworkreply.html#error-1 +} + +void BaseJob::fail(int errorCode, QString errorString) +{ + setError( errorCode ); + setErrorText( errorString ); + emitResult(); +} + +QNetworkReply* BaseJob::networkReply() const +{ + return d->reply; +} + +// void BaseJob::networkError(QNetworkReply::NetworkError code) +// { +// fail( KJob::UserDefinedError+1, d->reply->errorString() ); +// } + +void BaseJob::gotReply() +{ + if( d->reply->error() != QNetworkReply::NoError ) + { + qDebug() << "NetworkError!!!"; + fail( NetworkError, d->reply->errorString() ); + return; + } + QJsonParseError error; + QJsonDocument data = QJsonDocument::fromJson(d->reply->readAll(), &error); + if( error.error != QJsonParseError::NoError ) + { + fail( JsonParseError, error.errorString() ); + return; + } + parseJson(data); +} + +void BaseJob::timeout() +{ + qDebug() << "Timeout!"; + if( d->reply->isRunning() ) + d->reply->abort(); +} diff --git a/jobs/basejob.h b/jobs/basejob.h new file mode 100644 index 00000000..88911ca1 --- /dev/null +++ b/jobs/basejob.h @@ -0,0 +1,76 @@ +/****************************************************************************** + * Copyright (C) 2015 Felix Rohrbach <kde@fxrh.de> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef QMATRIXCLIENT_BASEJOB_H +#define QMATRIXCLIENT_BASEJOB_H + +#ifdef USING_SYSTEM_KCOREADDONS +#include <KCoreAddons/KJob> +#else +#include "kjob.h" +#endif // KCOREADDONS_FOUND + +#include <QtCore/QJsonDocument> +#include <QtCore/QJsonObject> +#include <QtCore/QUrlQuery> +#include <QtNetwork/QNetworkReply> + +namespace QMatrixClient +{ + class ConnectionData; + + enum class JobHttpType { GetJob, PutJob, PostJob }; + + class BaseJob: public KJob + { + Q_OBJECT + public: + BaseJob(ConnectionData* connection, JobHttpType type, bool needsToken=true); + virtual ~BaseJob(); + + void start() override; + + enum ErrorCode { NetworkError = KJob::UserDefinedError, JsonParseError, UserDefinedError }; + + protected: + ConnectionData* connection() const; + + // to implement + virtual QString apiPath()=0; + virtual QUrlQuery query(); + virtual QJsonObject data(); + virtual void parseJson(const QJsonDocument& data); + + void fail( int errorCode, QString errorString ); + QNetworkReply* networkReply() const; + + + protected slots: + virtual void gotReply(); + void timeout(); + + //void networkError(QNetworkReply::NetworkError code); + + + private: + class Private; + Private* d; + }; +} + +#endif // QMATRIXCLIENT_BASEJOB_H diff --git a/jobs/checkauthmethods.cpp b/jobs/checkauthmethods.cpp new file mode 100644 index 00000000..18b5f9a5 --- /dev/null +++ b/jobs/checkauthmethods.cpp @@ -0,0 +1,63 @@ +/****************************************************************************** + * Copyright (C) 2015 Felix Rohrbach <kde@fxrh.de> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "checkauthmethods.h" + +#include <QtNetwork/QNetworkAccessManager> +#include <QtNetwork/QNetworkReply> +#include <QtCore/QJsonDocument> +#include <QtCore/QJsonObject> +#include <QtCore/QJsonParseError> + +#include "../connectiondata.h" + +using namespace QMatrixClient; + +class CheckAuthMethods::Private +{ + public: + Private() {} + + QString session; +}; + +CheckAuthMethods::CheckAuthMethods(ConnectionData* connection) + : BaseJob(connection, JobHttpType::GetJob, false) + , d(new Private) +{ +} + +CheckAuthMethods::~CheckAuthMethods() +{ + delete d; +} + +QString CheckAuthMethods::session() +{ + return d->session; +} + +QString CheckAuthMethods::apiPath() +{ + return "_matrix/client/r0/login"; +} + +void CheckAuthMethods::parseJson(const QJsonDocument& data) +{ + // TODO +}
\ No newline at end of file diff --git a/jobs/checkauthmethods.h b/jobs/checkauthmethods.h new file mode 100644 index 00000000..2c9ab61a --- /dev/null +++ b/jobs/checkauthmethods.h @@ -0,0 +1,47 @@ +/****************************************************************************** + * Copyright (C) 2015 Felix Rohrbach <kde@fxrh.de> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef QMATRIXCLIENT_CHECKAUTHMETHODS_H +#define QMATRIXCLIENT_CHECKAUTHMETHODS_H + +#include "basejob.h" + +namespace QMatrixClient +{ + class ConnectionData; + + class CheckAuthMethods : public BaseJob + { + Q_OBJECT + public: + CheckAuthMethods(ConnectionData* connection); + virtual ~CheckAuthMethods(); + + QString session(); + + protected: + QString apiPath(); + void parseJson(const QJsonDocument& data); + + private: + class Private; + Private* d; + }; +} + +#endif // QMATRIXCLIENT_CHECKAUTHMETHODS_H
\ No newline at end of file diff --git a/jobs/geteventsjob.cpp b/jobs/geteventsjob.cpp new file mode 100644 index 00000000..748a0189 --- /dev/null +++ b/jobs/geteventsjob.cpp @@ -0,0 +1,96 @@ +/****************************************************************************** + * Copyright (C) 2015 Felix Rohrbach <kde@fxrh.de> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "geteventsjob.h" + +#include <QtCore/QJsonDocument> +#include <QtCore/QJsonObject> +#include <QtCore/QJsonValue> +#include <QtCore/QJsonArray> +#include <QtCore/QDebug> + +#include <QtNetwork/QNetworkReply> + +#include "../room.h" +#include "../connectiondata.h" +#include "../events/event.h" + +using namespace QMatrixClient; + +class GetEventsJob::Private +{ + public: + Private() {} + + QList<Event*> events; + QString from; +}; + +GetEventsJob::GetEventsJob(ConnectionData* connection, QString from) + : BaseJob(connection, JobHttpType::GetJob) + , d(new Private) +{ + if( from.isEmpty() ) + from = connection->lastEvent(); + d->from = from; +} + +GetEventsJob::~GetEventsJob() +{ + delete d; +} + +QList< Event* > GetEventsJob::events() +{ + return d->events; +} + +QString GetEventsJob::apiPath() +{ + return "_matrix/client/r0/events"; +} + +QUrlQuery GetEventsJob::query() +{ + QUrlQuery query; + query.addQueryItem("from", d->from); + return query; +} + +void GetEventsJob::parseJson(const QJsonDocument& data) +{ + QJsonObject json = data.object(); + if( !json.contains("chunk") || !json.value("chunk").isArray() ) + { + fail( BaseJob::UserDefinedError, "Couldn't find chunk" ); + return; + } + QJsonArray chunk = json.value("chunk").toArray(); +// qDebug() << chunk; + for( const QJsonValue& val: chunk ) + { + QJsonObject eventObj = val.toObject(); + Event* event = Event::fromJson(eventObj); + if( event ) + { + d->events.append(event); + } + } + connection()->setLastEvent( json.value("end").toString() ); + emitResult(); +}
\ No newline at end of file diff --git a/jobs/geteventsjob.h b/jobs/geteventsjob.h new file mode 100644 index 00000000..d2eb75eb --- /dev/null +++ b/jobs/geteventsjob.h @@ -0,0 +1,49 @@ +/****************************************************************************** + * Copyright (C) 2015 Felix Rohrbach <kde@fxrh.de> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef QMATRIXCLIENT_GETEVENTSJOB_H +#define QMATRIXCLIENT_GETEVENTSJOB_H + +#include "basejob.h" + +namespace QMatrixClient +{ + class ConnectionData; + class Room; + class Event; + class GetEventsJob: public BaseJob + { + Q_OBJECT + public: + GetEventsJob(ConnectionData* connection, QString from=QString()); + virtual ~GetEventsJob(); + + QList<Event*> events(); + + protected: + QString apiPath(); + QUrlQuery query(); + void parseJson(const QJsonDocument& data); + + private: + class Private; + Private* d; + }; +} + +#endif // QMATRIXCLIENT_GETEVENTSJOB_H
\ No newline at end of file diff --git a/jobs/initialsyncjob.cpp b/jobs/initialsyncjob.cpp new file mode 100644 index 00000000..9512e0ea --- /dev/null +++ b/jobs/initialsyncjob.cpp @@ -0,0 +1,117 @@ +/****************************************************************************** + * Copyright (C) 2015 Felix Rohrbach <kde@fxrh.de> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "initialsyncjob.h" + +#include <QtNetwork/QNetworkAccessManager> +#include <QtNetwork/QNetworkReply> +#include <QtCore/QUrlQuery> +#include <QtCore/QJsonDocument> +#include <QtCore/QJsonObject> +#include <QtCore/QJsonArray> +#include <QtCore/QJsonValue> + +#include "../connectiondata.h" +#include "../room.h" +#include "../state.h" +#include "../events/event.h" + +using namespace QMatrixClient; + +class InitialSyncJob::Private +{ + public: + Private() {} + + QList<Event*> events; + QList<State*> initialState; +}; + +InitialSyncJob::InitialSyncJob(ConnectionData* connection) + : BaseJob(connection, JobHttpType::GetJob) + , d(new Private) +{ +} + +InitialSyncJob::~InitialSyncJob() +{ + delete d; +} + +QList< Event* > InitialSyncJob::events() +{ + return d->events; +} + +QList< State* > InitialSyncJob::initialState() +{ + return d->initialState; +} + +QString InitialSyncJob::apiPath() +{ + return "_matrix/client/r0/initialSync"; +} + +QUrlQuery InitialSyncJob::query() +{ + QUrlQuery query; + query.addQueryItem("limit", "200"); + return query; +} + +void InitialSyncJob::parseJson(const QJsonDocument& data) +{ + QJsonObject json = data.object(); + if( !json.contains("rooms") || !json.value("rooms").isArray() ) + { + fail( KJob::UserDefinedError+2, "Didn't find rooms" ); + qDebug() << json; + return; + } + QJsonArray array = json.value("rooms").toArray(); + for( const QJsonValue& val : array ) + { + if( !val.isObject() ) + { + qWarning() << "Strange: " << val; + continue; + } + QJsonObject obj = val.toObject(); + QJsonObject messages = obj.value("messages").toObject(); + QJsonArray chunk = messages.value("chunk").toArray(); + for( const QJsonValue& val: chunk ) + { + Event* event = Event::fromJson(val.toObject()); + if( event ) + d->events.append( event ); + } + QJsonArray state = obj.value("state").toArray(); + for( const QJsonValue& val: state ) + { +// qDebug() << val.toObject(); + State* state = State::fromJson(val.toObject()); + if( state ) + d->initialState.append( state ); + } + } + connection()->setLastEvent( json.value("end").toString() ); + qDebug() << connection()->lastEvent(); + emitResult(); +} + diff --git a/jobs/initialsyncjob.h b/jobs/initialsyncjob.h new file mode 100644 index 00000000..188522c1 --- /dev/null +++ b/jobs/initialsyncjob.h @@ -0,0 +1,53 @@ +/****************************************************************************** + * Copyright (C) 2015 Felix Rohrbach <kde@fxrh.de> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef QMATRIXCLIENT_INITIALSYNCJOB_H +#define QMATRIXCLIENT_INITIALSYNCJOB_H + +#include "basejob.h" + +#include "lib/room.h" + +namespace QMatrixClient +{ + class ConnectionData; + class Event; + class State; + + class InitialSyncJob: public BaseJob + { + Q_OBJECT + public: + InitialSyncJob(ConnectionData* connection); + virtual ~InitialSyncJob(); + + QList<Event*> events(); + QList<State*> initialState(); + + protected: + QString apiPath(); + QUrlQuery query(); + void parseJson(const QJsonDocument& data); + + private: + class Private; + Private* d; + }; +} + +#endif // QMATRIXCLIENT_INITIALSYNCJOB_H
\ No newline at end of file diff --git a/jobs/joinroomjob.cpp b/jobs/joinroomjob.cpp new file mode 100644 index 00000000..85a331fa --- /dev/null +++ b/jobs/joinroomjob.cpp @@ -0,0 +1,71 @@ +/****************************************************************************** + * Copyright (C) 2015 Felix Rohrbach <kde@fxrh.de> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "joinroomjob.h" + +#include <QtCore/QJsonObject> +#include <QtNetwork/QNetworkReply> + +#include "../connectiondata.h" + +using namespace QMatrixClient; + +class JoinRoomJob::Private +{ + public: + QString roomId; + QString roomAlias; +}; + +JoinRoomJob::JoinRoomJob(ConnectionData* data, QString roomAlias) + : BaseJob(data, JobHttpType::PostJob) + , d(new Private) +{ + d->roomAlias = roomAlias; +} + +JoinRoomJob::~JoinRoomJob() +{ + delete d; +} + +QString JoinRoomJob::roomId() +{ + return d->roomId; +} + +QString JoinRoomJob::apiPath() +{ + return QString("_matrix/client/r0/join/%1").arg(d->roomAlias); +} + +void JoinRoomJob::parseJson(const QJsonDocument& data) +{ + QJsonObject json = data.object(); + if( !json.contains("room_id") ) + { + fail( BaseJob::UserDefinedError, "Something went wrong..." ); + qDebug() << data; + return; + } + else + { + d->roomId = json.value("room_id").toString(); + } + emitResult(); +} diff --git a/jobs/joinroomjob.h b/jobs/joinroomjob.h new file mode 100644 index 00000000..8f4059f2 --- /dev/null +++ b/jobs/joinroomjob.h @@ -0,0 +1,46 @@ +/****************************************************************************** + * Copyright (C) 2015 Felix Rohrbach <kde@fxrh.de> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef QMATRIXCLIENT_JOINROOMJOB_H +#define QMATRIXCLIENT_JOINROOMJOB_H + +#include "basejob.h" + +namespace QMatrixClient +{ + class ConnectionData; + + class JoinRoomJob: public BaseJob + { + public: + JoinRoomJob(ConnectionData* data, QString roomAlias); + virtual ~JoinRoomJob(); + + QString roomId(); + + protected: + QString apiPath(); + void parseJson(const QJsonDocument& data); + + private: + class Private; + Private* d; + }; +} + +#endif // QMATRIXCLIENT_JOINROOMJOB_H
\ No newline at end of file diff --git a/jobs/leaveroomjob.cpp b/jobs/leaveroomjob.cpp new file mode 100644 index 00000000..1adab31f --- /dev/null +++ b/jobs/leaveroomjob.cpp @@ -0,0 +1,50 @@ +/****************************************************************************** + * Copyright (C) 2015 Felix Rohrbach <kde@fxrh.de> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "leaveroomjob.h" + +#include <QtNetwork/QNetworkReply> + +#include "../room.h" +#include "../connectiondata.h" + +using namespace QMatrixClient; + +class LeaveRoomJob::Private +{ + public: + Private(Room* r) : room(r) {} + + Room* room; +}; + +LeaveRoomJob::LeaveRoomJob(ConnectionData* data, Room* room) + : BaseJob(data, JobHttpType::PostJob) + , d(new Private(room)) +{ +} + +LeaveRoomJob::~LeaveRoomJob() +{ + delete d; +} + +QString LeaveRoomJob::apiPath() +{ + return QString("_matrix/client/r0/rooms/%1/leave").arg(d->room->id()); +} diff --git a/jobs/leaveroomjob.h b/jobs/leaveroomjob.h new file mode 100644 index 00000000..535c44b8 --- /dev/null +++ b/jobs/leaveroomjob.h @@ -0,0 +1,44 @@ +/****************************************************************************** + * Copyright (C) 2015 Felix Rohrbach <kde@fxrh.de> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef QMATRIXCLIENT_LEAVEROOMJOB_H +#define QMATRIXCLIENT_LEAVEROOMJOB_H + +#include "basejob.h" + +namespace QMatrixClient +{ + class ConnectionData; + class Room; + + class LeaveRoomJob: public BaseJob + { + public: + LeaveRoomJob(ConnectionData* data, Room* room); + virtual ~LeaveRoomJob(); + + protected: + QString apiPath(); + + private: + class Private; + Private* d; + }; +} + +#endif // QMATRIXCLIENT_LEAVEROOMJOB_H
\ No newline at end of file diff --git a/jobs/mediathumbnailjob.cpp b/jobs/mediathumbnailjob.cpp new file mode 100644 index 00000000..02d9bbd9 --- /dev/null +++ b/jobs/mediathumbnailjob.cpp @@ -0,0 +1,88 @@ +/****************************************************************************** + * Copyright (C) 2016 Felix Rohrbach <kde@fxrh.de> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "mediathumbnailjob.h" + +#include <QtCore/QDebug> + +using namespace QMatrixClient; + +class MediaThumbnailJob::Private +{ + public: + QUrl url; + QPixmap thumbnail; + int requestedHeight; + int requestedWidth; + ThumbnailType thumbnailType; +}; + +MediaThumbnailJob::MediaThumbnailJob(ConnectionData* data, QUrl url, int requestedWidth, int requestedHeight, + ThumbnailType thumbnailType) + : BaseJob(data, JobHttpType::GetJob) + , d(new Private) +{ + d->url = url; + d->requestedHeight = requestedHeight; + d->requestedWidth = requestedWidth; + d->thumbnailType = thumbnailType; +} + +MediaThumbnailJob::~MediaThumbnailJob() +{ + delete d; +} + +QPixmap MediaThumbnailJob::thumbnail() +{ + return d->thumbnail; +} + +QString MediaThumbnailJob::apiPath() +{ + return QString("/_matrix/media/v1/thumbnail/%1/%2").arg(d->url.host()).arg(d->url.path()); +} + +QUrlQuery MediaThumbnailJob::query() +{ + QUrlQuery query; + query.addQueryItem("width", QString::number(d->requestedWidth)); + query.addQueryItem("height", QString::number(d->requestedHeight)); + if( d->thumbnailType == ThumbnailType::Scale ) + query.addQueryItem("method", "scale"); + else + query.addQueryItem("method", "crop"); + return query; +} + +void MediaThumbnailJob::gotReply() +{ + if( networkReply()->error() != QNetworkReply::NoError ) + { + qDebug() << "NetworkError!!!"; + qDebug() << networkReply()->errorString(); + fail( NetworkError, networkReply()->errorString() ); + return; + } + + if( !d->thumbnail.loadFromData( networkReply()->readAll() ) ) + { + qDebug() << "MediaThumbnailJob: could not read image data"; + } + emitResult(); +} diff --git a/jobs/mediathumbnailjob.h b/jobs/mediathumbnailjob.h new file mode 100644 index 00000000..85dc8251 --- /dev/null +++ b/jobs/mediathumbnailjob.h @@ -0,0 +1,53 @@ +/****************************************************************************** + * Copyright (C) 2016 Felix Rohrbach <kde@fxrh.de> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef QMATRIXCLIENT_MEDIATHUMBNAILJOB_H +#define QMATRIXCLIENT_MEDIATHUMBNAILJOB_H + +#include "basejob.h" + +#include <QtGui/QPixmap> + +namespace QMatrixClient +{ + enum class ThumbnailType {Crop, Scale}; + + class MediaThumbnailJob: public BaseJob + { + Q_OBJECT + public: + MediaThumbnailJob(ConnectionData* data, QUrl url, int requestedWidth, int requestedHeight, + ThumbnailType thumbnailType=ThumbnailType::Scale); + virtual ~MediaThumbnailJob(); + + QPixmap thumbnail(); + + protected: + QString apiPath() override; + QUrlQuery query() override; + + protected slots: + void gotReply() override; + + private: + class Private; + Private* d; + }; +} + +#endif // QMATRIXCLIENT_MEDIATHUMBNAILJOB_H
\ No newline at end of file diff --git a/jobs/passwordlogin.cpp b/jobs/passwordlogin.cpp new file mode 100644 index 00000000..d989815f --- /dev/null +++ b/jobs/passwordlogin.cpp @@ -0,0 +1,96 @@ +/****************************************************************************** + * Copyright (C) 2015 Felix Rohrbach <kde@fxrh.de> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "passwordlogin.h" + +#include <QtCore/QJsonDocument> +#include <QtCore/QJsonObject> +#include <QtNetwork/QNetworkReply> + +#include "../connectiondata.h" + +using namespace QMatrixClient; + +class PasswordLogin::Private +{ + public: + Private() {} + + QString user; + QString password; + QString returned_id; + QString returned_server; + QString returned_token; +}; + +PasswordLogin::PasswordLogin(ConnectionData* connection, QString user, QString password) + : BaseJob(connection, JobHttpType::PostJob, false) + , d(new Private) +{ + d->user = user; + d->password = password; +} + +PasswordLogin::~PasswordLogin() +{ + delete d; +} + +QString PasswordLogin::token() +{ + return d->returned_token; +} + +QString PasswordLogin::id() +{ + return d->returned_id; +} + +QString PasswordLogin::server() +{ + return d->returned_server; +} + +QString PasswordLogin::apiPath() +{ + return "_matrix/client/r0/login"; +} + +QJsonObject PasswordLogin::data() +{ + QJsonObject json; + json.insert("type", "m.login.password"); + json.insert("user", d->user); + json.insert("password", d->password); + return json; +} + +void PasswordLogin::parseJson(const QJsonDocument& data) +{ + QJsonObject json = data.object(); + if( !json.contains("access_token") || !json.contains("home_server") || !json.contains("user_id") ) + { + fail( BaseJob::UserDefinedError, "Unexpected data" ); + } + d->returned_token = json.value("access_token").toString(); + qDebug() << d->returned_token; + d->returned_server = json.value("home_server").toString(); + d->returned_id = json.value("user_id").toString(); + connection()->setToken(d->returned_token); + emitResult(); +} diff --git a/jobs/passwordlogin.h b/jobs/passwordlogin.h new file mode 100644 index 00000000..19df118e --- /dev/null +++ b/jobs/passwordlogin.h @@ -0,0 +1,50 @@ +/****************************************************************************** + * Copyright (C) 2015 Felix Rohrbach <kde@fxrh.de> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef QMATRIXCLIENT_PASSWORDLOGIN_H +#define QMATRIXCLIENT_PASSWORDLOGIN_H + +#include "basejob.h" + +namespace QMatrixClient +{ + class ConnectionData; + + class PasswordLogin : public BaseJob + { + Q_OBJECT + public: + PasswordLogin(ConnectionData* connection, QString user, QString password); + virtual ~PasswordLogin(); + + QString token(); + QString id(); + QString server(); + + protected: + QString apiPath(); + QJsonObject data(); + void parseJson(const QJsonDocument& data); + + private: + class Private; + Private* d; + }; +} + +#endif // QMATRIXCLIENT_PASSWORDLOGIN_H
\ No newline at end of file diff --git a/jobs/postmessagejob.cpp b/jobs/postmessagejob.cpp new file mode 100644 index 00000000..67c79669 --- /dev/null +++ b/jobs/postmessagejob.cpp @@ -0,0 +1,74 @@ +/****************************************************************************** + * Copyright (C) 2015 Felix Rohrbach <kde@fxrh.de> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "postmessagejob.h" +#include "../room.h" +#include "../connectiondata.h" + +#include <QtNetwork/QNetworkReply> + +using namespace QMatrixClient; + +class PostMessageJob::Private +{ + public: + Private() {} + + QString type; + QString message; + Room* room; +}; + +PostMessageJob::PostMessageJob(ConnectionData* connection, Room* room, QString type, QString message) + : BaseJob(connection, JobHttpType::PostJob) + , d(new Private) +{ + d->type = type; + d->message = message; + d->room = room; +} + +PostMessageJob::~PostMessageJob() +{ + delete d; +} + +QString PostMessageJob::apiPath() +{ + return QString("_matrix/client/r0/rooms/%1/send/m.room.message").arg(d->room->id()); +} + +QJsonObject PostMessageJob::data() +{ + QJsonObject json; + json.insert("msgtype", d->type); + json.insert("body", d->message); + return json; +} + +void PostMessageJob::parseJson(const QJsonDocument& data) +{ + QJsonObject json = data.object(); + if( !json.contains("event_id") ) + { + fail( BaseJob::UserDefinedError, "Something went wrong..." ); + qDebug() << data; + return; + } + emitResult(); +}
\ No newline at end of file diff --git a/jobs/postmessagejob.h b/jobs/postmessagejob.h new file mode 100644 index 00000000..7f40534e --- /dev/null +++ b/jobs/postmessagejob.h @@ -0,0 +1,47 @@ +/****************************************************************************** + * Copyright (C) 2015 Felix Rohrbach <kde@fxrh.de> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef QMATRIXCLIENT_POSTMESSAGEJOB_H +#define QMATRIXCLIENT_POSTMESSAGEJOB_H + +#include "basejob.h" + +namespace QMatrixClient +{ + class Room; + class PostMessageJob: public BaseJob + { + Q_OBJECT + public: + PostMessageJob(ConnectionData* connection, Room* room, QString type, QString message); + virtual ~PostMessageJob(); + + //bool success(); + + protected: + QString apiPath(); + QJsonObject data(); + void parseJson(const QJsonDocument& data); + + private: + class Private; + Private* d; + }; +} + +#endif // QMATRIXCLIENT_POSTMESSAGEJOB_H
\ No newline at end of file diff --git a/jobs/postreceiptjob.cpp b/jobs/postreceiptjob.cpp new file mode 100644 index 00000000..c26186e8 --- /dev/null +++ b/jobs/postreceiptjob.cpp @@ -0,0 +1,52 @@ +/****************************************************************************** + * Copyright (C) 2016 Felix Rohrbach <kde@fxrh.de> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "postreceiptjob.h" +#include "../room.h" +#include "../connectiondata.h" + +#include <QtNetwork/QNetworkReply> + +using namespace QMatrixClient; + +class PostReceiptJob::Private +{ + public: + Private() {} + + QString roomId; + QString eventId; +}; + +PostReceiptJob::PostReceiptJob(ConnectionData* connection, QString roomId, QString eventId) + : BaseJob(connection, JobHttpType::PostJob) + , d(new Private) +{ + d->roomId = roomId; + d->eventId = eventId; +} + +PostReceiptJob::~PostReceiptJob() +{ + delete d; +} + +QString PostReceiptJob::apiPath() +{ + return QString("/_matrix/client/r0/rooms/%1/receipt/m.read/%2").arg(d->roomId).arg(d->eventId); +} diff --git a/jobs/postreceiptjob.h b/jobs/postreceiptjob.h new file mode 100644 index 00000000..2767fa9a --- /dev/null +++ b/jobs/postreceiptjob.h @@ -0,0 +1,43 @@ +/****************************************************************************** + * Copyright (C) 2016 Felix Rohrbach <kde@fxrh.de> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef QMATRIXCLIENT_POSTRECEIPTJOB_H +#define QMATRIXCLIENT_POSTRECEIPTJOB_H + +#include "basejob.h" + +namespace QMatrixClient +{ + class Room; + class PostReceiptJob: public BaseJob + { + Q_OBJECT + public: + PostReceiptJob(ConnectionData* connection, QString roomId, QString eventId); + virtual ~PostReceiptJob(); + + protected: + QString apiPath(); + + private: + class Private; + Private* d; + }; +} + +#endif // QMATRIXCLIENT_POSTRECEIPTJOB_H diff --git a/jobs/roommembersjob.cpp b/jobs/roommembersjob.cpp new file mode 100644 index 00000000..e12bc6c4 --- /dev/null +++ b/jobs/roommembersjob.cpp @@ -0,0 +1,70 @@ +/****************************************************************************** + * Copyright (C) 2015 Felix Rohrbach <kde@fxrh.de> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "roommembersjob.h" + +#include <QtCore/QJsonObject> +#include <QtCore/QJsonArray> + +#include "../room.h" +#include "../state.h" + +using namespace QMatrixClient; + +class RoomMembersJob::Private +{ + public: + Room* room; + QList<State*> states; +}; + +RoomMembersJob::RoomMembersJob(ConnectionData* data, Room* room) + : BaseJob(data, JobHttpType::GetJob) + , d(new Private) +{ + d->room = room; +} + +RoomMembersJob::~RoomMembersJob() +{ + delete d; +} + +QList< State* > RoomMembersJob::states() +{ + return d->states; +} + +QString RoomMembersJob::apiPath() +{ + return QString("_matrix/client/r0/rooms/%1/members").arg(d->room->id()); +} + +void RoomMembersJob::parseJson(const QJsonDocument& data) +{ + QJsonObject obj = data.object(); + QJsonArray chunk = obj.value("chunk").toArray(); + for( const QJsonValue& val : chunk ) + { + State* state = State::fromJson(val.toObject()); + if( state ) + d->states.append(state); + } + qDebug() << "States: " << d->states.count(); + emitResult(); +}
\ No newline at end of file diff --git a/jobs/roommembersjob.h b/jobs/roommembersjob.h new file mode 100644 index 00000000..0349aa02 --- /dev/null +++ b/jobs/roommembersjob.h @@ -0,0 +1,47 @@ +/****************************************************************************** + * Copyright (C) 2015 Felix Rohrbach <kde@fxrh.de> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef QMATRIXCLIENT_ROOMMEMBERSJOB_H +#define QMATRIXCLIENT_ROOMMEMBERSJOB_H + +#include "basejob.h" + +namespace QMatrixClient +{ + class ConnectionData; + class Room; + class State; + class RoomMembersJob: public BaseJob + { + public: + RoomMembersJob(ConnectionData* data, Room* room); + virtual ~RoomMembersJob(); + + QList<State*> states(); + + protected: + virtual QString apiPath(); + virtual void parseJson(const QJsonDocument& data); + + private: + class Private; + Private* d; + }; +} + +#endif // QMATRIXCLIENT_ROOMMEMBERSJOB_H
\ No newline at end of file diff --git a/jobs/roommessagesjob.cpp b/jobs/roommessagesjob.cpp new file mode 100644 index 00000000..fc3f5f1e --- /dev/null +++ b/jobs/roommessagesjob.cpp @@ -0,0 +1,95 @@ +/****************************************************************************** + * Copyright (C) 2016 Felix Rohrbach <kde@fxrh.de> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "roommessagesjob.h" +#include "../room.h" +#include "../events/event.h" + +#include <QtCore/QJsonObject> +#include <QtCore/QJsonArray> + +using namespace QMatrixClient; + +class RoomMessagesJob::Private +{ + public: + Private() {} + + Room* room; + QString from; + FetchDirectory dir; + int limit; + + QList<Event*> events; + QString end; +}; + +RoomMessagesJob::RoomMessagesJob(ConnectionData* data, Room* room, QString from, FetchDirectory dir, int limit) + : BaseJob(data, JobHttpType::GetJob) +{ + d = new Private(); + d->room = room; + d->from = from; + d->dir = dir; + d->limit = limit; +} + +RoomMessagesJob::~RoomMessagesJob() +{ + delete d; +} + +QList<Event*> RoomMessagesJob::events() +{ + return d->events; +} + +QString RoomMessagesJob::end() +{ + return d->end; +} + +QString RoomMessagesJob::apiPath() +{ + return QString("/_matrix/client/r0/rooms/%1/messages").arg(d->room->id()); +} + +QUrlQuery RoomMessagesJob::query() +{ + QUrlQuery query; + query.addQueryItem("from", d->from); + if( d->dir == FetchDirectory::Backwards ) + query.addQueryItem("dir", "b"); + else + query.addQueryItem("dir", "f"); + query.addQueryItem("limit", QString::number(d->limit)); + return query; +} + +void RoomMessagesJob::parseJson(const QJsonDocument& data) +{ + QJsonObject obj = data.object(); + QJsonArray chunk = obj.value("chunk").toArray(); + for( const QJsonValue& val: chunk ) + { + Event* event = Event::fromJson(val.toObject()); + d->events.append(event); + } + d->end = obj.value("end").toString(); + emitResult(); +}
\ No newline at end of file diff --git a/jobs/roommessagesjob.h b/jobs/roommessagesjob.h new file mode 100644 index 00000000..2a12ac08 --- /dev/null +++ b/jobs/roommessagesjob.h @@ -0,0 +1,52 @@ +/****************************************************************************** + * Copyright (C) 2016 Felix Rohrbach <kde@fxrh.de> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef QMATRIXCLIENT_ROOMMESSAGESJOB_H +#define QMATRIXCLIENT_ROOMMESSAGESJOB_H + +#include "basejob.h" + +namespace QMatrixClient +{ + class Room; + class Event; + + enum class FetchDirectory { Backwards, Forward }; + + class RoomMessagesJob: public BaseJob + { + Q_OBJECT + public: + RoomMessagesJob(ConnectionData* data, Room* room, QString from, FetchDirectory dir = FetchDirectory::Backwards, int limit=10); + virtual ~RoomMessagesJob(); + + QList<Event*> events(); + QString end(); + + protected: + QString apiPath(); + QUrlQuery query(); + void parseJson(const QJsonDocument& data); + + private: + class Private; + Private* d; + }; +} + +#endif // QMATRIXCLIENT_ROOMMESSAGESJOB_H
\ No newline at end of file diff --git a/jobs/syncjob.cpp b/jobs/syncjob.cpp new file mode 100644 index 00000000..cd60e2c1 --- /dev/null +++ b/jobs/syncjob.cpp @@ -0,0 +1,176 @@ +/****************************************************************************** + * Copyright (C) 2016 Felix Rohrbach <kde@fxrh.de> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "syncjob.h" + +#include <QtCore/QJsonDocument> +#include <QtCore/QJsonObject> +#include <QtCore/QJsonValue> +#include <QtCore/QJsonArray> +#include <QtCore/QDebug> + +#include <QtNetwork/QNetworkReply> + +#include "../room.h" +#include "../connectiondata.h" +#include "../events/event.h" + +using namespace QMatrixClient; + +class SyncJob::Private +{ + public: + QString since; + QString filter; + bool fullState; + QString presence; + int timeout; + QString nextBatch; + + QList<SyncRoomData> roomData; + + void parseEvents(QString roomId, const QJsonObject& room, JoinState joinState); +}; + +SyncJob::SyncJob(ConnectionData* connection, QString since) + : BaseJob(connection, JobHttpType::GetJob) + , d(new Private) +{ + d->since = since; + d->fullState = false; + d->timeout = -1; +} + +SyncJob::~SyncJob() +{ + delete d; +} + +void SyncJob::setFilter(QString filter) +{ + d->filter = filter; +} + +void SyncJob::setFullState(bool full) +{ + d->fullState = full; +} + +void SyncJob::setPresence(QString presence) +{ + d->presence = presence; +} + +void SyncJob::setTimeout(int timeout) +{ + d->timeout = timeout; +} + +QString SyncJob::nextBatch() const +{ + return d->nextBatch; +} + +QList<SyncRoomData> SyncJob::roomData() const +{ + return d->roomData; +} + +QString SyncJob::apiPath() +{ + return "_matrix/client/r0/sync"; +} + +QUrlQuery SyncJob::query() +{ + QUrlQuery query; + if( !d->filter.isEmpty() ) + query.addQueryItem("filter", d->filter); + if( d->fullState ) + query.addQueryItem("full_state", "true"); + if( !d->presence.isEmpty() ) + query.addQueryItem("set_presence", d->presence); + if( d->timeout >= 0 ) + query.addQueryItem("timeout", QString::number(d->timeout)); + if( !d->since.isEmpty() ) + query.addQueryItem("since", d->since); + return query; +} + +void SyncJob::parseJson(const QJsonDocument& data) +{ + QJsonObject json = data.object(); + d->nextBatch = json.value("next_batch").toString(); + // TODO: presence + // TODO: account_data + QJsonObject rooms = json.value("rooms").toObject(); + + QJsonObject joinRooms = rooms.value("join").toObject(); + for( const QString& roomId: joinRooms.keys() ) + { + d->parseEvents(roomId, joinRooms.value(roomId).toObject(), JoinState::Join); + } + + QJsonObject inviteRooms = rooms.value("invite").toObject(); + for( const QString& roomId: inviteRooms.keys() ) + { + d->parseEvents(roomId, inviteRooms.value(roomId).toObject(), JoinState::Invite); + } + + QJsonObject leaveRooms = rooms.value("leave").toObject(); + for( const QString& roomId: leaveRooms.keys() ) + { + d->parseEvents(roomId, leaveRooms.value(roomId).toObject(), JoinState::Leave); + } + emitResult(); +} + +SyncRoomData::SyncRoomData(QString roomId_, const QJsonObject& room_, JoinState joinState_) + : roomId(roomId_), joinState(joinState_) +{ + const QList<QPair<QString, QList<Event *> *> > eventLists = { + { "state", &state }, + { "timeline", &timeline }, + { "ephemeral", &ephemeral }, + { "account_data", &accountData } + }; + + for (auto elist: eventLists) { + QJsonArray array = room_.value(elist.first).toObject().value("events").toArray(); + for( QJsonValue val: array ) + { + if ( Event* event = Event::fromJson(val.toObject()) ) + elist.second->append(event); + } + } + + QJsonObject timeline = room_.value("timeline").toObject(); + timelineLimited = timeline.value("limited").toBool(); + timelinePrevBatch = timeline.value("prev_batch").toString(); + + QJsonObject unread = room_.value("unread_notifications").toObject(); + highlightCount = unread.value("highlight_count").toInt(); + notificationCount = unread.value("notification_count").toInt(); + qDebug() << "Highlights: " << highlightCount << " Notifications:" << notificationCount; +} + +void SyncJob::Private::parseEvents(QString roomId, const QJsonObject& room, JoinState joinState) +{ + roomData.append(SyncRoomData{roomId, room, joinState}); +} + diff --git a/jobs/syncjob.h b/jobs/syncjob.h new file mode 100644 index 00000000..507d46eb --- /dev/null +++ b/jobs/syncjob.h @@ -0,0 +1,75 @@ +/****************************************************************************** + * Copyright (C) 2016 Felix Rohrbach <kde@fxrh.de> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef QMATRIXCLIENT_SYNCJOB_H +#define QMATRIXCLIENT_SYNCJOB_H + +#include "basejob.h" + +#include "../joinstate.h" + +namespace QMatrixClient +{ + class Event; + + class SyncRoomData + { + public: + QString roomId; + JoinState joinState; + QList<Event*> state; + QList<Event*> timeline; + QList<Event*> ephemeral; + QList<Event*> accountData; + + bool timelineLimited; + QString timelinePrevBatch; + int highlightCount; + int notificationCount; + + SyncRoomData(QString roomId_, const QJsonObject& room_, JoinState joinState_); + }; + + class ConnectionData; + class SyncJob: public BaseJob + { + Q_OBJECT + public: + SyncJob(ConnectionData* connection, QString since=QString()); + virtual ~SyncJob(); + + void setFilter(QString filter); + void setFullState(bool full); + void setPresence(QString presence); + void setTimeout(int timeout); + + QList<SyncRoomData> roomData() const; + QString nextBatch() const; + + protected: + QString apiPath(); + QUrlQuery query(); + void parseJson(const QJsonDocument& data); + + private: + class Private; + Private* d; + }; +} + +#endif // QMATRIXCLIENT_GETEVENTSJOB_H diff --git a/joinstate.h b/joinstate.h new file mode 100644 index 00000000..e7175b2b --- /dev/null +++ b/joinstate.h @@ -0,0 +1,32 @@ +/****************************************************************************** + * Copyright (C) 2015 Felix Rohrbach <kde@fxrh.de> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef QMATRIXCLIENT_JOINSTATE_H +#define QMATRIXCLIENT_JOINSTATE_H + +namespace QMatrixClient +{ + enum class JoinState + { + Join, + Invite, + Leave + }; +} + +#endif // QMATRIXCLIENT_JOINSTATE_H
\ No newline at end of file diff --git a/logmessage.cpp b/logmessage.cpp new file mode 100644 index 00000000..8b385c25 --- /dev/null +++ b/logmessage.cpp @@ -0,0 +1,60 @@ +/****************************************************************************** + * Copyright (C) 2015 Felix Rohrbach <kde@fxrh.de> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "logmessage.h" + +using namespace QMatrixClient; + +class LogMessage::Private +{ + public: + Private() {} + + MessageType type; + QString message; + QString author; +}; + +LogMessage::LogMessage(LogMessage::MessageType type, QString message, QString author) + : d(new Private) +{ + d->type = type; + d->message = message; + d->author = author; +} + +LogMessage::~LogMessage() +{ + delete d; +} + +LogMessage::MessageType LogMessage::type() const +{ + return d->type; +} + +QString LogMessage::message() const +{ + return d->message; +} + +QString LogMessage::author() const +{ + return d->author; +} + diff --git a/logmessage.h b/logmessage.h new file mode 100644 index 00000000..bb58af65 --- /dev/null +++ b/logmessage.h @@ -0,0 +1,44 @@ +/****************************************************************************** + * Copyright (C) 2015 Felix Rohrbach <kde@fxrh.de> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef QMATRIXCLIENT_LOGMESSAGE_H +#define QMATRIXCLIENT_LOGMESSAGE_H + +#include <QtCore/QString> + +namespace QMatrixClient +{ + class LogMessage + { + public: + enum MessageType{ UserMessage, StatusMessage }; + + LogMessage( MessageType type, QString message, QString author=QString() ); + virtual ~LogMessage(); + + MessageType type() const; + QString message() const; + QString author() const; + + private: + class Private; + Private* d; + }; +} + +#endif // QMATRIXCLIENT_LOGMESSAGE_H
\ No newline at end of file diff --git a/room.cpp b/room.cpp new file mode 100644 index 00000000..178ada27 --- /dev/null +++ b/room.cpp @@ -0,0 +1,458 @@ +/****************************************************************************** + * Copyright (C) 2015 Felix Rohrbach <kde@fxrh.de> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "room.h" + +#include <QtCore/QJsonArray> +#include <QtCore/QDebug> + +#include "connection.h" +#include "state.h" +#include "user.h" +#include "events/event.h" +#include "events/roommessageevent.h" +#include "events/roomnameevent.h" +#include "events/roomaliasesevent.h" +#include "events/roomcanonicalaliasevent.h" +#include "events/roomtopicevent.h" +#include "events/roommemberevent.h" +#include "events/typingevent.h" +#include "events/receiptevent.h" +#include "jobs/roommessagesjob.h" + +using namespace QMatrixClient; + +class Room::Private: public QObject +{ + public: + Private(Room* parent): q(parent) {} + + Room* q; + + //static LogMessage* parseMessage(const QJsonObject& message); + void gotMessages(KJob* job); + + Connection* connection; + QList<Event*> messageEvents; + QString id; + QStringList aliases; + QString canonicalAlias; + QString name; + QString topic; + JoinState joinState; + int highlightCount; + int notificationCount; + QList<User*> users; + QList<User*> usersTyping; + QHash<User*, QString> lastReadEvent; + QString prevBatch; + bool gettingNewContent; +}; + +Room::Room(Connection* connection, QString id) + : d(new Private(this)) +{ + d->id = id; + d->connection = connection; + d->joinState = JoinState::Join; + d->gettingNewContent = false; + qDebug() << "New Room: " << id; + + //connection->getMembers(this); // I don't think we need this anymore in r0.0.1 +} + +Room::~Room() +{ + delete d; +} + +QString Room::id() const +{ + return d->id; +} + +QList< Event* > Room::messages() const +{ + return d->messageEvents; +} + +QString Room::name() const +{ + return d->name; +} + +QStringList Room::aliases() const +{ + return d->aliases; +} + +QString Room::canonicalAlias() const +{ + return d->canonicalAlias; +} + +QString Room::displayName() const +{ + if (name().isEmpty()) + { + // Without a human name, try to find a substitute + if (!canonicalAlias().isEmpty()) + return canonicalAlias(); + if (!aliases().empty() && !aliases().at(0).isEmpty()) + return aliases().at(0); + // Ok, last attempt - one on one chat + if (users().size() == 2) { + return users().at(0)->displayname() + " and " + + users().at(1)->displayname(); + } + // Fail miserably + return id(); + } + + // If we have a non-empty name, try to stack canonical alias to it. + // The format is unwittingly borrowed from the email address format. + QString dispName = name(); + if (!canonicalAlias().isEmpty()) + dispName += " <" + canonicalAlias() + ">"; + + return dispName; +} + +QString Room::topic() const +{ + return d->topic; +} + +JoinState Room::joinState() const +{ + return d->joinState; +} + +void Room::setJoinState(JoinState state) +{ + JoinState oldState = d->joinState; + if( state == oldState ) + return; + d->joinState = state; + emit joinStateChanged(oldState, state); +} + +void Room::markMessageAsRead(Event* event) +{ + d->connection->postReceipt(this, event); +} + +QString Room::lastReadEvent(User* user) +{ + return d->lastReadEvent.value(user); +} + +int Room::notificationCount() const +{ + return d->notificationCount; +} + +void Room::resetNotificationCount() +{ + if( d->notificationCount == 0 ) + return; + d->notificationCount = 0; + emit notificationCountChanged(this); +} + +int Room::highlightCount() const +{ + return d->highlightCount; +} + +void Room::resetHighlightCount() +{ +if( d->highlightCount == 0 ) + return; + d->highlightCount = 0; + emit highlightCountChanged(this); +} + +QList< User* > Room::usersTyping() const +{ + return d->usersTyping; +} + +QList< User* > Room::users() const +{ + return d->users; +} + +void Room::addMessage(Event* event) +{ + processMessageEvent(event); + emit newMessage(event); + //d->addState(event); +} + +void Room::addInitialState(State* state) +{ + processStateEvent(state->event()); +} + +void Room::updateData(const SyncRoomData& data) +{ + if( d->prevBatch.isEmpty() ) + d->prevBatch = data.timelinePrevBatch; + setJoinState(data.joinState); + + for( Event* stateEvent: data.state ) + { + processStateEvent(stateEvent); + } + + for( Event* timelineEvent: data.timeline ) + { + + processMessageEvent(timelineEvent); + emit newMessage(timelineEvent); + // State changes can arrive in a timeline event - try to check those. + processStateEvent(timelineEvent); + } + + for( Event* ephemeralEvent: data.ephemeral ) + { + processEphemeralEvent(ephemeralEvent); + } + + if( data.highlightCount != d->highlightCount ) + { + d->highlightCount = data.highlightCount; + emit highlightCountChanged(this); + } + if( data.notificationCount != d->notificationCount ) + { + d->notificationCount = data.notificationCount; + emit notificationCountChanged(this); + } +} + +void Room::getPreviousContent() +{ + if( !d->gettingNewContent ) + { + d->gettingNewContent = true; + RoomMessagesJob* job = d->connection->getMessages(this, d->prevBatch); + connect( job, &RoomMessagesJob::result, d, &Room::Private::gotMessages ); + } +} + +void Room::Private::gotMessages(KJob* job) +{ + RoomMessagesJob* realJob = static_cast<RoomMessagesJob*>(job); + if( realJob->error() ) + { + qDebug() << realJob->errorString(); + } + else + { + for( Event* event: realJob->events() ) + { + q->processMessageEvent(event); + emit q->newMessage(event); + } + prevBatch = realJob->end(); + } + gettingNewContent = false; +} + +Connection* Room::connection() +{ + return d->connection; +} + +void Room::processMessageEvent(Event* event) +{ + for( int i=0; i<d->messageEvents.count(); i++ ) + { + if( event->timestamp() < d->messageEvents.at(i)->timestamp() ) + { + d->messageEvents.insert(i, event); + return; + } + } + d->messageEvents.append(event); +} + +void Room::processStateEvent(Event* event) +{ + if( event->type() == EventType::RoomName ) + { + if (RoomNameEvent* nameEvent = static_cast<RoomNameEvent*>(event)) + { + d->name = nameEvent->name(); + qDebug() << "room name: " << d->name; + emit namesChanged(this); + } else + { + qDebug() << + "!!! event type is RoomName but the event is not RoomNameEvent"; + } + } + if( event->type() == EventType::RoomAliases ) + { + RoomAliasesEvent* aliasesEvent = static_cast<RoomAliasesEvent*>(event); + d->aliases = aliasesEvent->aliases(); + qDebug() << "room aliases: " << d->aliases; + emit namesChanged(this); + } + if( event->type() == EventType::RoomCanonicalAlias ) + { + RoomCanonicalAliasEvent* aliasEvent = static_cast<RoomCanonicalAliasEvent*>(event); + d->canonicalAlias = aliasEvent->alias(); + qDebug() << "room canonical alias: " << d->canonicalAlias; + emit namesChanged(this); + } + if( event->type() == EventType::RoomTopic ) + { + RoomTopicEvent* topicEvent = static_cast<RoomTopicEvent*>(event); + d->topic = topicEvent->topic(); + emit topicChanged(); + } + if( event->type() == EventType::RoomMember ) + { + RoomMemberEvent* memberEvent = static_cast<RoomMemberEvent*>(event); + User* u = d->connection->user(memberEvent->userId()); + if( !u ) qDebug() << "addState: invalid user!" << u << memberEvent->userId(); + u->processEvent(event); + if( memberEvent->membership() == MembershipType::Join and !d->users.contains(u) ) + { + d->users.append(u); + emit userAdded(u); + } + else if( memberEvent->membership() == MembershipType::Leave + and d->users.contains(u) ) + { + d->users.removeAll(u); + emit userRemoved(u); + } + } +} + +void Room::processEphemeralEvent(Event* event) +{ + if( event->type() == EventType::Typing ) + { + TypingEvent* typingEvent = static_cast<TypingEvent*>(event); + d->usersTyping.clear(); + for( const QString& user: typingEvent->users() ) + { + d->usersTyping.append(d->connection->user(user)); + } + emit typingChanged(); + } + if( event->type() == EventType::Receipt ) + { + ReceiptEvent* receiptEvent = static_cast<ReceiptEvent*>(event); + for( QString eventId: receiptEvent->events() ) + { + QList<Receipt> receipts = receiptEvent->receiptsForEvent(eventId); + for( Receipt r: receipts ) + { + d->lastReadEvent.insert(d->connection->user(r.userId), eventId); + } + } + } +} + +// void Room::setAlias(QString alias) +// { +// d->alias = alias; +// emit aliasChanged(this); +// } +// +// bool Room::parseEvents(const QJsonObject& json) +// { +// QList<LogMessage*> newMessages; +// QJsonValue value = json.value("messages").toObject().value("chunk"); +// if( !value.isArray() ) +// { +// return false; +// } +// QJsonArray messages = value.toArray(); +// for(const QJsonValue& val: messages ) +// { +// if( !val.isObject() ) +// continue; +// LogMessage* msg = Private::parseMessage(val.toObject()); +// if( msg ) +// { +// newMessages.append(msg); +// } +// +// } +// addMessages(newMessages); +// return true; +// } +// +// bool Room::parseSingleEvent(const QJsonObject& json) +// { +// qDebug() << "parseSingleEvent"; +// LogMessage* msg = Private::parseMessage(json); +// if( msg ) +// { +// addMessage(msg); +// return true; +// } +// return false; +// } +// +// bool Room::parseState(const QJsonObject& json) +// { +// QJsonValue value = json.value("state"); +// if( !value.isArray() ) +// { +// return false; +// } +// QJsonArray states = value.toArray(); +// for( const QJsonValue& val: states ) +// { +// QJsonObject state = val.toObject(); +// QString type = state.value("type").toString(); +// if( type == "m.room.aliases" ) +// { +// QJsonArray aliases = state.value("content").toObject().value("aliases").toArray(); +// if( aliases.count() > 0 ) +// { +// setAlias(aliases.at(0).toString()); +// } +// } +// } +// return true; +// } +// +// LogMessage* Room::Private::parseMessage(const QJsonObject& message) +// { +// if( message.value("type") == "m.room.message" ) +// { +// QJsonObject content = message.value("content").toObject(); +// if( content.value("msgtype").toString() != "m.text" ) +// return 0; +// QString user = message.value("user_id").toString(); +// QString body = content.value("body").toString(); +// LogMessage* msg = new LogMessage( LogMessage::UserMessage, body, user ); +// return msg; +// } +// return 0; +// } + @@ -0,0 +1,94 @@ +/****************************************************************************** + * Copyright (C) 2015 Felix Rohrbach <kde@fxrh.de> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef QMATRIXCLIENT_ROOM_H +#define QMATRIXCLIENT_ROOM_H + +#include <QtCore/QList> +#include <QtCore/QObject> +#include <QtCore/QJsonObject> + +#include "jobs/syncjob.h" +#include "joinstate.h" + +namespace QMatrixClient +{ + class Event; + class State; + class Connection; + class User; + + class Room: public QObject + { + Q_OBJECT + public: + Room(Connection* connection, QString id); + virtual ~Room(); + + QString id() const; + QList<Event*> messages() const; + QString name() const; + QStringList aliases() const; + QString canonicalAlias() const; + QString displayName() const; + QString topic() const; + JoinState joinState() const; + QList<User*> usersTyping() const; + + QList<User*> users() const; + + void addMessage( Event* event ); + void addInitialState( State* state ); + void updateData( const SyncRoomData& data ); + void setJoinState( JoinState state ); + + void markMessageAsRead( Event* event ); + QString lastReadEvent(User* user); + + int notificationCount() const; + void resetNotificationCount(); + int highlightCount() const; + void resetHighlightCount(); + + public slots: + void getPreviousContent(); + + signals: + void newMessage(Event* event); + void namesChanged(Room* room); + void topicChanged(); + void userAdded(User* user); + void userRemoved(User* user); + void joinStateChanged(JoinState oldState, JoinState newState); + void typingChanged(); + void highlightCountChanged(Room* room); + void notificationCountChanged(Room* room); + + protected: + Connection* connection(); + virtual void processMessageEvent(Event* event); + virtual void processStateEvent(Event* event); + virtual void processEphemeralEvent(Event* event); + + private: + class Private; + Private* d; + }; +} + +#endif // QMATRIXCLIENT_ROOM_H diff --git a/state.cpp b/state.cpp new file mode 100644 index 00000000..ed57bea4 --- /dev/null +++ b/state.cpp @@ -0,0 +1,69 @@ +/****************************************************************************** + * Copyright (C) 2015 Felix Rohrbach <kde@fxrh.de> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "state.h" + +#include "events/event.h" + +using namespace QMatrixClient; + +class State::Private +{ + public: + Event* event; + QString stateKey; + QString replacesState; +}; + + +State::State(Event* event) + : d(new Private) +{ + d->event = event; +} + +State::~State() +{ + delete d; +} + +Event* State::event() const +{ + return d->event; +} + +QString State::stateKey() const +{ + return d->stateKey; +} + +QString State::replacesState() const +{ + return d->replacesState; +} + +State* State::fromJson(const QJsonObject& obj) +{ + Event* event = Event::fromJson(obj); + if( !event ) + return 0; + State* state = new State(event); + state->d->stateKey = obj.value("state_key").toString(); + state->d->replacesState = obj.value("replaces_state").toString(); + return state; +} diff --git a/state.h b/state.h new file mode 100644 index 00000000..79566eba --- /dev/null +++ b/state.h @@ -0,0 +1,50 @@ +/****************************************************************************** + * Copyright (C) 2015 Felix Rohrbach <kde@fxrh.de> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef QMATRIXCLIENT_STATE_H +#define QMATRIXCLIENT_STATE_H + +#include <QtCore/QString> +#include <QtCore/QJsonObject> + +namespace QMatrixClient +{ + class Event; + + /** + * Wraps an event that is a state + */ + class State + { + public: + State(Event* event); + virtual ~State(); + + Event* event() const; + QString stateKey() const; + QString replacesState() const; + + static State* fromJson(const QJsonObject& obj); + + private: + class Private; + Private* d; + }; +} + +#endif // QMATRIXCLIENT_STATE_H
\ No newline at end of file diff --git a/user.cpp b/user.cpp new file mode 100644 index 00000000..1b232ab8 --- /dev/null +++ b/user.cpp @@ -0,0 +1,150 @@ +/****************************************************************************** + * Copyright (C) 2015 Felix Rohrbach <kde@fxrh.de> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "user.h" + +#include "connection.h" +#include "events/event.h" +#include "events/roommemberevent.h" +#include "jobs/mediathumbnailjob.h" + +#include <QtCore/QTimer> +#include <QtCore/QPair> + +using namespace QMatrixClient; + +class User::Private: public QObject +{ + public: + User* q; + QString userId; + QString name; + QUrl avatarUrl; + Connection* connection; + + QPixmap avatar; + int requestedWidth; + int requestedHeight; + bool avatarValid; + bool avatarOngoingRequest; + QHash<QPair<int,int>,QPixmap> scaledMap; + + void requestAvatar(); + public slots: + void gotAvatar(KJob* job); +}; + +User::User(QString userId, Connection* connection) + : d(new Private) +{ + d->q = this; + d->connection = connection; + d->userId = userId; + d->avatarValid = false; + d->avatarOngoingRequest = false; +} + +User::~User() +{ + delete d; +} + +QString User::id() const +{ + return d->userId; +} + +QString User::name() const +{ + return d->name; +} + +QString User::displayname() const +{ + if( !d->name.isEmpty() ) + return d->name; + return d->userId; +} + +QPixmap User::avatar(int width, int height) +{ + if( !d->avatarValid + || width > d->requestedWidth + || height > d->requestedHeight ) + { + if( !d->avatarOngoingRequest && d->avatarUrl.isValid() ) + { + qDebug() << "Get avatar..."; + d->requestedWidth = width; + d->requestedHeight = height; + d->avatarOngoingRequest = true; + QTimer::singleShot(0, this, SLOT(requestAvatar())); + } + } + + if( d->avatar.isNull() ) + return d->avatar; + QPair<int,int> size(width, height); + if( !d->scaledMap.contains(size) ) + { + d->scaledMap.insert(size, d->avatar.scaled(width, height, Qt::KeepAspectRatio, Qt::SmoothTransformation)); + } + return d->scaledMap.value(size); +} + +void User::processEvent(Event* event) +{ + if( event->type() == EventType::RoomMember ) + { + RoomMemberEvent* e = static_cast<RoomMemberEvent*>(event); + if( d->name != e->displayName() ) + { + d->name = e->displayName(); + emit nameChanged(); + } + if( d->avatarUrl != e->avatarUrl() ) + { + d->avatarUrl = e->avatarUrl(); + d->avatarValid = false; + } + } +} + +void User::requestAvatar() +{ + d->requestAvatar(); +} + +void User::Private::requestAvatar() +{ + MediaThumbnailJob* job = + connection->getThumbnail(avatarUrl, requestedWidth, requestedHeight); + connect( job, &MediaThumbnailJob::result, this, &User::Private::gotAvatar ); +} + +void User::Private::gotAvatar(KJob* job) +{ + avatarOngoingRequest = false; + avatarValid = true; + avatar = + static_cast<MediaThumbnailJob*>(job)->thumbnail() + .scaled(requestedWidth, requestedHeight, + Qt::KeepAspectRatio, Qt::SmoothTransformation); + scaledMap.clear(); + emit q->avatarChanged(q); +}
\ No newline at end of file @@ -0,0 +1,68 @@ +/****************************************************************************** + * Copyright (C) 2015 Felix Rohrbach <kde@fxrh.de> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef QMATRIXCLIENT_USER_H +#define QMATRIXCLIENT_USER_H + +#include <QtCore/QString> +#include <QtCore/QObject> + +namespace QMatrixClient +{ + class Event; + class Connection; + class User: public QObject + { + Q_OBJECT + public: + User(QString userId, Connection* connection); + virtual ~User(); + + /** + * Returns the id of the user + */ + QString id() const; + + /** + * Returns the name chosen by the user + */ + QString name() const; + + /** + * Returns the name that should be used to display the user. + */ + QString displayname() const; + + QPixmap avatar(int requestedWidth, int requestedHeight); + + void processEvent(Event* event); + + public slots: + void requestAvatar(); + + signals: + void nameChanged(); + void avatarChanged(User* user); + + private: + class Private; + Private* d; + }; +} + +#endif // QMATRIXCLIENT_USER_H |