From 4474d269cc925c9245f7e0e331cc8435f87593a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Mr=C3=A1zek?= Date: Sun, 6 Jan 2019 22:14:13 +0100 Subject: [PATCH] NOISSUE granular model updates for language model --- api/logic/translations/TranslationsModel.cpp | 115 ++++++++++++++----- application/pages/global/MultiMCPage.cpp | 3 +- 2 files changed, 87 insertions(+), 31 deletions(-) diff --git a/api/logic/translations/TranslationsModel.cpp b/api/logic/translations/TranslationsModel.cpp index c9c30212..3f374083 100644 --- a/api/logic/translations/TranslationsModel.cpp +++ b/api/logic/translations/TranslationsModel.cpp @@ -54,6 +54,42 @@ struct Language total = translated + untranslated + fuzzy; } + 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; + total = other.fuzzy; + localFileType = other.localFileType; + return *this; + } + QString key; QLocale locale; bool updated; @@ -70,13 +106,14 @@ struct Language FileType localFileType = FileType::NONE; }; + + struct TranslationsModel::Private { QDir m_dir; // initial state is just english QVector m_languages = {Language (defaultLangCode)}; - QMap m_languageLookup = {{defaultLangCode, 0}}; QString m_selectedLanguage = defaultLangCode; std::unique_ptr m_qt_translator; @@ -125,7 +162,7 @@ void TranslationsModel::indexRecieved() } namespace { -void readIndex(const QString & path, QVector& languages, QMap& languagesLookup) +void readIndex(const QString & path, QMap& languages) { QByteArray data; try @@ -169,8 +206,7 @@ void readIndex(const QString & path, QVector& languages, QMap& languages, QMap languages = {Language (defaultLangCode)}; - QMap languageLookup = {{defaultLangCode, 0}}; + QMap languages = {{defaultLangCode, Language(defaultLangCode)}}; - readIndex(d->m_dir.absoluteFilePath("index_v2.json"), languages, languageLookup); - auto fileTypeToString = [](FileType ft) -> QString - { - switch(ft) - { - case FileType::NONE: - return QString(); - case FileType::QM: - return "QM"; - case FileType::PO: - return "PO"; - } - return QString(); - }; + readIndex(d->m_dir.absoluteFilePath("index_v2.json"), languages); auto entries = d->m_dir.entryInfoList({"mmc_*.qm", "*.po"}, QDir::Files | QDir::NoDotAndDotDot); for(auto & entry: entries) { @@ -221,13 +243,12 @@ void TranslationsModel::reloadLocalFiles() continue; } - auto langIter = languageLookup.find(langCode); - if(langIter != languageLookup.end()) + auto langIter = languages.find(langCode); + if(langIter != languages.end()) { - auto & language = languages[*langIter]; + auto & language = *langIter; if(int(fileType) > int(language.localFileType)) { - qDebug() << "Found" << fileTypeToString(fileType) << "local file for language" << langCode; language.localFileType = fileType; } } @@ -237,16 +258,50 @@ void TranslationsModel::reloadLocalFiles() { Language localFound(langCode); localFound.localFileType = FileType::PO; - languages.append(localFound); - qDebug() << "Found standalone translation PO file: " << langCode; + languages.insert(langCode, localFound); } } - } - beginResetModel(); - d->m_languages.swap(languages); - endResetModel(); + // 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; + } + beginInsertRows(QModelIndex(), d->m_languages.size(), d->m_languages.size() + languages.size() - 1); + for(auto & language: languages) + { + d->m_languages.append(language); + } + endInsertRows(); } QVariant TranslationsModel::data(const QModelIndex& index, int role) const diff --git a/application/pages/global/MultiMCPage.cpp b/application/pages/global/MultiMCPage.cpp index 1991c9ba..4dd070ac 100644 --- a/application/pages/global/MultiMCPage.cpp +++ b/application/pages/global/MultiMCPage.cpp @@ -237,7 +237,8 @@ void MultiMCPage::applySettings() auto s = MMC->settings(); // Language - s->set("Language", ui->languageBox->itemData(ui->languageBox->currentIndex()).toString()); + auto langCode = ui->languageBox->itemData(ui->languageBox->currentIndex()).toString(); + s->set("Language", langCode.isEmpty() ? "en" : langCode); if (ui->resetNotificationsBtn->isChecked()) {