KTextAddons

bergamotengineutils.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 "bergamotengineutils.h"
8#include "libbergamot_debug.h"
9#include "translator.h"
10
11#include <KConfigGroup>
12#include <KSharedConfig>
13#include <QDebug>
14#include <QJsonDocument>
15#include <QStandardPaths>
16
17QString BergamotEngineUtils::defaultBergamotRepository()
18{
19 return QStringLiteral("https://translatelocally.com/models.json");
20}
21
22QString BergamotEngineUtils::groupName()
23{
24 return QStringLiteral("BergamotTranslator");
25}
26
27QString BergamotEngineUtils::coreNumberKey()
28{
29 return QStringLiteral("CoreNumber");
30}
31
32QString BergamotEngineUtils::memoryByThreadKey()
33{
34 return QStringLiteral("MemoryByThread");
35}
36
37QString BergamotEngineUtils::useLocalCacheKey()
38{
39 return QStringLiteral("UseLocalKey");
40}
41
42QString BergamotEngineUtils::storageLanguagePath()
43{
44 return QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QStringLiteral("/bergamot-translator");
45}
46
47void BergamotEngineUtils::SettingsInfo::loadSettingsInfo()
48{
49 KConfigGroup myGroup(KSharedConfig::openConfig(), BergamotEngineUtils::groupName());
50 numberOfThread = myGroup.readEntry(BergamotEngineUtils::coreNumberKey(), 2);
51 memoryByThread = myGroup.readEntry(BergamotEngineUtils::memoryByThreadKey(), 64);
52 useLocalCache = myGroup.readEntry(BergamotEngineUtils::useLocalCacheKey(), false);
53}
54
55void BergamotEngineUtils::SettingsInfo::saveSettingsInfo()
56{
57 KConfigGroup myGroup(KSharedConfig::openConfig(), BergamotEngineUtils::groupName());
58 myGroup.writeEntry(BergamotEngineUtils::coreNumberKey(), numberOfThread);
59 myGroup.writeEntry(BergamotEngineUtils::memoryByThreadKey(), memoryByThread);
60 myGroup.writeEntry(BergamotEngineUtils::useLocalCacheKey(), useLocalCache);
61 myGroup.sync();
62}
63
64QVector<BergamotEngineUtils::LanguageInstalled> BergamotEngineUtils::languageLocallyStored(const QString &path)
65{
66 QString newPath = path;
67 if (newPath.isEmpty()) {
68 newPath = BergamotEngineUtils::storageLanguagePath();
69 }
70 QDir dir(newPath);
73 // qCDebug(TRANSLATOR_LIBBERGAMOT_LOG) << " list " << list;
74 for (const auto &name : list) {
75 // qCDebug(TRANSLATOR_LIBBERGAMOT_LOG) << " name " << dir;
76 const QString modelLanguagePath{dir.absolutePath() + QLatin1Char('/') + name};
77 QFile modelInfoFile(modelLanguagePath + QStringLiteral("/model_info.json"));
78 if (!modelInfoFile.exists()) {
79 qCWarning(TRANSLATOR_LIBBERGAMOT_LOG) << "model_info.json not found in " << name;
80 return {};
81 }
82 if (!modelInfoFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
83 qCWarning(TRANSLATOR_LIBBERGAMOT_LOG) << "Impossible to open file " << name;
84 return {};
85 }
86
87 const QByteArray data = modelInfoFile.readAll();
88 modelInfoFile.close();
89 const QJsonDocument jsonResponse = QJsonDocument::fromJson(data);
90 Translator translator;
91 translator.parse(jsonResponse.object(), false);
92 if (translator.isValid()) {
93 // With slimt we can use only tiny model
94 if (translator.type() == QLatin1String("tiny")) {
95 // We can't test with isValid() as local info doesn't have url it's logical. // TODO create specific class ???
96 // qDebug() << " translator " << translator;
97 BergamotEngineUtils::LanguageInstalled lang;
98 const QString shortName = translator.shortName();
99 const QStringList langIdentifier = shortName.split(QLatin1Char('-'));
100 if (langIdentifier.count() >= 2) {
101 lang.from = langIdentifier.at(0);
102 lang.to = langIdentifier.at(1);
103 lang.shortName = shortName;
104 lang.absoluteLanguageModelPath = modelLanguagePath;
105 lang.version = translator.version();
106 languages.append(lang);
107 }
108 }
109 }
110 }
111 return languages;
112}
113
114QDebug operator<<(QDebug d, const BergamotEngineUtils::LanguageInstalled &t)
115{
116 d << " from " << t.from;
117 d << " to " << t.to;
118 d << " shortName " << t.shortName;
119 d << " absoluteLanguageModelPath " << t.absoluteLanguageModelPath;
120 d << " version " << t.version;
121 return d;
122}
123
124bool BergamotEngineUtils::LanguageInstalled::operator==(const LanguageInstalled &other) const
125{
126 return from == other.from && to == other.to && shortName == other.shortName && absoluteLanguageModelPath == other.absoluteLanguageModelPath
127 && version == other.version;
128}
static KSharedConfig::Ptr openConfig(const QString &fileName=QString(), OpenFlags mode=FullConfig, QStandardPaths::StandardLocation type=QStandardPaths::GenericConfigLocation)
KCOREADDONS_EXPORT unsigned int version()
QDebug operator<<(QDebug dbg, const DcrawInfoContainer &c)
QString path(const QString &relativePath)
KIOCORE_EXPORT QString dir(const QString &fileClass)
KIOCORE_EXPORT QStringList list(const QString &fileClass)
QString name(StandardAction id)
KEDUVOCDOCUMENT_EXPORT QStringList languages()
QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error)
QJsonObject object() const const
void append(QList< T > &&value)
const_reference at(qsizetype i) const const
qsizetype count() const const
QString writableLocation(StandardLocation type)
bool isEmpty() const const
QStringList split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
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.