diff --git a/src/DataTypes/device.cpp b/src/DataTypes/device.cpp index 4a77b79..c24f98f 100644 --- a/src/DataTypes/device.cpp +++ b/src/DataTypes/device.cpp @@ -108,14 +108,15 @@ void Device::updateDevice(const Device *new_device) this->chatmix = new_device->chatmix; } -void Device::updateDevice(const QList &new_device_list) +bool Device::updateDevice(const QList &new_device_list) { for (int i = 0; i < new_device_list.length(); ++i) { if (this != new_device_list.at(i)) { this->updateDevice(new_device_list.at(i)); - break; + return true; } } + return false; } QJsonObject Device::toJson() const @@ -205,7 +206,7 @@ QList mergeDevices(QList connectedDevices, const QList &new_device_list); + bool updateDevice(const QList &new_device_list); QJsonObject toJson() const; static Device fromJson(const QJsonObject &json); diff --git a/src/UI/mainwindow.cpp b/src/UI/mainwindow.cpp index fc69231..2c5d149 100644 --- a/src/UI/mainwindow.cpp +++ b/src/UI/mainwindow.cpp @@ -18,6 +18,7 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) , trayIcon(new QSystemTrayIcon(this)) + , trayMenu(new QMenu(this)) , timerGUI(new QTimer(this)) , API(HeadsetControlAPI(HEADSETCONTROL_FILE_PATH)) { @@ -46,9 +47,19 @@ MainWindow::MainWindow(QWidget *parent) hide(); } +void MainWindow::deleteDevices(QList deviceList) +{ + for (Device *device : deviceList) { + delete device; + } + deviceList.clear(); +} + MainWindow::~MainWindow() { timerGUI->stop(); + delete timerGUI; + delete trayMenu; delete trayIcon; delete ui; } @@ -169,7 +180,6 @@ void MainWindow::setupTrayIcon() changeTrayIconTo("headphones"); trayIcon->setToolTip("HeadsetControl"); - trayMenu = new QMenu(this); trayMenu->addAction(tr("Hide/Show"), this, &MainWindow::toggleWindow); ledOn = trayMenu->addAction(tr("Turn Lights On"), &API, [=]() { API.setLights(selectedDevice, true); @@ -300,10 +310,15 @@ void MainWindow::resetGUI() //Devices Managing Section void MainWindow::loadDevices() { - QList c = API.getConnectedDevices(), s = getSavedDevices(); - n_connected = c.length(); - n_saved = s.length(); - deviceList = mergeDevices(c, s); + QList connected = API.getConnectedDevices(), saved = getSavedDevices(); + n_connected = connected.length(); + n_saved = saved.length(); + QList merged = mergeDevices(connected, saved); + + deleteDevices(deviceList); + deleteDevices(saved); + + deviceList = merged; } void MainWindow::loadDevice(int deviceIndex) @@ -484,7 +499,11 @@ QList MainWindow::getSavedDevices() void MainWindow::updateDevice() { QList newDl = API.getConnectedDevices(); - selectedDevice->updateDevice(newDl); + if (!selectedDevice->updateDevice(newDl)) { + selectedDevice = nullptr; + } + + deleteDevices(newDl); } //Update GUI Section @@ -686,6 +705,7 @@ void MainWindow::selectDevice() loadDevice(index); } } + delete (loadDevWindow); } void MainWindow::editProgramSetting() @@ -697,6 +717,7 @@ void MainWindow::editProgramSetting() timerGUI->setInterval(settings.msecUpdateIntervalTime); updateStyle(); } + delete (settingsW); } void MainWindow::checkForUpdates(bool firstStart) @@ -733,7 +754,8 @@ void MainWindow::checkForUpdates(bool firstStart) QString text = "HeadesetControl: " + s1 + "
HeadesetControl-GUI: " + s2; dialogWindow->setLabel(text); - dialogWindow->show(); + dialogWindow->exec(); + delete (dialogWindow); } } @@ -750,7 +772,9 @@ void MainWindow::showAbout() + qApp->applicationVersion(); dialogWindow->setLabel(text); - dialogWindow->show(); + dialogWindow->exec(); + + delete (dialogWindow); } void MainWindow::showCredits() @@ -762,5 +786,7 @@ void MainWindow::showCredits() "href='https://github.com/Sapd/HeadsetControl'>HeadsetCoontrol"); dialogWindow->setLabel(text); - dialogWindow->show(); + dialogWindow->exec(); + + delete (dialogWindow); } diff --git a/src/UI/mainwindow.h b/src/UI/mainwindow.h index ae7fdf1..10fd765 100644 --- a/src/UI/mainwindow.h +++ b/src/UI/mainwindow.h @@ -44,11 +44,11 @@ class MainWindow : public QMainWindow Ui::MainWindow *ui; QSystemTrayIcon *trayIcon; - QTimer *timerGUI; QString trayIconName; QMenu *trayMenu; QAction *ledOn; QAction *ledOff; + QTimer *timerGUI; Settings settings; @@ -60,6 +60,8 @@ class MainWindow : public QMainWindow QList slidersEq; + void deleteDevices(QList deviceList); + void bindEvents(); //Tray Icon Section diff --git a/src/Utils/headsetcontrolapi.cpp b/src/Utils/headsetcontrolapi.cpp index f3447e0..6ad7c6f 100644 --- a/src/Utils/headsetcontrolapi.cpp +++ b/src/Utils/headsetcontrolapi.cpp @@ -74,6 +74,8 @@ QString HeadsetControlAPI::sendCommand(const QStringList &args_list) // qDebug() << output; qDebug() << "Error: \t" << proc->error(); + delete (proc); + return output; }