f2e835903111b3950a0a34243b6e62f15fff33f5
[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 <QProcess>
23 #include "fileutil.h"
24 #include "zlib.h" /* crc32 */
25 //#include "OSDaB-Zip/unzip.h"
26 #include "../exceptions/emufrontexception.h"
27 #include "../dataobjects/setup.h"
28 #include "../dataobjects/mediaimage.h"
29 #include "../dataobjects/mediaimagecontainer.h"
30 #include "../dataobjects/mediatype.h"
31 #include "../dataobjects/platform.h"
32 #include "../db/dbmediaimagecontainer.h"
33 #include "unziphelper.h"
34
35 //int FileUtil::MIC_BUFFER_SIZE = 50;
36
37 FileUtil::FileUtil(QObject *parent) : QObject(parent)
38 {
39     buf = new char[READ_BUFFER];
40     unzipHelper = new UnzipHelper(this);
41 }
42
43 FileUtil::~FileUtil()
44 {
45     delete[] buf;
46 }
47
48 /* Throws EmuFrontException */
49 int FileUtil::scanFilePath(FilePathObject *fp, QStringList filters, DbMediaImageContainer *dbMic)
50 {
51     if (!fp->getSetup()){
52         throw EmuFrontException(tr("Setup not available with %1.").arg(fp->getName()));
53     }
54     else if(!fp->getSetup()->getPlatform()){
55         throw EmuFrontException(tr("No platform object available with %1.")
56             .arg(fp->getSetup()->getName()));
57     }
58     else if (!fp->getSetup()->getMediaType()){
59         throw new EmuFrontException(tr("No media type available with %1.")
60             .arg(fp->getSetup()->getName()));
61     }
62     int count = 0;
63     qDebug() << QString("We have a platform %1, media type %2")
64         .arg(fp->getSetup()->getPlatform()->getName())
65         .arg(fp->getSetup()->getMediaType()->getName());
66     QDir dir(fp->getName());
67     if (!dir.exists() || !dir.isReadable())
68         throw EmuFrontException(tr("Directory %1 doesn't exists or isn't readable!").arg(fp->getName()));
69
70     qDebug() << QString("Scanning directory %1.").arg(fp->getName());
71     dir.setFilter(QDir::Files | QDir::NoSymLinks | QDir::NoDotAndDotDot | QDir::Readable);
72
73     if (filters.count() > 0) dir.setNameFilters(filters);
74
75     // we'll go through the filtered archive files...
76     QFileInfoList list = dir.entryInfoList();
77     // TODO: only a buffer of objects should be kept here,
78     // and write to database each time the buffer is filled.
79     QList<MediaImageContainer*> containers;
80     for (int i = 0; i < list.size(); ++i)
81     {
82         QFileInfo fileInfo = list.at(i);
83         qDebug() << QString("%1 %2").arg(fileInfo.size(), 10).arg(fileInfo.absoluteFilePath());
84
85         //... and collect the contents of each archive
86         QMap<QString, EmuFrontObject*> files = unzipHelper->listContents(fileInfo.absoluteFilePath(), fp);
87
88         if (files.count() > 0)
89         {
90             // read crc32 checksum for media image container
91             quint32 crc = readCrc32(fileInfo.absoluteFilePath());
92             FilePathObject *fpo = new FilePathObject(*fp);
93             MediaImageContainer *con = new MediaImageContainer (
94                     fileInfo.fileName(),
95                     QString("%1").arg(crc, 0, 16),
96                     fileInfo.size(),
97                     files,
98                     fpo // we need a copy since MediaImageContainers are deleted and the original filepath object would get deleted also.
99                 );
100             containers.append(con);
101             ++count;
102             qDebug() << "We have " << containers.count() << " containers.";
103
104             if (containers.count() >= MIC_BUFFER_SIZE)  {
105                 qDebug() << "We have " << containers.count()
106                     << " containers .. storing to db.";
107                 dbMic->storeContainers(containers, fp);
108                 qDeleteAll(containers);
109                 containers.clear();
110                 qDebug() << "containers now: " << containers.count();
111             }
112             qDebug() << "We have " << containers.size() << " containers.";
113         }
114     }
115     if (containers.count() > 0) {
116         qDebug() << "Storing the rest " << containers.count() << " containers.";
117         dbMic->storeContainers(containers, fp);
118         qDeleteAll(containers);
119         containers.clear();
120
121     }
122     qDebug() << "Done scanning files!";
123     return count;
124 }
125
126 /* Uses crc32 from zlib.h to count crc32 checksum value */
127 quint32 FileUtil::readCrc32(QString filePath)
128 {
129     // todo ... use some crc32 tool for this ... or maybe use md5 or something like that!!!
130     QFile file(filePath);
131     qDebug() << "readCrc32: " << filePath;
132     if (!file.open(QIODevice::ReadOnly)) {
133         throw new EmuFrontException(QString(tr("Failed opening file %1 for reading the checksum!")).arg(filePath));
134     }
135     quint32 crc = crc32(0L, Z_NULL, 0);
136     int read = 0;
137     while((read = file.read(buf, READ_BUFFER))) {
138         crc = crc32(crc, (const Bytef*) buf, read);
139     }
140     file.close();
141     if (crc <= 0)
142         throw new EmuFrontException(QString(tr("Failed reading crc checksum for file %1!")).arg(filePath));
143     qDebug() << QString("readCrc32, crc: %1").arg(crc, 0, 16);
144     return crc;
145 }
146
147 bool FileUtil::isSupportedFile(const QString filename, const QStringList supportedFileExtensions)
148 {
149     QString ext = filename.section('.', -1);
150     return supportedFileExtensions.contains(ext, Qt::CaseInsensitive);
151 }