-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathokarchive.cpp
More file actions
407 lines (379 loc) · 10.3 KB
/
okarchive.cpp
File metadata and controls
407 lines (379 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/*
* Copyright (c) 2013-2017 Thomas Isaac Lightburn
*
*
* This file is part of OpenKJ.
*
* OpenKJ is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "okarchive.h"
#include <QDebug>
#include <QFile>
#include <QBuffer>
#include <QTemporaryDir>
#ifdef Q_OS_WIN
#include <io.h>
#else
#include <QProcess>
#include <unistd.h>
#endif
QString infoZipPath;
OkArchive::OkArchive(QString ArchiveFile, QObject *parent) : QObject(parent)
{
process = new QProcess();
archiveFile = ArchiveFile;
qWarning() << "OkArchive opening file: " << archiveFile;
m_cdgFound = false;
m_audioFound = false;
m_cdgSize = 0;
m_audioSize = 0;
goodArchive = false;
audioFileName = "";
cdgFileName = "";
audioExt = "";
m_entriesProcessed = false;
lastError = "";
audioExtensions.append(".mp3");
audioExtensions.append(".wav");
audioExtensions.append(".ogg");
audioExtensions.append(".mov");
audioExtensions.append(".flac");
#ifdef Q_OS_WIN
infoZipPath = "unzip.exe";
#else
infoZipPath = "/usr/bin/unzip";
#endif
}
OkArchive::OkArchive(QObject *parent) : QObject(parent)
{
process = new QProcess();
m_cdgFound = false;
m_audioFound = false;
m_cdgSize = 0;
m_audioSize = 0;
goodArchive = false;
audioFileName = "";
cdgFileName = "";
audioExt = "";
m_entriesProcessed = false;
lastError = "";
audioExtensions.append(".mp3");
audioExtensions.append(".wav");
audioExtensions.append(".ogg");
audioExtensions.append(".mov");
audioExtensions.append(".flac");
#ifdef Q_OS_WIN
infoZipPath = "unzip.exe";
#else
infoZipPath = "/usr/bin/unzip";
#endif
}
OkArchive::~OkArchive()
{
delete process;
}
unsigned int OkArchive::getSongDuration()
{
if ((m_cdgFound) && (m_cdgSize > 0))
return ((m_cdgSize / 96) / 75) * 1000;
else if (findCDG())
return ((m_cdgSize / 96) / 75) * 1000;
else
return 0;
}
QByteArray OkArchive::getCDGData()
{
if (findCDG())
{
QTemporaryDir dir;
if (!extractFile(cdgFileName, dir.path(), "tmp.cdg"))
return QByteArray();
QByteArray data;
QFile cdg(dir.path() + QDir::separator() + "tmp.cdg");
cdg.open(QFile::ReadOnly);
data = cdg.readAll();
cdg.close();
return data;
}
return QByteArray();
}
QString OkArchive::getArchiveFile() const
{
return archiveFile;
}
void OkArchive::setArchiveFile(const QString &value)
{
// qWarning() << "OkArchive opening archive file: " << value;
archiveFile = value;
m_cdgFound = false;
m_audioFound = false;
m_cdgSize = 0;
m_audioSize = 0;
m_entries.clear();
audioFileName = "";
cdgFileName = "";
audioExt = "";
m_entriesProcessed = false;
lastError = "";
}
bool OkArchive::checkCDG()
{
if (!findCDG())
return false;
if (m_cdgSize <= 0)
return false;
return true;
}
bool OkArchive::checkAudio()
{
if (!findAudio())
return false;
if (m_audioSize <= 0)
return false;
return true;
}
QString OkArchive::audioExtension()
{
return audioExt;
}
bool OkArchive::extractAudio(QString destPath, QString destFile)
{
if (findAudio())
{
if (extractFile(audioFileName, destPath, destFile))
return true;
}
return false;
}
bool OkArchive::isValidKaraokeFile()
{
// if (!zipIsValid())
// {
// qWarning() << archiveFile << " - Archive is corrupt or invalid";
// lastError = "Corrupt or invalid archive";
// return false;
// }
if (!findEntries())
{
if (!goodArchive)
{
qWarning() << archiveFile << " - Invalid or corrupt zip file";
lastError = "Invalid or corrupt zip file";
return false;
}
if (!m_cdgFound)
{
qWarning() << archiveFile << " - Missing CDG file";
lastError = "CDG not found in zip file";
}
if (!m_audioFound)
{
qWarning() << archiveFile << " - Missing audio file";
lastError = "Audio file not found in zip file";
}
return false;
}
if (m_audioSize <= 0)
{
qWarning() << archiveFile << " - Zero byte audio file";
lastError = "Zero byte audio file";
return false;
}
if (m_cdgSize <= 0)
{
qWarning() << archiveFile << " - Zero byte CDG file";
lastError = "Zero byte CDG file";
return false;
}
return true;
}
QString OkArchive::getLastError()
{
return lastError;
}
bool OkArchive::findCDG()
{
findEntries();
if (m_cdgFound)
return true;
return false;
}
bool OkArchive::findAudio()
{
findEntries();
if (m_audioFound)
return true;
return false;
}
bool OkArchive::findEntries()
{
if (m_entriesProcessed && (!m_audioFound || !m_cdgFound))
return false;
if (m_audioFound && m_cdgFound)
return true;
getZipContents();
for (int i=0; i < m_entries.size(); i++)
{
QString fileName = m_entries.at(i).fileName;
if (fileName.endsWith(".cdg",Qt::CaseInsensitive))
{
cdgFileName = fileName;
m_cdgSize = m_entries.at(i).fileSize;
m_cdgFound = true;
}
else
{
for (int e=0; e < audioExtensions.size(); e++)
{
if (fileName.endsWith(audioExtensions.at(e), Qt::CaseInsensitive))
{
audioFileName = fileName;
audioExt = audioExtensions.at(e);
m_audioSize = m_entries.at(i).fileSize;
m_audioFound = true;
}
}
}
if (m_audioFound && m_cdgFound)
{
return true;
}
}
return false;
}
zipEntries OkArchive::getZipContents()
{
if (m_entriesProcessed)
return m_entries;
QStringList arguments;
arguments << "-l";
arguments << archiveFile;
process->setProcessChannelMode(QProcess::MergedChannels);
process->setProgram(infoZipPath);
process->setArguments(arguments);
process->start(QProcess::ReadOnly);
process->waitForFinished();
if (process->exitCode() == 0)
{
// no error
goodArchive = true;
}
else if (process->exitCode() <= 2)
{
qWarning() << "Non-fatal error while processing zip: " << archiveFile;
qWarning() << "infozip returned error code: " << process->exitCode();
goodArchive = true;
}
else if (process->exitCode() >= 3)
{
qWarning() << "Fatal error while processing zip: " << archiveFile;
qWarning() << "infozip returned error code: " << process->exitCode();
goodArchive = false;
return zipEntries();
}
QString output = process->readAll();
QStringList data = output.split(QRegExp("[\r\n]"),QString::SkipEmptyParts);
int fnStart = 0;
int listStart = 0;
for (int l=0; l < data.size(); l++)
{
if (data.at(l).contains("Name", Qt::CaseInsensitive) && data.at(l).contains("Length", Qt::CaseInsensitive) && data.at(l).contains("Date", Qt::CaseInsensitive))
{
fnStart = data.at(l).indexOf("Name");
listStart = l;
break;
}
}
for (int l=0; l < listStart; l++)
data.removeFirst();
data.removeFirst();
data.removeFirst();
data.removeLast();
data.removeLast();
for (int i=0; i < data.size(); i++)
{
zipEntry entry;
int fnOffset = data.at(i).size() - fnStart;
entry.fileName = data.at(i).right(fnOffset);
entry.fileSize = data.at(i).split(" ", QString::SkipEmptyParts).at(0).toInt();
m_entries.append(entry);
}
m_entriesProcessed = true;
return m_entries;
}
bool OkArchive::extractFile(QString fileName, QString destDir, QString destFile)
{
qWarning() << "OkArchive(" << fileName << ", " << destDir << ", " << destFile << ") called";
QTemporaryDir tmpDir;
QString tmpZipPath = tmpDir.path() + QDir::separator() + "tmp.zip";
if (!QFile::copy(archiveFile, tmpZipPath))
{
qWarning() << "error copying zip";
return false;
}
QStringList arguments;
arguments << "-j";
arguments << tmpZipPath;
arguments << fileName;
arguments << "-d";
arguments << destDir;
process->start(infoZipPath, arguments, QProcess::ReadOnly);
process->waitForFinished();
if (process->exitCode() == 0)
{
// no error
}
else if (process->exitCode() <= 2)
{
qWarning() << "Non-fatal error while processing zip: " << archiveFile;
qWarning() << "infozip returned error code: " << process->exitCode();
}
else if (process->exitCode() >= 3)
{
qWarning() << "Fatal error while processing zip: " << archiveFile;
qWarning() << "infozip returned error code: " << process->exitCode();
return false;
}
if (!QFile::rename(destDir + QDir::separator() + fileName, destDir + QDir::separator() + destFile))
{
qWarning() << "infozip didn't report fatal error, but file was not unzipped successfully";
return false;
}
return true;
}
bool OkArchive::zipIsValid()
{
QProcess *process = new QProcess(this);
QStringList arguments;
arguments << "-t";
arguments << archiveFile;
process->setProcessChannelMode(QProcess::ForwardedChannels);
process->start(infoZipPath, arguments, QProcess::ReadOnly);
process->waitForFinished();
//qWarning() << process->readAll();
qWarning() << process->state();
qWarning() << process->error();
if (process->exitCode() != 0)
{
process->close();
delete process;
return false;
}
else
{
process->close();
delete process;
return true;
}
}