FilePath object is also deleted when deleting a MediaImageContainer
[emufront] / src / utils / fileutil.cpp
1 #include <QDir>
2 #include <QDebug>
3 #include "fileutil.h"
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"
12
13 FileUtil::FileUtil(QObject *parent) : QObject(parent)
14 {
15     buf = new char[READ_BUFFER];
16 }
17
18 FileUtil::~FileUtil()
19 {
20     delete[] buf;
21 }
22
23 /* Throws EmuFrontException */
24 QList<MediaImageContainer*> FileUtil::scanFilePath(FilePathObject *fp, QStringList filters)
25 {
26     if (!fp->getSetup()){
27         throw EmuFrontException(tr("Setup not available with %1.").arg(fp->getName()));
28     }
29     else if(!fp->getSetup()->getPlatform()){
30         throw EmuFrontException(tr("No platform object available with %1.")
31             .arg(fp->getSetup()->getName()));
32     }
33     else if (!fp->getSetup()->getMediaType()){
34         throw new EmuFrontException(tr("No media type available with %1.")
35             .arg(fp->getSetup()->getName()));
36     }
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()));
44
45     qDebug() << QString("Scanning directory %1.").arg(fp->getName());
46     dir.setFilter(QDir::Files | QDir::NoSymLinks | QDir::NoDotAndDotDot | QDir::Readable);
47
48     if (filters.count() > 0) dir.setNameFilters(filters);
49
50     // we'll go through the filtered archive files...
51     QFileInfoList list = dir.entryInfoList();
52     for (int i = 0; i < list.size(); ++i)
53     {
54         QFileInfo fileInfo = list.at(i);
55         qDebug() << QString("%1 %2").arg(fileInfo.size(), 10).arg(fileInfo.absoluteFilePath());
56
57         //... and collect the contents of each archive
58         QList<MediaImage*> files = listContents(fileInfo.absoluteFilePath(), fp);
59
60         if (files.count() > 0)
61         {
62             quint32 crc = readCrc32(fileInfo.absoluteFilePath());
63             MediaImageContainer *con = new MediaImageContainer
64                 (
65                     fileInfo.fileName(),
66                     QString("%1").arg(crc, 0, 16),
67                     fileInfo.size(),
68                     files,
69                     new FilePathObject(*fp)
70                 );
71             containers.append(con);
72             qDebug() << "We have " << containers.size() << " containers.";
73         }
74     }
75     qDebug() << "Done scanning files!";
76     return containers;
77 }
78
79 /* Uses crc32 from zlib.h to count crc32 checksum value */
80 quint32 FileUtil::readCrc32(QString filePath)
81 {
82     QFile file(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));
86     }
87     quint32 crc = crc32(0L, Z_NULL, 0);
88     int read = 0;
89     while((read = file.read(buf, READ_BUFFER))) {
90         crc = crc32(crc, (const Bytef*) buf, read);
91     }
92     file.close();
93     if (crc <= 0)
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);
96     return crc;
97 }
98
99 QList<MediaImage*> FileUtil::listContents(const QString filePath, const FilePathObject *fp)
100 {
101
102     UnZip uz;
103     UnZip::ErrorCode ec = uz.openArchive(filePath);
104     if (ec != UnZip::Ok)
105         throw EmuFrontException(tr("Error while opening zip-file %1, error code %2").arg(filePath).arg(ec));
106
107     if (!fp->getSetup()){
108         throw EmuFrontException(tr("Setup not available with %1.").arg(fp->getName()));
109     }
110
111     Setup *sup = fp->getSetup();
112     QList<UnZip::ZipEntry> list = uz.entryList();
113     QList<MediaImage*>  fileList;
114     foreach(UnZip::ZipEntry entry, list)
115     {
116         qDebug() << "Zip entry " << entry.filename;
117         if (isSupportedFile(entry.filename, sup->getSupportedFileTypeExtensions()))
118         {
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);
123             fileList << effo;
124         }
125     }
126
127     qDebug() << "File list has " << fileList.size() << " entries.";
128     return fileList;
129
130 }
131
132 bool FileUtil::isSupportedFile(const QString filename, const QStringList supportedFileExtensions)
133 {
134     QString ext = filename.section('.', -1);
135     return supportedFileExtensions.contains(ext, Qt::CaseInsensitive);
136 }