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
74 return mTranslators.count();
77bool TranslatorModel::needToUpdateLanguageModel(
const Translator &translator)
const
79 const QString shortName{translator.shortName()};
80 if (isInstalled(shortName)) {
81 if (versionInstalled(shortName) < translator.version()) {
90 if (
index.row() < 0 ||
index.row() >= mTranslators.count()) {
93 const auto translator = mTranslators.at(
index.row());
94 const int col =
index.column();
96 if (needToUpdateLanguageModel(translator)) {
97 if (
static_cast<TranslatorRoles
>(col) == TranslatorModel::InstalledVersion) {
103 switch (
static_cast<TranslatorRoles
>(col)) {
104 case TranslatorModel::Source: {
105 return translator.source();
107 case TranslatorModel::Target: {
108 return translator.target();
110 case TranslatorModel::TypeTranslator: {
111 return translator.type();
113 case TranslatorModel::Repository: {
114 return translator.repository();
116 case TranslatorModel::InstalledVersion: {
117 const QString shortName{translator.shortName()};
118 if (isInstalled(shortName)) {
119 return versionInstalled(shortName);
123 case TranslatorModel::AvailableVersion: {
124 return translator.version();
126 case TranslatorModel::Installed: {
127 if (isInstalled(translator.shortName())) {
128 return i18n(
"Installed");
132 case TranslatorModel::Identifier: {
133 return translator.shortName();
135 case TranslatorModel::Url: {
136 return translator.url();
138 case TranslatorModel::CheckSum: {
139 return translator.checkSum();
141 case TranslatorModel::NeedToUpdateLanguage: {
142 return needToUpdateLanguageModel(translator);
149bool TranslatorModel::isInstalled(
const QString &shortName)
const
151 for (
const auto &lang : std::as_const(mLanguageInstalled)) {
152 if (lang.shortName == shortName) {
159int TranslatorModel::versionInstalled(
const QString &shortName)
const
161 for (
const auto &lang : std::as_const(mLanguageInstalled)) {
162 if (lang.shortName == shortName) {
169void TranslatorModel::clear()
171 if (rowCount() != 0) {
173 mTranslators.clear();
176 updateInstalledLanguage();
179int TranslatorModel::columnCount(
const QModelIndex &parent)
const
182 return static_cast<int>(TranslatorRoles::LastColumn) + 1;
185void TranslatorModel::removeLanguage(
const QString &identifier)
187 auto index = std::find_if(mLanguageInstalled.begin(), mLanguageInstalled.end(), [identifier](
const BergamotEngineUtils::LanguageInstalled &installed) {
188 return (identifier == installed.shortName);
190 if (
index != mLanguageInstalled.end()) {
191 const QString absoluteLanguageModelPath = (*index).absoluteLanguageModelPath;
192 if (!QDir(absoluteLanguageModelPath).removeRecursively()) {
193 qCDebug(TRANSLATOR_LIBBERGAMOT_LOG) <<
"Impossible to delete " << absoluteLanguageModelPath;
196 mLanguageInstalled.removeAll(*
index);
200 updateInstalledLanguage();
203#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=0
virtual QModelIndex parent(const QModelIndex &index) const const=0
qsizetype count() const const
bool isEmpty() const const