2014-05-30 08:53:19 +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.
|
2014-05-30 08:53:19 +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
|
2014-05-30 08:53:19 +00:00
|
|
|
*/
|
2017-04-06 14:38:10 +00:00
|
|
|
#include "boxes/connection_box.h"
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2017-09-26 11:49:16 +00:00
|
|
|
#include "data/data_photo.h"
|
|
|
|
#include "data/data_document.h"
|
2017-06-27 20:11:38 +00:00
|
|
|
#include "boxes/confirm_box.h"
|
2017-04-13 08:27:10 +00:00
|
|
|
#include "lang/lang_keys.h"
|
2017-03-04 10:23:56 +00:00
|
|
|
#include "storage/localstorage.h"
|
2014-05-30 08:53:19 +00:00
|
|
|
#include "mainwidget.h"
|
2016-04-12 21:31:28 +00:00
|
|
|
#include "mainwindow.h"
|
2018-01-17 16:21:01 +00:00
|
|
|
#include "auth_session.h"
|
|
|
|
#include "data/data_session.h"
|
2016-11-11 13:46:04 +00:00
|
|
|
#include "ui/widgets/checkbox.h"
|
|
|
|
#include "ui/widgets/buttons.h"
|
2016-11-15 11:56:49 +00:00
|
|
|
#include "ui/widgets/input_fields.h"
|
2018-04-28 09:27:43 +00:00
|
|
|
#include "ui/wrap/fade_wrap.h"
|
2018-04-28 14:06:59 +00:00
|
|
|
#include "ui/wrap/padding_wrap.h"
|
2018-04-28 09:27:43 +00:00
|
|
|
#include "ui/wrap/vertical_layout.h"
|
|
|
|
#include "ui/text_options.h"
|
2016-09-27 13:37:18 +00:00
|
|
|
#include "history/history_location_manager.h"
|
2018-04-23 10:24:03 +00:00
|
|
|
#include "application.h"
|
2016-11-15 11:56:49 +00:00
|
|
|
#include "styles/style_boxes.h"
|
2018-04-28 09:27:43 +00:00
|
|
|
#include "styles/style_chat_helpers.h"
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2018-04-28 14:06:59 +00:00
|
|
|
constexpr auto kSaveSettingsDelayedTimeout = TimeMs(1000);
|
|
|
|
|
2018-04-28 09:27:43 +00:00
|
|
|
class ProxyRow : public Ui::RippleButton {
|
|
|
|
public:
|
|
|
|
using View = ProxiesBoxController::ItemView;
|
|
|
|
|
|
|
|
ProxyRow(QWidget *parent, View &&view);
|
|
|
|
|
|
|
|
void updateFields(View &&view);
|
|
|
|
|
|
|
|
rpl::producer<> deleteClicks() const;
|
|
|
|
rpl::producer<> restoreClicks() const;
|
|
|
|
rpl::producer<> editClicks() const;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
int resizeGetHeight(int newWidth) override;
|
|
|
|
|
|
|
|
void paintEvent(QPaintEvent *e) override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
void setupControls(View &&view);
|
|
|
|
int countAvailableWidth() const;
|
|
|
|
|
|
|
|
View _view;
|
|
|
|
|
|
|
|
Text _title;
|
|
|
|
object_ptr<Ui::FadeWrapScaled<Ui::IconButton>> _edit;
|
|
|
|
object_ptr<Ui::FadeWrapScaled<Ui::IconButton>> _delete;
|
|
|
|
object_ptr<Ui::FadeWrapScaled<Ui::RoundButton>> _restore;
|
|
|
|
int _skipLeft = 0;
|
|
|
|
int _skipRight = 0;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
class ProxiesBox : public BoxContent {
|
|
|
|
public:
|
|
|
|
using View = ProxiesBoxController::ItemView;
|
|
|
|
|
|
|
|
ProxiesBox(QWidget*, not_null<ProxiesBoxController*> controller);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void prepare() override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
void setupContent();
|
2018-04-28 14:06:59 +00:00
|
|
|
void addNewProxy();
|
2018-04-28 09:27:43 +00:00
|
|
|
void applyView(View &&view);
|
|
|
|
void setupButtons(int id, not_null<ProxyRow*> button);
|
|
|
|
|
|
|
|
not_null<ProxiesBoxController*> _controller;
|
2018-04-28 14:06:59 +00:00
|
|
|
object_ptr<Ui::PaddingWrap<Ui::Checkbox>> _useProxy;
|
|
|
|
object_ptr<Ui::PaddingWrap<Ui::Checkbox>> _tryIPv6;
|
|
|
|
object_ptr<Ui::VerticalLayout> _initialWrap;
|
|
|
|
QPointer<Ui::VerticalLayout> _wrap;
|
|
|
|
|
2018-04-28 09:27:43 +00:00
|
|
|
base::flat_map<int, QPointer<ProxyRow>> _rows;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
class ProxyBox : public BoxContent {
|
|
|
|
public:
|
|
|
|
ProxyBox(
|
|
|
|
QWidget*,
|
|
|
|
const ProxyData &data,
|
|
|
|
base::lambda<void(ProxyData)> callback);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void prepare() override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
base::lambda<void(ProxyData)> _callback;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
ProxyRow::ProxyRow(QWidget *parent, View &&view)
|
|
|
|
: RippleButton(parent, st::proxyRowRipple)
|
|
|
|
, _edit(this, object_ptr<Ui::IconButton>(this, st::proxyRowEdit))
|
|
|
|
, _delete(this, object_ptr<Ui::IconButton>(this, st::stickersRemove))
|
|
|
|
, _restore(
|
|
|
|
this,
|
|
|
|
object_ptr<Ui::RoundButton>(
|
|
|
|
this,
|
|
|
|
langFactory(lng_proxy_undo_delete),
|
|
|
|
st::stickersUndoRemove)) {
|
|
|
|
setupControls(std::move(view));
|
|
|
|
}
|
|
|
|
|
|
|
|
rpl::producer<> ProxyRow::deleteClicks() const {
|
|
|
|
return _delete->entity()->clicks();
|
|
|
|
}
|
|
|
|
|
|
|
|
rpl::producer<> ProxyRow::restoreClicks() const {
|
|
|
|
return _restore->entity()->clicks();
|
|
|
|
}
|
|
|
|
|
|
|
|
rpl::producer<> ProxyRow::editClicks() const {
|
|
|
|
return _edit->entity()->clicks();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProxyRow::setupControls(View &&view) {
|
|
|
|
updateFields(std::move(view));
|
|
|
|
|
|
|
|
_delete->finishAnimating();
|
|
|
|
_restore->finishAnimating();
|
|
|
|
_edit->finishAnimating();
|
|
|
|
}
|
|
|
|
|
|
|
|
int ProxyRow::countAvailableWidth() const {
|
|
|
|
return width() - _skipLeft - _skipRight;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProxyRow::updateFields(View &&view) {
|
|
|
|
_view = std::move(view);
|
|
|
|
const auto endpoint = _view.host + ':' + QString::number(_view.port);
|
|
|
|
_title.setText(
|
|
|
|
st::proxyRowTitleStyle,
|
|
|
|
_view.type + ' ' + textcmdLink(1, endpoint),
|
|
|
|
Ui::ItemTextDefaultOptions());
|
|
|
|
_delete->toggle(!_view.deleted, anim::type::instant);
|
|
|
|
_edit->toggle(!_view.deleted, anim::type::instant);
|
|
|
|
_restore->toggle(_view.deleted, anim::type::instant);
|
|
|
|
|
2018-04-28 14:06:59 +00:00
|
|
|
setPointerCursor(!_view.deleted);
|
|
|
|
|
2018-04-28 09:27:43 +00:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
int ProxyRow::resizeGetHeight(int newWidth) {
|
|
|
|
const auto result = st::proxyRowPadding.top()
|
|
|
|
+ st::semiboldFont->height
|
|
|
|
+ st::proxyRowSkip
|
|
|
|
+ st::normalFont->height
|
|
|
|
+ st::proxyRowPadding.bottom();
|
|
|
|
auto right = st::proxyRowPadding.right();
|
|
|
|
_delete->moveToRight(
|
|
|
|
right,
|
|
|
|
(result - _delete->height()) / 2,
|
|
|
|
newWidth);
|
|
|
|
_restore->moveToRight(
|
|
|
|
right,
|
|
|
|
(result - _restore->height()) / 2,
|
|
|
|
newWidth);
|
|
|
|
right += _delete->width();
|
|
|
|
_edit->moveToRight(
|
|
|
|
right,
|
|
|
|
(result - _edit->height()) / 2,
|
|
|
|
newWidth);
|
|
|
|
right -= _edit->width();
|
|
|
|
_skipRight = right;
|
|
|
|
_skipLeft = st::proxyRowPadding.left()
|
|
|
|
+ st::proxyRowIconSkip;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProxyRow::paintEvent(QPaintEvent *e) {
|
|
|
|
Painter p(this);
|
|
|
|
|
|
|
|
if (!_view.deleted) {
|
|
|
|
const auto ms = getms();
|
|
|
|
paintRipple(p, 0, 0, ms);
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto left = _skipLeft;
|
|
|
|
const auto availableWidth = countAvailableWidth();
|
|
|
|
auto top = st::proxyRowPadding.top();
|
|
|
|
|
|
|
|
if (_view.deleted) {
|
|
|
|
p.setOpacity(st::stickersRowDisabledOpacity);
|
|
|
|
} else if (_view.selected) {
|
|
|
|
st::proxyRowSelectedIcon.paint(
|
|
|
|
p,
|
|
|
|
st::proxyRowPadding.left(),
|
|
|
|
(height() - st::proxyRowSelectedIcon.height()) / 2,
|
|
|
|
width());
|
|
|
|
}
|
|
|
|
|
|
|
|
p.setPen(st::proxyRowTitleFg);
|
|
|
|
p.setFont(st::semiboldFont);
|
|
|
|
p.setTextPalette(st::proxyRowTitlePalette);
|
|
|
|
_title.drawLeftElided(p, left, top, availableWidth, width());
|
|
|
|
top += st::semiboldFont->height + st::proxyRowSkip;
|
|
|
|
|
|
|
|
const auto statusFg = [&] {
|
|
|
|
switch (_view.state) {
|
|
|
|
case View::State::Online:
|
|
|
|
return st::proxyRowStatusFgOnline;
|
|
|
|
case View::State::Unavailable:
|
|
|
|
return st::proxyRowStatusFgOffline;
|
|
|
|
default:
|
|
|
|
return st::proxyRowStatusFg;
|
|
|
|
}
|
|
|
|
}();
|
|
|
|
const auto status = [&] {
|
|
|
|
switch (_view.state) {
|
|
|
|
case View::State::Available:
|
|
|
|
return lang(lng_proxy_available);
|
|
|
|
case View::State::Checking:
|
|
|
|
return lang(lng_proxy_available);
|
|
|
|
case View::State::Connecting:
|
|
|
|
return lang(lng_proxy_connecting);
|
|
|
|
case View::State::Online:
|
|
|
|
return lang(lng_proxy_online);
|
|
|
|
case View::State::Unavailable:
|
|
|
|
return lang(lng_proxy_unavailable);
|
|
|
|
}
|
|
|
|
Unexpected("State in ProxyRow::paintEvent.");
|
|
|
|
}();
|
|
|
|
p.setPen(statusFg);
|
|
|
|
p.setFont(st::normalFont);
|
|
|
|
p.drawTextLeft(left, top, width(), status);
|
|
|
|
top += st::normalFont->height + st::proxyRowPadding.bottom();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
ProxiesBox::ProxiesBox(
|
|
|
|
QWidget*,
|
|
|
|
not_null<ProxiesBoxController*> controller)
|
|
|
|
: _controller(controller)
|
2018-04-28 14:06:59 +00:00
|
|
|
, _useProxy(
|
|
|
|
this,
|
|
|
|
object_ptr<Ui::Checkbox>(
|
|
|
|
this,
|
|
|
|
lang(lng_proxy_use),
|
|
|
|
Global::UseProxy()),
|
|
|
|
st::proxyUsePadding)
|
|
|
|
, _tryIPv6(
|
|
|
|
this,
|
|
|
|
object_ptr<Ui::Checkbox>(
|
|
|
|
this,
|
|
|
|
lang(lng_connection_try_ipv6),
|
|
|
|
Global::TryIPv6()),
|
|
|
|
st::proxyTryIPv6Padding)
|
|
|
|
, _initialWrap(this) {
|
2018-04-28 09:27:43 +00:00
|
|
|
_controller->views(
|
|
|
|
) | rpl::start_with_next([=](View &&view) {
|
|
|
|
applyView(std::move(view));
|
|
|
|
}, lifetime());
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProxiesBox::prepare() {
|
|
|
|
setTitle(langFactory(lng_proxy_settings));
|
|
|
|
|
2018-04-28 14:06:59 +00:00
|
|
|
addButton(langFactory(lng_proxy_add), [=] { addNewProxy(); });
|
|
|
|
addButton(langFactory(lng_close), [=] { closeBox(); });
|
2018-04-28 09:27:43 +00:00
|
|
|
|
|
|
|
setupContent();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProxiesBox::setupContent() {
|
2018-04-28 14:06:59 +00:00
|
|
|
_useProxy->resizeToWidth(st::boxWideWidth);
|
|
|
|
_useProxy->moveToLeft(0, 0);
|
|
|
|
subscribe(_useProxy->entity()->checkedChanged, [=](bool checked) {
|
|
|
|
if (!_controller->setProxyEnabled(checked)) {
|
|
|
|
addNewProxy();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
subscribe(Global::RefConnectionTypeChanged(), [=] {
|
|
|
|
_useProxy->entity()->setChecked(Global::UseProxy());
|
|
|
|
});
|
2018-04-28 09:27:43 +00:00
|
|
|
|
2018-04-28 14:06:59 +00:00
|
|
|
_tryIPv6->resizeToWidth(st::boxWideWidth);
|
|
|
|
|
|
|
|
const auto topSkip = _useProxy->heightNoMargins();
|
|
|
|
const auto bottomSkip = _tryIPv6->heightNoMargins();
|
|
|
|
const auto inner = setInnerWidget(
|
|
|
|
object_ptr<Ui::VerticalLayout>(this),
|
|
|
|
topSkip,
|
|
|
|
bottomSkip);
|
|
|
|
inner->add(object_ptr<Ui::FixedHeightWidget>(
|
|
|
|
inner,
|
|
|
|
st::proxyRowPadding.top()));
|
|
|
|
_wrap = inner->add(std::move(_initialWrap));
|
|
|
|
inner->add(object_ptr<Ui::FixedHeightWidget>(
|
|
|
|
inner,
|
|
|
|
st::proxyRowPadding.bottom()));
|
|
|
|
|
|
|
|
inner->resizeToWidth(st::boxWideWidth);
|
|
|
|
|
|
|
|
inner->heightValue(
|
|
|
|
) | rpl::map([=](int height) {
|
|
|
|
return std::min(
|
|
|
|
topSkip + height + bottomSkip,
|
|
|
|
st::boxMaxListHeight);
|
2018-04-28 09:27:43 +00:00
|
|
|
}) | rpl::distinct_until_changed(
|
|
|
|
) | rpl::start_with_next([=](int height) {
|
|
|
|
setDimensions(st::boxWideWidth, height);
|
2018-04-28 14:06:59 +00:00
|
|
|
}, inner->lifetime());
|
|
|
|
|
|
|
|
heightValue(
|
|
|
|
) | rpl::start_with_next([=](int height) {
|
|
|
|
_tryIPv6->moveToLeft(0, height - _tryIPv6->heightNoMargins());
|
|
|
|
}, _tryIPv6->lifetime());
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProxiesBox::addNewProxy() {
|
|
|
|
Ui::show(_controller->addNewItemBox(), LayerOption::KeepOther);
|
2018-04-28 09:27:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ProxiesBox::applyView(View &&view) {
|
|
|
|
const auto id = view.id;
|
|
|
|
const auto i = _rows.find(id);
|
|
|
|
if (i == _rows.end()) {
|
2018-04-28 14:06:59 +00:00
|
|
|
const auto wrap = _wrap
|
|
|
|
? _wrap.data()
|
|
|
|
: _initialWrap.data();
|
|
|
|
const auto [i, ok] = _rows.emplace(id, wrap->insert(
|
|
|
|
0,
|
2018-04-28 09:27:43 +00:00
|
|
|
object_ptr<ProxyRow>(
|
2018-04-28 14:06:59 +00:00
|
|
|
wrap,
|
2018-04-28 09:27:43 +00:00
|
|
|
std::move(view))));
|
|
|
|
setupButtons(id, i->second);
|
|
|
|
} else {
|
|
|
|
i->second->updateFields(std::move(view));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProxiesBox::setupButtons(int id, not_null<ProxyRow*> button) {
|
|
|
|
button->deleteClicks(
|
|
|
|
) | rpl::start_with_next([=] {
|
|
|
|
_controller->deleteItem(id);
|
|
|
|
}, button->lifetime());
|
|
|
|
|
|
|
|
button->restoreClicks(
|
|
|
|
) | rpl::start_with_next([=] {
|
|
|
|
_controller->restoreItem(id);
|
|
|
|
}, button->lifetime());
|
|
|
|
|
|
|
|
button->editClicks(
|
|
|
|
) | rpl::start_with_next([=] {
|
|
|
|
Ui::show(_controller->editItemBox(id), LayerOption::KeepOther);
|
|
|
|
}, button->lifetime());
|
2018-04-28 14:06:59 +00:00
|
|
|
|
|
|
|
button->clicks(
|
|
|
|
) | rpl::start_with_next([=] {
|
|
|
|
_controller->applyItem(id);
|
|
|
|
}, button->lifetime());
|
2018-04-28 09:27:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ProxyBox::ProxyBox(
|
|
|
|
QWidget*,
|
|
|
|
const ProxyData &data,
|
|
|
|
base::lambda<void(ProxyData)> callback)
|
|
|
|
: _callback(std::move(callback)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProxyBox::prepare() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2018-04-27 16:16:50 +00:00
|
|
|
void ConnectionBox::ShowApplyProxyConfirmation(
|
|
|
|
ProxyData::Type type,
|
|
|
|
const QMap<QString, QString> &fields) {
|
|
|
|
const auto server = fields.value(qsl("server"));
|
|
|
|
const auto port = fields.value(qsl("port")).toUInt();
|
2018-04-27 17:26:45 +00:00
|
|
|
auto proxy = ProxyData();
|
|
|
|
proxy.type = type;
|
|
|
|
proxy.host = server;
|
|
|
|
proxy.port = port;
|
|
|
|
if (type == ProxyData::Type::Socks5) {
|
|
|
|
proxy.user = fields.value(qsl("user"));
|
|
|
|
proxy.password = fields.value(qsl("pass"));
|
|
|
|
} else if (type == ProxyData::Type::Mtproto) {
|
|
|
|
proxy.password = fields.value(qsl("secret"));
|
|
|
|
}
|
|
|
|
if (proxy) {
|
2018-04-23 10:24:03 +00:00
|
|
|
const auto box = std::make_shared<QPointer<ConfirmBox>>();
|
2018-04-27 16:16:50 +00:00
|
|
|
const auto text = lng_sure_enable_socks(
|
|
|
|
lt_server,
|
|
|
|
server,
|
|
|
|
lt_port,
|
|
|
|
QString::number(port));
|
|
|
|
*box = Ui::show(Box<ConfirmBox>(text, lang(lng_sure_enable), [=] {
|
2018-04-27 17:26:45 +00:00
|
|
|
auto &proxies = Global::RefProxiesList();
|
|
|
|
if (ranges::find(proxies, proxy) == end(proxies)) {
|
2018-04-28 14:06:59 +00:00
|
|
|
proxies.push_back(proxy);
|
2018-04-27 16:16:50 +00:00
|
|
|
}
|
2018-04-27 17:26:45 +00:00
|
|
|
Global::SetSelectedProxy(proxy);
|
|
|
|
Global::SetUseProxy(true);
|
2017-06-27 20:11:38 +00:00
|
|
|
Local::writeSettings();
|
2018-04-23 10:24:03 +00:00
|
|
|
Sandbox::refreshGlobalProxy();
|
2017-06-27 20:11:38 +00:00
|
|
|
Global::RefConnectionTypeChanged().notify();
|
|
|
|
MTP::restart();
|
2018-04-23 10:24:03 +00:00
|
|
|
if (const auto strong = box->data()) {
|
|
|
|
strong->closeBox();
|
|
|
|
}
|
2017-09-15 17:34:41 +00:00
|
|
|
}), LayerOption::KeepOther);
|
2017-06-27 20:11:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-13 17:07:56 +00:00
|
|
|
ConnectionBox::ConnectionBox(QWidget *parent)
|
2018-04-27 17:26:45 +00:00
|
|
|
: _hostInput(this, st::connectionHostInputField, langFactory(lng_connection_host_ph), Global::SelectedProxy().host)
|
|
|
|
, _portInput(this, st::connectionPortInputField, langFactory(lng_connection_port_ph), QString::number(Global::SelectedProxy().port))
|
|
|
|
, _userInput(this, st::connectionUserInputField, langFactory(lng_connection_user_ph), Global::SelectedProxy().user)
|
|
|
|
, _passwordInput(this, st::connectionPasswordInputField, langFactory(lng_connection_password_ph), Global::SelectedProxy().password)
|
|
|
|
, _typeGroup(std::make_shared<Ui::RadioenumGroup<Type>>(Global::SelectedProxy().type))
|
|
|
|
, _autoRadio(this, _typeGroup, Type::None, lang(lng_connection_auto_rb), st::defaultBoxCheckbox)
|
|
|
|
, _httpProxyRadio(this, _typeGroup, Type::Http, lang(lng_connection_http_proxy_rb), st::defaultBoxCheckbox)
|
|
|
|
, _tcpProxyRadio(this, _typeGroup, Type::Socks5, lang(lng_connection_tcp_proxy_rb), st::defaultBoxCheckbox)
|
2016-12-13 17:07:56 +00:00
|
|
|
, _tryIPv6(this, lang(lng_connection_try_ipv6), Global::TryIPv6(), st::defaultBoxCheckbox) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConnectionBox::prepare() {
|
2017-05-30 15:21:05 +00:00
|
|
|
setTitle(langFactory(lng_connection_header));
|
2015-10-06 19:49:23 +00:00
|
|
|
|
2017-05-30 15:21:05 +00:00
|
|
|
addButton(langFactory(lng_connection_save), [this] { onSave(); });
|
|
|
|
addButton(langFactory(lng_cancel), [this] { closeBox(); });
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2018-04-27 17:26:45 +00:00
|
|
|
_typeGroup->setChangedCallback([this](Type value) { typeChanged(value); });
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2016-10-28 09:20:24 +00:00
|
|
|
connect(_hostInput, SIGNAL(submitted(bool)), this, SLOT(onSubmit()));
|
|
|
|
connect(_portInput, SIGNAL(submitted(bool)), this, SLOT(onSubmit()));
|
|
|
|
connect(_userInput, SIGNAL(submitted(bool)), this, SLOT(onSubmit()));
|
|
|
|
connect(_passwordInput, SIGNAL(submitted(bool)), this, SLOT(onSubmit()));
|
2017-06-27 22:03:37 +00:00
|
|
|
connect(_hostInput, SIGNAL(focused()), this, SLOT(onFieldFocus()));
|
|
|
|
connect(_portInput, SIGNAL(focused()), this, SLOT(onFieldFocus()));
|
|
|
|
connect(_userInput, SIGNAL(focused()), this, SLOT(onFieldFocus()));
|
|
|
|
connect(_passwordInput, SIGNAL(focused()), this, SLOT(onFieldFocus()));
|
2015-10-06 19:49:23 +00:00
|
|
|
|
2016-11-18 13:34:58 +00:00
|
|
|
updateControlsVisibility();
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2017-06-27 22:03:37 +00:00
|
|
|
bool ConnectionBox::badProxyValue() const {
|
|
|
|
return (_hostInput->getLastText().isEmpty() || !_portInput->getLastText().toInt());
|
|
|
|
}
|
|
|
|
|
2016-11-18 13:34:58 +00:00
|
|
|
void ConnectionBox::updateControlsVisibility() {
|
2017-08-05 11:43:59 +00:00
|
|
|
auto newHeight = st::boxOptionListPadding.top() + _autoRadio->heightNoMargins() + st::boxOptionListSkip + _httpProxyRadio->heightNoMargins() + st::boxOptionListSkip + _tcpProxyRadio->heightNoMargins() + st::boxOptionListSkip + st::connectionIPv6Skip + _tryIPv6->heightNoMargins() + st::defaultCheckbox.margin.bottom() + st::boxOptionListPadding.bottom() + st::boxPadding.bottom();
|
2018-04-24 19:09:20 +00:00
|
|
|
if (!proxyFieldsVisible()) {
|
2017-03-18 21:06:10 +00:00
|
|
|
_hostInput->hide();
|
|
|
|
_portInput->hide();
|
|
|
|
_userInput->hide();
|
|
|
|
_passwordInput->hide();
|
|
|
|
} else {
|
2016-12-13 17:07:56 +00:00
|
|
|
newHeight += 2 * st::boxOptionInputSkip + 2 * _hostInput->height();
|
2016-10-28 09:20:24 +00:00
|
|
|
_hostInput->show();
|
|
|
|
_portInput->show();
|
|
|
|
_userInput->show();
|
|
|
|
_passwordInput->show();
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2016-12-13 17:07:56 +00:00
|
|
|
setDimensions(st::boxWidth, newHeight);
|
|
|
|
updateControlsPosition();
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2018-04-24 19:09:20 +00:00
|
|
|
bool ConnectionBox::proxyFieldsVisible() const {
|
2018-04-27 17:26:45 +00:00
|
|
|
return (_typeGroup->value() == ProxyData::Type::Http
|
|
|
|
|| _typeGroup->value() == ProxyData::Type::Socks5);
|
2018-04-24 19:09:20 +00:00
|
|
|
}
|
|
|
|
|
2016-12-13 17:07:56 +00:00
|
|
|
void ConnectionBox::setInnerFocus() {
|
2018-04-27 17:26:45 +00:00
|
|
|
if (proxyFieldsVisible()) {
|
2016-12-20 13:03:51 +00:00
|
|
|
_hostInput->setFocusFast();
|
2018-04-27 17:26:45 +00:00
|
|
|
} else {
|
|
|
|
setFocus();
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-02 10:33:19 +00:00
|
|
|
void ConnectionBox::resizeEvent(QResizeEvent *e) {
|
2016-12-13 17:07:56 +00:00
|
|
|
BoxContent::resizeEvent(e);
|
|
|
|
|
|
|
|
updateControlsPosition();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConnectionBox::updateControlsPosition() {
|
2017-03-18 21:06:10 +00:00
|
|
|
auto type = _typeGroup->value();
|
2017-07-04 13:59:00 +00:00
|
|
|
_autoRadio->moveToLeft(st::boxPadding.left() + st::boxOptionListPadding.left(), _autoRadio->getMargins().top() + st::boxOptionListPadding.top());
|
2016-12-13 17:07:56 +00:00
|
|
|
_httpProxyRadio->moveToLeft(st::boxPadding.left() + st::boxOptionListPadding.left(), _autoRadio->bottomNoMargins() + st::boxOptionListSkip);
|
2015-04-02 10:33:19 +00:00
|
|
|
|
2017-03-18 21:06:10 +00:00
|
|
|
auto inputy = 0;
|
2018-04-24 19:09:20 +00:00
|
|
|
auto fieldsVisible = proxyFieldsVisible();
|
2018-04-27 17:26:45 +00:00
|
|
|
auto fieldsBelowHttp = fieldsVisible && (type == ProxyData::Type::Http);
|
|
|
|
auto fieldsBelowTcp = fieldsVisible && (type == ProxyData::Type::Socks5);
|
2017-06-27 22:03:37 +00:00
|
|
|
if (fieldsBelowHttp) {
|
2016-12-09 18:56:01 +00:00
|
|
|
inputy = _httpProxyRadio->bottomNoMargins() + st::boxOptionInputSkip;
|
2016-12-13 17:07:56 +00:00
|
|
|
_tcpProxyRadio->moveToLeft(st::boxPadding.left() + st::boxOptionListPadding.left(), inputy + st::boxOptionInputSkip + 2 * _hostInput->height() + st::boxOptionListSkip);
|
2014-05-30 08:53:19 +00:00
|
|
|
} else {
|
2016-12-13 17:07:56 +00:00
|
|
|
_tcpProxyRadio->moveToLeft(st::boxPadding.left() + st::boxOptionListPadding.left(), _httpProxyRadio->bottomNoMargins() + st::boxOptionListSkip);
|
2017-06-27 22:03:37 +00:00
|
|
|
if (fieldsBelowTcp) {
|
2016-12-09 18:56:01 +00:00
|
|
|
inputy = _tcpProxyRadio->bottomNoMargins() + st::boxOptionInputSkip;
|
2015-04-02 10:33:19 +00:00
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2015-04-02 10:33:19 +00:00
|
|
|
|
|
|
|
if (inputy) {
|
2017-07-07 11:27:13 +00:00
|
|
|
_hostInput->moveToLeft(st::boxPadding.left() + st::boxOptionListPadding.left() + st::defaultCheck.diameter + st::defaultBoxCheckbox.textPosition.x() - st::defaultInputField.textMargins.left(), inputy);
|
2016-10-28 09:20:24 +00:00
|
|
|
_portInput->moveToRight(st::boxPadding.right(), inputy);
|
2017-07-07 11:27:13 +00:00
|
|
|
_userInput->moveToLeft(st::boxPadding.left() + st::boxOptionListPadding.left() + st::defaultCheck.diameter + st::defaultBoxCheckbox.textPosition.x() - st::defaultInputField.textMargins.left(), _hostInput->y() + _hostInput->height() + st::boxOptionInputSkip);
|
2016-10-28 09:20:24 +00:00
|
|
|
_passwordInput->moveToRight(st::boxPadding.right(), _userInput->y());
|
2015-04-02 10:33:19 +00:00
|
|
|
}
|
|
|
|
|
2017-06-27 22:03:37 +00:00
|
|
|
auto tryipv6y = (fieldsBelowTcp ? _userInput->bottomNoMargins() : _tcpProxyRadio->bottomNoMargins()) + st::boxOptionListSkip + st::connectionIPv6Skip;
|
2016-10-28 09:20:24 +00:00
|
|
|
_tryIPv6->moveToLeft(st::boxPadding.left() + st::boxOptionListPadding.left(), tryipv6y);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2018-04-27 17:26:45 +00:00
|
|
|
void ConnectionBox::typeChanged(Type type) {
|
|
|
|
if (!proxyFieldsVisible()) {
|
2017-06-27 22:03:37 +00:00
|
|
|
setFocus();
|
|
|
|
}
|
2016-11-18 13:34:58 +00:00
|
|
|
updateControlsVisibility();
|
2018-04-27 17:26:45 +00:00
|
|
|
if (proxyFieldsVisible()) {
|
2016-12-20 13:03:51 +00:00
|
|
|
if (!_hostInput->hasFocus() && !_portInput->hasFocus() && !_userInput->hasFocus() && !_passwordInput->hasFocus()) {
|
|
|
|
_hostInput->setFocusFast();
|
|
|
|
}
|
2018-04-27 17:26:45 +00:00
|
|
|
if ((type == Type::Http) && !_portInput->getLastText().toInt()) {
|
2016-10-28 09:20:24 +00:00
|
|
|
_portInput->setText(qsl("80"));
|
2017-09-30 19:20:40 +00:00
|
|
|
_portInput->finishAnimating();
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2017-06-27 22:03:37 +00:00
|
|
|
void ConnectionBox::onFieldFocus() {
|
|
|
|
}
|
|
|
|
|
2015-10-06 19:49:23 +00:00
|
|
|
void ConnectionBox::onSubmit() {
|
2017-06-27 22:03:37 +00:00
|
|
|
onFieldFocus();
|
2016-10-28 09:20:24 +00:00
|
|
|
if (_hostInput->hasFocus()) {
|
|
|
|
if (!_hostInput->getLastText().trimmed().isEmpty()) {
|
|
|
|
_portInput->setFocus();
|
2015-10-06 19:49:23 +00:00
|
|
|
} else {
|
2016-10-28 09:20:24 +00:00
|
|
|
_hostInput->showError();
|
2015-10-06 19:49:23 +00:00
|
|
|
}
|
2016-10-28 09:20:24 +00:00
|
|
|
} else if (_portInput->hasFocus()) {
|
|
|
|
if (_portInput->getLastText().trimmed().toInt() > 0) {
|
|
|
|
_userInput->setFocus();
|
2015-10-06 19:49:23 +00:00
|
|
|
} else {
|
2016-10-28 09:20:24 +00:00
|
|
|
_portInput->showError();
|
2015-10-06 19:49:23 +00:00
|
|
|
}
|
2016-10-28 09:20:24 +00:00
|
|
|
} else if (_userInput->hasFocus()) {
|
|
|
|
_passwordInput->setFocus();
|
|
|
|
} else if (_passwordInput->hasFocus()) {
|
|
|
|
if (_hostInput->getLastText().trimmed().isEmpty()) {
|
|
|
|
_hostInput->setFocus();
|
|
|
|
_hostInput->showError();
|
|
|
|
} else if (_portInput->getLastText().trimmed().toInt() <= 0) {
|
|
|
|
_portInput->setFocus();
|
|
|
|
_portInput->showError();
|
2015-10-06 19:49:23 +00:00
|
|
|
} else {
|
|
|
|
onSave();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-30 08:53:19 +00:00
|
|
|
void ConnectionBox::onSave() {
|
2018-04-23 10:24:03 +00:00
|
|
|
auto proxy = ProxyData();
|
|
|
|
proxy.host = _hostInput->getLastText().trimmed();
|
|
|
|
proxy.user = _userInput->getLastText().trimmed();
|
|
|
|
proxy.password = _passwordInput->getLastText().trimmed();
|
|
|
|
proxy.port = _portInput->getLastText().toUInt();
|
2017-06-27 22:03:37 +00:00
|
|
|
|
2017-03-18 21:06:10 +00:00
|
|
|
auto type = _typeGroup->value();
|
2018-04-27 17:26:45 +00:00
|
|
|
if (type == Type::None) {
|
|
|
|
proxy = ProxyData();
|
|
|
|
} else if (type == Type::Mtproto) {
|
|
|
|
proxy = Global::SelectedProxy();
|
2017-03-18 21:06:10 +00:00
|
|
|
} else {
|
2018-04-23 10:24:03 +00:00
|
|
|
if (proxy.host.isEmpty()) {
|
2017-06-27 22:03:37 +00:00
|
|
|
_hostInput->showError();
|
2014-05-30 08:53:19 +00:00
|
|
|
return;
|
2018-04-23 10:24:03 +00:00
|
|
|
} else if (!proxy.port) {
|
2017-06-27 22:03:37 +00:00
|
|
|
_portInput->showError();
|
2014-05-30 08:53:19 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-04-27 17:26:45 +00:00
|
|
|
proxy.type = type;
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2018-04-27 17:26:45 +00:00
|
|
|
Global::SetSelectedProxy(proxy ? proxy : ProxyData());
|
|
|
|
Global::SetUseProxy(proxy ? true : false);
|
2016-10-28 09:20:24 +00:00
|
|
|
if (cPlatform() == dbipWindows && Global::TryIPv6() != _tryIPv6->checked()) {
|
|
|
|
Global::SetTryIPv6(_tryIPv6->checked());
|
2015-06-25 18:04:40 +00:00
|
|
|
Local::writeSettings();
|
2016-08-27 04:49:18 +00:00
|
|
|
Global::RefConnectionTypeChanged().notify();
|
|
|
|
|
2016-10-28 12:44:28 +00:00
|
|
|
App::restart();
|
2015-06-25 18:04:40 +00:00
|
|
|
} else {
|
2016-10-28 09:20:24 +00:00
|
|
|
Global::SetTryIPv6(_tryIPv6->checked());
|
2015-06-25 18:04:40 +00:00
|
|
|
Local::writeSettings();
|
2018-04-23 10:24:03 +00:00
|
|
|
Sandbox::refreshGlobalProxy();
|
2016-08-27 04:49:18 +00:00
|
|
|
Global::RefConnectionTypeChanged().notify();
|
|
|
|
|
2015-06-25 18:04:40 +00:00
|
|
|
MTP::restart();
|
2016-12-13 17:07:56 +00:00
|
|
|
closeBox();
|
2015-06-25 18:04:40 +00:00
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2015-12-23 21:19:57 +00:00
|
|
|
|
2016-12-13 17:07:56 +00:00
|
|
|
AutoDownloadBox::AutoDownloadBox(QWidget *parent)
|
|
|
|
: _photoPrivate(this, lang(lng_media_auto_private_chats), !(cAutoDownloadPhoto() & dbiadNoPrivate), st::defaultBoxCheckbox)
|
2016-06-10 11:18:55 +00:00
|
|
|
, _photoGroups(this, lang(lng_media_auto_groups), !(cAutoDownloadPhoto() & dbiadNoGroups), st::defaultBoxCheckbox)
|
|
|
|
, _audioPrivate(this, lang(lng_media_auto_private_chats), !(cAutoDownloadAudio() & dbiadNoPrivate), st::defaultBoxCheckbox)
|
|
|
|
, _audioGroups(this, lang(lng_media_auto_groups), !(cAutoDownloadAudio() & dbiadNoGroups), st::defaultBoxCheckbox)
|
|
|
|
, _gifPrivate(this, lang(lng_media_auto_private_chats), !(cAutoDownloadGif() & dbiadNoPrivate), st::defaultBoxCheckbox)
|
|
|
|
, _gifGroups(this, lang(lng_media_auto_groups), !(cAutoDownloadGif() & dbiadNoGroups), st::defaultBoxCheckbox)
|
|
|
|
, _gifPlay(this, lang(lng_media_auto_play), cAutoPlayGif(), st::defaultBoxCheckbox)
|
2017-07-12 13:53:42 +00:00
|
|
|
, _sectionHeight(st::boxTitleHeight + 2 * (st::defaultCheck.diameter + st::setLittleSkip)) {
|
2016-12-13 17:07:56 +00:00
|
|
|
}
|
2015-12-23 21:19:57 +00:00
|
|
|
|
2016-12-13 17:07:56 +00:00
|
|
|
void AutoDownloadBox::prepare() {
|
2017-05-30 15:21:05 +00:00
|
|
|
addButton(langFactory(lng_connection_save), [this] { onSave(); });
|
|
|
|
addButton(langFactory(lng_cancel), [this] { closeBox(); });
|
2015-12-23 21:19:57 +00:00
|
|
|
|
2016-12-20 13:03:51 +00:00
|
|
|
setDimensions(st::boxWidth, 3 * _sectionHeight - st::autoDownloadTopDelta + st::setLittleSkip + _gifPlay->heightNoMargins() + st::setLittleSkip);
|
2015-12-23 21:19:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AutoDownloadBox::paintEvent(QPaintEvent *e) {
|
2016-12-13 17:07:56 +00:00
|
|
|
BoxContent::paintEvent(e);
|
2016-11-19 14:47:28 +00:00
|
|
|
|
2015-12-23 21:19:57 +00:00
|
|
|
Painter p(this);
|
|
|
|
|
2016-12-13 17:07:56 +00:00
|
|
|
p.setPen(st::boxTitleFg);
|
|
|
|
p.setFont(st::autoDownloadTitleFont);
|
|
|
|
p.drawTextLeft(st::autoDownloadTitlePosition.x(), st::autoDownloadTitlePosition.y(), width(), lang(lng_media_auto_photo));
|
|
|
|
p.drawTextLeft(st::autoDownloadTitlePosition.x(), _sectionHeight + st::autoDownloadTitlePosition.y(), width(), lang(lng_media_auto_audio));
|
|
|
|
p.drawTextLeft(st::autoDownloadTitlePosition.x(), 2 * _sectionHeight + st::autoDownloadTitlePosition.y(), width(), lang(lng_media_auto_gif));
|
2015-12-23 21:19:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AutoDownloadBox::resizeEvent(QResizeEvent *e) {
|
2016-12-13 17:07:56 +00:00
|
|
|
BoxContent::resizeEvent(e);
|
|
|
|
|
2016-12-20 13:03:51 +00:00
|
|
|
auto top = st::boxTitleHeight - st::autoDownloadTopDelta;
|
|
|
|
_photoPrivate->moveToLeft(st::boxTitlePosition.x(), top + st::setLittleSkip);
|
2016-12-02 19:16:35 +00:00
|
|
|
_photoGroups->moveToLeft(st::boxTitlePosition.x(), _photoPrivate->bottomNoMargins() + st::setLittleSkip);
|
2015-12-23 21:19:57 +00:00
|
|
|
|
2016-12-20 13:03:51 +00:00
|
|
|
_audioPrivate->moveToLeft(st::boxTitlePosition.x(), _sectionHeight + top + st::setLittleSkip);
|
2016-12-02 19:16:35 +00:00
|
|
|
_audioGroups->moveToLeft(st::boxTitlePosition.x(), _audioPrivate->bottomNoMargins() + st::setLittleSkip);
|
2015-12-23 21:19:57 +00:00
|
|
|
|
2016-12-20 13:03:51 +00:00
|
|
|
_gifPrivate->moveToLeft(st::boxTitlePosition.x(), 2 * _sectionHeight + top + st::setLittleSkip);
|
2016-12-02 19:16:35 +00:00
|
|
|
_gifGroups->moveToLeft(st::boxTitlePosition.x(), _gifPrivate->bottomNoMargins() + st::setLittleSkip);
|
|
|
|
_gifPlay->moveToLeft(st::boxTitlePosition.x(), _gifGroups->bottomNoMargins() + st::setLittleSkip);
|
2015-12-23 21:19:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AutoDownloadBox::onSave() {
|
2018-01-17 16:21:01 +00:00
|
|
|
auto photosChanged = false;
|
|
|
|
auto documentsChanged = false;
|
|
|
|
auto autoplayChanged = false;
|
|
|
|
auto photosEnabled = false;
|
|
|
|
auto voiceEnabled = false;
|
|
|
|
auto animationsEnabled = false;
|
|
|
|
auto autoDownloadPhoto = (_photoPrivate->checked() ? 0 : dbiadNoPrivate)
|
|
|
|
| (_photoGroups->checked() ? 0 : dbiadNoGroups);
|
2015-12-27 21:37:48 +00:00
|
|
|
if (cAutoDownloadPhoto() != autoDownloadPhoto) {
|
2018-01-17 16:21:01 +00:00
|
|
|
const auto enabledPrivate = (cAutoDownloadPhoto() & dbiadNoPrivate)
|
|
|
|
&& !(autoDownloadPhoto & dbiadNoPrivate);
|
|
|
|
const auto enabledGroups = (cAutoDownloadPhoto() & dbiadNoGroups)
|
|
|
|
&& !(autoDownloadPhoto & dbiadNoGroups);
|
|
|
|
photosEnabled = enabledPrivate || enabledGroups;
|
|
|
|
photosChanged = true;
|
2015-12-27 21:37:48 +00:00
|
|
|
cSetAutoDownloadPhoto(autoDownloadPhoto);
|
|
|
|
}
|
2018-01-17 16:21:01 +00:00
|
|
|
auto autoDownloadAudio = (_audioPrivate->checked() ? 0 : dbiadNoPrivate)
|
|
|
|
| (_audioGroups->checked() ? 0 : dbiadNoGroups);
|
2015-12-27 21:37:48 +00:00
|
|
|
if (cAutoDownloadAudio() != autoDownloadAudio) {
|
2018-01-17 16:21:01 +00:00
|
|
|
const auto enabledPrivate = (cAutoDownloadAudio() & dbiadNoPrivate)
|
|
|
|
&& !(autoDownloadAudio & dbiadNoPrivate);
|
|
|
|
const auto enabledGroups = (cAutoDownloadAudio() & dbiadNoGroups)
|
|
|
|
&& !(autoDownloadAudio & dbiadNoGroups);
|
|
|
|
voiceEnabled = enabledPrivate || enabledGroups;
|
|
|
|
documentsChanged = true;
|
2015-12-27 21:37:48 +00:00
|
|
|
cSetAutoDownloadAudio(autoDownloadAudio);
|
|
|
|
}
|
2018-01-17 16:21:01 +00:00
|
|
|
auto autoDownloadGif = (_gifPrivate->checked() ? 0 : dbiadNoPrivate)
|
|
|
|
| (_gifGroups->checked() ? 0 : dbiadNoGroups);
|
2015-12-27 21:37:48 +00:00
|
|
|
if (cAutoDownloadGif() != autoDownloadGif) {
|
2018-01-17 16:21:01 +00:00
|
|
|
const auto enabledPrivate = (cAutoDownloadGif() & dbiadNoPrivate)
|
|
|
|
&& !(autoDownloadGif & dbiadNoPrivate);
|
|
|
|
const auto enabledGroups = (cAutoDownloadGif() & dbiadNoGroups)
|
|
|
|
&& !(autoDownloadGif & dbiadNoGroups);
|
|
|
|
animationsEnabled = enabledPrivate || enabledGroups;
|
|
|
|
documentsChanged = true;
|
2015-12-27 21:37:48 +00:00
|
|
|
cSetAutoDownloadGif(autoDownloadGif);
|
|
|
|
}
|
2016-10-28 09:20:24 +00:00
|
|
|
if (cAutoPlayGif() != _gifPlay->checked()) {
|
|
|
|
cSetAutoPlayGif(_gifPlay->checked());
|
2015-12-27 21:37:48 +00:00
|
|
|
if (!cAutoPlayGif()) {
|
2018-01-17 16:21:01 +00:00
|
|
|
Auth().data().stopAutoplayAnimations();
|
2015-12-27 21:37:48 +00:00
|
|
|
}
|
2018-01-17 16:21:01 +00:00
|
|
|
autoplayChanged = true;
|
|
|
|
}
|
|
|
|
if (photosChanged || documentsChanged || autoplayChanged) {
|
|
|
|
Local::writeUserSettings();
|
|
|
|
}
|
|
|
|
if (photosEnabled) {
|
|
|
|
Auth().data().photoLoadSettingsChanged();
|
|
|
|
}
|
|
|
|
if (voiceEnabled) {
|
|
|
|
Auth().data().voiceLoadSettingsChanged();
|
|
|
|
}
|
|
|
|
if (animationsEnabled) {
|
|
|
|
Auth().data().animationLoadSettingsChanged();
|
2015-12-27 21:37:48 +00:00
|
|
|
}
|
2016-12-13 17:07:56 +00:00
|
|
|
closeBox();
|
2015-12-23 21:19:57 +00:00
|
|
|
}
|
2018-04-28 09:27:43 +00:00
|
|
|
|
2018-04-28 14:06:59 +00:00
|
|
|
ProxiesBoxController::ProxiesBoxController()
|
|
|
|
: _saveTimer([] { Local::writeSettings(); }) {
|
2018-04-28 09:27:43 +00:00
|
|
|
_list = ranges::view::all(
|
|
|
|
Global::ProxiesList()
|
|
|
|
) | ranges::view::transform([&](const ProxyData &proxy) {
|
|
|
|
return Item{ ++_idCounter, proxy };
|
|
|
|
}) | ranges::to_vector;
|
|
|
|
}
|
|
|
|
|
|
|
|
object_ptr<BoxContent> ProxiesBoxController::CreateOwningBox() {
|
|
|
|
auto controller = std::make_unique<ProxiesBoxController>();
|
|
|
|
auto box = controller->create();
|
|
|
|
Ui::AttachAsChild(box, std::move(controller));
|
|
|
|
return box;
|
|
|
|
}
|
|
|
|
|
|
|
|
object_ptr<BoxContent> ProxiesBoxController::create() {
|
|
|
|
auto result = Box<ProxiesBox>(this);
|
2018-04-28 14:06:59 +00:00
|
|
|
for (const auto &item : base::reversed(_list)) {
|
2018-04-28 09:27:43 +00:00
|
|
|
updateView(item);
|
|
|
|
}
|
|
|
|
return std::move(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto ProxiesBoxController::findById(int id) -> std::vector<Item>::iterator {
|
|
|
|
const auto result = ranges::find(
|
|
|
|
_list,
|
|
|
|
id,
|
|
|
|
[](const Item &item) { return item.id; });
|
|
|
|
Assert(result != end(_list));
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-04-28 14:06:59 +00:00
|
|
|
auto ProxiesBoxController::findByProxy(const ProxyData &proxy)
|
|
|
|
->std::vector<Item>::iterator {
|
|
|
|
return ranges::find(
|
|
|
|
_list,
|
|
|
|
proxy,
|
|
|
|
[](const Item &item) { return item.data; });
|
|
|
|
}
|
|
|
|
|
2018-04-28 09:27:43 +00:00
|
|
|
void ProxiesBoxController::deleteItem(int id) {
|
|
|
|
setDeleted(id, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProxiesBoxController::restoreItem(int id) {
|
|
|
|
setDeleted(id, false);
|
|
|
|
}
|
|
|
|
|
2018-04-28 14:06:59 +00:00
|
|
|
void ProxiesBoxController::applyItem(int id) {
|
|
|
|
auto item = findById(id);
|
|
|
|
if (Global::UseProxy() && Global::SelectedProxy() == item->data) {
|
|
|
|
return;
|
|
|
|
} else if (item->deleted) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto j = findByProxy(Global::SelectedProxy());
|
|
|
|
|
|
|
|
Global::SetSelectedProxy(item->data);
|
|
|
|
Global::SetUseProxy(true);
|
|
|
|
applyChanges();
|
|
|
|
|
|
|
|
if (j != end(_list)) {
|
|
|
|
updateView(*j);
|
|
|
|
}
|
|
|
|
updateView(*item);
|
|
|
|
}
|
|
|
|
|
2018-04-28 09:27:43 +00:00
|
|
|
void ProxiesBoxController::setDeleted(int id, bool deleted) {
|
|
|
|
auto item = findById(id);
|
|
|
|
item->deleted = deleted;
|
2018-04-28 14:06:59 +00:00
|
|
|
|
|
|
|
if (deleted) {
|
|
|
|
auto &proxies = Global::RefProxiesList();
|
|
|
|
proxies.erase(ranges::remove(proxies, item->data), end(proxies));
|
|
|
|
|
|
|
|
if (item->data == Global::SelectedProxy()) {
|
|
|
|
_lastSelectedProxy = base::take(Global::RefSelectedProxy());
|
|
|
|
if (Global::UseProxy()) {
|
|
|
|
_lastSelectedProxyUsed = true;
|
|
|
|
Global::SetUseProxy(false);
|
|
|
|
applyChanges();
|
|
|
|
} else {
|
|
|
|
_lastSelectedProxyUsed = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
auto &proxies = Global::RefProxiesList();
|
|
|
|
if (ranges::find(proxies, item->data) == end(proxies)) {
|
|
|
|
auto insertBefore = item + 1;
|
|
|
|
while (insertBefore != end(_list) && insertBefore->deleted) {
|
|
|
|
++insertBefore;
|
|
|
|
}
|
|
|
|
auto insertBeforeIt = (insertBefore == end(_list))
|
|
|
|
? end(proxies)
|
|
|
|
: ranges::find(proxies, insertBefore->data);
|
|
|
|
proxies.insert(insertBeforeIt, item->data);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Global::SelectedProxy() && _lastSelectedProxy == item->data) {
|
|
|
|
Assert(!Global::UseProxy());
|
|
|
|
|
|
|
|
Global::SetSelectedProxy(base::take(_lastSelectedProxy));
|
|
|
|
if (base::take(_lastSelectedProxyUsed)) {
|
|
|
|
Global::SetUseProxy(true);
|
|
|
|
applyChanges();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
saveDelayed();
|
2018-04-28 09:27:43 +00:00
|
|
|
updateView(*item);
|
|
|
|
}
|
|
|
|
|
|
|
|
object_ptr<BoxContent> ProxiesBoxController::editItemBox(int id) {
|
|
|
|
return Box<ProxyBox>(findById(id)->data, [=](const ProxyData &result) {
|
|
|
|
auto i = findById(id);
|
|
|
|
auto j = ranges::find(
|
|
|
|
_list,
|
|
|
|
result,
|
|
|
|
[](const Item &item) { return item.data; });
|
|
|
|
if (j != end(_list) && j != i) {
|
|
|
|
_views.fire({ i->id });
|
|
|
|
_list.erase(i);
|
|
|
|
if (j->deleted) {
|
|
|
|
restoreItem(j->id);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
i->data = result;
|
|
|
|
if (i->deleted) {
|
|
|
|
restoreItem(i->id);
|
|
|
|
} else {
|
|
|
|
updateView(*i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
object_ptr<BoxContent> ProxiesBoxController::addNewItemBox() {
|
|
|
|
return Box<ProxyBox>(ProxyData(), [=](const ProxyData &result) {
|
|
|
|
auto j = ranges::find(
|
|
|
|
_list,
|
|
|
|
result,
|
|
|
|
[](const Item &item) { return item.data; });
|
|
|
|
if (j != end(_list)) {
|
|
|
|
if (j->deleted) {
|
|
|
|
restoreItem(j->id);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
_list.push_back({ ++_idCounter, result });
|
|
|
|
updateView(_list.back());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-04-28 14:06:59 +00:00
|
|
|
bool ProxiesBoxController::setProxyEnabled(bool enabled) {
|
|
|
|
if (enabled) {
|
|
|
|
if (Global::ProxiesList().empty()) {
|
|
|
|
return false;
|
|
|
|
} else if (!Global::SelectedProxy()) {
|
|
|
|
Global::SetSelectedProxy(Global::ProxiesList().back());
|
|
|
|
auto j = findByProxy(Global::SelectedProxy());
|
|
|
|
if (j != end(_list)) {
|
|
|
|
updateView(*j);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Global::SetUseProxy(enabled);
|
|
|
|
applyChanges();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProxiesBoxController::applyChanges() {
|
|
|
|
Sandbox::refreshGlobalProxy();
|
|
|
|
Global::RefConnectionTypeChanged().notify();
|
|
|
|
MTP::restart();
|
|
|
|
saveDelayed();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProxiesBoxController::saveDelayed() {
|
|
|
|
_saveTimer.callOnce(kSaveSettingsDelayedTimeout);
|
|
|
|
}
|
|
|
|
|
2018-04-28 09:27:43 +00:00
|
|
|
auto ProxiesBoxController::views() const -> rpl::producer<ItemView> {
|
|
|
|
return _views.events();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProxiesBoxController::updateView(const Item &item) {
|
|
|
|
const auto state = ItemView::State::Checking;
|
|
|
|
const auto ping = 0;
|
|
|
|
const auto selected = (Global::SelectedProxy() == item.data);
|
|
|
|
const auto deleted = item.deleted;
|
|
|
|
const auto type = [&] {
|
|
|
|
switch (item.data.type) {
|
|
|
|
case ProxyData::Type::Http:
|
|
|
|
return qsl("HTTP");
|
|
|
|
case ProxyData::Type::Socks5:
|
|
|
|
return qsl("SOCKS5");
|
|
|
|
case ProxyData::Type::Mtproto:
|
|
|
|
return qsl("MTPROTO");
|
|
|
|
}
|
|
|
|
Unexpected("Proxy type in ProxiesBoxController::updateView.");
|
|
|
|
}();
|
|
|
|
_views.fire({
|
|
|
|
item.id,
|
|
|
|
type,
|
|
|
|
item.data.host,
|
|
|
|
item.data.port,
|
|
|
|
ping,
|
|
|
|
!deleted && selected,
|
|
|
|
deleted,
|
|
|
|
state });
|
|
|
|
}
|
2018-04-28 14:06:59 +00:00
|
|
|
|
|
|
|
ProxiesBoxController::~ProxiesBoxController() {
|
|
|
|
if (_saveTimer.isActive()) {
|
|
|
|
App::CallDelayed(
|
|
|
|
kSaveSettingsDelayedTimeout,
|
|
|
|
QApplication::instance(),
|
|
|
|
[] { Local::writeSettings(); });
|
|
|
|
}
|
|
|
|
}
|