Implemented initial exception handling
[emufront] / src / dialogs / dbobjectdialog.cpp
1 // EmuFront
2 // Copyright 2010 Mikko Keinänen
3 //
4 // This file is part of EmuFront.
5 //
6 //
7 // EmuFront is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation, either version 3 of the License, or
10 // (at your option) any later version.
11 //
12 // Foobar is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
19
20 #include <QtGui>
21 #include <QSqlTableModel>
22 #include "dbobjectdialog.h"
23 #include "../db/databasemanager.h"
24
25 DbObjectDialog::DbObjectDialog(QWidget *parent)
26     : EmuFrontDialog(parent)
27 {
28     dbObject = 0;
29     dbManager = 0;
30     nameDialog = 0;
31     editButton = new QPushButton(tr("&Edit")); 
32     editButton->setEnabled(false);
33     addButton = new QPushButton(tr("&Add"));
34     deleteButton = new QPushButton(tr("&Delete"));
35     deleteButton->setEnabled(false);
36     objectList = new QTableView(this);
37     buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok, Qt::Vertical);
38     buttonBox->addButton(editButton, QDialogButtonBox::ActionRole);
39     buttonBox->addButton(addButton, QDialogButtonBox::ActionRole);
40     buttonBox->addButton(deleteButton, QDialogButtonBox::ActionRole);
41     // this be called from the implementing classes:
42     //connectSignals();
43     layout();
44
45
46 DbObjectDialog::~DbObjectDialog()
47 {
48     // no need to explicitically delete widgets within a parented layout
49     // they are automatically parented and will be deleted
50     // dbManager is also parented and will be implicitically deleted
51     // this must be deleted in an implementing class
52     //delete dbObject;
53 }
54
55 void DbObjectDialog::connectSignals()
56 {
57     qDebug() << "DbObjectDialog connecting signals";
58     connect(buttonBox, SIGNAL(accepted()), this, SLOT(close()));
59     connect(objectList, SIGNAL(clicked(const QModelIndex &)),
60         this, SLOT(listObjectClicked(const QModelIndex &)));
61     connect(editButton, SIGNAL(clicked()), this, SLOT(editButtonClicked()));
62     connect(addButton, SIGNAL(clicked()), this, SLOT(addButtonClicked()));
63     connect(deleteButton, SIGNAL(clicked()), this, SLOT(deleteButtonClicked()));
64     connect(nameDialog, SIGNAL(dataObjectUpdated()), this, SLOT(updateData()));
65     connect(nameDialog, SIGNAL(updateRejected()), this, SLOT(updateReject()));
66     connect(nameDialog, SIGNAL(test()), this, SLOT(testSlot()));
67 }
68
69 void DbObjectDialog::testSlot()
70 {
71     qDebug() << "TEST SIGNAL RECEIVED!";
72 }
73
74 void DbObjectDialog::insertDb(const EmuFrontObject *ob) const
75 {
76     if ( dbManager->insertDataObjectToModel(ob) )
77         qDebug() << "Db insert ok";
78     else qDebug() << "Db insert failed";
79 }
80
81 void DbObjectDialog::addObject()
82 {
83     if (!nameDialog) initEditDialog();
84     deleteCurrentObject();
85     dbObject = createObject();
86     nameDialog->setDataObject(dbObject);
87     activateNameDialog();
88 }
89
90 void DbObjectDialog::editObject()
91 {
92     QModelIndex index = objectList->currentIndex();
93     if (!index.isValid())
94         return;
95     if (!nameDialog) initEditDialog();
96     deleteCurrentObject();
97     dbObject = dbManager->getDataObjectFromModel(&index);
98     nameDialog->setDataObject(dbObject);
99     activateNameDialog();
100 }
101
102 bool DbObjectDialog::deleteItem()
103 {
104     QModelIndex index = objectList->currentIndex();
105     if (!index.isValid()) return false;
106
107     qDebug() << "DbObjectDialog going to delete item at row " << index.row();
108
109     try
110     {
111         EmuFrontObject *ob = dbManager->getDataObjectFromModel(&index);
112
113         if (!ob) return false;
114         qDebug() << "DbObjectDialog going to delete item" << ob->getName();
115
116         int numBindings = dbManager->countDataObjectRefs(ob->getId());
117
118         qDebug() << "Got " << numBindings << " bindings.";
119
120         if (numBindings > 0 && !confirmDelete(ob->getName(), numBindings))
121         { return false; }
122         deleteCurrentObject();
123
124         qDebug() << "Deleted object from memory, going to delete from db.";
125
126         bool delOk = dbManager->deleteDataObjectFromModel(&index);
127         if (!delOk)
128         {
129             qDebug() << "delete failed";
130             return false;
131         }
132         qDebug() << "Object deleted from database";
133         updateList();
134         objectList->setFocus();
135     }
136     catch(EmuFrontException e)
137     {
138         errorMessage->showMessage(e.what());
139     }
140     return false;
141 }
142
143 void DbObjectDialog::updateDb(const EmuFrontObject *ob) const
144 {
145     if (!ob) return;
146     qDebug() << "Updating platform " << ob->getName();
147     if ( dbManager->updateDataObjectToModel(ob) )
148     { qDebug() << "Db update ok!"; }
149     else qDebug() << "Db update failed";
150
151 }
152
153 void DbObjectDialog::updateList() const
154 {
155     if (!dbManager) return;
156     qDebug() << "Going to reset the data model";
157     dbManager->resetModel();
158 }
159
160 void DbObjectDialog::addButtonClicked()
161 {
162     disableSelection();
163     addObject();
164 }
165
166 void DbObjectDialog::editButtonClicked()
167 {
168     disableSelection();
169     editObject();
170 }
171
172 void DbObjectDialog::deleteButtonClicked()
173 {
174     QItemSelectionModel *selModel = objectList->selectionModel();
175     if (!selModel->hasSelection()) return;
176
177     QAbstractItemModel *tblModel = objectList->model();
178     QModelIndex index = selModel->currentIndex();
179     QVariant vName = tblModel->data(index);
180     QString name = vName.toString();
181     disableSelection();
182
183     QString msg =  tr("Do you want to delete") + name + "?";
184     int yn = QMessageBox::question(this, "Confirm", msg, QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
185     if (yn == QMessageBox::Yes)
186     {
187         qDebug() << "Deleting item..." << name << ".";
188         deleteItem();
189     }
190 }
191
192 void DbObjectDialog::layout()
193 {
194     QHBoxLayout *mainLayout = new QHBoxLayout;
195     mainLayout->addWidget(objectList);
196     mainLayout->addWidget(buttonBox);
197     setLayout(mainLayout);
198 }
199
200 void DbObjectDialog::listObjectClicked(const QModelIndex &index)
201 {
202     const QModelIndex *x;
203     x = &index;
204     qDebug() << "Row " << x->row() << ", column " << x->column() << " clicked.";
205     setButtonsEnabled(index.isValid());
206     if(!index.isValid()) 
207         return;
208 }
209
210 void DbObjectDialog::setButtonsEnabled(bool enabled)
211 {
212     editButton->setEnabled(enabled);
213     deleteButton->setEnabled(enabled);
214 }
215
216 void DbObjectDialog::disableSelection()
217 {
218     setButtonsEnabled(false);
219 }
220
221 void DbObjectDialog::activateNameDialog()
222 {
223     if (!nameDialog) return;
224     nameDialog->show();
225     nameDialog->raise();
226     nameDialog->activateWindow();
227 }
228
229 void DbObjectDialog::initDataTable()
230 {
231    objectList->setModel(dbManager->getDataModel());
232    objectList->setSelectionMode(QAbstractItemView::SingleSelection);
233    objectList->resizeColumnsToContents();
234 }
235
236 void DbObjectDialog::updateReject()
237 {
238     qDebug() << "Update rejected ... going to delete current object.";
239     // we don't want to keep this in memory
240     deleteCurrentObject();
241 }
242
243 void DbObjectDialog::updateData()
244 {
245     qDebug() << "Update accepted.";
246     // update data model
247     if (!dbObject) return;
248
249     qDebug() << "dbObject is not 0";
250
251     qDebug() << "We have a " + dbObject->getName();
252
253     qDebug() << "Data will be inserted/updated...";
254
255     // if data object id > -1 we are updating the data otherwise we are inserting new data
256     if (dbObject->getId() > -1) updateDb(dbObject);
257     else insertDb(dbObject);
258
259     // we don't need dbObject anymore
260     deleteCurrentObject();
261     dbObject = 0;
262     updateList();
263 }
264
265 void DbObjectDialog::deleteCurrentObject()
266 {
267     delete dbObject;
268 }
269
270 bool DbObjectDialog::confirmDelete(QString name, int numRefs)
271 {
272     int r = QMessageBox::warning(this, tr("Confirm delete"),
273                                  QString("Do you really want to delete %1 with %2 data bindings?")
274                                  .arg(name).arg(numRefs),
275                                  QMessageBox::Yes | QMessageBox::No);
276     if ( r == QMessageBox::No )
277         return false;
278     return true;
279 }