diff options
-rw-r--r-- | connection.cpp | 23 | ||||
-rw-r--r-- | connection.h | 6 | ||||
-rw-r--r-- | connectiondata.cpp | 14 | ||||
-rw-r--r-- | connectiondata.h | 6 | ||||
-rw-r--r-- | connectionprivate.cpp | 29 | ||||
-rw-r--r-- | connectionprivate.h | 4 | ||||
-rw-r--r-- | jobs/basejob.cpp | 9 | ||||
-rw-r--r-- | jobs/basejob.h | 1 | ||||
-rw-r--r-- | libqmatrixclient.pri | 72 | ||||
-rw-r--r-- | room.h | 40 | ||||
-rw-r--r-- | user.cpp | 4 | ||||
-rw-r--r-- | user.h | 6 |
12 files changed, 184 insertions, 30 deletions
diff --git a/connection.cpp b/connection.cpp index 5127dea8..e04de7aa 100644 --- a/connection.cpp +++ b/connection.cpp @@ -54,6 +54,11 @@ Connection::~Connection() delete d; } +void Connection::resolveServer(QString domain) +{ + d->resolveServer( domain ); +} + void Connection::connectToServer(QString user, QString password) { PasswordLogin* loginJob = new PasswordLogin(d->data, user, password); @@ -63,6 +68,14 @@ void Connection::connectToServer(QString user, QString password) d->password = password; } +void Connection::connectWithToken(QString userId, QString token) +{ + d->isConnected = true; + d->userId = userId; + d->data->setToken(token); + emit connected(); +} + void Connection::reconnect() { PasswordLogin* loginJob = new PasswordLogin(d->data, d->username, d->password ); @@ -144,6 +157,16 @@ User *Connection::user() return user(d->userId); } +QString Connection::userId() +{ + return d->userId; +} + +QString Connection::token() +{ + return d->data->token(); +} + QHash< QString, Room* > Connection::roomMap() const { return d->roomMap; diff --git a/connection.h b/connection.h index 64d3d077..9f3d23f3 100644 --- a/connection.h +++ b/connection.h @@ -44,7 +44,9 @@ namespace QMatrixClient QHash<QString, Room*> roomMap() const; Q_INVOKABLE virtual bool isConnected(); + Q_INVOKABLE virtual void resolveServer( QString domain ); Q_INVOKABLE virtual void connectToServer( QString user, QString password ); + Q_INVOKABLE virtual void connectWithToken( QString userId, QString token ); Q_INVOKABLE virtual void reconnect(); Q_INVOKABLE virtual SyncJob* sync(int timeout=-1); Q_INVOKABLE virtual void postMessage( Room* room, QString type, QString message ); @@ -57,16 +59,20 @@ namespace QMatrixClient Q_INVOKABLE virtual User* user(QString userId); Q_INVOKABLE virtual User* user(); + Q_INVOKABLE virtual QString userId(); + Q_INVOKABLE virtual QString token(); signals: void connected(); void reconnected(); + void resolved(); void syncDone(); void newRoom(Room* room); void joinedRoom(Room* room); void loginError(QString error); void connectionError(QString error); + void resolveError(QString error); //void jobError(BaseJob* job); protected: diff --git a/connectiondata.cpp b/connectiondata.cpp index 374484b0..4a3bd47f 100644 --- a/connectiondata.cpp +++ b/connectiondata.cpp @@ -72,6 +72,18 @@ void ConnectionData::setToken(QString token) d->token = token; } +void ConnectionData::setHost(QString host) +{ + d->baseUrl.setHost(host); + qDebug() << "updated baseUrl to" << d->baseUrl; +} + +void ConnectionData::setPort(int port) +{ + d->baseUrl.setPort(port); + qDebug() << "updated baseUrl to" << d->baseUrl; +} + QString ConnectionData::lastEvent() const { return d->lastEvent; @@ -80,4 +92,4 @@ QString ConnectionData::lastEvent() const void ConnectionData::setLastEvent(QString identifier) { d->lastEvent = identifier; -}
\ No newline at end of file +} diff --git a/connectiondata.h b/connectiondata.h index e11f54b5..6407780c 100644 --- a/connectiondata.h +++ b/connectiondata.h @@ -34,9 +34,11 @@ namespace QMatrixClient //bool isConnected() const; QString token() const; QUrl baseUrl() const; - + QNetworkAccessManager* nam() const; void setToken( QString token ); + void setHost( QString host ); + void setPort( int port ); QString lastEvent() const; void setLastEvent( QString identifier ); @@ -47,4 +49,4 @@ namespace QMatrixClient }; } -#endif // QMATRIXCLIENT_CONNECTIONDATA_H
\ No newline at end of file +#endif // QMATRIXCLIENT_CONNECTIONDATA_H diff --git a/connectionprivate.cpp b/connectionprivate.cpp index 90733ed0..50fd1b0c 100644 --- a/connectionprivate.cpp +++ b/connectionprivate.cpp @@ -31,6 +31,7 @@ #include "events/roommemberevent.h" #include <QtCore/QDebug> +#include <QtNetwork/QDnsLookup> using namespace QMatrixClient; @@ -46,6 +47,32 @@ ConnectionPrivate::~ConnectionPrivate() delete data; } +void ConnectionPrivate::resolveServer(QString domain) +{ + // Find the Matrix server for the given domain. + QDnsLookup* dns = new QDnsLookup(); + dns->setType(QDnsLookup::SRV); + dns->setName("_matrix._tcp." + domain); + + connect(dns, &QDnsLookup::finished, [this,dns]() { + // Check the lookup succeeded. + if (dns->error() != QDnsLookup::NoError || + dns->serviceRecords().isEmpty()) { + emit q->resolveError("DNS lookup failed"); + dns->deleteLater(); + return; + } + + // Handle the results. + QDnsServiceRecord record = dns->serviceRecords().first(); + data->setHost(record.target()); + data->setPort(record.port()); + emit q->resolved(); + dns->deleteLater(); + }); + dns->lookup(); +} + void ConnectionPrivate::processState(State* state) { QString roomId = state->event()->roomId(); @@ -175,4 +202,4 @@ void ConnectionPrivate::gotRoomMembers(KJob* job) if( membersJob->error() == BaseJob::NetworkError ) emit q->connectionError( membersJob->errorString() ); } -}
\ No newline at end of file +} diff --git a/connectionprivate.h b/connectionprivate.h index 2aeeaf0b..1fccc759 100644 --- a/connectionprivate.h +++ b/connectionprivate.h @@ -43,6 +43,8 @@ namespace QMatrixClient ConnectionPrivate(Connection* parent); ~ConnectionPrivate(); + void resolveServer( QString domain ); + void processState( State* state ); void processRooms( const QList<SyncRoomData>& data ); @@ -64,4 +66,4 @@ namespace QMatrixClient }; } -#endif // QMATRIXCLIENT_CONNECTIONPRIVATE_H
\ No newline at end of file +#endif // QMATRIXCLIENT_CONNECTIONPRIVATE_H diff --git a/jobs/basejob.cpp b/jobs/basejob.cpp index bf4ade89..519e1517 100644 --- a/jobs/basejob.cpp +++ b/jobs/basejob.cpp @@ -97,6 +97,7 @@ void BaseJob::start() d->reply = d->connection->nam()->put(req, data.toJson()); break; } + connect( d->reply, &QNetworkReply::sslErrors, this, &BaseJob::sslErrors ); 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), @@ -144,3 +145,11 @@ void BaseJob::timeout() if( d->reply->isRunning() ) d->reply->abort(); } + +void BaseJob::sslErrors(const QList<QSslError>& errors) +{ + foreach (const QSslError &error, errors) { + qWarning() << "SSL ERROR" << error.errorString(); + } + d->reply->ignoreSslErrors(); // TODO: insecure! should prompt user first +} diff --git a/jobs/basejob.h b/jobs/basejob.h index 88911ca1..95cf4232 100644 --- a/jobs/basejob.h +++ b/jobs/basejob.h @@ -63,6 +63,7 @@ namespace QMatrixClient protected slots: virtual void gotReply(); void timeout(); + void sslErrors(const QList<QSslError>& errors); //void networkError(QNetworkReply::NetworkError code); diff --git a/libqmatrixclient.pri b/libqmatrixclient.pri new file mode 100644 index 00000000..1290aa19 --- /dev/null +++ b/libqmatrixclient.pri @@ -0,0 +1,72 @@ +QT += network +CONFIG += c++11 + +INCLUDEPATH += $$PWD $$PWD/kcoreaddons/src/lib/jobs + +HEADERS += \ + $$PWD/connectiondata.h \ + $$PWD/connection.h \ + $$PWD/connectionprivate.h \ + $$PWD/room.h \ + $$PWD/user.h \ + $$PWD/logmessage.h \ + $$PWD/state.h \ + $$PWD/events/event.h \ + $$PWD/events/roommessageevent.h \ + $$PWD/events/roomnameevent.h \ + $$PWD/events/roomaliasesevent.h \ + $$PWD/events/roomcanonicalaliasevent.h \ + $$PWD/events/roommemberevent.h \ + $$PWD/events/roomtopicevent.h \ + $$PWD/events/typingevent.h \ + $$PWD/events/receiptevent.h \ + $$PWD/events/unknownevent.h \ + $$PWD/jobs/basejob.h \ + $$PWD/jobs/checkauthmethods.h \ + $$PWD/jobs/passwordlogin.h \ + $$PWD/jobs/postmessagejob.h \ + $$PWD/jobs/postreceiptjob.h \ + $$PWD/jobs/joinroomjob.h \ + $$PWD/jobs/leaveroomjob.h \ + $$PWD/jobs/roommembersjob.h \ + $$PWD/jobs/roommessagesjob.h \ + $$PWD/jobs/syncjob.h \ + $$PWD/jobs/mediathumbnailjob.h \ + $$PWD/kcoreaddons/src/lib/jobs/kjob.h \ + $$PWD/kcoreaddons/src/lib/jobs/kcompositejob.h \ + $$PWD/kcoreaddons/src/lib/jobs/kjobtrackerinterface.h \ + $$PWD/kcoreaddons/src/lib/jobs/kjobuidelegate.h + +SOURCES += \ + $$PWD/connectiondata.cpp \ + $$PWD/connection.cpp \ + $$PWD/connectionprivate.cpp \ + $$PWD/room.cpp \ + $$PWD/user.cpp \ + $$PWD/logmessage.cpp \ + $$PWD/state.cpp \ + $$PWD/events/event.cpp \ + $$PWD/events/roommessageevent.cpp \ + $$PWD/events/roomnameevent.cpp \ + $$PWD/events/roomaliasesevent.cpp \ + $$PWD/events/roomcanonicalaliasevent.cpp \ + $$PWD/events/roommemberevent.cpp \ + $$PWD/events/roomtopicevent.cpp \ + $$PWD/events/typingevent.cpp \ + $$PWD/events/receiptevent.cpp \ + $$PWD/events/unknownevent.cpp \ + $$PWD/jobs/basejob.cpp \ + $$PWD/jobs/checkauthmethods.cpp \ + $$PWD/jobs/passwordlogin.cpp \ + $$PWD/jobs/postmessagejob.cpp \ + $$PWD/jobs/postreceiptjob.cpp \ + $$PWD/jobs/joinroomjob.cpp \ + $$PWD/jobs/leaveroomjob.cpp \ + $$PWD/jobs/roommembersjob.cpp \ + $$PWD/jobs/roommessagesjob.cpp \ + $$PWD/jobs/syncjob.cpp \ + $$PWD/jobs/mediathumbnailjob.cpp \ + $$PWD/kcoreaddons/src/lib/jobs/kjob.cpp \ + $$PWD/kcoreaddons/src/lib/jobs/kcompositejob.cpp \ + $$PWD/kcoreaddons/src/lib/jobs/kjobtrackerinterface.cpp \ + $$PWD/kcoreaddons/src/lib/jobs/kjobuidelegate.cpp @@ -40,30 +40,30 @@ namespace QMatrixClient Room(Connection* connection, QString id); virtual ~Room(); - QString id() const; - QList<Event*> messageEvents() const; - QString name() const; - QStringList aliases() const; - QString canonicalAlias() const; - QString displayName() const; - QString topic() const; - JoinState joinState() const; - QList<User*> usersTyping() const; + Q_INVOKABLE QString id() const; + Q_INVOKABLE QList<Event*> messageEvents() const; + Q_INVOKABLE QString name() const; + Q_INVOKABLE QStringList aliases() const; + Q_INVOKABLE QString canonicalAlias() const; + Q_INVOKABLE QString displayName() const; + Q_INVOKABLE QString topic() const; + Q_INVOKABLE JoinState joinState() const; + Q_INVOKABLE QList<User*> usersTyping() const; - QList<User*> users() const; + Q_INVOKABLE QList<User*> users() const; - void addMessage( Event* event ); - void addInitialState( State* state ); - void updateData( const SyncRoomData& data ); - void setJoinState( JoinState state ); + Q_INVOKABLE void addMessage( Event* event ); + Q_INVOKABLE void addInitialState( State* state ); + Q_INVOKABLE void updateData( const SyncRoomData& data ); + Q_INVOKABLE void setJoinState( JoinState state ); - void markMessageAsRead( Event* event ); - QString lastReadEvent(User* user); + Q_INVOKABLE void markMessageAsRead( Event* event ); + Q_INVOKABLE QString lastReadEvent(User* user); - int notificationCount() const; - void resetNotificationCount(); - int highlightCount() const; - void resetHighlightCount(); + Q_INVOKABLE int notificationCount() const; + Q_INVOKABLE void resetNotificationCount(); + Q_INVOKABLE int highlightCount() const; + Q_INVOKABLE void resetHighlightCount(); public slots: void getPreviousContent(); @@ -50,7 +50,7 @@ class User::Private: public QObject }; User::User(QString userId, Connection* connection) - : d(new Private) + : QObject(connection), d(new Private) { d->q = this; d->connection = connection; @@ -147,4 +147,4 @@ void User::Private::gotAvatar(KJob* job) Qt::KeepAspectRatio, Qt::SmoothTransformation); scaledMap.clear(); emit q->avatarChanged(q); -}
\ No newline at end of file +} @@ -36,17 +36,17 @@ namespace QMatrixClient /** * Returns the id of the user */ - QString id() const; + Q_INVOKABLE QString id() const; /** * Returns the name chosen by the user */ - QString name() const; + Q_INVOKABLE QString name() const; /** * Returns the name that should be used to display the user. */ - QString displayname() const; + Q_INVOKABLE QString displayname() const; QPixmap avatar(int requestedWidth, int requestedHeight); |