This repository has been archived by the owner on Nov 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 59
/
packager.cpp
196 lines (156 loc) · 4.1 KB
/
packager.cpp
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
#include "packager.h"
packagerDialog::packagerDialog(QWidget *parent, QFile *file) : QDialog(parent)
{
setupUi(this);
layout()->setSizeConstraint(QLayout::SetFixedSize);
setWindowFlags(windowFlags() | Qt::MSWindowsFixedSizeDialogHint);
connect(&timer, SIGNAL(timeout()), this, SLOT(timer_refreshTime()));
dir = file;
finished = false;
canceled = false;
time.start();
timer.start(500);
QTimer::singleShot(100, this, SLOT(startPackaging()));
}
void packagerDialog::startPackaging()
{
QFileInfoList entries;
QStringList files;
QFile file;
QByteArray tgz;
QByteArray pkg;
ccrypt_stream_t ccrypt;
int index = 0;
short err_code;
QString err_msg;
entries = QDir(dir->fileName(), "*.wav", QDir::Name, QDir::Files).entryInfoList();
foreach(QFileInfo fi, entries)
{
files.append(fi.canonicalFilePath());
}
file.setFileName(TGZ);
file.remove();
compressor
.setArchive(file.fileName(), files)
.setArchiveFormat(QArchive::GZIP)
.setFunc(QArchive::COMPRESSED, [&](__attribute__ ((unused)) QString file)
{
label_file->setText(entries.at(index).fileName());
label_count->setText(QString("%1 / %2").arg(1 + index).arg(entries.count()));
label_size->setText(QString("%1 Byte").arg(entries.at(index++).size()));
})
.setFunc([&](int prog)
{
progressBar_compress->setValue(prog);
})
.setFunc(QArchive::FINISHED, [&]()
{
finished = true;
})
.setFunc(QArchive::CANCELED, [&]()
{
canceled = true;
})
.setFunc([&](short errorCode, QString eMsg)
{
canceled = true;
finished = true;
err_code = errorCode;
err_msg = eMsg;
})
.start();
while(!finished && !canceled)
{
QThread::msleep(10);
QCoreApplication::processEvents();
}
if(canceled)
{
timer.stop();
if(!finished)
{
QMessageBox::warning(this, APPNAME, tr("Packaging aborted by user!"));
}
else
{
QMessageBox::critical(this, APPNAME, tr("Packaging error!\n\n%1\n\n%2").arg(reinterpret_cast<MainWindow*>(parent())->qarchive_error_strings.at(err_code)).arg(err_msg));
}
close();
return;
}
if(file.open(QIODevice::ReadOnly) && file.size())
{
tgz = file.readAll();
file.close();
}
else
{
QMessageBox::warning(this, APPNAME, tr("Creating voice package failed!"));
return;
}
pkg.resize(32 + tgz.size());
ccrypt.next_in = tgz.data();
ccrypt.avail_in = static_cast<uint>(tgz.size());
ccrypt.next_out = pkg.data();
ccrypt.avail_out = static_cast<uint>(pkg.size());
progressBar_encrypt->setValue(50);
ccencrypt_init(&ccrypt, PKG_KEY);
ccencrypt(&ccrypt);
ccencrypt_end(&ccrypt);
progressBar_encrypt->setValue(100);
QCoreApplication::processEvents();
timer.stop();
file.setFileName(PKG);
file.remove();
if(file.open(QIODevice::WriteOnly) && pkg.size() > 32)
{
file.write(pkg);
file.close();
if(QMessageBox::question(this, APPNAME, tr("Install created voice package now?"), QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes) == QMessageBox::Yes)
{
hide();
file.open(QIODevice::ReadOnly);
uploadDialog(reinterpret_cast<QWidget*>(parent()), &file).exec();
}
else
{
QFile save(QFileDialog::getSaveFileName(this, tr("Select file to save voice package"), QStandardPaths::writableLocation(QStandardPaths::DownloadLocation) + "/rrcc-voice.pkg", "*.pkg", nullptr, QFileDialog::DontUseNativeDialog));
if(!save.fileName().isEmpty())
{
if(save.exists())
{
save.remove();
}
file.copy(save.fileName());
}
}
}
else
{
QMessageBox::warning(this, APPNAME, tr("Crypting voice file failed!"));
}
close();
}
void packagerDialog::timer_refreshTime()
{
QTime elapsed = QTime::fromMSecsSinceStartOfDay(time.elapsed());
label_time->setText(QString("%1h:%2m:%3s").arg(elapsed.hour(), 2, 10, QChar('0')).arg(elapsed.minute(), 2, 10, QChar('0')).arg(elapsed.second(), 2, 10, QChar('0')));
}
void packagerDialog::reject()
{
if(!finished && !canceled)
{
if(QMessageBox::question(this, APPNAME, tr("Really abort packaging?"), QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes) == QMessageBox::Yes)
{
compressor.cancel();
}
}
else
{
accept();
}
}
void packagerDialog::on_buttonBox_clicked(__attribute__((unused)) QAbstractButton *button)
{
close();
}