2018-06-03 13:30:40 +00:00
|
|
|
/*
|
|
|
|
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 "window/window_lock_widgets.h"
|
|
|
|
|
|
|
|
#include "lang/lang_keys.h"
|
|
|
|
#include "storage/localstorage.h"
|
|
|
|
#include "mainwindow.h"
|
2019-01-21 13:42:21 +00:00
|
|
|
#include "core/application.h"
|
2019-09-16 11:14:06 +00:00
|
|
|
#include "api/api_text_entities.h"
|
2018-06-03 13:30:40 +00:00
|
|
|
#include "ui/text/text.h"
|
|
|
|
#include "ui/widgets/buttons.h"
|
|
|
|
#include "ui/widgets/checkbox.h"
|
|
|
|
#include "ui/widgets/input_fields.h"
|
|
|
|
#include "ui/widgets/labels.h"
|
|
|
|
#include "ui/wrap/vertical_layout.h"
|
|
|
|
#include "ui/toast/toast.h"
|
2019-09-09 15:54:38 +00:00
|
|
|
#include "window/window_controller.h"
|
2018-06-03 13:30:40 +00:00
|
|
|
#include "window/window_slide_animation.h"
|
2019-06-06 10:21:40 +00:00
|
|
|
#include "window/window_session_controller.h"
|
2019-09-13 06:06:02 +00:00
|
|
|
#include "main/main_account.h"
|
|
|
|
#include "facades.h"
|
2019-09-18 11:19:05 +00:00
|
|
|
#include "styles/style_layers.h"
|
|
|
|
#include "styles/style_boxes.h"
|
2018-06-03 13:30:40 +00:00
|
|
|
|
|
|
|
namespace Window {
|
|
|
|
|
2019-09-09 15:54:38 +00:00
|
|
|
LockWidget::LockWidget(QWidget *parent, not_null<Controller*> window)
|
|
|
|
: RpWidget(parent)
|
|
|
|
, _window(window) {
|
2018-06-03 13:30:40 +00:00
|
|
|
show();
|
|
|
|
}
|
|
|
|
|
2019-09-13 06:06:02 +00:00
|
|
|
not_null<Controller*> LockWidget::window() const {
|
|
|
|
return _window;
|
|
|
|
}
|
|
|
|
|
2018-06-03 13:30:40 +00:00
|
|
|
void LockWidget::setInnerFocus() {
|
2019-09-09 15:54:38 +00:00
|
|
|
if (const auto controller = _window->sessionController()) {
|
2018-06-03 13:30:40 +00:00
|
|
|
controller->dialogsListFocused().set(false, true);
|
|
|
|
}
|
|
|
|
setFocus();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LockWidget::showAnimated(const QPixmap &bgAnimCache, bool back) {
|
|
|
|
_showBack = back;
|
|
|
|
(_showBack ? _cacheOver : _cacheUnder) = bgAnimCache;
|
|
|
|
|
2019-04-02 09:13:30 +00:00
|
|
|
_a_show.stop();
|
2018-06-03 13:30:40 +00:00
|
|
|
|
|
|
|
showChildren();
|
|
|
|
setInnerFocus();
|
|
|
|
(_showBack ? _cacheUnder : _cacheOver) = Ui::GrabWidget(this);
|
|
|
|
hideChildren();
|
|
|
|
|
|
|
|
_a_show.start(
|
|
|
|
[this] { animationCallback(); },
|
|
|
|
0.,
|
|
|
|
1.,
|
|
|
|
st::slideDuration,
|
|
|
|
Window::SlideAnimation::transition());
|
|
|
|
show();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LockWidget::animationCallback() {
|
|
|
|
update();
|
|
|
|
if (!_a_show.animating()) {
|
|
|
|
showChildren();
|
2019-09-09 15:54:38 +00:00
|
|
|
_window->widget()->setInnerFocus();
|
2018-06-03 13:30:40 +00:00
|
|
|
|
|
|
|
Ui::showChatsList();
|
|
|
|
|
|
|
|
_cacheUnder = _cacheOver = QPixmap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LockWidget::paintEvent(QPaintEvent *e) {
|
|
|
|
Painter p(this);
|
|
|
|
|
2019-04-02 09:13:30 +00:00
|
|
|
auto progress = _a_show.value(1.);
|
2018-06-03 13:30:40 +00:00
|
|
|
if (_a_show.animating()) {
|
|
|
|
auto coordUnder = _showBack ? anim::interpolate(-st::slideShift, 0, progress) : anim::interpolate(0, -st::slideShift, progress);
|
|
|
|
auto coordOver = _showBack ? anim::interpolate(0, width(), progress) : anim::interpolate(width(), 0, progress);
|
|
|
|
auto shadow = _showBack ? (1. - progress) : progress;
|
|
|
|
if (coordOver > 0) {
|
|
|
|
p.drawPixmap(QRect(0, 0, coordOver, height()), _cacheUnder, QRect(-coordUnder * cRetinaFactor(), 0, coordOver * cRetinaFactor(), height() * cRetinaFactor()));
|
|
|
|
p.setOpacity(shadow);
|
|
|
|
p.fillRect(0, 0, coordOver, height(), st::slideFadeOutBg);
|
|
|
|
p.setOpacity(1);
|
|
|
|
}
|
|
|
|
p.drawPixmap(coordOver, 0, _cacheOver);
|
|
|
|
p.setOpacity(shadow);
|
|
|
|
st::slideShadow.fill(p, QRect(coordOver - st::slideShadow.width(), 0, st::slideShadow.width(), height()));
|
|
|
|
} else {
|
|
|
|
paintContent(p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LockWidget::paintContent(Painter &p) {
|
|
|
|
p.fillRect(rect(), st::windowBg);
|
|
|
|
}
|
|
|
|
|
2019-09-09 15:54:38 +00:00
|
|
|
PasscodeLockWidget::PasscodeLockWidget(
|
|
|
|
QWidget *parent,
|
|
|
|
not_null<Controller*> window)
|
|
|
|
: LockWidget(parent, window)
|
2019-06-18 14:07:45 +00:00
|
|
|
, _passcode(this, st::passcodeInput, tr::lng_passcode_ph())
|
2019-06-18 16:53:27 +00:00
|
|
|
, _submit(this, tr::lng_passcode_submit(), st::passcodeSubmit)
|
2019-06-19 15:09:03 +00:00
|
|
|
, _logout(this, tr::lng_passcode_logout(tr::now)) {
|
2018-06-03 13:30:40 +00:00
|
|
|
connect(_passcode, &Ui::MaskedInputField::changed, [=] { changed(); });
|
|
|
|
connect(_passcode, &Ui::MaskedInputField::submitted, [=] { submit(); });
|
|
|
|
|
|
|
|
_submit->setClickedCallback([=] { submit(); });
|
2019-09-09 15:54:38 +00:00
|
|
|
_logout->setClickedCallback([=] { window->widget()->onLogout(); });
|
2018-06-03 13:30:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PasscodeLockWidget::paintContent(Painter &p) {
|
|
|
|
LockWidget::paintContent(p);
|
|
|
|
|
|
|
|
p.setFont(st::passcodeHeaderFont);
|
|
|
|
p.setPen(st::windowFg);
|
2019-06-19 15:09:03 +00:00
|
|
|
p.drawText(QRect(0, _passcode->y() - st::passcodeHeaderHeight, width(), st::passcodeHeaderHeight), tr::lng_passcode_enter(tr::now), style::al_center);
|
2018-06-03 13:30:40 +00:00
|
|
|
|
|
|
|
if (!_error.isEmpty()) {
|
|
|
|
p.setFont(st::boxTextFont);
|
|
|
|
p.setPen(st::boxTextFgError);
|
|
|
|
p.drawText(QRect(0, _passcode->y() + _passcode->height(), width(), st::passcodeSubmitSkip), _error, style::al_center);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PasscodeLockWidget::submit() {
|
|
|
|
if (_passcode->text().isEmpty()) {
|
|
|
|
_passcode->showError();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!passcodeCanTry()) {
|
2019-06-19 15:09:03 +00:00
|
|
|
_error = tr::lng_flood_error(tr::now);
|
2018-06-03 13:30:40 +00:00
|
|
|
_passcode->showError();
|
|
|
|
update();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-07 07:44:38 +00:00
|
|
|
const auto passcode = _passcode->text().toUtf8();
|
2019-09-13 06:06:02 +00:00
|
|
|
const auto correct = window()->account().sessionExists()
|
2018-06-07 07:44:38 +00:00
|
|
|
? Local::checkPasscode(passcode)
|
|
|
|
: (Local::readMap(passcode) != Local::ReadMapPassNeeded);
|
|
|
|
if (!correct) {
|
|
|
|
cSetPasscodeBadTries(cPasscodeBadTries() + 1);
|
2019-02-19 06:57:53 +00:00
|
|
|
cSetPasscodeLastTry(crl::now());
|
2018-06-07 07:44:38 +00:00
|
|
|
error();
|
|
|
|
return;
|
2018-06-03 13:30:40 +00:00
|
|
|
}
|
2018-06-07 07:44:38 +00:00
|
|
|
|
2019-01-21 13:42:21 +00:00
|
|
|
Core::App().unlockPasscode(); // Destroys this widget.
|
2018-06-03 13:30:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PasscodeLockWidget::error() {
|
2019-06-19 15:09:03 +00:00
|
|
|
_error = tr::lng_passcode_wrong(tr::now);
|
2018-06-03 13:30:40 +00:00
|
|
|
_passcode->selectAll();
|
|
|
|
_passcode->showError();
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PasscodeLockWidget::changed() {
|
|
|
|
if (!_error.isEmpty()) {
|
|
|
|
_error = QString();
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PasscodeLockWidget::resizeEvent(QResizeEvent *e) {
|
|
|
|
_passcode->move((width() - _passcode->width()) / 2, (height() / 3));
|
|
|
|
_submit->move(_passcode->x(), _passcode->y() + _passcode->height() + st::passcodeSubmitSkip);
|
|
|
|
_logout->move(_passcode->x() + (_passcode->width() - _logout->width()) / 2, _submit->y() + _submit->height() + st::linkFont->ascent);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PasscodeLockWidget::setInnerFocus() {
|
|
|
|
LockWidget::setInnerFocus();
|
|
|
|
_passcode->setFocusFast();
|
|
|
|
}
|
|
|
|
|
|
|
|
TermsLock TermsLock::FromMTP(const MTPDhelp_termsOfService &data) {
|
2019-07-05 13:38:38 +00:00
|
|
|
const auto minAge = data.vmin_age_confirm();
|
2018-06-03 13:30:40 +00:00
|
|
|
return {
|
2019-07-05 13:38:38 +00:00
|
|
|
bytes::make_vector(data.vid().c_dataJSON().vdata().v),
|
2018-06-03 13:30:40 +00:00
|
|
|
TextWithEntities {
|
2019-07-05 13:38:38 +00:00
|
|
|
TextUtilities::Clean(qs(data.vtext())),
|
2019-09-16 11:14:06 +00:00
|
|
|
Api::EntitiesFromMTP(data.ventities().v) },
|
2019-07-05 13:38:38 +00:00
|
|
|
(minAge ? std::make_optional(minAge->v) : std::nullopt),
|
2018-06-03 13:30:40 +00:00
|
|
|
data.is_popup()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
TermsBox::TermsBox(
|
|
|
|
QWidget*,
|
|
|
|
const TermsLock &data,
|
2019-06-18 16:53:27 +00:00
|
|
|
rpl::producer<QString> agree,
|
|
|
|
rpl::producer<QString> cancel)
|
2018-06-03 13:30:40 +00:00
|
|
|
: _data(data)
|
2019-06-18 16:53:27 +00:00
|
|
|
, _agree(std::move(agree))
|
|
|
|
, _cancel(std::move(cancel)) {
|
2018-06-03 13:30:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TermsBox::TermsBox(
|
|
|
|
QWidget*,
|
|
|
|
const TextWithEntities &text,
|
2019-06-18 16:53:27 +00:00
|
|
|
rpl::producer<QString> agree,
|
|
|
|
rpl::producer<QString> cancel,
|
2018-06-03 13:30:40 +00:00
|
|
|
bool attentionAgree)
|
2018-09-21 16:28:46 +00:00
|
|
|
: _data{ {}, text, std::nullopt, false }
|
2019-06-18 16:53:27 +00:00
|
|
|
, _agree(std::move(agree))
|
|
|
|
, _cancel(std::move(cancel))
|
2018-06-03 13:30:40 +00:00
|
|
|
, _attentionAgree(attentionAgree) {
|
|
|
|
}
|
|
|
|
|
|
|
|
rpl::producer<> TermsBox::agreeClicks() const {
|
|
|
|
return _agreeClicks.events();
|
|
|
|
}
|
|
|
|
|
|
|
|
rpl::producer<> TermsBox::cancelClicks() const {
|
|
|
|
return _cancelClicks.events();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TermsBox::prepare() {
|
2019-06-18 15:00:55 +00:00
|
|
|
setTitle(tr::lng_terms_header());
|
2018-06-03 13:30:40 +00:00
|
|
|
|
|
|
|
auto check = std::make_unique<Ui::CheckView>(st::defaultCheck, false);
|
|
|
|
const auto ageCheck = check.get();
|
|
|
|
const auto age = _data.minAge
|
|
|
|
? Ui::CreateChild<Ui::PaddingWrap<Ui::Checkbox>>(
|
|
|
|
this,
|
|
|
|
object_ptr<Ui::Checkbox>(
|
|
|
|
this,
|
2019-06-19 16:39:25 +00:00
|
|
|
tr::lng_terms_age(tr::now, lt_count, *_data.minAge),
|
2018-06-03 13:30:40 +00:00
|
|
|
st::defaultCheckbox,
|
|
|
|
std::move(check)),
|
|
|
|
st::termsAgePadding)
|
|
|
|
: nullptr;
|
|
|
|
if (age) {
|
|
|
|
age->resizeToNaturalWidth(st::boxWideWidth);
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto content = setInnerWidget(
|
|
|
|
object_ptr<Ui::PaddingWrap<Ui::FlatLabel>>(
|
|
|
|
this,
|
|
|
|
object_ptr<Ui::FlatLabel> (
|
|
|
|
this,
|
|
|
|
rpl::single(_data.text),
|
|
|
|
st::termsContent),
|
|
|
|
st::termsPadding),
|
|
|
|
0,
|
|
|
|
age ? age->height() : 0);
|
2018-06-22 19:46:20 +00:00
|
|
|
content->entity()->setClickHandlerFilter([=](
|
2018-06-03 13:30:40 +00:00
|
|
|
const ClickHandlerPtr &handler,
|
|
|
|
Qt::MouseButton button) {
|
|
|
|
const auto link = handler
|
|
|
|
? handler->copyToClipboardText()
|
|
|
|
: QString();
|
|
|
|
if (TextUtilities::RegExpMention().match(link).hasMatch()) {
|
|
|
|
_lastClickedMention = link;
|
2019-06-19 16:39:25 +00:00
|
|
|
Ui::Toast::Show(tr::lng_terms_agree_to_proceed(tr::now, lt_bot, link));
|
2018-06-03 13:30:40 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
const auto errorAnimationCallback = [=] {
|
|
|
|
const auto check = ageCheck;
|
2019-04-02 09:13:30 +00:00
|
|
|
const auto error = _ageErrorAnimation.value(
|
2018-06-03 13:30:40 +00:00
|
|
|
_ageErrorShown ? 1. : 0.);
|
|
|
|
if (error == 0.) {
|
2018-09-21 16:28:46 +00:00
|
|
|
check->setUntoggledOverride(std::nullopt);
|
2018-06-03 13:30:40 +00:00
|
|
|
} else {
|
|
|
|
const auto color = anim::color(
|
|
|
|
st::defaultCheck.untoggledFg,
|
|
|
|
st::boxTextFgError,
|
|
|
|
error);
|
|
|
|
check->setUntoggledOverride(color);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const auto toggleAgeError = [=](bool shown) {
|
|
|
|
if (_ageErrorShown != shown) {
|
|
|
|
_ageErrorShown = shown;
|
|
|
|
_ageErrorAnimation.start(
|
|
|
|
[=] { errorAnimationCallback(); },
|
|
|
|
_ageErrorShown ? 0. : 1.,
|
|
|
|
_ageErrorShown ? 1. : 0.,
|
|
|
|
st::defaultCheck.duration);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto &agreeStyle = _attentionAgree
|
|
|
|
? st::attentionBoxButton
|
|
|
|
: st::defaultBoxButton;
|
2019-06-18 16:53:27 +00:00
|
|
|
addButton(std::move(_agree), [=] {}, agreeStyle)->clicks(
|
2018-06-03 13:30:40 +00:00
|
|
|
) | rpl::filter([=] {
|
|
|
|
if (age && !age->entity()->checked()) {
|
|
|
|
toggleAgeError(true);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2018-09-13 20:09:26 +00:00
|
|
|
}) | rpl::map([] {
|
|
|
|
return rpl::empty_value();
|
2018-06-03 13:30:40 +00:00
|
|
|
}) | rpl::start_to_stream(_agreeClicks, lifetime());
|
|
|
|
|
|
|
|
if (_cancel) {
|
2019-06-18 16:53:27 +00:00
|
|
|
addButton(std::move(_cancel), [=] {})->clicks(
|
2018-09-13 20:09:26 +00:00
|
|
|
) | rpl::map([] {
|
|
|
|
return rpl::empty_value();
|
|
|
|
}) | rpl::start_to_stream(_cancelClicks, lifetime());
|
2018-06-03 13:30:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (age) {
|
2018-12-05 09:55:56 +00:00
|
|
|
age->entity()->checkedChanges(
|
2018-06-03 13:30:40 +00:00
|
|
|
) | rpl::start_with_next([=] {
|
|
|
|
toggleAgeError(false);
|
2018-12-05 09:55:56 +00:00
|
|
|
}, age->lifetime());
|
2018-06-03 13:30:40 +00:00
|
|
|
|
|
|
|
heightValue(
|
|
|
|
) | rpl::start_with_next([=](int height) {
|
|
|
|
age->moveToLeft(0, height - age->height());
|
|
|
|
}, age->lifetime());
|
|
|
|
}
|
|
|
|
|
|
|
|
content->resizeToWidth(st::boxWideWidth);
|
|
|
|
|
|
|
|
using namespace rpl::mappers;
|
|
|
|
rpl::combine(
|
|
|
|
content->heightValue(),
|
|
|
|
age ? age->heightValue() : rpl::single(0),
|
|
|
|
_1 + _2
|
|
|
|
) | rpl::start_with_next([=](int height) {
|
|
|
|
setDimensions(st::boxWideWidth, height);
|
|
|
|
}, content->lifetime());
|
|
|
|
}
|
|
|
|
|
|
|
|
void TermsBox::keyPressEvent(QKeyEvent *e) {
|
|
|
|
if (e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return) {
|
|
|
|
_agreeClicks.fire({});
|
|
|
|
} else {
|
|
|
|
BoxContent::keyPressEvent(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QString TermsBox::lastClickedMention() const {
|
|
|
|
return _lastClickedMention;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Window
|