3 ** Copyright 2010 Mikko Keinänen
5 ** This file is part of EmuFront.
8 ** EmuFront is free software: you can redistribute it and/or modify
9 ** it under the terms of the GNU General Public License version 2 as published by
10 ** the Free Software Foundation and appearing in the file gpl.txt included in the
11 ** packaging of this file.
13 ** EmuFront is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ** GNU General Public License for more details.
18 ** You should have received a copy of the GNU General Public License
19 ** along with EmuFront. If not, see <http://www.gnu.org/licenses/>.
25 #include <QProgressDialog>
27 #include "zlib.h" /* crc32 */
28 #include "emufrontexception.h"
30 #include "mediaimage.h"
31 #include "mediaimagecontainer.h"
32 #include "mediatype.h"
34 #include "mediaimagecontainermodel.h"
35 #include "unziphelper.h"
37 FileUtil::FileUtil(QObject *parent) : QObject(parent)
39 buf = new char[READ_BUFFER];
40 unzipHelper = new UnzipHelper(this);
48 /* Throws EmuFrontException */
49 int FileUtil::scanFilePath(FilePathObject *fp, QStringList filters, QProgressDialog *progressDialog)
52 throw EmuFrontException(tr("Setup not available with %1.").arg(fp->getName()));
54 else if(!fp->getSetup()->getPlatform()){
55 throw EmuFrontException(tr("No platform object available with %1.")
56 .arg(fp->getSetup()->getName()));
58 else if (!fp->getSetup()->getMediaType()){
59 throw EmuFrontException(tr("No media type available with %1.")
60 .arg(fp->getSetup()->getName()));
63 MediaImageContainerModel micModel;
65 // Remove old instances scanned from this file path
66 micModel.removeFromFilePath(fp->getId());
69 /*qDebug() << QString("We have a platform %1, media type %2")
70 .arg(fp->getSetup()->getPlatform()->getName())
71 .arg(fp->getSetup()->getMediaType()->getName());*/
72 QDir dir(fp->getName());
73 if (!dir.exists() || !dir.isReadable())
74 throw EmuFrontException(tr("Directory %1 doesn't exists or isn't readable!").arg(fp->getName()));
76 //qDebug() << QString("Scanning directory %1.").arg(fp->getName());
77 dir.setFilter(QDir::Files | QDir::NoSymLinks | QDir::NoDotAndDotDot | QDir::Readable);
79 if (filters.count() > 0) dir.setNameFilters(filters);
81 // we'll go through the filtered archive files...
82 QFileInfoList list = dir.entryInfoList();
83 //qDebug() << "We have " << list.count() << " files to go through.";
84 QList<MediaImageContainer*> containers;
86 progressDialog->setMinimum(0);
87 progressDialog->setMaximum(list.size());
88 for (int i = 0; i < list.size(); ++i)
90 progressDialog->setValue(i);
91 if (progressDialog->wasCanceled())
93 QFileInfo fileInfo = list.at(i);
94 //qDebug() << QString("%1 %2").arg(fileInfo.size(), 10).arg(fileInfo.absoluteFilePath());
96 //... and collect the contents of each archive
97 QMap<QString, EmuFrontObject*> files = unzipHelper->listContents(fileInfo.absoluteFilePath(), fp);
99 if (files.count() > 0)
101 // read crc32 checksum for media image container
102 quint32 crc = readCrc32(fileInfo.absoluteFilePath());
103 FilePathObject *fpo = new FilePathObject(*fp);
104 MediaImageContainer *con = new MediaImageContainer (
106 QString("%1").arg(crc, 0, 16),
109 fpo // we need a copy since MediaImageContainers are deleted and the original filepath object would get deleted also.
111 containers.append(con);
113 //qDebug() << "We have " << containers.count() << " containers.";
115 if (containers.count() >= MIC_BUFFER_SIZE) {
116 //qDebug() << "We have " << containers.count() << " containers .. storing to db.";
117 showDbUpdating(progressDialog);
118 micModel.storeContainers(containers, fp);
119 hideDbUpdating(progressDialog);
120 qDeleteAll(containers);
122 //qDebug() << "containers now: " << containers.count();
124 //qDebug() << "We have " << containers.size() << " containers.";
125 } // files.count() > 0
127 qDebug() << "No files from container " << fileInfo.absoluteFilePath();
130 progressDialog->setValue(list.size());
131 if (containers.count() > 0) {
132 //qDebug() << "Storing the rest " << containers.count() << " containers.";
133 //emit dbUpdateInProgress();
134 showDbUpdating(progressDialog);
135 micModel.storeContainers(containers, fp);
136 hideDbUpdating(progressDialog);
137 //emit dbUpdateFinished();
138 qDeleteAll(containers);
140 } } catch (EmuFrontException &e) { qDebug() << "Got exception " << e.what() << ", aborting & deleting created data objects.";
141 qDeleteAll(containers);
144 //qDebug() << "Done scanning files!";
148 /* Uses crc32 from zlib.h to count crc32 checksum value */
149 /* Throws EmuFrontException */
150 quint32 FileUtil::readCrc32(QString filePath)
152 // todo ... use some crc32 tool for this ... or maybe use md5 or something like that!!!
153 QFile file(filePath);
154 //qDebug() << "readCrc32: " << filePath;
155 if (!file.open(QIODevice::ReadOnly)) {
156 throw EmuFrontException(QString(tr("Failed opening file %1 for reading the checksum!")).arg(filePath));
158 quint32 crc = crc32(0L, Z_NULL, 0);
160 while((read = file.read(buf, READ_BUFFER))) {
161 crc = crc32(crc, (const Bytef*) buf, read);
165 throw EmuFrontException(QString(tr("Failed reading crc checksum for file %1!")).arg(filePath));
166 //qDebug() << QString("readCrc32, crc: %1").arg(crc, 0, 16);
170 bool FileUtil::isSupportedFile(const QString filename, const QStringList supportedFileExtensions)
172 QString ext = filename.section('.', -1);
173 return supportedFileExtensions.contains(ext, Qt::CaseInsensitive);
176 void FileUtil::showDbUpdating(QProgressDialog *progressDialog)
178 qDebug() << "DB updating";
179 // TODO: the following is not currently working
180 progressDialog->setWindowTitle(tr("Updating DB... please wait!"));
181 progressDialog->setEnabled(false);
184 void FileUtil::hideDbUpdating(QProgressDialog *progressDialog)
186 qDebug() << "DB update finished";
187 // TODO: the following is not currently working
188 progressDialog->setEnabled(true);
189 progressDialog->setWindowTitle(tr("Scanning files"));