Apply language with app restart if logged in.

Confirm and restart if logged in, just quickly apply if not.
This commit is contained in:
John Preston 2017-05-30 20:58:25 +03:00
parent d51fd7b3ad
commit 665a1acfd8
14 changed files with 138 additions and 68 deletions

View File

@ -84,19 +84,10 @@ void LanguageBox::Inner::refresh() {
void LanguageBox::Inner::languageChanged(int languageIndex) { void LanguageBox::Inner::languageChanged(int languageIndex) {
Expects(languageIndex >= 0 && languageIndex < _languages->size()); Expects(languageIndex >= 0 && languageIndex < _languages->size());
auto currentId = Lang::Current().id();
auto languageId = (*_languages)[languageIndex].id;
if (languageId == currentId) {
return;
}
if (languageId == qsl("custom")) {
activateCurrent(); activateCurrent();
Lang::Current().chooseCustomFile(); auto languageId = (*_languages)[languageIndex].id;
} else {
Lang::CurrentCloudManager().switchToLanguage(languageId); Lang::CurrentCloudManager().switchToLanguage(languageId);
} }
}
void LanguageBox::Inner::activateCurrent() { void LanguageBox::Inner::activateCurrent() {
auto currentId = Lang::Current().id(); auto currentId = Lang::Current().id();

View File

@ -53,10 +53,6 @@ public:
protected: protected:
void resizeEvent(QResizeEvent *e) override; void resizeEvent(QResizeEvent *e) override;
bool hasChangeLanguageNoCover() const override {
return true;
}
private slots: private slots:
void onInputChange(); void onInputChange();
void onCheckRequest(); void onCheckRequest();

View File

@ -69,6 +69,9 @@ PwdCheckWidget::PwdCheckWidget(QWidget *parent, Widget::Data *data) : Step(paren
void PwdCheckWidget::refreshLang() { void PwdCheckWidget::refreshLang() {
if (_toRecover) _toRecover->setText(lang(lng_signin_recover)); if (_toRecover) _toRecover->setText(lang(lng_signin_recover));
if (_toPassword) _toPassword->setText(lang(lng_signin_try_password)); if (_toPassword) _toPassword->setText(lang(lng_signin_try_password));
if (!_hint.isEmpty()) {
_pwdHint->setText(lng_signin_hint(lt_password_hint, _hint));
}
updateControlsGeometry(); updateControlsGeometry();
} }

View File

@ -113,7 +113,7 @@ void Widget::createLanguageLink() {
_changeLanguage->entity()->setClickedCallback([this, languageId] { _changeLanguage->entity()->setClickedCallback([this, languageId] {
Lang::CurrentCloudManager().switchToLanguage(languageId); Lang::CurrentCloudManager().switchToLanguage(languageId);
}); });
_changeLanguage->toggleAnimated(getStep()->hasChangeLanguage()); _changeLanguage->toggleAnimated(!_resetAccount);
updateControlsGeometry(); updateControlsGeometry();
}; };
@ -189,7 +189,7 @@ void Widget::historyMove(Direction direction) {
auto stepHasCover = getStep()->hasCover(); auto stepHasCover = getStep()->hasCover();
_settings->toggleAnimated(!stepHasCover); _settings->toggleAnimated(!stepHasCover);
if (_update) _update->toggleAnimated(!stepHasCover); if (_update) _update->toggleAnimated(!stepHasCover);
if (_changeLanguage) _changeLanguage->toggleAnimated(getStep()->hasChangeLanguage()); if (_changeLanguage) _changeLanguage->toggleAnimated(!_resetAccount);
_next->setText([this] { return getStep()->nextButtonText(); }); _next->setText([this] { return getStep()->nextButtonText(); });
if (_resetAccount) _resetAccount->hideAnimated(); if (_resetAccount) _resetAccount->hideAnimated();
getStep()->showAnimated(direction); getStep()->showAnimated(direction);
@ -238,6 +238,7 @@ void Widget::showResetButton() {
updateControlsGeometry(); updateControlsGeometry();
} }
_resetAccount->showAnimated(); _resetAccount->showAnimated();
if (_changeLanguage) _changeLanguage->hideAnimated();
} }
void Widget::resetAccount() { void Widget::resetAccount() {
@ -298,7 +299,7 @@ void Widget::showControls() {
auto hasCover = getStep()->hasCover(); auto hasCover = getStep()->hasCover();
_settings->toggleFast(!hasCover); _settings->toggleFast(!hasCover);
if (_update) _update->toggleFast(!hasCover); if (_update) _update->toggleFast(!hasCover);
if (_changeLanguage) _changeLanguage->toggleFast(getStep()->hasChangeLanguage()); if (_changeLanguage) _changeLanguage->toggleFast(!_resetAccount);
_back->toggleFast(getStep()->hasBack()); _back->toggleFast(getStep()->hasBack());
} }

View File

@ -111,9 +111,6 @@ public:
bool animating() const; bool animating() const;
bool hasCover() const; bool hasCover() const;
bool hasChangeLanguage() const {
return hasCover() || hasChangeLanguageNoCover();
}
virtual bool hasBack() const; virtual bool hasBack() const;
virtual void activate(); virtual void activate();
virtual void cancelled(); virtual void cancelled();
@ -164,9 +161,6 @@ public:
void showResetButton() { void showResetButton() {
if (_showResetCallback) _showResetCallback(); if (_showResetCallback) _showResetCallback();
} }
virtual bool hasChangeLanguageNoCover() const {
return false;
}
private: private:
struct CoverAnimation { struct CoverAnimation {

View File

@ -27,6 +27,8 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
#include "apiwrap.h" #include "apiwrap.h"
#include "auth_session.h" #include "auth_session.h"
#include "boxes/confirm_box.h" #include "boxes/confirm_box.h"
#include "lang/lang_file_parser.h"
#include "core/file_utilities.h"
namespace Lang { namespace Lang {
@ -87,6 +89,9 @@ void CloudManager::applyLangPackDifference(const MTPLangPackDifference &differen
auto langpackId = qs(langpack.vlang_code); auto langpackId = qs(langpack.vlang_code);
if (needToApplyLangPack(langpackId)) { if (needToApplyLangPack(langpackId)) {
applyLangPackData(langpack); applyLangPackData(langpack);
if (_restartAfterSwitch) {
App::restart();
}
} else { } else {
LOG(("Lang Warning: Ignoring update for '%1' because our language is '%2'").arg(langpackId).arg(_langpack.id())); LOG(("Lang Warning: Ignoring update for '%1' because our language is '%2'").arg(langpackId).arg(_langpack.id()));
} }
@ -152,17 +157,13 @@ bool CloudManager::showOfferSwitchBox() {
if (_offerSwitchToId.isEmpty()) { if (_offerSwitchToId.isEmpty()) {
return; return;
} }
if (_offerSwitchToData) { request(_langPackRequestId).cancel();
t_assert(_offerSwitchToData->type() == mtpc_langPackDifference); performSwitchAndRestart(_offerSwitchToId);
applyLangPackData(base::take(_offerSwitchToData)->c_langPackDifference());
} else {
switchToLanguage(_offerSwitchToId);
}
}, [this] { }, [this] {
Ui::hideLayer(); Ui::hideLayer();
changeIdAndReInitConnection(DefaultLanguageId()); changeIdAndReInitConnection(DefaultLanguageId());
Local::writeLangPack(); Local::writeLangPack();
})); }), KeepOtherLayers);
return true; return true;
} }
@ -178,11 +179,108 @@ void CloudManager::applyLangPackData(const MTPDlangPackDifference &data) {
} }
} }
void CloudManager::switchToLanguage(const QString &id) { bool CloudManager::canApplyWithoutRestart(const QString &id) const {
if (id == qstr("TEST_X") || id == qstr("TEST_0")) {
return true;
}
// We don't support instant language switch if the auth session exists :(
return !AuthSession::Exists();
}
void CloudManager::resetToDefault() {
performSwitch(DefaultLanguageId());
}
void CloudManager::switchToLanguage(QString id) {
if (id.isEmpty()) {
id = DefaultLanguageId();
}
if (_langpack.id() == id) {
return;
}
request(_switchingToLanguageRequest).cancel();
if (id == qstr("custom")) {
performSwitchToCustom();
} else if (canApplyWithoutRestart(id)) {
performSwitch(id);
} else {
QVector<MTPstring> keys;
keys.reserve(3);
keys.push_back(MTP_string("lng_sure_save_language"));
keys.push_back(MTP_string("lng_box_ok"));
keys.push_back(MTP_string("lng_cancel"));
_switchingToLanguageRequest = request(MTPlangpack_GetStrings(MTP_string(id), MTP_vector<MTPstring>(std::move(keys)))).done([this, id](const MTPVector<MTPLangPackString> &result) {
auto values = Instance::ParseStrings(result);
auto getValue = [&values](LangKey key) {
auto it = values.find(key);
return (it == values.cend()) ? GetOriginalValue(key) : it->second;
};
auto text = getValue(lng_sure_save_language);
auto save = getValue(lng_box_ok);
auto cancel = getValue(lng_cancel);
Ui::show(Box<ConfirmBox>(text, save, cancel, [this, id] {
performSwitchAndRestart(id);
}), KeepOtherLayers);
}).send();
}
}
void CloudManager::performSwitchToCustom() {
auto filter = qsl("Language files (*.strings)");
auto title = qsl("Choose language .strings file");
FileDialog::GetOpenPath(title, filter, [weak = base::weak_unique_ptr<CloudManager>(this)](const FileDialog::OpenResult &result) {
if (!weak || result.paths.isEmpty()) {
return;
}
auto filePath = result.paths.front();
Lang::FileParser loader(filePath, { lng_sure_save_language, lng_box_ok, lng_cancel });
if (loader.errors().isEmpty()) {
weak->request(weak->_switchingToLanguageRequest).cancel();
if (weak->canApplyWithoutRestart(qsl("custom"))) {
weak->_langpack.switchToCustomFile(filePath);
} else {
auto values = loader.found();
auto getValue = [&values](LangKey key) {
auto it = values.find(key);
return (it == values.cend()) ? GetOriginalValue(key) : it.value();
};
auto text = getValue(lng_sure_save_language);
auto save = getValue(lng_box_ok);
auto cancel = getValue(lng_cancel);
Ui::show(Box<ConfirmBox>(text, save, cancel, [weak, filePath] {
weak->_langpack.switchToCustomFile(filePath);
App::restart();
}), KeepOtherLayers);
}
} else {
Ui::show(Box<InformBox>("Custom lang failed :(\n\nError: " + loader.errors()), KeepOtherLayers);
}
});
}
void CloudManager::switchToTestLanguage() {
auto testLanguageId = (_langpack.id() == qstr("TEST_X")) ? qsl("TEST_0") : qsl("TEST_X");
performSwitch(testLanguageId);
}
void CloudManager::performSwitch(const QString &id) {
_restartAfterSwitch = false;
switchLangPackId(id); switchLangPackId(id);
requestLangPackDifference(); requestLangPackDifference();
} }
void CloudManager::performSwitchAndRestart(const QString &id) {
performSwitch(id);
if (_langPackRequestId) {
_restartAfterSwitch = true;
} else {
App::restart();
}
}
void CloudManager::switchLangPackId(const QString &id) { void CloudManager::switchLangPackId(const QString &id) {
auto currentId = _langpack.id(); auto currentId = _langpack.id();
auto notChanged = (currentId == id) || (currentId.isEmpty() && id == DefaultLanguageId()); auto notChanged = (currentId == id) || (currentId.isEmpty() && id == DefaultLanguageId());

View File

@ -21,6 +21,7 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
#pragma once #pragma once
#include "mtproto/sender.h" #include "mtproto/sender.h"
#include "base/weak_unique_ptr.h"
namespace MTP { namespace MTP {
class Instance; class Instance;
@ -30,7 +31,7 @@ namespace Lang {
class Instance; class Instance;
class CloudManager : private MTP::Sender, private base::Subscriber { class CloudManager : public base::enable_weak_from_this, private MTP::Sender, private base::Subscriber {
public: public:
CloudManager(Instance &langpack, gsl::not_null<MTP::Instance*> mtproto); CloudManager(Instance &langpack, gsl::not_null<MTP::Instance*> mtproto);
@ -50,7 +51,9 @@ public:
void requestLangPackDifference(); void requestLangPackDifference();
void applyLangPackDifference(const MTPLangPackDifference &difference); void applyLangPackDifference(const MTPLangPackDifference &difference);
void switchToLanguage(const QString &id); void resetToDefault();
void switchToLanguage(QString id);
void switchToTestLanguage();
void setSuggestedLanguage(const QString &langCode); void setSuggestedLanguage(const QString &langCode);
QString suggestedLanguage() const { QString suggestedLanguage() const {
return _suggestedLanguage; return _suggestedLanguage;
@ -60,6 +63,10 @@ public:
} }
private: private:
bool canApplyWithoutRestart(const QString &id) const;
void performSwitchToCustom();
void performSwitch(const QString &id);
void performSwitchAndRestart(const QString &id);
void offerSwitchLangPack(); void offerSwitchLangPack();
bool showOfferSwitchBox(); bool showOfferSwitchBox();
QString findOfferedLanguageName(); QString findOfferedLanguageName();
@ -76,12 +83,14 @@ private:
mtpRequestId _languagesRequestId = 0; mtpRequestId _languagesRequestId = 0;
QString _offerSwitchToId; QString _offerSwitchToId;
std::unique_ptr<MTPLangPackDifference> _offerSwitchToData; bool _restartAfterSwitch = false;
QString _suggestedLanguage; QString _suggestedLanguage;
bool _languageWasSuggested = false; bool _languageWasSuggested = false;
base::Observable<void> _firstLanguageSuggestion; base::Observable<void> _firstLanguageSuggestion;
mtpRequestId _switchingToLanguageRequest = 0;
}; };
inline bool operator==(const CloudManager::Language &a, const CloudManager::Language &b) { inline bool operator==(const CloudManager::Language &a, const CloudManager::Language &b) {

View File

@ -21,12 +21,11 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
#include "lang/lang_instance.h" #include "lang/lang_instance.h"
#include "messenger.h" #include "messenger.h"
#include "lang/lang_file_parser.h"
#include "storage/serialize_common.h" #include "storage/serialize_common.h"
#include "storage/localstorage.h" #include "storage/localstorage.h"
#include "platform/platform_specific.h" #include "platform/platform_specific.h"
#include "core/file_utilities.h"
#include "boxes/confirm_box.h" #include "boxes/confirm_box.h"
#include "lang/lang_file_parser.h"
namespace Lang { namespace Lang {
namespace { namespace {
@ -262,24 +261,6 @@ void Instance::switchToId(const QString &id) {
} }
} }
void Instance::chooseCustomFile() {
auto filter = qsl("Language files (*.strings)");
auto title = qsl("Choose language .strings file");
FileDialog::GetOpenPath(title, filter, [weak = base::weak_unique_ptr<Instance>(this)](const FileDialog::OpenResult &result) {
if (!weak || result.paths.isEmpty()) {
return;
}
auto filePath = result.paths.front();
Lang::FileParser loader(filePath, { lng_language_name });
if (loader.errors().isEmpty()) {
weak->switchToCustomFile(filePath);
} else {
Ui::show(Box<InformBox>("Custom lang failed :(\n\nError: " + loader.errors()));
}
});
}
void Instance::switchToCustomFile(const QString &filePath) { void Instance::switchToCustomFile(const QString &filePath) {
reset(); reset();
fillFromCustomFile(filePath); fillFromCustomFile(filePath);
@ -319,7 +300,6 @@ QString Instance::systemLangCode() const {
_systemLanguage = DefaultLanguageId(); _systemLanguage = DefaultLanguageId();
} }
} }
_systemLanguage = "de"; // TESTING
} }
return _systemLanguage; return _systemLanguage;
} }

View File

@ -34,13 +34,12 @@ QString DefaultLanguageId();
class Instance; class Instance;
Instance &Current(); Instance &Current();
class Instance : public base::enable_weak_from_this { class Instance {
public: public:
Instance() { Instance() {
fillDefaults(); fillDefaults();
} }
void switchToId(const QString &id); void switchToId(const QString &id);
void chooseCustomFile();
void switchToCustomFile(const QString &filePath); void switchToCustomFile(const QString &filePath);
Instance(const Instance &other) = delete; Instance(const Instance &other) = delete;

View File

@ -71,9 +71,7 @@ void FeedLangTestingKey(int key) {
} }
if (codeState == 4) { if (codeState == 4) {
codeState = 0; codeState = 0;
Lang::CurrentCloudManager().switchToTestLanguage();
auto testLanguageId = (Lang::Current().id() == qstr("TEST_X")) ? qsl("TEST_0") : qsl("TEST_X");
Lang::CurrentCloudManager().switchToLanguage(testLanguageId);
} }
} }

View File

@ -1085,7 +1085,7 @@ bool Instance::Private::onErrorDefault(mtpRequestId requestId, const RPCError &e
} }
return true; return true;
} else if (err == qstr("CONNECTION_LANG_CODE_INVALID")) { } else if (err == qstr("CONNECTION_LANG_CODE_INVALID")) {
Lang::CurrentCloudManager().switchToLanguage(qsl("en")); Lang::CurrentCloudManager().resetToDefault();
} else if (err == qstr("MSG_WAIT_FAILED")) { } else if (err == qstr("MSG_WAIT_FAILED")) {
mtpRequest request; mtpRequest request;
{ {

View File

@ -207,7 +207,7 @@ void GeneralWidget::refreshControls() {
void GeneralWidget::onChangeLanguage() { void GeneralWidget::onChangeLanguage() {
if ((_changeLanguage->clickModifiers() & Qt::ShiftModifier) && (_changeLanguage->clickModifiers() & Qt::AltModifier)) { if ((_changeLanguage->clickModifiers() & Qt::ShiftModifier) && (_changeLanguage->clickModifiers() & Qt::AltModifier)) {
Lang::Current().chooseCustomFile(); Lang::CurrentCloudManager().switchToLanguage(qsl("custom"));
return; return;
} }
auto manager = Messenger::Instance().langCloudManager(); auto manager = Messenger::Instance().langCloudManager();

View File

@ -33,6 +33,7 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
#include "storage/localstorage.h" #include "storage/localstorage.h"
#include "boxes/confirm_box.h" #include "boxes/confirm_box.h"
#include "lang/lang_keys.h" #include "lang/lang_keys.h"
#include "lang/lang_cloud_manager.h"
#include "messenger.h" #include "messenger.h"
#include "mtproto/mtp_instance.h" #include "mtproto/mtp_instance.h"
#include "mtproto/dc_options.h" #include "mtproto/dc_options.h"
@ -61,7 +62,7 @@ void fillCodes() {
})); }));
}); });
Codes.insert(qsl("loadlang"), [] { Codes.insert(qsl("loadlang"), [] {
Lang::Current().chooseCustomFile(); Lang::CurrentCloudManager().switchToLanguage(qsl("custom"));
}); });
Codes.insert(qsl("debugfiles"), [] { Codes.insert(qsl("debugfiles"), [] {
if (!cDebug()) return; if (!cDebug()) return;

View File

@ -1335,7 +1335,7 @@ void FlatTextarea::refreshPlaceholder() {
auto skipWidth = placeholderSkipWidth(); auto skipWidth = placeholderSkipWidth();
auto placeholderText = _placeholderFactory ? _placeholderFactory() : QString(); auto placeholderText = _placeholderFactory ? _placeholderFactory() : QString();
_placeholder = _st.font->elided(placeholderText, width() - _st.textMrg.left() - _st.textMrg.right() - _st.phPos.x() - 1 - skipWidth); _placeholder = _st.font->elided(placeholderText, width() - _st.textMrg.left() - _st.textMrg.right() - _st.phPos.x() - 1 - skipWidth);
if (_placeholderVisible) update(); update();
} }
void FlatTextarea::updatePlaceholder() { void FlatTextarea::updatePlaceholder() {
@ -2477,12 +2477,12 @@ void InputArea::refreshPlaceholder() {
} else { } else {
_placeholder = _st.placeholderFont->elided(placeholderText, availableWidth); _placeholder = _st.placeholderFont->elided(placeholderText, availableWidth);
} }
update();
} }
void InputArea::setPlaceholder(base::lambda<QString()> placeholderFactory) { void InputArea::setPlaceholder(base::lambda<QString()> placeholderFactory) {
_placeholderFactory = std::move(placeholderFactory); _placeholderFactory = std::move(placeholderFactory);
refreshPlaceholder(); refreshPlaceholder();
update();
} }
void InputArea::showError() { void InputArea::showError() {
@ -3243,12 +3243,12 @@ void InputField::refreshPlaceholder() {
} else { } else {
_placeholder = _st.placeholderFont->elided(placeholderText, availableWidth); _placeholder = _st.placeholderFont->elided(placeholderText, availableWidth);
} }
update();
} }
void InputField::setPlaceholder(base::lambda<QString()> placeholderFactory) { void InputField::setPlaceholder(base::lambda<QString()> placeholderFactory) {
_placeholderFactory = std::move(placeholderFactory); _placeholderFactory = std::move(placeholderFactory);
refreshPlaceholder(); refreshPlaceholder();
update();
} }
void InputField::showError() { void InputField::showError() {
@ -3534,12 +3534,12 @@ void MaskedInputField::refreshPlaceholder() {
} else { } else {
_placeholder = _st.placeholderFont->elided(placeholderText, availableWidth); _placeholder = _st.placeholderFont->elided(placeholderText, availableWidth);
} }
update();
} }
void MaskedInputField::setPlaceholder(base::lambda<QString()> placeholderFactory) { void MaskedInputField::setPlaceholder(base::lambda<QString()> placeholderFactory) {
_placeholderFactory = std::move(placeholderFactory); _placeholderFactory = std::move(placeholderFactory);
refreshPlaceholder(); refreshPlaceholder();
update();
} }
void MaskedInputField::contextMenuEvent(QContextMenuEvent *e) { void MaskedInputField::contextMenuEvent(QContextMenuEvent *e) {