7#include "translatormodel.h"
8#include "libbergamot_debug.h"
10#include <KLocalizedString>
13TranslatorModel::TranslatorModel(
QObject *parent)
16 updateInstalledLanguage();
19TranslatorModel::~TranslatorModel() =
default;
23 if (rowCount() != 0) {
30 mTranslators = translators;
33 updateInstalledLanguage();
36void TranslatorModel::updateInstalledLanguage()
38 mLanguageInstalled = BergamotEngineUtils::languageLocallyStored();
39 qCDebug(TRANSLATOR_LIBBERGAMOT_LOG) <<
"mLanguageInstalled " << mLanguageInstalled;
45 switch (
static_cast<TranslatorRoles
>(section)) {
46 case TranslatorRoles::Source:
47 return i18n(
"Source");
48 case TranslatorModel::Target:
49 return i18n(
"Target");
50 case TranslatorModel::TypeTranslator:
52 case TranslatorModel::Repository:
53 return i18n(
"Repository");
54 case TranslatorModel::InstalledVersion:
55 return i18n(
"Installed Version");
56 case TranslatorModel::AvailableVersion:
57 return i18n(
"Available Version");
58 case TranslatorModel::Installed:
59 return i18n(
"Installed");
60 case TranslatorModel::Identifier:
61 case TranslatorModel::CheckSum:
62 case TranslatorModel::NeedToUpdateLanguage:
63 case TranslatorModel::Url:
70int TranslatorModel::rowCount(
const QModelIndex &parent)
const
73 return mTranslators.count();
76bool TranslatorModel::needToUpdateLanguageModel(
const Translator &translator)
const
78 const QString shortName{translator.shortName()};
79 if (isInstalled(shortName)) {
80 if (versionInstalled(shortName) < translator.version()) {
92 const auto translator = mTranslators.at(
index.
row());
95 if (needToUpdateLanguageModel(translator)) {
96 if (
static_cast<TranslatorRoles
>(col) == TranslatorModel::InstalledVersion) {
102 switch (
static_cast<TranslatorRoles
>(col)) {
103 case TranslatorModel::Source: {
104 return translator.source();
106 case TranslatorModel::Target: {
107 return translator.target();
109 case TranslatorModel::TypeTranslator: {
110 return translator.type();
112 case TranslatorModel::Repository: {
113 return translator.repository();
115 case TranslatorModel::InstalledVersion: {
116 const QString shortName{translator.shortName()};
117 if (isInstalled(shortName)) {
118 return versionInstalled(shortName);
122 case TranslatorModel::AvailableVersion: {
123 return translator.version();
125 case TranslatorModel::Installed: {
126 if (isInstalled(translator.shortName())) {
127 return i18n(
"Installed");
131 case TranslatorModel::Identifier: {
132 return translator.shortName();
134 case TranslatorModel::Url: {
135 return translator.url();
137 case TranslatorModel::CheckSum: {
138 return translator.checkSum();
140 case TranslatorModel::NeedToUpdateLanguage: {
141 return needToUpdateLanguageModel(translator);
148bool TranslatorModel::isInstalled(
const QString &shortName)
const
150 for (
const auto &lang : std::as_const(mLanguageInstalled)) {
151 if (lang.shortName == shortName) {
158int TranslatorModel::versionInstalled(
const QString &shortName)
const
160 for (
const auto &lang : std::as_const(mLanguageInstalled)) {
161 if (lang.shortName == shortName) {
168void TranslatorModel::clear()
170 if (rowCount() != 0) {
172 mTranslators.clear();
175 updateInstalledLanguage();
178int TranslatorModel::columnCount(
const QModelIndex &parent)
const
181 return static_cast<int>(TranslatorRoles::LastColumn) + 1;
184void TranslatorModel::removeLanguage(
const QString &identifier)
186 auto index = std::find_if(mLanguageInstalled.begin(), mLanguageInstalled.end(), [identifier](
const BergamotEngineUtils::LanguageInstalled &installed) {
187 return (identifier == installed.shortName);
189 if (
index != mLanguageInstalled.end()) {
190 const QString absoluteLanguageModelPath = (*index).absoluteLanguageModelPath;
191 if (!
QDir(absoluteLanguageModelPath).removeRecursively()) {
192 qCDebug(TRANSLATOR_LIBBERGAMOT_LOG) <<
"Impossible to delete " << absoluteLanguageModelPath;
195 mLanguageInstalled.removeAll(*
index);
199 updateInstalledLanguage();
202#include "moc_translatormodel.cpp"
QString i18n(const char *text, const TYPE &arg...)
void beginInsertRows(const QModelIndex &parent, int first, int last)
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
qsizetype count() const const
bool isEmpty() const const
QObject * parent() const const