Remember connection type settings.

This commit is contained in:
John Preston 2017-06-28 01:03:37 +03:00
parent 7245319351
commit 9e6f2a5d2e
5 changed files with 89 additions and 28 deletions

View File

@ -43,6 +43,7 @@ void ConnectionBox::ShowApplyProxyConfirmation(const QMap<QString, QString> &fie
p.password = fields.value(qsl("pass")); p.password = fields.value(qsl("pass"));
p.port = fields.value(qsl("port")).toInt(); p.port = fields.value(qsl("port")).toInt();
Global::SetConnectionType(dbictTcpProxy); Global::SetConnectionType(dbictTcpProxy);
Global::SetLastProxyType(dbictTcpProxy);
Global::SetConnectionProxy(p); Global::SetConnectionProxy(p);
Local::writeSettings(); Local::writeSettings();
Global::RefConnectionTypeChanged().notify(); Global::RefConnectionTypeChanged().notify();
@ -79,13 +80,21 @@ void ConnectionBox::prepare() {
connect(_portInput, SIGNAL(submitted(bool)), this, SLOT(onSubmit())); connect(_portInput, SIGNAL(submitted(bool)), this, SLOT(onSubmit()));
connect(_userInput, SIGNAL(submitted(bool)), this, SLOT(onSubmit())); connect(_userInput, SIGNAL(submitted(bool)), this, SLOT(onSubmit()));
connect(_passwordInput, SIGNAL(submitted(bool)), this, SLOT(onSubmit())); connect(_passwordInput, SIGNAL(submitted(bool)), this, SLOT(onSubmit()));
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()));
updateControlsVisibility(); updateControlsVisibility();
} }
bool ConnectionBox::badProxyValue() const {
return (_hostInput->getLastText().isEmpty() || !_portInput->getLastText().toInt());
}
void ConnectionBox::updateControlsVisibility() { void ConnectionBox::updateControlsVisibility() {
auto newHeight = st::boxOptionListPadding.top() + _autoRadio->heightNoMargins() + st::boxOptionListSkip + _httpProxyRadio->heightNoMargins() + st::boxOptionListSkip + _tcpProxyRadio->heightNoMargins() + st::boxOptionListSkip + st::connectionIPv6Skip + _tryIPv6->heightNoMargins() + st::boxOptionListPadding.bottom() + st::boxPadding.bottom(); auto newHeight = st::boxOptionListPadding.top() + _autoRadio->heightNoMargins() + st::boxOptionListSkip + _httpProxyRadio->heightNoMargins() + st::boxOptionListSkip + _tcpProxyRadio->heightNoMargins() + st::boxOptionListSkip + st::connectionIPv6Skip + _tryIPv6->heightNoMargins() + st::boxOptionListPadding.bottom() + st::boxPadding.bottom();
if (_typeGroup->value() == dbictAuto) { if (_typeGroup->value() == dbictAuto && badProxyValue()) {
_hostInput->hide(); _hostInput->hide();
_portInput->hide(); _portInput->hide();
_userInput->hide(); _userInput->hide();
@ -103,7 +112,7 @@ void ConnectionBox::updateControlsVisibility() {
} }
void ConnectionBox::setInnerFocus() { void ConnectionBox::setInnerFocus() {
if (_hostInput->isHidden()) { if (_typeGroup->value() == dbictAuto) {
setFocus(); setFocus();
} else { } else {
_hostInput->setFocusFast(); _hostInput->setFocusFast();
@ -122,12 +131,15 @@ void ConnectionBox::updateControlsPosition() {
_httpProxyRadio->moveToLeft(st::boxPadding.left() + st::boxOptionListPadding.left(), _autoRadio->bottomNoMargins() + st::boxOptionListSkip); _httpProxyRadio->moveToLeft(st::boxPadding.left() + st::boxOptionListPadding.left(), _autoRadio->bottomNoMargins() + st::boxOptionListSkip);
auto inputy = 0; auto inputy = 0;
if (type == dbictHttpProxy) { auto fieldsVisible = (type != dbictAuto) || (!badProxyValue() && Global::LastProxyType() != dbictAuto);
auto fieldsBelowHttp = fieldsVisible && (type == dbictHttpProxy || (type == dbictAuto && Global::LastProxyType() == dbictHttpProxy));
auto fieldsBelowTcp = fieldsVisible && (type == dbictTcpProxy || (type == dbictAuto && Global::LastProxyType() == dbictTcpProxy));
if (fieldsBelowHttp) {
inputy = _httpProxyRadio->bottomNoMargins() + st::boxOptionInputSkip; inputy = _httpProxyRadio->bottomNoMargins() + st::boxOptionInputSkip;
_tcpProxyRadio->moveToLeft(st::boxPadding.left() + st::boxOptionListPadding.left(), inputy + st::boxOptionInputSkip + 2 * _hostInput->height() + st::boxOptionListSkip); _tcpProxyRadio->moveToLeft(st::boxPadding.left() + st::boxOptionListPadding.left(), inputy + st::boxOptionInputSkip + 2 * _hostInput->height() + st::boxOptionListSkip);
} else { } else {
_tcpProxyRadio->moveToLeft(st::boxPadding.left() + st::boxOptionListPadding.left(), _httpProxyRadio->bottomNoMargins() + st::boxOptionListSkip); _tcpProxyRadio->moveToLeft(st::boxPadding.left() + st::boxOptionListPadding.left(), _httpProxyRadio->bottomNoMargins() + st::boxOptionListSkip);
if (type == dbictTcpProxy) { if (fieldsBelowTcp) {
inputy = _tcpProxyRadio->bottomNoMargins() + st::boxOptionInputSkip; inputy = _tcpProxyRadio->bottomNoMargins() + st::boxOptionInputSkip;
} }
} }
@ -139,13 +151,17 @@ void ConnectionBox::updateControlsPosition() {
_passwordInput->moveToRight(st::boxPadding.right(), _userInput->y()); _passwordInput->moveToRight(st::boxPadding.right(), _userInput->y());
} }
auto tryipv6y = ((type == dbictTcpProxy) ? _userInput->bottomNoMargins() : _tcpProxyRadio->bottomNoMargins()) + st::boxOptionListSkip + st::connectionIPv6Skip; auto tryipv6y = (fieldsBelowTcp ? _userInput->bottomNoMargins() : _tcpProxyRadio->bottomNoMargins()) + st::boxOptionListSkip + st::connectionIPv6Skip;
_tryIPv6->moveToLeft(st::boxPadding.left() + st::boxOptionListPadding.left(), tryipv6y); _tryIPv6->moveToLeft(st::boxPadding.left() + st::boxOptionListPadding.left(), tryipv6y);
} }
void ConnectionBox::typeChanged(DBIConnectionType type) { void ConnectionBox::typeChanged(DBIConnectionType type) {
if (type == dbictAuto) {
setFocus();
}
updateControlsVisibility(); updateControlsVisibility();
if (type != dbictAuto) { if (type != dbictAuto) {
Global::SetLastProxyType(type);
if (!_hostInput->hasFocus() && !_portInput->hasFocus() && !_userInput->hasFocus() && !_passwordInput->hasFocus()) { if (!_hostInput->hasFocus() && !_portInput->hasFocus() && !_userInput->hasFocus() && !_passwordInput->hasFocus()) {
_hostInput->setFocusFast(); _hostInput->setFocusFast();
} }
@ -157,7 +173,16 @@ void ConnectionBox::typeChanged(DBIConnectionType type) {
update(); update();
} }
void ConnectionBox::onFieldFocus() {
if (Global::LastProxyType() == dbictHttpProxy) {
_typeGroup->setValue(dbictHttpProxy);
} else if (Global::LastProxyType() == dbictTcpProxy) {
_typeGroup->setValue(dbictTcpProxy);
}
}
void ConnectionBox::onSubmit() { void ConnectionBox::onSubmit() {
onFieldFocus();
if (_hostInput->hasFocus()) { if (_hostInput->hasFocus()) {
if (!_hostInput->getLastText().trimmed().isEmpty()) { if (!_hostInput->getLastText().trimmed().isEmpty()) {
_portInput->setFocus(); _portInput->setFocus();
@ -186,30 +211,33 @@ void ConnectionBox::onSubmit() {
} }
void ConnectionBox::onSave() { void ConnectionBox::onSave() {
auto p = ProxyData();
p.host = _hostInput->getLastText().trimmed();
p.user = _userInput->getLastText().trimmed();
p.password = _passwordInput->getLastText().trimmed();
p.port = _portInput->getLastText().toInt();
auto type = _typeGroup->value(); auto type = _typeGroup->value();
if (type == dbictAuto) { if (type == dbictAuto) {
Global::SetConnectionType(type); if (p.host.isEmpty() || !p.port) {
Global::SetConnectionProxy(ProxyData()); p = ProxyData();
}
#ifndef TDESKTOP_DISABLE_NETWORK_PROXY #ifndef TDESKTOP_DISABLE_NETWORK_PROXY
QNetworkProxyFactory::setUseSystemConfiguration(false); QNetworkProxyFactory::setUseSystemConfiguration(false);
QNetworkProxyFactory::setUseSystemConfiguration(true); QNetworkProxyFactory::setUseSystemConfiguration(true);
#endif // !TDESKTOP_DISABLE_NETWORK_PROXY #endif // !TDESKTOP_DISABLE_NETWORK_PROXY
} else { } else {
ProxyData p;
p.host = _hostInput->getLastText().trimmed();
p.user = _userInput->getLastText().trimmed();
p.password = _passwordInput->getLastText().trimmed();
p.port = _portInput->getLastText().toInt();
if (p.host.isEmpty()) { if (p.host.isEmpty()) {
_hostInput->setFocus(); _hostInput->showError();
return; return;
} else if (!p.port) { } else if (!p.port) {
_portInput->setFocus(); _portInput->showError();
return; return;
} }
Global::SetConnectionType(type); Global::SetLastProxyType(type);
Global::SetConnectionProxy(p);
} }
Global::SetConnectionType(type);
Global::SetConnectionProxy(p);
if (cPlatform() == dbipWindows && Global::TryIPv6() != _tryIPv6->checked()) { if (cPlatform() == dbipWindows && Global::TryIPv6() != _tryIPv6->checked()) {
Global::SetTryIPv6(_tryIPv6->checked()); Global::SetTryIPv6(_tryIPv6->checked());
Local::writeSettings(); Local::writeSettings();

View File

@ -49,12 +49,14 @@ protected:
private slots: private slots:
void onSubmit(); void onSubmit();
void onFieldFocus();
void onSave(); void onSave();
private: private:
void typeChanged(DBIConnectionType type); void typeChanged(DBIConnectionType type);
void updateControlsVisibility(); void updateControlsVisibility();
void updateControlsPosition(); void updateControlsPosition();
bool badProxyValue() const;
object_ptr<Ui::InputField> _hostInput; object_ptr<Ui::InputField> _hostInput;
object_ptr<Ui::PortInput> _portInput; object_ptr<Ui::PortInput> _portInput;

View File

@ -664,6 +664,7 @@ struct Data {
bool NotificationsDemoIsShown = false; bool NotificationsDemoIsShown = false;
DBIConnectionType ConnectionType = dbictAuto; DBIConnectionType ConnectionType = dbictAuto;
DBIConnectionType LastProxyType = dbictAuto;
bool TryIPv6 = (cPlatform() == dbipWindows) ? false : true; bool TryIPv6 = (cPlatform() == dbipWindows) ? false : true;
ProxyData ConnectionProxy; ProxyData ConnectionProxy;
base::Observable<void> ConnectionTypeChanged; base::Observable<void> ConnectionTypeChanged;
@ -784,6 +785,7 @@ DefineVar(Global, Notify::ScreenCorner, NotificationsCorner);
DefineVar(Global, bool, NotificationsDemoIsShown); DefineVar(Global, bool, NotificationsDemoIsShown);
DefineVar(Global, DBIConnectionType, ConnectionType); DefineVar(Global, DBIConnectionType, ConnectionType);
DefineVar(Global, DBIConnectionType, LastProxyType);
DefineVar(Global, bool, TryIPv6); DefineVar(Global, bool, TryIPv6);
DefineVar(Global, ProxyData, ConnectionProxy); DefineVar(Global, ProxyData, ConnectionProxy);
DefineRefVar(Global, base::Observable<void>, ConnectionTypeChanged); DefineRefVar(Global, base::Observable<void>, ConnectionTypeChanged);

View File

@ -377,6 +377,7 @@ DeclareVar(Notify::ScreenCorner, NotificationsCorner);
DeclareVar(bool, NotificationsDemoIsShown); DeclareVar(bool, NotificationsDemoIsShown);
DeclareVar(DBIConnectionType, ConnectionType); DeclareVar(DBIConnectionType, ConnectionType);
DeclareVar(DBIConnectionType, LastProxyType);
DeclareVar(bool, TryIPv6); DeclareVar(bool, TryIPv6);
DeclareVar(ProxyData, ConnectionProxy); DeclareVar(ProxyData, ConnectionProxy);
DeclareRefVar(base::Observable<void>, ConnectionTypeChanged); DeclareRefVar(base::Observable<void>, ConnectionTypeChanged);

View File

@ -522,7 +522,7 @@ enum {
dbiAutoUpdate = 0x0c, dbiAutoUpdate = 0x0c,
dbiLastUpdateCheck = 0x0d, dbiLastUpdateCheck = 0x0d,
dbiWindowPosition = 0x0e, dbiWindowPosition = 0x0e,
dbiConnectionType = 0x0f, dbiConnectionTypeOld = 0x0f,
// 0x10 reserved // 0x10 reserved
dbiDefaultAttach = 0x11, dbiDefaultAttach = 0x11,
dbiCatsAndDogs = 0x12, dbiCatsAndDogs = 0x12,
@ -576,6 +576,7 @@ enum {
dbiLastSeenWarningSeenOld = 0x4c, dbiLastSeenWarningSeenOld = 0x4c,
dbiAuthSessionData = 0x4d, dbiAuthSessionData = 0x4d,
dbiLangPackKey = 0x4e, dbiLangPackKey = 0x4e,
dbiConnectionType = 0x4f,
dbiEncryptedWithSalt = 333, dbiEncryptedWithSalt = 333,
dbiEncrypted = 444, dbiEncrypted = 444,
@ -1128,7 +1129,7 @@ bool _readSetting(quint32 blockId, QDataStream &stream, int version, ReadSetting
Global::RefWorkMode().set(newMode()); Global::RefWorkMode().set(newMode());
} break; } break;
case dbiConnectionType: { case dbiConnectionTypeOld: {
qint32 v; qint32 v;
stream >> v; stream >> v;
if (!_checkStreamStatus(stream)) return false; if (!_checkStreamStatus(stream)) return false;
@ -1148,6 +1149,36 @@ bool _readSetting(quint32 blockId, QDataStream &stream, int version, ReadSetting
case dbictHttpAuto: case dbictHttpAuto:
default: Global::SetConnectionType(dbictAuto); break; default: Global::SetConnectionType(dbictAuto); break;
}; };
Global::SetLastProxyType(Global::ConnectionType());
} break;
case dbiConnectionType: {
ProxyData p;
qint32 connectionType, lastProxyType, port;
stream >> connectionType >> lastProxyType >> p.host >> port >> p.user >> p.password;
if (!_checkStreamStatus(stream)) return false;
p.port = port;
switch (connectionType) {
case dbictHttpProxy:
case dbictTcpProxy: {
Global::SetConnectionType(DBIConnectionType(lastProxyType));
} break;
case dbictHttpAuto:
default: Global::SetConnectionType(dbictAuto); break;
};
switch (lastProxyType) {
case dbictHttpProxy:
case dbictTcpProxy: {
Global::SetLastProxyType(DBIConnectionType(lastProxyType));
Global::SetConnectionProxy(p);
} break;
case dbictHttpAuto:
default: {
Global::SetLastProxyType(dbictAuto);
Global::SetConnectionProxy(ProxyData());
} break;
}
} break; } break;
case dbiThemeKey: { case dbiThemeKey: {
@ -2310,11 +2341,10 @@ void writeSettings() {
quint32 size = 12 * (sizeof(quint32) + sizeof(qint32)); quint32 size = 12 * (sizeof(quint32) + sizeof(qint32));
size += sizeof(quint32) + Serialize::bytearraySize(dcOptionsSerialized); size += sizeof(quint32) + Serialize::bytearraySize(dcOptionsSerialized);
size += sizeof(quint32) + sizeof(qint32); auto &proxy = Global::ConnectionProxy();
if (Global::ConnectionType() == dbictHttpProxy || Global::ConnectionType() == dbictTcpProxy) { size += sizeof(quint32) + sizeof(qint32) + sizeof(qint32);
auto &proxy = Global::ConnectionProxy(); size += Serialize::stringSize(proxy.host) + sizeof(qint32) + Serialize::stringSize(proxy.user) + Serialize::stringSize(proxy.password);
size += Serialize::stringSize(proxy.host) + sizeof(qint32) + Serialize::stringSize(proxy.user) + Serialize::stringSize(proxy.password);
}
if (_themeKey) { if (_themeKey) {
size += sizeof(quint32) + sizeof(quint64); size += sizeof(quint32) + sizeof(quint64);
} }
@ -2338,11 +2368,9 @@ void writeSettings() {
data.stream << quint32(dbiScale) << qint32(cConfigScale()); data.stream << quint32(dbiScale) << qint32(cConfigScale());
data.stream << quint32(dbiDcOptions) << dcOptionsSerialized; data.stream << quint32(dbiDcOptions) << dcOptionsSerialized;
data.stream << quint32(dbiConnectionType) << qint32(Global::ConnectionType()); data.stream << quint32(dbiConnectionType) << qint32(Global::ConnectionType()) << qint32(Global::LastProxyType());
if (Global::ConnectionType() == dbictHttpProxy || Global::ConnectionType() == dbictTcpProxy) { data.stream << proxy.host << qint32(proxy.port) << proxy.user << proxy.password;
auto &proxy = Global::ConnectionProxy();
data.stream << proxy.host << qint32(proxy.port) << proxy.user << proxy.password;
}
data.stream << quint32(dbiTryIPv6) << qint32(Global::TryIPv6()); data.stream << quint32(dbiTryIPv6) << qint32(Global::TryIPv6());
if (_themeKey) { if (_themeKey) {
data.stream << quint32(dbiThemeKey) << quint64(_themeKey); data.stream << quint32(dbiThemeKey) << quint64(_themeKey);