From d7ba5b4f76b39ecddfdf890f08a08c40a769a729 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Mikko=20Kein=C3=A4nen?= Date: Thu, 18 Nov 2010 23:02:48 +0200 Subject: [PATCH] Exception handling and cleaning dynamic objects. --- src/dialogs/mediaimagepathmaindialog.cpp | 9 +- src/emulauncher.cpp | 137 ++++++++++++++---------------- src/emulauncher.h | 4 +- src/mainwindow.cpp | 2 +- src/utils/emuhelper.cpp | 11 ++- src/utils/fileutil.cpp | 6 ++ 6 files changed, 89 insertions(+), 80 deletions(-) diff --git a/src/dialogs/mediaimagepathmaindialog.cpp b/src/dialogs/mediaimagepathmaindialog.cpp index 106bfff..325d6af 100644 --- a/src/dialogs/mediaimagepathmaindialog.cpp +++ b/src/dialogs/mediaimagepathmaindialog.cpp @@ -71,11 +71,13 @@ void MediaImagePathMainDialog::beginScanFilePath() return; } FileUtil fileUtil(this); + FilePathObject *fpo = 0; try { EmuFrontObject *ob = dbManager->getDataObjectFromModel(&index); if (!ob) return; - FilePathObject *fpo = dynamic_cast(ob); + fpo = dynamic_cast(ob); + if (!fpo) return; QStringList l; l << "*.zip"; // TODO set filters in a global constant class @@ -92,12 +94,13 @@ void MediaImagePathMainDialog::beginScanFilePath() QMessageBox msgBox; msgBox.setText(tr("Scanned %1 files to database.").arg(count)); msgBox.exec(); - delete fpo; } catch (EmuFrontException s) { errorMessage->showMessage( s.what() ); - } + } + delete fpo; + fpo = 0; } EmuFrontObject* MediaImagePathMainDialog::createObject() diff --git a/src/emulauncher.cpp b/src/emulauncher.cpp index 42a6f41..733a4d0 100644 --- a/src/emulauncher.cpp +++ b/src/emulauncher.cpp @@ -32,8 +32,8 @@ #include "utils/emuhelper.h" #include "dialogs/emufrontinputdialog.h" -EmuLauncher::EmuLauncher(QWidget *parent, QString tmp) : - QWidget(parent), tmpDirPath(tmp) +EmuLauncher::EmuLauncher(QErrorMessage *errorMessage, QWidget *parent, QString tmp) : + QWidget(parent), tmpDirPath(tmp), errorMessage(errorMessage) { dbPlatform = new DbPlatform(this); dbMediaType = new DbMediaType(this); @@ -166,13 +166,10 @@ void EmuLauncher::launchEmu() throw EmuFrontException(tr("Failed creating Emulator object!")); } - qDebug() << "File types; " << exe->getSetup()->getSupportedFileTypeExtensions().count(); bool mame = exe->getSetup()->getSupportedFileTypeExtensions().isEmpty(); - - if (mame && listMIndex.count() > 1) { throw EmuFrontException(tr("No supported file types configured for this emulator configuration. " "Assuming emulator support container files as is. " @@ -180,7 +177,6 @@ void EmuLauncher::launchEmu() )); } - // Now we have one or more media image containers and an emulator selected, // let's fetch the media image container data. @@ -206,37 +202,53 @@ void EmuLauncher::launchEmu() emuHelper->launch(exe, mediaImageContainers); return; } + else { + // mediaImageContainers list contains all the selected media image containers and + // mediaImages list contains all the media images inside all the selected containers - // mediaImageContainers list contains all the selected media image containers and - // mediaImages list contains all the media images inside all the selected containers - - - QList selectedImages; - if (mediaImages.count() < 1) { - throw EmuFrontException("No media images available!"); - } + QList selectedImages; + if (mediaImages.count() < 1) { + throw EmuFrontException("No media images available!"); + } - // check if command options have slots for nr media images > 1 e.g. "-diska $1 -diskb $2 ..." - QString opts = exe->getOptions(); - QRegExp rx("(\\$\\d+)"); - QStringList list; - int pos = 0; - while ((pos = rx.indexIn(opts, pos)) != -1) { - list << rx.cap(1); - pos += rx.matchedLength(); - } - bool ok; + // check if command options have slots for nr media images > 1 e.g. "-diska $1 -diskb $2 ..." + QString opts = exe->getOptions(); + QRegExp rx("(\\$\\d+)"); + QStringList list; + int pos = 0; + while ((pos = rx.indexIn(opts, pos)) != -1) { + list << rx.cap(1); + pos += rx.matchedLength(); + } + bool ok; - if (list.count() > mediaImages.count()) { - throw EmuFrontException(tr("Select %1 media images for this emulator configuration").arg(list.count())); - } - if (list.count() > 1) { - // more than one placeholder for media image in the command line ($1, $2, ...) - int lim = list.count() == mediaImages.count() ? list.count() - 1 : list.count(); - // user sets the order of media images - for(int i = 0; i < lim; i++) { + if (list.count() > mediaImages.count()) { + throw EmuFrontException(tr("Select %1 media images for this emulator configuration").arg(list.count())); + } + if (list.count() > 1) { + // more than one placeholder for media image in the command line ($1, $2, ...) + int lim = list.count() == mediaImages.count() ? list.count() - 1 : list.count(); + // user sets the order of media images + for(int i = 0; i < lim; i++) { + EmuFrontObject *efo = EmuFrontInputDialog::getItem( + this, tr("Select image no. %1").arg(i+1), tr("Select"), mediaImages.values(), 0, false, &ok); + if (!ok) { + throw EmuFrontException(tr("Boot image selection was canceled, aborting.")); + } + selectedImages << efo; + MediaImage *mi = dynamic_cast(efo); + QString key = mi->getCheckSum(); + mediaImages.remove(key); + } + // there should be at least one media image left in mediaImages map... + /*if (mediaImages.count() == 1) { + selectedImages << mediaImages.values().first(); + } ... this is added later-> */ + } + else if (mediaImages.count() > 1) { + // show select boot image dialog EmuFrontObject *efo = EmuFrontInputDialog::getItem( - this, tr("Select image no. %1").arg(i+1), tr("Select"), mediaImages.values(), 0, false, &ok); + this, tr("Select boot image"), tr("Select"), mediaImages.values(), 0, false, &ok); if (!ok) { throw EmuFrontException(tr("Boot image selection was canceled, aborting.")); } @@ -245,50 +257,33 @@ void EmuLauncher::launchEmu() QString key = mi->getCheckSum(); mediaImages.remove(key); } - // there should be at least one media image left in mediaImages map... - /*if (mediaImages.count() == 1) { - selectedImages << mediaImages.values().first(); - } ... this is added later-> */ - } - else if (mediaImages.count() > 1) { - // show select boot image dialog - EmuFrontObject *efo = EmuFrontInputDialog::getItem( - this, tr("Select boot image"), tr("Select"), mediaImages.values(), 0, false, &ok); - if (!ok) { - throw EmuFrontException(tr("Boot image selection was canceled, aborting.")); + else if (mediaImages.count() == 1) { + EmuFrontObject *efo = mediaImages.values().first(); + selectedImages << efo; + MediaImage *mi = dynamic_cast(efo); + QString key = mi->getCheckSum(); + mediaImages.remove(key); } - selectedImages << efo; - MediaImage *mi = dynamic_cast(efo); - QString key = mi->getCheckSum(); - mediaImages.remove(key); - } - else if (mediaImages.count() == 1) { - EmuFrontObject *efo = mediaImages.values().first(); - selectedImages << efo; - MediaImage *mi = dynamic_cast(efo); - QString key = mi->getCheckSum(); - mediaImages.remove(key); - } - // in all the both cases the (ordered) list of media images will be passed to emuHelper + // in all the both cases the (ordered) list of media images will be passed to emuHelper - // wee also keep the rest of the mediaimages in the selected containers for reference! - foreach(EmuFrontObject *efo, mediaImages) { - selectedImages << efo; - } + // wee also keep the rest of the mediaimages in the selected containers for reference! + foreach(EmuFrontObject *efo, mediaImages) { + selectedImages << efo; + } - if (selectedImages.count() < 1) - throw EmuFrontException(tr("No media images selected")); + if (selectedImages.count() < 1) + throw EmuFrontException(tr("No media images selected")); - emuHelper->launch(exe, mediaImageContainers, selectedImages, list.count(), tmpDirPath); - micTable->clearSelection(); + emuHelper->launch(exe, mediaImageContainers, selectedImages, list.count(), tmpDirPath); + } } catch (EmuFrontException efe) { - if (exe) delete exe; - qDeleteAll(mediaImageContainers); - //qDeleteAll(mediaImages); these are already deleted along with containers - QMessageBox::information(this, tr("Launching emulator"), - efe.what(), QMessageBox::Ok); - return; + errorMessage->showMessage(efe.what()); } + + micTable->clearSelection(); + if (exe) delete exe; + qDeleteAll(mediaImageContainers); + //qDeleteAll(mediaImages); these are already deleted along with containers } void EmuLauncher::processError(QProcess::ProcessError e) diff --git a/src/emulauncher.h b/src/emulauncher.h index 1e88136..4998240 100644 --- a/src/emulauncher.h +++ b/src/emulauncher.h @@ -25,6 +25,7 @@ class QTableView; class QPushButton; +class QErrorMessage; class DbMediaType; class DbPlatform; class DbMediaImageContainer; @@ -39,7 +40,7 @@ class EmuLauncher : public QWidget { Q_OBJECT public: - EmuLauncher(QWidget *parent = 0, QString tmp = "."); + EmuLauncher(QErrorMessage *, QWidget *parent = 0, QString tmp = "."); ~EmuLauncher(); void updateData(); void setTmpDirPath(QString); @@ -72,6 +73,7 @@ private: void launch(const Executable*, const MediaImageContainer*); void cleanTmp(); QString tmpDirPath; + QErrorMessage *errorMessage; }; #endif // EMULAUNCHER_H diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 91dd252..5354d83 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -50,7 +50,7 @@ MainWindow::MainWindow(bool reset) if (tmpDirFilePath.isEmpty()) tmpDirFilePath = QDir::homePath(); qDebug() << "Temporary dir is " << tmpDirFilePath; - launcher = new EmuLauncher(this, tmpDirFilePath); + launcher = new EmuLauncher(errorMessage, this, tmpDirFilePath); setCentralWidget(launcher); createActions(); createMenus(); diff --git a/src/utils/emuhelper.cpp b/src/utils/emuhelper.cpp index 6c06a2a..3307ac5 100644 --- a/src/utils/emuhelper.cpp +++ b/src/utils/emuhelper.cpp @@ -63,8 +63,9 @@ void EmuHelper::launch(const Executable * ex, QList micLi // for the moment, we'll wait for the process to be finished until we continue waitForFinished(-1); - delete ex; - qDeleteAll(micList); + // these will be deleted in calling function: + //delete ex; + //qDeleteAll(micList); } /* Throws EmuFrontException */ @@ -137,9 +138,11 @@ void EmuHelper::launch(const Executable * ex, QList micLi } } catch (EmuFrontException e) { qDebug() << e.what(); + throw e; } - delete ex; - qDeleteAll(micList); + // these will be deleted in calling function: + //delete ex; + //qDeleteAll(micList);delete ex; //qDeleteAll(miList); these objects are already deleted along with micList } diff --git a/src/utils/fileutil.cpp b/src/utils/fileutil.cpp index 6ab5334..0cf3cd1 100644 --- a/src/utils/fileutil.cpp +++ b/src/utils/fileutil.cpp @@ -77,6 +77,7 @@ int FileUtil::scanFilePath(FilePathObject *fp, QFileInfoList list = dir.entryInfoList(); //qDebug() << "We have " << list.count() << " files to go through."; QList containers; + try { progressDialog.setMinimum(0); progressDialog.setMaximum(list.size()); for (int i = 0; i < list.size(); ++i) @@ -127,6 +128,11 @@ int FileUtil::scanFilePath(FilePathObject *fp, containers.clear(); } + } catch (EmuFrontException &e) { + qDebug() << "Got exception " << e.what() << ", aborting & deleting created data objects."; + qDeleteAll(containers); + throw e; + } //qDebug() << "Done scanning files!"; return count; } -- 1.7.9.5