]> Nutra Git (v1) - nutratech/gui.git/commitdiff
don't allow re-opening same DB (weird loop)
authorShane Jaroch <chown_tee@proton.me>
Wed, 21 Jan 2026 19:04:43 +0000 (14:04 -0500)
committerShane Jaroch <chown_tee@proton.me>
Wed, 21 Jan 2026 19:04:43 +0000 (14:04 -0500)
src/db/databasemanager.cpp
src/mainwindow.cpp

index de731d45392f3c94088c70e75b1d316641c57935..3d27bed72aae966139262602bb16c834d706354c 100644 (file)
@@ -31,7 +31,10 @@ bool DatabaseManager::isValidNutraDatabase(const QSqlDatabase& db) {
 
 bool DatabaseManager::connect(const QString& path) {
     if (m_db.isOpen()) {
-        return true;
+        if (m_db.databaseName() == path) {
+            return true;
+        }
+        m_db.close();
     }
 
     if (!QFileInfo::exists(path)) {
index 4779b98101e00b72188efd2432e65afa3d82e0aa..9b8bdeef5569eebe2dfcfea82755f79889c9f9f1 100644 (file)
@@ -107,6 +107,12 @@ void MainWindow::onOpenDatabase() {
                                      "SQLite Databases (*.sqlite3 *.db)");
 
     if (!fileName.isEmpty()) {
+        if (DatabaseManager::instance().isOpen() &&
+            DatabaseManager::instance().database().databaseName() == fileName) {
+            QMessageBox::information(this, "Already Open", "This database is already loaded.");
+            return;
+        }
+
         if (DatabaseManager::instance().connect(fileName)) {
             qDebug() << "Switched to database:" << fileName;
             addToRecentFiles(fileName);
@@ -124,6 +130,13 @@ void MainWindow::onRecentFileClick() {
     auto* action = qobject_cast<QAction*>(sender());
     if (action != nullptr) {
         QString fileName = action->data().toString();
+
+        if (DatabaseManager::instance().isOpen() &&
+            DatabaseManager::instance().database().databaseName() == fileName) {
+            QMessageBox::information(this, "Already Open", "This database is already loaded.");
+            return;
+        }
+
         if (DatabaseManager::instance().connect(fileName)) {
             qDebug() << "Switched to database (recent):" << fileName;
             addToRecentFiles(fileName);