From 0f59ec5516b44f284cbe9b1e930d7df148e4f144 Mon Sep 17 00:00:00 2001 From: nicola02nb <61830443+nicola02nb@users.noreply.github.com> Date: Thu, 19 Sep 2024 17:37:17 +0200 Subject: [PATCH] Reorganized device managing Befores was stored in memory both connected and seved devices; now only connected --- src/DataTypes/device.cpp | 39 +++++++++++++++++++-------------------- src/DataTypes/device.h | 2 +- src/UI/mainwindow.cpp | 28 +++++++++++++++------------- src/UI/mainwindow.h | 2 +- 4 files changed, 36 insertions(+), 35 deletions(-) diff --git a/src/DataTypes/device.cpp b/src/DataTypes/device.cpp index c24f98f..4267997 100644 --- a/src/DataTypes/device.cpp +++ b/src/DataTypes/device.cpp @@ -175,29 +175,29 @@ Device Device::fromJson(const QJsonObject &json) return device; } -QList mergeDevices(QList connectedDevices, const QList &savedDevices) +void updateDevicesFromSource(QList &devicesToUpdate, const QList &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; @@ -206,10 +206,9 @@ QList mergeDevices(QList connectedDevices, const QList &devices, const QString &filePath) diff --git a/src/DataTypes/device.h b/src/DataTypes/device.h index 603daed..4f3a6fb 100644 --- a/src/DataTypes/device.h +++ b/src/DataTypes/device.h @@ -83,7 +83,7 @@ class Device static Device fromJson(const QJsonObject &json); }; -QList mergeDevices(QList connectedDevices, const QList &savedDevices); +void updateDevicesFromSource(QList &devicesToUpdate, const QList &sourceDevices); void serializeDevices(const QList &devices, const QString &filePath); QList deserializeDevices(const QString &filePath); diff --git a/src/UI/mainwindow.cpp b/src/UI/mainwindow.cpp index 2c5d149..d3b5563 100644 --- a/src/UI/mainwindow.cpp +++ b/src/UI/mainwindow.cpp @@ -310,15 +310,12 @@ void MainWindow::resetGUI() //Devices Managing Section void MainWindow::loadDevices() { - QList connected = API.getConnectedDevices(), saved = getSavedDevices(); - n_connected = connected.length(); - n_saved = saved.length(); - QList merged = mergeDevices(connected, saved); + deleteDevices(connectedDevices); + QList saved = getSavedDevices(); + connectedDevices = API.getConnectedDevices(); + updateDevicesFromSource(connectedDevices, saved); - deleteDevices(deviceList); deleteDevices(saved); - - deviceList = merged; } void MainWindow::loadDevice(int deviceIndex) @@ -330,7 +327,7 @@ void MainWindow::loadDevice(int deviceIndex) return; } - selectedDevice = deviceList.value(deviceIndex); + selectedDevice = connectedDevices.value(deviceIndex); QSet &capabilities = selectedDevice->capabilities; ui->missingheadsetcontrolFrame->setHidden(true); @@ -488,7 +485,12 @@ void MainWindow::loadGUIValues() void MainWindow::saveDevicesSettings() { - serializeDevices(deviceList, DEVICES_SETTINGS_FILEPATH); + QList toSave = getSavedDevices(); + updateDevicesFromSource(toSave, connectedDevices); + + serializeDevices(toSave, DEVICES_SETTINGS_FILEPATH); + + deleteDevices(toSave); } QList MainWindow::getSavedDevices() @@ -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(); @@ -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; } diff --git a/src/UI/mainwindow.h b/src/UI/mainwindow.h index 10fd765..6d2ba80 100644 --- a/src/UI/mainwindow.h +++ b/src/UI/mainwindow.h @@ -56,7 +56,7 @@ class MainWindow : public QMainWindow HeadsetControlAPI API; Device *selectedDevice = nullptr; - QList deviceList; + QList connectedDevices; QList slidersEq;