File indexing completed on 2024-11-24 04:16:55

0001 /*
0002   SPDX-FileCopyrightText: 2023-2024 Laurent Montel <montel.org>
0003 
0004   SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "translatormodel.h"
0008 #include "libbergamot_debug.h"
0009 
0010 #include <KLocalizedString>
0011 #include <QColor>
0012 
0013 TranslatorModel::TranslatorModel(QObject *parent)
0014     : QAbstractListModel{parent}
0015 {
0016     updateInstalledLanguage();
0017 }
0018 
0019 TranslatorModel::~TranslatorModel() = default;
0020 
0021 void TranslatorModel::insertTranslators(const QVector<Translator> &translators)
0022 {
0023     if (rowCount() != 0) {
0024         beginResetModel();
0025         mTranslators.clear();
0026         endResetModel();
0027     }
0028     if (!translators.isEmpty()) {
0029         beginInsertRows(QModelIndex(), 0, translators.count() - 1);
0030         mTranslators = translators;
0031         endInsertRows();
0032     }
0033     updateInstalledLanguage();
0034 }
0035 
0036 void TranslatorModel::updateInstalledLanguage()
0037 {
0038     mLanguageInstalled = BergamotEngineUtils::languageLocallyStored();
0039     qCDebug(TRANSLATOR_LIBBERGAMOT_LOG) << "mLanguageInstalled " << mLanguageInstalled;
0040 }
0041 
0042 QVariant TranslatorModel::headerData(int section, Qt::Orientation orientation, int role) const
0043 {
0044     if (role == Qt::DisplayRole && orientation == Qt::Horizontal) {
0045         switch (static_cast<TranslatorRoles>(section)) {
0046         case TranslatorRoles::Source:
0047             return i18n("Source");
0048         case TranslatorModel::Target:
0049             return i18n("Target");
0050         case TranslatorModel::TypeTranslator:
0051             return i18n("Type");
0052         case TranslatorModel::Repository:
0053             return i18n("Repository");
0054         case TranslatorModel::InstalledVersion:
0055             return i18n("Installed Version");
0056         case TranslatorModel::AvailableVersion:
0057             return i18n("Available Version");
0058         case TranslatorModel::Installed:
0059             return i18n("Installed");
0060         case TranslatorModel::Identifier:
0061         case TranslatorModel::CheckSum:
0062         case TranslatorModel::NeedToUpdateLanguage:
0063         case TranslatorModel::Url:
0064             return {};
0065         }
0066     }
0067     return {};
0068 }
0069 
0070 int TranslatorModel::rowCount(const QModelIndex &parent) const
0071 {
0072     Q_UNUSED(parent)
0073     return mTranslators.count();
0074 }
0075 
0076 bool TranslatorModel::needToUpdateLanguageModel(const Translator &translator) const
0077 {
0078     const QString shortName{translator.shortName()};
0079     if (isInstalled(shortName)) {
0080         if (versionInstalled(shortName) < translator.version()) {
0081             return true;
0082         }
0083     }
0084     return false;
0085 }
0086 
0087 QVariant TranslatorModel::data(const QModelIndex &index, int role) const
0088 {
0089     if (index.row() < 0 || index.row() >= mTranslators.count()) {
0090         return {};
0091     }
0092     const auto translator = mTranslators.at(index.row());
0093     const int col = index.column();
0094     if (role == Qt::BackgroundRole) {
0095         if (needToUpdateLanguageModel(translator)) {
0096             if (static_cast<TranslatorRoles>(col) == TranslatorModel::InstalledVersion) {
0097                 return QColor(Qt::red);
0098             }
0099         }
0100         return {};
0101     } else if (role == Qt::DisplayRole) {
0102         switch (static_cast<TranslatorRoles>(col)) {
0103         case TranslatorModel::Source: {
0104             return translator.source();
0105         }
0106         case TranslatorModel::Target: {
0107             return translator.target();
0108         }
0109         case TranslatorModel::TypeTranslator: {
0110             return translator.type();
0111         }
0112         case TranslatorModel::Repository: {
0113             return translator.repository();
0114         }
0115         case TranslatorModel::InstalledVersion: {
0116             const QString shortName{translator.shortName()};
0117             if (isInstalled(shortName)) {
0118                 return versionInstalled(shortName);
0119             }
0120             return {};
0121         }
0122         case TranslatorModel::AvailableVersion: {
0123             return translator.version();
0124         }
0125         case TranslatorModel::Installed: {
0126             if (isInstalled(translator.shortName())) {
0127                 return i18n("Installed");
0128             }
0129             return {};
0130         }
0131         case TranslatorModel::Identifier: {
0132             return translator.shortName();
0133         }
0134         case TranslatorModel::Url: {
0135             return translator.url();
0136         }
0137         case TranslatorModel::CheckSum: {
0138             return translator.checkSum();
0139         }
0140         case TranslatorModel::NeedToUpdateLanguage: {
0141             return needToUpdateLanguageModel(translator);
0142         }
0143         }
0144     }
0145     return {};
0146 }
0147 
0148 bool TranslatorModel::isInstalled(const QString &shortName) const
0149 {
0150     for (const auto &lang : std::as_const(mLanguageInstalled)) {
0151         if (lang.shortName == shortName) {
0152             return true;
0153         }
0154     }
0155     return false;
0156 }
0157 
0158 int TranslatorModel::versionInstalled(const QString &shortName) const
0159 {
0160     for (const auto &lang : std::as_const(mLanguageInstalled)) {
0161         if (lang.shortName == shortName) {
0162             return lang.version;
0163         }
0164     }
0165     return false;
0166 }
0167 
0168 void TranslatorModel::clear()
0169 {
0170     if (rowCount() != 0) {
0171         beginResetModel();
0172         mTranslators.clear();
0173         endResetModel();
0174     }
0175     updateInstalledLanguage();
0176 }
0177 
0178 int TranslatorModel::columnCount(const QModelIndex &parent) const
0179 {
0180     Q_UNUSED(parent)
0181     return static_cast<int>(TranslatorRoles::LastColumn) + 1;
0182 }
0183 
0184 void TranslatorModel::removeLanguage(const QString &identifier)
0185 {
0186     auto index = std::find_if(mLanguageInstalled.begin(), mLanguageInstalled.end(), [identifier](const BergamotEngineUtils::LanguageInstalled &installed) {
0187         return (identifier == installed.shortName);
0188     });
0189     if (index != mLanguageInstalled.end()) {
0190         const QString absoluteLanguageModelPath = (*index).absoluteLanguageModelPath;
0191         if (!QDir(absoluteLanguageModelPath).removeRecursively()) {
0192             qCDebug(TRANSLATOR_LIBBERGAMOT_LOG) << "Impossible to delete " << absoluteLanguageModelPath;
0193             return;
0194         }
0195         mLanguageInstalled.removeAll(*index);
0196         beginResetModel();
0197         endResetModel();
0198     }
0199     updateInstalledLanguage();
0200 }
0201 
0202 #include "moc_translatormodel.cpp"