aboutsummaryrefslogtreecommitdiffstats
path: root/tools/shared/qqmltoolingsettings.cpp
blob: fa4cd01355a8df2de1dc105cee0398975047f1ae (plain)
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/****************************************************************************
**
** Copyright (C) 2022 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the tools applications of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:COMM$
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** $QT_END_LICENSE$
**
**
**
**
**
**
**
**
******************************************************************************/

#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>

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"_qs.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"_qs.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 : qAsConst(dirs))
                m_seenDirectories[dir] = iniFile;
            return true;
        }

        if (!dir.cdUp())
            break;
    }

    if (const QString iniFile = QStandardPaths::locate(QStandardPaths::GenericConfigLocation, u"%1.ini"_qs.arg(m_toolName)); !iniFile.isEmpty()) {
        if (read(iniFile)) {
            for (const QString &dir : qAsConst(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 : qAsConst(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());
}