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"
12 FileUtil::FileUtil(QObject *parent) : QObject(parent)
15 QList<MediaImageContainer*> FileUtil::scanFilePath(const FilePathObject *fp, QStringList filters)
18 throw EmuFrontException(tr("Setup not available with %1.").arg(fp->getName()));
20 else if(!fp->getSetup()->getPlatform()){
21 throw EmuFrontException(tr("No platform object available with %1.")
22 .arg(fp->getSetup()->getName()));
24 else if (!fp->getSetup()->getMediaType()){
25 throw new EmuFrontException(tr("No media type available with %1.")
26 .arg(fp->getSetup()->getName()));
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()));
36 qDebug() << QString("Scanning directory %1.").arg(fp->getName());
37 dir.setFilter(QDir::Files | QDir::NoSymLinks | QDir::NoDotAndDotDot | QDir::Readable);
39 if (filters.count() > 0) dir.setNameFilters(filters);
41 // we'll go through the filtered archive files...
42 QFileInfoList list = dir.entryInfoList();
43 for (int i = 0; i < list.size(); ++i)
45 QFileInfo fileInfo = list.at(i);
46 qDebug() << QString("%1 %2").arg(fileInfo.size(), 10).arg(fileInfo.absoluteFilePath());
48 //... and collect the contents of each archive
49 QList<MediaImage*> files = listContents(fileInfo.absoluteFilePath(), fp);
51 if (files.count() > 0)
53 MediaImageContainer *con = new MediaImageContainer
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!!!
67 containers.append(con);
68 qDebug() << "We have " << containers.size() << " containers.";
71 qDebug() << "Done scanning files!";
75 QList<MediaImage*> FileUtil::listContents(const QString filePath, const FilePathObject *fp)
79 UnZip::ErrorCode ec = uz.openArchive(filePath);
81 throw EmuFrontException(tr("Error while opening zip-file %1, error code %2").arg(filePath).arg(ec));
84 throw EmuFrontException(tr("Setup not available with %1.").arg(fp->getName()));
87 Setup *sup = fp->getSetup();
88 QList<UnZip::ZipEntry> list = uz.entryList();
89 QList<MediaImage*> fileList;
90 foreach(UnZip::ZipEntry entry, list)
92 qDebug() << "Zip entry " << entry.filename;
93 if (isSupportedFile(entry.filename, sup->getSupportedFileTypeExtensions()))
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);
103 qDebug() << "File list has " << fileList.size() << " entries.";
108 bool FileUtil::isSupportedFile(const QString filename, const QStringList supportedFileExtensions)
110 QString ext = filename.section('.', -1);
111 return supportedFileExtensions.contains(ext, Qt::CaseInsensitive);