Use const refs for the deserialized data

pull/1/head
Konstantinos Sideris 8 years ago
parent 5bcaaa3aa3
commit 73e73f46ea
  1. 2
      CMakeLists.txt
  2. 10
      Makefile
  3. 9
      README.md
  4. 8
      include/ChatPage.h
  5. 6
      include/Deserializable.h
  6. 4
      include/HistoryView.h
  7. 2
      include/HistoryViewItem.h
  8. 37
      include/Login.h
  9. 23
      include/MatrixClient.h
  10. 16
      include/Profile.h
  11. 2
      include/RoomInfoListItem.h
  12. 124
      include/Sync.h
  13. 8
      src/ChatPage.cc
  14. 4
      src/HistoryView.cc
  15. 2
      src/HistoryViewItem.cc
  16. 27
      src/Login.cc
  17. 6
      src/MatrixClient.cc
  18. 12
      src/Profile.cc
  19. 92
      src/Sync.cc

@ -31,7 +31,7 @@ ELSE("${CMAKE_BUILD_TYPE}" STREQUAL "Release")
COMMAND ${GIT} rev-parse --short HEAD
OUTPUT_VARIABLE GIT_OUT OUTPUT_STRIP_TRAILING_WHITESPACE
)
SET(PATCH_OUT "0-git${GIT_OUT}")
SET(PATCH_OUT "0-${GIT_OUT}")
ELSE(GIT)
SET(PATCH_OUT "0")
ENDIF(GIT)

@ -1,12 +1,12 @@
run: build
@./build/nheko
./build/nheko
build:
@cmake -H. -Bbuild
debug:
@cmake -H. -Bbuild -DCMAKE_BUILD_TYPE=Debug
@make -C build
release:
@cmake -H. -Bbuild -DCMAKE_BUILD_TYPE=Release
release-debug:
@cmake -H. -Bbuild -DCMAKE_BUILD_TYPE=RelWithDebInfo
@make -C build
clean:

@ -27,7 +27,14 @@ $ sudo pacman -S qt5-base cmake gcc
#### Building
Run `make build`. The `nheko` binary will be located in the `build` directory.
Run
```bash
cmake -H. -Bbuild -DCMAKE_BUILD_TYPE=Release # Default is Debug.
make -C build
```
The `nheko` binary will be located in the `build` directory.
#### Contributing

@ -50,10 +50,10 @@ public:
public slots:
// Updates the user info box.
void updateOwnProfileInfo(QUrl avatar_url, QString display_name);
void updateOwnProfileInfo(const QUrl &avatar_url, const QString &display_name);
void fetchRoomAvatar(const QString &roomid, const QUrl &avatar_url);
void initialSyncCompleted(SyncResponse response);
void syncCompleted(SyncResponse response);
void initialSyncCompleted(const SyncResponse &response);
void syncCompleted(const SyncResponse &response);
void changeTopRoomInfo(const RoomInfo &info);
void sendTextMessage(const QString &msg);
void messageSent(const QString event_id, int txn_id);
@ -62,7 +62,7 @@ public slots:
private:
Ui::ChatPage *ui;
void setOwnAvatar(QByteArray img);
void setOwnAvatar(const QByteArray &img);
RoomList *room_list_;
HistoryViewManager *view_manager_;

@ -38,13 +38,13 @@ private:
class Deserializable
{
public:
virtual void deserialize(QJsonValue) throw(DeserializationException)
virtual void deserialize(const QJsonValue &) throw(DeserializationException)
{
}
virtual void deserialize(QJsonObject) throw(DeserializationException)
virtual void deserialize(const QJsonObject &) throw(DeserializationException)
{
}
virtual void deserialize(QJsonDocument) throw(DeserializationException)
virtual void deserialize(const QJsonDocument &) throw(DeserializationException)
{
}
};

@ -33,10 +33,10 @@ class HistoryView : public QWidget
public:
explicit HistoryView(QWidget *parent = 0);
explicit HistoryView(QList<Event> events, QWidget *parent = 0);
explicit HistoryView(const QList<Event> &events, QWidget *parent = 0);
~HistoryView();
void addHistoryItem(Event event, QString color, bool with_sender);
void addHistoryItem(const Event &event, const QString &color, bool with_sender);
void addEvents(const QList<Event> &events);
public slots:

@ -28,7 +28,7 @@ class HistoryViewItem : public QWidget
{
Q_OBJECT
public:
HistoryViewItem(Event event, bool with_sender, QString color, QWidget *parent = 0);
HistoryViewItem(const Event &event, bool with_sender, const QString &color, QWidget *parent = 0);
~HistoryViewItem();
private:

@ -30,22 +30,32 @@ public:
QByteArray serialize();
void setPassword(QString password);
void setUser(QString username);
inline void setPassword(QString password);
inline void setUser(QString username);
private:
QString user_;
QString password_;
};
inline void LoginRequest::setPassword(QString password)
{
password_ = password;
}
inline void LoginRequest::setUser(QString username)
{
user_ = username;
}
class LoginResponse : public Deserializable
{
public:
void deserialize(QJsonDocument data) throw(DeserializationException) override;
void deserialize(const QJsonDocument &data) throw(DeserializationException) override;
QString getAccessToken();
QString getHomeServer();
QString getUserId();
inline QString getAccessToken();
inline QString getHomeServer();
inline QString getUserId();
private:
QString access_token_;
@ -53,4 +63,19 @@ private:
QString user_id_;
};
inline QString LoginResponse::getAccessToken()
{
return access_token_;
}
inline QString LoginResponse::getHomeServer()
{
return home_server_;
}
inline QString LoginResponse::getUserId()
{
return user_id_;
}
#endif // LOGIN_H

@ -32,12 +32,11 @@ class MatrixClient : public QNetworkAccessManager
Q_OBJECT
public:
MatrixClient(QString server, QObject *parent = 0);
~MatrixClient();
// Client API.
void initialSync();
void sync();
void sendTextMessage(QString roomid, QString msg);
void sendTextMessage(const QString &roomid, const QString &msg);
void login(const QString &username, const QString &password);
void registerUser(const QString &username, const QString &password);
void versions();
@ -49,22 +48,22 @@ public slots:
// Profile
void getOwnProfile();
inline void setServer(QString server);
inline void setAccessToken(QString token);
inline void setServer(const QString &server);
inline void setAccessToken(const QString &token);
inline void setNextBatchToken(const QString &next_batch);
signals:
// Emitted after a error during the login.
void loginError(QString error);
void loginError(const QString &error);
// Emitted after succesfull user login. A new access token is returned by the server.
void loginSuccess(QString user_id, QString home_server, QString token);
void loginSuccess(const QString &userid, const QString &homeserver, const QString &token);
// Returned profile data for the user's account.
void getOwnProfileResponse(QUrl avatar_url, QString display_name);
void initialSyncCompleted(SyncResponse response);
void syncCompleted(SyncResponse response);
void messageSent(QString event_id, int txn_id);
void getOwnProfileResponse(const QUrl &avatar_url, const QString &display_name);
void initialSyncCompleted(const SyncResponse &response);
void syncCompleted(const SyncResponse &response);
void messageSent(const QString &event_id, const int txn_id);
private slots:
void onResponse(QNetworkReply *reply);
@ -111,12 +110,12 @@ inline QString MatrixClient::getHomeServer()
return server_;
}
inline void MatrixClient::setServer(QString server)
inline void MatrixClient::setServer(const QString &server)
{
server_ = "https://" + server;
}
inline void MatrixClient::setAccessToken(QString token)
inline void MatrixClient::setAccessToken(const QString &token)
{
token_ = token;
}

@ -26,14 +26,24 @@
class ProfileResponse : public Deserializable
{
public:
void deserialize(QJsonDocument data) throw(DeserializationException) override;
void deserialize(const QJsonDocument &data) throw(DeserializationException) override;
QUrl getAvatarUrl();
QString getDisplayName();
inline QUrl getAvatarUrl();
inline QString getDisplayName();
private:
QUrl avatar_url_;
QString display_name_;
};
inline QUrl ProfileResponse::getAvatarUrl()
{
return avatar_url_;
}
inline QString ProfileResponse::getDisplayName()
{
return display_name_;
}
#endif // PROFILE_H

@ -40,7 +40,7 @@ public:
inline void setAvatar(const QImage &avatar_image);
signals:
void clicked(RoomInfo info_);
void clicked(const RoomInfo &info_);
public slots:
void setPressedState(bool state);

@ -27,17 +27,17 @@
class Event : public Deserializable
{
public:
QJsonObject content() const;
QJsonObject unsigned_content() const;
inline QJsonObject content() const;
inline QJsonObject unsigned_content() const;
QString sender() const;
QString state_key() const;
QString type() const;
QString eventId() const;
inline QString sender() const;
inline QString state_key() const;
inline QString type() const;
inline QString eventId() const;
uint64_t timestamp() const;
inline uint64_t timestamp() const;
void deserialize(QJsonValue data) throw(DeserializationException) override;
void deserialize(const QJsonValue &data) throw(DeserializationException) override;
private:
QJsonObject content_;
@ -51,24 +51,64 @@ private:
uint64_t origin_server_ts_;
};
inline QJsonObject Event::content() const
{
return content_;
}
inline QJsonObject Event::unsigned_content() const
{
return unsigned_;
}
inline QString Event::sender() const
{
return sender_;
}
inline QString Event::state_key() const
{
return state_key_;
}
inline QString Event::type() const
{
return type_;
}
inline QString Event::eventId() const
{
return event_id_;
}
inline uint64_t Event::timestamp() const
{
return origin_server_ts_;
}
class State : public Deserializable
{
public:
void deserialize(QJsonValue data) throw(DeserializationException) override;
QList<Event> events() const;
void deserialize(const QJsonValue &data) throw(DeserializationException) override;
inline QList<Event> events() const;
private:
QList<Event> events_;
};
inline QList<Event> State::events() const
{
return events_;
}
class Timeline : public Deserializable
{
public:
QList<Event> events() const;
QString previousBatch() const;
bool limited() const;
inline QList<Event> events() const;
inline QString previousBatch() const;
inline bool limited() const;
void deserialize(QJsonValue data) throw(DeserializationException) override;
void deserialize(const QJsonValue &data) throw(DeserializationException) override;
private:
QList<Event> events_;
@ -76,14 +116,29 @@ private:
bool limited_;
};
inline QList<Event> Timeline::events() const
{
return events_;
}
inline QString Timeline::previousBatch() const
{
return prev_batch_;
}
inline bool Timeline::limited() const
{
return limited_;
}
// TODO: Add support for ehpmeral, account_data, undread_notifications
class JoinedRoom : public Deserializable
{
public:
State state() const;
Timeline timeline() const;
inline State state() const;
inline Timeline timeline() const;
void deserialize(QJsonValue data) throw(DeserializationException) override;
void deserialize(const QJsonValue &data) throw(DeserializationException) override;
private:
State state_;
@ -93,27 +148,52 @@ private:
/* UnreadNotifications unread_notifications_; */
};
inline State JoinedRoom::state() const
{
return state_;
}
inline Timeline JoinedRoom::timeline() const
{
return timeline_;
}
// TODO: Add support for invited and left rooms.
class Rooms : public Deserializable
{
public:
QMap<QString, JoinedRoom> join() const;
void deserialize(QJsonValue data) throw(DeserializationException) override;
inline QMap<QString, JoinedRoom> join() const;
void deserialize(const QJsonValue &data) throw(DeserializationException) override;
private:
QMap<QString, JoinedRoom> join_;
};
inline QMap<QString, JoinedRoom> Rooms::join() const
{
return join_;
}
class SyncResponse : public Deserializable
{
public:
void deserialize(QJsonDocument data) throw(DeserializationException) override;
QString nextBatch() const;
Rooms rooms() const;
void deserialize(const QJsonDocument &data) throw(DeserializationException) override;
inline QString nextBatch() const;
inline Rooms rooms() const;
private:
QString next_batch_;
Rooms rooms_;
};
inline Rooms SyncResponse::rooms() const
{
return rooms_;
}
inline QString SyncResponse::nextBatch() const
{
return next_batch_;
}
#endif // SYNC_H

@ -124,7 +124,7 @@ void ChatPage::startSync()
matrix_client_->sync();
}
void ChatPage::setOwnAvatar(QByteArray img)
void ChatPage::setOwnAvatar(const QByteArray &img)
{
if (img.size() == 0)
return;
@ -134,7 +134,7 @@ void ChatPage::setOwnAvatar(QByteArray img)
user_info_widget_->setAvatar(pixmap.toImage());
}
void ChatPage::syncCompleted(SyncResponse response)
void ChatPage::syncCompleted(const SyncResponse &response)
{
matrix_client_->setNextBatchToken(response.nextBatch());
@ -142,7 +142,7 @@ void ChatPage::syncCompleted(SyncResponse response)
view_manager_->sync(response.rooms());
}
void ChatPage::initialSyncCompleted(SyncResponse response)
void ChatPage::initialSyncCompleted(const SyncResponse &response)
{
if (!response.nextBatch().isEmpty())
matrix_client_->setNextBatchToken(response.nextBatch());
@ -210,7 +210,7 @@ void ChatPage::fetchRoomAvatar(const QString &roomid, const QUrl &avatar_url)
});
}
void ChatPage::updateOwnProfileInfo(QUrl avatar_url, QString display_name)
void ChatPage::updateOwnProfileInfo(const QUrl &avatar_url, const QString &display_name)
{
QSettings settings;
auto userid = settings.value("auth/user_id").toString();

@ -50,7 +50,7 @@ const QList<QString> HistoryView::COLORS({"#FFF46E",
"#a2b636",
"#4BBE2E"});
HistoryView::HistoryView(QList<Event> events, QWidget *parent)
HistoryView::HistoryView(const QList<Event> &events, QWidget *parent)
: QWidget(parent)
{
init();
@ -133,7 +133,7 @@ void HistoryView::init()
SLOT(sliderRangeChanged(int, int)));
}
void HistoryView::addHistoryItem(Event event, QString color, bool with_sender)
void HistoryView::addHistoryItem(const Event &event, const QString &color, bool with_sender)
{
// TODO: Probably create another function instead of passing the flag.
HistoryViewItem *item = new HistoryViewItem(event, with_sender, color, scroll_widget_);

@ -20,7 +20,7 @@
#include "HistoryViewItem.h"
HistoryViewItem::HistoryViewItem(Event event, bool with_sender, QString color, QWidget *parent)
HistoryViewItem::HistoryViewItem(const Event &event, bool with_sender, const QString &color, QWidget *parent)
: QWidget(parent)
{
QString sender = "";

@ -42,32 +42,7 @@ QByteArray LoginRequest::serialize()
return QJsonDocument(body).toJson(QJsonDocument::Compact);
}
void LoginRequest::setPassword(QString password)
{
password_ = password;
}
void LoginRequest::setUser(QString username)
{
user_ = username;
}
QString LoginResponse::getAccessToken()
{
return access_token_;
}
QString LoginResponse::getHomeServer()
{
return home_server_;
}
QString LoginResponse::getUserId()
{
return user_id_;
}
void LoginResponse::deserialize(QJsonDocument data) throw(DeserializationException)
void LoginResponse::deserialize(const QJsonDocument &data) throw(DeserializationException)
{
if (!data.isObject())
throw DeserializationException("Login response is not a JSON object");

@ -42,10 +42,6 @@ MatrixClient::MatrixClient(QString server, QObject *parent)
connect(this, SIGNAL(finished(QNetworkReply *)), this, SLOT(onResponse(QNetworkReply *)));
}
MatrixClient::~MatrixClient()
{
}
void MatrixClient::onVersionsResponse(QNetworkReply *reply)
{
reply->deleteLater();
@ -289,7 +285,7 @@ void MatrixClient::sync()
reply->setProperty("endpoint", Endpoint::Sync);
}
void MatrixClient::sendTextMessage(QString roomid, QString msg)
void MatrixClient::sendTextMessage(const QString &roomid, const QString &msg)
{
QUrlQuery query;
query.addQueryItem("access_token", token_);

@ -22,17 +22,7 @@
#include "Deserializable.h"
#include "Profile.h"
QUrl ProfileResponse::getAvatarUrl()
{
return avatar_url_;
}
QString ProfileResponse::getDisplayName()
{
return display_name_;
}
void ProfileResponse::deserialize(QJsonDocument data) throw(DeserializationException)
void ProfileResponse::deserialize(const QJsonDocument &data) throw(DeserializationException)
{
if (!data.isObject())
throw DeserializationException("Profile response is not a JSON object");

@ -24,12 +24,7 @@
#include "Deserializable.h"
#include "Sync.h"
QString SyncResponse::nextBatch() const
{
return next_batch_;
}
void SyncResponse::deserialize(QJsonDocument data) throw(DeserializationException)
void SyncResponse::deserialize(const QJsonDocument &data) throw(DeserializationException)
{
if (!data.isObject())
throw DeserializationException("Sync response is not a JSON object");
@ -46,17 +41,7 @@ void SyncResponse::deserialize(QJsonDocument data) throw(DeserializationExceptio
next_batch_ = object.value("next_batch").toString();
}
Rooms SyncResponse::rooms() const
{
return rooms_;
}
QMap<QString, JoinedRoom> Rooms::join() const
{
return join_;
}
void Rooms::deserialize(QJsonValue data) throw(DeserializationException)
void Rooms::deserialize(const QJsonValue &data) throw(DeserializationException)
{
if (!data.isObject())
throw DeserializationException("Rooms value is not a JSON object");
@ -96,17 +81,7 @@ void Rooms::deserialize(QJsonValue data) throw(DeserializationException)
}
}
State JoinedRoom::state() const
{
return state_;
}
Timeline JoinedRoom::timeline() const
{
return timeline_;
}
void JoinedRoom::deserialize(QJsonValue data) throw(DeserializationException)
void JoinedRoom::deserialize(const QJsonValue &data) throw(DeserializationException)
{
if (!data.isObject())
throw DeserializationException("JoinedRoom is not a JSON object");
@ -137,42 +112,7 @@ void JoinedRoom::deserialize(QJsonValue data) throw(DeserializationException)
timeline_.deserialize(object.value("timeline"));
}
QJsonObject Event::content() const
{
return content_;
}
QJsonObject Event::unsigned_content() const
{
return unsigned_;
}
QString Event::sender() const
{
return sender_;
}
QString Event::state_key() const
{
return state_key_;
}
QString Event::type() const
{
return type_;
}
QString Event::eventId() const
{
return event_id_;
}
uint64_t Event::timestamp() const
{
return origin_server_ts_;
}
void Event::deserialize(QJsonValue data) throw(DeserializationException)
void Event::deserialize(const QJsonValue &data) throw(DeserializationException)
{
if (!data.isObject())
throw DeserializationException("Event is not a JSON object");
@ -212,12 +152,7 @@ void Event::deserialize(QJsonValue data) throw(DeserializationException)
origin_server_ts_ = object.value("origin_server_ts").toDouble();
}
QList<Event> State::events() const
{
return events_;
}
void State::deserialize(QJsonValue data) throw(DeserializationException)
void State::deserialize(const QJsonValue &data) throw(DeserializationException)
{
if (!data.isArray())
throw DeserializationException("State is not a JSON array");
@ -237,22 +172,7 @@ void State::deserialize(QJsonValue data) throw(DeserializationException)
}
}
QList<Event> Timeline::events() const
{
return events_;
}
QString Timeline::previousBatch() const
{
return prev_batch_;
}
bool Timeline::limited() const
{
return limited_;
}
void Timeline::deserialize(QJsonValue data) throw(DeserializationException)
void Timeline::deserialize(const QJsonValue &data) throw(DeserializationException)
{
if (!data.isObject())
throw DeserializationException("Timeline is not a JSON object");

Loading…
Cancel
Save