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