7#include "bergamotengineutils.h"
8#include "libbergamot_debug.h"
11#include <KConfigGroup>
12#include <KSharedConfig>
14#include <QJsonDocument>
15#include <QStandardPaths>
17QString BergamotEngineUtils::defaultBergamotRepository()
19 return QStringLiteral(
"https://translatelocally.com/models.json");
22QString BergamotEngineUtils::groupName()
24 return QStringLiteral(
"BergamotTranslator");
27QString BergamotEngineUtils::coreNumberKey()
29 return QStringLiteral(
"CoreNumber");
32QString BergamotEngineUtils::memoryByThreadKey()
34 return QStringLiteral(
"MemoryByThread");
37QString BergamotEngineUtils::useLocalCacheKey()
39 return QStringLiteral(
"UseLocalKey");
42QString BergamotEngineUtils::storageLanguagePath()
47void BergamotEngineUtils::SettingsInfo::loadSettingsInfo()
50 numberOfThread = myGroup.readEntry(BergamotEngineUtils::coreNumberKey(), 2);
51 memoryByThread = myGroup.readEntry(BergamotEngineUtils::memoryByThreadKey(), 64);
52 useLocalCache = myGroup.readEntry(BergamotEngineUtils::useLocalCacheKey(),
false);
55void BergamotEngineUtils::SettingsInfo::saveSettingsInfo()
58 myGroup.writeEntry(BergamotEngineUtils::coreNumberKey(), numberOfThread);
59 myGroup.writeEntry(BergamotEngineUtils::memoryByThreadKey(), memoryByThread);
60 myGroup.writeEntry(BergamotEngineUtils::useLocalCacheKey(), useLocalCache);
64QVector<BergamotEngineUtils::LanguageInstalled> BergamotEngineUtils::languageLocallyStored(
const QString &path)
66 QString newPath =
path;
68 newPath = BergamotEngineUtils::storageLanguagePath();
71 QVector<BergamotEngineUtils::LanguageInstalled>
languages;
74 for (
const auto &name : list) {
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;
83 qCWarning(TRANSLATOR_LIBBERGAMOT_LOG) <<
"Impossible to open file " <<
name;
87 const QByteArray data = modelInfoFile.readAll();
88 modelInfoFile.close();
90 Translator translator;
91 translator.parse(jsonResponse.
object(),
false);
92 if (translator.isValid()) {
94 if (translator.type() == QLatin1String(
"tiny")) {
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();
114QDebug
operator<<(QDebug d,
const BergamotEngineUtils::LanguageInstalled &t)
116 d <<
" from " << t.from;
118 d <<
" shortName " << t.shortName;
119 d <<
" absoluteLanguageModelPath " << t.absoluteLanguageModelPath;
120 d <<
" version " << t.version;
124bool BergamotEngineUtils::LanguageInstalled::operator==(
const LanguageInstalled &other)
const
126 return from == other.from && to == other.to && shortName == other.shortName && absoluteLanguageModelPath == other.absoluteLanguageModelPath
static KSharedConfig::Ptr openConfig(const QString &fileName=QString(), OpenFlags mode=FullConfig, QStandardPaths::StandardLocation type=QStandardPaths::GenericConfigLocation)
QString path(const QString &relativePath)
KIOCORE_EXPORT QString dir(const QString &fileClass)
KIOCORE_EXPORT QStringList list(const QString &fileClass)
QString name(StandardAction id)
KTEXTEDITOR_EXPORT QDebug operator<<(QDebug s, const MovingCursor &cursor)
NETWORKMANAGERQT_EXPORT QString version()
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