Clean up host information propertly once removed from settings

This commit is contained in:
Simon Stürz 2024-10-24 15:26:54 +02:00
parent 264f803a17
commit 422e7675f9
6 changed files with 69 additions and 22 deletions

View File

@ -103,7 +103,6 @@ public:
Connection *currentConnection() const;
void sendData(const QByteArray &data);
signals:

View File

@ -92,12 +92,15 @@ void NymeaHosts::removeHost(NymeaHost *host)
{
int idx = m_hosts.indexOf(host);
if (idx == -1) {
qWarning() << "Cannot remove NymeaHost" << host << "as its nit in the model";
qWarning() << "Cannot remove NymeaHost" << host << "as its not in the model";
return;
}
beginRemoveRows(QModelIndex(), idx, idx);
m_hosts.takeAt(idx);
endRemoveRows();
emit hostRemoved(host);
emit countChanged();
}

View File

@ -1,6 +1,8 @@
#include "configuredhostsmodel.h"
#include <QDir>
#include <QSettings>
#include <QStandardPaths>
#include <QLoggingCategory>
Q_DECLARE_LOGGING_CATEGORY(dcApplication)
@ -99,12 +101,55 @@ void ConfiguredHostsModel::removeHost(int index)
qCWarning(dcApplication()) << "Cannot remove connection at index" << index;
return;
}
beginRemoveRows(QModelIndex(), index, index);
m_list.takeAt(index)->deleteLater();
ConfiguredHost *host = m_list.takeAt(index);
qCDebug(dcApplication()) << "Remove configured host" << host->name() << host->uuid().toString();
QSettings settings;
QString hostUuidString = host->uuid().toString();
qCDebug(dcApplication()) << "-> Remove stored token";
settings.beginGroup("jsonTokens");
if (settings.contains(hostUuidString)) {
settings.remove(hostUuidString);
}
settings.endGroup();
qCDebug(dcApplication()) << "-> Remove cached hosts";
settings.beginGroup("HostCache");
settings.beginGroup(hostUuidString);
settings.remove("");
settings.endGroup();
settings.endGroup();
qCDebug(dcApplication()) << "-> Remove host settings";
settings.beginGroup(hostUuidString);
settings.remove("");
settings.endGroup();
QDir dir(QStandardPaths::writableLocation(QStandardPaths::DataLocation) + "/sslcerts/");
QFile certFile(dir.absoluteFilePath(hostUuidString.remove(QRegExp("[{}]")) + ".pem"));
if (certFile.exists()) {
if (!certFile.remove()) {
qCWarning(dcApplication()) << "Failed to remove certificate file" << certFile.fileName() << certFile.errorString();
} else {
qCDebug(dcApplication()) << "-> Removed successfully host certificate" << certFile.fileName();
}
} else {
qCDebug(dcApplication()) << "-> No certificated stored for this host";
}
host->deleteLater();
saveToDisk();
endRemoveRows();
emit countChanged();
if (m_list.isEmpty()) {
createHost();
}