Added new class hierarchy for process helpers and first implementation
[emufront] / src / utils / unziphelper.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 <QFile>
21 #include <QDebug>
22 #include "unziphelper.h"
23 #include "../dataobjects/mediaimage.h"
24 #include "../dataobjects/filepathobject.h"
25 #include "../exceptions/emufrontexception.h"
26
27 const QString UnzipHelper::UNZIP_COMMAND = "unzip ";
28 const QString UnzipHelper::UNZIP_LIST_ARGS = "-lv ";
29
30 UnzipHelper::UnzipHelper(QObject *parent) :
31     ProcessHelper(parent)
32 {
33 }
34
35 QList<MediaImage*> UnzipHelper::listContents(const QString filePath, const FilePathObject *fp)
36 {
37     if (!fp->getSetup()){
38         throw EmuFrontException(tr("Setup not available with %1.").arg(fp->getName()));
39     }
40
41     QFile fl(filePath);
42     if (!fl.open(QIODevice::ReadOnly)) {
43         throw new EmuFrontException(tr("Couldn't read file %1.").arg(filePath));
44     }
45
46     Setup *sup = fp->getSetup();
47     QList<MediaImage*>  fileList;
48
49     QProcess proc(this);
50     QString command;
51     command.append(UNZIP_COMMAND);
52     command.append(UNZIP_LIST_ARGS);
53     command.append("\"");
54     command.append(filePath);
55     command.append("\"");
56     qDebug() << command;
57     proc.start(command);
58     // TODO: slot(s) for (start and) error signal(s)
59     bool procOk = proc.waitForFinished();
60     if (!procOk) {
61         throw new EmuFrontException(tr("Listing information from file %1 failed with unzip.").arg(filePath));
62     }
63     QString err = proc.readAllStandardError();
64     QString msg = proc.readAllStandardOutput();
65     qDebug() << "\nErrors:\n" << err << "\nMessage:\n" << msg;
66
67     /*
68
69     The unzip output should have 8 columns, we need to collect the data from
70     size, crc-32 and name columns.
71
72     $ unzip -lv zak.zip
73     Archive:  zak.zip
74      Length   Method    Size  Cmpr    Date    Time   CRC-32   Name
75     --------  ------  ------- ---- ---------- ----- --------  ----
76       174848  Defl:N    21936  88% 1996-12-24 23:32 cd68329c  Zak McKracken and the Alien Mindbenders (1988)(Lucasfilm Games)(Disk 1 of 2 Side A)(Boot).d64
77       174848  Defl:N    21949  87% 1996-12-24 23:32 dc0d89f8  Zak McKracken and the Alien Mindbenders (1988)(Lucasfilm Games)(Disk 1 of 2 Side A)(Boot)[a].d64
78       174848  Defl:N    81818  53% 1996-12-24 23:32 a11bc616  Zak McKracken and the Alien Mindbenders (1988)(Lucasfilm Games)(Disk 1 of 2 Side A)(Boot)[cr ESI].d64
79       174848  Defl:N    48833  72% 1996-12-24 23:32 0815053d  Zak McKracken and the Alien Mindbenders (1988)(Lucasfilm Games)(Disk 1 of 2 Side A)(Boot)[cr SCI].d64
80       174848  Defl:N   105964  39% 1996-12-24 23:32 0c943d80  Zak McKracken and the Alien Mindbenders (1988)(Lucasfilm Games)(Disk 1 of 2 Side A)[cr Ikari].d64
81       174848  Defl:N    17876  90% 1996-12-24 23:32 51397eb8  Zak McKracken and the Alien Mindbenders (1988)(Lucasfilm Games)(Disk 1 of 2 Side B)[cr SCI].d64
82       174848  Defl:N   106231  39% 1996-12-24 23:32 0efadb0a  Zak McKracken and the Alien Mindbenders (1988)(Lucasfilm Games)(Disk 2 of 2 Side A).d64
83       174848  Defl:N   105974  39% 1996-12-24 23:32 6935c3e7  Zak McKracken and the Alien Mindbenders (1988)(Lucasfilm Games)(Disk 2 of 2 Side A)[a].d64
84       174848  Defl:N   105963  39% 1996-12-24 23:32 1e9c31de  Zak McKracken and the Alien Mindbenders (1988)(Lucasfilm Games)(Disk 2 of 2 Side A)[cr ESI].d64
85       174848  Defl:N    26294  85% 1996-12-24 23:32 ba5fdfdd  Zak McKracken and the Alien Mindbenders (1988)(Lucasfilm Games)(Disk 2 of 2 Side A)[cr Ikari].d64
86       174848  Defl:N   117996  33% 1996-12-24 23:32 efbf3fd6  Zak McKracken and the Alien Mindbenders (1988)(Lucasfilm Games)(Disk 2 of 2 Side B).d64
87       174848  Defl:N   118015  33% 1996-12-24 23:32 c9541ecd  Zak McKracken and the Alien Mindbenders (1988)(Lucasfilm Games)(Disk 2 of 2 Side B)[a].d64
88       174848  Defl:N   118010  33% 1996-12-24 23:32 68341056  Zak McKracken and the Alien Mindbenders (1988)(Lucasfilm Games)(Disk 2 of 2 Side B)[cr ESI].d64
89     --------          -------  ---                            -------
90      2273024           996859  56%                            13 files
91
92      Here's a regex tested in VIM matching an entry line
93      /^\s\+\d\+\s\+[A-Za-z:]*\s\+\d\+\s\+\d\{1,3}%\s\+\d\{4}-\d\{2}-\d\{2}\s\+\d\{2}:\d\{2}\s\+[0-9a-f]\{8}\s\+.\+$
94
95      Here's a regex (tested in VIM) picking the three required fields, length, crc-32 and filename:
96      :%s/^\s\+\(\d\+\)\s\+[A-Za-z:]*\s\+\d\+\s\+\d\{1,3}%\s\+\d\{4}-\d\{2}-\d\{2}\s\+\d\{2}:\d\{2}\s\+\([0-9a-f]\{8}\)\s\+\(.*$\)/\1 \2 \3/gc
97      */
98     QStringList lines = msg.split('\n'
99         //QRegExp("^\\s+\\d+\\s+[A-Za-z:]*\\s+\\d+\\s+\\d{1,3}%\\s+\\d{4}-\\d{2}-\\d{2}\\s+\\d{2}:\\d{2}\\s+[0-9a-f]{8}\\s+.+$")
100         );
101     QStringList entries;
102     QRegExp test("^\\s+\\d+\\s+[A-Za-z:]*\\s+\\d+\\s+\\d{1,3}%\\s+\\d{4}-\\d{2}-\\d{2}\\s+\\d{2}:\\d{2}\\s+[0-9a-f]{8}\\s+.+$");
103     QRegExp regExEntries("^\\s+(\\d+)\\s+[A-Za-z:]*\\s+\\d+\\s+\\d{1,3}%\\s+\\d{4}-\\d{2}-\\d{2}\\s+\\d{2}:\\d{2}\\s+([0-9a-f]{8})\\s+(\\S.+)$");
104     foreach(QString ln, lines) {
105         if (!test.exactMatch(ln)) continue;
106         int pos = regExEntries.indexIn(ln);
107         entries = regExEntries.capturedTexts();
108         if (entries.count() < 4) continue;
109         QString filename = entries[3];
110         QString checksum = entries[2];
111         QString lenStr = entries[1];
112         bool ok;
113         int length = lenStr.toInt(&ok);
114         if (!ok) continue;
115         MediaImage *effo = new MediaImage(filename, checksum, length);
116         fileList << effo;
117     }
118
119     /*UnZip uz;
120     UnZip::ErrorCode ec = uz.openArchive(filePath);
121     if (ec != UnZip::Ok)
122         throw EmuFrontException(tr("Error while opening zip-file %1, error code %2").arg(filePath).arg(ec));
123
124     QList<UnZip::ZipEntry> list = uz.entryList();
125     foreach(UnZip::ZipEntry entry, list)
126     {
127         qDebug() << "Zip entry " << entry.filename;
128         if (isSupportedFile(entry.filename, sup->getSupportedFileTypeExtensions()))
129         {
130             QString checksum = QString("%1").arg(entry.crc32, 0, 16);
131             qDebug() << "Checksum " << checksum;
132             MediaImage *effo = new MediaImage(entry.filename,
133                 checksum, entry.uncompressedSize);
134             fileList << effo;
135         }
136     }*/
137
138     qDebug() << "File list has " << fileList.size() << " entries.";
139     return fileList;
140
141 }
142