2014-12-19 21:33:22 +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.
|
2014-12-19 21:33:22 +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
|
2014-12-19 21:33:22 +00:00
|
|
|
*/
|
2017-04-06 14:38:10 +00:00
|
|
|
#include "boxes/language_box.h"
|
2014-12-19 21:33:22 +00:00
|
|
|
|
2017-04-13 08:27:10 +00:00
|
|
|
#include "lang/lang_keys.h"
|
2016-11-11 13:46:04 +00:00
|
|
|
#include "ui/widgets/checkbox.h"
|
|
|
|
#include "ui/widgets/buttons.h"
|
2017-03-04 10:23:56 +00:00
|
|
|
#include "storage/localstorage.h"
|
2017-04-06 14:38:10 +00:00
|
|
|
#include "boxes/confirm_box.h"
|
2014-12-19 21:33:22 +00:00
|
|
|
#include "mainwidget.h"
|
2016-04-12 21:31:28 +00:00
|
|
|
#include "mainwindow.h"
|
2017-04-13 17:59:05 +00:00
|
|
|
#include "lang/lang_instance.h"
|
2017-04-18 15:21:03 +00:00
|
|
|
#include "lang/lang_cloud_manager.h"
|
2016-11-15 11:56:49 +00:00
|
|
|
#include "styles/style_boxes.h"
|
2014-12-19 21:33:22 +00:00
|
|
|
|
2017-04-18 18:57:32 +00:00
|
|
|
class LanguageBox::Inner : public TWidget, private base::Subscriber {
|
2017-04-18 15:21:03 +00:00
|
|
|
public:
|
2017-08-17 08:31:24 +00:00
|
|
|
Inner(QWidget *parent, not_null<Languages*> languages);
|
2017-04-18 15:21:03 +00:00
|
|
|
|
|
|
|
void setSelected(int index);
|
|
|
|
void refresh();
|
|
|
|
|
|
|
|
private:
|
2017-04-18 18:57:32 +00:00
|
|
|
void activateCurrent();
|
2017-04-18 15:21:03 +00:00
|
|
|
void languageChanged(int languageIndex);
|
|
|
|
|
2017-08-17 08:31:24 +00:00
|
|
|
not_null<Languages*> _languages;
|
2017-04-18 15:21:03 +00:00
|
|
|
std::shared_ptr<Ui::RadiobuttonGroup> _group;
|
|
|
|
std::vector<object_ptr<Ui::Radiobutton>> _buttons;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2017-08-17 08:31:24 +00:00
|
|
|
LanguageBox::Inner::Inner(QWidget *parent, not_null<Languages*> languages) : TWidget(parent)
|
2017-04-18 15:21:03 +00:00
|
|
|
, _languages(languages) {
|
|
|
|
_group = std::make_shared<Ui::RadiobuttonGroup>(0);
|
|
|
|
_group->setChangedCallback([this](int value) { languageChanged(value); });
|
2017-04-18 18:57:32 +00:00
|
|
|
subscribe(Lang::Current().updated(), [this] {
|
|
|
|
activateCurrent();
|
2017-07-18 17:09:48 +00:00
|
|
|
refresh();
|
2017-04-18 18:57:32 +00:00
|
|
|
});
|
2017-04-18 15:21:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LanguageBox::Inner::setSelected(int index) {
|
|
|
|
_group->setValue(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LanguageBox::Inner::refresh() {
|
|
|
|
for (auto &button : _buttons) {
|
|
|
|
button.destroy();
|
|
|
|
}
|
|
|
|
_buttons.clear();
|
|
|
|
|
2017-07-04 13:59:00 +00:00
|
|
|
auto y = st::boxOptionListPadding.top() + st::langsButton.margin.top();
|
2017-04-18 15:21:03 +00:00
|
|
|
_buttons.reserve(_languages->size());
|
|
|
|
auto index = 0;
|
|
|
|
for_const (auto &language, *_languages) {
|
2017-06-04 13:09:53 +00:00
|
|
|
_buttons.emplace_back(this, _group, index++, language.nativeName, st::langsButton);
|
2017-04-18 15:21:03 +00:00
|
|
|
auto button = _buttons.back().data();
|
2017-07-04 13:59:00 +00:00
|
|
|
button->moveToLeft(st::boxPadding.left() + st::boxOptionListPadding.left(), y);
|
2017-04-18 15:21:03 +00:00
|
|
|
button->show();
|
|
|
|
y += button->heightNoMargins() + st::boxOptionListSkip;
|
|
|
|
}
|
2017-07-04 13:59:00 +00:00
|
|
|
auto newHeight = y - st::boxOptionListSkip + st::boxOptionListPadding.bottom() + st::langsButton.margin.bottom();
|
2017-04-18 15:21:03 +00:00
|
|
|
resize(st::langsWidth, newHeight);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LanguageBox::Inner::languageChanged(int languageIndex) {
|
|
|
|
Expects(languageIndex >= 0 && languageIndex < _languages->size());
|
|
|
|
|
2017-05-30 17:58:25 +00:00
|
|
|
activateCurrent();
|
2017-04-18 15:21:03 +00:00
|
|
|
auto languageId = (*_languages)[languageIndex].id;
|
2017-06-02 11:46:02 +00:00
|
|
|
if (Lang::Current().id() != languageId) {
|
|
|
|
// "custom" is applied each time it is passed to switchToLanguage().
|
|
|
|
// So we check that the language really has changed.
|
|
|
|
Lang::CurrentCloudManager().switchToLanguage(languageId);
|
|
|
|
}
|
2017-04-18 15:21:03 +00:00
|
|
|
}
|
|
|
|
|
2017-04-18 18:57:32 +00:00
|
|
|
void LanguageBox::Inner::activateCurrent() {
|
|
|
|
auto currentId = Lang::Current().id();
|
|
|
|
for (auto i = 0, count = _languages->size(); i != count; ++i) {
|
2017-07-18 17:09:48 +00:00
|
|
|
auto languageId = (*_languages)[i].id;
|
|
|
|
auto isCurrent = (languageId == currentId) || (languageId == Lang::DefaultLanguageId() && currentId.isEmpty());
|
|
|
|
if (isCurrent) {
|
2017-04-18 18:57:32 +00:00
|
|
|
_group->setValue(i);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-13 17:07:56 +00:00
|
|
|
void LanguageBox::prepare() {
|
2017-04-18 17:37:14 +00:00
|
|
|
refreshLang();
|
2017-04-18 15:21:03 +00:00
|
|
|
subscribe(Lang::Current().updated(), [this] {
|
2017-04-18 17:37:14 +00:00
|
|
|
refreshLang();
|
2017-04-18 15:21:03 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
_inner = setInnerWidget(object_ptr<Inner>(this, &_languages), st::boxLayerScroll);
|
|
|
|
|
|
|
|
refresh();
|
|
|
|
subscribe(Lang::CurrentCloudManager().languageListChanged(), [this] {
|
|
|
|
refresh();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-04-18 17:37:14 +00:00
|
|
|
void LanguageBox::refreshLang() {
|
2017-04-18 15:21:03 +00:00
|
|
|
clearButtons();
|
2017-05-30 15:21:05 +00:00
|
|
|
addButton(langFactory(lng_box_ok), [this] { closeBox(); });
|
2014-12-19 21:33:22 +00:00
|
|
|
|
2017-05-30 15:21:05 +00:00
|
|
|
setTitle(langFactory(lng_languages));
|
2014-12-19 21:33:22 +00:00
|
|
|
|
2017-04-18 15:21:03 +00:00
|
|
|
update();
|
|
|
|
}
|
2017-04-13 17:59:05 +00:00
|
|
|
|
2017-04-18 15:21:03 +00:00
|
|
|
void LanguageBox::refresh() {
|
|
|
|
refreshLanguages();
|
2017-04-13 17:59:05 +00:00
|
|
|
|
2017-04-18 15:21:03 +00:00
|
|
|
_inner->refresh();
|
2017-07-12 11:05:29 +00:00
|
|
|
setDimensions(st::langsWidth, qMin(_inner->height(), st::boxMaxListHeight));
|
2015-01-06 11:13:43 +00:00
|
|
|
}
|
|
|
|
|
2017-04-18 15:21:03 +00:00
|
|
|
void LanguageBox::refreshLanguages() {
|
|
|
|
_languages = Languages();
|
|
|
|
auto list = Lang::CurrentCloudManager().languageList();
|
|
|
|
_languages.reserve(list.size() + 1);
|
|
|
|
auto currentId = Lang::Current().id();
|
|
|
|
auto currentIndex = -1;
|
2017-06-04 13:09:53 +00:00
|
|
|
_languages.push_back({ qsl("en"), qsl("English"), qsl("English") });
|
2017-04-18 15:21:03 +00:00
|
|
|
for (auto &language : list) {
|
2017-04-18 18:57:32 +00:00
|
|
|
auto isCurrent = (language.id == currentId) || (language.id == Lang::DefaultLanguageId() && currentId.isEmpty());
|
2017-04-18 15:21:03 +00:00
|
|
|
if (language.id != qstr("en")) {
|
|
|
|
if (isCurrent) {
|
|
|
|
currentIndex = _languages.size();
|
|
|
|
}
|
|
|
|
_languages.push_back(language);
|
|
|
|
} else if (isCurrent) {
|
|
|
|
currentIndex = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (currentId == qstr("custom")) {
|
2017-06-04 13:09:53 +00:00
|
|
|
_languages.insert(_languages.begin(), { currentId, qsl("Custom LangPack"), qsl("Custom LangPack") });
|
2017-04-18 15:21:03 +00:00
|
|
|
currentIndex = 0;
|
|
|
|
} else if (currentIndex < 0) {
|
|
|
|
currentIndex = _languages.size();
|
2017-06-04 13:09:53 +00:00
|
|
|
_languages.push_back({ currentId, lang(lng_language_name), lang(lng_language_name) });
|
2017-04-18 15:21:03 +00:00
|
|
|
}
|
|
|
|
_inner->setSelected(currentIndex);
|
2014-12-19 21:33:22 +00:00
|
|
|
}
|