KTextAddons

translatormodel.cpp
1/*
2 SPDX-FileCopyrightText: 2023-2024 Laurent Montel <montel.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "translatormodel.h"
8#include "libbergamot_debug.h"
9
10#include <KLocalizedString>
11#include <QColor>
12
13TranslatorModel::TranslatorModel(QObject *parent)
14 : QAbstractListModel{parent}
15{
16 updateInstalledLanguage();
17}
18
19TranslatorModel::~TranslatorModel() = default;
20
21void TranslatorModel::insertTranslators(const QVector<Translator> &translators)
22{
23 if (rowCount() != 0) {
25 mTranslators.clear();
27 }
28 if (!translators.isEmpty()) {
29 beginInsertRows(QModelIndex(), 0, translators.count() - 1);
30 mTranslators = translators;
32 }
33 updateInstalledLanguage();
34}
35
36void TranslatorModel::updateInstalledLanguage()
37{
38 mLanguageInstalled = BergamotEngineUtils::languageLocallyStored();
39 qCDebug(TRANSLATOR_LIBBERGAMOT_LOG) << "mLanguageInstalled " << mLanguageInstalled;
40}
41
42QVariant TranslatorModel::headerData(int section, Qt::Orientation orientation, int role) const
43{
44 if (role == Qt::DisplayRole && orientation == Qt::Horizontal) {
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:
51 return i18n("Type");
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:
64 return {};
65 }
66 }
67 return {};
68}
69
70int TranslatorModel::rowCount(const QModelIndex &parent) const
71{
72 Q_UNUSED(parent)
73 return mTranslators.count();
74}
75
76bool TranslatorModel::needToUpdateLanguageModel(const Translator &translator) const
77{
78 const QString shortName{translator.shortName()};
79 if (isInstalled(shortName)) {
80 if (versionInstalled(shortName) < translator.version()) {
81 return true;
82 }
83 }
84 return false;
85}
86
87QVariant TranslatorModel::data(const QModelIndex &index, int role) const
88{
89 if (index.row() < 0 || index.row() >= mTranslators.count()) {
90 return {};
91 }
92 const auto translator = mTranslators.at(index.row());
93 const int col = index.column();
94 if (role == Qt::BackgroundRole) {
95 if (needToUpdateLanguageModel(translator)) {
96 if (static_cast<TranslatorRoles>(col) == TranslatorModel::InstalledVersion) {
97 return QColor(Qt::red);
98 }
99 }
100 return {};
101 } else if (role == Qt::DisplayRole) {
102 switch (static_cast<TranslatorRoles>(col)) {
103 case TranslatorModel::Source: {
104 return translator.source();
105 }
106 case TranslatorModel::Target: {
107 return translator.target();
108 }
109 case TranslatorModel::TypeTranslator: {
110 return translator.type();
111 }
112 case TranslatorModel::Repository: {
113 return translator.repository();
114 }
115 case TranslatorModel::InstalledVersion: {
116 const QString shortName{translator.shortName()};
117 if (isInstalled(shortName)) {
118 return versionInstalled(shortName);
119 }
120 return {};
121 }
122 case TranslatorModel::AvailableVersion: {
123 return translator.version();
124 }
125 case TranslatorModel::Installed: {
126 if (isInstalled(translator.shortName())) {
127 return i18n("Installed");
128 }
129 return {};
130 }
131 case TranslatorModel::Identifier: {
132 return translator.shortName();
133 }
134 case TranslatorModel::Url: {
135 return translator.url();
136 }
137 case TranslatorModel::CheckSum: {
138 return translator.checkSum();
139 }
140 case TranslatorModel::NeedToUpdateLanguage: {
141 return needToUpdateLanguageModel(translator);
142 }
143 }
144 }
145 return {};
146}
147
148bool TranslatorModel::isInstalled(const QString &shortName) const
149{
150 for (const auto &lang : std::as_const(mLanguageInstalled)) {
151 if (lang.shortName == shortName) {
152 return true;
153 }
154 }
155 return false;
156}
157
158int TranslatorModel::versionInstalled(const QString &shortName) const
159{
160 for (const auto &lang : std::as_const(mLanguageInstalled)) {
161 if (lang.shortName == shortName) {
162 return lang.version;
163 }
164 }
165 return false;
166}
167
168void TranslatorModel::clear()
169{
170 if (rowCount() != 0) {
172 mTranslators.clear();
174 }
175 updateInstalledLanguage();
176}
177
178int TranslatorModel::columnCount(const QModelIndex &parent) const
179{
180 Q_UNUSED(parent)
181 return static_cast<int>(TranslatorRoles::LastColumn) + 1;
182}
183
184void TranslatorModel::removeLanguage(const QString &identifier)
185{
186 auto index = std::find_if(mLanguageInstalled.begin(), mLanguageInstalled.end(), [identifier](const BergamotEngineUtils::LanguageInstalled &installed) {
187 return (identifier == installed.shortName);
188 });
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;
193 return;
194 }
195 mLanguageInstalled.removeAll(*index);
198 }
199 updateInstalledLanguage();
200}
201
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
int column() const const
int row() const const
QObject * parent() const const
DisplayRole
Orientation
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:56:02 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.