2016-12-05 06:59:08 +05:30
|
|
|
#include "TranslationsModel.h"
|
|
|
|
|
|
|
|
#include <QCoreApplication>
|
|
|
|
#include <QTranslator>
|
|
|
|
#include <QLocale>
|
|
|
|
#include <QDir>
|
|
|
|
#include <QLibraryInfo>
|
|
|
|
#include <QDebug>
|
2021-11-22 03:51:12 +05:30
|
|
|
|
|
|
|
#include "FileSystem.h"
|
|
|
|
#include "net/NetJob.h"
|
|
|
|
#include "net/ChecksumValidator.h"
|
|
|
|
#include "BuildConfig.h"
|
2019-01-02 06:11:07 +05:30
|
|
|
#include "Json.h"
|
|
|
|
|
|
|
|
#include "POTranslator.h"
|
2016-12-05 06:59:08 +05:30
|
|
|
|
2021-11-22 03:51:12 +05:30
|
|
|
#include "Application.h"
|
|
|
|
|
2021-04-08 22:35:44 +05:30
|
|
|
const static QLatin1Literal defaultLangCode("en_US");
|
|
|
|
|
2019-01-02 06:11:07 +05:30
|
|
|
enum class FileType
|
|
|
|
{
|
|
|
|
NONE,
|
|
|
|
QM,
|
|
|
|
PO
|
|
|
|
};
|
|
|
|
|
2016-12-05 06:59:08 +05:30
|
|
|
struct Language
|
|
|
|
{
|
2019-01-02 06:11:07 +05:30
|
|
|
Language()
|
|
|
|
{
|
|
|
|
updated = true;
|
|
|
|
}
|
|
|
|
Language(const QString & _key)
|
|
|
|
{
|
|
|
|
key = _key;
|
2021-04-13 18:23:21 +05:30
|
|
|
locale = QLocale(key);
|
2019-01-02 06:11:07 +05:30
|
|
|
updated = (key == defaultLangCode);
|
|
|
|
}
|
|
|
|
|
2021-12-08 05:53:12 +05:30
|
|
|
QString languageName() const {
|
|
|
|
QString result;
|
|
|
|
if(key == "ja_KANJI") {
|
|
|
|
result = locale.nativeLanguageName() + u8" (漢字)";
|
2021-12-11 17:36:14 +05:30
|
|
|
}
|
|
|
|
else if(key == "es_UY") {
|
|
|
|
result = u8"español de Latinoamérica";
|
2021-12-08 05:53:12 +05:30
|
|
|
}
|
|
|
|
else {
|
|
|
|
result = locale.nativeLanguageName();
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-01-02 06:11:07 +05:30
|
|
|
float percentTranslated() const
|
|
|
|
{
|
|
|
|
if (total == 0)
|
|
|
|
{
|
|
|
|
return 100.0f;
|
|
|
|
}
|
2019-01-08 06:50:36 +05:30
|
|
|
return 100.0f * float(translated) / float(total);
|
2019-01-02 06:11:07 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void setTranslationStats(unsigned _translated, unsigned _untranslated, unsigned _fuzzy)
|
|
|
|
{
|
|
|
|
translated = _translated;
|
|
|
|
untranslated = _untranslated;
|
|
|
|
fuzzy = _fuzzy;
|
|
|
|
total = translated + untranslated + fuzzy;
|
|
|
|
}
|
|
|
|
|
2019-01-07 02:44:13 +05:30
|
|
|
bool isOfSameNameAs(const Language& other) const
|
|
|
|
{
|
|
|
|
return key == other.key;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isIdenticalTo(const Language& other) const
|
|
|
|
{
|
|
|
|
return
|
|
|
|
(
|
|
|
|
key == other.key &&
|
|
|
|
file_name == other.file_name &&
|
|
|
|
file_size == other.file_size &&
|
|
|
|
file_sha1 == other.file_sha1 &&
|
|
|
|
translated == other.translated &&
|
|
|
|
fuzzy == other.fuzzy &&
|
|
|
|
total == other.fuzzy &&
|
|
|
|
localFileType == other.localFileType
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Language & apply(Language & other)
|
|
|
|
{
|
|
|
|
if(!isOfSameNameAs(other))
|
|
|
|
{
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
file_name = other.file_name;
|
|
|
|
file_size = other.file_size;
|
|
|
|
file_sha1 = other.file_sha1;
|
|
|
|
translated = other.translated;
|
|
|
|
fuzzy = other.fuzzy;
|
2019-01-08 06:50:36 +05:30
|
|
|
total = other.total;
|
2019-01-07 02:44:13 +05:30
|
|
|
localFileType = other.localFileType;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2018-07-15 18:21:05 +05:30
|
|
|
QString key;
|
|
|
|
QLocale locale;
|
|
|
|
bool updated;
|
2019-01-02 06:11:07 +05:30
|
|
|
|
|
|
|
QString file_name = QString();
|
|
|
|
std::size_t file_size = 0;
|
|
|
|
QString file_sha1 = QString();
|
|
|
|
|
|
|
|
unsigned translated = 0;
|
|
|
|
unsigned untranslated = 0;
|
|
|
|
unsigned fuzzy = 0;
|
|
|
|
unsigned total = 0;
|
|
|
|
|
|
|
|
FileType localFileType = FileType::NONE;
|
2016-12-05 06:59:08 +05:30
|
|
|
};
|
|
|
|
|
2019-01-07 02:44:13 +05:30
|
|
|
|
|
|
|
|
2016-12-05 06:59:08 +05:30
|
|
|
struct TranslationsModel::Private
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
QDir m_dir;
|
|
|
|
|
|
|
|
// initial state is just english
|
2019-01-02 06:11:07 +05:30
|
|
|
QVector<Language> m_languages = {Language (defaultLangCode)};
|
|
|
|
|
2018-07-15 18:21:05 +05:30
|
|
|
QString m_selectedLanguage = defaultLangCode;
|
|
|
|
std::unique_ptr<QTranslator> m_qt_translator;
|
|
|
|
std::unique_ptr<QTranslator> m_app_translator;
|
|
|
|
|
2021-11-22 03:51:12 +05:30
|
|
|
Net::Download::Ptr m_index_task;
|
2018-07-15 18:21:05 +05:30
|
|
|
QString m_downloadingTranslation;
|
2021-11-22 03:51:12 +05:30
|
|
|
NetJob::Ptr m_dl_job;
|
|
|
|
NetJob::Ptr m_index_job;
|
2018-07-15 18:21:05 +05:30
|
|
|
QString m_nextDownload;
|
2019-01-02 06:11:07 +05:30
|
|
|
|
|
|
|
std::unique_ptr<POTranslator> m_po_translator;
|
|
|
|
QFileSystemWatcher *watcher;
|
2016-12-05 06:59:08 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
TranslationsModel::TranslationsModel(QString path, QObject* parent): QAbstractListModel(parent)
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
d.reset(new Private);
|
|
|
|
d->m_dir.setPath(path);
|
2019-03-08 05:51:04 +05:30
|
|
|
FS::ensureFolderPathExists(path);
|
2019-01-02 06:11:07 +05:30
|
|
|
reloadLocalFiles();
|
|
|
|
|
|
|
|
d->watcher = new QFileSystemWatcher(this);
|
|
|
|
connect(d->watcher, &QFileSystemWatcher::directoryChanged, this, &TranslationsModel::translationDirChanged);
|
|
|
|
d->watcher->addPath(d->m_dir.canonicalPath());
|
2016-12-05 06:59:08 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
TranslationsModel::~TranslationsModel()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-01-02 06:11:07 +05:30
|
|
|
void TranslationsModel::translationDirChanged(const QString& path)
|
|
|
|
{
|
|
|
|
qDebug() << "Dir changed:" << path;
|
|
|
|
reloadLocalFiles();
|
|
|
|
selectLanguage(selectedLanguage());
|
|
|
|
}
|
|
|
|
|
2019-03-08 05:51:04 +05:30
|
|
|
void TranslationsModel::indexReceived()
|
2019-01-02 06:11:07 +05:30
|
|
|
{
|
|
|
|
qDebug() << "Got translations index!";
|
|
|
|
d->m_index_job.reset();
|
|
|
|
if(d->m_selectedLanguage != defaultLangCode)
|
|
|
|
{
|
|
|
|
downloadTranslation(d->m_selectedLanguage);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
2019-01-07 02:44:13 +05:30
|
|
|
void readIndex(const QString & path, QMap<QString, Language>& languages)
|
2019-01-02 06:11:07 +05:30
|
|
|
{
|
|
|
|
QByteArray data;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
data = FS::read(path);
|
|
|
|
}
|
|
|
|
catch (const Exception &e)
|
|
|
|
{
|
|
|
|
qCritical() << "Translations Download Failed: index file not readable";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int index = 1;
|
|
|
|
try
|
|
|
|
{
|
2020-06-28 16:55:53 +05:30
|
|
|
auto toplevel_doc = Json::requireDocument(data);
|
|
|
|
auto doc = Json::requireObject(toplevel_doc);
|
2019-01-02 06:11:07 +05:30
|
|
|
auto file_type = Json::requireString(doc, "file_type");
|
2021-10-22 02:57:01 +05:30
|
|
|
if(file_type != "MMC-TRANSLATION-INDEX")
|
2019-01-02 06:11:07 +05:30
|
|
|
{
|
|
|
|
qCritical() << "Translations Download Failed: index file is of unknown file type" << file_type;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto version = Json::requireInteger(doc, "version");
|
|
|
|
if(version > 2)
|
|
|
|
{
|
|
|
|
qCritical() << "Translations Download Failed: index file is of unknown format version" << file_type;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto langObjs = Json::requireObject(doc, "languages");
|
|
|
|
for(auto iter = langObjs.begin(); iter != langObjs.end(); iter++)
|
|
|
|
{
|
|
|
|
Language lang(iter.key());
|
|
|
|
|
|
|
|
auto langObj = Json::requireObject(iter.value());
|
|
|
|
lang.setTranslationStats(
|
|
|
|
Json::ensureInteger(langObj, "translated", 0),
|
|
|
|
Json::ensureInteger(langObj, "untranslated", 0),
|
|
|
|
Json::ensureInteger(langObj, "fuzzy", 0)
|
|
|
|
);
|
|
|
|
lang.file_name = Json::requireString(langObj, "file");
|
|
|
|
lang.file_sha1 = Json::requireString(langObj, "sha1");
|
|
|
|
lang.file_size = Json::requireInteger(langObj, "size");
|
|
|
|
|
2019-01-07 02:44:13 +05:30
|
|
|
languages.insert(lang.key, lang);
|
2019-01-02 06:11:07 +05:30
|
|
|
index++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Json::JsonException & e)
|
|
|
|
{
|
|
|
|
qCritical() << "Translations Download Failed: index file could not be parsed as json";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TranslationsModel::reloadLocalFiles()
|
|
|
|
{
|
2019-01-07 02:44:13 +05:30
|
|
|
QMap<QString, Language> languages = {{defaultLangCode, Language(defaultLangCode)}};
|
2019-01-02 06:11:07 +05:30
|
|
|
|
2019-01-07 02:44:13 +05:30
|
|
|
readIndex(d->m_dir.absoluteFilePath("index_v2.json"), languages);
|
2019-01-02 06:11:07 +05:30
|
|
|
auto entries = d->m_dir.entryInfoList({"mmc_*.qm", "*.po"}, QDir::Files | QDir::NoDotAndDotDot);
|
|
|
|
for(auto & entry: entries)
|
|
|
|
{
|
|
|
|
auto completeSuffix = entry.completeSuffix();
|
|
|
|
QString langCode;
|
|
|
|
FileType fileType = FileType::NONE;
|
|
|
|
if(completeSuffix == "qm")
|
|
|
|
{
|
|
|
|
langCode = entry.baseName().remove(0,4);
|
|
|
|
fileType = FileType::QM;
|
|
|
|
}
|
|
|
|
else if(completeSuffix == "po")
|
|
|
|
{
|
|
|
|
langCode = entry.baseName();
|
|
|
|
fileType = FileType::PO;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-01-07 02:44:13 +05:30
|
|
|
auto langIter = languages.find(langCode);
|
|
|
|
if(langIter != languages.end())
|
2019-01-02 06:11:07 +05:30
|
|
|
{
|
2019-01-07 02:44:13 +05:30
|
|
|
auto & language = *langIter;
|
2019-01-02 06:11:07 +05:30
|
|
|
if(int(fileType) > int(language.localFileType))
|
|
|
|
{
|
|
|
|
language.localFileType = fileType;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(fileType == FileType::PO)
|
|
|
|
{
|
|
|
|
Language localFound(langCode);
|
|
|
|
localFound.localFileType = FileType::PO;
|
2019-01-07 02:44:13 +05:30
|
|
|
languages.insert(langCode, localFound);
|
2019-01-02 06:11:07 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-07 02:44:13 +05:30
|
|
|
// changed and removed languages
|
|
|
|
for(auto iter = d->m_languages.begin(); iter != d->m_languages.end();)
|
|
|
|
{
|
|
|
|
auto &language = *iter;
|
|
|
|
auto row = iter - d->m_languages.begin();
|
|
|
|
|
|
|
|
auto updatedLanguageIter = languages.find(language.key);
|
|
|
|
if(updatedLanguageIter != languages.end())
|
|
|
|
{
|
|
|
|
if(language.isIdenticalTo(*updatedLanguageIter))
|
|
|
|
{
|
|
|
|
languages.remove(language.key);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
language.apply(*updatedLanguageIter);
|
|
|
|
emit dataChanged(index(row), index(row));
|
|
|
|
languages.remove(language.key);
|
|
|
|
}
|
|
|
|
iter++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
beginRemoveRows(QModelIndex(), row, row);
|
|
|
|
iter = d->m_languages.erase(iter);
|
|
|
|
endRemoveRows();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// added languages
|
|
|
|
if(languages.isEmpty())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2021-04-13 18:23:21 +05:30
|
|
|
beginInsertRows(QModelIndex(), 0, d->m_languages.size() + languages.size() - 1);
|
2019-01-07 02:44:13 +05:30
|
|
|
for(auto & language: languages)
|
|
|
|
{
|
|
|
|
d->m_languages.append(language);
|
|
|
|
}
|
2021-04-13 18:23:21 +05:30
|
|
|
std::sort(d->m_languages.begin(), d->m_languages.end(), [](const Language& a, const Language& b) {
|
|
|
|
return a.key.compare(b.key) < 0;
|
|
|
|
});
|
2019-01-07 02:44:13 +05:30
|
|
|
endInsertRows();
|
2019-01-02 06:11:07 +05:30
|
|
|
}
|
|
|
|
|
2019-01-08 06:50:36 +05:30
|
|
|
namespace {
|
|
|
|
enum class Column
|
|
|
|
{
|
|
|
|
Language,
|
2019-01-14 06:06:04 +05:30
|
|
|
Completeness
|
2019-01-08 06:50:36 +05:30
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-12-05 06:59:08 +05:30
|
|
|
QVariant TranslationsModel::data(const QModelIndex& index, int role) const
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
if (!index.isValid())
|
|
|
|
return QVariant();
|
|
|
|
|
|
|
|
int row = index.row();
|
2019-01-08 06:50:36 +05:30
|
|
|
auto column = static_cast<Column>(index.column());
|
2018-07-15 18:21:05 +05:30
|
|
|
|
|
|
|
if (row < 0 || row >= d->m_languages.size())
|
|
|
|
return QVariant();
|
|
|
|
|
2019-01-08 06:50:36 +05:30
|
|
|
auto & lang = d->m_languages[row];
|
2018-07-15 18:21:05 +05:30
|
|
|
switch (role)
|
|
|
|
{
|
|
|
|
case Qt::DisplayRole:
|
2019-01-08 06:50:36 +05:30
|
|
|
{
|
|
|
|
switch(column)
|
|
|
|
{
|
|
|
|
case Column::Language:
|
|
|
|
{
|
2021-12-08 05:53:12 +05:30
|
|
|
return lang.languageName();
|
2019-01-08 06:50:36 +05:30
|
|
|
}
|
2019-01-14 06:06:04 +05:30
|
|
|
case Column::Completeness:
|
2019-01-08 06:50:36 +05:30
|
|
|
{
|
|
|
|
QString text;
|
|
|
|
text.sprintf("%3.1f %%", lang.percentTranslated());
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case Qt::ToolTipRole:
|
|
|
|
{
|
|
|
|
return tr("%1:\n%2 translated\n%3 fuzzy\n%4 total").arg(lang.key, QString::number(lang.translated), QString::number(lang.fuzzy), QString::number(lang.total));
|
|
|
|
}
|
2018-07-15 18:21:05 +05:30
|
|
|
case Qt::UserRole:
|
2021-04-13 18:23:21 +05:30
|
|
|
return lang.key;
|
2018-07-15 18:21:05 +05:30
|
|
|
default:
|
|
|
|
return QVariant();
|
|
|
|
}
|
2016-12-05 06:59:08 +05:30
|
|
|
}
|
|
|
|
|
2019-01-08 06:50:36 +05:30
|
|
|
QVariant TranslationsModel::headerData(int section, Qt::Orientation orientation, int role) const
|
|
|
|
{
|
|
|
|
auto column = static_cast<Column>(section);
|
|
|
|
if(role == Qt::DisplayRole)
|
|
|
|
{
|
|
|
|
switch(column)
|
|
|
|
{
|
|
|
|
case Column::Language:
|
|
|
|
{
|
|
|
|
return tr("Language");
|
|
|
|
}
|
2019-01-14 06:06:04 +05:30
|
|
|
case Column::Completeness:
|
2019-01-08 06:50:36 +05:30
|
|
|
{
|
2019-01-14 06:06:04 +05:30
|
|
|
return tr("Completeness");
|
2019-01-08 06:50:36 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(role == Qt::ToolTipRole)
|
|
|
|
{
|
|
|
|
switch(column)
|
|
|
|
{
|
|
|
|
case Column::Language:
|
|
|
|
{
|
|
|
|
return tr("The native language name.");
|
|
|
|
}
|
2019-01-14 06:06:04 +05:30
|
|
|
case Column::Completeness:
|
2019-01-08 06:50:36 +05:30
|
|
|
{
|
2019-01-14 06:06:04 +05:30
|
|
|
return tr("Completeness is the percentage of fully translated strings, not counting automatically guessed ones.");
|
2019-01-08 06:50:36 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return QAbstractListModel::headerData(section, orientation, role);
|
|
|
|
}
|
|
|
|
|
2016-12-05 06:59:08 +05:30
|
|
|
int TranslationsModel::rowCount(const QModelIndex& parent) const
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
return d->m_languages.size();
|
2016-12-05 06:59:08 +05:30
|
|
|
}
|
|
|
|
|
2019-01-08 06:50:36 +05:30
|
|
|
int TranslationsModel::columnCount(const QModelIndex& parent) const
|
|
|
|
{
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
2016-12-05 06:59:08 +05:30
|
|
|
Language * TranslationsModel::findLanguage(const QString& key)
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
auto found = std::find_if(d->m_languages.begin(), d->m_languages.end(), [&](Language & lang)
|
|
|
|
{
|
|
|
|
return lang.key == key;
|
|
|
|
});
|
|
|
|
if(found == d->m_languages.end())
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return found;
|
|
|
|
}
|
2016-12-05 06:59:08 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
bool TranslationsModel::selectLanguage(QString key)
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
QString &langCode = key;
|
|
|
|
auto langPtr = findLanguage(key);
|
|
|
|
if(!langPtr)
|
|
|
|
{
|
|
|
|
qWarning() << "Selected invalid language" << key << ", defaulting to" << defaultLangCode;
|
|
|
|
langCode = defaultLangCode;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
langCode = langPtr->key;
|
|
|
|
}
|
|
|
|
|
|
|
|
// uninstall existing translators if there are any
|
|
|
|
if (d->m_app_translator)
|
|
|
|
{
|
|
|
|
QCoreApplication::removeTranslator(d->m_app_translator.get());
|
|
|
|
d->m_app_translator.reset();
|
|
|
|
}
|
|
|
|
if (d->m_qt_translator)
|
|
|
|
{
|
|
|
|
QCoreApplication::removeTranslator(d->m_qt_translator.get());
|
|
|
|
d->m_qt_translator.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* FIXME: potential source of crashes:
|
|
|
|
* In a multithreaded application, the default locale should be set at application startup, before any non-GUI threads are created.
|
|
|
|
* This function is not reentrant.
|
|
|
|
*/
|
2021-04-13 18:23:21 +05:30
|
|
|
QLocale locale = QLocale(langCode);
|
2018-07-15 18:21:05 +05:30
|
|
|
QLocale::setDefault(locale);
|
|
|
|
|
|
|
|
// if it's the default UI language, finish
|
|
|
|
if(langCode == defaultLangCode)
|
|
|
|
{
|
|
|
|
d->m_selectedLanguage = langCode;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// otherwise install new translations
|
|
|
|
bool successful = false;
|
|
|
|
// FIXME: this is likely never present. FIX IT.
|
|
|
|
d->m_qt_translator.reset(new QTranslator());
|
|
|
|
if (d->m_qt_translator->load("qt_" + langCode, QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
|
|
|
|
{
|
|
|
|
qDebug() << "Loading Qt Language File for" << langCode.toLocal8Bit().constData() << "...";
|
|
|
|
if (!QCoreApplication::installTranslator(d->m_qt_translator.get()))
|
|
|
|
{
|
|
|
|
qCritical() << "Loading Qt Language File failed.";
|
|
|
|
d->m_qt_translator.reset();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
successful = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
d->m_qt_translator.reset();
|
|
|
|
}
|
|
|
|
|
2019-01-02 06:11:07 +05:30
|
|
|
if(langPtr->localFileType == FileType::PO)
|
2018-07-15 18:21:05 +05:30
|
|
|
{
|
|
|
|
qDebug() << "Loading Application Language File for" << langCode.toLocal8Bit().constData() << "...";
|
2019-01-02 06:11:07 +05:30
|
|
|
auto poTranslator = new POTranslator(FS::PathCombine(d->m_dir.path(), langCode + ".po"));
|
|
|
|
if(!poTranslator->isEmpty())
|
|
|
|
{
|
|
|
|
if (!QCoreApplication::installTranslator(poTranslator))
|
|
|
|
{
|
|
|
|
delete poTranslator;
|
|
|
|
qCritical() << "Installing Application Language File failed.";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
d->m_app_translator.reset(poTranslator);
|
|
|
|
successful = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2018-07-15 18:21:05 +05:30
|
|
|
{
|
|
|
|
qCritical() << "Loading Application Language File failed.";
|
|
|
|
d->m_app_translator.reset();
|
|
|
|
}
|
2019-01-02 06:11:07 +05:30
|
|
|
}
|
|
|
|
else if(langPtr->localFileType == FileType::QM)
|
|
|
|
{
|
|
|
|
d->m_app_translator.reset(new QTranslator());
|
|
|
|
if (d->m_app_translator->load("mmc_" + langCode, d->m_dir.path()))
|
|
|
|
{
|
|
|
|
qDebug() << "Loading Application Language File for" << langCode.toLocal8Bit().constData() << "...";
|
|
|
|
if (!QCoreApplication::installTranslator(d->m_app_translator.get()))
|
|
|
|
{
|
|
|
|
qCritical() << "Installing Application Language File failed.";
|
|
|
|
d->m_app_translator.reset();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
successful = true;
|
|
|
|
}
|
|
|
|
}
|
2018-07-15 18:21:05 +05:30
|
|
|
else
|
|
|
|
{
|
2019-01-02 06:11:07 +05:30
|
|
|
d->m_app_translator.reset();
|
2018-07-15 18:21:05 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
d->m_app_translator.reset();
|
|
|
|
}
|
|
|
|
d->m_selectedLanguage = langCode;
|
|
|
|
return successful;
|
2016-12-05 06:59:08 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
QModelIndex TranslationsModel::selectedIndex()
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
auto found = findLanguage(d->m_selectedLanguage);
|
|
|
|
if(found)
|
|
|
|
{
|
|
|
|
// QVector iterator freely converts to pointer to contained type
|
|
|
|
return index(found - d->m_languages.begin(), 0, QModelIndex());
|
|
|
|
}
|
|
|
|
return QModelIndex();
|
2016-12-05 06:59:08 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
QString TranslationsModel::selectedLanguage()
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
return d->m_selectedLanguage;
|
2016-12-05 06:59:08 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void TranslationsModel::downloadIndex()
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
if(d->m_index_job || d->m_dl_job)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
qDebug() << "Downloading Translations Index...";
|
2021-12-31 09:57:59 +05:30
|
|
|
d->m_index_job = new NetJob("Translations Index", APPLICATION->network());
|
2021-11-22 03:51:12 +05:30
|
|
|
MetaEntryPtr entry = APPLICATION->metacache()->resolveEntry("translations", "index_v2.json");
|
2019-01-02 06:11:07 +05:30
|
|
|
entry->setStale(true);
|
|
|
|
d->m_index_task = Net::Download::makeCached(QUrl("https://files.multimc.org/translations/index_v2.json"), entry);
|
2018-07-15 18:21:05 +05:30
|
|
|
d->m_index_job->addNetAction(d->m_index_task);
|
|
|
|
connect(d->m_index_job.get(), &NetJob::failed, this, &TranslationsModel::indexFailed);
|
2019-03-08 05:51:04 +05:30
|
|
|
connect(d->m_index_job.get(), &NetJob::succeeded, this, &TranslationsModel::indexReceived);
|
2021-12-31 09:57:59 +05:30
|
|
|
d->m_index_job->start();
|
2016-12-05 06:59:08 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void TranslationsModel::updateLanguage(QString key)
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
if(key == defaultLangCode)
|
|
|
|
{
|
|
|
|
qWarning() << "Cannot update builtin language" << key;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto found = findLanguage(key);
|
|
|
|
if(!found)
|
|
|
|
{
|
|
|
|
qWarning() << "Cannot update invalid language" << key;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(!found->updated)
|
|
|
|
{
|
|
|
|
downloadTranslation(key);
|
|
|
|
}
|
2016-12-05 06:59:08 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void TranslationsModel::downloadTranslation(QString key)
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
if(d->m_dl_job)
|
|
|
|
{
|
|
|
|
d->m_nextDownload = key;
|
|
|
|
return;
|
|
|
|
}
|
2019-01-02 06:11:07 +05:30
|
|
|
auto lang = findLanguage(key);
|
|
|
|
if(!lang)
|
|
|
|
{
|
|
|
|
qWarning() << "Will not download an unknown translation" << key;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-15 18:21:05 +05:30
|
|
|
d->m_downloadingTranslation = key;
|
2021-11-22 03:51:12 +05:30
|
|
|
MetaEntryPtr entry = APPLICATION->metacache()->resolveEntry("translations", "mmc_" + key + ".qm");
|
2018-07-15 18:21:05 +05:30
|
|
|
entry->setStale(true);
|
2019-01-02 06:11:07 +05:30
|
|
|
|
2020-07-18 19:48:02 +05:30
|
|
|
auto dl = Net::Download::makeCached(QUrl(BuildConfig.TRANSLATIONS_BASE_URL + lang->file_name), entry);
|
2019-01-02 06:11:07 +05:30
|
|
|
auto rawHash = QByteArray::fromHex(lang->file_sha1.toLatin1());
|
|
|
|
dl->addValidator(new Net::ChecksumValidator(QCryptographicHash::Sha1, rawHash));
|
|
|
|
dl->m_total_progress = lang->file_size;
|
|
|
|
|
2021-12-31 09:57:59 +05:30
|
|
|
d->m_dl_job = new NetJob("Translation for " + key, APPLICATION->network());
|
2019-01-02 06:11:07 +05:30
|
|
|
d->m_dl_job->addNetAction(dl);
|
|
|
|
|
2018-07-15 18:21:05 +05:30
|
|
|
connect(d->m_dl_job.get(), &NetJob::succeeded, this, &TranslationsModel::dlGood);
|
|
|
|
connect(d->m_dl_job.get(), &NetJob::failed, this, &TranslationsModel::dlFailed);
|
2019-01-02 06:11:07 +05:30
|
|
|
|
2021-12-31 09:57:59 +05:30
|
|
|
d->m_dl_job->start();
|
2016-12-05 06:59:08 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void TranslationsModel::downloadNext()
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
if(!d->m_nextDownload.isEmpty())
|
|
|
|
{
|
|
|
|
downloadTranslation(d->m_nextDownload);
|
|
|
|
d->m_nextDownload.clear();
|
|
|
|
}
|
2016-12-05 06:59:08 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void TranslationsModel::dlFailed(QString reason)
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
qCritical() << "Translations Download Failed:" << reason;
|
|
|
|
d->m_dl_job.reset();
|
|
|
|
downloadNext();
|
2016-12-05 06:59:08 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void TranslationsModel::dlGood()
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
qDebug() << "Got translation:" << d->m_downloadingTranslation;
|
|
|
|
|
|
|
|
if(d->m_downloadingTranslation == d->m_selectedLanguage)
|
|
|
|
{
|
|
|
|
selectLanguage(d->m_selectedLanguage);
|
|
|
|
}
|
|
|
|
d->m_dl_job.reset();
|
|
|
|
downloadNext();
|
2016-12-05 06:59:08 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void TranslationsModel::indexFailed(QString reason)
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
qCritical() << "Translations Index Download Failed:" << reason;
|
|
|
|
d->m_index_job.reset();
|
2016-12-05 06:59:08 +05:30
|
|
|
}
|