Database updates, code is not completely in sync yet.
[emufront] / src / utils / fileutil.cpp
1 #include <QDir>
2 #include <QDebug>
3 #include "fileutil.h"
4 #include "OSDaB-Zip/unzip.h"
5 #include "../exceptions/emufrontexception.h"
6 #include "../dataobjects/setup.h"
7 #include "../dataobjects/mediaimage.h"
8 #include "../dataobjects/mediaimagecontainer.h"
9
10 FileUtil::FileUtil(QObject *parent) : QObject(parent)
11 {}
12
13 QList<MediaImageContainer*> FileUtil::scanFilePath(const FilePathObject *fp, QStringList filters)
14 {
15     QList<MediaImageContainer*> containers;
16     QDir dir(fp->getName());
17     if (!dir.exists() || !dir.isReadable())
18         throw EmuFrontException(tr("Directory %1 doesn't exists or isn't readable!").arg(fp->getName()));
19
20     dir.setFilter(QDir::Files | QDir::NoSymLinks | QDir::NoDotAndDotDot | QDir::Readable);
21
22     if (filters.count() > 0) dir.setNameFilters(filters);
23
24     // we'll go through the filtered archive files...
25     QFileInfoList list = dir.entryInfoList();
26     for (int i = 0; i < list.size(); ++i)
27     {
28         QFileInfo fileInfo = list.at(i);
29         qDebug() << QString("%1 %2").arg(fileInfo.size(), 10).arg(fileInfo.absoluteFilePath());
30
31
32         //... and collect the contents of each archive
33         QList<MediaImage*> files = listContents(fileInfo.absoluteFilePath(), fp);
34
35         // if the archive contained supported file types we will create a media image container object
36         // - platform
37         // - meditype
38         // - media image objects in a list
39         // - time created / updated
40         if (files.count() > 0)
41         {
42             MediaImageContainer *con = new MediaImageContainer();
43             containers.append(con);
44         }
45
46         /*foreach (EmuFrontFileObject *o, files)
47         {
48             qDebug() << o->getFilename();
49         }*/
50     }
51     return containers;
52 }
53
54 QList<MediaImage*> FileUtil::listContents(const QString filePath, const FilePathObject *fp)
55 {
56
57     UnZip uz;
58     UnZip::ErrorCode ec = uz.openArchive(filePath);
59     if (ec != UnZip::Ok)
60         throw EmuFrontException(tr("Error while opening zip-file %1, error code %2").arg(filePath).arg(ec));
61
62     Setup *sup = fp->getSetup();
63     QList<UnZip::ZipEntry> list = uz.entryList();
64     QList<MediaImage*>  fileList;
65     foreach(UnZip::ZipEntry entry, list)
66     {
67         qDebug() << "We have an entry " << entry.filename
68             << "with crc32 " << entry.crc32
69             << " and file size " << entry.uncompressedSize;
70
71         if (isSupportedFile(entry.filename, sup->getSupportedFileTypeExtensions()))
72         {
73             MediaImage *effo = new MediaImage();
74             fileList << effo;
75         }
76     }
77
78     return fileList;
79
80 }
81
82 bool FileUtil::isSupportedFile(const QString filename, const QStringList supportedFileExtensions)
83 {
84     QString ext = filename.section('.', -1);
85     return (supportedFileExtensions.indexOf(ext) > -1);
86 }