From 73e73f46eaff4f2236b31ed5858738c6b5e26ea3 Mon Sep 17 00:00:00 2001 From: Konstantinos Sideris Date: Thu, 6 Apr 2017 19:56:33 +0300 Subject: [PATCH] Use const refs for the deserialized data --- CMakeLists.txt | 2 +- Makefile | 10 +-- README.md | 9 ++- include/ChatPage.h | 8 +-- include/Deserializable.h | 6 +- include/HistoryView.h | 4 +- include/HistoryViewItem.h | 2 +- include/Login.h | 37 +++++++++-- include/MatrixClient.h | 23 ++++--- include/Profile.h | 16 ++++- include/RoomInfoListItem.h | 2 +- include/Sync.h | 124 ++++++++++++++++++++++++++++++------- src/ChatPage.cc | 8 +-- src/HistoryView.cc | 4 +- src/HistoryViewItem.cc | 2 +- src/Login.cc | 27 +------- src/MatrixClient.cc | 6 +- src/Profile.cc | 12 +--- src/Sync.cc | 92 ++------------------------- 19 files changed, 198 insertions(+), 196 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3880bb92..217f1c17 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/Makefile b/Makefile index fc3cb5cf..7f97e934 100644 --- a/Makefile +++ b/Makefile @@ -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: diff --git a/README.md b/README.md index f0e01f14..42b33dc9 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/include/ChatPage.h b/include/ChatPage.h index c3fa6bf6..165d7808 100644 --- a/include/ChatPage.h +++ b/include/ChatPage.h @@ -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_; diff --git a/include/Deserializable.h b/include/Deserializable.h index 6a9b4cd5..f51abc73 100644 --- a/include/Deserializable.h +++ b/include/Deserializable.h @@ -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) { } }; diff --git a/include/HistoryView.h b/include/HistoryView.h index 9266d6ac..284b6a8c 100644 --- a/include/HistoryView.h +++ b/include/HistoryView.h @@ -33,10 +33,10 @@ class HistoryView : public QWidget public: explicit HistoryView(QWidget *parent = 0); - explicit HistoryView(QList events, QWidget *parent = 0); + explicit HistoryView(const QList &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 &events); public slots: diff --git a/include/HistoryViewItem.h b/include/HistoryViewItem.h index b817194b..2d93e451 100644 --- a/include/HistoryViewItem.h +++ b/include/HistoryViewItem.h @@ -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: diff --git a/include/Login.h b/include/Login.h index 857be14b..5ea8bc68 100644 --- a/include/Login.h +++ b/include/Login.h @@ -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 diff --git a/include/MatrixClient.h b/include/MatrixClient.h index 46d6cc5b..cf6a94e5 100644 --- a/include/MatrixClient.h +++ b/include/MatrixClient.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; } diff --git a/include/Profile.h b/include/Profile.h index a36393ec..acf81fda 100644 --- a/include/Profile.h +++ b/include/Profile.h @@ -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 diff --git a/include/RoomInfoListItem.h b/include/RoomInfoListItem.h index 4eabfa23..8e19abc1 100644 --- a/include/RoomInfoListItem.h +++ b/include/RoomInfoListItem.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); diff --git a/include/Sync.h b/include/Sync.h index 110e8a6e..699c3ee8 100644 --- a/include/Sync.h +++ b/include/Sync.h @@ -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 events() const; + void deserialize(const QJsonValue &data) throw(DeserializationException) override; + inline QList events() const; private: QList events_; }; +inline QList State::events() const +{ + return events_; +} + class Timeline : public Deserializable { public: - QList events() const; - QString previousBatch() const; - bool limited() const; + inline QList 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 events_; @@ -76,14 +116,29 @@ private: bool limited_; }; +inline QList 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 join() const; - void deserialize(QJsonValue data) throw(DeserializationException) override; + inline QMap join() const; + void deserialize(const QJsonValue &data) throw(DeserializationException) override; private: QMap join_; }; +inline QMap 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 diff --git a/src/ChatPage.cc b/src/ChatPage.cc index 732e3651..b5707cb9 100644 --- a/src/ChatPage.cc +++ b/src/ChatPage.cc @@ -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(); diff --git a/src/HistoryView.cc b/src/HistoryView.cc index 0949d17c..3775db9a 100644 --- a/src/HistoryView.cc +++ b/src/HistoryView.cc @@ -50,7 +50,7 @@ const QList HistoryView::COLORS({"#FFF46E", "#a2b636", "#4BBE2E"}); -HistoryView::HistoryView(QList events, QWidget *parent) +HistoryView::HistoryView(const QList &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_); diff --git a/src/HistoryViewItem.cc b/src/HistoryViewItem.cc index 04c42f45..84890185 100644 --- a/src/HistoryViewItem.cc +++ b/src/HistoryViewItem.cc @@ -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 = ""; diff --git a/src/Login.cc b/src/Login.cc index f3b8e2f4..0ce05a80 100644 --- a/src/Login.cc +++ b/src/Login.cc @@ -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"); diff --git a/src/MatrixClient.cc b/src/MatrixClient.cc index 5510b6d9..725aede6 100644 --- a/src/MatrixClient.cc +++ b/src/MatrixClient.cc @@ -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_); diff --git a/src/Profile.cc b/src/Profile.cc index aa556370..2322834a 100644 --- a/src/Profile.cc +++ b/src/Profile.cc @@ -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"); diff --git a/src/Sync.cc b/src/Sync.cc index 3ba6d220..b51ecff7 100644 --- a/src/Sync.cc +++ b/src/Sync.cc @@ -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 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 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 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");