From a563cf553c267fbf35874ff4a3ba2f90e7470ee8 Mon Sep 17 00:00:00 2001 From: John Preston Date: Thu, 16 Mar 2017 18:14:01 +0300 Subject: [PATCH] Rename / move settings_blocked_box_controller. Next commit fixes the build. --- ...r.cpp => settings_privacy_controllers.cpp} | 278 ++++++++++-------- ...oller.h => settings_privacy_controllers.h} | 29 +- 2 files changed, 154 insertions(+), 153 deletions(-) rename Telegram/SourceFiles/settings/{settings_blocked_box_controller.cpp => settings_privacy_controllers.cpp} (89%) rename Telegram/SourceFiles/settings/{settings_blocked_box_controller.h => settings_privacy_controllers.h} (69%) diff --git a/Telegram/SourceFiles/settings/settings_blocked_box_controller.cpp b/Telegram/SourceFiles/settings/settings_privacy_controllers.cpp similarity index 89% rename from Telegram/SourceFiles/settings/settings_blocked_box_controller.cpp rename to Telegram/SourceFiles/settings/settings_privacy_controllers.cpp index 73b2e13dc..1d11c4439 100644 --- a/Telegram/SourceFiles/settings/settings_blocked_box_controller.cpp +++ b/Telegram/SourceFiles/settings/settings_privacy_controllers.cpp @@ -18,7 +18,7 @@ to link the code of portions of this program with the OpenSSL library. Full license: https://github.com/telegramdesktop/tdesktop/blob/master/LICENSE Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org */ -#include "settings/settings_blocked_box_controller.h" +#include "settings/settings_privacy_controllers.h" #include "lang.h" #include "apiwrap.h" @@ -30,138 +30,35 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org namespace Settings { namespace { -constexpr auto kPerPage = 40; +constexpr auto kBlockedPerPage = 40; -} // namespace +class BlockUserBoxController : public PeerListBox::Controller, private base::Subscriber { +public: + void prepare() override; + void rowClicked(PeerData *peer) override; + std::unique_ptr createGlobalRow(PeerData *peer) override; -void BlockedBoxController::prepare() { - view()->setTitle(lang(lng_blocked_list_title)); - view()->addButton(lang(lng_close), [this] { view()->closeBox(); }); - view()->addLeftButton(lang(lng_blocked_list_add), [this] { blockUser(); }); - view()->setAboutText(lang(lng_contacts_loading)); - view()->refreshRows(); +private: + void rebuildRows(); + void checkForEmptyRows(); + void updateIsBlocked(PeerListBox::Row *row, UserData *user) const; + bool appendRow(History *history); - subscribe(Notify::PeerUpdated(), Notify::PeerUpdatedHandler(Notify::PeerUpdate::Flag::UserIsBlocked, [this](const Notify::PeerUpdate &update) { - if (auto user = update.peer->asUser()) { - handleBlockedEvent(user); + class Row : public PeerListBox::Row { + public: + Row(History *history) : PeerListBox::Row(history->peer), _history(history) { } - })); - - preloadRows(); -} - -void BlockedBoxController::preloadRows() { - if (_loadRequestId || _allLoaded) { - return; - } - - _loadRequestId = MTP::send(MTPcontacts_GetBlocked(MTP_int(_offset), MTP_int(kPerPage)), rpcDone(base::lambda_guarded(this, [this](const MTPcontacts_Blocked &result) { - _loadRequestId = 0; - - if (!_offset) { - view()->setAboutText(lang(lng_blocked_list_about)); + History *history() const { + return _history; } - auto handleContactsBlocked = [](auto &list) { - App::feedUsers(list.vusers); - return list.vblocked.v; - }; - switch (result.type()) { - case mtpc_contacts_blockedSlice: { - receivedUsers(handleContactsBlocked(result.c_contacts_blockedSlice())); - } break; - case mtpc_contacts_blocked: { - _allLoaded = true; - receivedUsers(handleContactsBlocked(result.c_contacts_blocked())); - } break; - default: t_assert(!"Bad type() in MTPcontacts_GetBlocked() result."); - } - })), rpcFail(base::lambda_guarded(this, [this](const RPCError &error) { - if (MTP::isDefaultHandledError(error)) { - return false; - } - _loadRequestId = 0; - return true; - }))); -} + private: + History *_history = nullptr; -void BlockedBoxController::rowClicked(PeerData *peer) { - Ui::showPeerHistoryAsync(peer->id, ShowAtUnreadMsgId); -} - -void BlockedBoxController::rowActionClicked(PeerData *peer) { - auto user = peer->asUser(); - t_assert(user != nullptr); - - App::api()->unblockUser(user); -} - -void BlockedBoxController::receivedUsers(const QVector &result) { - if (result.empty()) { - _allLoaded = true; - } - - for_const (auto &item, result) { - ++_offset; - if (item.type() != mtpc_contactBlocked) { - continue; - } - auto &contactBlocked = item.c_contactBlocked(); - auto userId = contactBlocked.vuser_id.v; - if (auto user = App::userLoaded(userId)) { - appendRow(user); - user->setBlockStatus(UserData::BlockStatus::Blocked); - } - } - view()->refreshRows(); -} - -void BlockedBoxController::handleBlockedEvent(UserData *user) { - if (user->isBlocked()) { - if (prependRow(user)) { - view()->refreshRows(); - view()->onScrollToY(0); - } - } else if (auto row = view()->findRow(user)) { - view()->removeRow(row); - view()->refreshRows(); - } -} - -void BlockedBoxController::blockUser() { - Ui::show(Box(std::make_unique()), KeepOtherLayers); -} - -bool BlockedBoxController::appendRow(UserData *user) { - if (view()->findRow(user)) { - return false; - } - view()->appendRow(createRow(user)); - return true; -} - -bool BlockedBoxController::prependRow(UserData *user) { - if (view()->findRow(user)) { - return false; - } - view()->prependRow(createRow(user)); - return true; -} - -std::unique_ptr BlockedBoxController::createRow(UserData *user) const { - auto row = std::make_unique(user); - row->setActionLink(lang(lng_blocked_list_unblock)); - auto status = [user]() -> QString { - if (user->botInfo) { - return lang(lng_status_bot); - } else if (user->phone().isEmpty()) { - return lang(lng_blocked_list_unknown_phone); - } - return App::formatPhone(user->phone()); }; - row->setCustomStatus(status()); - return row; -} + std::unique_ptr createRow(History *history) const; + +}; void BlockUserBoxController::prepare() { view()->setTitle(lang(lng_blocked_list_add_title)); @@ -271,4 +168,135 @@ std::unique_ptr BlockUserBoxController::createRow(H return row; } +} // namespace + +void BlockedBoxController::prepare() { + view()->setTitle(lang(lng_blocked_list_title)); + view()->addButton(lang(lng_close), [this] { view()->closeBox(); }); + view()->addLeftButton(lang(lng_blocked_list_add), [this] { blockUser(); }); + view()->setAboutText(lang(lng_contacts_loading)); + view()->refreshRows(); + + subscribe(Notify::PeerUpdated(), Notify::PeerUpdatedHandler(Notify::PeerUpdate::Flag::UserIsBlocked, [this](const Notify::PeerUpdate &update) { + if (auto user = update.peer->asUser()) { + handleBlockedEvent(user); + } + })); + + preloadRows(); +} + +void BlockedBoxController::preloadRows() { + if (_loadRequestId || _allLoaded) { + return; + } + + _loadRequestId = MTP::send(MTPcontacts_GetBlocked(MTP_int(_offset), MTP_int(kBlockedPerPage)), rpcDone(base::lambda_guarded(this, [this](const MTPcontacts_Blocked &result) { + _loadRequestId = 0; + + if (!_offset) { + view()->setAboutText(lang(lng_blocked_list_about)); + } + + auto handleContactsBlocked = [](auto &list) { + App::feedUsers(list.vusers); + return list.vblocked.v; + }; + switch (result.type()) { + case mtpc_contacts_blockedSlice: { + receivedUsers(handleContactsBlocked(result.c_contacts_blockedSlice())); + } break; + case mtpc_contacts_blocked: { + _allLoaded = true; + receivedUsers(handleContactsBlocked(result.c_contacts_blocked())); + } break; + default: t_assert(!"Bad type() in MTPcontacts_GetBlocked() result."); + } + })), rpcFail(base::lambda_guarded(this, [this](const RPCError &error) { + if (MTP::isDefaultHandledError(error)) { + return false; + } + _loadRequestId = 0; + return true; + }))); +} + +void BlockedBoxController::rowClicked(PeerData *peer) { + Ui::showPeerHistoryAsync(peer->id, ShowAtUnreadMsgId); +} + +void BlockedBoxController::rowActionClicked(PeerData *peer) { + auto user = peer->asUser(); + t_assert(user != nullptr); + + App::api()->unblockUser(user); +} + +void BlockedBoxController::receivedUsers(const QVector &result) { + if (result.empty()) { + _allLoaded = true; + } + + for_const (auto &item, result) { + ++_offset; + if (item.type() != mtpc_contactBlocked) { + continue; + } + auto &contactBlocked = item.c_contactBlocked(); + auto userId = contactBlocked.vuser_id.v; + if (auto user = App::userLoaded(userId)) { + appendRow(user); + user->setBlockStatus(UserData::BlockStatus::Blocked); + } + } + view()->refreshRows(); +} + +void BlockedBoxController::handleBlockedEvent(UserData *user) { + if (user->isBlocked()) { + if (prependRow(user)) { + view()->refreshRows(); + view()->onScrollToY(0); + } + } else if (auto row = view()->findRow(user)) { + view()->removeRow(row); + view()->refreshRows(); + } +} + +void BlockedBoxController::blockUser() { + Ui::show(Box(std::make_unique()), KeepOtherLayers); +} + +bool BlockedBoxController::appendRow(UserData *user) { + if (view()->findRow(user)) { + return false; + } + view()->appendRow(createRow(user)); + return true; +} + +bool BlockedBoxController::prependRow(UserData *user) { + if (view()->findRow(user)) { + return false; + } + view()->prependRow(createRow(user)); + return true; +} + +std::unique_ptr BlockedBoxController::createRow(UserData *user) const { + auto row = std::make_unique(user); + row->setActionLink(lang(lng_blocked_list_unblock)); + auto status = [user]() -> QString { + if (user->botInfo) { + return lang(lng_status_bot); + } else if (user->phone().isEmpty()) { + return lang(lng_blocked_list_unknown_phone); + } + return App::formatPhone(user->phone()); + }; + row->setCustomStatus(status()); + return row; +} + } // namespace Settings diff --git a/Telegram/SourceFiles/settings/settings_blocked_box_controller.h b/Telegram/SourceFiles/settings/settings_privacy_controllers.h similarity index 69% rename from Telegram/SourceFiles/settings/settings_blocked_box_controller.h rename to Telegram/SourceFiles/settings/settings_privacy_controllers.h index a30c732d8..3486b1052 100644 --- a/Telegram/SourceFiles/settings/settings_blocked_box_controller.h +++ b/Telegram/SourceFiles/settings/settings_privacy_controllers.h @@ -21,6 +21,7 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org #pragma once #include "boxes/peer_list_box.h" +#include "boxes/edit_privacy_box.h" namespace Settings { @@ -46,32 +47,4 @@ private: }; -class BlockUserBoxController : public QObject, public PeerListBox::Controller, private base::Subscriber { -public: - void prepare() override; - void rowClicked(PeerData *peer) override; - std::unique_ptr createGlobalRow(PeerData *peer) override; - -private: - void rebuildRows(); - void checkForEmptyRows(); - void updateIsBlocked(PeerListBox::Row *row, UserData *user) const; - bool appendRow(History *history); - - class Row : public PeerListBox::Row { - public: - Row(History *history) : PeerListBox::Row(history->peer), _history(history) { - } - History *history() const { - return _history; - } - - private: - History *_history = nullptr; - - }; - std::unique_ptr createRow(History *history) const; - -}; - } // namespace Settings