From 3447aac9407d603a2ab5b7adb7d2f638db10bf84 Mon Sep 17 00:00:00 2001
From: John Preston <johnprestonmail@gmail.com>
Date: Fri, 26 Oct 2018 13:08:21 +0400
Subject: [PATCH] Prefix client-side lang_code-s with #.

---
 Telegram/SourceFiles/boxes/language_box.cpp      |  4 ++--
 Telegram/SourceFiles/lang/lang_cloud_manager.cpp | 14 +++++++++-----
 Telegram/SourceFiles/lang/lang_instance.cpp      |  7 +++++--
 Telegram/SourceFiles/lang/lang_instance.h        |  6 +++---
 Telegram/SourceFiles/settings/settings_codes.cpp |  2 +-
 Telegram/SourceFiles/settings/settings_main.cpp  |  2 +-
 6 files changed, 21 insertions(+), 14 deletions(-)

diff --git a/Telegram/SourceFiles/boxes/language_box.cpp b/Telegram/SourceFiles/boxes/language_box.cpp
index 73742d876..2ea33f775 100644
--- a/Telegram/SourceFiles/boxes/language_box.cpp
+++ b/Telegram/SourceFiles/boxes/language_box.cpp
@@ -76,7 +76,7 @@ void LanguageBox::Inner::languageChanged(int languageIndex) {
 	activateCurrent();
 	auto languageId = (*_languages)[languageIndex].id;
 	if (Lang::Current().id() != languageId) {
-		// "custom" is applied each time it is passed to switchToLanguage().
+		// "#custom" is applied each time it's passed to switchToLanguage().
 		// So we check that the language really has changed.
 		Lang::CurrentCloudManager().switchToLanguage(languageId);
 	}
@@ -142,7 +142,7 @@ void LanguageBox::refreshLanguages() {
 			currentIndex = 0;
 		}
 	}
-	if (currentId == qstr("custom")) {
+	if (currentId == qstr("#custom")) {
 		_languages.insert(_languages.begin(), { currentId, qsl("Custom LangPack"), qsl("Custom LangPack") });
 		currentIndex = 0;
 	} else if (currentIndex < 0) {
diff --git a/Telegram/SourceFiles/lang/lang_cloud_manager.cpp b/Telegram/SourceFiles/lang/lang_cloud_manager.cpp
index f3159a995..74505aa5c 100644
--- a/Telegram/SourceFiles/lang/lang_cloud_manager.cpp
+++ b/Telegram/SourceFiles/lang/lang_cloud_manager.cpp
@@ -185,13 +185,15 @@ void CloudManager::applyLangPackData(const MTPDlangPackDifference &data) {
 	} else if (!data.vstrings.v.isEmpty()) {
 		_langpack.applyDifference(data);
 		Local::writeLangPack();
+	} else if (_restartAfterSwitch) {
+		Local::writeLangPack();
 	} else {
 		LOG(("Lang Info: Up to date."));
 	}
 }
 
 bool CloudManager::canApplyWithoutRestart(const QString &id) const {
-	if (id == qstr("TEST_X") || id == qstr("TEST_0")) {
+	if (id == qstr("#TEST_X") || id == qstr("#TEST_0")) {
 		return true;
 	}
 
@@ -207,12 +209,12 @@ void CloudManager::switchToLanguage(QString id) {
 	if (id.isEmpty()) {
 		id = DefaultLanguageId();
 	}
-	if (_langpack.id() == id && id != qstr("custom")) {
+	if (_langpack.id() == id && id != qstr("#custom")) {
 		return;
 	}
 
 	request(_switchingToLanguageRequest).cancel();
-	if (id == qstr("custom")) {
+	if (id == qstr("#custom")) {
 		performSwitchToCustom();
 	} else if (canApplyWithoutRestart(id)) {
 		performSwitch(id);
@@ -258,7 +260,7 @@ void CloudManager::performSwitchToCustom() {
 		Lang::FileParser loader(filePath, { lng_sure_save_language });
 		if (loader.errors().isEmpty()) {
 			weak->request(weak->_switchingToLanguageRequest).cancel();
-			if (weak->canApplyWithoutRestart(qsl("custom"))) {
+			if (weak->canApplyWithoutRestart(qsl("#custom"))) {
 				weak->_langpack.switchToCustomFile(filePath);
 			} else {
 				const auto values = loader.found();
@@ -292,7 +294,9 @@ void CloudManager::performSwitchToCustom() {
 }
 
 void CloudManager::switchToTestLanguage() {
-	auto testLanguageId = (_langpack.id() == qstr("TEST_X")) ? qsl("TEST_0") : qsl("TEST_X");
+	const auto testLanguageId = (_langpack.id() == qstr("#TEST_X"))
+		? qsl("#TEST_0")
+		: qsl("#TEST_X");
 	performSwitch(testLanguageId);
 }
 
diff --git a/Telegram/SourceFiles/lang/lang_instance.cpp b/Telegram/SourceFiles/lang/lang_instance.cpp
index 9bc0a43af..2cd85d1e9 100644
--- a/Telegram/SourceFiles/lang/lang_instance.cpp
+++ b/Telegram/SourceFiles/lang/lang_instance.cpp
@@ -176,7 +176,7 @@ QString CloudLangPackName() {
 void Instance::switchToId(const QString &id) {
 	reset();
 	_id = id;
-	if (_id == qstr("TEST_X") || _id == qstr("TEST_0")) {
+	if (_id == qstr("#TEST_X") || _id == qstr("#TEST_0")) {
 		for (auto &value : _values) {
 			value = PrepareTestValue(value, _id[5]);
 		}
@@ -287,6 +287,9 @@ void Instance::fillFromSerialized(const QByteArray &data) {
 	}
 
 	if (!customFilePathAbsolute.isEmpty()) {
+		if (id == qstr("custom")) {
+			id = '#' + id;
+		}
 		auto currentCustomFileContent = Lang::FileParser::ReadFile(customFilePathAbsolute, customFilePathRelative);
 		if (!currentCustomFileContent.isEmpty() && currentCustomFileContent != customFileContent) {
 			loadFromCustomContent(customFilePathAbsolute, customFilePathRelative, currentCustomFileContent);
@@ -333,7 +336,7 @@ void Instance::loadFromContent(const QByteArray &content) {
 }
 
 void Instance::loadFromCustomContent(const QString &absolutePath, const QString &relativePath, const QByteArray &content) {
-	_id = qsl("custom");
+	_id = qsl("#custom");
 	_version = 0;
 	_customFilePathAbsolute = absolutePath;
 	_customFilePathRelative = relativePath;
diff --git a/Telegram/SourceFiles/lang/lang_instance.h b/Telegram/SourceFiles/lang/lang_instance.h
index 4e51494de..3518d94af 100644
--- a/Telegram/SourceFiles/lang/lang_instance.h
+++ b/Telegram/SourceFiles/lang/lang_instance.h
@@ -60,9 +60,9 @@ public:
 		return _id;
 	}
 	bool isCustom() const {
-		return (_id == qstr("custom")
-			|| _id == qstr("TEST_X")
-			|| _id == qstr("TEST_0"));
+		return (_id == qstr("#custom"))
+			|| (_id == qstr("#TEST_X"))
+			|| (_id == qstr("#TEST_0"));
 	}
 	int version() const {
 		return _version;
diff --git a/Telegram/SourceFiles/settings/settings_codes.cpp b/Telegram/SourceFiles/settings/settings_codes.cpp
index cf83c16c4..e05d4a6fd 100644
--- a/Telegram/SourceFiles/settings/settings_codes.cpp
+++ b/Telegram/SourceFiles/settings/settings_codes.cpp
@@ -51,7 +51,7 @@ auto GenerateCodes() {
 		});
 	}
 	codes.emplace(qsl("loadlang"), [] {
-		Lang::CurrentCloudManager().switchToLanguage(qsl("custom"));
+		Lang::CurrentCloudManager().switchToLanguage(qsl("#custom"));
 	});
 	codes.emplace(qsl("debugfiles"), [] {
 		if (!Logs::DebugEnabled()) {
diff --git a/Telegram/SourceFiles/settings/settings_main.cpp b/Telegram/SourceFiles/settings/settings_main.cpp
index 347035f2a..2489db8df 100644
--- a/Telegram/SourceFiles/settings/settings_main.cpp
+++ b/Telegram/SourceFiles/settings/settings_main.cpp
@@ -40,7 +40,7 @@ void SetupLanguageButton(
 	button->addClickHandler([=] {
 		const auto m = button->clickModifiers();
 		if ((m & Qt::ShiftModifier) && (m & Qt::AltModifier)) {
-			Lang::CurrentCloudManager().switchToLanguage(qsl("custom"));
+			Lang::CurrentCloudManager().switchToLanguage(qsl("#custom"));
 		} else {
 			*guard = LanguageBox::Show();
 		}