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 /* Throws EmuFrontException */
24 QList<MediaImageContainer*> FileUtil::scanFilePath(FilePathObject *fp, QStringList filters)
27 throw EmuFrontException(tr("Setup not available with %1.").arg(fp->getName()));
29 else if(!fp->getSetup()->getPlatform()){
30 throw EmuFrontException(tr("No platform object available with %1.")
31 .arg(fp->getSetup()->getName()));
33 else if (!fp->getSetup()->getMediaType()){
34 throw new EmuFrontException(tr("No media type available with %1.")
35 .arg(fp->getSetup()->getName()));
37 qDebug() << QString("We have a platform %1, media type %2")
38 .arg(fp->getSetup()->getPlatform()->getName())
39 .arg(fp->getSetup()->getMediaType()->getName());
40 QList<MediaImageContainer*> containers;
41 QDir dir(fp->getName());
42 if (!dir.exists() || !dir.isReadable())
43 throw EmuFrontException(tr("Directory %1 doesn't exists or isn't readable!").arg(fp->getName()));
45 qDebug() << QString("Scanning directory %1.").arg(fp->getName());
46 dir.setFilter(QDir::Files | QDir::NoSymLinks | QDir::NoDotAndDotDot | QDir::Readable);
48 if (filters.count() > 0) dir.setNameFilters(filters);
50 // we'll go through the filtered archive files...
51 QFileInfoList list = dir.entryInfoList();
52 for (int i = 0; i < list.size(); ++i)
54 QFileInfo fileInfo = list.at(i);
55 qDebug() << QString("%1 %2").arg(fileInfo.size(), 10).arg(fileInfo.absoluteFilePath());
57 //... and collect the contents of each archive
58 QList<MediaImage*> files = listContents(fileInfo.absoluteFilePath(), fp);
60 if (files.count() > 0)
62 quint32 crc = readCrc32(fileInfo.absoluteFilePath());
63 MediaImageContainer *con = new MediaImageContainer
66 QString("%1").arg(crc, 0, 16),
69 new FilePathObject(*fp)
71 containers.append(con);
72 qDebug() << "We have " << containers.size() << " containers.";
75 qDebug() << "Done scanning files!";
79 /* Uses crc32 from zlib.h to count crc32 checksum value */
80 quint32 FileUtil::readCrc32(QString filePath)
83 qDebug() << "readCrc32: " << filePath;
84 if (!file.open(QIODevice::ReadOnly)) {
85 throw new EmuFrontException(QString(tr("Failed opening file %1 for reading the checksum!")).arg(filePath));
87 quint32 crc = crc32(0L, Z_NULL, 0);
89 while((read = file.read(buf, READ_BUFFER))) {
90 crc = crc32(crc, (const Bytef*) buf, read);
94 throw new EmuFrontException(QString(tr("Failed reading crc checksum for file %1!")).arg(filePath));
95 qDebug() << QString("readCrc32, crc: %1").arg(crc, 0, 16);
99 QList<MediaImage*> FileUtil::listContents(const QString filePath, const FilePathObject *fp)
103 UnZip::ErrorCode ec = uz.openArchive(filePath);
105 throw EmuFrontException(tr("Error while opening zip-file %1, error code %2").arg(filePath).arg(ec));
107 if (!fp->getSetup()){
108 throw EmuFrontException(tr("Setup not available with %1.").arg(fp->getName()));
111 Setup *sup = fp->getSetup();
112 QList<UnZip::ZipEntry> list = uz.entryList();
113 QList<MediaImage*> fileList;
114 foreach(UnZip::ZipEntry entry, list)
116 qDebug() << "Zip entry " << entry.filename;
117 if (isSupportedFile(entry.filename, sup->getSupportedFileTypeExtensions()))
119 QString checksum = QString("%1").arg(entry.crc32, 0, 16);
120 qDebug() << "Checksum " << checksum;
121 MediaImage *effo = new MediaImage(entry.filename,
122 checksum, entry.uncompressedSize);
127 qDebug() << "File list has " << fileList.size() << " entries.";
132 bool FileUtil::isSupportedFile(const QString filename, const QStringList supportedFileExtensions)
134 QString ext = filename.section('.', -1);
135 return supportedFileExtensions.contains(ext, Qt::CaseInsensitive);