d779a0c35c59d202c33040bfc42b1b23ea21637d
[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 <QProgressDialog>
24 #include "fileutil.h"
25 #include "zlib.h" /* crc32 */
26 #include "emufrontexception.h"
27 #include "setup.h"
28 #include "mediaimage.h"
29 #include "mediaimagecontainer.h"
30 #include "mediatype.h"
31 #include "platform.h"
32 #include "dbmediaimagecontainer.h"
33 #include "unziphelper.h"
34
35 FileUtil::FileUtil(QObject *parent) : QObject(parent)
36 {
37     buf = new char[READ_BUFFER];
38     unzipHelper = new UnzipHelper(this);
39 }
40
41 FileUtil::~FileUtil()
42 {
43     delete[] buf;
44 }
45
46 /* Throws EmuFrontException */
47 int FileUtil::scanFilePath(FilePathObject *fp,
48     QStringList filters, DbMediaImageContainer *dbMic,
49     QProgressDialog *progressDialog)
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 EmuFrontException(tr("No media type available with %1.")
60             .arg(fp->getSetup()->getName()));
61     }
62
63     int count = 0;
64     /*qDebug() << QString("We have a platform %1, media type %2")
65         .arg(fp->getSetup()->getPlatform()->getName())
66         .arg(fp->getSetup()->getMediaType()->getName());*/
67     QDir dir(fp->getName());
68     if (!dir.exists() || !dir.isReadable())
69         throw EmuFrontException(tr("Directory %1 doesn't exists or isn't readable!").arg(fp->getName()));
70
71     //qDebug() << QString("Scanning directory %1.").arg(fp->getName());
72     dir.setFilter(QDir::Files | QDir::NoSymLinks | QDir::NoDotAndDotDot | QDir::Readable);
73
74     if (filters.count() > 0) dir.setNameFilters(filters);
75
76     // we'll go through the filtered archive files...
77     QFileInfoList list = dir.entryInfoList();
78     //qDebug() << "We have " << list.count() << " files to go through.";
79     QList<MediaImageContainer*> containers;
80     try {
81         progressDialog->setMinimum(0);
82         progressDialog->setMaximum(list.size());
83         for (int i = 0; i < list.size(); ++i)
84         {
85             progressDialog->setValue(i);
86             if (progressDialog->wasCanceled())
87                 break;
88             QFileInfo fileInfo = list.at(i);
89             //qDebug() << QString("%1 %2").arg(fileInfo.size(), 10).arg(fileInfo.absoluteFilePath());
90
91             //... and collect the contents of each archive
92             QMap<QString, EmuFrontObject*> files = unzipHelper->listContents(fileInfo.absoluteFilePath(), fp);
93
94             if (files.count() > 0)
95             {
96                 // read crc32 checksum for media image container
97                 quint32 crc = readCrc32(fileInfo.absoluteFilePath());
98                 FilePathObject *fpo = new FilePathObject(*fp);
99                 MediaImageContainer *con = new MediaImageContainer (
100                         fileInfo.fileName(),
101                         QString("%1").arg(crc, 0, 16),
102                         fileInfo.size(),
103                         files,
104                         fpo // we need a copy since MediaImageContainers are deleted and the original filepath object would get deleted also.
105                         );
106                 containers.append(con);
107                 ++count;
108                 //qDebug() << "We have " << containers.count() << " containers.";
109
110                 if (containers.count() >= MIC_BUFFER_SIZE)  {
111                     //qDebug() << "We have " << containers.count() << " containers .. storing to db.";
112                     showDbUpdating(progressDialog);
113                     dbMic->storeContainers(containers, fp);
114                     hideDbUpdating(progressDialog);
115                     qDeleteAll(containers);
116                     containers.clear();
117                     //qDebug() << "containers now: " << containers.count();
118                 }
119                 //qDebug() << "We have " << containers.size() << " containers.";
120             } // files.count() > 0
121             else {
122                 qDebug() << "No files from container " << fileInfo.absoluteFilePath();
123             }
124         }
125         progressDialog->setValue(list.size());
126         if (containers.count() > 0) {
127             //qDebug() << "Storing the rest " << containers.count() << " containers.";
128             //emit dbUpdateInProgress();
129             showDbUpdating(progressDialog);
130             dbMic->storeContainers(containers, fp);
131             hideDbUpdating(progressDialog);
132             //emit dbUpdateFinished();
133             qDeleteAll(containers);
134             containers.clear();
135  } } catch (EmuFrontException &e) { qDebug() << "Got exception " << e.what() << ", aborting & deleting created data objects.";
136         qDeleteAll(containers);
137         throw e;
138     }
139     //qDebug() << "Done scanning files!";
140     return count;
141 }
142
143 /* Uses crc32 from zlib.h to count crc32 checksum value */
144 /* Throws EmuFrontException */
145 quint32 FileUtil::readCrc32(QString filePath)
146 {
147     // todo ... use some crc32 tool for this ... or maybe use md5 or something like that!!!
148     QFile file(filePath);
149     //qDebug() << "readCrc32: " << filePath;
150     if (!file.open(QIODevice::ReadOnly)) {
151         throw EmuFrontException(QString(tr("Failed opening file %1 for reading the checksum!")).arg(filePath));
152     }
153     quint32 crc = crc32(0L, Z_NULL, 0);
154     int read = 0;
155     while((read = file.read(buf, READ_BUFFER))) {
156         crc = crc32(crc, (const Bytef*) buf, read);
157     }
158     file.close();
159     if (crc <= 0)
160         throw EmuFrontException(QString(tr("Failed reading crc checksum for file %1!")).arg(filePath));
161     //qDebug() << QString("readCrc32, crc: %1").arg(crc, 0, 16);
162     return crc;
163 }
164
165 bool FileUtil::isSupportedFile(const QString filename, const QStringList supportedFileExtensions)
166 {
167     QString ext = filename.section('.', -1);
168     return supportedFileExtensions.contains(ext, Qt::CaseInsensitive);
169 }
170
171 void FileUtil::showDbUpdating(QProgressDialog *progressDialog)
172 {
173     qDebug() << "DB updating";
174     // TODO: the following is not currently working
175     progressDialog->setWindowTitle(tr("Updating DB... please wait!"));
176     progressDialog->setEnabled(false);
177 }
178
179 void FileUtil::hideDbUpdating(QProgressDialog *progressDialog)
180 {
181     qDebug() << "DB update finished";
182     // TODO: the following is not currently working
183     progressDialog->setEnabled(true);
184     progressDialog->setWindowTitle(tr("Scanning files"));
185 }
186