-
Notifications
You must be signed in to change notification settings - Fork 1
/
workerdialog.cpp
192 lines (146 loc) · 4.07 KB
/
workerdialog.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
#include "workerdialog.h"
#include "ui_workerdialog.h"
#include "debug.h"
WorkerDialog::WorkerDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::WorkerDialog)
{
ui->setupUi(this);
setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
window = dynamic_cast<MainWindow *>(parent);
}
WorkerDialog::~WorkerDialog()
{
delete ui;
}
int WorkerDialog::exec()
{
DoExport();
return QDialog::exec();
}
void WorkerDialog::setExport(RdbFile *rdb, const std::vector<uint32_t> &hashes, const std::string &dir)
{
this->rdb = rdb;
this->max_jobs = (int)hashes.size();
files_idx.clear();
files_idx.resize(hashes.size());
for (size_t i = 0; i < hashes.size(); i++)
{
files_idx[i] = rdb->FindFileByID(hashes[i]);
}
out_dir = dir;
ui->label->setText("Extracting files...");
setWindowTitle("Extracting files...");
ui->progressBar->setMaximum((int)files_idx.size());
}
void WorkerDialog::setExportAll(RdbFile *rdb, const std::string &dir)
{
this->rdb = rdb;
this->max_jobs = (int)rdb->GetNumFiles();
files_idx.clear();
files_idx.resize(rdb->GetNumFiles());
for (size_t i = 0; i < files_idx.size(); i++)
files_idx[i] = i;
out_dir = dir;
ui->label->setText("Extracting files...");
setWindowTitle("Extracting files...");
ui->progressBar->setMaximum((int)files_idx.size());
}
void WorkerDialog::DoExport()
{
QThreadPool *pool = QThreadPool::globalInstance();
QVector<ExportWork *> works;
works.resize((int)files_idx.size());
priority = ui->priorityComboBox->currentIndex();
for (int i = 0; i < (int)files_idx.size(); i++)
{
std::string file;
rdb->GetFileName(files_idx[i], file);
file = Utils::MakePathString(out_dir, file);
works[i] = new ExportWork(window, files_idx[i], file, &priority);
connect(works[i], SIGNAL(workFinished()), this, SLOT(onWorkFinished()));
connect(this, SIGNAL(cancelSignal()), works[i], SLOT(onCancel()));
connect(works[i], SIGNAL(errorSignal()), this, SLOT(onError()));
}
jobs_finished = 0;
/*pool->setMaxThreadCount(QThread::idealThreadCount());
if (pool->maxThreadCount() > 16)
pool->setMaxThreadCount(16);*/
pool->setMaxThreadCount(1); // Currently RdbFile::ExtractFile is not multithread safe.
//pool->setMaxThreadCount(1); // For slower testing
for (int i = 0; i < works.size(); i++)
{
pool->start(works[i]);
}
}
void WorkerDialog::onWorkFinished()
{
QMutexLocker locker(&mutex);
jobs_finished++;
ui->progressBar->setValue(jobs_finished);
if (jobs_finished == max_jobs)
{
QThreadPool::globalInstance()->waitForDone();
done(1);
}
}
void WorkerDialog::reject()
{
QMutexLocker locker(&mutex);
if (jobs_finished != max_jobs)
{
ui->label->setText("Cancelling...");
QThreadPool::globalInstance()->clear();
emit cancelSignal();
QThreadPool::globalInstance()->waitForDone();
done(0);
}
}
void WorkerDialog::onError()
{
QMutexLocker locker(&mutex);
ui->label->setText("There was an error. Cancelling all jobs...");
QThreadPool::globalInstance()->clear();
emit cancelSignal();
QThreadPool::globalInstance()->waitForDone();
done(-1);
}
void WorkerDialog::onSetWorkSize(int size)
{
max_jobs = size;
ui->progressBar->setMaximum(size);
}
void WorkerDialog::on_cancelButton_clicked()
{
reject();
}
void WorkerDialog::on_priorityComboBox_activated(int index)
{
priority = index;
}
void ExportWork::run()
{
if (*current_priority == 1)
{
SetThreadPriority(GetCurrentThread(), THREAD_MODE_BACKGROUND_BEGIN);
}
else
{
SetThreadPriority(GetCurrentThread(), THREAD_MODE_BACKGROUND_END);
}
if (cancel)
return;
bool success = window->rdb->ExtractFile(idx, file, true, true);
if (!success && !cancel)
{
emit errorSignal();
return;
}
if (cancel)
return;
emit workFinished();
}
void ExportWork::onCancel()
{
cancel = true;
}