2017-03-16 15:15:07 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2017-03-16 15:15:07 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2017-03-16 15:15:07 +00:00
|
|
|
*/
|
|
|
|
#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"
|
2018-09-19 09:06:21 +00:00
|
|
|
#include "ui/wrap/vertical_layout.h"
|
2018-01-13 12:45:11 +00:00
|
|
|
#include "history/history.h"
|
2017-08-14 12:53:49 +00:00
|
|
|
#include "boxes/peer_list_controllers.h"
|
2018-09-19 09:06:21 +00:00
|
|
|
#include "info/profile/info_profile_button.h"
|
|
|
|
#include "settings/settings_common.h"
|
2018-09-12 17:02:30 +00:00
|
|
|
#include "calls/calls_instance.h"
|
2018-09-11 09:11:52 +00:00
|
|
|
#include "base/binary_guard.h"
|
|
|
|
#include "lang/lang_keys.h"
|
2017-03-17 17:19:46 +00:00
|
|
|
#include "apiwrap.h"
|
2017-08-04 14:54:32 +00:00
|
|
|
#include "auth_session.h"
|
2018-09-19 09:06:21 +00:00
|
|
|
#include "styles/style_settings.h"
|
|
|
|
#include "styles/style_boxes.h"
|
2017-03-16 15:15:07 +00:00
|
|
|
|
2017-03-17 12:05:50 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class PrivacyExceptionsBoxController : public ChatsListBoxController {
|
|
|
|
public:
|
2018-06-04 15:35:11 +00:00
|
|
|
PrivacyExceptionsBoxController(Fn<QString()> titleFactory, const std::vector<not_null<UserData*>> &selected);
|
2017-08-17 08:31:24 +00:00
|
|
|
void rowClicked(not_null<PeerListRow*> row) override;
|
2017-06-13 21:08:47 +00:00
|
|
|
|
2017-08-17 08:31:24 +00:00
|
|
|
std::vector<not_null<UserData*>> getResult() const;
|
2017-03-17 12:05:50 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
void prepareViewHook() override;
|
2017-08-17 08:31:24 +00:00
|
|
|
std::unique_ptr<Row> createRow(not_null<History*> history) override;
|
2017-03-17 12:05:50 +00:00
|
|
|
|
|
|
|
private:
|
2018-06-04 15:35:11 +00:00
|
|
|
Fn<QString()> _titleFactory;
|
2017-08-17 08:31:24 +00:00
|
|
|
std::vector<not_null<UserData*>> _selected;
|
2017-03-17 12:05:50 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2018-06-04 15:35:11 +00:00
|
|
|
PrivacyExceptionsBoxController::PrivacyExceptionsBoxController(Fn<QString()> titleFactory, const std::vector<not_null<UserData*>> &selected)
|
2017-05-30 15:21:05 +00:00
|
|
|
: _titleFactory(std::move(titleFactory))
|
2017-06-13 21:08:47 +00:00
|
|
|
, _selected(selected) {
|
2017-03-17 12:05:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PrivacyExceptionsBoxController::prepareViewHook() {
|
2017-06-13 21:08:47 +00:00
|
|
|
delegate()->peerListSetTitle(_titleFactory);
|
|
|
|
delegate()->peerListAddSelectedRows(_selected);
|
|
|
|
}
|
|
|
|
|
2017-08-17 08:31:24 +00:00
|
|
|
std::vector<not_null<UserData*>> PrivacyExceptionsBoxController::getResult() const {
|
2017-06-13 21:08:47 +00:00
|
|
|
auto peers = delegate()->peerListCollectSelectedRows();
|
2017-08-17 08:31:24 +00:00
|
|
|
auto users = std::vector<not_null<UserData*>>();
|
2017-06-13 21:08:47 +00:00
|
|
|
if (!peers.empty()) {
|
|
|
|
users.reserve(peers.size());
|
|
|
|
for_const (auto peer, peers) {
|
|
|
|
auto user = peer->asUser();
|
2017-08-17 09:06:26 +00:00
|
|
|
Assert(user != nullptr);
|
2017-06-13 21:08:47 +00:00
|
|
|
users.push_back(user);
|
2017-03-17 12:05:50 +00:00
|
|
|
}
|
2017-06-13 21:08:47 +00:00
|
|
|
}
|
|
|
|
return users;
|
2017-03-17 12:05:50 +00:00
|
|
|
}
|
|
|
|
|
2017-08-17 08:31:24 +00:00
|
|
|
void PrivacyExceptionsBoxController::rowClicked(not_null<PeerListRow*> row) {
|
2017-06-13 21:08:47 +00:00
|
|
|
delegate()->peerListSetRowChecked(row, !row->checked());
|
2017-03-17 12:05:50 +00:00
|
|
|
}
|
|
|
|
|
2017-08-17 08:31:24 +00:00
|
|
|
std::unique_ptr<PrivacyExceptionsBoxController::Row> PrivacyExceptionsBoxController::createRow(not_null<History*> history) {
|
2017-08-13 16:14:00 +00:00
|
|
|
if (history->peer->isSelf()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2017-03-17 12:05:50 +00:00
|
|
|
if (auto user = history->peer->asUser()) {
|
2017-08-13 16:14:00 +00:00
|
|
|
return std::make_unique<Row>(history);
|
2017-03-17 12:05:50 +00:00
|
|
|
}
|
2017-08-13 16:14:00 +00:00
|
|
|
return nullptr;
|
2017-03-17 12:05:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2018-09-11 09:11:52 +00:00
|
|
|
EditPrivacyBox::EditPrivacyBox(
|
|
|
|
QWidget*,
|
|
|
|
std::unique_ptr<Controller> controller,
|
2018-09-19 09:06:21 +00:00
|
|
|
const Value &value)
|
2018-09-11 09:11:52 +00:00
|
|
|
: _controller(std::move(controller))
|
2018-09-19 09:06:21 +00:00
|
|
|
, _value(value) {
|
2017-03-16 15:15:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void EditPrivacyBox::prepare() {
|
|
|
|
_controller->setView(this);
|
|
|
|
|
2018-09-19 09:06:21 +00:00
|
|
|
setupContent();
|
2017-03-19 08:29:05 +00:00
|
|
|
}
|
|
|
|
|
2018-09-19 09:06:21 +00:00
|
|
|
void EditPrivacyBox::editExceptionUsers(
|
|
|
|
Exception exception,
|
|
|
|
Fn<void()> done) {
|
|
|
|
auto controller = std::make_unique<PrivacyExceptionsBoxController>(
|
|
|
|
crl::guard(this, [=] {
|
|
|
|
return _controller->exceptionBoxTitle(exception);
|
|
|
|
}),
|
|
|
|
exceptionUsers(exception));
|
|
|
|
auto initBox = [=, controller = controller.get()](
|
|
|
|
not_null<PeerListBox*> box) {
|
|
|
|
box->addButton(langFactory(lng_settings_save), crl::guard(this, [=] {
|
2017-06-13 21:08:47 +00:00
|
|
|
exceptionUsers(exception) = controller->getResult();
|
2018-09-19 09:06:21 +00:00
|
|
|
const auto removeFrom = ([=] {
|
2017-06-13 21:08:47 +00:00
|
|
|
switch (exception) {
|
|
|
|
case Exception::Always: return Exception::Never;
|
|
|
|
case Exception::Never: return Exception::Always;
|
|
|
|
}
|
|
|
|
Unexpected("Invalid exception value.");
|
|
|
|
})();
|
|
|
|
auto &removeFromUsers = exceptionUsers(removeFrom);
|
2018-09-19 09:06:21 +00:00
|
|
|
for (const auto user : exceptionUsers(exception)) {
|
|
|
|
const auto from = ranges::remove(removeFromUsers, user);
|
|
|
|
removeFromUsers.erase(from, end(removeFromUsers));
|
2017-03-17 12:05:50 +00:00
|
|
|
}
|
2018-09-19 09:06:21 +00:00
|
|
|
done();
|
2017-06-13 21:08:47 +00:00
|
|
|
box->closeBox();
|
|
|
|
}));
|
2018-09-19 09:06:21 +00:00
|
|
|
box->addButton(langFactory(lng_cancel), [=] { box->closeBox(); });
|
2017-06-13 21:08:47 +00:00
|
|
|
};
|
2017-09-15 17:34:41 +00:00
|
|
|
Ui::show(
|
|
|
|
Box<PeerListBox>(std::move(controller), std::move(initBox)),
|
|
|
|
LayerOption::KeepOther);
|
2017-03-16 15:15:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QVector<MTPInputPrivacyRule> EditPrivacyBox::collectResult() {
|
|
|
|
auto collectInputUsers = [](auto &users) {
|
|
|
|
auto result = QVector<MTPInputUser>();
|
|
|
|
result.reserve(users.size());
|
|
|
|
for (auto user : users) {
|
|
|
|
result.push_back(user->inputUser);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
2017-03-19 08:29:05 +00:00
|
|
|
constexpr auto kMaxRules = 3; // allow users, disallow users, option
|
2017-03-16 15:15:07 +00:00
|
|
|
auto result = QVector<MTPInputPrivacyRule>();
|
2017-03-19 08:29:05 +00:00
|
|
|
result.reserve(kMaxRules);
|
2018-09-11 09:11:52 +00:00
|
|
|
if (showExceptionLink(Exception::Always) && !_value.always.empty()) {
|
|
|
|
result.push_back(MTP_inputPrivacyValueAllowUsers(MTP_vector<MTPInputUser>(collectInputUsers(_value.always))));
|
2017-03-16 15:15:07 +00:00
|
|
|
}
|
2018-09-11 09:11:52 +00:00
|
|
|
if (showExceptionLink(Exception::Never) && !_value.never.empty()) {
|
|
|
|
result.push_back(MTP_inputPrivacyValueDisallowUsers(MTP_vector<MTPInputUser>(collectInputUsers(_value.never))));
|
2017-03-16 15:15:07 +00:00
|
|
|
}
|
2018-09-19 09:06:21 +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.");
|
|
|
|
}());
|
2017-03-16 15:15:07 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-08-17 08:31:24 +00:00
|
|
|
std::vector<not_null<UserData*>> &EditPrivacyBox::exceptionUsers(Exception exception) {
|
2017-03-17 12:05:50 +00:00
|
|
|
switch (exception) {
|
2018-09-11 09:11:52 +00:00
|
|
|
case Exception::Always: return _value.always;
|
|
|
|
case Exception::Never: return _value.never;
|
2017-03-17 12:05:50 +00:00
|
|
|
}
|
|
|
|
Unexpected("Invalid exception value.");
|
|
|
|
}
|
|
|
|
|
2018-09-19 09:06:21 +00:00
|
|
|
bool EditPrivacyBox::showExceptionLink(Exception exception) const {
|
2017-03-17 12:05:50 +00:00
|
|
|
switch (exception) {
|
2018-09-19 09:06:21 +00:00
|
|
|
case Exception::Always:
|
|
|
|
return (_value.option == Option::Contacts)
|
|
|
|
|| (_value.option == Option::Nobody);
|
|
|
|
case Exception::Never:
|
|
|
|
return (_value.option == Option::Everyone)
|
|
|
|
|| (_value.option == Option::Contacts);
|
2017-03-17 12:05:50 +00:00
|
|
|
}
|
|
|
|
Unexpected("Invalid exception value.");
|
2017-03-16 15:15:07 +00:00
|
|
|
}
|
|
|
|
|
2018-09-19 09:06:21 +00:00
|
|
|
Ui::Radioenum<EditPrivacyBox::Option> *EditPrivacyBox::AddOption(
|
|
|
|
not_null<Ui::VerticalLayout*> container,
|
|
|
|
const std::shared_ptr<Ui::RadioenumGroup<Option>> &group,
|
|
|
|
Option option) {
|
|
|
|
const auto label = [&] {
|
|
|
|
switch (option) {
|
|
|
|
case Option::Everyone: return lng_edit_privacy_everyone;
|
|
|
|
case Option::Contacts: return lng_edit_privacy_contacts;
|
|
|
|
case Option::Nobody: return lng_edit_privacy_nobody;
|
|
|
|
}
|
|
|
|
Unexpected("Option value in EditPrivacyBox::AddOption.");
|
|
|
|
}();
|
|
|
|
return container->add(
|
|
|
|
object_ptr<Ui::Radioenum<Option>>(
|
|
|
|
container,
|
|
|
|
group,
|
|
|
|
option,
|
|
|
|
lang(label),
|
|
|
|
st::settingsSendType),
|
|
|
|
st::settingsSendTypePadding);
|
2017-03-16 15:15:07 +00:00
|
|
|
}
|
|
|
|
|
2018-09-19 09:06:21 +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();
|
|
|
|
}
|
2017-03-16 15:15:07 +00:00
|
|
|
|
2018-09-19 09:06:21 +00:00
|
|
|
void EditPrivacyBox::setupContent() {
|
|
|
|
using namespace Settings;
|
|
|
|
|
2018-09-20 18:25:43 +00:00
|
|
|
setTitle([=] { return _controller->title(); });
|
|
|
|
|
2018-09-19 09:06:21 +00:00
|
|
|
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::AttachAsChild(content, rpl::event_stream<>());
|
|
|
|
|
|
|
|
group->setChangedCallback([=](Option value) {
|
|
|
|
_value.option = value;
|
|
|
|
toggle->fire({});
|
|
|
|
});
|
|
|
|
|
|
|
|
const auto addOption = [&](Option option) {
|
|
|
|
return (_controller->hasOption(option) || (_value.option == option))
|
|
|
|
? AddOption(content, group, option)
|
|
|
|
: nullptr;
|
2017-03-19 08:29:05 +00:00
|
|
|
};
|
2018-09-19 09:06:21 +00:00
|
|
|
const auto addExceptionLink = [=](Exception exception) {
|
|
|
|
const auto update = Ui::AttachAsChild(
|
|
|
|
content,
|
|
|
|
rpl::event_stream<>());
|
|
|
|
auto label = update->events_starting_with(
|
|
|
|
rpl::empty_value()
|
|
|
|
) | rpl::map([=] {
|
|
|
|
return exceptionUsers(exception).size();
|
|
|
|
}) | rpl::map([](int count) {
|
|
|
|
return count
|
|
|
|
? lng_edit_privacy_exceptions_count(lt_count, count)
|
|
|
|
: lang(lng_edit_privacy_exceptions_add);
|
|
|
|
});
|
|
|
|
auto text = _controller->exceptionButtonTextKey(exception);
|
|
|
|
const auto button = content->add(
|
|
|
|
object_ptr<Ui::SlideWrap<Button>>(
|
|
|
|
content,
|
|
|
|
object_ptr<Button>(
|
|
|
|
content,
|
|
|
|
Lang::Viewer(text),
|
|
|
|
st::settingsButton)));
|
|
|
|
CreateRightLabel(
|
|
|
|
button->entity(),
|
|
|
|
std::move(label),
|
|
|
|
st::settingsButton,
|
|
|
|
text);
|
|
|
|
button->toggleOn(toggle->events_starting_with(
|
|
|
|
rpl::empty_value()
|
|
|
|
) | rpl::map([=] {
|
|
|
|
return showExceptionLink(exception);
|
|
|
|
}))->entity()->addClickHandler([=] {
|
|
|
|
editExceptionUsers(exception, [=] { update->fire({}); });
|
|
|
|
});
|
|
|
|
return button;
|
2017-03-16 15:15:07 +00:00
|
|
|
};
|
2017-03-19 08:29:05 +00:00
|
|
|
|
2018-09-19 09:06:21 +00:00
|
|
|
AddSubsectionTitle(content, _controller->optionsTitleKey());
|
|
|
|
addOption(Option::Everyone);
|
|
|
|
addOption(Option::Contacts);
|
|
|
|
addOption(Option::Nobody);
|
|
|
|
AddLabel(content, _controller->warning());
|
|
|
|
AddSkip(content);
|
|
|
|
|
|
|
|
AddDivider(content);
|
|
|
|
AddSkip(content);
|
|
|
|
AddSubsectionTitle(content, lng_edit_privacy_exceptions);
|
|
|
|
const auto always = addExceptionLink(Exception::Always);
|
|
|
|
const auto never = addExceptionLink(Exception::Never);
|
|
|
|
AddLabel(content, _controller->exceptionsDescription());
|
|
|
|
AddSkip(content);
|
|
|
|
|
|
|
|
const auto saveAdditional = _controller->setupAdditional(content);
|
|
|
|
|
|
|
|
addButton(langFactory(lng_settings_save), [=] {
|
|
|
|
const auto someAreDisallowed = (_value.option != Option::Everyone)
|
|
|
|
|| !_value.never.empty();
|
|
|
|
_controller->confirmSave(someAreDisallowed, crl::guard(this, [=] {
|
2018-09-11 09:11:52 +00:00
|
|
|
Auth().api().savePrivacy(
|
|
|
|
_controller->apiKey(),
|
|
|
|
collectResult());
|
2018-09-19 09:06:21 +00:00
|
|
|
if (saveAdditional) {
|
|
|
|
saveAdditional();
|
|
|
|
}
|
2017-03-17 18:44:55 +00:00
|
|
|
closeBox();
|
|
|
|
}));
|
2017-03-17 17:19:46 +00:00
|
|
|
});
|
2017-05-30 15:21:05 +00:00
|
|
|
addButton(langFactory(lng_cancel), [this] { closeBox(); });
|
2017-03-19 08:29:05 +00:00
|
|
|
|
2018-09-19 09:06:21 +00:00
|
|
|
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());
|
2018-09-12 17:02:30 +00:00
|
|
|
}
|