/* 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 "api/api_global_privacy.h" #include "boxes/filters/edit_filter_chats_list.h" #include "ui/effects/premium_graphics.h" #include "ui/layers/generic_box.h" #include "ui/widgets/checkbox.h" #include "ui/widgets/shadow.h" #include "ui/text/text_utilities.h" #include "ui/toast/toast.h" #include "ui/wrap/slide_wrap.h" #include "ui/painter.h" #include "ui/vertical_list.h" #include "history/history.h" #include "boxes/peer_list_controllers.h" #include "settings/settings_premium.h" #include "settings/settings_privacy_security.h" #include "calls/calls_instance.h" #include "lang/lang_keys.h" #include "apiwrap.h" #include "main/main_app_config.h" #include "main/main_session.h" #include "data/data_user.h" #include "data/data_chat.h" #include "data/data_channel.h" #include "data/data_peer_values.h" #include "window/window_session_controller.h" #include "styles/style_boxes.h" #include "styles/style_settings.h" #include "styles/style_layers.h" #include "styles/style_menu_icons.h" namespace { constexpr auto kPremiumsRowId = PeerId(FakeChatId(BareId(1))).value; using Exceptions = Api::UserPrivacy::Exceptions; void CreateRadiobuttonLock( not_null widget, const style::Checkbox &st) { const auto lock = Ui::CreateChild(widget.get()); lock->setAttribute(Qt::WA_TransparentForMouseEvents); lock->resize(st::defaultRadio.diameter, st::defaultRadio.diameter); widget->sizeValue( ) | rpl::start_with_next([=, &st](QSize size) { lock->move(st.checkPosition); }, lock->lifetime()); lock->paintRequest() | rpl::start_with_next([=] { auto p = QPainter(lock); auto hq = PainterHighQualityEnabler(p); const auto &icon = st::messagePrivacyLock; const auto size = st::defaultRadio.diameter; const auto image = icon.instance(st::checkboxFg->c); p.drawImage(QRectF( (size - icon.width()) / 2., (size - icon.height()) / 2., icon.width(), icon.height()), image); }, lock->lifetime()); } void AddPremiumRequiredRow( not_null widget, not_null session, Fn clickedCallback, Fn setDefaultOption, const style::Checkbox &st) { const auto row = Ui::CreateChild(widget.get()); widget->sizeValue( ) | rpl::start_with_next([=](const QSize &s) { row->resize(s); }, row->lifetime()); row->setClickedCallback(std::move(clickedCallback)); CreateRadiobuttonLock(row, st); Data::AmPremiumValue( session ) | rpl::start_with_next([=](bool premium) { row->setVisible(!premium); if (!premium) { setDefaultOption(); } }, row->lifetime()); } class PrivacyExceptionsBoxController : public ChatsListBoxController { public: PrivacyExceptionsBoxController( not_null session, rpl::producer title, const Exceptions &selected, bool allowChoosePremiums); Main::Session &session() const override; void rowClicked(not_null row) override; bool isForeignRow(PeerListRowId itemId) override; bool handleDeselectForeignRow(PeerListRowId itemId) override; [[nodiscard]] bool premiumsSelected() const; protected: void prepareViewHook() override; std::unique_ptr createRow(not_null history) override; private: [[nodiscard]] object_ptr preparePremiumsRowList(); const not_null _session; rpl::producer _title; Exceptions _selected; bool _allowChoosePremiums = false; PeerListContentDelegate *_typesDelegate = nullptr; Fn _deselectOption; }; struct RowSelectionChange { not_null row; bool checked = false; }; class PremiumsRow final : public PeerListRow { public: PremiumsRow(); QString generateName() override; QString generateShortName() override; PaintRoundImageCallback generatePaintUserpicCallback( bool forceRound) override; bool useForumLikeUserpic() const override; }; class TypesController final : public PeerListController { public: TypesController(not_null session, bool premiums); Main::Session &session() const override; void prepare() override; void rowClicked(not_null row) override; [[nodiscard]] bool premiumsSelected() const; [[nodiscard]] rpl::producer premiumsChanges() const; [[nodiscard]] auto rowSelectionChanges() const -> rpl::producer; private: const not_null _session; rpl::event_stream<> _selectionChanged; rpl::event_stream _rowSelectionChanges; }; PremiumsRow::PremiumsRow() : PeerListRow(kPremiumsRowId) { setCustomStatus(tr::lng_edit_privacy_premium_status(tr::now)); } QString PremiumsRow::generateName() { return tr::lng_edit_privacy_premium(tr::now); } QString PremiumsRow::generateShortName() { return generateName(); } PaintRoundImageCallback PremiumsRow::generatePaintUserpicCallback( bool forceRound) { return [=](QPainter &p, int x, int y, int outerWidth, int size) { auto gradient = QLinearGradient( QPointF(x, y), QPointF(x + size, y + size)); gradient.setStops(Ui::Premium::ButtonGradientStops()); auto hq = PainterHighQualityEnabler(p); p.setPen(Qt::NoPen); p.setBrush(gradient); if (forceRound) { p.drawEllipse(x, y, size, size); } else { const auto radius = size * Ui::ForumUserpicRadiusMultiplier(); p.drawRoundedRect(x, y, size, size, radius, radius); } st::settingsPrivacyPremium.paintInCenter(p, { x, y, size, size }); }; } bool PremiumsRow::useForumLikeUserpic() const { return true; } TypesController::TypesController( not_null session, bool premiums) : _session(session) { } Main::Session &TypesController::session() const { return *_session; } void TypesController::prepare() { delegate()->peerListAppendRow(std::make_unique()); delegate()->peerListRefreshRows(); } bool TypesController::premiumsSelected() const { const auto row = delegate()->peerListFindRow(kPremiumsRowId); Assert(row != nullptr); return row->checked(); } void TypesController::rowClicked(not_null row) { const auto checked = !row->checked(); delegate()->peerListSetRowChecked(row, checked); _rowSelectionChanges.fire({ row, checked }); } rpl::producer TypesController::premiumsChanges() const { return _rowSelectionChanges.events( ) | rpl::map([=] { return premiumsSelected(); }); } auto TypesController::rowSelectionChanges() const -> rpl::producer { return _rowSelectionChanges.events(); } PrivacyExceptionsBoxController::PrivacyExceptionsBoxController( not_null session, rpl::producer title, const Exceptions &selected, bool allowChoosePremiums) : ChatsListBoxController(session) , _session(session) , _title(std::move(title)) , _selected(selected) , _allowChoosePremiums(allowChoosePremiums) { } Main::Session &PrivacyExceptionsBoxController::session() const { return *_session; } void PrivacyExceptionsBoxController::prepareViewHook() { delegate()->peerListSetTitle(std::move(_title)); if (_allowChoosePremiums || _selected.premiums) { delegate()->peerListSetAboveWidget(preparePremiumsRowList()); } delegate()->peerListAddSelectedPeers(_selected.peers); } bool PrivacyExceptionsBoxController::isForeignRow(PeerListRowId itemId) { return (itemId == kPremiumsRowId); } bool PrivacyExceptionsBoxController::handleDeselectForeignRow( PeerListRowId itemId) { if (isForeignRow(itemId)) { _deselectOption(itemId); return true; } return false; } auto PrivacyExceptionsBoxController::preparePremiumsRowList() -> object_ptr { auto result = object_ptr((QWidget*)nullptr); const auto container = result.data(); container->add(CreatePeerListSectionSubtitle( container, tr::lng_edit_privacy_user_types())); auto &lifetime = container->lifetime(); _typesDelegate = lifetime.make_state(); const auto controller = lifetime.make_state( &session(), _selected.premiums); const auto content = result->add(object_ptr( container, controller)); _typesDelegate->setContent(content); controller->setDelegate(_typesDelegate); if (_selected.premiums) { const auto row = _typesDelegate->peerListFindRow(kPremiumsRowId); Assert(row != nullptr); content->changeCheckState(row, true, anim::type::instant); this->delegate()->peerListSetForeignRowChecked( row, true, anim::type::instant); } container->add(CreatePeerListSectionSubtitle( container, tr::lng_edit_privacy_users_and_groups())); controller->premiumsChanges( ) | rpl::start_with_next([=](bool premiums) { _selected.premiums = premiums; }, lifetime); controller->rowSelectionChanges( ) | rpl::start_with_next([=](RowSelectionChange update) { this->delegate()->peerListSetForeignRowChecked( update.row, update.checked, anim::type::normal); }, lifetime); _deselectOption = [=](PeerListRowId itemId) { if (const auto row = _typesDelegate->peerListFindRow(itemId)) { if (itemId == kPremiumsRowId) { _selected.premiums = false; } _typesDelegate->peerListSetRowChecked(row, false); } }; return result; } [[nodiscard]] bool PrivacyExceptionsBoxController::premiumsSelected() const { return _selected.premiums; } void PrivacyExceptionsBoxController::rowClicked(not_null row) { const auto peer = row->peer(); // This call may delete row, if it was a search result row. delegate()->peerListSetRowChecked(row, !row->checked()); if (const auto channel = peer->asChannel()) { if (!channel->membersCountKnown()) { channel->updateFull(); } } } auto PrivacyExceptionsBoxController::createRow(not_null history) -> std::unique_ptr { 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 bool EditPrivacyController::hasOption(Option option) const { return (option != Option::CloseFriends); } QString EditPrivacyController::optionLabel(Option option) const { 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::CloseFriends: return tr::lng_edit_privacy_close_friends(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) { if (_controller->allowPremiumsToggle(Exception::Always) && _value.option == Option::Everyone) { // If we switch from Everyone to Contacts or Nobody suggest Premiums. _value.always.premiums = true; } } void EditPrivacyBox::prepare() { _controller->setView(this); setupContent(); } void EditPrivacyBox::editExceptions( Exception exception, Fn done) { auto controller = std::make_unique( &_window->session(), _controller->exceptionBoxTitle(exception), exceptions(exception), _controller->allowPremiumsToggle(exception)); auto initBox = [=, controller = controller.get()]( not_null box) { box->addButton(tr::lng_settings_save(), crl::guard(this, [=] { exceptions(exception).peers = box->collectSelectedRows(); exceptions(exception).premiums = controller->premiumsSelected(); 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).peers; for (const auto peer : exceptions(exception).peers) { removeFrom.erase( ranges::remove(removeFrom, peer), end(removeFrom)); } done(); box->closeBox(); })); box->addButton(tr::lng_cancel(), [=] { box->closeBox(); }); }; _window->show( Box(std::move(controller), std::move(initBox))); } EditPrivacyBox::Exceptions &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::CloseFriends) || (_value.option == Option::Nobody); case Exception::Never: return (_value.option == Option::Everyone) || (_value.option == Option::Contacts) || (_value.option == Option::CloseFriends); } 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::settingsPrivacyOption), (st::settingsSendTypePadding + style::margins( -st::lineWidth, st::settingsPrivacySkipTop, 0, 0))); } Ui::FlatLabel *EditPrivacyBox::addLabel( not_null container, rpl::producer text, int topSkip) { if (!text) { return nullptr; } auto label = object_ptr( container, rpl::duplicate(text), st::boxDividerLabel); const auto result = label.data(); container->add( object_ptr( container, std::move(label), st::defaultBoxDividerLabelPadding), { 0, topSkip, 0, 0 }); return result; } Ui::FlatLabel *EditPrivacyBox::addLabelOrDivider( not_null container, rpl::producer text, int topSkip) { if (const auto result = addLabel(container, std::move(text), topSkip)) { return result; } container->add( object_ptr(container), { 0, topSkip, 0, 0 }); return nullptr; } 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::map([=] { const auto &value = exceptions(exception); const auto count = Settings::ExceptionUsersCount(value.peers); const auto users = count ? tr::lng_edit_privacy_exceptions_count( tr::now, lt_count, count) : tr::lng_edit_privacy_exceptions_add(tr::now); return !value.premiums ? users : !count ? tr::lng_edit_privacy_premium(tr::now) : tr::lng_edit_privacy_exceptions_premium_and( tr::now, lt_users, users); }); _controller->handleExceptionsChange( exception, update->events_starting_with({}) | rpl::map([=] { return Settings::ExceptionUsersCount( exceptions(exception).peers); })); auto text = _controller->exceptionButtonTextKey(exception); const auto button = content->add( object_ptr>( content, object_ptr