Fixed bugs: dialogs remained disabled (signals were not attached to
[emufront] / src / dialogs / mediaimagepathmaindialog.cpp
index e7f1836..1ba8a02 100644 (file)
@@ -5,41 +5,43 @@
 //
 //
 // EmuFront is free software: you can redistribute it and/or modify
-// it under the terms of the GNU General Public License as published by
-// the Free Software Foundation, either version 3 of the License, or
-// (at your option) any later version.
+// it under the terms of the GNU General Public License version 2 as published by
+// the Free Software Foundation and appearing in the file gpl.txt included in the
+// packaging of this file.
 //
-// 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 "../dataobjects/filepathobject.h"
+#include "../dataobjects/emufrontfileobject.h"
 #include "../db/dbfilepath.h"
+#include "../utils/fileutil.h"
 #include "mediaimagepathmaindialog.h"
 #include "mediaimagepathdialog.h"
 
 MediaImagePathMainDialog::MediaImagePathMainDialog(QWidget *parent)
     : DbObjectDialog(parent)
 {
-    qDebug() << "MediaImagePathMainDialog";
     setWindowTitle(tr("Set media image paths"));
-    qDebug() << "Creating MediaImagePathDialog";
-    nameDialog = new MediaImagePathDialog(this, dynamic_cast<FilePathObject*>(dbObject));
-    qDebug() << "Creating DbFilePath";
     dbManager = new DbFilePath(this);
-    qDebug() << "Initializing data table";
+    dbMediaImageContainer = new DbMediaImageContainer(this);
     initDataTable();
 
     scanButton = new QPushButton(tr("&Scan"));
     buttonBox->addButton(scanButton, QDialogButtonBox::ActionRole);
 
-    qDebug() << "Connecting signals";
+    initEditDialog();
+    hiddenColumns << DbFilePath::FilePath_Id;
+    hiddenColumns << DbFilePath::FilePath_SetupId;
+    hideColumns();
+
     // do not move to parent class:
     connectSignals();
 }
@@ -50,50 +52,57 @@ void MediaImagePathMainDialog::connectSignals()
     connect(scanButton, SIGNAL(clicked()), this, SLOT(beginScanFilePath()));
 }
 
+void MediaImagePathMainDialog::initEditDialog()
+{
+    nameDialog = new MediaImagePathDialog(this, dynamic_cast<FilePathObject*>(dbObject));
+    connectNameDialogSignals();
+}
+
 void MediaImagePathMainDialog::beginScanFilePath()
 {
-    qDebug() << "Scan file path requested";
     QModelIndex index = objectList->currentIndex();
     if (!index.isValid()) return;
+    if (QMessageBox::question(this,
+        tr("Confirm"),
+        tr("Do you want to continue? "
+        "If you have tons of huge files this may take even hours! "
+        "If you are low on battery power, consider carefully!"),
+        QMessageBox::Yes, QMessageBox::No, QMessageBox::NoButton ) == QMessageBox::No) {
+            return;
+        }
+    FileUtil fileUtil(this);
     EmuFrontObject *ob = dbManager->getDataObjectFromModel(&index);
     if (!ob) return;
     FilePathObject *fpo = dynamic_cast<FilePathObject*>(ob);
     try
     {
         QStringList l;
-        scanFilePath(fpo->getName(), l); // TODO implement file filters
+        l << "*.zip"; // TODO set filters in a global constant class
+
+        dbMediaImageContainer->removeFromFilePath(fpo->getId());
+
+        QProgressDialog progressDialog(this);
+        progressDialog.setWindowTitle("Scanning files...");
+        progressDialog.setCancelButtonText("Abort");
+        progressDialog.setWindowModality(Qt::WindowModal);
+        progressDialog.show();
+
+        int count = fileUtil.scanFilePath(fpo, l, dbMediaImageContainer, progressDialog);
+        progressDialog.hide();
+        QMessageBox msgBox;
+        msgBox.setText(tr("Scanned %1 files to database.").arg(count));
+        msgBox.exec();
+        delete fpo;
     }
-    catch (QString s)
+    catch (EmuFrontException s)
     {
-        QMessageBox::warning(this, tr("Warning"), s, QMessageBox::Ok);
-    }
-}
-
-void MediaImagePathMainDialog::scanFilePath(const QString fp, const QStringList filters)
-{
-    qDebug() << "Will scan file path " << fp;
-    QDir dir(fp);
-    if (!dir.exists() || !dir.isReadable())
-        throw QString(tr("Directory %1 doesn't exists or isn't readable!").arg(fp));
-
-    dir.setFilter(QDir::Files | QDir::NoSymLinks | QDir::NoDotAndDotDot | QDir::Readable);
-
-    if (filters.count() > 0) dir.setNameFilters(filters);
-
-    QFileInfoList list = dir.entryInfoList();
-    for (int i = 0; i < list.size(); ++i)
-    {
-        QFileInfo fileInfo = list.at(i);
-        qDebug() << QString("%1 %2").arg(fileInfo.size(), 10).arg(fileInfo.absoluteFilePath());
-
-        QFile f(fileInfo.absoluteFilePath());
-
-    }
+        errorMessage->showMessage( s.what() );
+   }
 }
 
 EmuFrontObject* MediaImagePathMainDialog::createObject()
 {
-    return new FilePathObject;
+    return new FilePathObject(FilePathObject::FilePathType_MediaImageDir);
 }
 
 MediaImagePathMainDialog::~MediaImagePathMainDialog()
@@ -103,6 +112,22 @@ MediaImagePathMainDialog::~MediaImagePathMainDialog()
 
 void MediaImagePathMainDialog::deleteCurrentObject()
 {
-    delete dynamic_cast<FilePathObject*>(dbObject);
-    dbObject = 0;
+    if (dbObject) {
+        FilePathObject* fpo =  dynamic_cast<FilePathObject*>(dbObject);
+        if (fpo) delete fpo;
+        else qDebug() << "Failed deleteing FilePathObject";
+        dbObject = 0;
+    }
+}
+
+void MediaImagePathMainDialog::cleanUp()
+{
+    deleteCurrentObject();
+    if (nameDialog) {
+        MediaImagePathDialog *pnd =
+            dynamic_cast<MediaImagePathDialog*>(nameDialog);
+        if (pnd) delete pnd;
+        else qDebug() << "Failed to delete MediaImagePathDialog";
+        nameDialog = 0;
+    }
 }