EmuFront ... NOT Foobar :D
[emufront] / src / dialogs / dbobjectdialog.cpp
index b34f595..9bcb91b 100644 (file)
@@ -9,13 +9,13 @@
 // the Free Software Foundation, either version 3 of the License, or
 // (at your option) any later version.
 //
-// Foobar is distributed in the hope that it will be useful,
+// EmuFront is distributed in the hope that it will be useful,
 // but WITHOUT ANY WARRANTY; without even the implied warranty of
 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 // GNU General Public License for more details.
 //
 // You should have received a copy of the GNU General Public License
-// along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
+// along with EmuFront.  If not, see <http://www.gnu.org/licenses/>.
 
 #include <QtGui>
 #include <QSqlTableModel>
@@ -27,6 +27,7 @@ DbObjectDialog::DbObjectDialog(QWidget *parent)
 {
     dbObject = 0;
     dbManager = 0;
+    nameDialog = 0;
     editButton = new QPushButton(tr("&Edit")); 
     editButton->setEnabled(false);
     addButton = new QPushButton(tr("&Add"));
@@ -53,6 +54,7 @@ DbObjectDialog::~DbObjectDialog()
 
 void DbObjectDialog::connectSignals()
 {
+    qDebug() << "DbObjectDialog connecting signals";
     connect(buttonBox, SIGNAL(accepted()), this, SLOT(close()));
     connect(objectList, SIGNAL(clicked(const QModelIndex &)),
         this, SLOT(listObjectClicked(const QModelIndex &)));
@@ -60,6 +62,83 @@ void DbObjectDialog::connectSignals()
     connect(addButton, SIGNAL(clicked()), this, SLOT(addButtonClicked()));
     connect(deleteButton, SIGNAL(clicked()), this, SLOT(deleteButtonClicked()));
     connect(nameDialog, SIGNAL(dataObjectUpdated()), this, SLOT(updateData()));
+    connect(nameDialog, SIGNAL(updateRejected()), this, SLOT(updateReject()));
+    connect(nameDialog, SIGNAL(test()), this, SLOT(testSlot()));
+}
+    
+void DbObjectDialog::testSlot()
+{
+    qDebug() << "TEST SIGNAL RECEIVED!";
+}
+
+void DbObjectDialog::insertDb(const EmuFrontObject *ob) const
+{
+    if (!dbManager->insertDataObjectToModel(ob))
+        errorMessage->showMessage(tr("Inserting data object %1 failed.").arg(ob->getName()));
+}
+
+void DbObjectDialog::addObject()
+{
+    if (!nameDialog) initEditDialog();
+    deleteCurrentObject();
+    dbObject = createObject();
+    nameDialog->setDataObject(dbObject);
+    activateNameDialog();
+}
+
+void DbObjectDialog::editObject()
+{
+    QModelIndex index = objectList->currentIndex();
+    if (!index.isValid())
+        return;
+    if (!nameDialog) initEditDialog();
+    deleteCurrentObject();
+    dbObject = dbManager->getDataObjectFromModel(&index);
+    nameDialog->setDataObject(dbObject);
+    activateNameDialog();
+}
+
+bool DbObjectDialog::deleteItem()
+{
+    QModelIndex index = objectList->currentIndex();
+    if (!index.isValid()) return false;
+    try
+    {
+        EmuFrontObject *ob = dbManager->getDataObjectFromModel(&index);
+
+        if (!ob)
+        {
+            errorMessage->showMessage(tr("Couldn't find the selected data object from data model!"));
+            return false;
+        }
+
+        int numBindings = dbManager->countDataObjectRefs(ob->getId());
+
+        if (numBindings > 0 && !confirmDelete(ob->getName(), numBindings))
+        { return false; }
+        deleteCurrentObject();
+
+        bool delOk = dbManager->deleteDataObjectFromModel(&index);
+        if (!delOk)
+        {
+            errorMessage->showMessage(tr("Deleting data object %1 failed!").arg(ob->getName()));
+            return false;
+        }
+        updateList();
+        objectList->setFocus();
+    }
+    catch(EmuFrontException e)
+    {
+        errorMessage->showMessage(e.what());
+    }
+    return false;
+}
+
+void DbObjectDialog::updateDb(const EmuFrontObject *ob) const
+{
+    if (!ob) return;
+    if ( !dbManager->updateDataObjectToModel(ob) )
+    { errorMessage->showMessage("Database update failed!"); }
 }
 
 void DbObjectDialog::updateList() const
@@ -95,7 +174,6 @@ void DbObjectDialog::deleteButtonClicked()
     int yn = QMessageBox::question(this, "Confirm", msg, QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
     if (yn == QMessageBox::Yes)
     {
-        qDebug() << "Deleting item..." << name << ".";
         deleteItem();
     }
 }
@@ -143,18 +221,18 @@ void DbObjectDialog::initDataTable()
    objectList->setSelectionMode(QAbstractItemView::SingleSelection);
    objectList->resizeColumnsToContents();
 }
+
+void DbObjectDialog::updateReject()
+{
+    // we don't want to keep this in memory
+    deleteCurrentObject();
+}
+
 void DbObjectDialog::updateData()
 {
-    qDebug() << "Update data";
     // update data model
     if (!dbObject) return;
 
-    qDebug() << "dbObject is not 0";
-
-    qDebug() << "We have a " + dbObject->getName();
-
-    qDebug() << "Data will be inserted/updated...";
-
     // if data object id > -1 we are updating the data otherwise we are inserting new data
     if (dbObject->getId() > -1) updateDb(dbObject);
     else insertDb(dbObject);
@@ -180,3 +258,8 @@ bool DbObjectDialog::confirmDelete(QString name, int numRefs)
         return false;
     return true;
 }
+
+void DbObjectDialog::refreshDataModel()
+{
+    dbManager->resetModel();
+}