Case insensitive comparison of the supported file type extensions.
[emufront] / src / utils / fileutil.cpp
1 #include <QDir>
2 #include <QDebug>
3 #include "fileutil.h"
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
10 FileUtil::FileUtil(QObject *parent) : QObject(parent)
11 {}
12
13 // TODO: fp is missing setup object reference!
14 QList<MediaImageContainer*> FileUtil::scanFilePath(const FilePathObject *fp, QStringList filters)
15 {
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()));
20
21     dir.setFilter(QDir::Files | QDir::NoSymLinks | QDir::NoDotAndDotDot | QDir::Readable);
22
23     if (filters.count() > 0) dir.setNameFilters(filters);
24
25     // we'll go through the filtered archive files...
26     QFileInfoList list = dir.entryInfoList();
27     for (int i = 0; i < list.size(); ++i)
28     {
29         QFileInfo fileInfo = list.at(i);
30         qDebug() << QString("%1 %2").arg(fileInfo.size(), 10).arg(fileInfo.absoluteFilePath());
31
32         //... and collect the contents of each archive
33         QList<MediaImage*> files = listContents(fileInfo.absoluteFilePath(), fp);
34
35         if (files.count() > 0)
36         {
37             MediaImageContainer *con = new MediaImageContainer
38                 (
39                     fileInfo.fileName(),
40                     "" /* TODO */,
41                     fileInfo.size(),
42                     files,
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!?
48                     fp->getSetup()
49                 );
50             containers.append(con);
51         }
52     }
53     return containers;
54 }
55
56 QList<MediaImage*> FileUtil::listContents(const QString filePath, const FilePathObject *fp)
57 {
58
59     UnZip uz;
60     UnZip::ErrorCode ec = uz.openArchive(filePath);
61     if (ec != UnZip::Ok)
62         throw EmuFrontException(tr("Error while opening zip-file %1, error code %2").arg(filePath).arg(ec));
63
64     Setup *sup = fp->getSetup();
65     QList<UnZip::ZipEntry> list = uz.entryList();
66     QList<MediaImage*>  fileList;
67     foreach(UnZip::ZipEntry entry, list)
68     {
69         if (isSupportedFile(entry.filename, sup->getSupportedFileTypeExtensions()))
70         {
71             QString checksum = QString("%1").arg(entry.crc32, 0, 16);
72             MediaImage *effo = new MediaImage(entry.filename,
73                 checksum, entry.uncompressedSize);
74             fileList << effo;
75         }
76     }
77
78     return fileList;
79
80 }
81
82 bool FileUtil::isSupportedFile(const QString filename, const QStringList supportedFileExtensions)
83 {
84     QString ext = filename.section('.', -1);
85     return supportedFileExtensions.contains(ext, Qt::CaseInsensitive);
86 }