forked from LazyT/rrcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unpackager.cpp
147 lines (117 loc) · 3.11 KB
/
unpackager.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
#include "unpackager.h"
unpackagerDialog::unpackagerDialog(QWidget *parent, QFile *package, QFile *directory) : QDialog(parent)
{
setupUi(this);
layout()->setSizeConstraint(QLayout::SetFixedSize);
setWindowFlags(windowFlags() | Qt::MSWindowsFixedSizeDialogHint);
connect(&timer, SIGNAL(timeout()), this, SLOT(timer_refreshTime()));
file_pkg = package;
file_dir = directory;
finished = false;
canceled = false;
time.start();
timer.start(500);
label_file->setText(file_pkg->fileName());
label_size->setText(QString::number(file_pkg->size()));
QTimer::singleShot(100, this, SLOT(startUnpackaging()));
}
void unpackagerDialog::startUnpackaging()
{
QFile file_tgz(TGZ);
QByteArray pkg;
QByteArray tgz;
ccrypt_stream_t ccrypt;
short err_code;
QString err_msg;
int count = 0;
pkg = file_pkg->readAll();
file_pkg->close();
tgz.resize(pkg.size() - 32);
ccrypt.next_in = pkg.data();
ccrypt.avail_in = static_cast<uint>(pkg.size());
ccrypt.next_out = tgz.data();
ccrypt.avail_out = static_cast<uint>(tgz.size());
progressBar_decrypt->setValue(50);
ccdecrypt_init(&ccrypt, PKG_KEY, 0);
ccdecrypt(&ccrypt);
ccdecrypt_end(&ccrypt);
progressBar_decrypt->setValue(100);
QCoreApplication::processEvents();
if(file_tgz.open(QIODevice::WriteOnly))
{
file_tgz.write(tgz);
file_tgz.close();
}
extractor
.setArchive(file_tgz.fileName(), file_dir->fileName())
.setFunc(QArchive::EXTRACTED, [&](QString file)
{
label_file->setText(QFileInfo(file).fileName());
label_count->setText(QString::number(++count));
label_size->setText(QString("%1 Byte").arg(QFileInfo(file).size()));
})
.setFunc(QArchive::PROGRESS, [&](int prog)
{
progressBar_decompress->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();
}
timer.stop();
if(canceled)
{
if(!finished)
{
QMessageBox::warning(this, APPNAME, tr("Unpackaging aborted by user!"));
}
else
{
QMessageBox::critical(this, APPNAME, tr("Unpackaging error!\n\n%1\n\n%2").arg(reinterpret_cast<MainWindow*>(parent())->qarchive_error_strings.at(err_code)).arg(err_msg));
}
close();
return;
}
QDesktopServices::openUrl(QUrl("file:///" + file_dir->fileName()));
close();
}
void unpackagerDialog::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 unpackagerDialog::reject()
{
if(!finished && !canceled)
{
if(QMessageBox::question(this, APPNAME, tr("Really abort unpackaging?"), QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes) == QMessageBox::Yes)
{
extractor.cancel();
}
}
else
{
accept();
}
}
void unpackagerDialog::on_buttonBox_clicked(__attribute__((unused)) QAbstractButton *button)
{
close();
}