2 // Copyright 2010 Mikko Keinänen
4 // This file is part of EmuFront.
7 // EmuFront is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License version 2 as published by
9 // the Free Software Foundation and appearing in the file gpl.txt included in the
10 // packaging of this file.
12 // EmuFront is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with EmuFront. If not, see <http://www.gnu.org/licenses/>.
24 #include "zlib.h" /* crc32 */
25 #include "../exceptions/emufrontexception.h"
26 #include "../dataobjects/setup.h"
27 #include "../dataobjects/mediaimage.h"
28 #include "../dataobjects/mediaimagecontainer.h"
29 #include "../dataobjects/mediatype.h"
30 #include "../dataobjects/platform.h"
31 #include "../db/dbmediaimagecontainer.h"
32 #include "unziphelper.h"
34 FileUtil::FileUtil(QObject *parent) : QObject(parent)
36 buf = new char[READ_BUFFER];
37 unzipHelper = new UnzipHelper(this);
45 /* Throws EmuFrontException */
46 int FileUtil::scanFilePath(FilePathObject *fp, QStringList filters, DbMediaImageContainer *dbMic)
49 throw EmuFrontException(tr("Setup not available with %1.").arg(fp->getName()));
51 else if(!fp->getSetup()->getPlatform()){
52 throw EmuFrontException(tr("No platform object available with %1.")
53 .arg(fp->getSetup()->getName()));
55 else if (!fp->getSetup()->getMediaType()){
56 throw new EmuFrontException(tr("No media type available with %1.")
57 .arg(fp->getSetup()->getName()));
61 qDebug() << QString("We have a platform %1, media type %2")
62 .arg(fp->getSetup()->getPlatform()->getName())
63 .arg(fp->getSetup()->getMediaType()->getName());
64 QDir dir(fp->getName());
65 if (!dir.exists() || !dir.isReadable())
66 throw EmuFrontException(tr("Directory %1 doesn't exists or isn't readable!").arg(fp->getName()));
68 qDebug() << QString("Scanning directory %1.").arg(fp->getName());
69 dir.setFilter(QDir::Files | QDir::NoSymLinks | QDir::NoDotAndDotDot | QDir::Readable);
71 if (filters.count() > 0) dir.setNameFilters(filters);
73 // we'll go through the filtered archive files...
74 QFileInfoList list = dir.entryInfoList();
75 // TODO: only a buffer of objects should be kept here,
76 // and write to database each time the buffer is filled.
77 QList<MediaImageContainer*> containers;
78 for (int i = 0; i < list.size(); ++i)
80 QFileInfo fileInfo = list.at(i);
81 qDebug() << QString("%1 %2").arg(fileInfo.size(), 10).arg(fileInfo.absoluteFilePath());
83 //... and collect the contents of each archive
84 QMap<QString, EmuFrontObject*> files = unzipHelper->listContents(fileInfo.absoluteFilePath(), fp);
86 if (files.count() > 0)
88 // read crc32 checksum for media image container
89 quint32 crc = readCrc32(fileInfo.absoluteFilePath());
90 FilePathObject *fpo = new FilePathObject(*fp);
91 MediaImageContainer *con = new MediaImageContainer (
93 QString("%1").arg(crc, 0, 16),
96 fpo // we need a copy since MediaImageContainers are deleted and the original filepath object would get deleted also.
98 containers.append(con);
100 qDebug() << "We have " << containers.count() << " containers.";
102 if (containers.count() >= MIC_BUFFER_SIZE) {
103 qDebug() << "We have " << containers.count()
104 << " containers .. storing to db.";
105 dbMic->storeContainers(containers, fp);
106 qDeleteAll(containers);
108 qDebug() << "containers now: " << containers.count();
110 qDebug() << "We have " << containers.size() << " containers.";
113 if (containers.count() > 0) {
114 qDebug() << "Storing the rest " << containers.count() << " containers.";
115 dbMic->storeContainers(containers, fp);
116 qDeleteAll(containers);
120 qDebug() << "Done scanning files!";
124 /* Uses crc32 from zlib.h to count crc32 checksum value */
125 quint32 FileUtil::readCrc32(QString filePath)
127 // todo ... use some crc32 tool for this ... or maybe use md5 or something like that!!!
128 QFile file(filePath);
129 qDebug() << "readCrc32: " << filePath;
130 if (!file.open(QIODevice::ReadOnly)) {
131 throw new EmuFrontException(QString(tr("Failed opening file %1 for reading the checksum!")).arg(filePath));
133 quint32 crc = crc32(0L, Z_NULL, 0);
135 while((read = file.read(buf, READ_BUFFER))) {
136 crc = crc32(crc, (const Bytef*) buf, read);
140 throw new EmuFrontException(QString(tr("Failed reading crc checksum for file %1!")).arg(filePath));
141 qDebug() << QString("readCrc32, crc: %1").arg(crc, 0, 16);
145 bool FileUtil::isSupportedFile(const QString filename, const QStringList supportedFileExtensions)
147 QString ext = filename.section('.', -1);
148 return supportedFileExtensions.contains(ext, Qt::CaseInsensitive);