1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
|
// Copyright (C) 2021 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include "qqmltoolingsettings.h"
#include <algorithm>
#include <QtCore/qdebug.h>
#include <QtCore/qdir.h>
#include <QtCore/qfileinfo.h>
#include <QtCore/qset.h>
#include <QtCore/qsettings.h>
#include <QtCore/qstandardpaths.h>
using namespace Qt::StringLiterals;
void QQmlToolingSettings::addOption(const QString &name, QVariant defaultValue)
{
m_values[name] = defaultValue;
}
bool QQmlToolingSettings::read(const QString &settingsFilePath)
{
if (!QFileInfo::exists(settingsFilePath))
return false;
if (m_currentSettingsPath == settingsFilePath)
return true;
QSettings settings(settingsFilePath, QSettings::IniFormat);
for (const QString &key : settings.allKeys())
m_values[key] = settings.value(key).toString();
m_currentSettingsPath = settingsFilePath;
return true;
}
bool QQmlToolingSettings::writeDefaults() const
{
const QString path = QFileInfo(u".%1.ini"_s.arg(m_toolName)).absoluteFilePath();
QSettings settings(path, QSettings::IniFormat);
for (auto it = m_values.constBegin(); it != m_values.constEnd(); ++it) {
settings.setValue(it.key(), it.value().isNull() ? QString() : it.value());
}
settings.sync();
if (settings.status() != QSettings::NoError) {
qWarning() << "Failed to write default settings to" << path
<< "Error:" << settings.status();
return false;
}
qInfo() << "Wrote default settings to" << path;
return true;
}
bool QQmlToolingSettings::search(const QString &path)
{
QFileInfo fileInfo(path);
QDir dir(fileInfo.isDir() ? path : fileInfo.dir());
QSet<QString> dirs;
const QString settingsFileName = u".%1.ini"_s.arg(m_toolName);
while (dir.exists() && dir.isReadable()) {
const QString dirPath = dir.absolutePath();
if (m_seenDirectories.contains(dirPath)) {
const QString cachedIniPath = m_seenDirectories[dirPath];
if (cachedIniPath.isEmpty())
return false;
return read(cachedIniPath);
}
dirs << dirPath;
const QString iniFile = dir.absoluteFilePath(settingsFileName);
if (read(iniFile)) {
for (const QString &dir : std::as_const(dirs))
m_seenDirectories[dir] = iniFile;
return true;
}
if (!dir.cdUp())
break;
}
if (const QString iniFile = QStandardPaths::locate(QStandardPaths::GenericConfigLocation, u"%1.ini"_s.arg(m_toolName)); !iniFile.isEmpty()) {
if (read(iniFile)) {
for (const QString &dir : std::as_const(dirs))
m_seenDirectories[dir] = iniFile;
return true;
}
}
// No INI file found anywhere, record the failure so we won't have to traverse the entire
// filesystem again
for (const QString &dir : std::as_const(dirs))
m_seenDirectories[dir] = QString();
return false;
}
QVariant QQmlToolingSettings::value(QString name) const
{
return m_values.value(name);
}
bool QQmlToolingSettings::isSet(QString name) const
{
if (!m_values.contains(name))
return false;
QVariant variant = m_values[name];
// Unset is encoded as an empty string
return !(variant.canConvert(QMetaType(QMetaType::QString)) && variant.toString().isEmpty());
}
|