diff --git a/CMakeLists.txt b/CMakeLists.txt index 9ecfe2bb..dc1a28cc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -156,7 +156,6 @@ set(SRC_FILES src/RegisterPage.cc src/RoomInfoListItem.cc src/RoomList.cc - src/RoomState.cc src/RunGuard.cc src/SideBarActions.cc src/Splitter.cc diff --git a/include/Cache.h b/include/Cache.h index cc5f7065..6e54f1e9 100644 --- a/include/Cache.h +++ b/include/Cache.h @@ -23,7 +23,6 @@ #include #include -#include "RoomState.h" #include "Utils.h" struct SearchResult diff --git a/include/ChatPage.h b/include/ChatPage.h index a6789aea..b3b379cb 100644 --- a/include/ChatPage.h +++ b/include/ChatPage.h @@ -34,7 +34,6 @@ class MatrixClient; class OverlayModal; class QuickSwitcher; class RoomList; -class RoomState; class SideBarActions; class Splitter; class TextInputWidget; @@ -117,7 +116,6 @@ private: static ChatPage *instance_; using UserID = QString; - using RoomStates = std::map>; using Membership = mtx::events::StateEvent; using Memberships = std::map; @@ -175,8 +173,6 @@ private: UserInfoWidget *user_info_widget_; - RoomStates roomStates_; - std::map> communities_; // Keeps track of the users currently typing on each room. diff --git a/include/RoomList.h b/include/RoomList.h index f9cdc210..53549cb4 100644 --- a/include/RoomList.h +++ b/include/RoomList.h @@ -30,7 +30,6 @@ class LeaveRoomDialog; class MatrixClient; class OverlayModal; class RoomInfoListItem; -class RoomState; class Sync; class UserSettings; struct DescInfo; diff --git a/include/RoomSettings.h b/include/RoomSettings.h deleted file mode 100644 index d67e406a..00000000 --- a/include/RoomSettings.h +++ /dev/null @@ -1,52 +0,0 @@ -/* - * nheko Copyright (C) 2017 Konstantinos Sideris - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program 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 General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#pragma once - -#include - -class RoomSettings -{ -public: - RoomSettings(QString room_id) - { - path_ = QString("notifications/%1").arg(room_id); - isNotificationsEnabled_ = true; - - QSettings settings; - - if (settings.contains(path_)) - isNotificationsEnabled_ = settings.value(path_).toBool(); - else - settings.setValue(path_, isNotificationsEnabled_); - }; - - bool isNotificationsEnabled() { return isNotificationsEnabled_; }; - - void toggleNotifications() - { - isNotificationsEnabled_ = !isNotificationsEnabled_; - - QSettings settings; - settings.setValue(path_, isNotificationsEnabled_); - } - -private: - QString path_; - - bool isNotificationsEnabled_; -}; diff --git a/include/RoomState.h b/include/RoomState.h deleted file mode 100644 index edef2f5a..00000000 --- a/include/RoomState.h +++ /dev/null @@ -1,121 +0,0 @@ -/* - * nheko Copyright (C) 2017 Konstantinos Sideris - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program 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 General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#pragma once - -#include -#include -#include - -#include - -class RoomState -{ -public: - RoomState() = default; - RoomState(const mtx::responses::Timeline &timeline); - RoomState(const mtx::responses::State &state); - - // Calculate room data that are not immediatly accessible. Like room name and - // avatar. - // - // e.g If the room is 1-on-1 name and avatar should be extracted from a user. - void resolveName(); - void resolveAvatar(); - void parse(const nlohmann::json &object); - - QUrl getAvatar() const { return avatar_; }; - QString getName() const { return name_; }; - QString getTopic() const - { - return QString::fromStdString(topic.content.topic).simplified(); - }; - - void removeLeaveMemberships(); - void update(const RoomState &state); - - template - void updateFromEvents(const std::vector &collection); - - std::string serialize() const; - - // The latest state events. - mtx::events::StateEvent aliases; - mtx::events::StateEvent avatar; - mtx::events::StateEvent canonical_alias; - mtx::events::StateEvent create; - mtx::events::StateEvent history_visibility; - mtx::events::StateEvent join_rules; - mtx::events::StateEvent name; - mtx::events::StateEvent power_levels; - mtx::events::StateEvent topic; - - // Contains the m.room.member events for all the joined users. - using UserID = std::string; - std::map> memberships; - -private: - QUrl avatar_; - QString name_; - - // It defines the user whose avatar is used for the room. If the room has an - // avatar event this should be empty. - QString userAvatar_; -}; - -Q_DECLARE_METATYPE(RoomState) - -template -void -RoomState::updateFromEvents(const std::vector &collection) -{ - using Aliases = mtx::events::StateEvent; - using Avatar = mtx::events::StateEvent; - using CanonicalAlias = mtx::events::StateEvent; - using Create = mtx::events::StateEvent; - using HistoryVisibility = mtx::events::StateEvent; - using JoinRules = mtx::events::StateEvent; - using Member = mtx::events::StateEvent; - using Name = mtx::events::StateEvent; - using PowerLevels = mtx::events::StateEvent; - using Topic = mtx::events::StateEvent; - - for (const auto &event : collection) { - if (mpark::holds_alternative(event)) { - this->aliases = mpark::get(event); - } else if (mpark::holds_alternative(event)) { - this->avatar = mpark::get(event); - } else if (mpark::holds_alternative(event)) { - this->canonical_alias = mpark::get(event); - } else if (mpark::holds_alternative(event)) { - this->create = mpark::get(event); - } else if (mpark::holds_alternative(event)) { - this->history_visibility = mpark::get(event); - } else if (mpark::holds_alternative(event)) { - this->join_rules = mpark::get(event); - } else if (mpark::holds_alternative(event)) { - this->name = mpark::get(event); - } else if (mpark::holds_alternative(event)) { - auto membership = mpark::get(event); - memberships[membership.state_key] = membership; - } else if (mpark::holds_alternative(event)) { - this->power_levels = mpark::get(event); - } else if (mpark::holds_alternative(event)) { - this->topic = mpark::get(event); - } - } -} diff --git a/src/Cache.cc b/src/Cache.cc index 18e4d8ef..e5ab421d 100644 --- a/src/Cache.cc +++ b/src/Cache.cc @@ -26,7 +26,6 @@ #include #include "Cache.h" -#include "RoomState.h" //! Should be changed when a breaking change occurs in the cache format. //! This will reset client's data. diff --git a/src/ChatPage.cc b/src/ChatPage.cc index 673f7b08..a37cc9f0 100644 --- a/src/ChatPage.cc +++ b/src/ChatPage.cc @@ -29,7 +29,6 @@ #include "OverlayModal.h" #include "QuickSwitcher.h" #include "RoomList.h" -#include "RoomState.h" #include "SideBarActions.h" #include "Splitter.h" #include "TextInputWidget.h" diff --git a/src/RoomList.cc b/src/RoomList.cc index bb3e7505..caa4adae 100644 --- a/src/RoomList.cc +++ b/src/RoomList.cc @@ -26,7 +26,6 @@ #include "OverlayModal.h" #include "RoomInfoListItem.h" #include "RoomList.h" -#include "RoomState.h" #include "UserSettingsPage.h" RoomList::RoomList(QSharedPointer client, diff --git a/src/RoomState.cc b/src/RoomState.cc deleted file mode 100644 index de816524..00000000 --- a/src/RoomState.cc +++ /dev/null @@ -1,302 +0,0 @@ -/* - * nheko Copyright (C) 2017 Konstantinos Sideris - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program 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 General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#include -#include -#include -#include - -#include "RoomState.h" - -RoomState::RoomState(const mtx::responses::Timeline &timeline) -{ - updateFromEvents(timeline.events); -} -RoomState::RoomState(const mtx::responses::State &state) { updateFromEvents(state.events); } - -void -RoomState::resolveName() -{ - name_ = "Empty Room"; - userAvatar_.clear(); - - if (!name.content.name.empty()) { - name_ = QString::fromStdString(name.content.name).simplified(); - return; - } - - if (!canonical_alias.content.alias.empty()) { - name_ = QString::fromStdString(canonical_alias.content.alias).simplified(); - return; - } - - // FIXME: Doesn't follow the spec guidelines. - if (aliases.content.aliases.size() != 0) { - name_ = QString::fromStdString(aliases.content.aliases[0]).simplified(); - return; - } - - QSettings settings; - auto user_id = settings.value("auth/user_id").toString(); - - // TODO: Display names should be sorted alphabetically. - for (const auto membership : memberships) { - const auto stateKey = QString::fromStdString(membership.second.state_key); - - if (stateKey == user_id) { - name_ = QString::fromStdString(membership.second.content.display_name); - - if (name_.isEmpty()) - name_ = stateKey; - - userAvatar_ = stateKey; - - continue; - } - - if (membership.second.content.membership == mtx::events::state::Membership::Join || - membership.second.content.membership == - mtx::events::state::Membership::Invite) { - userAvatar_ = stateKey; - - name_ = QString::fromStdString(membership.second.content.display_name); - - if (name_.isEmpty()) - name_ = stateKey; - - // TODO: pluralization - if (memberships.size() > 2) - name_ = QString("%1 and %2 others") - .arg(name_) - .arg(memberships.size() - 2); - break; - } - } -} - -void -RoomState::resolveAvatar() -{ - if (userAvatar_.isEmpty()) { - avatar_ = QString::fromStdString(avatar.content.url); - return; - } - - if (memberships.count(userAvatar_.toStdString()) != 0) { - avatar_ = - QString::fromStdString(memberships[userAvatar_.toStdString()].content.avatar_url); - } else { - qWarning() << "Setting room avatar from unknown user id" << userAvatar_; - } -} - -// Should be used only after initial sync. -void -RoomState::removeLeaveMemberships() -{ - for (auto it = memberships.cbegin(); it != memberships.cend();) { - if (it->second.content.membership == mtx::events::state::Membership::Leave) { - it = memberships.erase(it); - } else { - ++it; - } - } -} - -void -RoomState::update(const RoomState &state) -{ - bool needsNameCalculation = false; - bool needsAvatarCalculation = false; - - if (aliases.event_id != state.aliases.event_id) - aliases = state.aliases; - - if (avatar.event_id != state.avatar.event_id) { - avatar = state.avatar; - needsAvatarCalculation = true; - } - - if (canonical_alias.event_id != state.canonical_alias.event_id) { - canonical_alias = state.canonical_alias; - needsNameCalculation = true; - } - - if (create.event_id != state.create.event_id) - create = state.create; - - if (history_visibility.event_id != state.history_visibility.event_id) - history_visibility = state.history_visibility; - - if (join_rules.event_id != state.join_rules.event_id) - join_rules = state.join_rules; - - if (name.event_id != state.name.event_id) { - name = state.name; - needsNameCalculation = true; - } - - if (power_levels.event_id != state.power_levels.event_id) - power_levels = state.power_levels; - - if (topic.event_id != state.topic.event_id) - topic = state.topic; - - for (auto it = state.memberships.cbegin(); it != state.memberships.cend(); ++it) { - auto membershipState = it->second.content.membership; - - if (it->first == userAvatar_.toStdString()) { - needsNameCalculation = true; - needsAvatarCalculation = true; - } - - if (membershipState == mtx::events::state::Membership::Leave) - this->memberships.erase(it->first); - else - this->memberships.emplace(it->first, it->second); - } - - if (needsNameCalculation) - resolveName(); - - if (needsAvatarCalculation) - resolveAvatar(); -} - -std::string -RoomState::serialize() const -{ - nlohmann::json obj; - - if (!aliases.event_id.empty()) - obj["aliases"] = aliases; - - if (!avatar.event_id.empty()) - obj["avatar"] = avatar; - - if (!canonical_alias.event_id.empty()) - obj["canonical_alias"] = canonical_alias; - - if (!create.event_id.empty()) - obj["create"] = create; - - if (!history_visibility.event_id.empty()) - obj["history_visibility"] = history_visibility; - - if (!join_rules.event_id.empty()) - obj["join_rules"] = join_rules; - - if (!name.event_id.empty()) - obj["name"] = name; - - if (!power_levels.event_id.empty()) - obj["power_levels"] = power_levels; - - if (!topic.event_id.empty()) - obj["topic"] = topic; - - return obj.dump(); -} - -void -RoomState::parse(const nlohmann::json &object) -{ - if (object.count("aliases") != 0) { - try { - aliases = object.at("aliases") - .get>(); - } catch (std::exception &e) { - qWarning() << "RoomState::parse - aliases" << e.what(); - } - } - - if (object.count("avatar") != 0) { - try { - avatar = object.at("avatar") - .get>(); - } catch (std::exception &e) { - qWarning() << "RoomState::parse - avatar" << e.what(); - } - } - - if (object.count("canonical_alias") != 0) { - try { - canonical_alias = - object.at("canonical_alias") - .get>(); - } catch (std::exception &e) { - qWarning() << "RoomState::parse - canonical_alias" << e.what(); - } - } - - if (object.count("create") != 0) { - try { - create = object.at("create") - .get>(); - } catch (std::exception &e) { - qWarning() << "RoomState::parse - create" << e.what(); - } - } - - if (object.count("history_visibility") != 0) { - try { - history_visibility = - object.at("history_visibility") - .get>(); - } catch (std::exception &e) { - qWarning() << "RoomState::parse - history_visibility" << e.what(); - } - } - - if (object.count("join_rules") != 0) { - try { - join_rules = - object.at("join_rules") - .get>(); - } catch (std::exception &e) { - qWarning() << "RoomState::parse - join_rules" << e.what(); - } - } - - if (object.count("name") != 0) { - try { - name = object.at("name") - .get>(); - } catch (std::exception &e) { - qWarning() << "RoomState::parse - name" << e.what(); - } - } - - if (object.count("power_levels") != 0) { - try { - power_levels = - object.at("power_levels") - .get>(); - } catch (std::exception &e) { - qWarning() << "RoomState::parse - power_levels" << e.what(); - } - } - - if (object.count("topic") != 0) { - try { - topic = object.at("topic") - .get>(); - } catch (std::exception &e) { - qWarning() << "RoomState::parse - topic" << e.what(); - } - } -} diff --git a/src/TopRoomBar.cc b/src/TopRoomBar.cc index 6caf2edb..3b60ab6a 100644 --- a/src/TopRoomBar.cc +++ b/src/TopRoomBar.cc @@ -25,7 +25,6 @@ #include "MainWindow.h" #include "Menu.h" #include "OverlayModal.h" -#include "RoomSettings.h" #include "TopRoomBar.h" #include "Utils.h"