/* 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/peers/edit_linked_chat_box.h" #include "lang/lang_keys.h" #include "data/data_channel.h" #include "data/data_chat.h" #include "ui/widgets/labels.h" #include "ui/wrap/vertical_layout.h" #include "ui/text/text_utilities.h" // Ui::Text::ToUpper #include "info/profile/info_profile_button.h" #include "boxes/peer_list_box.h" #include "boxes/confirm_box.h" #include "boxes/add_contact_box.h" #include "apiwrap.h" #include "auth_session.h" #include "styles/style_boxes.h" #include "styles/style_info.h" namespace { constexpr auto kEnableSearchRowsCount = 10; class Controller : public PeerListController, public base::has_weak_ptr { public: Controller( not_null channel, ChannelData *chat, const std::vector> &chats, Fn callback); void prepare() override; void rowClicked(not_null row) override; int contentWidth() const override; private: void choose(not_null chat); void choose(not_null chat); not_null _channel; ChannelData *_chat = nullptr; std::vector> _chats; Fn _callback; ChannelData *_waitForFull = nullptr; }; Controller::Controller( not_null channel, ChannelData *chat, const std::vector> &chats, Fn callback) : _channel(channel) , _chat(chat) , _chats(std::move(chats)) , _callback(std::move(callback)) { base::ObservableViewer( channel->session().api().fullPeerUpdated() ) | rpl::start_with_next([=](PeerData *peer) { if (peer == _waitForFull) { choose(std::exchange(_waitForFull, nullptr)); } }, lifetime()); } int Controller::contentWidth() const { return st::boxWidth; } void Controller::prepare() { const auto appendRow = [&](not_null chat) { if (delegate()->peerListFindRow(chat->id)) { return; } auto row = std::make_unique(chat); const auto username = chat->userName(); row->setCustomStatus(username.isEmpty() ? tr::lng_manage_discussion_group_private_status(tr::now) : ('@' + username)); delegate()->peerListAppendRow(std::move(row)); }; if (_chat) { appendRow(_chat); } else { for (const auto chat : _chats) { appendRow(chat); } if (_chats.size() >= kEnableSearchRowsCount) { delegate()->peerListSetSearchMode(PeerListSearchMode::Enabled); } } } void Controller::rowClicked(not_null row) { if (_chat != nullptr) { Ui::showPeerHistory(_chat, ShowAtUnreadMsgId); return; } const auto peer = row->peer(); if (const auto channel = peer->asChannel()) { if (channel->wasFullUpdated()) { choose(channel); return; } _waitForFull = channel; channel->updateFull(); } else if (const auto chat = peer->asChat()) { choose(chat); } } void Controller::choose(not_null chat) { auto text = tr::lng_manage_discussion_group_sure( tr::now, lt_group, Ui::Text::Bold(chat->name), lt_channel, Ui::Text::Bold(_channel->name), Ui::Text::WithEntities); if (!_channel->isPublic()) { text.append( "\n\n" + tr::lng_manage_linked_channel_private(tr::now)); } if (!chat->isPublic()) { text.append( "\n\n" + tr::lng_manage_discussion_group_private(tr::now)); if (chat->hiddenPreHistory()) { text.append("\n\n"); text.append(tr::lng_manage_discussion_group_warning( tr::now, Ui::Text::RichLangValue)); } } const auto box = std::make_shared>(); const auto sure = [=] { if (*box) { (*box)->closeBox(); } const auto onstack = _callback; onstack(chat); }; *box = Ui::show( Box( text, tr::lng_manage_discussion_group_link(tr::now), sure), LayerOption::KeepOther); } void Controller::choose(not_null chat) { auto text = tr::lng_manage_discussion_group_sure( tr::now, lt_group, Ui::Text::Bold(chat->name), lt_channel, Ui::Text::Bold(_channel->name), Ui::Text::WithEntities); if (!_channel->isPublic()) { text.append("\n\n" + tr::lng_manage_linked_channel_private(tr::now)); } text.append("\n\n" + tr::lng_manage_discussion_group_private(tr::now)); text.append("\n\n"); text.append(tr::lng_manage_discussion_group_warning( tr::now, Ui::Text::RichLangValue)); const auto box = std::make_shared>(); const auto sure = [=] { if (*box) { (*box)->closeBox(); } const auto done = [=](not_null chat) { const auto onstack = _callback; onstack(chat); }; chat->session().api().migrateChat(chat, crl::guard(this, done)); }; *box = Ui::show( Box( text, tr::lng_manage_discussion_group_link(tr::now), sure), LayerOption::KeepOther); } object_ptr SetupAbout( not_null parent, not_null channel, ChannelData *chat) { auto about = object_ptr( parent, QString(), st::linkedChatAbout); about->setMarkedText([&] { if (!channel->isBroadcast()) { return tr::lng_manage_linked_channel_about( tr::now, lt_channel, Ui::Text::Bold(chat->name), Ui::Text::WithEntities); } else if (chat != nullptr) { return tr::lng_manage_discussion_group_about_chosen( tr::now, lt_group, Ui::Text::Bold(chat->name), Ui::Text::WithEntities); } return tr::lng_manage_discussion_group_about( tr::now, Ui::Text::WithEntities); }()); return std::move(about); } object_ptr SetupFooter( not_null parent, not_null channel) { return object_ptr( parent, (channel->isBroadcast() ? tr::lng_manage_discussion_group_posted : tr::lng_manage_linked_channel_posted)(), st::linkedChatAbout); } object_ptr SetupCreateGroup( not_null parent, not_null channel, Fn callback) { Expects(channel->isBroadcast()); auto result = object_ptr( parent, tr::lng_manage_discussion_group_create( ) | Ui::Text::ToUpper(), st::infoCreateLinkedChatButton); result->addClickHandler([=] { const auto guarded = crl::guard(parent, callback); Ui::show( Box( GroupInfoBox::Type::Megagroup, channel->name + " Chat", guarded), LayerOption::KeepOther); }); return result; } object_ptr SetupUnlink( not_null parent, not_null channel, Fn callback) { auto result = object_ptr( parent, (channel->isBroadcast() ? tr::lng_manage_discussion_group_unlink : tr::lng_manage_linked_channel_unlink)() | Ui::Text::ToUpper(), st::infoUnlinkChatButton); result->addClickHandler([=] { callback(nullptr); }); return result; } object_ptr EditLinkedChatBox( not_null channel, ChannelData *chat, std::vector> &&chats, bool canEdit, Fn callback) { Expects((channel->isBroadcast() && canEdit) || (chat != nullptr)); const auto init = [=](not_null box) { auto above = object_ptr(box); above->add( SetupAbout(above, channel, chat), st::linkedChatAboutPadding); if (!chat) { above->add(SetupCreateGroup(above, channel, callback)); } box->peerListSetAboveWidget(std::move(above)); auto below = object_ptr(box); if (chat && canEdit) { below->add(SetupUnlink(below, channel, callback)); } below->add( SetupFooter(below, channel), st::linkedChatAboutPadding); box->peerListSetBelowWidget(std::move(below)); box->setTitle(channel->isBroadcast() ? tr::lng_manage_discussion_group() : tr::lng_manage_linked_channel()); box->addButton(tr::lng_close(), [=] { box->closeBox(); }); }; auto controller = std::make_unique( channel, chat, std::move(chats), std::move(callback)); return Box(std::move(controller), init); } } // namespace object_ptr EditLinkedChatBox( not_null channel, std::vector> &&chats, Fn callback) { return EditLinkedChatBox( channel, nullptr, std::move(chats), true, callback); } object_ptr EditLinkedChatBox( not_null channel, not_null chat, bool canEdit, Fn callback) { return EditLinkedChatBox(channel, chat, {}, canEdit, callback); }