aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/qbsprojectmanager/qbssettings.cpp
blob: 38dcb7f7a9a59188adaa4745fa6320fc9b933036 (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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
// Copyright (C) 2019 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "qbssettings.h"

#include "qbsprojectmanagerconstants.h"
#include "qbsprojectmanagertr.h"

#include <coreplugin/icore.h>
#include <projectexplorer/devicesupport/devicekitaspects.h>
#include <projectexplorer/devicesupport/devicemanager.h>
#include <projectexplorer/devicesupport/idevice.h>
#include <projectexplorer/projectexplorerconstants.h>
#include <utils/environment.h>
#include <utils/hostosinfo.h>
#include <utils/pathchooser.h>
#include <utils/qtcprocess.h>
#include <utils/qtcsettings.h>

#include <QCheckBox>
#include <QFormLayout>
#include <QGuiApplication>
#include <QLabel>
#include <QPushButton>

using namespace ProjectExplorer;
using namespace Utils;

namespace QbsProjectManager::Internal {

const char QBS_EXE_KEY[] = "QbsProjectManager/QbsExecutable";
const char QBS_DEFAULT_INSTALL_DIR_KEY[] = "QbsProjectManager/DefaultInstallDir";
const char USE_CREATOR_SETTINGS_KEY[] = "QbsProjectManager/useCreatorDir";

static Environment getQbsProcessEnvironment(const FilePath &qbsExe)
{
    if (qbsExe == QbsSettings::defaultQbsExecutableFilePath())
        return Environment::originalSystemEnvironment();
    return qbsExe.deviceEnvironment();
}

static QString getQbsVersion(const FilePath &qbsExe)
{
    if (qbsExe.isEmpty() || !qbsExe.exists())
        return {};
    Process qbsProc;
    qbsProc.setCommand({qbsExe, {"--version"}});
    qbsProc.setEnvironment(getQbsProcessEnvironment(qbsExe));
    qbsProc.start();
    using namespace std::chrono_literals;
    if (!qbsProc.waitForFinished(5s) || qbsProc.exitCode() != 0)
        return {};
    return QString::fromLocal8Bit(qbsProc.rawStdOut()).trimmed();
}

static bool operator==(const QbsSettingsData &s1, const QbsSettingsData &s2)
{
    return s1.qbsExecutableFilePath == s2.qbsExecutableFilePath
            && s1.defaultInstallDirTemplate == s2.defaultInstallDirTemplate
            && s1.useCreatorSettings == s2.useCreatorSettings;
}
static bool operator!=(const QbsSettingsData &s1, const QbsSettingsData &s2)
{
    return !(s1 == s2);
}

FilePath QbsSettings::qbsExecutableFilePath(const Kit &kit)
{
    return qbsExecutableFilePath(BuildDeviceKitAspect::device(&kit));
}

FilePath QbsSettings::qbsExecutableFilePath(const IDeviceConstPtr &device)
{
    if (!device)
        return {};
    if (device->id() == ProjectExplorer::Constants::DESKTOP_DEVICE_ID) {
        FilePath candidate = instance().m_settings.qbsExecutableFilePath;
        if (!candidate.exists())
            candidate = defaultQbsExecutableFilePath();
        return candidate;
    }
    return device->searchExecutableInPath("qbs");
}

FilePath QbsSettings::defaultQbsExecutableFilePath()
{
    const QString fileName = HostOsInfo::withExecutableSuffix("qbs");
    FilePath candidate = FilePath::fromString(QCoreApplication::applicationDirPath())
                             .pathAppended(fileName);
    if (!candidate.exists())
        candidate = Environment::systemEnvironment().searchInPath(fileName);
    return candidate;
}

FilePath QbsSettings::qbsConfigFilePath(const IDeviceConstPtr &device)
{
    const FilePath qbsExe = qbsExecutableFilePath(device);
    if (!qbsExe.isExecutableFile())
        return {};
    const FilePath qbsConfig = qbsExe.absolutePath().pathAppended("qbs-config")
            .withExecutableSuffix();
    if (!qbsConfig.isExecutableFile())
        return {};
    return qbsConfig;
}

Environment QbsSettings::qbsProcessEnvironment(const IDeviceConstPtr &device)
{
    return getQbsProcessEnvironment(qbsExecutableFilePath(device));
}

QString QbsSettings::defaultInstallDirTemplate()
{
    return instance().m_settings.defaultInstallDirTemplate;
}

bool QbsSettings::useCreatorSettingsDirForQbs(const IDeviceConstPtr &device)
{
    if (!device || device->id() != ProjectExplorer::Constants::DESKTOP_DEVICE_ID)
        return false;
    return instance().m_settings.useCreatorSettings;
}

FilePath QbsSettings::qbsSettingsBaseDir(const IDeviceConstPtr &device)
{
    return useCreatorSettingsDirForQbs(device) ? Core::ICore::userResourcePath() : FilePath();
}

QVersionNumber QbsSettings::qbsVersion(const IDeviceConstPtr &device)
{
    return QVersionNumber::fromString(getQbsVersion(qbsExecutableFilePath(device)));
}

QbsSettings &QbsSettings::instance()
{
    static QbsSettings theSettings;
    return theSettings;
}

void QbsSettings::setSettingsData(const QbsSettingsData &settings)
{
    if (instance().m_settings != settings) {
        instance().m_settings = settings;
        instance().storeSettings();
        emit instance().settingsChanged();
    }
}

QbsSettingsData QbsSettings::rawSettingsData()
{
    return instance().m_settings;
}

QbsSettings::QbsSettings()
{
    loadSettings();
}

void QbsSettings::loadSettings()
{
    QtcSettings * const s = Core::ICore::settings();
    m_settings.qbsExecutableFilePath = FilePath::fromString(s->value(QBS_EXE_KEY).toString());
    m_settings.defaultInstallDirTemplate = s->value(
                QBS_DEFAULT_INSTALL_DIR_KEY,
                "%{CurrentBuild:QbsBuildRoot}/install-root").toString();
    m_settings.useCreatorSettings = s->value(USE_CREATOR_SETTINGS_KEY, true).toBool();
}

void QbsSettings::storeSettings() const
{
    QtcSettings * const s = Core::ICore::settings();
    s->setValueWithDefault(QBS_EXE_KEY, m_settings.qbsExecutableFilePath.toUrlishString(),
                           defaultQbsExecutableFilePath().toUrlishString());
    s->setValue(QBS_DEFAULT_INSTALL_DIR_KEY, m_settings.defaultInstallDirTemplate);
    s->setValue(USE_CREATOR_SETTINGS_KEY, m_settings.useCreatorSettings);
}

class QbsSettingsPageWidget : public Core::IOptionsPageWidget
{
public:
    QbsSettingsPageWidget()
    {
        m_qbsExePathChooser.setExpectedKind(PathChooser::ExistingCommand);
        const IDeviceConstPtr desktopDevice = DeviceManager::defaultDesktopDevice();
        m_qbsExePathChooser.setFilePath(QbsSettings::qbsExecutableFilePath(desktopDevice));
        m_resetQbsExeButton.setText(Tr::tr("Reset"));
        m_defaultInstallDirLineEdit.setText(QbsSettings::defaultInstallDirTemplate());
        m_versionLabel.setText(getQbsVersionString());
        //: %1 == "Qt Creator" or "Qt Design Studio"
        m_settingsDirCheckBox.setText(Tr::tr("Use %1 settings directory for Qbs")
                                          .arg(QGuiApplication::applicationDisplayName()));
        m_settingsDirCheckBox.setChecked(QbsSettings::useCreatorSettingsDirForQbs(desktopDevice));

        const auto layout = new QFormLayout(this);
        layout->addRow(&m_settingsDirCheckBox);
        const auto qbsExeLayout = new QHBoxLayout;
        qbsExeLayout->addWidget(&m_qbsExePathChooser);
        qbsExeLayout->addWidget(&m_resetQbsExeButton);
        layout->addRow(Tr::tr("Path to qbs executable:"), qbsExeLayout);
        layout->addRow(Tr::tr("Default installation directory:"), &m_defaultInstallDirLineEdit);
        layout->addRow(Tr::tr("Qbs version:"), &m_versionLabel);

        connect(&m_qbsExePathChooser, &PathChooser::textChanged, this, [this] {
            m_versionLabel.setText(getQbsVersionString());
        });
        connect(&m_resetQbsExeButton, &QPushButton::clicked, this, [this] {
            m_qbsExePathChooser.setFilePath(QbsSettings::defaultQbsExecutableFilePath());
        });
    }

    void apply() final
    {
        QbsSettingsData settings = QbsSettings::rawSettingsData();
        if (m_qbsExePathChooser.filePath()
                != QbsSettings::qbsExecutableFilePath(DeviceManager::defaultDesktopDevice())) {
            settings.qbsExecutableFilePath = m_qbsExePathChooser.filePath();
        }
        settings.defaultInstallDirTemplate = m_defaultInstallDirLineEdit.text();
        settings.useCreatorSettings = m_settingsDirCheckBox.isChecked();
        QbsSettings::setSettingsData(settings);
    }

private:
    QString getQbsVersionString()
    {
        const QString version = getQbsVersion(m_qbsExePathChooser.filePath());
        return version.isEmpty() ? Tr::tr("Failed to retrieve version.") : version;
    }

    PathChooser m_qbsExePathChooser;
    QPushButton m_resetQbsExeButton;
    QLabel m_versionLabel;
    QCheckBox m_settingsDirCheckBox;
    FancyLineEdit m_defaultInstallDirLineEdit;
};

QbsSettingsPage::QbsSettingsPage()
{
    setId("A.QbsProjectManager.QbsSettings");
    setDisplayName(Tr::tr("General"));
    setCategory(Constants::QBS_SETTINGS_CATEGORY);
    setWidgetCreator([] { return new QbsSettingsPageWidget; });
}

} // QbsProjectManager::Internal