Use const refs for the deserialized data

This commit is contained in:
Konstantinos Sideris 2017-04-06 19:56:33 +03:00
parent 5bcaaa3aa3
commit 73e73f46ea
19 changed files with 198 additions and 196 deletions

View File

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

View File

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

View File

@ -27,7 +27,14 @@ $ sudo pacman -S qt5-base cmake gcc
#### Building #### 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 #### Contributing

View File

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

View File

@ -38,13 +38,13 @@ private:
class Deserializable class Deserializable
{ {
public: 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)
{ {
} }
}; };

View File

@ -33,10 +33,10 @@ class HistoryView : public QWidget
public: public:
explicit HistoryView(QWidget *parent = 0); explicit HistoryView(QWidget *parent = 0);
explicit HistoryView(QList<Event> events, QWidget *parent = 0); explicit HistoryView(const QList<Event> &events, QWidget *parent = 0);
~HistoryView(); ~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); void addEvents(const QList<Event> &events);
public slots: public slots:

View File

@ -28,7 +28,7 @@ class HistoryViewItem : public QWidget
{ {
Q_OBJECT Q_OBJECT
public: 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(); ~HistoryViewItem();
private: private:

View File

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

View File

@ -32,12 +32,11 @@ class MatrixClient : public QNetworkAccessManager
Q_OBJECT Q_OBJECT
public: public:
MatrixClient(QString server, QObject *parent = 0); MatrixClient(QString server, QObject *parent = 0);
~MatrixClient();
// Client API. // Client API.
void initialSync(); void initialSync();
void sync(); 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 login(const QString &username, const QString &password);
void registerUser(const QString &username, const QString &password); void registerUser(const QString &username, const QString &password);
void versions(); void versions();
@ -49,22 +48,22 @@ public slots:
// Profile // Profile
void getOwnProfile(); void getOwnProfile();
inline void setServer(QString server); inline void setServer(const QString &server);
inline void setAccessToken(QString token); inline void setAccessToken(const QString &token);
inline void setNextBatchToken(const QString &next_batch); inline void setNextBatchToken(const QString &next_batch);
signals: signals:
// Emitted after a error during the login. // 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. // 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. // Returned profile data for the user's account.
void getOwnProfileResponse(QUrl avatar_url, QString display_name); void getOwnProfileResponse(const QUrl &avatar_url, const QString &display_name);
void initialSyncCompleted(SyncResponse response); void initialSyncCompleted(const SyncResponse &response);
void syncCompleted(SyncResponse response); void syncCompleted(const SyncResponse &response);
void messageSent(QString event_id, int txn_id); void messageSent(const QString &event_id, const int txn_id);
private slots: private slots:
void onResponse(QNetworkReply *reply); void onResponse(QNetworkReply *reply);
@ -111,12 +110,12 @@ inline QString MatrixClient::getHomeServer()
return server_; return server_;
} }
inline void MatrixClient::setServer(QString server) inline void MatrixClient::setServer(const QString &server)
{ {
server_ = "https://" + server; server_ = "https://" + server;
} }
inline void MatrixClient::setAccessToken(QString token) inline void MatrixClient::setAccessToken(const QString &token)
{ {
token_ = token; token_ = token;
} }

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -20,7 +20,7 @@
#include "HistoryViewItem.h" #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) : QWidget(parent)
{ {
QString sender = ""; QString sender = "";

View File

@ -42,32 +42,7 @@ QByteArray LoginRequest::serialize()
return QJsonDocument(body).toJson(QJsonDocument::Compact); return QJsonDocument(body).toJson(QJsonDocument::Compact);
} }
void LoginRequest::setPassword(QString password) void LoginResponse::deserialize(const QJsonDocument &data) throw(DeserializationException)
{
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)
{ {
if (!data.isObject()) if (!data.isObject())
throw DeserializationException("Login response is not a JSON object"); throw DeserializationException("Login response is not a JSON object");

View File

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

View File

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

View File

@ -24,12 +24,7 @@
#include "Deserializable.h" #include "Deserializable.h"
#include "Sync.h" #include "Sync.h"
QString SyncResponse::nextBatch() const void SyncResponse::deserialize(const QJsonDocument &data) throw(DeserializationException)
{
return next_batch_;
}
void SyncResponse::deserialize(QJsonDocument data) throw(DeserializationException)
{ {
if (!data.isObject()) if (!data.isObject())
throw DeserializationException("Sync response is not a JSON object"); 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(); next_batch_ = object.value("next_batch").toString();
} }
Rooms SyncResponse::rooms() const void Rooms::deserialize(const QJsonValue &data) throw(DeserializationException)
{
return rooms_;
}
QMap<QString, JoinedRoom> Rooms::join() const
{
return join_;
}
void Rooms::deserialize(QJsonValue data) throw(DeserializationException)
{ {
if (!data.isObject()) if (!data.isObject())
throw DeserializationException("Rooms value is not a JSON object"); throw DeserializationException("Rooms value is not a JSON object");
@ -96,17 +81,7 @@ void Rooms::deserialize(QJsonValue data) throw(DeserializationException)
} }
} }
State JoinedRoom::state() const void JoinedRoom::deserialize(const QJsonValue &data) throw(DeserializationException)
{
return state_;
}
Timeline JoinedRoom::timeline() const
{
return timeline_;
}
void JoinedRoom::deserialize(QJsonValue data) throw(DeserializationException)
{ {
if (!data.isObject()) if (!data.isObject())
throw DeserializationException("JoinedRoom is not a JSON object"); throw DeserializationException("JoinedRoom is not a JSON object");
@ -137,42 +112,7 @@ void JoinedRoom::deserialize(QJsonValue data) throw(DeserializationException)
timeline_.deserialize(object.value("timeline")); timeline_.deserialize(object.value("timeline"));
} }
QJsonObject Event::content() const void Event::deserialize(const QJsonValue &data) throw(DeserializationException)
{
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)
{ {
if (!data.isObject()) if (!data.isObject())
throw DeserializationException("Event is not a JSON object"); 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(); origin_server_ts_ = object.value("origin_server_ts").toDouble();
} }
QList<Event> State::events() const void State::deserialize(const QJsonValue &data) throw(DeserializationException)
{
return events_;
}
void State::deserialize(QJsonValue data) throw(DeserializationException)
{ {
if (!data.isArray()) if (!data.isArray())
throw DeserializationException("State is not a JSON array"); throw DeserializationException("State is not a JSON array");
@ -237,22 +172,7 @@ void State::deserialize(QJsonValue data) throw(DeserializationException)
} }
} }
QList<Event> Timeline::events() const void Timeline::deserialize(const QJsonValue &data) throw(DeserializationException)
{
return events_;
}
QString Timeline::previousBatch() const
{
return prev_batch_;
}
bool Timeline::limited() const
{
return limited_;
}
void Timeline::deserialize(QJsonValue data) throw(DeserializationException)
{ {
if (!data.isObject()) if (!data.isObject())
throw DeserializationException("Timeline is not a JSON object"); throw DeserializationException("Timeline is not a JSON object");