0b18ee22c3e873a2a37192ea63f4e6946889f17e
[emufront] / src / utils / fileutil.cpp
1 // EmuFront
2 // Copyright 2010 Mikko Keinänen
3 //
4 // This file is part of EmuFront.
5 //
6 //
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.
11 //
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.
16 //
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/>.
19
20 #include <QDir>
21 #include <QDebug>
22 #include "fileutil.h"
23 #include "zlib.h" /* crc32 */
24 #include "OSDaB-Zip/unzip.h"
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
33 //int FileUtil::MIC_BUFFER_SIZE = 50;
34
35 FileUtil::FileUtil(QObject *parent) : QObject(parent)
36 {
37     buf = new char[READ_BUFFER];
38 }
39
40 FileUtil::~FileUtil()
41 {
42     delete[] buf;
43 }
44
45 /* Throws EmuFrontException */
46 int FileUtil::scanFilePath(FilePathObject *fp, QStringList filters, DbMediaImageContainer *dbMic)
47 {
48     if (!fp->getSetup()){
49         throw EmuFrontException(tr("Setup not available with %1.").arg(fp->getName()));
50     }
51     else if(!fp->getSetup()->getPlatform()){
52         throw EmuFrontException(tr("No platform object available with %1.")
53             .arg(fp->getSetup()->getName()));
54     }
55     else if (!fp->getSetup()->getMediaType()){
56         throw new EmuFrontException(tr("No media type available with %1.")
57             .arg(fp->getSetup()->getName()));
58     }
59     int count = 0;
60     qDebug() << QString("We have a platform %1, media type %2")
61         .arg(fp->getSetup()->getPlatform()->getName())
62         .arg(fp->getSetup()->getMediaType()->getName());
63     QDir dir(fp->getName());
64     if (!dir.exists() || !dir.isReadable())
65         throw EmuFrontException(tr("Directory %1 doesn't exists or isn't readable!").arg(fp->getName()));
66
67     qDebug() << QString("Scanning directory %1.").arg(fp->getName());
68     dir.setFilter(QDir::Files | QDir::NoSymLinks | QDir::NoDotAndDotDot | QDir::Readable);
69
70     if (filters.count() > 0) dir.setNameFilters(filters);
71
72     // we'll go through the filtered archive files...
73     QFileInfoList list = dir.entryInfoList();
74     // TODO: only a buffer of objects should be kept here,
75     // and write to database each time the buffer is filled.
76     QList<MediaImageContainer*> containers;
77     for (int i = 0; i < list.size(); ++i)
78     {
79         QFileInfo fileInfo = list.at(i);
80         qDebug() << QString("%1 %2").arg(fileInfo.size(), 10).arg(fileInfo.absoluteFilePath());
81
82         //... and collect the contents of each archive
83         QList<MediaImage*> files = listContents(fileInfo.absoluteFilePath(), fp);
84
85         if (files.count() > 0)
86         {
87             quint32 crc = readCrc32(fileInfo.absoluteFilePath());
88             FilePathObject *fpo = new FilePathObject(*fp);
89             MediaImageContainer *con = new MediaImageContainer
90                 (
91                     fileInfo.fileName(),
92                     QString("%1").arg(crc, 0, 16),
93                     fileInfo.size(),
94                     files,
95                     fpo // we need a copy since MediaImageContainers are deleted and the original filepath object would get deleted also.
96                 );
97             containers.append(con);
98             ++count;
99             qDebug() << "We have " << containers.count() << " containers.";
100
101             if (containers.count() >= MIC_BUFFER_SIZE)  {
102                 qDebug() << "We have " << containers.count()
103                     << " containers .. storing to db.";
104                 dbMic->storeContainers(containers, fp);
105                 qDeleteAll(containers);
106                 containers.clear();
107                 qDebug() << "containers now: " << containers.count();
108             }
109             qDebug() << "We have " << containers.size() << " containers.";
110         }
111     }
112     if (containers.count() > 0) {
113         qDebug() << "Storing the rest " << containers.count() << " containers.";
114         dbMic->storeContainers(containers, fp);
115         qDeleteAll(containers);
116         containers.clear();
117
118     }
119     qDebug() << "Done scanning files!";
120     return count;
121 }
122
123 /* Uses crc32 from zlib.h to count crc32 checksum value */
124 quint32 FileUtil::readCrc32(QString filePath)
125 {
126     QFile file(filePath);
127     qDebug() << "readCrc32: " << filePath;
128     if (!file.open(QIODevice::ReadOnly)) {
129         throw new EmuFrontException(QString(tr("Failed opening file %1 for reading the checksum!")).arg(filePath));
130     }
131     quint32 crc = crc32(0L, Z_NULL, 0);
132     int read = 0;
133     while((read = file.read(buf, READ_BUFFER))) {
134         crc = crc32(crc, (const Bytef*) buf, read);
135     }
136     file.close();
137     if (crc <= 0)
138         throw new EmuFrontException(QString(tr("Failed reading crc checksum for file %1!")).arg(filePath));
139     qDebug() << QString("readCrc32, crc: %1").arg(crc, 0, 16);
140     return crc;
141 }
142
143 QList<MediaImage*> FileUtil::listContents(const QString filePath, const FilePathObject *fp)
144 {
145
146     UnZip uz;
147     UnZip::ErrorCode ec = uz.openArchive(filePath);
148     if (ec != UnZip::Ok)
149         throw EmuFrontException(tr("Error while opening zip-file %1, error code %2").arg(filePath).arg(ec));
150
151     if (!fp->getSetup()){
152         throw EmuFrontException(tr("Setup not available with %1.").arg(fp->getName()));
153     }
154
155     Setup *sup = fp->getSetup();
156     QList<UnZip::ZipEntry> list = uz.entryList();
157     QList<MediaImage*>  fileList;
158     foreach(UnZip::ZipEntry entry, list)
159     {
160         qDebug() << "Zip entry " << entry.filename;
161         if (isSupportedFile(entry.filename, sup->getSupportedFileTypeExtensions()))
162         {
163             QString checksum = QString("%1").arg(entry.crc32, 0, 16);
164             qDebug() << "Checksum " << checksum;
165             MediaImage *effo = new MediaImage(entry.filename,
166                 checksum, entry.uncompressedSize);
167             fileList << effo;
168         }
169     }
170
171     qDebug() << "File list has " << fileList.size() << " entries.";
172     return fileList;
173
174 }
175
176 bool FileUtil::isSupportedFile(const QString filename, const QStringList supportedFileExtensions)
177 {
178     QString ext = filename.section('.', -1);
179     return supportedFileExtensions.contains(ext, Qt::CaseInsensitive);
180 }