/* This file is part of Telegram Desktop, the official desktop application for the Telegram messaging service. For license and copyright information please follow this link: https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL */ #include "boxes/edit_privacy_box.h" #include "ui/widgets/checkbox.h" #include "ui/widgets/labels.h" #include "ui/widgets/buttons.h" #include "ui/wrap/slide_wrap.h" #include "ui/wrap/vertical_layout.h" #include "history/history.h" #include "boxes/peer_list_controllers.h" #include "settings/settings_common.h" #include "settings/settings_privacy_security.h" #include "calls/calls_instance.h" #include "base/binary_guard.h" #include "lang/lang_keys.h" #include "apiwrap.h" #include "main/main_session.h" #include "data/data_user.h" #include "data/data_chat.h" #include "data/data_channel.h" #include "window/window_session_controller.h" #include "styles/style_settings.h" #include "styles/style_layers.h" namespace { class PrivacyExceptionsBoxController : public ChatsListBoxController { public: PrivacyExceptionsBoxController( not_null navigation, rpl::producer title, const std::vector> &selected); Main::Session &session() const override; void rowClicked(not_null row) override; std::vector> getResult() const; protected: void prepareViewHook() override; std::unique_ptr createRow(not_null history) override; private: not_null _navigation; rpl::producer _title; std::vector> _selected; }; PrivacyExceptionsBoxController::PrivacyExceptionsBoxController( not_null navigation, rpl::producer title, const std::vector> &selected) : ChatsListBoxController(navigation) , _navigation(navigation) , _title(std::move(title)) , _selected(selected) { } Main::Session &PrivacyExceptionsBoxController::session() const { return _navigation->session(); } void PrivacyExceptionsBoxController::prepareViewHook() { delegate()->peerListSetTitle(std::move(_title)); delegate()->peerListAddSelectedPeers(_selected); } std::vector> PrivacyExceptionsBoxController::getResult() const { return delegate()->peerListCollectSelectedRows(); } void PrivacyExceptionsBoxController::rowClicked(not_null row) { delegate()->peerListSetRowChecked(row, !row->checked()); if (const auto channel = row->peer()->asChannel()) { if (!channel->membersCountKnown()) { channel->updateFull(); } } } std::unique_ptr PrivacyExceptionsBoxController::createRow(not_null history) { if (history->peer->isSelf() || history->peer->isRepliesChat()) { return nullptr; } else if (!history->peer->isUser() && !history->peer->isChat() && !history->peer->isMegagroup()) { return nullptr; } auto result = std::make_unique(history); const auto count = [&] { if (const auto chat = history->peer->asChat()) { return chat->count; } else if (const auto channel = history->peer->asChannel()) { return channel->membersCountKnown() ? channel->membersCount() : 0; } return 0; }(); if (count > 0) { result->setCustomStatus( tr::lng_chat_status_members(tr::now, lt_count_decimal, count)); } return result; } } // namespace QString EditPrivacyController::optionLabel(Option option) { switch (option) { case Option::Everyone: return tr::lng_edit_privacy_everyone(tr::now); case Option::Contacts: return tr::lng_edit_privacy_contacts(tr::now); case Option::Nobody: return tr::lng_edit_privacy_nobody(tr::now); } Unexpected("Option value in optionsLabelKey."); } EditPrivacyBox::EditPrivacyBox( QWidget*, not_null window, std::unique_ptr controller, const Value &value) : _window(window) , _controller(std::move(controller)) , _value(value) { } void EditPrivacyBox::prepare() { _controller->setView(this); setupContent(); } void EditPrivacyBox::editExceptions( Exception exception, Fn done) { auto controller = std::make_unique( _window, _controller->exceptionBoxTitle(exception), exceptions(exception)); auto initBox = [=, controller = controller.get()]( not_null box) { box->addButton(tr::lng_settings_save(), crl::guard(this, [=] { exceptions(exception) = controller->getResult(); const auto type = [&] { switch (exception) { case Exception::Always: return Exception::Never; case Exception::Never: return Exception::Always; } Unexpected("Invalid exception value."); }(); auto &removeFrom = exceptions(type); for (const auto peer : exceptions(exception)) { removeFrom.erase( ranges::remove(removeFrom, peer), end(removeFrom)); } done(); box->closeBox(); })); box->addButton(tr::lng_cancel(), [=] { box->closeBox(); }); }; Ui::show( Box(std::move(controller), std::move(initBox)), Ui::LayerOption::KeepOther); } QVector EditPrivacyBox::collectResult() { const auto collectInputUsers = [](const auto &peers) { auto result = QVector(); result.reserve(peers.size()); for (const auto peer : peers) { if (const auto user = peer->asUser()) { result.push_back(user->inputUser); } } return result; }; const auto collectInputChats = [](const auto &peers) { auto result = QVector(); result.reserve(peers.size()); for (const auto peer : peers) { if (!peer->isUser()) { result.push_back(MTP_int(peer->bareId())); } } return result; }; constexpr auto kMaxRules = 3; // allow users, disallow users, option auto result = QVector(); result.reserve(kMaxRules); if (showExceptionLink(Exception::Always)) { const auto users = collectInputUsers(_value.always); const auto chats = collectInputChats(_value.always); if (!users.empty()) { result.push_back(MTP_inputPrivacyValueAllowUsers(MTP_vector(users))); } if (!chats.empty()) { result.push_back(MTP_inputPrivacyValueAllowChatParticipants(MTP_vector(chats))); } } if (showExceptionLink(Exception::Never)) { const auto users = collectInputUsers(_value.never); const auto chats = collectInputChats(_value.never); if (!users.empty()) { result.push_back(MTP_inputPrivacyValueDisallowUsers(MTP_vector(users))); } if (!chats.empty()) { result.push_back(MTP_inputPrivacyValueDisallowChatParticipants(MTP_vector(chats))); } } result.push_back([&] { switch (_value.option) { case Option::Everyone: return MTP_inputPrivacyValueAllowAll(); case Option::Contacts: return MTP_inputPrivacyValueAllowContacts(); case Option::Nobody: return MTP_inputPrivacyValueDisallowAll(); } Unexpected("Option value in EditPrivacyBox::collectResult."); }()); return result; } std::vector> &EditPrivacyBox::exceptions(Exception exception) { switch (exception) { case Exception::Always: return _value.always; case Exception::Never: return _value.never; } Unexpected("Invalid exception value."); } bool EditPrivacyBox::showExceptionLink(Exception exception) const { switch (exception) { case Exception::Always: return (_value.option == Option::Contacts) || (_value.option == Option::Nobody); case Exception::Never: return (_value.option == Option::Everyone) || (_value.option == Option::Contacts); } Unexpected("Invalid exception value."); } Ui::Radioenum *EditPrivacyBox::AddOption( not_null container, not_null controller, const std::shared_ptr> &group, Option option) { return container->add( object_ptr>( container, group, option, controller->optionLabel(option), st::settingsSendType), st::settingsSendTypePadding); } Ui::FlatLabel *EditPrivacyBox::addLabel( not_null container, rpl::producer text) { const auto wrap = container->add( object_ptr>( container, object_ptr( container, rpl::duplicate(text), st::boxDividerLabel), st::settingsPrivacyEditLabelPadding)); wrap->hide(anim::type::instant); wrap->toggleOn(std::move( text ) | rpl::map([](const QString &text) { return !text.isEmpty(); })); return wrap->entity(); } void EditPrivacyBox::setupContent() { using namespace Settings; setTitle(_controller->title()); auto wrap = object_ptr(this); const auto content = wrap.data(); setInnerWidget(object_ptr( this, std::move(wrap))); const auto group = std::make_shared>( _value.option); const auto toggle = Ui::CreateChild>(content); group->setChangedCallback([=](Option value) { _value.option = value; toggle->fire_copy(value); }); auto optionValue = toggle->events_starting_with_copy(_value.option); const auto addOptionRow = [&](Option option) { return (_controller->hasOption(option) || (_value.option == option)) ? AddOption(content, _controller.get(), group, option) : nullptr; }; const auto addExceptionLink = [=](Exception exception) { const auto update = Ui::CreateChild>(content); auto label = update->events_starting_with( rpl::empty_value() ) | rpl::map([=] { return Settings::ExceptionUsersCount(exceptions(exception)); }) | rpl::map([](int count) { return count ? tr::lng_edit_privacy_exceptions_count(tr::now, lt_count, count) : tr::lng_edit_privacy_exceptions_add(tr::now); }); auto text = _controller->exceptionButtonTextKey(exception); const auto button = content->add( object_ptr>( content, object_ptr