Skip to content

Commit

Permalink
Reorganized device managing
Browse files Browse the repository at this point in the history
Befores was stored in memory both connected and seved devices; now only connected
  • Loading branch information
nicola02nb committed Sep 19, 2024
1 parent 84eca3f commit 0f59ec5
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 35 deletions.
39 changes: 19 additions & 20 deletions src/DataTypes/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,29 +175,29 @@ Device Device::fromJson(const QJsonObject &json)
return device;
}

QList<Device *> mergeDevices(QList<Device *> connectedDevices, const QList<Device *> &savedDevices)
void updateDevicesFromSource(QList<Device *> &devicesToUpdate, const QList<Device *> &sourceDevices)
{
for (Device *savedDevice : savedDevices) {
for (Device *sourceDevice : sourceDevices) {
bool deviceFound = false;
for (Device *connectedDevice : connectedDevices) {
if (connectedDevice->id_vendor == savedDevice->id_vendor
&& connectedDevice->id_product == savedDevice->id_product) {
for (Device *toUpdateDevice : devicesToUpdate) {
if (toUpdateDevice->id_vendor == sourceDevice->id_vendor
&& toUpdateDevice->id_product == sourceDevice->id_product) {
// Update the connected device with saved device's information
connectedDevice->lights = savedDevice->lights;
connectedDevice->sidetone = savedDevice->sidetone;
connectedDevice->voice_prompts = savedDevice->voice_prompts;
connectedDevice->inactive_time = savedDevice->inactive_time;
toUpdateDevice->lights = sourceDevice->lights;
toUpdateDevice->sidetone = sourceDevice->sidetone;
toUpdateDevice->voice_prompts = sourceDevice->voice_prompts;
toUpdateDevice->inactive_time = sourceDevice->inactive_time;

connectedDevice->equalizer_preset = savedDevice->equalizer_preset;
connectedDevice->equalizer_curve = savedDevice->equalizer_curve;
connectedDevice->volume_limiter = savedDevice->volume_limiter;
toUpdateDevice->equalizer_preset = sourceDevice->equalizer_preset;
toUpdateDevice->equalizer_curve = sourceDevice->equalizer_curve;
toUpdateDevice->volume_limiter = sourceDevice->volume_limiter;

connectedDevice->rotate_to_mute = savedDevice->rotate_to_mute;
connectedDevice->mic_mute_led_brightness = savedDevice->mic_mute_led_brightness;
connectedDevice->mic_volume = savedDevice->mic_volume;
toUpdateDevice->rotate_to_mute = sourceDevice->rotate_to_mute;
toUpdateDevice->mic_mute_led_brightness = sourceDevice->mic_mute_led_brightness;
toUpdateDevice->mic_volume = sourceDevice->mic_volume;

connectedDevice->bt_when_powered_on = savedDevice->bt_when_powered_on;
connectedDevice->bt_call_volume = savedDevice->bt_call_volume;
toUpdateDevice->bt_when_powered_on = sourceDevice->bt_when_powered_on;
toUpdateDevice->bt_call_volume = sourceDevice->bt_call_volume;

deviceFound = true;
break;
Expand All @@ -206,10 +206,9 @@ QList<Device *> mergeDevices(QList<Device *> connectedDevices, const QList<Devic

if (!deviceFound) {
// If the device wasn't found in saved devices, add it
connectedDevices.append(savedDevice);
devicesToUpdate.append(sourceDevice);
}
}
return connectedDevices;
};
}

void serializeDevices(const QList<Device *> &devices, const QString &filePath)
Expand Down
2 changes: 1 addition & 1 deletion src/DataTypes/device.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class Device
static Device fromJson(const QJsonObject &json);
};

QList<Device *> mergeDevices(QList<Device *> connectedDevices, const QList<Device *> &savedDevices);
void updateDevicesFromSource(QList<Device *> &devicesToUpdate, const QList<Device *> &sourceDevices);

void serializeDevices(const QList<Device *> &devices, const QString &filePath);
QList<Device *> deserializeDevices(const QString &filePath);
Expand Down
28 changes: 15 additions & 13 deletions src/UI/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,15 +310,12 @@ void MainWindow::resetGUI()
//Devices Managing Section
void MainWindow::loadDevices()
{
QList<Device *> connected = API.getConnectedDevices(), saved = getSavedDevices();
n_connected = connected.length();
n_saved = saved.length();
QList<Device *> merged = mergeDevices(connected, saved);
deleteDevices(connectedDevices);
QList<Device *> saved = getSavedDevices();
connectedDevices = API.getConnectedDevices();
updateDevicesFromSource(connectedDevices, saved);

deleteDevices(deviceList);
deleteDevices(saved);

deviceList = merged;
}

void MainWindow::loadDevice(int deviceIndex)
Expand All @@ -330,7 +327,7 @@ void MainWindow::loadDevice(int deviceIndex)
return;
}

selectedDevice = deviceList.value(deviceIndex);
selectedDevice = connectedDevices.value(deviceIndex);
QSet<QString> &capabilities = selectedDevice->capabilities;

ui->missingheadsetcontrolFrame->setHidden(true);
Expand Down Expand Up @@ -488,7 +485,12 @@ void MainWindow::loadGUIValues()

void MainWindow::saveDevicesSettings()
{
serializeDevices(deviceList, DEVICES_SETTINGS_FILEPATH);
QList<Device *> toSave = getSavedDevices();
updateDevicesFromSource(toSave, connectedDevices);

serializeDevices(toSave, DEVICES_SETTINGS_FILEPATH);

deleteDevices(toSave);
}

QList<Device *> MainWindow::getSavedDevices()
Expand Down Expand Up @@ -516,10 +518,10 @@ void MainWindow::updateGUI()
} else {
if (selectedDevice == nullptr) {
loadDevices();
if (!deviceList.isEmpty() && n_connected > 0) {
loadDevice();
} else {
if (connectedDevices.isEmpty()) {
ui->missingheadsetcontrolFrame->setHidden(true);
} else {
loadDevice();
}
} else {
updateDevice();
Expand Down Expand Up @@ -687,7 +689,7 @@ void MainWindow::selectDevice()
this->loadDevices();

QStringList devices = QStringList();
for (Device *device : deviceList) {
for (Device *device : connectedDevices) {
if (device->connected) {
devices << device->device;
}
Expand Down
2 changes: 1 addition & 1 deletion src/UI/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class MainWindow : public QMainWindow

HeadsetControlAPI API;
Device *selectedDevice = nullptr;
QList<Device *> deviceList;
QList<Device *> connectedDevices;

QList<QSlider *> slidersEq;

Expand Down

0 comments on commit 0f59ec5

Please sign in to comment.