tdesktop/Telegram/SourceFiles/boxes/edit_privacy_box.cpp

414 lines
12 KiB
C++
Raw Normal View History

/*
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"
2017-09-13 16:57:44 +00:00
#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"
2019-05-20 18:40:53 +00:00
#include "settings/settings_privacy_security.h"
#include "calls/calls_instance.h"
#include "base/binary_guard.h"
#include "lang/lang_keys.h"
#include "apiwrap.h"
2019-07-24 11:45:24 +00:00
#include "main/main_session.h"
#include "data/data_user.h"
2019-05-20 18:40:53 +00:00
#include "data/data_chat.h"
#include "data/data_channel.h"
2019-07-24 14:00:30 +00:00
#include "window/window_session_controller.h"
#include "styles/style_settings.h"
2019-09-18 11:19:05 +00:00
#include "styles/style_layers.h"
namespace {
class PrivacyExceptionsBoxController : public ChatsListBoxController {
public:
PrivacyExceptionsBoxController(
2019-07-25 18:55:11 +00:00
not_null<Window::SessionNavigation*> navigation,
rpl::producer<QString> title,
const std::vector<not_null<PeerData*>> &selected);
2019-07-25 18:55:11 +00:00
Main::Session &session() const override;
void rowClicked(not_null<PeerListRow*> row) override;
2019-05-20 18:40:53 +00:00
std::vector<not_null<PeerData*>> getResult() const;
protected:
void prepareViewHook() override;
std::unique_ptr<Row> createRow(not_null<History*> history) override;
private:
2019-07-25 18:55:11 +00:00
not_null<Window::SessionNavigation*> _navigation;
rpl::producer<QString> _title;
2019-05-20 18:40:53 +00:00
std::vector<not_null<PeerData*>> _selected;
};
2019-05-20 18:40:53 +00:00
PrivacyExceptionsBoxController::PrivacyExceptionsBoxController(
2019-07-25 18:55:11 +00:00
not_null<Window::SessionNavigation*> navigation,
rpl::producer<QString> title,
2019-05-20 18:40:53 +00:00
const std::vector<not_null<PeerData*>> &selected)
2019-07-25 18:55:11 +00:00
: ChatsListBoxController(navigation)
, _navigation(navigation)
, _title(std::move(title))
, _selected(selected) {
}
2019-07-25 18:55:11 +00:00
Main::Session &PrivacyExceptionsBoxController::session() const {
return _navigation->session();
}
void PrivacyExceptionsBoxController::prepareViewHook() {
delegate()->peerListSetTitle(std::move(_title));
2020-03-13 13:05:21 +00:00
delegate()->peerListAddSelectedPeers(_selected);
}
2019-05-20 18:40:53 +00:00
std::vector<not_null<PeerData*>> PrivacyExceptionsBoxController::getResult() const {
return delegate()->peerListCollectSelectedRows();
}
void PrivacyExceptionsBoxController::rowClicked(not_null<PeerListRow*> 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()) {
2019-05-20 18:40:53 +00:00
if (!channel->membersCountKnown()) {
channel->updateFull();
}
}
}
std::unique_ptr<PrivacyExceptionsBoxController::Row> PrivacyExceptionsBoxController::createRow(not_null<History*> history) {
2020-09-11 15:33:26 +00:00
if (history->peer->isSelf() || history->peer->isRepliesChat()) {
return nullptr;
2019-05-20 18:40:53 +00:00
} else if (!history->peer->isUser()
&& !history->peer->isChat()
&& !history->peer->isMegagroup()) {
return nullptr;
}
2019-05-20 18:40:53 +00:00
auto result = std::make_unique<Row>(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));
}
2019-05-20 18:40:53 +00:00
return result;
}
} // namespace
2019-06-19 15:09:03 +00:00
QString EditPrivacyController::optionLabel(Option option) {
2018-11-16 05:40:42 +00:00
switch (option) {
2019-06-19 15:09:03 +00:00
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);
2018-11-16 05:40:42 +00:00
}
Unexpected("Option value in optionsLabelKey.");
}
EditPrivacyBox::EditPrivacyBox(
QWidget*,
2019-07-24 14:00:30 +00:00
not_null<Window::SessionController*> window,
std::unique_ptr<EditPrivacyController> controller,
const Value &value)
2019-07-24 14:00:30 +00:00
: _window(window)
, _controller(std::move(controller))
, _value(value) {
}
void EditPrivacyBox::prepare() {
_controller->setView(this);
setupContent();
}
2019-05-20 18:40:53 +00:00
void EditPrivacyBox::editExceptions(
Exception exception,
Fn<void()> done) {
auto controller = std::make_unique<PrivacyExceptionsBoxController>(
2019-07-25 18:55:11 +00:00
_window,
_controller->exceptionBoxTitle(exception),
2019-05-20 18:40:53 +00:00
exceptions(exception));
auto initBox = [=, controller = controller.get()](
not_null<PeerListBox*> box) {
box->addButton(tr::lng_settings_save(), crl::guard(this, [=] {
2019-05-20 18:40:53 +00:00
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.");
2019-05-20 18:40:53 +00:00
}();
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<PeerListBox>(std::move(controller), std::move(initBox)),
2019-09-18 11:19:05 +00:00
Ui::LayerOption::KeepOther);
}
QVector<MTPInputPrivacyRule> EditPrivacyBox::collectResult() {
2019-05-20 18:40:53 +00:00
const auto collectInputUsers = [](const auto &peers) {
auto result = QVector<MTPInputUser>();
2019-05-20 18:40:53 +00:00
result.reserve(peers.size());
for (const auto peer : peers) {
if (const auto user = peer->asUser()) {
result.push_back(user->inputUser);
}
}
return result;
};
2019-05-20 18:40:53 +00:00
const auto collectInputChats = [](const auto &peers) {
2019-05-20 14:57:25 +00:00
auto result = QVector<MTPint>();
2019-05-20 18:40:53 +00:00
result.reserve(peers.size());
for (const auto peer : peers) {
if (!peer->isUser()) {
result.push_back(MTP_int(peer->bareId()));
}
2019-05-20 14:57:25 +00:00
}
return result;
};
constexpr auto kMaxRules = 3; // allow users, disallow users, option
auto result = QVector<MTPInputPrivacyRule>();
result.reserve(kMaxRules);
2019-05-20 14:57:25 +00:00
if (showExceptionLink(Exception::Always)) {
2019-05-20 18:40:53 +00:00
const auto users = collectInputUsers(_value.always);
const auto chats = collectInputChats(_value.always);
if (!users.empty()) {
result.push_back(MTP_inputPrivacyValueAllowUsers(MTP_vector<MTPInputUser>(users)));
2019-05-20 14:57:25 +00:00
}
2019-05-20 18:40:53 +00:00
if (!chats.empty()) {
result.push_back(MTP_inputPrivacyValueAllowChatParticipants(MTP_vector<MTPint>(chats)));
2019-05-20 14:57:25 +00:00
}
}
2019-05-20 14:57:25 +00:00
if (showExceptionLink(Exception::Never)) {
2019-05-20 18:40:53 +00:00
const auto users = collectInputUsers(_value.never);
const auto chats = collectInputChats(_value.never);
if (!users.empty()) {
result.push_back(MTP_inputPrivacyValueDisallowUsers(MTP_vector<MTPInputUser>(users)));
2019-05-20 14:57:25 +00:00
}
2019-05-20 18:40:53 +00:00
if (!chats.empty()) {
result.push_back(MTP_inputPrivacyValueDisallowChatParticipants(MTP_vector<MTPint>(chats)));
2019-05-20 14:57:25 +00:00
}
}
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;
}
2019-05-20 18:40:53 +00:00
std::vector<not_null<PeerData*>> &EditPrivacyBox::exceptions(Exception exception) {
switch (exception) {
2019-05-20 18:40:53 +00:00
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.");
}
2019-08-29 08:44:03 +00:00
Ui::Radioenum<EditPrivacyBox::Option> *EditPrivacyBox::AddOption(
not_null<Ui::VerticalLayout*> container,
2019-08-29 08:44:03 +00:00
not_null<EditPrivacyController*> controller,
const std::shared_ptr<Ui::RadioenumGroup<Option>> &group,
Option option) {
return container->add(
object_ptr<Ui::Radioenum<Option>>(
container,
group,
option,
2019-08-29 08:44:03 +00:00
controller->optionLabel(option),
st::settingsSendType),
st::settingsSendTypePadding);
}
2018-11-16 05:40:42 +00:00
Ui::FlatLabel *EditPrivacyBox::addLabel(
not_null<Ui::VerticalLayout*> container,
rpl::producer<QString> text) {
const auto wrap = container->add(
object_ptr<Ui::SlideWrap<Ui::FlatLabel>>(
container,
object_ptr<Ui::FlatLabel>(
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<Ui::VerticalLayout>(this);
const auto content = wrap.data();
setInnerWidget(object_ptr<Ui::OverrideMargins>(
this,
std::move(wrap)));
const auto group = std::make_shared<Ui::RadioenumGroup<Option>>(
_value.option);
const auto toggle = Ui::CreateChild<rpl::event_stream<Option>>(content);
group->setChangedCallback([=](Option value) {
_value.option = value;
toggle->fire_copy(value);
});
auto optionValue = toggle->events_starting_with_copy(_value.option);
2018-11-16 05:40:42 +00:00
const auto addOptionRow = [&](Option option) {
return (_controller->hasOption(option) || (_value.option == option))
2019-08-29 08:44:03 +00:00
? AddOption(content, _controller.get(), group, option)
: nullptr;
};
const auto addExceptionLink = [=](Exception exception) {
2018-11-21 10:09:46 +00:00
const auto update = Ui::CreateChild<rpl::event_stream<>>(content);
auto label = update->events_starting_with(
rpl::empty_value()
) | rpl::map([=] {
2019-05-20 18:40:53 +00:00
return Settings::ExceptionUsersCount(exceptions(exception));
}) | rpl::map([](int count) {
return count
? tr::lng_edit_privacy_exceptions_count(tr::now, lt_count, count)
2019-06-19 15:09:03 +00:00
: tr::lng_edit_privacy_exceptions_add(tr::now);
});
auto text = _controller->exceptionButtonTextKey(exception);
const auto button = content->add(
object_ptr<Ui::SlideWrap<Button>>(
content,
object_ptr<Button>(
content,
2019-06-18 12:16:43 +00:00
rpl::duplicate(text),
st::settingsButton)));
CreateRightLabel(
button->entity(),
std::move(label),
st::settingsButton,
2019-06-18 12:16:43 +00:00
std::move(text));
button->toggleOn(rpl::duplicate(
optionValue
) | rpl::map([=] {
return showExceptionLink(exception);
}))->entity()->addClickHandler([=] {
2019-05-20 18:40:53 +00:00
editExceptions(exception, [=] { update->fire({}); });
});
return button;
};
auto above = _controller->setupAboveWidget(
content,
2019-08-29 08:44:03 +00:00
rpl::duplicate(optionValue));
if (above) {
content->add(std::move(above));
}
AddSubsectionTitle(content, _controller->optionsTitleKey());
2018-11-16 05:40:42 +00:00
addOptionRow(Option::Everyone);
addOptionRow(Option::Contacts);
addOptionRow(Option::Nobody);
addLabel(content, _controller->warning());
AddSkip(content);
2019-08-29 08:44:03 +00:00
auto middle = _controller->setupMiddleWidget(
_window,
content,
std::move(optionValue));
if (middle) {
content->add(std::move(middle));
}
AddDivider(content);
AddSkip(content);
2019-06-18 12:16:43 +00:00
AddSubsectionTitle(content, tr::lng_edit_privacy_exceptions());
const auto always = addExceptionLink(Exception::Always);
const auto never = addExceptionLink(Exception::Never);
2018-11-16 05:40:42 +00:00
addLabel(content, _controller->exceptionsDescription());
AddSkip(content);
2019-07-24 14:00:30 +00:00
if (auto below = _controller->setupBelowWidget(_window, content)) {
content->add(std::move(below));
}
addButton(tr::lng_settings_save(), [=] {
const auto someAreDisallowed = (_value.option != Option::Everyone)
2019-05-20 18:40:53 +00:00
|| !_value.never.empty();
_controller->confirmSave(someAreDisallowed, crl::guard(this, [=] {
2019-08-29 08:44:03 +00:00
_controller->saveAdditional();
2019-07-24 14:00:30 +00:00
_window->session().api().savePrivacy(
_controller->apiKey(),
collectResult());
closeBox();
}));
});
addButton(tr::lng_cancel(), [this] { closeBox(); });
const auto linkHeight = st::settingsButton.padding.top()
+ st::settingsButton.height
+ st::settingsButton.padding.bottom();
widthValue(
) | rpl::start_with_next([=](int width) {
content->resizeToWidth(width);
}, content->lifetime());
content->heightValue(
) | rpl::map([=](int height) {
return height - always->height() - never->height() + 2 * linkHeight;
}) | rpl::distinct_until_changed(
) | rpl::start_with_next([=](int height) {
setDimensions(st::boxWideWidth, height);
}, content->lifetime());
}