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

0001 /*
0002   SPDX-FileCopyrightText: 2023-2024 Laurent Montel <montel.org>
0003 
0004   SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "bergamotengineutils.h"
0008 #include "libbergamot_debug.h"
0009 #include "translator.h"
0010 
0011 #include <KConfigGroup>
0012 #include <KSharedConfig>
0013 #include <QDebug>
0014 #include <QJsonDocument>
0015 #include <QStandardPaths>
0016 
0017 QString BergamotEngineUtils::defaultBergamotRepository()
0018 {
0019     return QStringLiteral("https://translatelocally.com/models.json");
0020 }
0021 
0022 QString BergamotEngineUtils::groupName()
0023 {
0024     return QStringLiteral("BergamotTranslator");
0025 }
0026 
0027 QString BergamotEngineUtils::coreNumberKey()
0028 {
0029     return QStringLiteral("CoreNumber");
0030 }
0031 
0032 QString BergamotEngineUtils::memoryByThreadKey()
0033 {
0034     return QStringLiteral("MemoryByThread");
0035 }
0036 
0037 QString BergamotEngineUtils::useLocalCacheKey()
0038 {
0039     return QStringLiteral("UseLocalKey");
0040 }
0041 
0042 QString BergamotEngineUtils::storageLanguagePath()
0043 {
0044     return QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QStringLiteral("/bergamot-translator");
0045 }
0046 
0047 void BergamotEngineUtils::SettingsInfo::loadSettingsInfo()
0048 {
0049     KConfigGroup myGroup(KSharedConfig::openConfig(), BergamotEngineUtils::groupName());
0050     numberOfThread = myGroup.readEntry(BergamotEngineUtils::coreNumberKey(), 2);
0051     memoryByThread = myGroup.readEntry(BergamotEngineUtils::memoryByThreadKey(), 64);
0052     useLocalCache = myGroup.readEntry(BergamotEngineUtils::useLocalCacheKey(), false);
0053 }
0054 
0055 void BergamotEngineUtils::SettingsInfo::saveSettingsInfo()
0056 {
0057     KConfigGroup myGroup(KSharedConfig::openConfig(), BergamotEngineUtils::groupName());
0058     myGroup.writeEntry(BergamotEngineUtils::coreNumberKey(), numberOfThread);
0059     myGroup.writeEntry(BergamotEngineUtils::memoryByThreadKey(), memoryByThread);
0060     myGroup.writeEntry(BergamotEngineUtils::useLocalCacheKey(), useLocalCache);
0061     myGroup.sync();
0062 }
0063 
0064 QVector<BergamotEngineUtils::LanguageInstalled> BergamotEngineUtils::languageLocallyStored(const QString &path)
0065 {
0066     QString newPath = path;
0067     if (newPath.isEmpty()) {
0068         newPath = BergamotEngineUtils::storageLanguagePath();
0069     }
0070     QDir dir(newPath);
0071     QVector<BergamotEngineUtils::LanguageInstalled> languages;
0072     const QStringList list = dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot);
0073     // qCDebug(TRANSLATOR_LIBBERGAMOT_LOG) << " list " << list;
0074     for (const auto &name : list) {
0075         // qCDebug(TRANSLATOR_LIBBERGAMOT_LOG) << " name " << dir;
0076         const QString modelLanguagePath{dir.absolutePath() + QLatin1Char('/') + name};
0077         QFile modelInfoFile(modelLanguagePath + QStringLiteral("/model_info.json"));
0078         if (!modelInfoFile.exists()) {
0079             qCWarning(TRANSLATOR_LIBBERGAMOT_LOG) << "model_info.json not found in " << name;
0080             return {};
0081         }
0082         if (!modelInfoFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
0083             qCWarning(TRANSLATOR_LIBBERGAMOT_LOG) << "Impossible to open file " << name;
0084             return {};
0085         }
0086 
0087         const QByteArray data = modelInfoFile.readAll();
0088         modelInfoFile.close();
0089         const QJsonDocument jsonResponse = QJsonDocument::fromJson(data);
0090         Translator translator;
0091         translator.parse(jsonResponse.object(), false);
0092         if (translator.isValid()) {
0093             // With slimt we can use only tiny model
0094             if (translator.type() == QLatin1String("tiny")) {
0095                 // We can't test with isValid() as local info doesn't have url it's logical. // TODO create specific class ???
0096                 // qDebug() << " translator " << translator;
0097                 BergamotEngineUtils::LanguageInstalled lang;
0098                 const QString shortName = translator.shortName();
0099                 const QStringList langIdentifier = shortName.split(QLatin1Char('-'));
0100                 if (langIdentifier.count() >= 2) {
0101                     lang.from = langIdentifier.at(0);
0102                     lang.to = langIdentifier.at(1);
0103                     lang.shortName = shortName;
0104                     lang.absoluteLanguageModelPath = modelLanguagePath;
0105                     lang.version = translator.version();
0106                     languages.append(lang);
0107                 }
0108             }
0109         }
0110     }
0111     return languages;
0112 }
0113 
0114 QDebug operator<<(QDebug d, const BergamotEngineUtils::LanguageInstalled &t)
0115 {
0116     d << " from " << t.from;
0117     d << " to " << t.to;
0118     d << " shortName " << t.shortName;
0119     d << " absoluteLanguageModelPath " << t.absoluteLanguageModelPath;
0120     d << " version " << t.version;
0121     return d;
0122 }
0123 
0124 bool BergamotEngineUtils::LanguageInstalled::operator==(const LanguageInstalled &other) const
0125 {
0126     return from == other.from && to == other.to && shortName == other.shortName && absoluteLanguageModelPath == other.absoluteLanguageModelPath
0127         && version == other.version;
0128 }