2014-12-19 21:33:22 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
|
|
|
the official desktop version of Telegram messaging app, see https://telegram.org
|
|
|
|
|
|
|
|
Telegram Desktop is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
It is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
2015-10-03 13:16:42 +00:00
|
|
|
In addition, as a special exception, the copyright holders give permission
|
|
|
|
to link the code of portions of this program with the OpenSSL library.
|
|
|
|
|
2014-12-19 21:33:22 +00:00
|
|
|
Full license: https://github.com/telegramdesktop/tdesktop/blob/master/LICENSE
|
2017-01-11 18:31:31 +00:00
|
|
|
Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
|
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
|
|
|
}
|