Skip to content

Commit

Permalink
add option to save multiple credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
raoulh committed Sep 9, 2024
1 parent d1c99d2 commit df4b50d
Show file tree
Hide file tree
Showing 12 changed files with 749 additions and 221 deletions.
2 changes: 2 additions & 0 deletions calaos_installer.pro
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ TARGET = calaos_installer
TEMPLATE = app
SOURCES += src/main.cpp \
src/DialogZigbee2mqtt.cpp \
src/SimpleCrypt.cpp \
src/mainwindow.cpp \
src/common/base64.c \
src/common/Params.cpp \
Expand Down Expand Up @@ -111,6 +112,7 @@ SOURCES += src/main.cpp \

HEADERS += src/mainwindow.h \
src/DialogZigbee2mqtt.h \
src/SimpleCrypt.h \
src/common/Utils.h \
src/common/base64.h \
src/common/Params.h \
Expand Down
73 changes: 35 additions & 38 deletions data/DialogOpenOnline.ui
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>421</width>
<height>274</height>
<width>441</width>
<height>304</height>
</rect>
</property>
<property name="windowTitle">
Expand All @@ -23,7 +23,7 @@
<item>
<widget class="QLabel" name="label">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Maximum">
<sizepolicy hsizetype="MinimumExpanding" vsizetype="Maximum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
Expand All @@ -35,48 +35,45 @@
</item>
<item>
<widget class="QComboBox" name="comboIP">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>250</width>
<height>0</height>
</size>
</property>
<property name="editable">
<bool>true</bool>
</property>
<item>
<property name="text">
<string>192.168.1.100</string>
</property>
</item>
<item>
<property name="text">
<string>192.168.0.100</string>
</property>
</item>
<item>
<property name="text">
<string>10.0.0.100</string>
</property>
</item>
<item>
<property name="text">
<string>10.0.0.101</string>
</property>
</item>
<item>
<property name="text">
<string>http://192.168.0.100:5454/api.php</string>
</property>
</item>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButtonDelPref">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="toolTip">
<string>Remove saved login</string>
</property>
<property name="text">
<string/>
</property>
<property name="icon">
<iconset resource="resources.qrc">
<normaloff>:/img/edit_remove.png</normaloff>:/img/edit_remove.png</iconset>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QCheckBox" name="calaosfrCheck">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>Connect with home.calaos.fr</string>
</property>
</widget>
</item>
<item>
<widget class="QGroupBox" name="groupBox">
<property name="sizePolicy">
Expand Down
18 changes: 16 additions & 2 deletions data/DialogSaveOnline.ui
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>421</width>
<width>445</width>
<height>274</height>
</rect>
</property>
Expand All @@ -23,7 +23,7 @@
<item>
<widget class="QLabel" name="label">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Maximum">
<sizepolicy hsizetype="MinimumExpanding" vsizetype="Maximum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
Expand All @@ -35,6 +35,12 @@
</item>
<item>
<widget class="QComboBox" name="comboIP">
<property name="minimumSize">
<size>
<width>250</width>
<height>0</height>
</size>
</property>
<property name="editable">
<bool>true</bool>
</property>
Expand Down Expand Up @@ -65,6 +71,14 @@
</item>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButtonDelPref">
<property name="icon">
<iconset resource="resources.qrc">
<normaloff>:/img/edit_remove.png</normaloff>:/img/edit_remove.png</iconset>
</property>
</widget>
</item>
</layout>
</item>
<item>
Expand Down
104 changes: 99 additions & 5 deletions src/ConfigOptions.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,32 @@
#include "ConfigOptions.h"
#include <QRandomGenerator>

ConfigOptions::ConfigOptions()
{
wago_host = settings.value("calaos/wago_host", "10.0.0.123").toString();
host = settings.value("calaos/calaos_host", "192.168.1.100").toString();
quint64 cryptKey = settings.value("calaos/config_saved", 0).toLongLong();

username = settings.value("calaos/cn_user").toString();
password = settings.value("calaos/cn_pass").toString();
if (cryptKey == 0)
{
cryptKey = QRandomGenerator::global()->generate64();
settings.setValue("calaos/config_saved", cryptKey);
}

use_calaosfr = settings.value("calaos/use_calaos_fr").toBool();
use_wago_calaosd = settings.value("calaos/use_wago_calaosd").toBool();
crypt.setKey(cryptKey);
crypt.setIntegrityProtectionMode(SimpleCrypt::ProtectionHash);

if (settings.contains("calaos/calaos_host"))
{
CalaosAuth old;
old.username = settings.value("calaos/cn_user").toString();
old.password = settings.value("calaos/cn_pass").toString();
old.host = settings.value("calaos/calaos_host").toString();
addAuth(old);

settings.remove("calaos/calaos_host");
settings.remove("calaos/cn_user");
settings.remove("calaos/cn_pass");
}
}

void ConfigOptions::setOption(const QString &key, const QVariant &value)
Expand All @@ -21,3 +38,80 @@ QVariant ConfigOptions::getOption(const QString &key)
{
return settings.value(key);
}

QVector<CalaosAuth> ConfigOptions::getAuthList()
{
QVector<CalaosAuth> authList;

int size = settings.beginReadArray("authList");
for (int i = 0; i < size; ++i)
{
settings.setArrayIndex(i);
CalaosAuth auth;
auth.username = settings.value("username").toString();
auth.password = crypt.decryptToString(settings.value("password").toString());
auth.host = settings.value("host").toString();
authList.append(auth);
}
settings.endArray();

return authList;
}

void ConfigOptions::addAuth(const CalaosAuth &auth)
{
QVector<CalaosAuth> authList = getAuthList();
bool exists = false;

for (int i = 0; i < authList.size(); ++i)
{
if (authList[i].host == auth.host)
{
authList[i].username = auth.username;
authList[i].password = auth.password;
exists = true;
break;
}
}

if (!exists)
authList.append(auth);

settings.remove("authList");

settings.beginWriteArray("authList", authList.size());
for (int i = 0; i < authList.size(); ++i)
{
settings.setArrayIndex(i);
settings.setValue("username", authList[i].username);
settings.setValue("password", crypt.encryptToString(authList[i].password));
settings.setValue("host", authList[i].host);
}
settings.endArray();
}

void ConfigOptions::deleteAuth(const QString &host)
{
QVector<CalaosAuth> authList = getAuthList();

for (int i = 0; i < authList.size(); ++i)
{
if (authList[i].host == host)
{
authList.removeAt(i);
break;
}
}

settings.remove("authList");

settings.beginWriteArray("authList", authList.size());
for (int i = 0; i < authList.size(); ++i)
{
settings.setArrayIndex(i);
settings.setValue("username", authList[i].username);
settings.setValue("password", crypt.encryptToString(authList[i].password));
settings.setValue("host", authList[i].host);
}
settings.endArray();
}
21 changes: 15 additions & 6 deletions src/ConfigOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@

#include <QString>
#include <QSettings>
#include "SimpleCrypt.h"

struct CalaosAuth
{
QString username;
QString password;
QString host;
};

class ConfigOptions
{
Expand All @@ -25,12 +33,6 @@ class ConfigOptions
QString getWagoHost() { return wago_host; }
void setWagoHost(QString s) { wago_host = s; settings.setValue("calaos/wago_host", wago_host); }

bool useCalaosFr() { return use_calaosfr; }
void setUseCalaosFr(bool s) { use_calaosfr = s; settings.setValue("calaos/use_calaos_fr", use_calaosfr); }

bool useCalaosServer() { return use_wago_calaosd; }
void setUseCalaosServer(bool s) { use_wago_calaosd = s; settings.setValue("calaos/use_wago_calaosd", use_wago_calaosd); }

QString getMqttBrokerHost() { return settings.value("calaos/mqtt_broker_host", "192.168.0.10").toString(); }
void setMqttBrokerHost(QString s) { settings.setValue("calaos/mqtt_broker_host", s); }
int getMqttBrokerPort() { return settings.value("calaos/mqtt_broker_port", 1883).toInt(); }
Expand All @@ -46,6 +48,12 @@ class ConfigOptions
QVariant getOption(const QString &key);
bool optionExists(const QString &key) { return settings.contains(key); }

QVector<CalaosAuth> getAuthList();
void addAuth(const CalaosAuth &auth);
void deleteAuth(const QString &host);
int getCurrentAuthIndex() { return settings.value("calaos/current_auth", 0).toInt(); }
void setCurrentAuthIndex(int index) { settings.setValue("calaos/current_auth", index); }

private:
ConfigOptions();

Expand All @@ -57,6 +65,7 @@ class ConfigOptions
bool use_wago_calaosd;

QSettings settings;
SimpleCrypt crypt;

};

Expand Down
Loading

0 comments on commit df4b50d

Please sign in to comment.