2014-05-30 12:53:19 +04:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 13:23:14 +03:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2014-05-30 12:53:19 +04:00
|
|
|
|
2018-01-03 13:23:14 +03:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2014-05-30 12:53:19 +04:00
|
|
|
*/
|
2017-04-06 17:38:10 +03:00
|
|
|
#include "boxes/connection_box.h"
|
2014-05-30 12:53:19 +04:00
|
|
|
|
2017-09-26 14:49:16 +03:00
|
|
|
#include "data/data_photo.h"
|
|
|
|
#include "data/data_document.h"
|
2017-06-27 23:11:38 +03:00
|
|
|
#include "boxes/confirm_box.h"
|
2017-04-13 11:27:10 +03:00
|
|
|
#include "lang/lang_keys.h"
|
2017-03-04 13:23:56 +03:00
|
|
|
#include "storage/localstorage.h"
|
2018-04-28 19:58:22 +04:00
|
|
|
#include "base/qthelp_url.h"
|
2014-05-30 12:53:19 +04:00
|
|
|
#include "mainwidget.h"
|
2018-04-30 19:49:03 +04:00
|
|
|
#include "messenger.h"
|
2016-04-13 00:31:28 +03:00
|
|
|
#include "mainwindow.h"
|
2018-01-17 19:21:01 +03:00
|
|
|
#include "auth_session.h"
|
|
|
|
#include "data/data_session.h"
|
2018-04-30 19:49:03 +04:00
|
|
|
#include "mtproto/connection.h"
|
2016-11-11 16:46:04 +03:00
|
|
|
#include "ui/widgets/checkbox.h"
|
|
|
|
#include "ui/widgets/buttons.h"
|
2016-11-15 14:56:49 +03:00
|
|
|
#include "ui/widgets/input_fields.h"
|
2018-04-28 19:58:22 +04:00
|
|
|
#include "ui/widgets/labels.h"
|
2018-05-05 16:14:46 +03:00
|
|
|
#include "ui/widgets/dropdown_menu.h"
|
2018-04-28 13:27:43 +04:00
|
|
|
#include "ui/wrap/fade_wrap.h"
|
2018-04-28 18:06:59 +04:00
|
|
|
#include "ui/wrap/padding_wrap.h"
|
2018-04-28 19:58:22 +04:00
|
|
|
#include "ui/wrap/slide_wrap.h"
|
2018-04-28 13:27:43 +04:00
|
|
|
#include "ui/wrap/vertical_layout.h"
|
2018-04-28 19:58:22 +04:00
|
|
|
#include "ui/toast/toast.h"
|
2018-05-03 18:05:18 +03:00
|
|
|
#include "ui/effects/radial_animation.h"
|
2018-04-28 13:27:43 +04:00
|
|
|
#include "ui/text_options.h"
|
2016-09-27 16:37:18 +03:00
|
|
|
#include "history/history_location_manager.h"
|
2018-04-23 14:24:03 +04:00
|
|
|
#include "application.h"
|
2016-11-15 14:56:49 +03:00
|
|
|
#include "styles/style_boxes.h"
|
2018-04-28 13:27:43 +04:00
|
|
|
#include "styles/style_chat_helpers.h"
|
2018-05-05 16:14:46 +03:00
|
|
|
#include "styles/style_info.h"
|
2018-04-28 13:27:43 +04:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2018-04-28 18:06:59 +04:00
|
|
|
constexpr auto kSaveSettingsDelayedTimeout = TimeMs(1000);
|
|
|
|
|
2018-04-28 13:27:43 +04:00
|
|
|
class ProxyRow : public Ui::RippleButton {
|
|
|
|
public:
|
|
|
|
using View = ProxiesBoxController::ItemView;
|
2018-04-30 19:49:03 +04:00
|
|
|
using State = ProxiesBoxController::ItemState;
|
2018-04-28 13:27:43 +04:00
|
|
|
|
|
|
|
ProxyRow(QWidget *parent, View &&view);
|
|
|
|
|
|
|
|
void updateFields(View &&view);
|
|
|
|
|
|
|
|
rpl::producer<> deleteClicks() const;
|
|
|
|
rpl::producer<> restoreClicks() const;
|
|
|
|
rpl::producer<> editClicks() const;
|
2018-05-05 16:14:46 +03:00
|
|
|
rpl::producer<> shareClicks() const;
|
2018-04-28 13:27:43 +04:00
|
|
|
|
|
|
|
protected:
|
|
|
|
int resizeGetHeight(int newWidth) override;
|
|
|
|
|
|
|
|
void paintEvent(QPaintEvent *e) override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
void setupControls(View &&view);
|
|
|
|
int countAvailableWidth() const;
|
2018-05-03 18:05:18 +03:00
|
|
|
void step_radial(TimeMs ms, bool timer);
|
2018-05-04 21:28:26 +03:00
|
|
|
void paintCheck(Painter &p, TimeMs ms);
|
2018-05-05 16:14:46 +03:00
|
|
|
void showMenu();
|
2018-04-28 13:27:43 +04:00
|
|
|
|
|
|
|
View _view;
|
|
|
|
|
|
|
|
Text _title;
|
2018-05-05 16:14:46 +03:00
|
|
|
object_ptr<Ui::IconButton> _menuToggle;
|
|
|
|
rpl::event_stream<> _deleteClicks;
|
|
|
|
rpl::event_stream<> _restoreClicks;
|
|
|
|
rpl::event_stream<> _editClicks;
|
|
|
|
rpl::event_stream<> _shareClicks;
|
|
|
|
base::unique_qptr<Ui::DropdownMenu> _menu;
|
2018-05-04 21:28:26 +03:00
|
|
|
|
|
|
|
bool _set = false;
|
|
|
|
Animation _toggled;
|
|
|
|
Animation _setAnimation;
|
2018-05-03 18:05:18 +03:00
|
|
|
std::unique_ptr<Ui::InfiniteRadialAnimation> _progress;
|
2018-05-04 21:28:26 +03:00
|
|
|
std::unique_ptr<Ui::InfiniteRadialAnimation> _checking;
|
|
|
|
|
2018-04-28 13:27:43 +04:00
|
|
|
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 19:58:22 +04:00
|
|
|
void createNoRowsLabel();
|
2018-04-28 18:06:59 +04:00
|
|
|
void addNewProxy();
|
2018-04-28 13:27:43 +04:00
|
|
|
void applyView(View &&view);
|
|
|
|
void setupButtons(int id, not_null<ProxyRow*> button);
|
2018-04-28 19:58:22 +04:00
|
|
|
int rowHeight() const;
|
2018-05-07 00:29:53 +03:00
|
|
|
void refreshProxyForCalls();
|
2018-04-28 13:27:43 +04:00
|
|
|
|
|
|
|
not_null<ProxiesBoxController*> _controller;
|
2018-05-07 00:29:53 +03:00
|
|
|
QPointer<Ui::Checkbox> _tryIPv6;
|
|
|
|
QPointer<Ui::Checkbox> _useProxy;
|
|
|
|
QPointer<Ui::SlideWrap<Ui::Checkbox>> _proxyForCalls;
|
|
|
|
QPointer<Ui::DividerLabel> _about;
|
2018-04-28 19:58:22 +04:00
|
|
|
base::unique_qptr<Ui::RpWidget> _noRows;
|
2018-04-28 18:06:59 +04:00
|
|
|
object_ptr<Ui::VerticalLayout> _initialWrap;
|
|
|
|
QPointer<Ui::VerticalLayout> _wrap;
|
2018-05-07 00:29:53 +03:00
|
|
|
int _currentProxySupportsCallsId = 0;
|
2018-04-28 18:06:59 +04:00
|
|
|
|
2018-04-28 19:58:22 +04:00
|
|
|
base::flat_map<int, base::unique_qptr<ProxyRow>> _rows;
|
2018-04-28 13:27:43 +04:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
class ProxyBox : public BoxContent {
|
|
|
|
public:
|
|
|
|
ProxyBox(
|
|
|
|
QWidget*,
|
|
|
|
const ProxyData &data,
|
2018-05-05 16:14:46 +03:00
|
|
|
base::lambda<void(ProxyData)> callback,
|
|
|
|
base::lambda<void(ProxyData)> shareCallback);
|
2018-04-28 13:27:43 +04:00
|
|
|
|
|
|
|
protected:
|
|
|
|
void prepare() override;
|
|
|
|
|
|
|
|
private:
|
2018-04-28 19:58:22 +04:00
|
|
|
using Type = ProxyData::Type;
|
|
|
|
|
|
|
|
void refreshButtons();
|
|
|
|
ProxyData collectData();
|
|
|
|
void save();
|
|
|
|
void share();
|
|
|
|
void setupControls(const ProxyData &data);
|
|
|
|
void setupTypes();
|
|
|
|
void setupSocketAddress(const ProxyData &data);
|
|
|
|
void setupCredentials(const ProxyData &data);
|
|
|
|
void setupMtprotoCredentials(const ProxyData &data);
|
|
|
|
|
|
|
|
void addLabel(
|
|
|
|
not_null<Ui::VerticalLayout*> parent,
|
|
|
|
const QString &text) const;
|
|
|
|
|
2018-04-28 13:27:43 +04:00
|
|
|
base::lambda<void(ProxyData)> _callback;
|
2018-05-05 16:14:46 +03:00
|
|
|
base::lambda<void(ProxyData)> _shareCallback;
|
2018-04-28 13:27:43 +04:00
|
|
|
|
2018-04-28 19:58:22 +04:00
|
|
|
object_ptr<Ui::VerticalLayout> _content;
|
|
|
|
|
|
|
|
std::shared_ptr<Ui::RadioenumGroup<Type>> _type;
|
|
|
|
|
|
|
|
QPointer<Ui::InputField> _host;
|
|
|
|
QPointer<Ui::PortInput> _port;
|
|
|
|
QPointer<Ui::InputField> _user;
|
|
|
|
QPointer<Ui::PasswordInput> _password;
|
|
|
|
QPointer<Ui::HexInput> _secret;
|
|
|
|
|
|
|
|
QPointer<Ui::SlideWrap<Ui::VerticalLayout>> _credentials;
|
|
|
|
QPointer<Ui::SlideWrap<Ui::VerticalLayout>> _mtprotoCredentials;
|
|
|
|
|
2018-04-28 13:27:43 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
ProxyRow::ProxyRow(QWidget *parent, View &&view)
|
|
|
|
: RippleButton(parent, st::proxyRowRipple)
|
2018-05-05 16:14:46 +03:00
|
|
|
, _menuToggle(this, st::topBarMenuToggle) {
|
2018-04-28 13:27:43 +04:00
|
|
|
setupControls(std::move(view));
|
|
|
|
}
|
|
|
|
|
|
|
|
rpl::producer<> ProxyRow::deleteClicks() const {
|
2018-05-05 16:14:46 +03:00
|
|
|
return _deleteClicks.events();
|
2018-04-28 13:27:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
rpl::producer<> ProxyRow::restoreClicks() const {
|
2018-05-05 16:14:46 +03:00
|
|
|
return _restoreClicks.events();
|
2018-04-28 13:27:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
rpl::producer<> ProxyRow::editClicks() const {
|
2018-05-05 16:14:46 +03:00
|
|
|
return _editClicks.events();
|
|
|
|
}
|
|
|
|
|
|
|
|
rpl::producer<> ProxyRow::shareClicks() const {
|
|
|
|
return _shareClicks.events();
|
2018-04-28 13:27:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void ProxyRow::setupControls(View &&view) {
|
|
|
|
updateFields(std::move(view));
|
2018-05-04 21:28:26 +03:00
|
|
|
_toggled.finish();
|
|
|
|
_setAnimation.finish();
|
2018-04-28 13:27:43 +04:00
|
|
|
|
2018-05-05 16:14:46 +03:00
|
|
|
_menuToggle->addClickHandler([=] { showMenu(); });
|
2018-04-28 13:27:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int ProxyRow::countAvailableWidth() const {
|
|
|
|
return width() - _skipLeft - _skipRight;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProxyRow::updateFields(View &&view) {
|
2018-05-04 21:28:26 +03:00
|
|
|
if (_view.selected != view.selected) {
|
|
|
|
_toggled.start(
|
|
|
|
[=] { update(); },
|
|
|
|
view.selected ? 0. : 1.,
|
|
|
|
view.selected ? 1. : 0.,
|
|
|
|
st::defaultRadio.duration);
|
|
|
|
}
|
2018-04-28 13:27:43 +04:00
|
|
|
_view = std::move(view);
|
|
|
|
const auto endpoint = _view.host + ':' + QString::number(_view.port);
|
|
|
|
_title.setText(
|
|
|
|
st::proxyRowTitleStyle,
|
|
|
|
_view.type + ' ' + textcmdLink(1, endpoint),
|
|
|
|
Ui::ItemTextDefaultOptions());
|
|
|
|
|
2018-05-03 18:05:18 +03:00
|
|
|
const auto state = _view.state;
|
2018-05-04 21:28:26 +03:00
|
|
|
if (state == State::Connecting) {
|
2018-05-03 18:05:18 +03:00
|
|
|
if (!_progress) {
|
|
|
|
_progress = std::make_unique<Ui::InfiniteRadialAnimation>(
|
2018-05-04 19:57:50 +03:00
|
|
|
animation(this, &ProxyRow::step_radial),
|
|
|
|
st::proxyCheckingAnimation);
|
2018-05-04 21:28:26 +03:00
|
|
|
}
|
|
|
|
_progress->start();
|
|
|
|
} else if (_progress) {
|
|
|
|
_progress->stop();
|
|
|
|
}
|
|
|
|
if (state == State::Checking) {
|
|
|
|
if (!_checking) {
|
|
|
|
_checking = std::make_unique<Ui::InfiniteRadialAnimation>(
|
|
|
|
animation(this, &ProxyRow::step_radial),
|
|
|
|
st::proxyCheckingAnimation);
|
|
|
|
_checking->start();
|
2018-05-03 18:05:18 +03:00
|
|
|
}
|
|
|
|
} else {
|
2018-05-04 21:28:26 +03:00
|
|
|
_checking = nullptr;
|
|
|
|
}
|
|
|
|
const auto set = (state == State::Connecting || state == State::Online);
|
|
|
|
if (_set != set) {
|
|
|
|
_set = set;
|
|
|
|
_setAnimation.start(
|
|
|
|
[=] { update(); },
|
|
|
|
_set ? 0. : 1.,
|
|
|
|
_set ? 1. : 0.,
|
|
|
|
st::defaultRadio.duration);
|
2018-05-03 18:05:18 +03:00
|
|
|
}
|
|
|
|
|
2018-04-28 18:06:59 +04:00
|
|
|
setPointerCursor(!_view.deleted);
|
|
|
|
|
2018-04-28 13:27:43 +04:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2018-05-03 18:05:18 +03:00
|
|
|
void ProxyRow::step_radial(TimeMs ms, bool timer) {
|
|
|
|
if (timer) {
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-28 13:27:43 +04:00
|
|
|
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();
|
2018-05-05 16:14:46 +03:00
|
|
|
_menuToggle->moveToRight(
|
2018-04-28 13:27:43 +04:00
|
|
|
right,
|
2018-05-05 16:14:46 +03:00
|
|
|
(result - _menuToggle->height()) / 2,
|
2018-04-28 13:27:43 +04:00
|
|
|
newWidth);
|
2018-05-05 16:14:46 +03:00
|
|
|
right += _menuToggle->width();
|
2018-04-28 13:27:43 +04:00
|
|
|
_skipRight = right;
|
|
|
|
_skipLeft = st::proxyRowPadding.left()
|
|
|
|
+ st::proxyRowIconSkip;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProxyRow::paintEvent(QPaintEvent *e) {
|
|
|
|
Painter p(this);
|
|
|
|
|
2018-05-03 18:05:18 +03:00
|
|
|
const auto ms = getms();
|
2018-04-28 13:27:43 +04:00
|
|
|
if (!_view.deleted) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2018-05-04 21:28:26 +03:00
|
|
|
paintCheck(p, ms);
|
|
|
|
|
2018-04-28 13:27:43 +04:00
|
|
|
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) {
|
2018-04-30 19:49:03 +04:00
|
|
|
case State::Online:
|
2018-04-28 13:27:43 +04:00
|
|
|
return st::proxyRowStatusFgOnline;
|
2018-04-30 19:49:03 +04:00
|
|
|
case State::Unavailable:
|
2018-04-28 13:27:43 +04:00
|
|
|
return st::proxyRowStatusFgOffline;
|
2018-04-30 19:49:03 +04:00
|
|
|
case State::Available:
|
|
|
|
return st::proxyRowStatusFgAvailable;
|
2018-04-28 13:27:43 +04:00
|
|
|
default:
|
|
|
|
return st::proxyRowStatusFg;
|
|
|
|
}
|
|
|
|
}();
|
|
|
|
const auto status = [&] {
|
|
|
|
switch (_view.state) {
|
2018-04-30 19:49:03 +04:00
|
|
|
case State::Available:
|
|
|
|
return lng_proxy_available(
|
|
|
|
lt_ping,
|
|
|
|
QString::number(_view.ping));
|
|
|
|
case State::Checking:
|
|
|
|
return lang(lng_proxy_checking);
|
|
|
|
case State::Connecting:
|
2018-04-28 13:27:43 +04:00
|
|
|
return lang(lng_proxy_connecting);
|
2018-04-30 19:49:03 +04:00
|
|
|
case State::Online:
|
2018-04-28 13:27:43 +04:00
|
|
|
return lang(lng_proxy_online);
|
2018-04-30 19:49:03 +04:00
|
|
|
case State::Unavailable:
|
2018-04-28 13:27:43 +04:00
|
|
|
return lang(lng_proxy_unavailable);
|
|
|
|
}
|
|
|
|
Unexpected("State in ProxyRow::paintEvent.");
|
|
|
|
}();
|
2018-05-05 16:14:46 +03:00
|
|
|
p.setPen(_view.deleted ? st::proxyRowStatusFg : statusFg);
|
2018-04-28 13:27:43 +04:00
|
|
|
p.setFont(st::normalFont);
|
|
|
|
|
2018-05-03 18:05:18 +03:00
|
|
|
auto statusLeft = left;
|
2018-05-04 21:28:26 +03:00
|
|
|
if (_checking) {
|
|
|
|
_checking->step(ms);
|
|
|
|
if (_checking) {
|
|
|
|
_checking->draw(
|
2018-05-03 18:05:18 +03:00
|
|
|
p,
|
|
|
|
{
|
|
|
|
st::proxyCheckingPosition.x() + statusLeft,
|
2018-05-04 21:28:26 +03:00
|
|
|
st::proxyCheckingPosition.y() + top
|
|
|
|
},
|
2018-05-04 19:57:50 +03:00
|
|
|
width());
|
2018-05-03 18:05:18 +03:00
|
|
|
statusLeft += st::proxyCheckingPosition.x()
|
|
|
|
+ st::proxyCheckingAnimation.size.width()
|
|
|
|
+ st::proxyCheckingSkip;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p.drawTextLeft(statusLeft, top, width(), status);
|
|
|
|
top += st::normalFont->height + st::proxyRowPadding.bottom();
|
2018-04-28 13:27:43 +04:00
|
|
|
}
|
|
|
|
|
2018-05-04 21:28:26 +03:00
|
|
|
void ProxyRow::paintCheck(Painter &p, TimeMs ms) {
|
|
|
|
if (_progress) {
|
|
|
|
_progress->step(ms);
|
|
|
|
}
|
|
|
|
const auto loading = _progress
|
|
|
|
? _progress->computeState()
|
|
|
|
: Ui::InfiniteRadialAnimation::State{ 0., 0, FullArcLength };
|
|
|
|
const auto toggled = _toggled.current(ms, _view.selected ? 1. : 0.)
|
|
|
|
* (1. - loading.shown);
|
|
|
|
const auto _st = &st::defaultRadio;
|
|
|
|
const auto set = _setAnimation.current(ms, _set ? 1. : 0.);
|
|
|
|
|
|
|
|
PainterHighQualityEnabler hq(p);
|
|
|
|
|
|
|
|
const auto left = st::proxyRowPadding.left();
|
|
|
|
const auto top = (height() - _st->diameter - _st->thickness) / 2;
|
|
|
|
const auto outerWidth = width();
|
|
|
|
|
|
|
|
auto pen = anim::pen(_st->untoggledFg, _st->toggledFg, toggled * set);
|
|
|
|
pen.setWidth(_st->thickness);
|
|
|
|
p.setPen(pen);
|
|
|
|
p.setBrush(_st->bg);
|
|
|
|
const auto rect = rtlrect(QRectF(left, top, _st->diameter, _st->diameter).marginsRemoved(QMarginsF(_st->thickness / 2., _st->thickness / 2., _st->thickness / 2., _st->thickness / 2.)), outerWidth);
|
|
|
|
if (loading.arcLength < FullArcLength) {
|
|
|
|
p.drawArc(rect, loading.arcFrom, loading.arcLength);
|
|
|
|
} else {
|
|
|
|
p.drawEllipse(rect);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (toggled > 0) {
|
|
|
|
p.setPen(Qt::NoPen);
|
|
|
|
p.setBrush(anim::brush(_st->untoggledFg, _st->toggledFg, toggled * set));
|
|
|
|
|
|
|
|
auto skip0 = _st->diameter / 2., skip1 = _st->skip / 10., checkSkip = skip0 * (1. - toggled) + skip1 * toggled;
|
|
|
|
p.drawEllipse(rtlrect(QRectF(left, top, _st->diameter, _st->diameter).marginsRemoved(QMarginsF(checkSkip, checkSkip, checkSkip, checkSkip)), outerWidth));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-05 16:14:46 +03:00
|
|
|
void ProxyRow::showMenu() {
|
|
|
|
if (_menu) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_menu = base::make_unique_q<Ui::DropdownMenu>(window());
|
|
|
|
const auto weak = _menu.get();
|
|
|
|
_menu->setHiddenCallback([=] {
|
|
|
|
weak->deleteLater();
|
|
|
|
if (_menu == weak) {
|
|
|
|
_menuToggle->setForceRippled(false);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
_menu->setShowStartCallback([=] {
|
|
|
|
if (_menu == weak) {
|
|
|
|
_menuToggle->setForceRippled(true);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
_menu->setHideStartCallback([=] {
|
|
|
|
if (_menu == weak) {
|
|
|
|
_menuToggle->setForceRippled(false);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
_menuToggle->installEventFilter(_menu);
|
|
|
|
const auto addAction = [&](
|
|
|
|
const QString &text,
|
|
|
|
base::lambda<void()> callback) {
|
|
|
|
return _menu->addAction(text, std::move(callback));
|
|
|
|
};
|
|
|
|
addAction(lang(lng_proxy_menu_edit), [=] {
|
|
|
|
_editClicks.fire({});
|
|
|
|
});
|
2018-05-07 00:29:53 +03:00
|
|
|
if (_view.supportsShare) {
|
2018-05-05 16:14:46 +03:00
|
|
|
addAction(lang(lng_proxy_edit_share), [=] {
|
|
|
|
_shareClicks.fire({});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (_view.deleted) {
|
|
|
|
addAction(lang(lng_proxy_menu_restore), [=] {
|
|
|
|
_restoreClicks.fire({});
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
addAction(lang(lng_proxy_menu_delete), [=] {
|
|
|
|
_deleteClicks.fire({});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
const auto parentTopLeft = window()->mapToGlobal({ 0, 0 });
|
|
|
|
const auto buttonTopLeft = _menuToggle->mapToGlobal({ 0, 0 });
|
|
|
|
const auto parent = QRect(parentTopLeft, window()->size());
|
|
|
|
const auto button = QRect(buttonTopLeft, _menuToggle->size());
|
|
|
|
const auto bottom = button.y()
|
|
|
|
+ st::proxyDropdownDownPosition.y()
|
|
|
|
+ _menu->height()
|
|
|
|
- parent.y();
|
|
|
|
const auto top = button.y()
|
|
|
|
+ st::proxyDropdownUpPosition.y()
|
|
|
|
- _menu->height()
|
|
|
|
- parent.y();
|
|
|
|
if (bottom > parent.height() && top >= 0) {
|
|
|
|
const auto left = button.x()
|
|
|
|
+ button.width()
|
|
|
|
+ st::proxyDropdownUpPosition.x()
|
|
|
|
- _menu->width()
|
|
|
|
- parent.x();
|
|
|
|
_menu->move(left, top);
|
|
|
|
_menu->showAnimated(Ui::PanelAnimation::Origin::BottomRight);
|
|
|
|
} else {
|
|
|
|
const auto left = button.x()
|
|
|
|
+ button.width()
|
|
|
|
+ st::proxyDropdownDownPosition.x()
|
|
|
|
- _menu->width()
|
|
|
|
- parent.x();
|
|
|
|
_menu->move(left, bottom - _menu->height());
|
|
|
|
_menu->showAnimated(Ui::PanelAnimation::Origin::TopRight);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-28 13:27:43 +04:00
|
|
|
ProxiesBox::ProxiesBox(
|
|
|
|
QWidget*,
|
|
|
|
not_null<ProxiesBoxController*> controller)
|
|
|
|
: _controller(controller)
|
2018-04-28 18:06:59 +04:00
|
|
|
, _initialWrap(this) {
|
2018-04-28 13:27:43 +04: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 18:06:59 +04:00
|
|
|
addButton(langFactory(lng_proxy_add), [=] { addNewProxy(); });
|
|
|
|
addButton(langFactory(lng_close), [=] { closeBox(); });
|
2018-04-28 13:27:43 +04:00
|
|
|
|
|
|
|
setupContent();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProxiesBox::setupContent() {
|
2018-05-07 00:29:53 +03:00
|
|
|
const auto inner = setInnerWidget(object_ptr<Ui::VerticalLayout>(this));
|
|
|
|
|
|
|
|
_tryIPv6 = inner->add(
|
|
|
|
object_ptr<Ui::Checkbox>(
|
|
|
|
inner,
|
|
|
|
lang(lng_connection_try_ipv6),
|
|
|
|
Global::TryIPv6()),
|
|
|
|
st::proxyTryIPv6Padding);
|
|
|
|
_useProxy = inner->add(
|
|
|
|
object_ptr<Ui::Checkbox>(
|
|
|
|
inner,
|
|
|
|
lang(lng_proxy_use)),
|
|
|
|
st::proxyUsePadding);
|
|
|
|
_proxyForCalls = inner->add(
|
|
|
|
object_ptr<Ui::SlideWrap<Ui::Checkbox>>(
|
|
|
|
inner,
|
|
|
|
object_ptr<Ui::Checkbox>(
|
|
|
|
inner,
|
|
|
|
lang(lng_proxy_use_for_calls),
|
|
|
|
Global::UseProxyForCalls()),
|
|
|
|
style::margins(
|
|
|
|
0,
|
|
|
|
st::proxyUsePadding.top(),
|
|
|
|
0,
|
|
|
|
st::proxyUsePadding.bottom())),
|
|
|
|
style::margins(
|
|
|
|
st::proxyTryIPv6Padding.left(),
|
|
|
|
0,
|
|
|
|
st::proxyTryIPv6Padding.right(),
|
|
|
|
st::proxyTryIPv6Padding.top()));
|
|
|
|
|
|
|
|
_about = inner->add(
|
|
|
|
object_ptr<Ui::DividerLabel>(
|
|
|
|
inner,
|
|
|
|
object_ptr<Ui::FlatLabel>(
|
|
|
|
inner,
|
|
|
|
lang(lng_proxy_about),
|
|
|
|
Ui::FlatLabel::InitType::Simple,
|
|
|
|
st::boxDividerLabel),
|
|
|
|
st::proxyAboutPadding),
|
|
|
|
style::margins(0, 0, 0, st::proxyRowPadding.top()));
|
|
|
|
|
|
|
|
_wrap = inner->add(std::move(_initialWrap));
|
|
|
|
inner->add(object_ptr<Ui::FixedHeightWidget>(
|
|
|
|
inner,
|
|
|
|
st::proxyRowPadding.bottom()));
|
|
|
|
|
|
|
|
subscribe(_useProxy->checkedChanged, [=](bool checked) {
|
2018-04-28 18:06:59 +04:00
|
|
|
if (!_controller->setProxyEnabled(checked)) {
|
2018-05-07 00:29:53 +03:00
|
|
|
_useProxy->setChecked(false);
|
2018-04-28 18:06:59 +04:00
|
|
|
addNewProxy();
|
|
|
|
}
|
2018-05-07 00:29:53 +03:00
|
|
|
refreshProxyForCalls();
|
2018-04-28 18:06:59 +04:00
|
|
|
});
|
2018-05-07 00:29:53 +03:00
|
|
|
subscribe(_tryIPv6->checkedChanged, [=](bool checked) {
|
2018-04-28 19:58:22 +04:00
|
|
|
_controller->setTryIPv6(checked);
|
|
|
|
});
|
2018-04-30 20:11:36 +04:00
|
|
|
_controller->proxyEnabledValue(
|
|
|
|
) | rpl::start_with_next([=](bool enabled) {
|
2018-05-07 00:29:53 +03:00
|
|
|
_useProxy->setChecked(enabled);
|
|
|
|
}, _useProxy->lifetime());
|
|
|
|
_useProxy->finishAnimating();
|
|
|
|
subscribe(_proxyForCalls->entity()->checkedChanged, [=](bool checked) {
|
|
|
|
_controller->setProxyForCalls(checked);
|
|
|
|
});
|
2018-04-28 18:06:59 +04:00
|
|
|
|
2018-04-28 19:58:22 +04:00
|
|
|
if (_rows.empty()) {
|
|
|
|
createNoRowsLabel();
|
|
|
|
}
|
2018-05-07 00:29:53 +03:00
|
|
|
refreshProxyForCalls();
|
|
|
|
_proxyForCalls->finishAnimating();
|
2018-04-28 19:58:22 +04:00
|
|
|
|
2018-04-28 18:06:59 +04:00
|
|
|
inner->resizeToWidth(st::boxWideWidth);
|
|
|
|
|
|
|
|
inner->heightValue(
|
|
|
|
) | rpl::map([=](int height) {
|
|
|
|
return std::min(
|
2018-05-07 00:29:53 +03:00
|
|
|
std::max(height, _about->y()
|
|
|
|
+ _about->height()
|
|
|
|
+ 3 * rowHeight()),
|
2018-04-28 18:06:59 +04:00
|
|
|
st::boxMaxListHeight);
|
2018-04-28 13:27:43 +04:00
|
|
|
}) | rpl::distinct_until_changed(
|
|
|
|
) | rpl::start_with_next([=](int height) {
|
|
|
|
setDimensions(st::boxWideWidth, height);
|
2018-04-28 18:06:59 +04:00
|
|
|
}, inner->lifetime());
|
2018-05-07 00:29:53 +03:00
|
|
|
}
|
2018-04-28 18:06:59 +04:00
|
|
|
|
2018-05-07 00:29:53 +03:00
|
|
|
void ProxiesBox::refreshProxyForCalls() {
|
|
|
|
if (!_proxyForCalls) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_proxyForCalls->toggle(
|
|
|
|
_useProxy->checked() && _currentProxySupportsCallsId != 0,
|
|
|
|
anim::type::normal);
|
2018-04-28 18:06:59 +04:00
|
|
|
}
|
|
|
|
|
2018-04-28 19:58:22 +04:00
|
|
|
int ProxiesBox::rowHeight() const {
|
|
|
|
return st::proxyRowPadding.top()
|
|
|
|
+ st::semiboldFont->height
|
|
|
|
+ st::proxyRowSkip
|
|
|
|
+ st::normalFont->height
|
|
|
|
+ st::proxyRowPadding.bottom();
|
|
|
|
}
|
|
|
|
|
2018-04-28 18:06:59 +04:00
|
|
|
void ProxiesBox::addNewProxy() {
|
|
|
|
Ui::show(_controller->addNewItemBox(), LayerOption::KeepOther);
|
2018-04-28 13:27:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void ProxiesBox::applyView(View &&view) {
|
2018-05-07 00:29:53 +03:00
|
|
|
if (view.selected) {
|
|
|
|
_currentProxySupportsCallsId = view.supportsCalls ? view.id : 0;
|
|
|
|
} else if (view.id == _currentProxySupportsCallsId) {
|
|
|
|
_currentProxySupportsCallsId = 0;
|
|
|
|
}
|
|
|
|
refreshProxyForCalls();
|
|
|
|
|
2018-04-28 13:27:43 +04:00
|
|
|
const auto id = view.id;
|
|
|
|
const auto i = _rows.find(id);
|
|
|
|
if (i == _rows.end()) {
|
2018-04-28 18:06:59 +04:00
|
|
|
const auto wrap = _wrap
|
|
|
|
? _wrap.data()
|
|
|
|
: _initialWrap.data();
|
2018-04-28 19:58:22 +04:00
|
|
|
const auto [i, ok] = _rows.emplace(id, nullptr);
|
|
|
|
i->second.reset(wrap->insert(
|
2018-04-28 18:06:59 +04:00
|
|
|
0,
|
2018-04-28 13:27:43 +04:00
|
|
|
object_ptr<ProxyRow>(
|
2018-04-28 18:06:59 +04:00
|
|
|
wrap,
|
2018-04-28 13:27:43 +04:00
|
|
|
std::move(view))));
|
2018-04-28 19:58:22 +04:00
|
|
|
setupButtons(id, i->second.get());
|
|
|
|
_noRows.reset();
|
|
|
|
} else if (view.host.isEmpty()) {
|
|
|
|
_rows.erase(i);
|
2018-04-28 13:27:43 +04:00
|
|
|
} else {
|
|
|
|
i->second->updateFields(std::move(view));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-28 19:58:22 +04:00
|
|
|
void ProxiesBox::createNoRowsLabel() {
|
|
|
|
_noRows.reset(_wrap->add(
|
|
|
|
object_ptr<Ui::FixedHeightWidget>(
|
|
|
|
_wrap,
|
|
|
|
rowHeight()),
|
|
|
|
st::proxyEmptyListPadding));
|
|
|
|
_noRows->resize(
|
|
|
|
(st::boxWideWidth
|
|
|
|
- st::proxyEmptyListPadding.left()
|
|
|
|
- st::proxyEmptyListPadding.right()),
|
|
|
|
_noRows->height());
|
|
|
|
const auto label = Ui::CreateChild<Ui::FlatLabel>(
|
|
|
|
_noRows.get(),
|
|
|
|
lang(lng_proxy_description),
|
|
|
|
Ui::FlatLabel::InitType::Simple,
|
|
|
|
st::proxyEmptyListLabel);
|
|
|
|
_noRows->widthValue(
|
|
|
|
) | rpl::start_with_next([=](int width) {
|
|
|
|
label->resizeToWidth(width);
|
|
|
|
label->moveToLeft(0, 0);
|
2018-05-03 21:02:26 +03:00
|
|
|
}, label->lifetime());
|
2018-04-28 19:58:22 +04:00
|
|
|
}
|
|
|
|
|
2018-04-28 13:27:43 +04:00
|
|
|
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 18:06:59 +04:00
|
|
|
|
2018-05-05 16:14:46 +03:00
|
|
|
button->shareClicks(
|
|
|
|
) | rpl::start_with_next([=] {
|
|
|
|
_controller->shareItem(id);
|
|
|
|
}, button->lifetime());
|
|
|
|
|
2018-04-28 18:06:59 +04:00
|
|
|
button->clicks(
|
|
|
|
) | rpl::start_with_next([=] {
|
|
|
|
_controller->applyItem(id);
|
|
|
|
}, button->lifetime());
|
2018-04-28 13:27:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
ProxyBox::ProxyBox(
|
|
|
|
QWidget*,
|
|
|
|
const ProxyData &data,
|
2018-05-05 16:14:46 +03:00
|
|
|
base::lambda<void(ProxyData)> callback,
|
|
|
|
base::lambda<void(ProxyData)> shareCallback)
|
2018-04-28 19:58:22 +04:00
|
|
|
: _callback(std::move(callback))
|
2018-05-05 16:14:46 +03:00
|
|
|
, _shareCallback(std::move(shareCallback))
|
2018-04-28 19:58:22 +04:00
|
|
|
, _content(this) {
|
|
|
|
setupControls(data);
|
2018-04-28 13:27:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void ProxyBox::prepare() {
|
2018-04-28 19:58:22 +04:00
|
|
|
setTitle(langFactory(lng_proxy_edit));
|
|
|
|
|
|
|
|
refreshButtons();
|
|
|
|
|
|
|
|
_content->heightValue(
|
|
|
|
) | rpl::start_with_next([=](int height) {
|
2018-05-07 00:29:53 +03:00
|
|
|
setDimensions(st::boxWideWidth, height);
|
2018-04-28 19:58:22 +04:00
|
|
|
}, _content->lifetime());
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProxyBox::refreshButtons() {
|
|
|
|
clearButtons();
|
|
|
|
addButton(langFactory(lng_settings_save), [=] { save(); });
|
|
|
|
addButton(langFactory(lng_cancel), [=] { closeBox(); });
|
|
|
|
|
|
|
|
const auto type = _type->value();
|
|
|
|
if (type == Type::Socks5 || type == Type::Mtproto) {
|
|
|
|
addLeftButton(langFactory(lng_proxy_share), [=] { share(); });
|
|
|
|
}
|
|
|
|
}
|
2018-04-28 13:27:43 +04:00
|
|
|
|
2018-04-28 19:58:22 +04:00
|
|
|
void ProxyBox::save() {
|
|
|
|
if (const auto data = collectData()) {
|
|
|
|
_callback(data);
|
|
|
|
closeBox();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProxyBox::share() {
|
|
|
|
if (const auto data = collectData()) {
|
2018-05-05 16:14:46 +03:00
|
|
|
_shareCallback(data);
|
2018-04-28 19:58:22 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ProxyData ProxyBox::collectData() {
|
|
|
|
auto result = ProxyData();
|
|
|
|
result.type = _type->value();
|
|
|
|
result.host = _host->getLastText().trimmed();
|
|
|
|
result.port = _port->getLastText().trimmed().toInt();
|
|
|
|
result.user = (result.type == Type::Mtproto)
|
|
|
|
? QString()
|
|
|
|
: _user->getLastText();
|
|
|
|
result.password = (result.type == Type::Mtproto)
|
|
|
|
? _secret->getLastText()
|
|
|
|
: _password->getLastText();
|
|
|
|
if (result.host.isEmpty()) {
|
|
|
|
_host->showError();
|
|
|
|
} else if (!result.port) {
|
|
|
|
_port->showError();
|
|
|
|
} else if ((result.type == Type::Http || result.type == Type::Socks5)
|
|
|
|
&& !result.password.isEmpty() && result.user.isEmpty()) {
|
|
|
|
_user->showError();
|
|
|
|
} else if (result.type == Type::Mtproto
|
|
|
|
&& result.password.size() != 32) {
|
|
|
|
_secret->showError();
|
|
|
|
} else if (!result) {
|
|
|
|
_host->showError();
|
|
|
|
} else {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
return ProxyData();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProxyBox::setupTypes() {
|
|
|
|
const auto types = std::map<Type, QString>{
|
|
|
|
{ Type::Http, "HTTP" },
|
|
|
|
{ Type::Socks5, "SOCKS5" },
|
|
|
|
{ Type::Mtproto, "MTPROTO" },
|
|
|
|
};
|
|
|
|
for (const auto [type, label] : types) {
|
|
|
|
_content->add(
|
|
|
|
object_ptr<Ui::Radioenum<Type>>(
|
|
|
|
_content,
|
|
|
|
_type,
|
|
|
|
type,
|
|
|
|
label),
|
|
|
|
st::proxyEditTypePadding);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProxyBox::setupSocketAddress(const ProxyData &data) {
|
|
|
|
addLabel(_content, lang(lng_proxy_address_label));
|
|
|
|
const auto address = _content->add(
|
|
|
|
object_ptr<Ui::FixedHeightWidget>(
|
|
|
|
_content,
|
|
|
|
st::connectionHostInputField.heightMin),
|
|
|
|
st::proxyEditInputPadding);
|
|
|
|
_host = Ui::CreateChild<Ui::InputField>(
|
|
|
|
address,
|
|
|
|
st::connectionHostInputField,
|
|
|
|
langFactory(lng_connection_host_ph),
|
|
|
|
data.host);
|
|
|
|
_port = Ui::CreateChild<Ui::PortInput>(
|
|
|
|
address,
|
|
|
|
st::connectionPortInputField,
|
|
|
|
langFactory(lng_connection_port_ph),
|
|
|
|
data.port ? QString::number(data.port) : QString());
|
|
|
|
address->widthValue(
|
|
|
|
) | rpl::start_with_next([=](int width) {
|
|
|
|
_port->moveToRight(0, 0);
|
|
|
|
_host->resize(
|
|
|
|
width - _port->width() - st::proxyEditSkip,
|
|
|
|
_host->height());
|
|
|
|
_host->moveToLeft(0, 0);
|
|
|
|
}, address->lifetime());
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProxyBox::setupCredentials(const ProxyData &data) {
|
|
|
|
_credentials = _content->add(
|
|
|
|
object_ptr<Ui::SlideWrap<Ui::VerticalLayout>>(
|
|
|
|
_content,
|
|
|
|
object_ptr<Ui::VerticalLayout>(_content)));
|
|
|
|
const auto credentials = _credentials->entity();
|
|
|
|
addLabel(credentials, lang(lng_proxy_credentials_optional));
|
|
|
|
_user = credentials->add(
|
|
|
|
object_ptr<Ui::InputField>(
|
|
|
|
credentials,
|
|
|
|
st::connectionUserInputField,
|
|
|
|
langFactory(lng_connection_user_ph),
|
|
|
|
data.user),
|
|
|
|
st::proxyEditInputPadding);
|
|
|
|
|
|
|
|
auto passwordWrap = object_ptr<Ui::RpWidget>(credentials);
|
|
|
|
_password = Ui::CreateChild<Ui::PasswordInput>(
|
|
|
|
passwordWrap.data(),
|
|
|
|
st::connectionPasswordInputField,
|
|
|
|
langFactory(lng_connection_password_ph),
|
|
|
|
(data.type == Type::Mtproto) ? QString() : data.password);
|
|
|
|
_password->move(0, 0);
|
|
|
|
_password->heightValue(
|
|
|
|
) | rpl::start_with_next([=, wrap = passwordWrap.data()](int height) {
|
|
|
|
wrap->resize(wrap->width(), height);
|
|
|
|
}, _password->lifetime());
|
|
|
|
passwordWrap->widthValue(
|
|
|
|
) | rpl::start_with_next([=](int width) {
|
|
|
|
_password->resize(width, _password->height());
|
|
|
|
}, _password->lifetime());
|
|
|
|
credentials->add(std::move(passwordWrap), st::proxyEditInputPadding);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProxyBox::setupMtprotoCredentials(const ProxyData &data) {
|
|
|
|
_mtprotoCredentials = _content->add(
|
|
|
|
object_ptr<Ui::SlideWrap<Ui::VerticalLayout>>(
|
|
|
|
_content,
|
|
|
|
object_ptr<Ui::VerticalLayout>(_content)));
|
|
|
|
const auto mtproto = _mtprotoCredentials->entity();
|
|
|
|
addLabel(mtproto, lang(lng_proxy_credentials));
|
|
|
|
|
|
|
|
auto secretWrap = object_ptr<Ui::RpWidget>(mtproto);
|
|
|
|
_secret = Ui::CreateChild<Ui::HexInput>(
|
|
|
|
secretWrap.data(),
|
|
|
|
st::connectionUserInputField,
|
|
|
|
langFactory(lng_connection_proxy_secret_ph),
|
|
|
|
(data.type == Type::Mtproto) ? data.password : QString());
|
|
|
|
_secret->setMaxLength(32);
|
|
|
|
_secret->move(0, 0);
|
|
|
|
_secret->heightValue(
|
|
|
|
) | rpl::start_with_next([=, wrap = secretWrap.data()](int height) {
|
|
|
|
wrap->resize(wrap->width(), height);
|
|
|
|
}, _secret->lifetime());
|
|
|
|
secretWrap->widthValue(
|
|
|
|
) | rpl::start_with_next([=](int width) {
|
|
|
|
_secret->resize(width, _secret->height());
|
|
|
|
}, _secret->lifetime());
|
|
|
|
mtproto->add(std::move(secretWrap), st::proxyEditInputPadding);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProxyBox::setupControls(const ProxyData &data) {
|
|
|
|
_type = std::make_shared<Ui::RadioenumGroup<Type>>(
|
|
|
|
(data.type == Type::None
|
|
|
|
? Type::Socks5
|
|
|
|
: data.type));
|
|
|
|
_content.create(this);
|
2018-05-07 00:29:53 +03:00
|
|
|
_content->resizeToWidth(st::boxWideWidth);
|
2018-04-28 19:58:22 +04:00
|
|
|
_content->moveToLeft(0, 0);
|
|
|
|
|
|
|
|
setupTypes();
|
|
|
|
setupSocketAddress(data);
|
|
|
|
setupCredentials(data);
|
|
|
|
setupMtprotoCredentials(data);
|
|
|
|
|
2018-05-07 00:29:53 +03:00
|
|
|
_content->resizeToWidth(st::boxWideWidth);
|
2018-04-28 19:58:22 +04:00
|
|
|
|
|
|
|
const auto handleType = [=](Type type) {
|
|
|
|
_credentials->toggle(
|
|
|
|
type == Type::Http || type == Type::Socks5,
|
|
|
|
anim::type::instant);
|
|
|
|
_mtprotoCredentials->toggle(
|
|
|
|
type == Type::Mtproto,
|
|
|
|
anim::type::instant);
|
|
|
|
};
|
|
|
|
_type->setChangedCallback([=](Type type) {
|
|
|
|
handleType(type);
|
|
|
|
refreshButtons();
|
|
|
|
});
|
|
|
|
handleType(_type->value());
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProxyBox::addLabel(
|
|
|
|
not_null<Ui::VerticalLayout*> parent,
|
|
|
|
const QString &text) const {
|
|
|
|
parent->add(
|
|
|
|
object_ptr<Ui::FlatLabel>(
|
|
|
|
parent,
|
|
|
|
text,
|
|
|
|
Ui::FlatLabel::InitType::Simple,
|
|
|
|
st::proxyEditTitle),
|
|
|
|
st::proxyEditTitlePadding);
|
2018-04-28 13:27:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
2014-05-30 12:53:19 +04:00
|
|
|
|
2018-04-27 20:16:50 +04:00
|
|
|
void ConnectionBox::ShowApplyProxyConfirmation(
|
2018-05-05 16:14:46 +03:00
|
|
|
Type type,
|
2018-04-27 20:16:50 +04:00
|
|
|
const QMap<QString, QString> &fields) {
|
|
|
|
const auto server = fields.value(qsl("server"));
|
|
|
|
const auto port = fields.value(qsl("port")).toUInt();
|
2018-04-27 21:26:45 +04:00
|
|
|
auto proxy = ProxyData();
|
|
|
|
proxy.type = type;
|
|
|
|
proxy.host = server;
|
|
|
|
proxy.port = port;
|
2018-05-05 16:14:46 +03:00
|
|
|
if (type == Type::Socks5) {
|
2018-04-27 21:26:45 +04:00
|
|
|
proxy.user = fields.value(qsl("user"));
|
|
|
|
proxy.password = fields.value(qsl("pass"));
|
2018-05-05 16:14:46 +03:00
|
|
|
} else if (type == Type::Mtproto) {
|
2018-04-27 21:26:45 +04:00
|
|
|
proxy.password = fields.value(qsl("secret"));
|
|
|
|
}
|
|
|
|
if (proxy) {
|
2018-04-23 14:24:03 +04:00
|
|
|
const auto box = std::make_shared<QPointer<ConfirmBox>>();
|
2018-04-27 20:16:50 +04: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 21:26:45 +04:00
|
|
|
auto &proxies = Global::RefProxiesList();
|
|
|
|
if (ranges::find(proxies, proxy) == end(proxies)) {
|
2018-04-28 18:06:59 +04:00
|
|
|
proxies.push_back(proxy);
|
2018-04-27 20:16:50 +04:00
|
|
|
}
|
2018-04-27 21:26:45 +04:00
|
|
|
Global::SetSelectedProxy(proxy);
|
|
|
|
Global::SetUseProxy(true);
|
2017-06-27 23:11:38 +03:00
|
|
|
Local::writeSettings();
|
2018-04-23 14:24:03 +04:00
|
|
|
Sandbox::refreshGlobalProxy();
|
2017-06-27 23:11:38 +03:00
|
|
|
Global::RefConnectionTypeChanged().notify();
|
|
|
|
MTP::restart();
|
2018-04-23 14:24:03 +04:00
|
|
|
if (const auto strong = box->data()) {
|
|
|
|
strong->closeBox();
|
|
|
|
}
|
2017-09-15 20:34:41 +03:00
|
|
|
}), LayerOption::KeepOther);
|
2017-06-27 23:11:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-13 20:07:56 +03:00
|
|
|
ConnectionBox::ConnectionBox(QWidget *parent)
|
2018-04-27 21:26:45 +04: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 20:07:56 +03:00
|
|
|
, _tryIPv6(this, lang(lng_connection_try_ipv6), Global::TryIPv6(), st::defaultBoxCheckbox) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConnectionBox::prepare() {
|
2017-05-30 18:21:05 +03:00
|
|
|
setTitle(langFactory(lng_connection_header));
|
2015-10-06 22:49:23 +03:00
|
|
|
|
2017-05-30 18:21:05 +03:00
|
|
|
addButton(langFactory(lng_connection_save), [this] { onSave(); });
|
|
|
|
addButton(langFactory(lng_cancel), [this] { closeBox(); });
|
2014-05-30 12:53:19 +04:00
|
|
|
|
2018-04-27 21:26:45 +04:00
|
|
|
_typeGroup->setChangedCallback([this](Type value) { typeChanged(value); });
|
2014-05-30 12:53:19 +04:00
|
|
|
|
2016-10-28 12:20:24 +03: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-28 01:03:37 +03: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 22:49:23 +03:00
|
|
|
|
2016-11-18 16:34:58 +03:00
|
|
|
updateControlsVisibility();
|
2014-05-30 12:53:19 +04:00
|
|
|
}
|
|
|
|
|
2017-06-28 01:03:37 +03:00
|
|
|
bool ConnectionBox::badProxyValue() const {
|
|
|
|
return (_hostInput->getLastText().isEmpty() || !_portInput->getLastText().toInt());
|
|
|
|
}
|
|
|
|
|
2016-11-18 16:34:58 +03:00
|
|
|
void ConnectionBox::updateControlsVisibility() {
|
2017-08-05 13:43:59 +02: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 23:09:20 +04:00
|
|
|
if (!proxyFieldsVisible()) {
|
2017-03-19 00:06:10 +03:00
|
|
|
_hostInput->hide();
|
|
|
|
_portInput->hide();
|
|
|
|
_userInput->hide();
|
|
|
|
_passwordInput->hide();
|
|
|
|
} else {
|
2016-12-13 20:07:56 +03:00
|
|
|
newHeight += 2 * st::boxOptionInputSkip + 2 * _hostInput->height();
|
2016-10-28 12:20:24 +03:00
|
|
|
_hostInput->show();
|
|
|
|
_portInput->show();
|
|
|
|
_userInput->show();
|
|
|
|
_passwordInput->show();
|
2014-05-30 12:53:19 +04:00
|
|
|
}
|
|
|
|
|
2016-12-13 20:07:56 +03:00
|
|
|
setDimensions(st::boxWidth, newHeight);
|
|
|
|
updateControlsPosition();
|
2014-05-30 12:53:19 +04:00
|
|
|
}
|
|
|
|
|
2018-04-24 23:09:20 +04:00
|
|
|
bool ConnectionBox::proxyFieldsVisible() const {
|
2018-05-05 16:14:46 +03:00
|
|
|
return (_typeGroup->value() == Type::Http
|
|
|
|
|| _typeGroup->value() == Type::Socks5);
|
2018-04-24 23:09:20 +04:00
|
|
|
}
|
|
|
|
|
2016-12-13 20:07:56 +03:00
|
|
|
void ConnectionBox::setInnerFocus() {
|
2018-04-27 21:26:45 +04:00
|
|
|
if (proxyFieldsVisible()) {
|
2016-12-20 16:03:51 +03:00
|
|
|
_hostInput->setFocusFast();
|
2018-04-27 21:26:45 +04:00
|
|
|
} else {
|
|
|
|
setFocus();
|
2014-05-30 12:53:19 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-02 13:33:19 +03:00
|
|
|
void ConnectionBox::resizeEvent(QResizeEvent *e) {
|
2016-12-13 20:07:56 +03:00
|
|
|
BoxContent::resizeEvent(e);
|
|
|
|
|
|
|
|
updateControlsPosition();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConnectionBox::updateControlsPosition() {
|
2017-03-19 00:06:10 +03:00
|
|
|
auto type = _typeGroup->value();
|
2017-07-04 16:59:00 +03:00
|
|
|
_autoRadio->moveToLeft(st::boxPadding.left() + st::boxOptionListPadding.left(), _autoRadio->getMargins().top() + st::boxOptionListPadding.top());
|
2016-12-13 20:07:56 +03:00
|
|
|
_httpProxyRadio->moveToLeft(st::boxPadding.left() + st::boxOptionListPadding.left(), _autoRadio->bottomNoMargins() + st::boxOptionListSkip);
|
2015-04-02 13:33:19 +03:00
|
|
|
|
2017-03-19 00:06:10 +03:00
|
|
|
auto inputy = 0;
|
2018-04-24 23:09:20 +04:00
|
|
|
auto fieldsVisible = proxyFieldsVisible();
|
2018-05-05 16:14:46 +03:00
|
|
|
auto fieldsBelowHttp = fieldsVisible && (type == Type::Http);
|
|
|
|
auto fieldsBelowTcp = fieldsVisible && (type == Type::Socks5);
|
2017-06-28 01:03:37 +03:00
|
|
|
if (fieldsBelowHttp) {
|
2016-12-09 21:56:01 +03:00
|
|
|
inputy = _httpProxyRadio->bottomNoMargins() + st::boxOptionInputSkip;
|
2016-12-13 20:07:56 +03:00
|
|
|
_tcpProxyRadio->moveToLeft(st::boxPadding.left() + st::boxOptionListPadding.left(), inputy + st::boxOptionInputSkip + 2 * _hostInput->height() + st::boxOptionListSkip);
|
2014-05-30 12:53:19 +04:00
|
|
|
} else {
|
2016-12-13 20:07:56 +03:00
|
|
|
_tcpProxyRadio->moveToLeft(st::boxPadding.left() + st::boxOptionListPadding.left(), _httpProxyRadio->bottomNoMargins() + st::boxOptionListSkip);
|
2017-06-28 01:03:37 +03:00
|
|
|
if (fieldsBelowTcp) {
|
2016-12-09 21:56:01 +03:00
|
|
|
inputy = _tcpProxyRadio->bottomNoMargins() + st::boxOptionInputSkip;
|
2015-04-02 13:33:19 +03:00
|
|
|
}
|
2014-05-30 12:53:19 +04:00
|
|
|
}
|
2015-04-02 13:33:19 +03:00
|
|
|
|
|
|
|
if (inputy) {
|
2017-07-07 14:27:13 +03:00
|
|
|
_hostInput->moveToLeft(st::boxPadding.left() + st::boxOptionListPadding.left() + st::defaultCheck.diameter + st::defaultBoxCheckbox.textPosition.x() - st::defaultInputField.textMargins.left(), inputy);
|
2016-10-28 12:20:24 +03:00
|
|
|
_portInput->moveToRight(st::boxPadding.right(), inputy);
|
2017-07-07 14:27:13 +03: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 12:20:24 +03:00
|
|
|
_passwordInput->moveToRight(st::boxPadding.right(), _userInput->y());
|
2015-04-02 13:33:19 +03:00
|
|
|
}
|
|
|
|
|
2017-06-28 01:03:37 +03:00
|
|
|
auto tryipv6y = (fieldsBelowTcp ? _userInput->bottomNoMargins() : _tcpProxyRadio->bottomNoMargins()) + st::boxOptionListSkip + st::connectionIPv6Skip;
|
2016-10-28 12:20:24 +03:00
|
|
|
_tryIPv6->moveToLeft(st::boxPadding.left() + st::boxOptionListPadding.left(), tryipv6y);
|
2014-05-30 12:53:19 +04:00
|
|
|
}
|
|
|
|
|
2018-04-27 21:26:45 +04:00
|
|
|
void ConnectionBox::typeChanged(Type type) {
|
|
|
|
if (!proxyFieldsVisible()) {
|
2017-06-28 01:03:37 +03:00
|
|
|
setFocus();
|
|
|
|
}
|
2016-11-18 16:34:58 +03:00
|
|
|
updateControlsVisibility();
|
2018-04-27 21:26:45 +04:00
|
|
|
if (proxyFieldsVisible()) {
|
2016-12-20 16:03:51 +03:00
|
|
|
if (!_hostInput->hasFocus() && !_portInput->hasFocus() && !_userInput->hasFocus() && !_passwordInput->hasFocus()) {
|
|
|
|
_hostInput->setFocusFast();
|
|
|
|
}
|
2018-04-27 21:26:45 +04:00
|
|
|
if ((type == Type::Http) && !_portInput->getLastText().toInt()) {
|
2016-10-28 12:20:24 +03:00
|
|
|
_portInput->setText(qsl("80"));
|
2017-09-30 22:20:40 +03:00
|
|
|
_portInput->finishAnimating();
|
2014-05-30 12:53:19 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2017-06-28 01:03:37 +03:00
|
|
|
void ConnectionBox::onFieldFocus() {
|
|
|
|
}
|
|
|
|
|
2015-10-06 22:49:23 +03:00
|
|
|
void ConnectionBox::onSubmit() {
|
2017-06-28 01:03:37 +03:00
|
|
|
onFieldFocus();
|
2016-10-28 12:20:24 +03:00
|
|
|
if (_hostInput->hasFocus()) {
|
|
|
|
if (!_hostInput->getLastText().trimmed().isEmpty()) {
|
|
|
|
_portInput->setFocus();
|
2015-10-06 22:49:23 +03:00
|
|
|
} else {
|
2016-10-28 12:20:24 +03:00
|
|
|
_hostInput->showError();
|
2015-10-06 22:49:23 +03:00
|
|
|
}
|
2016-10-28 12:20:24 +03:00
|
|
|
} else if (_portInput->hasFocus()) {
|
|
|
|
if (_portInput->getLastText().trimmed().toInt() > 0) {
|
|
|
|
_userInput->setFocus();
|
2015-10-06 22:49:23 +03:00
|
|
|
} else {
|
2016-10-28 12:20:24 +03:00
|
|
|
_portInput->showError();
|
2015-10-06 22:49:23 +03:00
|
|
|
}
|
2016-10-28 12:20:24 +03: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 22:49:23 +03:00
|
|
|
} else {
|
|
|
|
onSave();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-30 12:53:19 +04:00
|
|
|
void ConnectionBox::onSave() {
|
2018-04-23 14:24:03 +04: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-28 01:03:37 +03:00
|
|
|
|
2017-03-19 00:06:10 +03:00
|
|
|
auto type = _typeGroup->value();
|
2018-04-27 21:26:45 +04:00
|
|
|
if (type == Type::None) {
|
|
|
|
proxy = ProxyData();
|
|
|
|
} else if (type == Type::Mtproto) {
|
|
|
|
proxy = Global::SelectedProxy();
|
2017-03-19 00:06:10 +03:00
|
|
|
} else {
|
2018-04-23 14:24:03 +04:00
|
|
|
if (proxy.host.isEmpty()) {
|
2017-06-28 01:03:37 +03:00
|
|
|
_hostInput->showError();
|
2014-05-30 12:53:19 +04:00
|
|
|
return;
|
2018-04-23 14:24:03 +04:00
|
|
|
} else if (!proxy.port) {
|
2017-06-28 01:03:37 +03:00
|
|
|
_portInput->showError();
|
2014-05-30 12:53:19 +04:00
|
|
|
return;
|
|
|
|
}
|
2018-04-27 21:26:45 +04:00
|
|
|
proxy.type = type;
|
2014-05-30 12:53:19 +04:00
|
|
|
}
|
2018-04-27 21:26:45 +04:00
|
|
|
Global::SetSelectedProxy(proxy ? proxy : ProxyData());
|
|
|
|
Global::SetUseProxy(proxy ? true : false);
|
2016-10-28 12:20:24 +03:00
|
|
|
if (cPlatform() == dbipWindows && Global::TryIPv6() != _tryIPv6->checked()) {
|
|
|
|
Global::SetTryIPv6(_tryIPv6->checked());
|
2015-06-25 21:04:40 +03:00
|
|
|
Local::writeSettings();
|
2016-08-26 22:49:18 -06:00
|
|
|
Global::RefConnectionTypeChanged().notify();
|
|
|
|
|
2016-10-28 15:44:28 +03:00
|
|
|
App::restart();
|
2015-06-25 21:04:40 +03:00
|
|
|
} else {
|
2016-10-28 12:20:24 +03:00
|
|
|
Global::SetTryIPv6(_tryIPv6->checked());
|
2015-06-25 21:04:40 +03:00
|
|
|
Local::writeSettings();
|
2018-04-23 14:24:03 +04:00
|
|
|
Sandbox::refreshGlobalProxy();
|
2016-08-26 22:49:18 -06:00
|
|
|
Global::RefConnectionTypeChanged().notify();
|
|
|
|
|
2015-06-25 21:04:40 +03:00
|
|
|
MTP::restart();
|
2016-12-13 20:07:56 +03:00
|
|
|
closeBox();
|
2015-06-25 21:04:40 +03:00
|
|
|
}
|
2014-05-30 12:53:19 +04:00
|
|
|
}
|
2015-12-24 00:19:57 +03:00
|
|
|
|
2016-12-13 20:07:56 +03:00
|
|
|
AutoDownloadBox::AutoDownloadBox(QWidget *parent)
|
|
|
|
: _photoPrivate(this, lang(lng_media_auto_private_chats), !(cAutoDownloadPhoto() & dbiadNoPrivate), st::defaultBoxCheckbox)
|
2016-06-10 14:18:55 +03: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 16:53:42 +03:00
|
|
|
, _sectionHeight(st::boxTitleHeight + 2 * (st::defaultCheck.diameter + st::setLittleSkip)) {
|
2016-12-13 20:07:56 +03:00
|
|
|
}
|
2015-12-24 00:19:57 +03:00
|
|
|
|
2016-12-13 20:07:56 +03:00
|
|
|
void AutoDownloadBox::prepare() {
|
2017-05-30 18:21:05 +03:00
|
|
|
addButton(langFactory(lng_connection_save), [this] { onSave(); });
|
|
|
|
addButton(langFactory(lng_cancel), [this] { closeBox(); });
|
2015-12-24 00:19:57 +03:00
|
|
|
|
2016-12-20 16:03:51 +03:00
|
|
|
setDimensions(st::boxWidth, 3 * _sectionHeight - st::autoDownloadTopDelta + st::setLittleSkip + _gifPlay->heightNoMargins() + st::setLittleSkip);
|
2015-12-24 00:19:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void AutoDownloadBox::paintEvent(QPaintEvent *e) {
|
2016-12-13 20:07:56 +03:00
|
|
|
BoxContent::paintEvent(e);
|
2016-11-19 17:47:28 +03:00
|
|
|
|
2015-12-24 00:19:57 +03:00
|
|
|
Painter p(this);
|
|
|
|
|
2016-12-13 20:07:56 +03: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-24 00:19:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void AutoDownloadBox::resizeEvent(QResizeEvent *e) {
|
2016-12-13 20:07:56 +03:00
|
|
|
BoxContent::resizeEvent(e);
|
|
|
|
|
2016-12-20 16:03:51 +03:00
|
|
|
auto top = st::boxTitleHeight - st::autoDownloadTopDelta;
|
|
|
|
_photoPrivate->moveToLeft(st::boxTitlePosition.x(), top + st::setLittleSkip);
|
2016-12-02 22:16:35 +03:00
|
|
|
_photoGroups->moveToLeft(st::boxTitlePosition.x(), _photoPrivate->bottomNoMargins() + st::setLittleSkip);
|
2015-12-24 00:19:57 +03:00
|
|
|
|
2016-12-20 16:03:51 +03:00
|
|
|
_audioPrivate->moveToLeft(st::boxTitlePosition.x(), _sectionHeight + top + st::setLittleSkip);
|
2016-12-02 22:16:35 +03:00
|
|
|
_audioGroups->moveToLeft(st::boxTitlePosition.x(), _audioPrivate->bottomNoMargins() + st::setLittleSkip);
|
2015-12-24 00:19:57 +03:00
|
|
|
|
2016-12-20 16:03:51 +03:00
|
|
|
_gifPrivate->moveToLeft(st::boxTitlePosition.x(), 2 * _sectionHeight + top + st::setLittleSkip);
|
2016-12-02 22:16:35 +03:00
|
|
|
_gifGroups->moveToLeft(st::boxTitlePosition.x(), _gifPrivate->bottomNoMargins() + st::setLittleSkip);
|
|
|
|
_gifPlay->moveToLeft(st::boxTitlePosition.x(), _gifGroups->bottomNoMargins() + st::setLittleSkip);
|
2015-12-24 00:19:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void AutoDownloadBox::onSave() {
|
2018-01-17 19:21:01 +03: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-28 00:37:48 +03:00
|
|
|
if (cAutoDownloadPhoto() != autoDownloadPhoto) {
|
2018-01-17 19:21:01 +03:00
|
|
|
const auto enabledPrivate = (cAutoDownloadPhoto() & dbiadNoPrivate)
|
|
|
|
&& !(autoDownloadPhoto & dbiadNoPrivate);
|
|
|
|
const auto enabledGroups = (cAutoDownloadPhoto() & dbiadNoGroups)
|
|
|
|
&& !(autoDownloadPhoto & dbiadNoGroups);
|
|
|
|
photosEnabled = enabledPrivate || enabledGroups;
|
|
|
|
photosChanged = true;
|
2015-12-28 00:37:48 +03:00
|
|
|
cSetAutoDownloadPhoto(autoDownloadPhoto);
|
|
|
|
}
|
2018-01-17 19:21:01 +03:00
|
|
|
auto autoDownloadAudio = (_audioPrivate->checked() ? 0 : dbiadNoPrivate)
|
|
|
|
| (_audioGroups->checked() ? 0 : dbiadNoGroups);
|
2015-12-28 00:37:48 +03:00
|
|
|
if (cAutoDownloadAudio() != autoDownloadAudio) {
|
2018-01-17 19:21:01 +03:00
|
|
|
const auto enabledPrivate = (cAutoDownloadAudio() & dbiadNoPrivate)
|
|
|
|
&& !(autoDownloadAudio & dbiadNoPrivate);
|
|
|
|
const auto enabledGroups = (cAutoDownloadAudio() & dbiadNoGroups)
|
|
|
|
&& !(autoDownloadAudio & dbiadNoGroups);
|
|
|
|
voiceEnabled = enabledPrivate || enabledGroups;
|
|
|
|
documentsChanged = true;
|
2015-12-28 00:37:48 +03:00
|
|
|
cSetAutoDownloadAudio(autoDownloadAudio);
|
|
|
|
}
|
2018-01-17 19:21:01 +03:00
|
|
|
auto autoDownloadGif = (_gifPrivate->checked() ? 0 : dbiadNoPrivate)
|
|
|
|
| (_gifGroups->checked() ? 0 : dbiadNoGroups);
|
2015-12-28 00:37:48 +03:00
|
|
|
if (cAutoDownloadGif() != autoDownloadGif) {
|
2018-01-17 19:21:01 +03:00
|
|
|
const auto enabledPrivate = (cAutoDownloadGif() & dbiadNoPrivate)
|
|
|
|
&& !(autoDownloadGif & dbiadNoPrivate);
|
|
|
|
const auto enabledGroups = (cAutoDownloadGif() & dbiadNoGroups)
|
|
|
|
&& !(autoDownloadGif & dbiadNoGroups);
|
|
|
|
animationsEnabled = enabledPrivate || enabledGroups;
|
|
|
|
documentsChanged = true;
|
2015-12-28 00:37:48 +03:00
|
|
|
cSetAutoDownloadGif(autoDownloadGif);
|
|
|
|
}
|
2016-10-28 12:20:24 +03:00
|
|
|
if (cAutoPlayGif() != _gifPlay->checked()) {
|
|
|
|
cSetAutoPlayGif(_gifPlay->checked());
|
2015-12-28 00:37:48 +03:00
|
|
|
if (!cAutoPlayGif()) {
|
2018-01-17 19:21:01 +03:00
|
|
|
Auth().data().stopAutoplayAnimations();
|
2015-12-28 00:37:48 +03:00
|
|
|
}
|
2018-01-17 19:21:01 +03: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-28 00:37:48 +03:00
|
|
|
}
|
2016-12-13 20:07:56 +03:00
|
|
|
closeBox();
|
2015-12-24 00:19:57 +03:00
|
|
|
}
|
2018-04-28 13:27:43 +04:00
|
|
|
|
2018-04-28 18:06:59 +04:00
|
|
|
ProxiesBoxController::ProxiesBoxController()
|
|
|
|
: _saveTimer([] { Local::writeSettings(); }) {
|
2018-04-28 13:27:43 +04:00
|
|
|
_list = ranges::view::all(
|
|
|
|
Global::ProxiesList()
|
|
|
|
) | ranges::view::transform([&](const ProxyData &proxy) {
|
|
|
|
return Item{ ++_idCounter, proxy };
|
|
|
|
}) | ranges::to_vector;
|
2018-04-30 20:11:36 +04:00
|
|
|
|
|
|
|
subscribe(Global::RefConnectionTypeChanged(), [=] {
|
|
|
|
_proxyEnabledChanges.fire_copy(Global::UseProxy());
|
|
|
|
const auto i = findByProxy(Global::SelectedProxy());
|
|
|
|
if (i != end(_list)) {
|
|
|
|
updateView(*i);
|
2018-04-30 19:49:03 +04:00
|
|
|
}
|
2018-04-30 20:11:36 +04:00
|
|
|
});
|
|
|
|
|
|
|
|
for (auto &item : _list) {
|
|
|
|
refreshChecker(item);
|
2018-04-30 19:49:03 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-30 20:11:36 +04:00
|
|
|
rpl::producer<bool> ProxiesBoxController::proxyEnabledValue() const {
|
|
|
|
return _proxyEnabledChanges.events_starting_with_copy(
|
|
|
|
Global::UseProxy()
|
|
|
|
) | rpl::distinct_until_changed();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProxiesBoxController::refreshChecker(Item &item) {
|
2018-04-30 19:49:03 +04:00
|
|
|
using Variants = MTP::DcOptions::Variants;
|
2018-05-05 16:14:46 +03:00
|
|
|
const auto type = (item.data.type == Type::Http)
|
2018-04-30 19:49:03 +04:00
|
|
|
? Variants::Http
|
|
|
|
: Variants::Tcp;
|
|
|
|
const auto mtproto = Messenger::Instance().mtp();
|
|
|
|
const auto dcId = mtproto->mainDcId();
|
|
|
|
|
|
|
|
item.state = ItemState::Checking;
|
|
|
|
const auto setup = [&](Checker &checker) {
|
|
|
|
checker = MTP::internal::AbstractConnection::create(
|
|
|
|
type,
|
|
|
|
QThread::currentThread());
|
|
|
|
setupChecker(item.id, checker);
|
|
|
|
};
|
|
|
|
setup(item.checker);
|
2018-05-05 16:14:46 +03:00
|
|
|
if (item.data.type == Type::Mtproto) {
|
2018-04-30 19:49:03 +04:00
|
|
|
item.checkerv6 = nullptr;
|
2018-04-30 20:11:36 +04:00
|
|
|
item.checker->setProxyOverride(item.data);
|
2018-04-30 19:49:03 +04:00
|
|
|
item.checker->connectToServer(
|
|
|
|
item.data.host,
|
|
|
|
item.data.port,
|
|
|
|
MTP::ProtocolSecretFromPassword(item.data.password),
|
|
|
|
dcId);
|
|
|
|
} else {
|
|
|
|
const auto options = mtproto->dcOptions()->lookup(
|
|
|
|
dcId,
|
|
|
|
MTP::DcType::Regular,
|
|
|
|
true);
|
|
|
|
const auto endpoint = options.data[Variants::IPv4][type];
|
|
|
|
const auto endpointv6 = options.data[Variants::IPv6][type];
|
|
|
|
if (endpoint.empty()) {
|
|
|
|
item.checker = nullptr;
|
|
|
|
}
|
|
|
|
if (Global::TryIPv6() && !endpointv6.empty()) {
|
|
|
|
setup(item.checkerv6);
|
|
|
|
} else {
|
|
|
|
item.checkerv6 = nullptr;
|
|
|
|
}
|
|
|
|
if (!item.checker && !item.checkerv6) {
|
|
|
|
item.state = ItemState::Unavailable;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto connect = [&](
|
|
|
|
const Checker &checker,
|
|
|
|
const std::vector<MTP::DcOptions::Endpoint> &endpoints) {
|
|
|
|
if (checker) {
|
|
|
|
checker->setProxyOverride(item.data);
|
|
|
|
checker->connectToServer(
|
|
|
|
QString::fromStdString(endpoints.front().ip),
|
|
|
|
endpoints.front().port,
|
2018-05-02 22:07:34 +03:00
|
|
|
endpoints.front().secret,
|
2018-04-30 19:49:03 +04:00
|
|
|
dcId);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
connect(item.checker, endpoint);
|
|
|
|
connect(item.checkerv6, endpointv6);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProxiesBoxController::setupChecker(int id, const Checker &checker) {
|
|
|
|
using Connection = MTP::internal::AbstractConnection;
|
|
|
|
const auto pointer = checker.get();
|
|
|
|
pointer->connect(pointer, &Connection::connected, [=] {
|
|
|
|
const auto item = findById(id);
|
|
|
|
const auto pingTime = pointer->pingTime();
|
|
|
|
item->checker = nullptr;
|
|
|
|
item->checkerv6 = nullptr;
|
|
|
|
if (item->state == ItemState::Checking) {
|
|
|
|
item->state = ItemState::Available;
|
|
|
|
item->ping = pingTime;
|
|
|
|
updateView(*item);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
const auto failed = [=] {
|
|
|
|
const auto item = findById(id);
|
|
|
|
if (item->checker == pointer) {
|
|
|
|
item->checker = nullptr;
|
|
|
|
} else if (item->checkerv6 == pointer) {
|
|
|
|
item->checkerv6 = nullptr;
|
|
|
|
}
|
2018-05-04 21:28:26 +03:00
|
|
|
if (!item->checker
|
|
|
|
&& !item->checkerv6
|
|
|
|
&& item->state == ItemState::Checking) {
|
2018-04-30 19:49:03 +04:00
|
|
|
item->state = ItemState::Unavailable;
|
|
|
|
updateView(*item);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
pointer->connect(pointer, &Connection::disconnected, failed);
|
|
|
|
pointer->connect(pointer, &Connection::error, failed);
|
2018-04-28 13:27:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
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 18:06:59 +04:00
|
|
|
for (const auto &item : base::reversed(_list)) {
|
2018-04-28 13:27:43 +04: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 18:06:59 +04: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 13:27:43 +04:00
|
|
|
void ProxiesBoxController::deleteItem(int id) {
|
|
|
|
setDeleted(id, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProxiesBoxController::restoreItem(int id) {
|
|
|
|
setDeleted(id, false);
|
|
|
|
}
|
|
|
|
|
2018-05-05 16:14:46 +03:00
|
|
|
void ProxiesBoxController::shareItem(int id) {
|
|
|
|
share(findById(id)->data);
|
|
|
|
}
|
|
|
|
|
2018-04-28 18:06:59 +04: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 13:27:43 +04:00
|
|
|
void ProxiesBoxController::setDeleted(int id, bool deleted) {
|
|
|
|
auto item = findById(id);
|
|
|
|
item->deleted = deleted;
|
2018-04-28 18:06:59 +04: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 13:27:43 +04: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) {
|
2018-04-28 19:58:22 +04:00
|
|
|
replaceItemWith(i, j);
|
2018-04-28 13:27:43 +04:00
|
|
|
} else {
|
2018-04-28 19:58:22 +04:00
|
|
|
replaceItemValue(i, result);
|
2018-04-28 13:27:43 +04:00
|
|
|
}
|
2018-05-05 16:14:46 +03:00
|
|
|
}, [=](const ProxyData &proxy) {
|
|
|
|
share(proxy);
|
2018-04-28 13:27:43 +04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-04-28 19:58:22 +04:00
|
|
|
void ProxiesBoxController::replaceItemWith(
|
|
|
|
std::vector<Item>::iterator which,
|
|
|
|
std::vector<Item>::iterator with) {
|
|
|
|
auto &proxies = Global::RefProxiesList();
|
|
|
|
proxies.erase(ranges::remove(proxies, which->data), end(proxies));
|
|
|
|
|
|
|
|
_views.fire({ which->id });
|
|
|
|
_list.erase(which);
|
|
|
|
|
|
|
|
if (with->deleted) {
|
|
|
|
restoreItem(with->id);
|
|
|
|
}
|
|
|
|
applyItem(with->id);
|
|
|
|
saveDelayed();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProxiesBoxController::replaceItemValue(
|
|
|
|
std::vector<Item>::iterator which,
|
|
|
|
const ProxyData &proxy) {
|
|
|
|
if (which->deleted) {
|
|
|
|
restoreItem(which->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto &proxies = Global::RefProxiesList();
|
|
|
|
const auto i = ranges::find(proxies, which->data);
|
|
|
|
Assert(i != end(proxies));
|
|
|
|
*i = proxy;
|
|
|
|
which->data = proxy;
|
2018-04-30 20:11:36 +04:00
|
|
|
refreshChecker(*which);
|
2018-04-28 19:58:22 +04:00
|
|
|
|
|
|
|
applyItem(which->id);
|
|
|
|
saveDelayed();
|
|
|
|
}
|
|
|
|
|
2018-04-28 13:27:43 +04:00
|
|
|
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);
|
|
|
|
}
|
2018-04-28 19:58:22 +04:00
|
|
|
applyItem(j->id);
|
2018-04-28 13:27:43 +04:00
|
|
|
} else {
|
2018-04-28 19:58:22 +04:00
|
|
|
addNewItem(result);
|
2018-04-28 13:27:43 +04:00
|
|
|
}
|
2018-05-05 16:14:46 +03:00
|
|
|
}, [=](const ProxyData &proxy) {
|
|
|
|
share(proxy);
|
2018-04-28 13:27:43 +04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-04-28 19:58:22 +04:00
|
|
|
void ProxiesBoxController::addNewItem(const ProxyData &proxy) {
|
|
|
|
auto &proxies = Global::RefProxiesList();
|
|
|
|
proxies.push_back(proxy);
|
|
|
|
|
|
|
|
_list.push_back({ ++_idCounter, proxy });
|
2018-04-30 20:11:36 +04:00
|
|
|
refreshChecker(_list.back());
|
2018-04-28 19:58:22 +04:00
|
|
|
applyItem(_list.back().id);
|
|
|
|
}
|
|
|
|
|
2018-04-28 18:06:59 +04:00
|
|
|
bool ProxiesBoxController::setProxyEnabled(bool enabled) {
|
2018-05-04 21:28:26 +03:00
|
|
|
if (Global::UseProxy() == enabled) {
|
|
|
|
return true;
|
|
|
|
} else if (enabled) {
|
2018-04-28 18:06:59 +04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-05-07 00:29:53 +03:00
|
|
|
void ProxiesBoxController::setProxyForCalls(bool enabled) {
|
|
|
|
if (Global::UseProxyForCalls() == enabled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Global::SetUseProxyForCalls(enabled);
|
|
|
|
if (Global::UseProxy() && Global::SelectedProxy().supportsCalls()) {
|
|
|
|
Global::RefConnectionTypeChanged().notify();
|
|
|
|
}
|
|
|
|
saveDelayed();
|
|
|
|
}
|
|
|
|
|
2018-04-28 19:58:22 +04:00
|
|
|
void ProxiesBoxController::setTryIPv6(bool enabled) {
|
|
|
|
if (Global::TryIPv6() == enabled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Global::SetTryIPv6(enabled);
|
|
|
|
applyChanges();
|
|
|
|
}
|
|
|
|
|
2018-04-28 18:06:59 +04:00
|
|
|
void ProxiesBoxController::applyChanges() {
|
|
|
|
Sandbox::refreshGlobalProxy();
|
|
|
|
Global::RefConnectionTypeChanged().notify();
|
|
|
|
MTP::restart();
|
|
|
|
saveDelayed();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProxiesBoxController::saveDelayed() {
|
|
|
|
_saveTimer.callOnce(kSaveSettingsDelayedTimeout);
|
|
|
|
}
|
|
|
|
|
2018-04-28 13:27:43 +04:00
|
|
|
auto ProxiesBoxController::views() const -> rpl::producer<ItemView> {
|
|
|
|
return _views.events();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProxiesBoxController::updateView(const Item &item) {
|
|
|
|
const auto ping = 0;
|
|
|
|
const auto selected = (Global::SelectedProxy() == item.data);
|
|
|
|
const auto deleted = item.deleted;
|
|
|
|
const auto type = [&] {
|
|
|
|
switch (item.data.type) {
|
2018-05-05 16:14:46 +03:00
|
|
|
case Type::Http:
|
2018-04-28 13:27:43 +04:00
|
|
|
return qsl("HTTP");
|
2018-05-05 16:14:46 +03:00
|
|
|
case Type::Socks5:
|
2018-04-28 13:27:43 +04:00
|
|
|
return qsl("SOCKS5");
|
2018-05-05 16:14:46 +03:00
|
|
|
case Type::Mtproto:
|
2018-04-28 13:27:43 +04:00
|
|
|
return qsl("MTPROTO");
|
|
|
|
}
|
|
|
|
Unexpected("Proxy type in ProxiesBoxController::updateView.");
|
|
|
|
}();
|
2018-04-30 20:11:36 +04:00
|
|
|
const auto state = [&] {
|
|
|
|
if (!selected || !Global::UseProxy()) {
|
|
|
|
return item.state;
|
|
|
|
} else if (MTP::dcstate() == MTP::ConnectedState) {
|
|
|
|
return ItemState::Online;
|
|
|
|
}
|
|
|
|
return ItemState::Connecting;
|
|
|
|
}();
|
2018-05-07 00:29:53 +03:00
|
|
|
const auto supportsShare = (item.data.type == Type::Socks5)
|
2018-05-05 16:14:46 +03:00
|
|
|
|| (item.data.type == Type::Mtproto);
|
2018-05-07 00:29:53 +03:00
|
|
|
const auto supportsCalls = item.data.supportsCalls();
|
2018-04-28 13:27:43 +04:00
|
|
|
_views.fire({
|
|
|
|
item.id,
|
|
|
|
type,
|
|
|
|
item.data.host,
|
|
|
|
item.data.port,
|
2018-04-30 19:49:03 +04:00
|
|
|
item.ping,
|
2018-04-28 13:27:43 +04:00
|
|
|
!deleted && selected,
|
|
|
|
deleted,
|
2018-05-07 00:29:53 +03:00
|
|
|
!deleted && supportsShare,
|
|
|
|
supportsCalls,
|
2018-04-30 20:11:36 +04:00
|
|
|
state });
|
2018-04-28 13:27:43 +04:00
|
|
|
}
|
2018-04-28 18:06:59 +04:00
|
|
|
|
2018-05-05 16:14:46 +03:00
|
|
|
void ProxiesBoxController::share(const ProxyData &proxy) {
|
|
|
|
if (proxy.type == Type::Http) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto link = qsl("https://t.me/")
|
|
|
|
+ (proxy.type == Type::Socks5 ? "socks" : "proxy")
|
|
|
|
+ "?server=" + proxy.host + "&port=" + QString::number(proxy.port)
|
|
|
|
+ ((proxy.type == Type::Socks5 && !proxy.user.isEmpty())
|
|
|
|
? "&user=" + qthelp::url_encode(proxy.user) : "")
|
|
|
|
+ ((proxy.type == Type::Socks5 && !proxy.password.isEmpty())
|
|
|
|
? "&pass=" + qthelp::url_encode(proxy.password) : "")
|
|
|
|
+ ((proxy.type == Type::Mtproto && !proxy.password.isEmpty())
|
|
|
|
? "&secret=" + proxy.password : "");
|
|
|
|
Application::clipboard()->setText(link);
|
|
|
|
Ui::Toast::Show(lang(lng_username_copied));
|
|
|
|
}
|
|
|
|
|
2018-04-28 18:06:59 +04:00
|
|
|
ProxiesBoxController::~ProxiesBoxController() {
|
|
|
|
if (_saveTimer.isActive()) {
|
|
|
|
App::CallDelayed(
|
|
|
|
kSaveSettingsDelayedTimeout,
|
|
|
|
QApplication::instance(),
|
|
|
|
[] { Local::writeSettings(); });
|
|
|
|
}
|
|
|
|
}
|