Exception handling and cleaning dynamic objects.
[emufront] / src / utils / fileutil.cpp
index b5d96fb..0cf3cd1 100644 (file)
@@ -20,9 +20,9 @@
 #include <QDir>
 #include <QDebug>
 #include <QProcess>
+#include <QProgressDialog>
 #include "fileutil.h"
 #include "zlib.h" /* crc32 */
-//#include "OSDaB-Zip/unzip.h"
 #include "../exceptions/emufrontexception.h"
 #include "../dataobjects/setup.h"
 #include "../dataobjects/mediaimage.h"
@@ -32,8 +32,6 @@
 #include "../db/dbmediaimagecontainer.h"
 #include "unziphelper.h"
 
-//int FileUtil::MIC_BUFFER_SIZE = 50;
-
 FileUtil::FileUtil(QObject *parent) : QObject(parent)
 {
     buf = new char[READ_BUFFER];
@@ -46,7 +44,9 @@ FileUtil::~FileUtil()
 }
 
 /* Throws EmuFrontException */
-int FileUtil::scanFilePath(FilePathObject *fp, QStringList filters, DbMediaImageContainer *dbMic)
+int FileUtil::scanFilePath(FilePathObject *fp,
+    QStringList filters, DbMediaImageContainer *dbMic,
+    QProgressDialog &progressDialog)
 {
     if (!fp->getSetup()){
         throw EmuFrontException(tr("Setup not available with %1.").arg(fp->getName()));
@@ -56,34 +56,37 @@ int FileUtil::scanFilePath(FilePathObject *fp, QStringList filters, DbMediaImage
             .arg(fp->getSetup()->getName()));
     }
     else if (!fp->getSetup()->getMediaType()){
-        throw new EmuFrontException(tr("No media type available with %1.")
+        throw EmuFrontException(tr("No media type available with %1.")
             .arg(fp->getSetup()->getName()));
     }
 
-    // TODO: if this is a rescan of this filepath remove all the old entries first!
-
     int count = 0;
-    qDebug() << QString("We have a platform %1, media type %2")
+    /*qDebug() << QString("We have a platform %1, media type %2")
         .arg(fp->getSetup()->getPlatform()->getName())
-        .arg(fp->getSetup()->getMediaType()->getName());
+        .arg(fp->getSetup()->getMediaType()->getName());*/
     QDir dir(fp->getName());
     if (!dir.exists() || !dir.isReadable())
         throw EmuFrontException(tr("Directory %1 doesn't exists or isn't readable!").arg(fp->getName()));
 
-    qDebug() << QString("Scanning directory %1.").arg(fp->getName());
+    //qDebug() << QString("Scanning directory %1.").arg(fp->getName());
     dir.setFilter(QDir::Files | QDir::NoSymLinks | QDir::NoDotAndDotDot | QDir::Readable);
 
     if (filters.count() > 0) dir.setNameFilters(filters);
 
     // we'll go through the filtered archive files...
     QFileInfoList list = dir.entryInfoList();
-    // TODO: only a buffer of objects should be kept here,
-    // and write to database each time the buffer is filled.
+    //qDebug() << "We have " << list.count() << " files to go through.";
     QList<MediaImageContainer*> containers;
+    try {
+    progressDialog.setMinimum(0);
+    progressDialog.setMaximum(list.size());
     for (int i = 0; i < list.size(); ++i)
     {
+        progressDialog.setValue(i);
+        if (progressDialog.wasCanceled())
+            break;
         QFileInfo fileInfo = list.at(i);
-        qDebug() << QString("%1 %2").arg(fileInfo.size(), 10).arg(fileInfo.absoluteFilePath());
+        //qDebug() << QString("%1 %2").arg(fileInfo.size(), 10).arg(fileInfo.absoluteFilePath());
 
         //... and collect the contents of each archive
         QMap<QString, EmuFrontObject*> files = unzipHelper->listContents(fileInfo.absoluteFilePath(), fp);
@@ -102,38 +105,47 @@ int FileUtil::scanFilePath(FilePathObject *fp, QStringList filters, DbMediaImage
                 );
             containers.append(con);
             ++count;
-            qDebug() << "We have " << containers.count() << " containers.";
+            //qDebug() << "We have " << containers.count() << " containers.";
 
             if (containers.count() >= MIC_BUFFER_SIZE)  {
-                qDebug() << "We have " << containers.count()
-                    << " containers .. storing to db.";
+                //qDebug() << "We have " << containers.count() << " containers .. storing to db.";
                 dbMic->storeContainers(containers, fp);
                 qDeleteAll(containers);
                 containers.clear();
-                qDebug() << "containers now: " << containers.count();
+                //qDebug() << "containers now: " << containers.count();
             }
-            qDebug() << "We have " << containers.size() << " containers.";
+            //qDebug() << "We have " << containers.size() << " containers.";
+        } // files.count() > 0
+        else {
+            qDebug() << "No files from container " << fileInfo.absoluteFilePath();
         }
     }
+    progressDialog.setValue(list.size());
     if (containers.count() > 0) {
-        qDebug() << "Storing the rest " << containers.count() << " containers.";
+        //qDebug() << "Storing the rest " << containers.count() << " containers.";
         dbMic->storeContainers(containers, fp);
         qDeleteAll(containers);
         containers.clear();
 
     }
-    qDebug() << "Done scanning files!";
+    } catch (EmuFrontException &e) {
+        qDebug() << "Got exception " << e.what() << ", aborting & deleting created data objects.";
+        qDeleteAll(containers);
+        throw e;
+    }
+    //qDebug() << "Done scanning files!";
     return count;
 }
 
 /* Uses crc32 from zlib.h to count crc32 checksum value */
+/* Throws EmuFrontException */
 quint32 FileUtil::readCrc32(QString filePath)
 {
     // todo ... use some crc32 tool for this ... or maybe use md5 or something like that!!!
     QFile file(filePath);
-    qDebug() << "readCrc32: " << filePath;
+    //qDebug() << "readCrc32: " << filePath;
     if (!file.open(QIODevice::ReadOnly)) {
-        throw new EmuFrontException(QString(tr("Failed opening file %1 for reading the checksum!")).arg(filePath));
+        throw EmuFrontException(QString(tr("Failed opening file %1 for reading the checksum!")).arg(filePath));
     }
     quint32 crc = crc32(0L, Z_NULL, 0);
     int read = 0;
@@ -142,8 +154,8 @@ quint32 FileUtil::readCrc32(QString filePath)
     }
     file.close();
     if (crc <= 0)
-        throw new EmuFrontException(QString(tr("Failed reading crc checksum for file %1!")).arg(filePath));
-    qDebug() << QString("readCrc32, crc: %1").arg(crc, 0, 16);
+        throw EmuFrontException(QString(tr("Failed reading crc checksum for file %1!")).arg(filePath));
+    //qDebug() << QString("readCrc32, crc: %1").arg(crc, 0, 16);
     return crc;
 }