Updated the git clone command.
[emufront] / src / utils / fileutil.cpp
1 /*
2 ** EmuFront
3 ** Copyright 2010 Mikko Keinänen
4 **
5 ** This file is part of EmuFront.
6 **
7 **
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.
12 **
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.
17 **
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/>.
20 */
21
22 #include <QDir>
23 #include <QDebug>
24 #include <QProcess>
25 #include <QProgressDialog>
26 #include "fileutil.h"
27 #include "zlib.h" /* crc32 */
28 #include "emufrontexception.h"
29 #include "setup.h"
30 #include "mediaimage.h"
31 #include "mediaimagecontainer.h"
32 #include "mediatype.h"
33 #include "platform.h"
34 #include "mediaimagecontainermodel.h"
35 #include "unziphelper.h"
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, 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     MediaImageContainerModel micModel;
64
65     // Remove old instances scanned from this file path
66     micModel.removeFromFilePath(fp->getId());
67
68     int count = 0;
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()));
75
76     //qDebug() << QString("Scanning directory %1.").arg(fp->getName());
77     dir.setFilter(QDir::Files | QDir::NoSymLinks | QDir::NoDotAndDotDot | QDir::Readable);
78
79     if (filters.count() > 0) dir.setNameFilters(filters);
80
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;
85     try {
86         progressDialog->setMinimum(0);
87         progressDialog->setMaximum(list.size());
88         for (int i = 0; i < list.size(); ++i)
89         {
90             progressDialog->setValue(i);
91             if (progressDialog->wasCanceled())
92                 break;
93             QFileInfo fileInfo = list.at(i);
94             //qDebug() << QString("%1 %2").arg(fileInfo.size(), 10).arg(fileInfo.absoluteFilePath());
95
96             //... and collect the contents of each archive
97             QMap<QString, EmuFrontObject*> files = unzipHelper->listContents(fileInfo.absoluteFilePath(), fp);
98
99             if (files.count() > 0)
100             {
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 (
105                         fileInfo.fileName(),
106                         QString("%1").arg(crc, 0, 16),
107                         fileInfo.size(),
108                         files,
109                         fpo // we need a copy since MediaImageContainers are deleted and the original filepath object would get deleted also.
110                         );
111                 containers.append(con);
112                 ++count;
113                 //qDebug() << "We have " << containers.count() << " containers.";
114
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);
121                     containers.clear();
122                     //qDebug() << "containers now: " << containers.count();
123                 }
124                 //qDebug() << "We have " << containers.size() << " containers.";
125             } // files.count() > 0
126             else {
127                 qDebug() << "No files from container " << fileInfo.absoluteFilePath();
128             }
129         }
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);
139             containers.clear();
140  } } catch (EmuFrontException &e) { qDebug() << "Got exception " << e.what() << ", aborting & deleting created data objects.";
141         qDeleteAll(containers);
142         throw e;
143     }
144     //qDebug() << "Done scanning files!";
145     return count;
146 }
147
148 /* Uses crc32 from zlib.h to count crc32 checksum value */
149 /* Throws EmuFrontException */
150 quint32 FileUtil::readCrc32(QString filePath)
151 {
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));
157     }
158     quint32 crc = crc32(0L, Z_NULL, 0);
159     int read = 0;
160     while((read = file.read(buf, READ_BUFFER))) {
161         crc = crc32(crc, (const Bytef*) buf, read);
162     }
163     file.close();
164     if (crc <= 0)
165         throw EmuFrontException(QString(tr("Failed reading crc checksum for file %1!")).arg(filePath));
166     //qDebug() << QString("readCrc32, crc: %1").arg(crc, 0, 16);
167     return crc;
168 }
169
170 bool FileUtil::isSupportedFile(const QString filename, const QStringList supportedFileExtensions)
171 {
172     QString ext = filename.section('.', -1);
173     return supportedFileExtensions.contains(ext, Qt::CaseInsensitive);
174 }
175
176 void FileUtil::showDbUpdating(QProgressDialog *progressDialog)
177 {
178     qDebug() << "DB updating";
179     // TODO: the following is not currently working
180     progressDialog->setWindowTitle(tr("Updating DB... please wait!"));
181     progressDialog->setEnabled(false);
182 }
183
184 void FileUtil::hideDbUpdating(QProgressDialog *progressDialog)
185 {
186     qDebug() << "DB update finished";
187     // TODO: the following is not currently working
188     progressDialog->setEnabled(true);
189     progressDialog->setWindowTitle(tr("Scanning files"));
190 }
191