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

@ -49,8 +49,8 @@ class NymeaConnection : public QObject
{ {
Q_OBJECT Q_OBJECT
Q_PROPERTY(bool connected READ connected NOTIFY connectedChanged) Q_PROPERTY(bool connected READ connected NOTIFY connectedChanged)
Q_PROPERTY(NymeaHost* currentHost READ currentHost WRITE setCurrentHost NOTIFY currentHostChanged) Q_PROPERTY(NymeaHost *currentHost READ currentHost WRITE setCurrentHost NOTIFY currentHostChanged)
Q_PROPERTY(Connection* currentConnection READ currentConnection NOTIFY currentConnectionChanged) Q_PROPERTY(Connection *currentConnection READ currentConnection NOTIFY currentConnectionChanged)
Q_PROPERTY(NymeaConnection::BearerTypes availableBearerTypes READ availableBearerTypes NOTIFY availableBearerTypesChanged) Q_PROPERTY(NymeaConnection::BearerTypes availableBearerTypes READ availableBearerTypes NOTIFY availableBearerTypesChanged)
Q_PROPERTY(ConnectionStatus connectionStatus READ connectionStatus NOTIFY connectionStatusChanged) Q_PROPERTY(ConnectionStatus connectionStatus READ connectionStatus NOTIFY connectionStatusChanged)
@ -98,11 +98,10 @@ public:
bool connected(); bool connected();
ConnectionStatus connectionStatus() const; ConnectionStatus connectionStatus() const;
NymeaHost* currentHost() const; NymeaHost *currentHost() const;
void setCurrentHost(NymeaHost *host); void setCurrentHost(NymeaHost *host);
Connection* currentConnection() const; Connection *currentConnection() const;
void sendData(const QByteArray &data); void sendData(const QByteArray &data);
@ -134,8 +133,8 @@ private:
ConnectionStatus m_connectionStatus = ConnectionStatusUnconnected; ConnectionStatus m_connectionStatus = ConnectionStatusUnconnected;
NetworkReachabilityMonitor *m_networkReachabilityMonitor = nullptr; NetworkReachabilityMonitor *m_networkReachabilityMonitor = nullptr;
QHash<QString, NymeaTransportInterfaceFactory*> m_transportFactories; QHash<QString, NymeaTransportInterfaceFactory *> m_transportFactories;
QHash<NymeaTransportInterface*, Connection*> m_transportCandidates; QHash<NymeaTransportInterface *, Connection *> m_transportCandidates;
NymeaTransportInterface *m_currentTransport = nullptr; NymeaTransportInterface *m_currentTransport = nullptr;
NymeaHost *m_currentHost = nullptr; NymeaHost *m_currentHost = nullptr;
Connection *m_preferredConnection = nullptr; Connection *m_preferredConnection = nullptr;

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

@ -79,7 +79,7 @@ protected:
QHash<int, QByteArray> roleNames() const; QHash<int, QByteArray> roleNames() const;
private: private:
QList<NymeaHost*> m_hosts; QList<NymeaHost *> m_hosts;
}; };
class NymeaHostsFilterModel: public QSortFilterProxyModel class NymeaHostsFilterModel: public QSortFilterProxyModel
@ -94,10 +94,10 @@ class NymeaHostsFilterModel: public QSortFilterProxyModel
public: public:
NymeaHostsFilterModel(QObject *parent = nullptr); NymeaHostsFilterModel(QObject *parent = nullptr);
NymeaDiscovery* discovery() const; NymeaDiscovery *discovery() const;
void setDiscovery(NymeaDiscovery *discovery); void setDiscovery(NymeaDiscovery *discovery);
JsonRpcClient* jsonRpcClient() const; JsonRpcClient *jsonRpcClient() const;
void setJsonRpcClient(JsonRpcClient* jsonRpcClient); void setJsonRpcClient(JsonRpcClient* jsonRpcClient);
bool showUnreachableBearers() const; bool showUnreachableBearers() const;

View File

@ -77,8 +77,8 @@ public:
NymeaConnection::BearerTypes availableBearerTypes() const; NymeaConnection::BearerTypes availableBearerTypes() const;
NymeaConnection::ConnectionStatus connectionStatus() const; NymeaConnection::ConnectionStatus connectionStatus() const;
bool connected() const; bool connected() const;
NymeaHost* currentHost() const; NymeaHost *currentHost() const;
Connection* currentConnection() const; Connection *currentConnection() const;
QVariantMap certificateIssuerInfo() const; QVariantMap certificateIssuerInfo() const;
bool initialSetupRequired() const; bool initialSetupRequired() const;
bool authenticationRequired() const; bool authenticationRequired() const;
@ -147,8 +147,8 @@ private slots:
private: private:
int m_id; int m_id;
// < namespace, method> > // < namespace, method> >
QHash<QObject*, QString> m_notificationHandlerMethods; QHash<QObject *, QString> m_notificationHandlerMethods;
QMultiHash<QString, QObject*> m_notificationHandlers; QMultiHash<QString, QObject *> m_notificationHandlers;
QHash<int, JsonRpcReply *> m_replies; QHash<int, JsonRpcReply *> m_replies;
NymeaConnection *m_connection = nullptr; NymeaConnection *m_connection = nullptr;

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();
} }

View File

@ -12,7 +12,7 @@ class ConfiguredHost: public QObject
Q_OBJECT Q_OBJECT
Q_PROPERTY(QUuid uuid READ uuid WRITE setUuid NOTIFY uuidChanged) Q_PROPERTY(QUuid uuid READ uuid WRITE setUuid NOTIFY uuidChanged)
Q_PROPERTY(QString name READ name NOTIFY nameChanged) Q_PROPERTY(QString name READ name NOTIFY nameChanged)
Q_PROPERTY(Engine* engine READ engine CONSTANT) Q_PROPERTY(Engine *engine READ engine CONSTANT)
public: public:
ConfiguredHost(const QUuid &uuid = QUuid(), QObject *parent = nullptr); ConfiguredHost(const QUuid &uuid = QUuid(), QObject *parent = nullptr);
@ -57,8 +57,8 @@ public:
void setCurrentIndex(int currentIndex); void setCurrentIndex(int currentIndex);
Q_INVOKABLE int indexOf(ConfiguredHost *host) const; Q_INVOKABLE int indexOf(ConfiguredHost *host) const;
Q_INVOKABLE ConfiguredHost* get(int index) const; Q_INVOKABLE ConfiguredHost *get(int index) const;
Q_INVOKABLE ConfiguredHost* createHost(); Q_INVOKABLE ConfiguredHost *createHost();
Q_INVOKABLE void removeHost(int index); Q_INVOKABLE void removeHost(int index);
Q_INVOKABLE void move(int from, int to); Q_INVOKABLE void move(int from, int to);
@ -72,7 +72,7 @@ private:
void saveToDisk(); void saveToDisk();
private: private:
QList<ConfiguredHost*> m_list; QList<ConfiguredHost *> m_list;
int m_currentIndex = 0; int m_currentIndex = 0;
}; };
@ -85,13 +85,13 @@ class ConfiguredHostsProxyModel: public QSortFilterProxyModel
public: public:
ConfiguredHostsProxyModel(QObject *parent = nullptr); ConfiguredHostsProxyModel(QObject *parent = nullptr);
ConfiguredHostsModel* model() const; ConfiguredHostsModel *model() const;
void setModel(ConfiguredHostsModel *model); void setModel(ConfiguredHostsModel *model);
QUuid currentHost() const; QUuid currentHost() const;
void setCurrentHost(const QUuid &currentHost); void setCurrentHost(const QUuid &currentHost);
Q_INVOKABLE ConfiguredHost* get(int index) const; Q_INVOKABLE ConfiguredHost *get(int index) const;
signals: signals:
void modelChanged(); void modelChanged();