2017-04-18 15:21:03 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2017-04-18 15:21:03 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2017-04-18 15:21:03 +00:00
|
|
|
*/
|
|
|
|
#include "lang/lang_cloud_manager.h"
|
|
|
|
|
|
|
|
#include "lang/lang_instance.h"
|
2019-09-17 07:51:02 +00:00
|
|
|
#include "lang/lang_file_parser.h"
|
|
|
|
#include "lang/lang_text_entity.h"
|
2017-04-18 15:21:03 +00:00
|
|
|
#include "mtproto/mtp_instance.h"
|
|
|
|
#include "storage/localstorage.h"
|
2019-01-21 13:42:21 +00:00
|
|
|
#include "core/application.h"
|
2019-11-27 08:02:56 +00:00
|
|
|
#include "main/main_account.h"
|
2020-06-18 18:04:16 +00:00
|
|
|
#include "main/main_domain.h"
|
2017-04-18 17:37:14 +00:00
|
|
|
#include "boxes/confirm_box.h"
|
2018-10-22 11:13:48 +00:00
|
|
|
#include "ui/wrap/padding_wrap.h"
|
|
|
|
#include "ui/widgets/labels.h"
|
2019-06-14 12:01:35 +00:00
|
|
|
#include "ui/text/text_utilities.h"
|
2017-05-30 17:58:25 +00:00
|
|
|
#include "core/file_utilities.h"
|
2018-10-22 11:13:48 +00:00
|
|
|
#include "core/click_handler_types.h"
|
2019-09-13 06:06:02 +00:00
|
|
|
#include "app.h"
|
2019-09-18 11:19:05 +00:00
|
|
|
#include "styles/style_layers.h"
|
2017-04-18 15:21:03 +00:00
|
|
|
|
|
|
|
namespace Lang {
|
2018-10-22 11:13:48 +00:00
|
|
|
namespace {
|
|
|
|
|
2019-09-18 11:19:05 +00:00
|
|
|
class ConfirmSwitchBox : public Ui::BoxContent {
|
2018-10-22 11:13:48 +00:00
|
|
|
public:
|
|
|
|
ConfirmSwitchBox(
|
|
|
|
QWidget*,
|
|
|
|
const MTPDlangPackLanguage &data,
|
|
|
|
Fn<void()> apply);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void prepare() override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
QString _name;
|
|
|
|
int _percent = 0;
|
2018-11-13 09:14:22 +00:00
|
|
|
bool _official = false;
|
2018-10-22 11:13:48 +00:00
|
|
|
QString _editLink;
|
|
|
|
Fn<void()> _apply;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2019-09-18 11:19:05 +00:00
|
|
|
class NotReadyBox : public Ui::BoxContent {
|
2018-11-02 13:52:55 +00:00
|
|
|
public:
|
|
|
|
NotReadyBox(
|
|
|
|
QWidget*,
|
2018-11-13 09:29:33 +00:00
|
|
|
const MTPDlangPackLanguage &data);
|
2018-11-02 13:52:55 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
void prepare() override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
QString _name;
|
|
|
|
QString _editLink;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2018-10-22 11:13:48 +00:00
|
|
|
ConfirmSwitchBox::ConfirmSwitchBox(
|
|
|
|
QWidget*,
|
|
|
|
const MTPDlangPackLanguage &data,
|
|
|
|
Fn<void()> apply)
|
2019-07-05 13:38:38 +00:00
|
|
|
: _name(qs(data.vnative_name()))
|
|
|
|
, _percent(data.vtranslated_count().v * 100 / data.vstrings_count().v)
|
2018-11-13 09:14:22 +00:00
|
|
|
, _official(data.is_official())
|
2019-07-05 13:38:38 +00:00
|
|
|
, _editLink(qs(data.vtranslations_url()))
|
2018-10-22 11:13:48 +00:00
|
|
|
, _apply(std::move(apply)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConfirmSwitchBox::prepare() {
|
2019-06-18 15:00:55 +00:00
|
|
|
setTitle(tr::lng_language_switch_title());
|
2018-10-22 11:13:48 +00:00
|
|
|
|
2019-06-18 16:53:27 +00:00
|
|
|
auto text = (_official
|
|
|
|
? tr::lng_language_switch_about_official
|
|
|
|
: tr::lng_language_switch_about_unofficial)(
|
|
|
|
lt_lang_name,
|
|
|
|
rpl::single(Ui::Text::Bold(_name)),
|
|
|
|
lt_percent,
|
|
|
|
rpl::single(Ui::Text::Bold(QString::number(_percent))),
|
|
|
|
lt_link,
|
|
|
|
tr::lng_language_switch_link() | Ui::Text::ToLink(_editLink),
|
|
|
|
Ui::Text::WithEntities);
|
2019-06-14 12:01:35 +00:00
|
|
|
const auto content = Ui::CreateChild<Ui::PaddingWrap<Ui::FlatLabel>>(
|
2018-10-22 11:13:48 +00:00
|
|
|
this,
|
|
|
|
object_ptr<Ui::FlatLabel>(
|
|
|
|
this,
|
2019-06-18 16:53:27 +00:00
|
|
|
std::move(text),
|
2018-11-02 13:52:55 +00:00
|
|
|
st::boxLabel),
|
2018-10-22 11:13:48 +00:00
|
|
|
QMargins{ st::boxPadding.left(), 0, st::boxPadding.right(), 0 });
|
2019-06-14 12:01:35 +00:00
|
|
|
content->entity()->setLinksTrusted();
|
2018-10-22 11:13:48 +00:00
|
|
|
|
2019-06-18 16:53:27 +00:00
|
|
|
addButton(tr::lng_language_switch_apply(), [=] {
|
2018-10-22 11:13:48 +00:00
|
|
|
const auto apply = _apply;
|
|
|
|
closeBox();
|
|
|
|
apply();
|
|
|
|
});
|
2019-06-18 16:53:27 +00:00
|
|
|
addButton(tr::lng_cancel(), [=] { closeBox(); });
|
2018-10-22 11:13:48 +00:00
|
|
|
|
2018-11-02 13:52:55 +00:00
|
|
|
content->resizeToWidth(st::boxWideWidth);
|
|
|
|
content->heightValue(
|
|
|
|
) | rpl::start_with_next([=](int height) {
|
|
|
|
setDimensions(st::boxWideWidth, height);
|
|
|
|
}, lifetime());
|
|
|
|
}
|
|
|
|
|
|
|
|
NotReadyBox::NotReadyBox(
|
|
|
|
QWidget*,
|
2018-11-13 09:29:33 +00:00
|
|
|
const MTPDlangPackLanguage &data)
|
2019-07-05 13:38:38 +00:00
|
|
|
: _name(qs(data.vnative_name()))
|
|
|
|
, _editLink(qs(data.vtranslations_url())) {
|
2018-11-02 13:52:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void NotReadyBox::prepare() {
|
2019-06-18 15:00:55 +00:00
|
|
|
setTitle(tr::lng_language_not_ready_title());
|
2018-11-02 13:52:55 +00:00
|
|
|
|
2019-06-18 16:53:27 +00:00
|
|
|
auto text = tr::lng_language_not_ready_about(
|
2018-11-02 13:52:55 +00:00
|
|
|
lt_lang_name,
|
2019-06-18 16:53:27 +00:00
|
|
|
rpl::single(_name) | Ui::Text::ToWithEntities(),
|
2018-11-02 13:52:55 +00:00
|
|
|
lt_link,
|
2019-06-18 16:53:27 +00:00
|
|
|
tr::lng_language_not_ready_link() | Ui::Text::ToLink(_editLink),
|
|
|
|
Ui::Text::WithEntities);
|
2019-06-14 12:01:35 +00:00
|
|
|
const auto content = Ui::CreateChild<Ui::PaddingWrap<Ui::FlatLabel>>(
|
2018-11-02 13:52:55 +00:00
|
|
|
this,
|
|
|
|
object_ptr<Ui::FlatLabel>(
|
|
|
|
this,
|
2019-06-18 16:53:27 +00:00
|
|
|
std::move(text),
|
2018-11-02 13:52:55 +00:00
|
|
|
st::boxLabel),
|
|
|
|
QMargins{ st::boxPadding.left(), 0, st::boxPadding.right(), 0 });
|
2019-06-14 12:01:35 +00:00
|
|
|
content->entity()->setLinksTrusted();
|
2018-11-02 13:52:55 +00:00
|
|
|
|
2019-06-18 16:53:27 +00:00
|
|
|
addButton(tr::lng_box_ok(), [=] { closeBox(); });
|
2018-11-02 13:52:55 +00:00
|
|
|
|
2018-10-22 11:13:48 +00:00
|
|
|
content->resizeToWidth(st::boxWidth);
|
|
|
|
content->heightValue(
|
|
|
|
) | rpl::start_with_next([=](int height) {
|
|
|
|
setDimensions(st::boxWidth, height);
|
|
|
|
}, lifetime());
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
2017-04-18 15:21:03 +00:00
|
|
|
|
2018-10-30 11:23:54 +00:00
|
|
|
Language ParseLanguage(const MTPLangPackLanguage &data) {
|
|
|
|
return data.match([](const MTPDlangPackLanguage &data) {
|
|
|
|
return Language{
|
2019-07-05 13:38:38 +00:00
|
|
|
qs(data.vlang_code()),
|
|
|
|
qs(data.vplural_code()),
|
|
|
|
qs(data.vbase_lang_code().value_or_empty()),
|
|
|
|
qs(data.vname()),
|
|
|
|
qs(data.vnative_name())
|
2018-10-30 11:23:54 +00:00
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-11-27 08:02:56 +00:00
|
|
|
CloudManager::CloudManager(Instance &langpack)
|
|
|
|
: _langpack(langpack) {
|
2020-06-18 18:04:16 +00:00
|
|
|
Core::App().domain().activeValue(
|
2020-06-15 16:25:02 +00:00
|
|
|
) | rpl::map([=](Main::Account *account) {
|
2020-06-17 09:36:25 +00:00
|
|
|
if (!account) {
|
2019-11-27 08:02:56 +00:00
|
|
|
_api.reset();
|
|
|
|
}
|
2020-06-17 09:36:25 +00:00
|
|
|
return account
|
|
|
|
? account->mtpValue()
|
|
|
|
: rpl::never<not_null<MTP::Instance*>>();
|
|
|
|
}) | rpl::flatten_latest(
|
|
|
|
) | rpl::start_with_next([=](not_null<MTP::Instance*> instance) {
|
|
|
|
_api.emplace(instance);
|
|
|
|
resendRequests();
|
2019-11-27 08:02:56 +00:00
|
|
|
}, _lifetime);
|
2017-04-18 15:21:03 +00:00
|
|
|
}
|
|
|
|
|
2018-10-22 11:13:48 +00:00
|
|
|
Pack CloudManager::packTypeFromId(const QString &id) const {
|
|
|
|
if (id == LanguageIdOrDefault(_langpack.id())) {
|
|
|
|
return Pack::Current;
|
|
|
|
} else if (id == _langpack.baseId()) {
|
|
|
|
return Pack::Base;
|
|
|
|
}
|
|
|
|
return Pack::None;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CloudManager::requestLangPackDifference(const QString &langId) {
|
|
|
|
Expects(!langId.isEmpty());
|
|
|
|
|
|
|
|
if (langId == LanguageIdOrDefault(_langpack.id())) {
|
|
|
|
requestLangPackDifference(Pack::Current);
|
|
|
|
} else {
|
|
|
|
requestLangPackDifference(Pack::Base);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mtpRequestId &CloudManager::packRequestId(Pack pack) {
|
|
|
|
return (pack != Pack::Base)
|
|
|
|
? _langPackRequestId
|
|
|
|
: _langPackBaseRequestId;
|
|
|
|
}
|
|
|
|
|
|
|
|
mtpRequestId CloudManager::packRequestId(Pack pack) const {
|
|
|
|
return (pack != Pack::Base)
|
|
|
|
? _langPackRequestId
|
|
|
|
: _langPackBaseRequestId;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CloudManager::requestLangPackDifference(Pack pack) {
|
2019-11-27 08:02:56 +00:00
|
|
|
if (!_api) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_api->request(base::take(packRequestId(pack))).cancel();
|
2017-07-26 12:50:39 +00:00
|
|
|
if (_langpack.isCustom()) {
|
2017-04-18 15:21:03 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-10-22 11:13:48 +00:00
|
|
|
const auto version = _langpack.version(pack);
|
|
|
|
const auto code = _langpack.cloudLangCode(pack);
|
|
|
|
if (code.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
2017-04-18 15:21:03 +00:00
|
|
|
if (version > 0) {
|
2019-11-27 08:02:56 +00:00
|
|
|
packRequestId(pack) = _api->request(MTPlangpack_GetDifference(
|
2019-03-12 10:36:33 +00:00
|
|
|
MTP_string(CloudLangPackName()),
|
2018-10-22 11:13:48 +00:00
|
|
|
MTP_string(code),
|
2018-08-20 11:31:40 +00:00
|
|
|
MTP_int(version)
|
|
|
|
)).done([=](const MTPLangPackDifference &result) {
|
2018-10-22 11:13:48 +00:00
|
|
|
packRequestId(pack) = 0;
|
2017-04-18 15:21:03 +00:00
|
|
|
applyLangPackDifference(result);
|
2021-03-12 12:48:00 +00:00
|
|
|
}).fail([=](const MTP::Error &error) {
|
2018-10-22 11:13:48 +00:00
|
|
|
packRequestId(pack) = 0;
|
2017-04-18 15:21:03 +00:00
|
|
|
}).send();
|
|
|
|
} else {
|
2019-11-27 08:02:56 +00:00
|
|
|
packRequestId(pack) = _api->request(MTPlangpack_GetLangPack(
|
2018-08-20 11:31:40 +00:00
|
|
|
MTP_string(CloudLangPackName()),
|
2018-10-22 11:13:48 +00:00
|
|
|
MTP_string(code)
|
2018-08-20 11:31:40 +00:00
|
|
|
)).done([=](const MTPLangPackDifference &result) {
|
2018-10-22 11:13:48 +00:00
|
|
|
packRequestId(pack) = 0;
|
2017-04-18 15:21:03 +00:00
|
|
|
applyLangPackDifference(result);
|
2021-03-12 12:48:00 +00:00
|
|
|
}).fail([=](const MTP::Error &error) {
|
2018-10-22 11:13:48 +00:00
|
|
|
packRequestId(pack) = 0;
|
2017-04-18 15:21:03 +00:00
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-30 09:31:32 +00:00
|
|
|
void CloudManager::setSuggestedLanguage(const QString &langCode) {
|
2018-10-22 11:13:48 +00:00
|
|
|
if (Lang::LanguageIdOrDefault(langCode) != Lang::DefaultLanguageId()) {
|
2017-05-30 09:31:32 +00:00
|
|
|
_suggestedLanguage = langCode;
|
|
|
|
} else {
|
|
|
|
_suggestedLanguage = QString();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!_languageWasSuggested) {
|
|
|
|
_languageWasSuggested = true;
|
|
|
|
_firstLanguageSuggestion.notify();
|
|
|
|
|
2020-06-10 13:24:41 +00:00
|
|
|
if (Core::App().offerLegacyLangPackSwitch()
|
2019-07-04 12:25:16 +00:00
|
|
|
&& _langpack.id().isEmpty()
|
|
|
|
&& !_suggestedLanguage.isEmpty()) {
|
|
|
|
_offerSwitchToId = _suggestedLanguage;
|
|
|
|
offerSwitchLangPack();
|
2017-05-30 09:31:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-06 11:29:01 +00:00
|
|
|
void CloudManager::setCurrentVersions(int version, int baseVersion) {
|
|
|
|
const auto check = [&](Pack pack, int version) {
|
|
|
|
if (version > _langpack.version(pack) && !packRequestId(pack)) {
|
|
|
|
requestLangPackDifference(pack);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
check(Pack::Current, version);
|
|
|
|
check(Pack::Base, baseVersion);
|
|
|
|
}
|
|
|
|
|
2018-10-22 11:13:48 +00:00
|
|
|
void CloudManager::applyLangPackDifference(
|
|
|
|
const MTPLangPackDifference &difference) {
|
2017-04-18 15:21:03 +00:00
|
|
|
Expects(difference.type() == mtpc_langPackDifference);
|
2018-10-22 11:13:48 +00:00
|
|
|
|
2017-04-18 17:37:14 +00:00
|
|
|
if (_langpack.isCustom()) {
|
2017-04-18 15:21:03 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-10-22 11:13:48 +00:00
|
|
|
const auto &langpack = difference.c_langPackDifference();
|
2019-07-05 13:38:38 +00:00
|
|
|
const auto langpackId = qs(langpack.vlang_code());
|
2018-10-22 11:13:48 +00:00
|
|
|
const auto pack = packTypeFromId(langpackId);
|
|
|
|
if (pack != Pack::None) {
|
|
|
|
applyLangPackData(pack, langpack);
|
2017-05-30 17:58:25 +00:00
|
|
|
if (_restartAfterSwitch) {
|
2018-10-22 11:13:48 +00:00
|
|
|
restartAfterSwitch();
|
2017-05-30 17:58:25 +00:00
|
|
|
}
|
2017-04-18 15:21:03 +00:00
|
|
|
} else {
|
2018-10-22 11:13:48 +00:00
|
|
|
LOG(("Lang Warning: "
|
2021-03-13 11:50:34 +00:00
|
|
|
"Ignoring update for '%1' because our language is '%2'").arg(
|
|
|
|
langpackId,
|
|
|
|
_langpack.id()));
|
2017-04-18 15:21:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CloudManager::requestLanguageList() {
|
2019-11-27 08:02:56 +00:00
|
|
|
if (!_api) {
|
|
|
|
_languagesRequestId = -1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_api->request(base::take(_languagesRequestId)).cancel();
|
|
|
|
_languagesRequestId = _api->request(MTPlangpack_GetLanguages(
|
2018-08-20 11:31:40 +00:00
|
|
|
MTP_string(CloudLangPackName())
|
|
|
|
)).done([=](const MTPVector<MTPLangPackLanguage> &result) {
|
2017-04-18 15:21:03 +00:00
|
|
|
auto languages = Languages();
|
2018-10-30 11:23:54 +00:00
|
|
|
for (const auto &language : result.v) {
|
|
|
|
languages.push_back(ParseLanguage(language));
|
2017-04-18 15:21:03 +00:00
|
|
|
}
|
|
|
|
if (_languages != languages) {
|
|
|
|
_languages = languages;
|
|
|
|
_languagesChanged.notify();
|
|
|
|
}
|
|
|
|
_languagesRequestId = 0;
|
2021-03-12 12:48:00 +00:00
|
|
|
}).fail([=](const MTP::Error &error) {
|
2017-04-18 15:21:03 +00:00
|
|
|
_languagesRequestId = 0;
|
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
2017-04-18 17:37:14 +00:00
|
|
|
void CloudManager::offerSwitchLangPack() {
|
|
|
|
Expects(!_offerSwitchToId.isEmpty());
|
|
|
|
Expects(_offerSwitchToId != DefaultLanguageId());
|
|
|
|
|
|
|
|
if (!showOfferSwitchBox()) {
|
|
|
|
subscribe(languageListChanged(), [this] {
|
|
|
|
showOfferSwitchBox();
|
|
|
|
});
|
|
|
|
requestLanguageList();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-13 09:14:22 +00:00
|
|
|
Language CloudManager::findOfferedLanguage() const {
|
2018-10-22 11:13:48 +00:00
|
|
|
for (const auto &language : _languages) {
|
2017-04-18 17:37:14 +00:00
|
|
|
if (language.id == _offerSwitchToId) {
|
2018-11-13 09:14:22 +00:00
|
|
|
return language;
|
2017-04-18 17:37:14 +00:00
|
|
|
}
|
|
|
|
}
|
2018-11-13 09:14:22 +00:00
|
|
|
return {};
|
2017-04-18 17:37:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CloudManager::showOfferSwitchBox() {
|
2018-11-13 09:14:22 +00:00
|
|
|
const auto language = findOfferedLanguage();
|
|
|
|
if (language.id.isEmpty()) {
|
2017-04-18 17:37:14 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-11-13 09:14:22 +00:00
|
|
|
const auto confirm = [=] {
|
2017-04-18 17:37:14 +00:00
|
|
|
Ui::hideLayer();
|
|
|
|
if (_offerSwitchToId.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
2018-11-13 09:14:22 +00:00
|
|
|
performSwitchAndRestart(language);
|
|
|
|
};
|
|
|
|
const auto cancel = [=] {
|
2017-04-18 17:37:14 +00:00
|
|
|
Ui::hideLayer();
|
2018-11-13 09:14:22 +00:00
|
|
|
changeIdAndReInitConnection(DefaultLanguage());
|
2017-04-18 17:37:14 +00:00
|
|
|
Local::writeLangPack();
|
2018-11-13 09:14:22 +00:00
|
|
|
};
|
|
|
|
Ui::show(
|
|
|
|
Box<ConfirmBox>(
|
|
|
|
"Do you want to switch your language to "
|
|
|
|
+ language.nativeName
|
|
|
|
+ "? You can always change your language in Settings.",
|
|
|
|
"Change",
|
2019-06-19 15:09:03 +00:00
|
|
|
tr::lng_cancel(tr::now),
|
2018-11-13 09:14:22 +00:00
|
|
|
confirm,
|
|
|
|
cancel),
|
2019-09-18 11:19:05 +00:00
|
|
|
Ui::LayerOption::KeepOther);
|
2017-04-18 17:37:14 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-10-22 11:13:48 +00:00
|
|
|
void CloudManager::applyLangPackData(
|
|
|
|
Pack pack,
|
|
|
|
const MTPDlangPackDifference &data) {
|
2019-07-05 13:38:38 +00:00
|
|
|
if (_langpack.version(pack) < data.vfrom_version().v) {
|
2018-10-22 11:13:48 +00:00
|
|
|
requestLangPackDifference(pack);
|
2019-07-05 13:38:38 +00:00
|
|
|
} else if (!data.vstrings().v.isEmpty()) {
|
2018-10-22 11:13:48 +00:00
|
|
|
_langpack.applyDifference(pack, data);
|
2017-04-18 17:37:14 +00:00
|
|
|
Local::writeLangPack();
|
2018-10-26 09:08:21 +00:00
|
|
|
} else if (_restartAfterSwitch) {
|
|
|
|
Local::writeLangPack();
|
2017-04-18 17:37:14 +00:00
|
|
|
} else {
|
|
|
|
LOG(("Lang Info: Up to date."));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-30 17:58:25 +00:00
|
|
|
bool CloudManager::canApplyWithoutRestart(const QString &id) const {
|
2018-10-26 09:08:21 +00:00
|
|
|
if (id == qstr("#TEST_X") || id == qstr("#TEST_0")) {
|
2017-05-30 17:58:25 +00:00
|
|
|
return true;
|
|
|
|
}
|
2020-06-10 13:24:41 +00:00
|
|
|
return Core::App().canApplyLangPackWithoutRestart();
|
2017-05-30 17:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CloudManager::resetToDefault() {
|
2018-11-13 09:14:22 +00:00
|
|
|
performSwitch(DefaultLanguage());
|
|
|
|
}
|
|
|
|
|
|
|
|
void CloudManager::switchToLanguage(const QString &id) {
|
|
|
|
requestLanguageAndSwitch(id, false);
|
2017-05-30 17:58:25 +00:00
|
|
|
}
|
|
|
|
|
2018-10-22 11:13:48 +00:00
|
|
|
void CloudManager::switchWithWarning(const QString &id) {
|
2018-11-13 09:14:22 +00:00
|
|
|
requestLanguageAndSwitch(id, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CloudManager::requestLanguageAndSwitch(
|
|
|
|
const QString &id,
|
|
|
|
bool warning) {
|
2018-10-22 11:13:48 +00:00
|
|
|
Expects(!id.isEmpty());
|
|
|
|
|
|
|
|
if (LanguageIdOrDefault(_langpack.id()) == id) {
|
2019-06-19 15:09:03 +00:00
|
|
|
Ui::show(Box<InformBox>(tr::lng_language_already(tr::now)));
|
2018-10-22 11:13:48 +00:00
|
|
|
return;
|
2018-12-25 11:22:01 +00:00
|
|
|
} else if (id == qstr("#custom")) {
|
|
|
|
performSwitchToCustom();
|
|
|
|
return;
|
2017-05-30 17:58:25 +00:00
|
|
|
}
|
2018-10-22 11:13:48 +00:00
|
|
|
|
2019-11-27 08:02:56 +00:00
|
|
|
_switchingToLanguageId = id;
|
|
|
|
_switchingToLanguageWarning = warning;
|
|
|
|
sendSwitchingToLanguageRequest();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CloudManager::sendSwitchingToLanguageRequest() {
|
|
|
|
if (!_api) {
|
|
|
|
_switchingToLanguageId = -1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_api->request(_switchingToLanguageRequest).cancel();
|
|
|
|
_switchingToLanguageRequest = _api->request(MTPlangpack_GetLanguage(
|
2018-10-22 11:13:48 +00:00
|
|
|
MTP_string(Lang::CloudLangPackName()),
|
2019-11-27 08:02:56 +00:00
|
|
|
MTP_string(_switchingToLanguageId)
|
2018-10-22 11:13:48 +00:00
|
|
|
)).done([=](const MTPLangPackLanguage &result) {
|
|
|
|
_switchingToLanguageRequest = 0;
|
2018-11-13 09:14:22 +00:00
|
|
|
const auto language = Lang::ParseLanguage(result);
|
|
|
|
const auto finalize = [=] {
|
2019-06-19 11:22:25 +00:00
|
|
|
if (canApplyWithoutRestart(language.id)) {
|
|
|
|
performSwitchAndAddToRecent(language);
|
|
|
|
} else {
|
|
|
|
performSwitchAndRestart(language);
|
|
|
|
}
|
2018-11-13 09:14:22 +00:00
|
|
|
};
|
2019-11-27 08:02:56 +00:00
|
|
|
if (!_switchingToLanguageWarning) {
|
2018-11-13 09:14:22 +00:00
|
|
|
finalize();
|
|
|
|
return;
|
|
|
|
}
|
2018-10-22 11:13:48 +00:00
|
|
|
result.match([=](const MTPDlangPackLanguage &data) {
|
2019-07-05 13:38:38 +00:00
|
|
|
if (data.vstrings_count().v > 0) {
|
2018-11-13 09:29:33 +00:00
|
|
|
Ui::show(Box<ConfirmSwitchBox>(data, finalize));
|
2018-10-22 11:13:48 +00:00
|
|
|
} else {
|
2018-11-13 09:29:33 +00:00
|
|
|
Ui::show(Box<NotReadyBox>(data));
|
2018-10-22 11:13:48 +00:00
|
|
|
}
|
|
|
|
});
|
2021-03-12 12:48:00 +00:00
|
|
|
}).fail([=](const MTP::Error &error) {
|
2018-10-22 11:13:48 +00:00
|
|
|
_switchingToLanguageRequest = 0;
|
2018-11-02 13:52:55 +00:00
|
|
|
if (error.type() == "LANG_CODE_NOT_SUPPORTED") {
|
2019-06-19 15:09:03 +00:00
|
|
|
Ui::show(Box<InformBox>(tr::lng_language_not_found(tr::now)));
|
2018-11-02 13:52:55 +00:00
|
|
|
}
|
2018-10-22 11:13:48 +00:00
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
|
2018-11-13 09:14:22 +00:00
|
|
|
void CloudManager::switchToLanguage(const Language &data) {
|
|
|
|
if (_langpack.id() == data.id && data.id != qstr("#custom")) {
|
2017-05-30 17:58:25 +00:00
|
|
|
return;
|
2019-11-27 08:02:56 +00:00
|
|
|
} else if (!_api) {
|
|
|
|
return;
|
2017-05-30 17:58:25 +00:00
|
|
|
}
|
|
|
|
|
2019-11-27 08:02:56 +00:00
|
|
|
_api->request(base::take(_getKeysForSwitchRequestId)).cancel();
|
2018-11-13 09:14:22 +00:00
|
|
|
if (data.id == qstr("#custom")) {
|
2017-05-30 17:58:25 +00:00
|
|
|
performSwitchToCustom();
|
2018-11-13 09:14:22 +00:00
|
|
|
} else if (canApplyWithoutRestart(data.id)) {
|
2019-06-19 11:22:25 +00:00
|
|
|
performSwitchAndAddToRecent(data);
|
2017-05-30 17:58:25 +00:00
|
|
|
} else {
|
|
|
|
QVector<MTPstring> keys;
|
|
|
|
keys.reserve(3);
|
|
|
|
keys.push_back(MTP_string("lng_sure_save_language"));
|
2019-11-27 08:02:56 +00:00
|
|
|
_getKeysForSwitchRequestId = _api->request(MTPlangpack_GetStrings(
|
2018-08-20 11:31:40 +00:00
|
|
|
MTP_string(Lang::CloudLangPackName()),
|
2018-11-13 09:14:22 +00:00
|
|
|
MTP_string(data.id),
|
2018-08-20 11:31:40 +00:00
|
|
|
MTP_vector<MTPstring>(std::move(keys))
|
|
|
|
)).done([=](const MTPVector<MTPLangPackString> &result) {
|
2019-11-27 08:02:56 +00:00
|
|
|
_getKeysForSwitchRequestId = 0;
|
2018-09-21 07:21:52 +00:00
|
|
|
const auto values = Instance::ParseStrings(result);
|
2019-06-19 15:42:16 +00:00
|
|
|
const auto getValue = [&](ushort key) {
|
2017-05-30 17:58:25 +00:00
|
|
|
auto it = values.find(key);
|
2018-08-20 11:31:40 +00:00
|
|
|
return (it == values.cend())
|
|
|
|
? GetOriginalValue(key)
|
|
|
|
: it->second;
|
2017-05-30 17:58:25 +00:00
|
|
|
};
|
2019-06-19 15:09:03 +00:00
|
|
|
const auto text = tr::lng_sure_save_language(tr::now)
|
2018-09-21 07:21:52 +00:00
|
|
|
+ "\n\n"
|
2019-06-19 15:42:16 +00:00
|
|
|
+ getValue(tr::lng_sure_save_language.base);
|
2018-09-21 07:21:52 +00:00
|
|
|
Ui::show(
|
|
|
|
Box<ConfirmBox>(
|
|
|
|
text,
|
2019-06-19 15:09:03 +00:00
|
|
|
tr::lng_box_ok(tr::now),
|
|
|
|
tr::lng_cancel(tr::now),
|
2018-11-13 09:14:22 +00:00
|
|
|
[=] { performSwitchAndRestart(data); }),
|
2019-09-18 11:19:05 +00:00
|
|
|
Ui::LayerOption::KeepOther);
|
2021-03-12 12:48:00 +00:00
|
|
|
}).fail([=](const MTP::Error &error) {
|
2019-11-27 08:02:56 +00:00
|
|
|
_getKeysForSwitchRequestId = 0;
|
2017-05-30 17:58:25 +00:00
|
|
|
}).send();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CloudManager::performSwitchToCustom() {
|
|
|
|
auto filter = qsl("Language files (*.strings)");
|
|
|
|
auto title = qsl("Choose language .strings file");
|
2019-11-27 08:02:56 +00:00
|
|
|
FileDialog::GetOpenPath(Core::App().getFileDialogParent(), title, filter, [=, weak = base::make_weak(this)](const FileDialog::OpenResult &result) {
|
2017-05-30 17:58:25 +00:00
|
|
|
if (!weak || result.paths.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-06-19 15:42:16 +00:00
|
|
|
const auto filePath = result.paths.front();
|
|
|
|
auto loader = Lang::FileParser(
|
|
|
|
filePath,
|
|
|
|
{ tr::lng_sure_save_language.base });
|
2017-05-30 17:58:25 +00:00
|
|
|
if (loader.errors().isEmpty()) {
|
2019-11-27 08:02:56 +00:00
|
|
|
if (_api) {
|
|
|
|
_api->request(
|
|
|
|
base::take(_switchingToLanguageRequest)
|
|
|
|
).cancel();
|
|
|
|
}
|
|
|
|
if (canApplyWithoutRestart(qsl("#custom"))) {
|
|
|
|
_langpack.switchToCustomFile(filePath);
|
2017-05-30 17:58:25 +00:00
|
|
|
} else {
|
2018-09-21 07:21:52 +00:00
|
|
|
const auto values = loader.found();
|
2019-06-19 15:42:16 +00:00
|
|
|
const auto getValue = [&](ushort key) {
|
2018-09-21 07:21:52 +00:00
|
|
|
const auto it = values.find(key);
|
|
|
|
return (it == values.cend())
|
|
|
|
? GetOriginalValue(key)
|
|
|
|
: it.value();
|
2017-05-30 17:58:25 +00:00
|
|
|
};
|
2019-06-19 15:09:03 +00:00
|
|
|
const auto text = tr::lng_sure_save_language(tr::now)
|
2018-09-21 07:21:52 +00:00
|
|
|
+ "\n\n"
|
2020-07-01 09:03:29 +00:00
|
|
|
+ getValue(tr::lng_sure_save_language.base);
|
2018-09-21 07:21:52 +00:00
|
|
|
const auto change = [=] {
|
2019-11-27 08:02:56 +00:00
|
|
|
_langpack.switchToCustomFile(filePath);
|
2017-05-30 17:58:25 +00:00
|
|
|
App::restart();
|
2018-09-21 07:21:52 +00:00
|
|
|
};
|
|
|
|
Ui::show(
|
|
|
|
Box<ConfirmBox>(
|
|
|
|
text,
|
2019-06-19 15:09:03 +00:00
|
|
|
tr::lng_box_ok(tr::now),
|
|
|
|
tr::lng_cancel(tr::now),
|
2018-09-21 07:21:52 +00:00
|
|
|
change),
|
2019-09-18 11:19:05 +00:00
|
|
|
Ui::LayerOption::KeepOther);
|
2017-05-30 17:58:25 +00:00
|
|
|
}
|
|
|
|
} else {
|
2017-09-15 17:34:41 +00:00
|
|
|
Ui::show(
|
|
|
|
Box<InformBox>("Custom lang failed :(\n\nError: " + loader.errors()),
|
2019-09-18 11:19:05 +00:00
|
|
|
Ui::LayerOption::KeepOther);
|
2017-05-30 17:58:25 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void CloudManager::switchToTestLanguage() {
|
2018-10-26 09:08:21 +00:00
|
|
|
const auto testLanguageId = (_langpack.id() == qstr("#TEST_X"))
|
|
|
|
? qsl("#TEST_0")
|
|
|
|
: qsl("#TEST_X");
|
2018-11-13 09:14:22 +00:00
|
|
|
performSwitch({ testLanguageId });
|
2017-05-30 17:58:25 +00:00
|
|
|
}
|
|
|
|
|
2018-11-13 09:14:22 +00:00
|
|
|
void CloudManager::performSwitch(const Language &data) {
|
2017-05-30 17:58:25 +00:00
|
|
|
_restartAfterSwitch = false;
|
2018-11-13 09:14:22 +00:00
|
|
|
switchLangPackId(data);
|
2018-10-22 11:13:48 +00:00
|
|
|
requestLangPackDifference(Pack::Current);
|
|
|
|
requestLangPackDifference(Pack::Base);
|
|
|
|
}
|
|
|
|
|
2019-06-19 11:22:25 +00:00
|
|
|
void CloudManager::performSwitchAndAddToRecent(const Language &data) {
|
2018-11-13 09:14:22 +00:00
|
|
|
Local::pushRecentLanguage(data);
|
|
|
|
performSwitch(data);
|
2019-06-19 11:22:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CloudManager::performSwitchAndRestart(const Language &data) {
|
|
|
|
performSwitchAndAddToRecent(data);
|
2018-10-22 11:13:48 +00:00
|
|
|
restartAfterSwitch();
|
2017-04-18 15:21:03 +00:00
|
|
|
}
|
|
|
|
|
2018-10-22 11:13:48 +00:00
|
|
|
void CloudManager::restartAfterSwitch() {
|
|
|
|
if (_langPackRequestId || _langPackBaseRequestId) {
|
2017-05-30 17:58:25 +00:00
|
|
|
_restartAfterSwitch = true;
|
|
|
|
} else {
|
|
|
|
App::restart();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-13 09:14:22 +00:00
|
|
|
void CloudManager::switchLangPackId(const Language &data) {
|
2018-10-22 11:13:48 +00:00
|
|
|
const auto currentId = _langpack.id();
|
|
|
|
const auto currentBaseId = _langpack.baseId();
|
2018-11-13 09:14:22 +00:00
|
|
|
const auto notChanged = (currentId == data.id
|
|
|
|
&& currentBaseId == data.baseId)
|
2018-10-22 11:13:48 +00:00
|
|
|
|| (currentId.isEmpty()
|
|
|
|
&& currentBaseId.isEmpty()
|
2018-11-13 09:14:22 +00:00
|
|
|
&& data.id == DefaultLanguageId());
|
2017-04-18 17:37:14 +00:00
|
|
|
if (!notChanged) {
|
2018-11-13 09:14:22 +00:00
|
|
|
changeIdAndReInitConnection(data);
|
2017-04-18 15:21:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-13 09:14:22 +00:00
|
|
|
void CloudManager::changeIdAndReInitConnection(const Language &data) {
|
|
|
|
_langpack.switchToId(data);
|
2019-11-27 08:02:56 +00:00
|
|
|
if (_api) {
|
2020-06-17 09:36:25 +00:00
|
|
|
const auto mtproto = &_api->instance();
|
2019-11-27 08:02:56 +00:00
|
|
|
mtproto->reInitConnection(mtproto->mainDcId());
|
|
|
|
}
|
|
|
|
}
|
2017-04-18 17:37:14 +00:00
|
|
|
|
2019-11-27 08:02:56 +00:00
|
|
|
void CloudManager::resendRequests() {
|
|
|
|
if (packRequestId(Pack::Base)) {
|
|
|
|
requestLangPackDifference(Pack::Base);
|
|
|
|
}
|
|
|
|
if (packRequestId(Pack::Current)) {
|
|
|
|
requestLangPackDifference(Pack::Current);
|
|
|
|
}
|
|
|
|
if (_languagesRequestId) {
|
|
|
|
requestLanguageList();
|
|
|
|
}
|
|
|
|
if (_switchingToLanguageRequest) {
|
|
|
|
sendSwitchingToLanguageRequest();
|
|
|
|
}
|
2017-04-18 17:37:14 +00:00
|
|
|
}
|
|
|
|
|
2017-04-18 15:21:03 +00:00
|
|
|
CloudManager &CurrentCloudManager() {
|
2019-01-21 13:42:21 +00:00
|
|
|
auto result = Core::App().langCloudManager();
|
2017-08-17 09:06:26 +00:00
|
|
|
Assert(result != nullptr);
|
2017-04-18 15:21:03 +00:00
|
|
|
return *result;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Lang
|