Tracking down error with storing media image files to database.
[emufront] / src / utils / fileutil.cpp
1 #include <QDir>
2 #include <QDebug>
3 #include "fileutil.h"
4 #include "OSDaB-Zip/unzip.h"
5 #include "../exceptions/emufrontexception.h"
6 #include "../dataobjects/setup.h"
7 #include "../dataobjects/mediaimage.h"
8 #include "../dataobjects/mediaimagecontainer.h"
9 #include "../dataobjects/mediatype.h"
10 #include "../dataobjects/platform.h"
11
12 FileUtil::FileUtil(QObject *parent) : QObject(parent)
13 {}
14
15 QList<MediaImageContainer*> FileUtil::scanFilePath(const FilePathObject *fp, QStringList filters)
16 {
17     if (!fp->getSetup()){
18         throw EmuFrontException(tr("Setup not available with %1.").arg(fp->getName()));
19     }
20     else if(!fp->getSetup()->getPlatform()){
21         throw EmuFrontException(tr("No platform object available with %1.")
22             .arg(fp->getSetup()->getName()));
23     }
24     else if (!fp->getSetup()->getMediaType()){
25         throw new EmuFrontException(tr("No media type available with %1.")
26             .arg(fp->getSetup()->getName()));
27     }
28     qDebug() << QString("We have a platform %1, media type %2")
29         .arg(fp->getSetup()->getPlatform()->getName())
30         .arg(fp->getSetup()->getMediaType()->getName());
31     QList<MediaImageContainer*> containers;
32     QDir dir(fp->getName());
33     if (!dir.exists() || !dir.isReadable())
34         throw EmuFrontException(tr("Directory %1 doesn't exists or isn't readable!").arg(fp->getName()));
35
36     qDebug() << QString("Scanning directory %1.").arg(fp->getName());
37     dir.setFilter(QDir::Files | QDir::NoSymLinks | QDir::NoDotAndDotDot | QDir::Readable);
38
39     if (filters.count() > 0) dir.setNameFilters(filters);
40
41     // we'll go through the filtered archive files...
42     QFileInfoList list = dir.entryInfoList();
43     for (int i = 0; i < list.size(); ++i)
44     {
45         QFileInfo fileInfo = list.at(i);
46         qDebug() << QString("%1 %2").arg(fileInfo.size(), 10).arg(fileInfo.absoluteFilePath());
47
48         //... and collect the contents of each archive
49         QList<MediaImage*> files = listContents(fileInfo.absoluteFilePath(), fp);
50
51         if (files.count() > 0)
52         {
53             MediaImageContainer *con = new MediaImageContainer
54                 (
55                     fileInfo.fileName(),
56                     "" /* TODO */,
57                     fileInfo.size(),
58                     files,
59                     // TODO: is it guaranteed, that the file path object containing the setup object remains alive
60                     // the whole lifecycle of (this) media image container object?
61                     // * if we assign a copy of the setup object -> waste of memory and time
62                     // * this function is designed to be used from media image path main dialog
63                     //   where we can ensure the lifecycle of file path object -> maybe move the implementation there!?
64                     // TODO: Ensure this! We really need a reference instead of 1000s of copies of setup object!!!
65                     fp->getSetup()
66                 );
67             containers.append(con);
68             qDebug() << "We have " << containers.size() << " containers.";
69         }
70     }
71     qDebug() << "Done scanning files!";
72     return containers;
73 }
74
75 QList<MediaImage*> FileUtil::listContents(const QString filePath, const FilePathObject *fp)
76 {
77
78     UnZip uz;
79     UnZip::ErrorCode ec = uz.openArchive(filePath);
80     if (ec != UnZip::Ok)
81         throw EmuFrontException(tr("Error while opening zip-file %1, error code %2").arg(filePath).arg(ec));
82
83     if (!fp->getSetup()){
84         throw EmuFrontException(tr("Setup not available with %1.").arg(fp->getName()));
85     }
86
87     Setup *sup = fp->getSetup();
88     QList<UnZip::ZipEntry> list = uz.entryList();
89     QList<MediaImage*>  fileList;
90     foreach(UnZip::ZipEntry entry, list)
91     {
92         qDebug() << "Zip entry " << entry.filename;
93         if (isSupportedFile(entry.filename, sup->getSupportedFileTypeExtensions()))
94         {
95             QString checksum = QString("%1").arg(entry.crc32, 0, 16);
96             qDebug() << "Checksum " << checksum;
97             MediaImage *effo = new MediaImage(entry.filename,
98                 checksum, entry.uncompressedSize);
99             fileList << effo;
100         }
101     }
102
103     qDebug() << "File list has " << fileList.size() << " entries.";
104     return fileList;
105
106 }
107
108 bool FileUtil::isSupportedFile(const QString filename, const QStringList supportedFileExtensions)
109 {
110     QString ext = filename.section('.', -1);
111     return supportedFileExtensions.contains(ext, Qt::CaseInsensitive);
112 }