KTextAddons

translatormodel.cpp
1/*
2 SPDX-FileCopyrightText: 2023-2025 Laurent Montel <montel@kde.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 if (parent.isValid()) // flat model
73 return 0;
74 return mTranslators.count();
75}
76
77bool TranslatorModel::needToUpdateLanguageModel(const Translator &translator) const
78{
79 const QString shortName{translator.shortName()};
80 if (isInstalled(shortName)) {
81 if (versionInstalled(shortName) < translator.version()) {
82 return true;
83 }
84 }
85 return false;
86}
87
88QVariant TranslatorModel::data(const QModelIndex &index, int role) const
89{
90 if (index.row() < 0 || index.row() >= mTranslators.count()) {
91 return {};
92 }
93 const auto translator = mTranslators.at(index.row());
94 const int col = index.column();
95 if (role == Qt::BackgroundRole) {
96 if (needToUpdateLanguageModel(translator)) {
97 if (static_cast<TranslatorRoles>(col) == TranslatorModel::InstalledVersion) {
98 return QColor(Qt::red);
99 }
100 }
101 return {};
102 } else if (role == Qt::DisplayRole) {
103 switch (static_cast<TranslatorRoles>(col)) {
104 case TranslatorModel::Source: {
105 return translator.source();
106 }
107 case TranslatorModel::Target: {
108 return translator.target();
109 }
110 case TranslatorModel::TypeTranslator: {
111 return translator.type();
112 }
113 case TranslatorModel::Repository: {
114 return translator.repository();
115 }
116 case TranslatorModel::InstalledVersion: {
117 const QString shortName{translator.shortName()};
118 if (isInstalled(shortName)) {
119 return versionInstalled(shortName);
120 }
121 return {};
122 }
123 case TranslatorModel::AvailableVersion: {
124 return translator.version();
125 }
126 case TranslatorModel::Installed: {
127 if (isInstalled(translator.shortName())) {
128 return i18n("Installed");
129 }
130 return {};
131 }
132 case TranslatorModel::Identifier: {
133 return translator.shortName();
134 }
135 case TranslatorModel::Url: {
136 return translator.url();
137 }
138 case TranslatorModel::CheckSum: {
139 return translator.checkSum();
140 }
141 case TranslatorModel::NeedToUpdateLanguage: {
142 return needToUpdateLanguageModel(translator);
143 }
144 }
145 }
146 return {};
147}
148
149bool TranslatorModel::isInstalled(const QString &shortName) const
150{
151 for (const auto &lang : std::as_const(mLanguageInstalled)) {
152 if (lang.shortName == shortName) {
153 return true;
154 }
155 }
156 return false;
157}
158
159int TranslatorModel::versionInstalled(const QString &shortName) const
160{
161 for (const auto &lang : std::as_const(mLanguageInstalled)) {
162 if (lang.shortName == shortName) {
163 return lang.version;
164 }
165 }
166 return false;
167}
168
169void TranslatorModel::clear()
170{
171 if (rowCount() != 0) {
173 mTranslators.clear();
175 }
176 updateInstalledLanguage();
177}
178
179int TranslatorModel::columnCount(const QModelIndex &parent) const
180{
181 Q_UNUSED(parent)
182 return static_cast<int>(TranslatorRoles::LastColumn) + 1;
183}
184
185void TranslatorModel::removeLanguage(const QString &identifier)
186{
187 auto index = std::find_if(mLanguageInstalled.begin(), mLanguageInstalled.end(), [identifier](const BergamotEngineUtils::LanguageInstalled &installed) {
188 return (identifier == installed.shortName);
189 });
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;
194 return;
195 }
196 mLanguageInstalled.removeAll(*index);
199 }
200 updateInstalledLanguage();
201}
202
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
DisplayRole
Orientation
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Mar 28 2025 11:48:40 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.