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; Connection *currentConnection() const;
void sendData(const QByteArray &data); void sendData(const QByteArray &data);
signals: signals:

View File

@ -92,12 +92,15 @@ void NymeaHosts::removeHost(NymeaHost *host)
{ {
int idx = m_hosts.indexOf(host); int idx = m_hosts.indexOf(host);
if (idx == -1) { 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; return;
} }
beginRemoveRows(QModelIndex(), idx, idx); beginRemoveRows(QModelIndex(), idx, idx);
m_hosts.takeAt(idx); m_hosts.takeAt(idx);
endRemoveRows(); endRemoveRows();
emit hostRemoved(host); emit hostRemoved(host);
emit countChanged(); emit countChanged();
} }

View File

@ -1,6 +1,8 @@
#include "configuredhostsmodel.h" #include "configuredhostsmodel.h"
#include <QDir>
#include <QSettings> #include <QSettings>
#include <QStandardPaths>
#include <QLoggingCategory> #include <QLoggingCategory>
Q_DECLARE_LOGGING_CATEGORY(dcApplication) Q_DECLARE_LOGGING_CATEGORY(dcApplication)
@ -99,12 +101,55 @@ void ConfiguredHostsModel::removeHost(int index)
qCWarning(dcApplication()) << "Cannot remove connection at index" << index; qCWarning(dcApplication()) << "Cannot remove connection at index" << index;
return; return;
} }
beginRemoveRows(QModelIndex(), index, index); 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(); saveToDisk();
endRemoveRows(); endRemoveRows();
emit countChanged(); emit countChanged();
if (m_list.isEmpty()) { if (m_list.isEmpty()) {
createHost(); createHost();
} }