2023-06-27 11:54:34 -04:00
|
|
|
#include "mysettings.h"
|
2024-06-04 14:47:11 -04:00
|
|
|
|
2023-09-15 14:45:25 -04:00
|
|
|
#include "../gpt4all-backend/llmodel.h"
|
2023-06-27 11:54:34 -04:00
|
|
|
|
2024-06-04 14:47:11 -04:00
|
|
|
#include <QDebug>
|
2023-06-28 13:47:57 -04:00
|
|
|
#include <QDir>
|
|
|
|
#include <QFile>
|
|
|
|
#include <QFileInfo>
|
2024-06-04 14:47:11 -04:00
|
|
|
#include <QGlobalStatic>
|
2024-07-12 16:14:58 -04:00
|
|
|
#include <QGuiApplication>
|
2024-06-04 14:47:11 -04:00
|
|
|
#include <QIODevice>
|
2024-06-24 18:49:23 -04:00
|
|
|
#include <QMap>
|
|
|
|
#include <QMetaObject>
|
2023-06-28 13:47:57 -04:00
|
|
|
#include <QStandardPaths>
|
2024-06-04 14:47:11 -04:00
|
|
|
#include <QThread>
|
2023-06-28 16:05:35 -04:00
|
|
|
#include <QUrl>
|
2024-06-04 14:47:11 -04:00
|
|
|
#include <QVariant>
|
2024-06-24 18:49:23 -04:00
|
|
|
#include <QtLogging>
|
2024-06-04 14:47:11 -04:00
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <string>
|
|
|
|
#include <thread>
|
|
|
|
#include <vector>
|
2023-06-28 13:47:57 -04:00
|
|
|
|
2024-06-24 18:49:23 -04:00
|
|
|
using namespace Qt::Literals::StringLiterals;
|
|
|
|
|
2024-07-16 16:12:44 -04:00
|
|
|
// used only for settings serialization, do not translate
|
|
|
|
static const QStringList suggestionModeNames { "LocalDocsOnly", "On", "Off" };
|
|
|
|
static const QStringList chatThemeNames { "Light", "Dark", "LegacyDark" };
|
|
|
|
static const QStringList fontSizeNames { "Small", "Medium", "Large" };
|
|
|
|
|
2024-07-12 16:14:58 -04:00
|
|
|
// FIXME: All of these default strings that are shown in the UI for settings need to be marked as
|
|
|
|
// translatable
|
|
|
|
|
2024-06-24 18:49:23 -04:00
|
|
|
namespace defaults {
|
|
|
|
|
|
|
|
static const int threadCount = std::min(4, (int32_t) std::thread::hardware_concurrency());
|
|
|
|
static const bool forceMetal = false;
|
|
|
|
static const bool networkIsActive = false;
|
|
|
|
static const bool networkUsageStatsActive = false;
|
|
|
|
static const QString device = "Auto";
|
2024-07-19 14:28:54 -04:00
|
|
|
static const QString languageAndLocale = "System Locale";
|
2024-06-24 18:49:23 -04:00
|
|
|
|
|
|
|
} // namespace defaults
|
|
|
|
|
|
|
|
static const QVariantMap basicDefaults {
|
2024-07-16 16:12:44 -04:00
|
|
|
{ "chatTheme", QVariant::fromValue(ChatTheme::Light) },
|
|
|
|
{ "fontSize", QVariant::fromValue(FontSize::Small) },
|
2024-06-24 18:49:23 -04:00
|
|
|
{ "lastVersionStarted", "" },
|
|
|
|
{ "networkPort", 4891, },
|
|
|
|
{ "saveChatsContext", false },
|
|
|
|
{ "serverChat", false },
|
|
|
|
{ "userDefaultModel", "Application default" },
|
2024-07-10 15:45:20 -04:00
|
|
|
{ "suggestionMode", QVariant::fromValue(SuggestionMode::LocalDocsOnly) },
|
2024-06-30 19:15:01 -04:00
|
|
|
{ "localdocs/chunkSize", 512 },
|
2024-06-24 18:49:23 -04:00
|
|
|
{ "localdocs/retrievalSize", 3 },
|
|
|
|
{ "localdocs/showReferences", true },
|
|
|
|
{ "localdocs/fileExtensions", QStringList { "txt", "pdf", "md", "rst" } },
|
|
|
|
{ "localdocs/useRemoteEmbed", false },
|
|
|
|
{ "localdocs/nomicAPIKey", "" },
|
2024-06-28 12:57:57 -04:00
|
|
|
{ "localdocs/embedDevice", "Auto" },
|
2024-06-24 18:49:23 -04:00
|
|
|
{ "network/attribution", "" },
|
|
|
|
};
|
2023-06-28 13:47:57 -04:00
|
|
|
|
|
|
|
static QString defaultLocalModelsPath()
|
|
|
|
{
|
|
|
|
QString localPath = QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation)
|
|
|
|
+ "/";
|
2024-06-24 18:49:23 -04:00
|
|
|
QString testWritePath = localPath + u"test_write.txt"_s;
|
2023-06-28 13:47:57 -04:00
|
|
|
QString canonicalLocalPath = QFileInfo(localPath).canonicalFilePath() + "/";
|
|
|
|
QDir localDir(localPath);
|
|
|
|
if (!localDir.exists()) {
|
|
|
|
if (!localDir.mkpath(localPath)) {
|
|
|
|
qWarning() << "ERROR: Local download directory can't be created:" << canonicalLocalPath;
|
|
|
|
return canonicalLocalPath;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (QFileInfo::exists(testWritePath))
|
|
|
|
return canonicalLocalPath;
|
|
|
|
|
|
|
|
QFile testWriteFile(testWritePath);
|
|
|
|
if (testWriteFile.open(QIODeviceBase::ReadWrite)) {
|
|
|
|
testWriteFile.close();
|
|
|
|
return canonicalLocalPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
qWarning() << "ERROR: Local download path appears not writeable:" << canonicalLocalPath;
|
|
|
|
return canonicalLocalPath;
|
|
|
|
}
|
2023-06-27 11:54:34 -04:00
|
|
|
|
2024-06-28 12:57:57 -04:00
|
|
|
static QStringList getDevices(bool skipKompute = false)
|
|
|
|
{
|
2024-07-19 14:28:54 -04:00
|
|
|
QStringList deviceList;
|
2024-06-28 12:57:57 -04:00
|
|
|
#if defined(Q_OS_MAC) && defined(__aarch64__)
|
|
|
|
deviceList << "Metal";
|
|
|
|
#else
|
|
|
|
std::vector<LLModel::GPUDevice> devices = LLModel::Implementation::availableGPUDevices();
|
|
|
|
for (LLModel::GPUDevice &d : devices) {
|
|
|
|
if (!skipKompute || strcmp(d.backend, "kompute"))
|
|
|
|
deviceList << QString::fromStdString(d.selectionName());
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
deviceList << "CPU";
|
|
|
|
return deviceList;
|
|
|
|
}
|
|
|
|
|
2024-07-12 16:14:58 -04:00
|
|
|
static QString getUiLanguage(const QString directory, const QString fileName)
|
|
|
|
{
|
|
|
|
QTranslator translator;
|
|
|
|
const QString filePath = directory + QDir::separator() + fileName;
|
|
|
|
if (translator.load(filePath)) {
|
|
|
|
const QString lang = fileName.mid(fileName.indexOf('_') + 1,
|
|
|
|
fileName.lastIndexOf('.') - fileName.indexOf('_') - 1);
|
|
|
|
return lang;
|
|
|
|
}
|
|
|
|
|
|
|
|
qDebug() << "ERROR: Failed to load translation file:" << filePath;
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
|
|
|
static QStringList getUiLanguages(const QString &modelPath)
|
|
|
|
{
|
2024-07-19 14:28:54 -04:00
|
|
|
QStringList languageList;
|
2024-07-12 16:14:58 -04:00
|
|
|
|
|
|
|
// Add the language translations from model path files first which is used by translation developers
|
|
|
|
// to load translations in progress without having to rebuild all of GPT4All from source
|
|
|
|
{
|
|
|
|
const QDir dir(modelPath);
|
|
|
|
const QStringList qmFiles = dir.entryList({"*.qm"}, QDir::Files);
|
|
|
|
for (const QString &fileName : qmFiles)
|
|
|
|
languageList << getUiLanguage(modelPath, fileName);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now add the internal language translations
|
|
|
|
{
|
|
|
|
const QDir dir(":/i18n");
|
|
|
|
const QStringList qmFiles = dir.entryList({"*.qm"}, QDir::Files);
|
|
|
|
for (const QString &fileName : qmFiles) {
|
|
|
|
const QString lang = getUiLanguage(":/i18n", fileName);
|
|
|
|
if (!languageList.contains(lang))
|
|
|
|
languageList.append(lang);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return languageList;
|
|
|
|
}
|
|
|
|
|
2023-06-27 11:54:34 -04:00
|
|
|
class MyPrivateSettings: public MySettings { };
|
|
|
|
Q_GLOBAL_STATIC(MyPrivateSettings, settingsInstance)
|
|
|
|
MySettings *MySettings::globalInstance()
|
|
|
|
{
|
|
|
|
return settingsInstance();
|
|
|
|
}
|
|
|
|
|
|
|
|
MySettings::MySettings()
|
2024-06-28 12:57:57 -04:00
|
|
|
: QObject(nullptr)
|
|
|
|
, m_deviceList(getDevices())
|
|
|
|
, m_embeddingsDeviceList(getDevices(/*skipKompute*/ true))
|
2024-07-12 16:14:58 -04:00
|
|
|
, m_uiLanguages(getUiLanguages(modelPath()))
|
2023-06-27 11:54:34 -04:00
|
|
|
{
|
2023-06-28 13:47:57 -04:00
|
|
|
}
|
|
|
|
|
2024-06-24 18:49:23 -04:00
|
|
|
QVariant MySettings::getBasicSetting(const QString &name) const
|
2023-07-01 11:34:21 -04:00
|
|
|
{
|
2024-06-24 18:49:23 -04:00
|
|
|
return m_settings.value(name, basicDefaults.value(name));
|
2023-07-01 11:34:21 -04:00
|
|
|
}
|
|
|
|
|
2024-06-24 18:49:23 -04:00
|
|
|
void MySettings::setBasicSetting(const QString &name, const QVariant &value, std::optional<QString> signal)
|
2023-07-01 11:34:21 -04:00
|
|
|
{
|
2024-06-24 18:49:23 -04:00
|
|
|
if (getBasicSetting(name) == value)
|
2023-07-01 11:34:21 -04:00
|
|
|
return;
|
|
|
|
|
2024-06-24 18:49:23 -04:00
|
|
|
m_settings.setValue(name, value);
|
|
|
|
QMetaObject::invokeMethod(this, u"%1Changed"_s.arg(signal.value_or(name)).toLatin1().constData());
|
2023-07-01 11:34:21 -04:00
|
|
|
}
|
|
|
|
|
2024-07-16 16:12:44 -04:00
|
|
|
int MySettings::getEnumSetting(const QString &setting, const QStringList &valueNames) const
|
|
|
|
{
|
|
|
|
int idx = valueNames.indexOf(getBasicSetting(setting).toString());
|
|
|
|
return idx != -1 ? idx : *reinterpret_cast<const int *>(basicDefaults.value(setting).constData());
|
|
|
|
}
|
|
|
|
|
2024-06-24 18:49:23 -04:00
|
|
|
void MySettings::restoreModelDefaults(const ModelInfo &info)
|
2024-03-05 11:31:31 -05:00
|
|
|
{
|
2024-06-24 18:49:23 -04:00
|
|
|
setModelTemperature(info, info.m_temperature);
|
|
|
|
setModelTopP(info, info.m_topP);
|
|
|
|
setModelMinP(info, info.m_minP);
|
|
|
|
setModelTopK(info, info.m_topK);;
|
|
|
|
setModelMaxLength(info, info.m_maxLength);
|
|
|
|
setModelPromptBatchSize(info, info.m_promptBatchSize);
|
|
|
|
setModelContextLength(info, info.m_contextLength);
|
|
|
|
setModelGpuLayers(info, info.m_gpuLayers);
|
|
|
|
setModelRepeatPenalty(info, info.m_repeatPenalty);
|
|
|
|
setModelRepeatPenaltyTokens(info, info.m_repeatPenaltyTokens);
|
|
|
|
setModelPromptTemplate(info, info.m_promptTemplate);
|
|
|
|
setModelSystemPrompt(info, info.m_systemPrompt);
|
2024-07-10 15:45:20 -04:00
|
|
|
setModelChatNamePrompt(info, info.m_chatNamePrompt);
|
|
|
|
setModelSuggestedFollowUpPrompt(info, info.m_suggestedFollowUpPrompt);
|
2024-03-05 11:31:31 -05:00
|
|
|
}
|
|
|
|
|
2024-06-24 18:49:23 -04:00
|
|
|
void MySettings::restoreApplicationDefaults()
|
2024-03-05 11:31:31 -05:00
|
|
|
{
|
2024-07-16 16:12:44 -04:00
|
|
|
setChatTheme(basicDefaults.value("chatTheme").value<ChatTheme>());
|
|
|
|
setFontSize(basicDefaults.value("fontSize").value<FontSize>());
|
2024-06-24 18:49:23 -04:00
|
|
|
setDevice(defaults::device);
|
|
|
|
setThreadCount(defaults::threadCount);
|
|
|
|
setSaveChatsContext(basicDefaults.value("saveChatsContext").toBool());
|
|
|
|
setServerChat(basicDefaults.value("serverChat").toBool());
|
|
|
|
setNetworkPort(basicDefaults.value("networkPort").toInt());
|
|
|
|
setModelPath(defaultLocalModelsPath());
|
|
|
|
setUserDefaultModel(basicDefaults.value("userDefaultModel").toString());
|
|
|
|
setForceMetal(defaults::forceMetal);
|
2024-07-10 15:45:20 -04:00
|
|
|
setSuggestionMode(basicDefaults.value("suggestionMode").value<SuggestionMode>());
|
2024-07-12 16:14:58 -04:00
|
|
|
setLanguageAndLocale(defaults::languageAndLocale);
|
2024-03-05 11:31:31 -05:00
|
|
|
}
|
|
|
|
|
2024-06-24 18:49:23 -04:00
|
|
|
void MySettings::restoreLocalDocsDefaults()
|
2024-03-05 11:31:31 -05:00
|
|
|
{
|
2024-06-24 18:49:23 -04:00
|
|
|
setLocalDocsChunkSize(basicDefaults.value("localdocs/chunkSize").toInt());
|
|
|
|
setLocalDocsRetrievalSize(basicDefaults.value("localdocs/retrievalSize").toInt());
|
|
|
|
setLocalDocsShowReferences(basicDefaults.value("localdocs/showReferences").toBool());
|
|
|
|
setLocalDocsFileExtensions(basicDefaults.value("localdocs/fileExtensions").toStringList());
|
|
|
|
setLocalDocsUseRemoteEmbed(basicDefaults.value("localdocs/useRemoteEmbed").toBool());
|
|
|
|
setLocalDocsNomicAPIKey(basicDefaults.value("localdocs/nomicAPIKey").toString());
|
2024-06-28 12:57:57 -04:00
|
|
|
setLocalDocsEmbedDevice(basicDefaults.value("localdocs/embedDevice").toString());
|
2024-03-05 11:31:31 -05:00
|
|
|
}
|
|
|
|
|
2024-06-24 18:49:23 -04:00
|
|
|
void MySettings::eraseModel(const ModelInfo &info)
|
2024-03-05 11:31:31 -05:00
|
|
|
{
|
2024-06-24 18:49:23 -04:00
|
|
|
m_settings.remove(u"model-%1"_s.arg(info.id()));
|
2024-03-05 11:31:31 -05:00
|
|
|
}
|
|
|
|
|
2024-06-24 18:49:23 -04:00
|
|
|
QString MySettings::modelName(const ModelInfo &info) const
|
2024-03-05 11:31:31 -05:00
|
|
|
{
|
2024-06-24 18:49:23 -04:00
|
|
|
return m_settings.value(u"model-%1/name"_s.arg(info.id()),
|
|
|
|
!info.m_name.isEmpty() ? info.m_name : info.m_filename).toString();
|
2024-03-05 11:31:31 -05:00
|
|
|
}
|
|
|
|
|
2024-06-24 18:49:23 -04:00
|
|
|
void MySettings::setModelName(const ModelInfo &info, const QString &value, bool force)
|
2024-03-05 11:31:31 -05:00
|
|
|
{
|
2024-06-24 18:49:23 -04:00
|
|
|
if ((modelName(info) == value || info.id().isEmpty()) && !force)
|
2024-03-05 11:31:31 -05:00
|
|
|
return;
|
|
|
|
|
2024-06-24 18:49:23 -04:00
|
|
|
if ((info.m_name == value || info.m_filename == value) && !info.shouldSaveMetadata())
|
|
|
|
m_settings.remove(u"model-%1/name"_s.arg(info.id()));
|
2024-03-05 11:31:31 -05:00
|
|
|
else
|
2024-06-24 18:49:23 -04:00
|
|
|
m_settings.setValue(u"model-%1/name"_s.arg(info.id()), value);
|
|
|
|
if (!force)
|
|
|
|
emit nameChanged(info);
|
2024-03-05 11:31:31 -05:00
|
|
|
}
|
|
|
|
|
2024-06-24 18:49:23 -04:00
|
|
|
static QString modelSettingName(const ModelInfo &info, const QString &name)
|
2024-03-05 11:31:31 -05:00
|
|
|
{
|
2024-06-24 18:49:23 -04:00
|
|
|
return u"model-%1/%2"_s.arg(info.id(), name);
|
2024-03-05 11:31:31 -05:00
|
|
|
}
|
|
|
|
|
2024-06-24 18:49:23 -04:00
|
|
|
QVariant MySettings::getModelSetting(const QString &name, const ModelInfo &info) const
|
2024-03-05 11:31:31 -05:00
|
|
|
{
|
2024-06-24 18:49:23 -04:00
|
|
|
return m_settings.value(modelSettingName(info, name), info.getFields().value(name));
|
2024-03-05 11:31:31 -05:00
|
|
|
}
|
|
|
|
|
2024-06-24 18:49:23 -04:00
|
|
|
void MySettings::setModelSetting(const QString &name, const ModelInfo &info, const QVariant &value, bool force,
|
|
|
|
bool signal)
|
2024-03-05 11:31:31 -05:00
|
|
|
{
|
2024-06-24 18:49:23 -04:00
|
|
|
if (!force && (info.id().isEmpty() || getModelSetting(name, info) == value))
|
2024-03-05 11:31:31 -05:00
|
|
|
return;
|
|
|
|
|
2024-06-24 18:49:23 -04:00
|
|
|
QString settingName = modelSettingName(info, name);
|
|
|
|
if (info.getFields().value(name) == value && !info.shouldSaveMetadata())
|
|
|
|
m_settings.remove(settingName);
|
2024-03-05 11:31:31 -05:00
|
|
|
else
|
2024-06-24 18:49:23 -04:00
|
|
|
m_settings.setValue(settingName, value);
|
|
|
|
if (signal && !force)
|
|
|
|
QMetaObject::invokeMethod(this, u"%1Changed"_s.arg(name).toLatin1().constData(), Q_ARG(ModelInfo, info));
|
2024-03-05 11:31:31 -05:00
|
|
|
}
|
|
|
|
|
2024-07-10 15:45:20 -04:00
|
|
|
QString MySettings::modelFilename (const ModelInfo &info) const { return getModelSetting("filename", info).toString(); }
|
|
|
|
QString MySettings::modelDescription (const ModelInfo &info) const { return getModelSetting("description", info).toString(); }
|
|
|
|
QString MySettings::modelUrl (const ModelInfo &info) const { return getModelSetting("url", info).toString(); }
|
|
|
|
QString MySettings::modelQuant (const ModelInfo &info) const { return getModelSetting("quant", info).toString(); }
|
|
|
|
QString MySettings::modelType (const ModelInfo &info) const { return getModelSetting("type", info).toString(); }
|
|
|
|
bool MySettings::modelIsClone (const ModelInfo &info) const { return getModelSetting("isClone", info).toBool(); }
|
|
|
|
bool MySettings::modelIsDiscovered (const ModelInfo &info) const { return getModelSetting("isDiscovered", info).toBool(); }
|
|
|
|
int MySettings::modelLikes (const ModelInfo &info) const { return getModelSetting("likes", info).toInt(); }
|
|
|
|
int MySettings::modelDownloads (const ModelInfo &info) const { return getModelSetting("downloads", info).toInt(); }
|
|
|
|
QDateTime MySettings::modelRecency (const ModelInfo &info) const { return getModelSetting("recency", info).toDateTime(); }
|
|
|
|
double MySettings::modelTemperature (const ModelInfo &info) const { return getModelSetting("temperature", info).toDouble(); }
|
|
|
|
double MySettings::modelTopP (const ModelInfo &info) const { return getModelSetting("topP", info).toDouble(); }
|
|
|
|
double MySettings::modelMinP (const ModelInfo &info) const { return getModelSetting("minP", info).toDouble(); }
|
|
|
|
int MySettings::modelTopK (const ModelInfo &info) const { return getModelSetting("topK", info).toInt(); }
|
|
|
|
int MySettings::modelMaxLength (const ModelInfo &info) const { return getModelSetting("maxLength", info).toInt(); }
|
|
|
|
int MySettings::modelPromptBatchSize (const ModelInfo &info) const { return getModelSetting("promptBatchSize", info).toInt(); }
|
|
|
|
int MySettings::modelContextLength (const ModelInfo &info) const { return getModelSetting("contextLength", info).toInt(); }
|
|
|
|
int MySettings::modelGpuLayers (const ModelInfo &info) const { return getModelSetting("gpuLayers", info).toInt(); }
|
|
|
|
double MySettings::modelRepeatPenalty (const ModelInfo &info) const { return getModelSetting("repeatPenalty", info).toDouble(); }
|
|
|
|
int MySettings::modelRepeatPenaltyTokens (const ModelInfo &info) const { return getModelSetting("repeatPenaltyTokens", info).toInt(); }
|
|
|
|
QString MySettings::modelPromptTemplate (const ModelInfo &info) const { return getModelSetting("promptTemplate", info).toString(); }
|
|
|
|
QString MySettings::modelSystemPrompt (const ModelInfo &info) const { return getModelSetting("systemPrompt", info).toString(); }
|
|
|
|
QString MySettings::modelChatNamePrompt (const ModelInfo &info) const { return getModelSetting("chatNamePrompt", info).toString(); }
|
|
|
|
QString MySettings::modelSuggestedFollowUpPrompt(const ModelInfo &info) const { return getModelSetting("suggestedFollowUpPrompt", info).toString(); }
|
2024-03-05 11:31:31 -05:00
|
|
|
|
2024-06-24 18:49:23 -04:00
|
|
|
void MySettings::setModelFilename(const ModelInfo &info, const QString &value, bool force)
|
2024-03-05 11:31:31 -05:00
|
|
|
{
|
2024-06-24 18:49:23 -04:00
|
|
|
setModelSetting("filename", info, value, force, true);
|
2024-03-05 11:31:31 -05:00
|
|
|
}
|
|
|
|
|
2024-06-24 18:49:23 -04:00
|
|
|
void MySettings::setModelDescription(const ModelInfo &info, const QString &value, bool force)
|
2024-03-05 11:31:31 -05:00
|
|
|
{
|
2024-06-24 18:49:23 -04:00
|
|
|
setModelSetting("description", info, value, force, true);
|
2024-03-05 11:31:31 -05:00
|
|
|
}
|
|
|
|
|
2024-06-24 18:49:23 -04:00
|
|
|
void MySettings::setModelUrl(const ModelInfo &info, const QString &value, bool force)
|
2024-03-05 11:31:31 -05:00
|
|
|
{
|
2024-06-24 18:49:23 -04:00
|
|
|
setModelSetting("url", info, value, force);
|
2024-03-05 11:31:31 -05:00
|
|
|
}
|
|
|
|
|
2024-06-24 18:49:23 -04:00
|
|
|
void MySettings::setModelQuant(const ModelInfo &info, const QString &value, bool force)
|
2023-07-01 11:34:21 -04:00
|
|
|
{
|
2024-06-24 18:49:23 -04:00
|
|
|
setModelSetting("quant", info, value, force);
|
2023-07-01 11:34:21 -04:00
|
|
|
}
|
|
|
|
|
2024-06-24 18:49:23 -04:00
|
|
|
void MySettings::setModelType(const ModelInfo &info, const QString &value, bool force)
|
2023-07-01 11:34:21 -04:00
|
|
|
{
|
2024-06-24 18:49:23 -04:00
|
|
|
setModelSetting("type", info, value, force);
|
2023-07-01 11:34:21 -04:00
|
|
|
}
|
|
|
|
|
2024-06-24 18:49:23 -04:00
|
|
|
void MySettings::setModelIsClone(const ModelInfo &info, bool value, bool force)
|
2023-06-28 13:47:57 -04:00
|
|
|
{
|
2024-06-24 18:49:23 -04:00
|
|
|
setModelSetting("isClone", info, value, force);
|
2023-06-28 13:47:57 -04:00
|
|
|
}
|
|
|
|
|
2024-06-24 18:49:23 -04:00
|
|
|
void MySettings::setModelIsDiscovered(const ModelInfo &info, bool value, bool force)
|
2024-02-24 17:51:34 -05:00
|
|
|
{
|
2024-06-24 18:49:23 -04:00
|
|
|
setModelSetting("isDiscovered", info, value, force);
|
2024-02-24 17:51:34 -05:00
|
|
|
}
|
|
|
|
|
2024-06-24 18:49:23 -04:00
|
|
|
void MySettings::setModelLikes(const ModelInfo &info, int value, bool force)
|
2023-06-28 13:47:57 -04:00
|
|
|
{
|
2024-06-24 18:49:23 -04:00
|
|
|
setModelSetting("likes", info, value, force);
|
2023-06-28 13:47:57 -04:00
|
|
|
}
|
|
|
|
|
2024-06-24 18:49:23 -04:00
|
|
|
void MySettings::setModelDownloads(const ModelInfo &info, int value, bool force)
|
2024-02-24 17:51:34 -05:00
|
|
|
{
|
2024-06-24 18:49:23 -04:00
|
|
|
setModelSetting("downloads", info, value, force);
|
2024-02-24 17:51:34 -05:00
|
|
|
}
|
|
|
|
|
2024-06-24 18:49:23 -04:00
|
|
|
void MySettings::setModelRecency(const ModelInfo &info, const QDateTime &value, bool force)
|
2023-06-28 13:47:57 -04:00
|
|
|
{
|
2024-06-24 18:49:23 -04:00
|
|
|
setModelSetting("recency", info, value, force);
|
2023-06-28 13:47:57 -04:00
|
|
|
}
|
|
|
|
|
2024-06-24 18:49:23 -04:00
|
|
|
void MySettings::setModelTemperature(const ModelInfo &info, double value, bool force)
|
2023-06-28 13:47:57 -04:00
|
|
|
{
|
2024-06-24 18:49:23 -04:00
|
|
|
setModelSetting("temperature", info, value, force, true);
|
2023-06-28 13:47:57 -04:00
|
|
|
}
|
|
|
|
|
2024-06-24 18:49:23 -04:00
|
|
|
void MySettings::setModelTopP(const ModelInfo &info, double value, bool force)
|
2023-06-28 13:47:57 -04:00
|
|
|
{
|
2024-06-24 18:49:23 -04:00
|
|
|
setModelSetting("topP", info, value, force, true);
|
2023-06-28 13:47:57 -04:00
|
|
|
}
|
|
|
|
|
2024-06-24 18:49:23 -04:00
|
|
|
void MySettings::setModelMinP(const ModelInfo &info, double value, bool force)
|
2023-06-28 13:47:57 -04:00
|
|
|
{
|
2024-06-24 18:49:23 -04:00
|
|
|
setModelSetting("minP", info, value, force, true);
|
2023-06-28 13:47:57 -04:00
|
|
|
}
|
|
|
|
|
2024-06-24 18:49:23 -04:00
|
|
|
void MySettings::setModelTopK(const ModelInfo &info, int value, bool force)
|
2023-06-28 13:47:57 -04:00
|
|
|
{
|
2024-06-24 18:49:23 -04:00
|
|
|
setModelSetting("topK", info, value, force, true);
|
2023-06-28 13:47:57 -04:00
|
|
|
}
|
|
|
|
|
2024-06-24 18:49:23 -04:00
|
|
|
void MySettings::setModelMaxLength(const ModelInfo &info, int value, bool force)
|
2023-06-28 13:47:57 -04:00
|
|
|
{
|
2024-06-24 18:49:23 -04:00
|
|
|
setModelSetting("maxLength", info, value, force, true);
|
2023-06-28 13:47:57 -04:00
|
|
|
}
|
|
|
|
|
2024-06-24 18:49:23 -04:00
|
|
|
void MySettings::setModelPromptBatchSize(const ModelInfo &info, int value, bool force)
|
2023-12-16 17:58:15 -05:00
|
|
|
{
|
2024-06-24 18:49:23 -04:00
|
|
|
setModelSetting("promptBatchSize", info, value, force, true);
|
2023-12-16 17:58:15 -05:00
|
|
|
}
|
|
|
|
|
2024-06-24 18:49:23 -04:00
|
|
|
void MySettings::setModelContextLength(const ModelInfo &info, int value, bool force)
|
2023-12-16 17:58:15 -05:00
|
|
|
{
|
2024-06-24 18:49:23 -04:00
|
|
|
setModelSetting("contextLength", info, value, force, true);
|
2023-12-16 17:58:15 -05:00
|
|
|
}
|
|
|
|
|
2024-06-24 18:49:23 -04:00
|
|
|
void MySettings::setModelGpuLayers(const ModelInfo &info, int value, bool force)
|
2024-01-31 14:17:44 -05:00
|
|
|
{
|
2024-06-24 18:49:23 -04:00
|
|
|
setModelSetting("gpuLayers", info, value, force, true);
|
2024-01-31 14:17:44 -05:00
|
|
|
}
|
|
|
|
|
2024-06-24 18:49:23 -04:00
|
|
|
void MySettings::setModelRepeatPenalty(const ModelInfo &info, double value, bool force)
|
2024-01-31 14:17:44 -05:00
|
|
|
{
|
2024-06-24 18:49:23 -04:00
|
|
|
setModelSetting("repeatPenalty", info, value, force, true);
|
2024-01-31 14:17:44 -05:00
|
|
|
}
|
|
|
|
|
2024-06-24 18:49:23 -04:00
|
|
|
void MySettings::setModelRepeatPenaltyTokens(const ModelInfo &info, int value, bool force)
|
2023-06-28 13:47:57 -04:00
|
|
|
{
|
2024-06-24 18:49:23 -04:00
|
|
|
setModelSetting("repeatPenaltyTokens", info, value, force, true);
|
2023-06-28 13:47:57 -04:00
|
|
|
}
|
|
|
|
|
2024-06-24 18:49:23 -04:00
|
|
|
void MySettings::setModelPromptTemplate(const ModelInfo &info, const QString &value, bool force)
|
2023-06-28 13:47:57 -04:00
|
|
|
{
|
2024-06-24 18:49:23 -04:00
|
|
|
setModelSetting("promptTemplate", info, value, force, true);
|
2023-06-28 13:47:57 -04:00
|
|
|
}
|
|
|
|
|
2024-06-24 18:49:23 -04:00
|
|
|
void MySettings::setModelSystemPrompt(const ModelInfo &info, const QString &value, bool force)
|
2023-06-28 13:47:57 -04:00
|
|
|
{
|
2024-06-24 18:49:23 -04:00
|
|
|
setModelSetting("systemPrompt", info, value, force, true);
|
2023-06-28 13:47:57 -04:00
|
|
|
}
|
|
|
|
|
2024-07-10 15:45:20 -04:00
|
|
|
void MySettings::setModelChatNamePrompt(const ModelInfo &info, const QString &value, bool force)
|
|
|
|
{
|
|
|
|
setModelSetting("chatNamePrompt", info, value, force, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MySettings::setModelSuggestedFollowUpPrompt(const ModelInfo &info, const QString &value, bool force)
|
|
|
|
{
|
|
|
|
setModelSetting("suggestedFollowUpPrompt", info, value, force, true);
|
|
|
|
}
|
|
|
|
|
2023-06-28 13:47:57 -04:00
|
|
|
int MySettings::threadCount() const
|
|
|
|
{
|
2024-06-24 18:49:23 -04:00
|
|
|
int c = m_settings.value("threadCount", defaults::threadCount).toInt();
|
2023-07-11 13:07:15 -04:00
|
|
|
// The old thread setting likely left many people with 0 in settings config file, which means
|
|
|
|
// we should reset it to the default going forward
|
|
|
|
if (c <= 0)
|
2024-06-24 18:49:23 -04:00
|
|
|
c = defaults::threadCount;
|
2023-07-10 16:52:32 -04:00
|
|
|
c = std::max(c, 1);
|
|
|
|
c = std::min(c, QThread::idealThreadCount());
|
|
|
|
return c;
|
2023-06-28 13:47:57 -04:00
|
|
|
}
|
|
|
|
|
2024-06-24 18:49:23 -04:00
|
|
|
void MySettings::setThreadCount(int value)
|
2023-06-28 13:47:57 -04:00
|
|
|
{
|
2024-06-24 18:49:23 -04:00
|
|
|
if (threadCount() == value)
|
2023-06-28 13:47:57 -04:00
|
|
|
return;
|
|
|
|
|
2024-06-24 18:49:23 -04:00
|
|
|
value = std::max(value, 1);
|
|
|
|
value = std::min(value, QThread::idealThreadCount());
|
|
|
|
m_settings.setValue("threadCount", value);
|
2023-06-28 13:47:57 -04:00
|
|
|
emit threadCountChanged();
|
|
|
|
}
|
|
|
|
|
2024-07-16 16:12:44 -04:00
|
|
|
bool MySettings::saveChatsContext() const { return getBasicSetting("saveChatsContext" ).toBool(); }
|
|
|
|
bool MySettings::serverChat() const { return getBasicSetting("serverChat" ).toBool(); }
|
|
|
|
int MySettings::networkPort() const { return getBasicSetting("networkPort" ).toInt(); }
|
|
|
|
QString MySettings::userDefaultModel() const { return getBasicSetting("userDefaultModel" ).toString(); }
|
|
|
|
QString MySettings::lastVersionStarted() const { return getBasicSetting("lastVersionStarted" ).toString(); }
|
|
|
|
int MySettings::localDocsChunkSize() const { return getBasicSetting("localdocs/chunkSize" ).toInt(); }
|
|
|
|
int MySettings::localDocsRetrievalSize() const { return getBasicSetting("localdocs/retrievalSize" ).toInt(); }
|
|
|
|
bool MySettings::localDocsShowReferences() const { return getBasicSetting("localdocs/showReferences").toBool(); }
|
|
|
|
QStringList MySettings::localDocsFileExtensions() const { return getBasicSetting("localdocs/fileExtensions").toStringList(); }
|
|
|
|
bool MySettings::localDocsUseRemoteEmbed() const { return getBasicSetting("localdocs/useRemoteEmbed").toBool(); }
|
|
|
|
QString MySettings::localDocsNomicAPIKey() const { return getBasicSetting("localdocs/nomicAPIKey" ).toString(); }
|
|
|
|
QString MySettings::localDocsEmbedDevice() const { return getBasicSetting("localdocs/embedDevice" ).toString(); }
|
|
|
|
QString MySettings::networkAttribution() const { return getBasicSetting("network/attribution" ).toString(); }
|
|
|
|
|
|
|
|
ChatTheme MySettings::chatTheme() const { return ChatTheme (getEnumSetting("chatTheme", chatThemeNames)); }
|
|
|
|
FontSize MySettings::fontSize() const { return FontSize (getEnumSetting("fontSize", fontSizeNames)); }
|
|
|
|
SuggestionMode MySettings::suggestionMode() const { return SuggestionMode(getEnumSetting("suggestionMode", suggestionModeNames)); }
|
2024-06-24 18:49:23 -04:00
|
|
|
|
|
|
|
void MySettings::setSaveChatsContext(bool value) { setBasicSetting("saveChatsContext", value); }
|
|
|
|
void MySettings::setServerChat(bool value) { setBasicSetting("serverChat", value); }
|
|
|
|
void MySettings::setNetworkPort(int value) { setBasicSetting("networkPort", value); }
|
|
|
|
void MySettings::setUserDefaultModel(const QString &value) { setBasicSetting("userDefaultModel", value); }
|
|
|
|
void MySettings::setLastVersionStarted(const QString &value) { setBasicSetting("lastVersionStarted", value); }
|
|
|
|
void MySettings::setLocalDocsChunkSize(int value) { setBasicSetting("localdocs/chunkSize", value, "localDocsChunkSize"); }
|
|
|
|
void MySettings::setLocalDocsRetrievalSize(int value) { setBasicSetting("localdocs/retrievalSize", value, "localDocsRetrievalSize"); }
|
|
|
|
void MySettings::setLocalDocsShowReferences(bool value) { setBasicSetting("localdocs/showReferences", value, "localDocsShowReferences"); }
|
|
|
|
void MySettings::setLocalDocsFileExtensions(const QStringList &value) { setBasicSetting("localdocs/fileExtensions", value, "localDocsFileExtensions"); }
|
|
|
|
void MySettings::setLocalDocsUseRemoteEmbed(bool value) { setBasicSetting("localdocs/useRemoteEmbed", value, "localDocsUseRemoteEmbed"); }
|
|
|
|
void MySettings::setLocalDocsNomicAPIKey(const QString &value) { setBasicSetting("localdocs/nomicAPIKey", value, "localDocsNomicAPIKey"); }
|
2024-06-28 12:57:57 -04:00
|
|
|
void MySettings::setLocalDocsEmbedDevice(const QString &value) { setBasicSetting("localdocs/embedDevice", value, "localDocsEmbedDevice"); }
|
2024-06-24 18:49:23 -04:00
|
|
|
void MySettings::setNetworkAttribution(const QString &value) { setBasicSetting("network/attribution", value, "networkAttribution"); }
|
2024-07-16 16:12:44 -04:00
|
|
|
|
|
|
|
void MySettings::setChatTheme(ChatTheme value) { setBasicSetting("chatTheme", chatThemeNames .value(int(value))); }
|
|
|
|
void MySettings::setFontSize(FontSize value) { setBasicSetting("fontSize", fontSizeNames .value(int(value))); }
|
|
|
|
void MySettings::setSuggestionMode(SuggestionMode value) { setBasicSetting("suggestionMode", suggestionModeNames.value(int(value))); }
|
2024-06-24 18:49:23 -04:00
|
|
|
|
|
|
|
QString MySettings::modelPath()
|
2023-06-28 13:47:57 -04:00
|
|
|
{
|
2023-07-09 11:50:44 -04:00
|
|
|
// We have to migrate the old setting because I changed the setting key recklessly in v2.4.11
|
|
|
|
// which broke a lot of existing installs
|
2024-06-24 18:49:23 -04:00
|
|
|
const bool containsOldSetting = m_settings.contains("modelPaths");
|
2023-07-09 11:50:44 -04:00
|
|
|
if (containsOldSetting) {
|
2024-06-24 18:49:23 -04:00
|
|
|
const bool containsNewSetting = m_settings.contains("modelPath");
|
2023-07-09 11:50:44 -04:00
|
|
|
if (!containsNewSetting)
|
2024-06-24 18:49:23 -04:00
|
|
|
m_settings.setValue("modelPath", m_settings.value("modelPaths"));
|
|
|
|
m_settings.remove("modelPaths");
|
2023-07-09 11:50:44 -04:00
|
|
|
}
|
2024-06-24 18:49:23 -04:00
|
|
|
return m_settings.value("modelPath", defaultLocalModelsPath()).toString();
|
2023-06-28 13:47:57 -04:00
|
|
|
}
|
|
|
|
|
2024-06-24 18:49:23 -04:00
|
|
|
void MySettings::setModelPath(const QString &value)
|
2023-06-28 13:47:57 -04:00
|
|
|
{
|
2024-06-24 18:49:23 -04:00
|
|
|
QString filePath = (value.startsWith("file://") ?
|
|
|
|
QUrl(value).toLocalFile() : value);
|
2023-06-28 16:05:35 -04:00
|
|
|
QString canonical = QFileInfo(filePath).canonicalFilePath() + "/";
|
|
|
|
if (modelPath() == canonical)
|
2023-06-28 13:47:57 -04:00
|
|
|
return;
|
2024-06-24 18:49:23 -04:00
|
|
|
m_settings.setValue("modelPath", canonical);
|
2023-06-28 13:47:57 -04:00
|
|
|
emit modelPathChanged();
|
|
|
|
}
|
|
|
|
|
2024-06-24 18:49:23 -04:00
|
|
|
QString MySettings::device()
|
2023-06-28 13:47:57 -04:00
|
|
|
{
|
2024-06-24 18:49:23 -04:00
|
|
|
auto value = m_settings.value("device");
|
2024-05-15 15:27:50 -04:00
|
|
|
if (!value.isValid())
|
2024-06-24 18:49:23 -04:00
|
|
|
return defaults::device;
|
2024-05-15 15:27:50 -04:00
|
|
|
|
|
|
|
auto device = value.toString();
|
|
|
|
if (!device.isEmpty()) {
|
|
|
|
auto deviceStr = device.toStdString();
|
|
|
|
auto newNameStr = LLModel::GPUDevice::updateSelectionName(deviceStr);
|
|
|
|
if (newNameStr != deviceStr) {
|
|
|
|
auto newName = QString::fromStdString(newNameStr);
|
|
|
|
qWarning() << "updating device name:" << device << "->" << newName;
|
|
|
|
device = newName;
|
2024-06-24 18:49:23 -04:00
|
|
|
m_settings.setValue("device", device);
|
2024-05-15 15:27:50 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return device;
|
2023-09-13 10:32:08 -04:00
|
|
|
}
|
|
|
|
|
2024-06-24 18:49:23 -04:00
|
|
|
void MySettings::setDevice(const QString &value)
|
2023-09-13 10:32:08 -04:00
|
|
|
{
|
2024-06-28 12:57:57 -04:00
|
|
|
if (device() != value) {
|
|
|
|
m_settings.setValue("device", value);
|
|
|
|
emit deviceChanged();
|
|
|
|
}
|
2023-09-13 10:32:08 -04:00
|
|
|
}
|
|
|
|
|
2023-06-27 11:54:34 -04:00
|
|
|
bool MySettings::forceMetal() const
|
|
|
|
{
|
|
|
|
return m_forceMetal;
|
|
|
|
}
|
|
|
|
|
2024-06-24 18:49:23 -04:00
|
|
|
void MySettings::setForceMetal(bool value)
|
2023-06-28 20:42:40 -04:00
|
|
|
{
|
2024-06-28 12:57:57 -04:00
|
|
|
if (m_forceMetal != value) {
|
|
|
|
m_forceMetal = value;
|
|
|
|
emit forceMetalChanged(value);
|
|
|
|
}
|
2023-06-28 20:42:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool MySettings::networkIsActive() const
|
|
|
|
{
|
2024-06-24 18:49:23 -04:00
|
|
|
return m_settings.value("network/isActive", defaults::networkIsActive).toBool();
|
2023-06-28 20:42:40 -04:00
|
|
|
}
|
|
|
|
|
2024-04-25 13:16:52 -04:00
|
|
|
bool MySettings::isNetworkIsActiveSet() const
|
2023-06-28 20:42:40 -04:00
|
|
|
{
|
2024-06-24 18:49:23 -04:00
|
|
|
return m_settings.value("network/isActive").isValid();
|
2024-04-25 13:16:52 -04:00
|
|
|
}
|
2023-06-28 20:42:40 -04:00
|
|
|
|
2024-06-24 18:49:23 -04:00
|
|
|
void MySettings::setNetworkIsActive(bool value)
|
2024-04-25 13:16:52 -04:00
|
|
|
{
|
2024-06-24 18:49:23 -04:00
|
|
|
auto cur = m_settings.value("network/isActive");
|
|
|
|
if (!cur.isValid() || cur.toBool() != value) {
|
|
|
|
m_settings.setValue("network/isActive", value);
|
2024-04-25 13:16:52 -04:00
|
|
|
emit networkIsActiveChanged();
|
|
|
|
}
|
2023-06-28 20:42:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool MySettings::networkUsageStatsActive() const
|
|
|
|
{
|
2024-06-24 18:49:23 -04:00
|
|
|
return m_settings.value("network/usageStatsActive", defaults::networkUsageStatsActive).toBool();
|
2023-06-28 20:42:40 -04:00
|
|
|
}
|
|
|
|
|
2024-04-25 13:16:52 -04:00
|
|
|
bool MySettings::isNetworkUsageStatsActiveSet() const
|
2023-06-28 20:42:40 -04:00
|
|
|
{
|
2024-06-24 18:49:23 -04:00
|
|
|
return m_settings.value("network/usageStatsActive").isValid();
|
2024-04-25 13:16:52 -04:00
|
|
|
}
|
2023-06-28 20:42:40 -04:00
|
|
|
|
2024-06-24 18:49:23 -04:00
|
|
|
void MySettings::setNetworkUsageStatsActive(bool value)
|
2024-04-25 13:16:52 -04:00
|
|
|
{
|
2024-06-24 18:49:23 -04:00
|
|
|
auto cur = m_settings.value("network/usageStatsActive");
|
|
|
|
if (!cur.isValid() || cur.toBool() != value) {
|
|
|
|
m_settings.setValue("network/usageStatsActive", value);
|
2024-04-25 13:16:52 -04:00
|
|
|
emit networkUsageStatsActiveChanged();
|
|
|
|
}
|
2023-06-28 20:42:40 -04:00
|
|
|
}
|
2024-07-12 16:14:58 -04:00
|
|
|
|
|
|
|
QString MySettings::languageAndLocale() const
|
|
|
|
{
|
|
|
|
auto value = m_settings.value("languageAndLocale");
|
|
|
|
if (!value.isValid())
|
|
|
|
return defaults::languageAndLocale;
|
|
|
|
return value.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString MySettings::filePathForLocale(const QLocale &locale)
|
|
|
|
{
|
|
|
|
// Check and see if we have a translation for the chosen locale and set it if possible otherwise
|
2024-08-08 13:41:47 -04:00
|
|
|
// we return the filepath for the 'en_US' translation
|
2024-07-12 17:18:01 -04:00
|
|
|
QStringList uiLanguages = locale.uiLanguages();
|
|
|
|
for (int i = 0; i < uiLanguages.size(); ++i)
|
|
|
|
uiLanguages[i].replace('-', '_');
|
2024-07-12 16:14:58 -04:00
|
|
|
|
|
|
|
// Scan this directory for files named like gpt4all_%1.qm that match and if so return them first
|
|
|
|
// this is the model download directory and it can be used by translation developers who are
|
|
|
|
// trying to test their translations by just compiling the translation with the lrelease tool
|
|
|
|
// rather than having to recompile all of GPT4All
|
|
|
|
QString directory = modelPath();
|
|
|
|
for (const QString &bcp47Name : uiLanguages) {
|
2024-08-08 13:41:47 -04:00
|
|
|
QString filePath = u"%1/gpt4all_%2.qm"_s.arg(directory, bcp47Name);
|
2024-07-12 16:14:58 -04:00
|
|
|
QFileInfo filePathInfo(filePath);
|
|
|
|
if (filePathInfo.exists()) return filePath;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now scan the internal built-in translations
|
|
|
|
for (QString bcp47Name : uiLanguages) {
|
2024-08-08 13:41:47 -04:00
|
|
|
QString filePath = u":/i18n/gpt4all_%1.qm"_s.arg(bcp47Name);
|
2024-07-12 16:14:58 -04:00
|
|
|
QFileInfo filePathInfo(filePath);
|
|
|
|
if (filePathInfo.exists()) return filePath;
|
|
|
|
}
|
2024-08-08 13:41:47 -04:00
|
|
|
return u":/i18n/gpt4all_en_US.qm"_s;
|
2024-07-12 16:14:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void MySettings::setLanguageAndLocale(const QString &bcp47Name)
|
|
|
|
{
|
|
|
|
if (!bcp47Name.isEmpty() && languageAndLocale() != bcp47Name)
|
|
|
|
m_settings.setValue("languageAndLocale", bcp47Name);
|
|
|
|
|
|
|
|
// When the app is started this method is called with no bcp47Name given which sets the translation
|
|
|
|
// to either the default which is the system locale or the one explicitly set by the user previously.
|
|
|
|
QLocale locale;
|
|
|
|
const QString l = languageAndLocale();
|
2024-07-19 14:28:54 -04:00
|
|
|
if (l == "System Locale")
|
2024-07-12 16:14:58 -04:00
|
|
|
locale = QLocale::system();
|
|
|
|
else
|
|
|
|
locale = QLocale(l);
|
|
|
|
|
2024-08-08 18:44:15 -04:00
|
|
|
#ifdef GPT4ALL_USE_TRANSLATIONS
|
2024-07-12 16:14:58 -04:00
|
|
|
// If we previously installed a translator, then remove it
|
|
|
|
if (m_translator) {
|
2024-08-08 13:41:47 -04:00
|
|
|
if (!qGuiApp->removeTranslator(m_translator.get())) {
|
2024-07-12 16:14:58 -04:00
|
|
|
qDebug() << "ERROR: Failed to remove the previous translator";
|
|
|
|
} else {
|
2024-08-08 13:41:47 -04:00
|
|
|
m_translator.reset();
|
2024-07-12 16:14:58 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We expect that the translator was removed and is now a nullptr
|
|
|
|
Q_ASSERT(!m_translator);
|
|
|
|
|
|
|
|
const QString filePath = filePathForLocale(locale);
|
2024-08-08 13:41:47 -04:00
|
|
|
if (!m_translator) {
|
2024-07-12 16:14:58 -04:00
|
|
|
// Create a new translator object on the heap
|
2024-08-08 13:41:47 -04:00
|
|
|
m_translator = std::make_unique<QTranslator>(this);
|
2024-07-12 16:14:58 -04:00
|
|
|
bool success = m_translator->load(filePath);
|
|
|
|
Q_ASSERT(success);
|
|
|
|
if (!success) {
|
|
|
|
qDebug() << "ERROR: Failed to load translation file:" << filePath;
|
2024-08-08 13:41:47 -04:00
|
|
|
m_translator.reset();
|
2024-07-12 16:14:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we've successfully loaded it, then try and install it
|
2024-08-08 13:41:47 -04:00
|
|
|
if (!qGuiApp->installTranslator(m_translator.get())) {
|
2024-07-12 16:14:58 -04:00
|
|
|
qDebug() << "ERROR: Failed to install the translator:" << filePath;
|
2024-08-08 13:41:47 -04:00
|
|
|
m_translator.reset();
|
2024-07-12 16:14:58 -04:00
|
|
|
}
|
|
|
|
}
|
2024-08-08 18:44:15 -04:00
|
|
|
#endif
|
2024-07-12 16:14:58 -04:00
|
|
|
|
|
|
|
// Finally, set the locale whether we have a translation or not
|
|
|
|
QLocale::setDefault(locale);
|
|
|
|
emit languageAndLocaleChanged();
|
|
|
|
}
|