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"
10 FileUtil::FileUtil(QObject *parent) : QObject(parent)
13 // TODO: fp is missing setup object reference!
14 QList<MediaImageContainer*> FileUtil::scanFilePath(const FilePathObject *fp, QStringList filters)
16 QList<MediaImageContainer*> containers;
17 QDir dir(fp->getName());
18 if (!dir.exists() || !dir.isReadable())
19 throw EmuFrontException(tr("Directory %1 doesn't exists or isn't readable!").arg(fp->getName()));
21 dir.setFilter(QDir::Files | QDir::NoSymLinks | QDir::NoDotAndDotDot | QDir::Readable);
23 if (filters.count() > 0) dir.setNameFilters(filters);
25 // we'll go through the filtered archive files...
26 QFileInfoList list = dir.entryInfoList();
27 for (int i = 0; i < list.size(); ++i)
29 QFileInfo fileInfo = list.at(i);
30 qDebug() << QString("%1 %2").arg(fileInfo.size(), 10).arg(fileInfo.absoluteFilePath());
32 //... and collect the contents of each archive
33 QList<MediaImage*> files = listContents(fileInfo.absoluteFilePath(), fp);
35 if (files.count() > 0)
37 MediaImageContainer *con = new MediaImageContainer
43 // TODO: is it guaranteed, that the file path object containing the setup object remains alive
44 // the whole lifecycle of (this) media image container object?
45 // * if we assign a copy of the setup object -> waste of memory and time
46 // * this function is designed to be used from media image path main dialog
47 // where we can ensure the lifecycle of file path object -> maybe move the implementation there!?
50 containers.append(con);
56 QList<MediaImage*> FileUtil::listContents(const QString filePath, const FilePathObject *fp)
60 UnZip::ErrorCode ec = uz.openArchive(filePath);
62 throw EmuFrontException(tr("Error while opening zip-file %1, error code %2").arg(filePath).arg(ec));
64 Setup *sup = fp->getSetup();
65 QList<UnZip::ZipEntry> list = uz.entryList();
66 QList<MediaImage*> fileList;
67 foreach(UnZip::ZipEntry entry, list)
69 if (isSupportedFile(entry.filename, sup->getSupportedFileTypeExtensions()))
71 QString checksum = QString("%1").arg(entry.crc32, 0, 16);
72 MediaImage *effo = new MediaImage(entry.filename,
73 checksum, entry.uncompressedSize);
82 bool FileUtil::isSupportedFile(const QString filename, const QStringList supportedFileExtensions)
84 QString ext = filename.section('.', -1);
85 return supportedFileExtensions.contains(ext, Qt::CaseInsensitive);