2022-12-12 16:45:31 +01:00
|
|
|
// Copyright (C) 2022 The Qt Company Ltd.
|
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial
|
2022-11-28 09:48:11 +01:00
|
|
|
|
|
|
|
|
#include "axivionsettings.h"
|
|
|
|
|
|
2022-12-12 16:45:31 +01:00
|
|
|
#include <utils/hostosinfo.h>
|
|
|
|
|
|
|
|
|
|
#include <QJsonDocument>
|
|
|
|
|
#include <QJsonObject>
|
2022-11-28 09:48:11 +01:00
|
|
|
#include <QSettings>
|
2022-12-12 16:45:31 +01:00
|
|
|
#include <QStandardPaths>
|
2022-11-28 09:48:11 +01:00
|
|
|
|
|
|
|
|
namespace Axivion::Internal {
|
|
|
|
|
|
2022-12-12 16:45:31 +01:00
|
|
|
const char curlKeyC[] = "Curl";
|
|
|
|
|
|
|
|
|
|
AxivionServer::AxivionServer(const Utils::Id &id, const QString &dashboard,
|
|
|
|
|
const QString &description, const QString &token)
|
|
|
|
|
: id(id)
|
|
|
|
|
, dashboard(dashboard)
|
|
|
|
|
, description(description)
|
|
|
|
|
, token(token)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool AxivionServer::operator==(const AxivionServer &other) const
|
|
|
|
|
{
|
|
|
|
|
return id == other.id && dashboard == other.dashboard
|
|
|
|
|
&& description == other.description && token == other.token;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool AxivionServer::operator!=(const AxivionServer &other) const
|
|
|
|
|
{
|
|
|
|
|
return !(*this == other);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QJsonObject AxivionServer::toJson() const
|
|
|
|
|
{
|
|
|
|
|
QJsonObject result;
|
|
|
|
|
result.insert("id", id.toString());
|
|
|
|
|
result.insert("dashboard", dashboard);
|
|
|
|
|
result.insert("description", description);
|
|
|
|
|
result.insert("token", token);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AxivionServer AxivionServer::fromJson(const QJsonObject &json)
|
|
|
|
|
{
|
|
|
|
|
const AxivionServer invalidServer;
|
|
|
|
|
const QJsonValue id = json.value("id");
|
|
|
|
|
if (id == QJsonValue::Undefined)
|
|
|
|
|
return invalidServer;
|
|
|
|
|
const QJsonValue dashboard = json.value("dashboard");
|
|
|
|
|
if (dashboard == QJsonValue::Undefined)
|
|
|
|
|
return invalidServer;
|
|
|
|
|
const QJsonValue description = json.value("description");
|
|
|
|
|
if (description == QJsonValue::Undefined)
|
|
|
|
|
return invalidServer;
|
|
|
|
|
const QJsonValue token = json.value("token");
|
|
|
|
|
if (token == QJsonValue::Undefined)
|
|
|
|
|
return invalidServer;
|
|
|
|
|
return { Utils::Id::fromString(id.toString()), dashboard.toString(),
|
|
|
|
|
description.toString(), token.toString() };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QStringList AxivionServer::curlArguments() const
|
|
|
|
|
{
|
|
|
|
|
QStringList args { "-sS" }; // silent, but show error
|
|
|
|
|
if (dashboard.startsWith("https://") && !validateCert)
|
|
|
|
|
args << "-k";
|
|
|
|
|
return args;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-28 09:48:11 +01:00
|
|
|
AxivionSettings::AxivionSettings()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-12 16:45:31 +01:00
|
|
|
static Utils::FilePath tokensFilePath(const QSettings *s)
|
|
|
|
|
{
|
|
|
|
|
return Utils::FilePath::fromString(s->fileName()).parentDir()
|
|
|
|
|
.pathAppended("qtcreator/axivion.json");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void writeTokenFile(const Utils::FilePath &filePath, const AxivionServer &server)
|
|
|
|
|
{
|
|
|
|
|
QJsonDocument doc;
|
|
|
|
|
doc.setObject(server.toJson());
|
|
|
|
|
// FIXME error handling?
|
|
|
|
|
filePath.writeFileContents(doc.toJson());
|
|
|
|
|
filePath.setPermissions(QFile::ReadUser | QFile::WriteUser);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static AxivionServer readTokenFile(const Utils::FilePath &filePath)
|
|
|
|
|
{
|
|
|
|
|
if (!filePath.exists())
|
|
|
|
|
return {};
|
|
|
|
|
Utils::expected_str<QByteArray> contents = filePath.fileContents();
|
|
|
|
|
if (!contents)
|
|
|
|
|
return {};
|
|
|
|
|
const QJsonDocument doc = QJsonDocument::fromJson(*contents);
|
|
|
|
|
if (!doc.isObject())
|
|
|
|
|
return {};
|
|
|
|
|
return AxivionServer::fromJson(doc.object());
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-28 09:48:11 +01:00
|
|
|
void AxivionSettings::toSettings(QSettings *s) const
|
|
|
|
|
{
|
2022-12-12 16:45:31 +01:00
|
|
|
writeTokenFile(tokensFilePath(s), server);
|
|
|
|
|
|
2022-11-28 09:48:11 +01:00
|
|
|
s->beginGroup("Axivion");
|
2022-12-12 16:45:31 +01:00
|
|
|
s->setValue(curlKeyC, curl.toVariant());
|
2022-11-28 09:48:11 +01:00
|
|
|
s->endGroup();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AxivionSettings::fromSettings(QSettings *s)
|
|
|
|
|
{
|
|
|
|
|
s->beginGroup("Axivion");
|
2022-12-12 16:45:31 +01:00
|
|
|
curl = Utils::FilePath::fromVariant(curlKeyC);
|
2022-11-28 09:48:11 +01:00
|
|
|
s->endGroup();
|
2022-12-12 16:45:31 +01:00
|
|
|
|
|
|
|
|
server = readTokenFile(tokensFilePath(s));
|
|
|
|
|
|
|
|
|
|
if (curl.isEmpty() || !curl.exists()) {
|
|
|
|
|
const QString curlPath = QStandardPaths::findExecutable(
|
|
|
|
|
Utils::HostOsInfo::withExecutableSuffix("curl"));
|
|
|
|
|
if (!curlPath.isEmpty())
|
|
|
|
|
curl = Utils::FilePath::fromString(curlPath);
|
|
|
|
|
}
|
2022-11-28 09:48:11 +01:00
|
|
|
}
|
|
|
|
|
|
2022-12-12 16:45:31 +01:00
|
|
|
} // Axivion::Internal
|