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"
13 FileUtil::FileUtil(QObject *parent) : QObject(parent)
15 buf = new char[READ_BUFFER];
23 QList<MediaImageContainer*> FileUtil::scanFilePath(FilePathObject *fp, QStringList filters)
26 throw EmuFrontException(tr("Setup not available with %1.").arg(fp->getName()));
28 else if(!fp->getSetup()->getPlatform()){
29 throw EmuFrontException(tr("No platform object available with %1.")
30 .arg(fp->getSetup()->getName()));
32 else if (!fp->getSetup()->getMediaType()){
33 throw new EmuFrontException(tr("No media type available with %1.")
34 .arg(fp->getSetup()->getName()));
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()));
44 qDebug() << QString("Scanning directory %1.").arg(fp->getName());
45 dir.setFilter(QDir::Files | QDir::NoSymLinks | QDir::NoDotAndDotDot | QDir::Readable);
47 if (filters.count() > 0) dir.setNameFilters(filters);
49 // we'll go through the filtered archive files...
50 QFileInfoList list = dir.entryInfoList();
51 for (int i = 0; i < list.size(); ++i)
53 QFileInfo fileInfo = list.at(i);
54 qDebug() << QString("%1 %2").arg(fileInfo.size(), 10).arg(fileInfo.absoluteFilePath());
56 //... and collect the contents of each archive
57 QList<MediaImage*> files = listContents(fileInfo.absoluteFilePath(), fp);
59 if (files.count() > 0)
61 quint32 crc = readCrc32(fileInfo.absoluteFilePath());
62 MediaImageContainer *con = new MediaImageContainer
65 QString("%1").arg(crc, 0, 16),
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!!!
77 containers.append(con);
78 qDebug() << "We have " << containers.size() << " containers.";
81 qDebug() << "Done scanning files!";
85 /* Uses crc32 from zlib.h to count crc32 checksum value */
86 quint32 FileUtil::readCrc32(QString 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));
93 quint32 crc = crc32(0L, Z_NULL, 0);
95 while((read = file.read(buf, READ_BUFFER))) {
96 crc = crc32(crc, (const Bytef*) buf, read);
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);
105 QList<MediaImage*> FileUtil::listContents(const QString filePath, const FilePathObject *fp)
109 UnZip::ErrorCode ec = uz.openArchive(filePath);
111 throw EmuFrontException(tr("Error while opening zip-file %1, error code %2").arg(filePath).arg(ec));
113 if (!fp->getSetup()){
114 throw EmuFrontException(tr("Setup not available with %1.").arg(fp->getName()));
117 Setup *sup = fp->getSetup();
118 QList<UnZip::ZipEntry> list = uz.entryList();
119 QList<MediaImage*> fileList;
120 foreach(UnZip::ZipEntry entry, list)
122 qDebug() << "Zip entry " << entry.filename;
123 if (isSupportedFile(entry.filename, sup->getSupportedFileTypeExtensions()))
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);
133 qDebug() << "File list has " << fileList.size() << " entries.";
138 bool FileUtil::isSupportedFile(const QString filename, const QStringList supportedFileExtensions)
140 QString ext = filename.section('.', -1);
141 return supportedFileExtensions.contains(ext, Qt::CaseInsensitive);