-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
506 lines (463 loc) · 15.7 KB
/
mainwindow.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
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
/*
QPar2 version 0.1.
QPar2 aims at integrating every functionnality of par2cmdline in a Qt GUI.
It depends on libpar2, libsigc++ and libqt4.
Copyright (C) 2007 Mathieu Guilbaud
This program 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 2 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, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "mainwindow.h"
MainWindow::MainWindow(int argc, char * argv[]) {
setupUi(this);
// menu bar
create_menu();
// tableWidget
tableWidget->setHorizontalHeaderLabels(QStringList()
<< tr("Filename") << tr("Progress") << tr("Block"));
tableWidget->verticalHeader()->hide();
setAcceptDrops(true);
statusBar()->showMessage(tr("Waiting for par2 files..."));
operation = none;
status = undef;
/*nbdone = 0;
nbfiles = 0;
avail_blocks = 0;*/
file_loaded = false;
show();
if (argc == 2) {
while (QCoreApplication::hasPendingEvents ())
QCoreApplication::processEvents();
sourceFileEdit->setText(QDir::convertSeparators(QString::fromAscii(argv[1])));
preprocess();
}
}
/*
* Create the menu bar
*/
void MainWindow::create_menu() {
QAction * openAction = new QAction(tr("&Open"), this);
openAction->setShortcut(tr("Ctrl+O"));
openAction->setStatusTip(tr("Open par2 files for verification"));
connect(openAction, SIGNAL(triggered()), this, SLOT(on_browseButton_clicked()));
QAction * quitAction = new QAction(tr("&Quit"), this);
quitAction->setShortcut(tr("Ctrl+Q"));
quitAction->setStatusTip(tr("Quit QPar2"));
connect(quitAction, SIGNAL(triggered()), this, SLOT(close()));
QAction * aboutAction = new QAction(tr("&About"), this);
aboutAction->setStatusTip(tr("Show QPar2's About box"));
connect(aboutAction, SIGNAL(triggered()), this, SLOT(about_dialog()));
QMenu * fileMenu = menuBar()->addMenu(tr("&File"));
fileMenu->addAction(openAction);
fileMenu->addAction(quitAction);
QMenu * helpMenu = menuBar()->addMenu(tr("&Help"));
helpMenu->addAction(aboutAction);
}
/*
* Show a message box with some information about QPar2
*/
void MainWindow::about_dialog() {
QMessageBox::about(this, tr("About QPar2"),
tr("<h2>QPar2 v0.1</h2>"
"<p>Copyright © 2007 Mathieu Guilbaud"
"<p>QPar2 aims at integrating every functionnality of par2cmdline in a Qt GUI."
"<p>This software is based on the work of <i>parchive</i> "
"(<a href=\"http://sourceforge.net/projects/parchive\">http://sourceforge.net/projects/parchive</a>), "
"especially <i>GPar2<i>."
));
}
/*
* Resize the table's columns :
* 40% for the column 1
* 40% for the column 2
* 20% for the column 3
*/
void MainWindow::resize_table_column() {
// if the vertical scrillbar is visible then reduce the width
int reduce = (tableWidget->verticalScrollBar()->isVisible()) ?
tableWidget->verticalScrollBar()->width() + 2 : 0;
int width = (int)((tableWidget->width()-reduce)/10);
// 4/10 4/10 2/10
tableWidget->setColumnWidth(0, 4*width);
tableWidget->setColumnWidth(1, 4*width);
tableWidget->setColumnWidth(2, 2*width);
}
/*
* Pop up an "Open file" dialog box
*/
void MainWindow::on_browseButton_clicked() {
if (operation != none)
return; // avoid problems while working
QString initialName = sourceFileEdit->text();
if (initialName.isEmpty()) {
initialName = QDir::homePath();
}
QString fileName = QFileDialog::getOpenFileName(this,
tr("Choose File"), initialName,
tr("PAR2 files (*.par2)"));
fileName = QDir::convertSeparators(fileName);
if (! fileName.isEmpty()) {
sourceFileEdit->setText(fileName);
preprocess();
}
}
/*
* Init the repairer, plugs the signals for catching
* repairere informations and scans the headers.
*/
void MainWindow::preprocess() {
statusBar()->showMessage(tr("Scanning headers..."));
operation = scanning;
status = undef;
file_loaded = true;
/*nbdone = 0;
nbfiles = 0;
avail_blocks = 0;*/
CommandLine * commandline = new CommandLine;
Result result = eInvalidCommandLineArguments;
// constructs the command line string
char ** argv;
char * args [3];
argv = args;
args[0] = "par2";
args[1] = "r";
char * buffer = strdup(sourceFileEdit->text().toLocal8Bit());
args[2] = buffer;
// parse the command line
bool res = commandline->Parse(3, argv);
if (! res) {
// there was something wrong with the command line
errors(cmdline_error);
verifyButton->setEnabled(false);
}
else {
// create the repairer and connect
// communication signals
repairer = new LibPar2(commandline);
repairer->sig_filename.connect(
sigc::mem_fun(*this, &MainWindow::signal_filename));
repairer->sig_progress.connect(
sigc::mem_fun(*this, &MainWindow::signal_progress));
repairer->sig_headers.connect(
sigc::mem_fun(*this, &MainWindow::signal_headers));
repairer->sig_done.connect(
sigc::mem_fun(*this, &MainWindow::signal_done));
result = repairer->PreProcess();
update_status(result);
verifyButton->setEnabled(true);
}
free(buffer);
globalProgressBar->reset();
operation = none;
}
/*
* If par2 files are loaded and not already verified
* then it scan verify the source files.
*/
void MainWindow::on_verifyButton_clicked() {
if (operation != none)
return; // avoid problems while working
if (! file_loaded)
errors(notloaded_verify);
else if (status != verifiable)
errors(already_verified);
else {
statusBar()->showMessage(tr("Verifying..."));
operation = verifying;
Result result = repairer->Process(false);
// activate the "repair" button only if needed
repairButton->setEnabled(result == eRepairPossible);
update_status(result);
operation = none;
}
}
/*
* If the set of files has already been verified
* and is repairable then it repairs the damaged files
*/
void MainWindow::on_repairButton_clicked() {
if (operation != none)
return; // avoid problems while working
if (! file_loaded || status == undef)
errors(notloaded_repair);
else if (status == complete)
errors(notnecessary_repair);
else if (status == unrepairable)
errors(notpossible_repair);
else {
statusBar()->showMessage(tr("Repairing..."));
operation = repairing;
Result result = repairer->Process(true);
update_status(result);
operation = none;
}
}
/*
* For each new file found by libpar2, a signal with the
* file name is send. We create a new row if the filename
* is not present.
*/
void MainWindow::signal_filename(std::string filename) {
int row = tableWidget->rowCount();
bool exist = false;
QString fname = QString::fromAscii(filename.c_str());
// if the table is not empty we search for a row
// that already contains the filename, if we found
// one then we return
if (row) {
QLabel * l;
for (int i=0; i<row && !exist; i++) {
l = (QLabel *)(tableWidget->cellWidget(i, 0));
if (l->text() == fname) {
exist = true;
}
}
}
// if the filename is not already present we create the row
if (!exist) {
QProgressBar * pb = new QProgressBar();
pb->setRange(0, 1000);
tableWidget->insertRow(row);
tableWidget->setCellWidget(row, 0, new QLabel(fname));
tableWidget->setCellWidget(row, 1, pb);
resize_table_column();
tableWidget->scrollToBottom();
}
// give back the control to events dispatcher during long operation
QCoreApplication::processEvents();
}
/*
* Long operations send their progress.
* Each header files and each data files send a progress
* relative to their own progress, not a global progress.
* The argument is a number between 0 and 1000.
*/
void MainWindow::signal_progress(double progress) {
if (operation == verifying || operation == scanning) {
int row = tableWidget->rowCount() - 1;
QProgressBar * progBar = (QProgressBar *)(tableWidget->cellWidget(row, 1));
progBar->setValue((int)progress);
}
else {
globalProgressBar->setValue((int)progress);
}
// give back the control to events dispatcher during long operation
QCoreApplication::processEvents();
}
/*
* When verifying this signal is send when the processing the file
* is done. The function signal_filename is send at the start,
* except if the file is missing.
*/
void MainWindow::signal_done(std::string filename, int blocks_available,
int blocks_total) {
// search the filename in the table. If it doesn't exist
// then it call signal_filename
QLabel * prev = (QLabel *)(tableWidget->cellWidget(tableWidget->rowCount()-1, 0));
if (prev->text() != QString::fromAscii(filename.c_str())) {
signal_filename(filename);
}
QLabel * lbl = new QLabel(QString::number(blocks_available)
+ " / " + QString::number(blocks_total));
lbl->setAlignment(Qt::AlignCenter);
tableWidget->setCellWidget(tableWidget->rowCount()-1, 2, lbl);
// give back the control to events dispatcher during long operation
QCoreApplication::processEvents();
}
/*
* The ParHeader is a summary of the par files.
* It gives information about the set of par files.
*/
void MainWindow::signal_headers(ParHeaders * headers) {
QString header_setid = tr("Set ID: ");
QString header_blocksize = tr("Block size: ");
QString header_sourceblockcount = tr("Data blocks: ");
QString header_totalsize = tr("Data size: ");
QString header_recoverablefiles = tr("Recoverable files: ");
QString header_otherfiles = tr("Other files: ");
QString header_chunksize = tr("Chunk size: ");
// set id
if (headers->setid.size() != 0) {
header_setid.append(headers->setid.c_str());
}
// block size
if (headers->block_size != -1) {
double size = headers->block_size;
if (size < 1024.0)
header_blocksize.append(QString::number(size) + tr(" bytes"));
else if (size < 1024.0*1024)
header_blocksize.append(QString::number(size/1024.0) + tr(" Kbytes"));
else if (size < 1024.0*1024*1024)
header_blocksize.append(QString::number(size/(1024.0*1024)) + tr(" Mbytes"));
else
header_blocksize.append(QString::number(size/(1024.0*1024*1024)) + tr(" Gbytes"));
}
// chunck ?
if (headers->chunk_size != -1) {
header_chunksize.append(QString::number(headers->chunk_size));
}
// data block
if (headers->data_blocks != -1) {
header_sourceblockcount.append(QString::number(headers->data_blocks));
}
// header total size
if (headers->data_size != -1) {
double size = headers->data_size;
if (size < 1024.0)
header_totalsize.append(QString::number(size) + tr(" bytes"));
else if (size < 1024.0*1024)
header_totalsize.append(QString::number(size/1024.0) + tr(" Kbytes"));
else if (size < 1024.0*1024*1024)
header_totalsize.append(QString::number(size/(1024.0*1024)) + tr(" Mbytes"));
else
header_totalsize.append(QString::number(size/(1024.0*1024*1024)) + tr(" Gbytes"));
}
// recoverable files
if (headers->recoverable_files != -1) {
nbfiles = headers->recoverable_files;
header_recoverablefiles.append(QString::number(headers->recoverable_files));
}
// other files
if (headers->other_files != -1) {
header_otherfiles.append(QString::number(headers->other_files));
}
lblSetId->setText(header_setid);
lblBlockSize->setText(header_blocksize);
lblDataBlock->setText(header_sourceblockcount);
lblDataSize->setText(header_totalsize);
lblRecoverable->setText(header_recoverablefiles);
lblOtherFiles->setText(header_otherfiles);
lblChunkSize->setText(header_chunksize);
// give back the control to events dispatcher during long operation
QCoreApplication::processEvents();
}
/*
* Display a different message box for each error
*/
void MainWindow::errors(Error error) {
// display if trying to repair and no archive is loaded
if (error == notloaded_repair) {
QMessageBox::warning(this, tr("Unable to repair archive"),
tr("A recovery set must be loaded before it can be repaired. Please check that a set has been loaded and try this request again."),
QMessageBox::Ok);
statusBar()->showMessage(tr("Repair failed"));
}
// display if trying to verify and archive not loaded
else if (error == notloaded_verify) {
QMessageBox::warning(this, tr("Unable to repair archive"),
tr("A recovery set must be loaded before it can be repaired. Please check that a set has been loaded and try this request again."),
QMessageBox::Ok);
statusBar()->showMessage(tr("Verification failed"));
}
// display if archive doesn't need repairing
else if (error == notnecessary_repair) {
QMessageBox::information(this, tr("Repair not necessary"),
tr("Verification of the recovery set has found the archive to be complete. The archive does not need to be repaired."),
QMessageBox::Ok);
statusBar()->showMessage(tr("Archive complete"));
}
// display if archive already verified
else if (error == already_verified) {
QMessageBox::information(this, tr("Verification completed"),
tr("Verification of the recovery set has already been performed. You may repair the archive if necessary."),
QMessageBox::Ok);
statusBar()->showMessage(tr("Archive already verified"));
}
// display if archive unrepairable
else if (error == notpossible_repair) {
QMessageBox::critical(this, tr("Repair not possible"),
tr("Unable to repair the archive. You do not have enough recovery blocks to repair."),
QMessageBox::Ok);
statusBar()->showMessage(tr("Repair not possible"));
}
// display if the loaded files is not a PAR2 file
else if (error == cmdline_error) {
QMessageBox::critical(this, tr("The recovery file does not exist"),
tr("The recovery file does not exist. Please check that the file you load is a PAR2 file."),
QMessageBox::Ok);
statusBar()->showMessage(tr("The recovery file does not exist"));
}
}
/*
* Update the status variable and the text displayed in the status bar.
* Result is a typedef define in libpar2/par2cmdline.h
*/
void MainWindow::update_status(Result result) {
switch (result) {
case eSuccess:
if (operation == scanning) {
status = verifiable;
statusBar()->showMessage(tr("Scan complete"));
}
else if (operation == verifying) {
status = complete;
statusBar()->showMessage(tr("Verifying complete, no need to repair"));
}
else if (operation == repairing) {
status = complete;
statusBar()->showMessage(tr("Repairing complete"));
}
break;
case eRepairPossible:
statusBar()->showMessage(tr("Repair possible"));
status = repairable;
break;
case eRepairNotPossible:
statusBar()->showMessage(tr("Repair not possible"));
status = unrepairable;
break;
case eInsufficientCriticalData:
statusBar()->showMessage(tr("Insufficient critical data"));
status = unrepairable;
break;
case eRepairFailed:
statusBar()->showMessage(tr("Repair failed"));
status = unrepairable;
break;
case eFileIOError:
statusBar()->showMessage(tr("I/O error"));
status = unrepairable;
break;
case eLogicError:
statusBar()->showMessage(tr("Internal error"));
status = unrepairable;
break;
case eMemoryError:
statusBar()->showMessage(tr("Out of memory"));
status = unrepairable;
break;
default:
break;
}
}
/*
* Drag'n'drop support
*/
void MainWindow::dragEnterEvent(QDragEnterEvent *event) {
if (event->mimeData()->hasFormat("text/plain"))
event->acceptProposedAction();
}
/*
* Drag'n'drop support
*/
void MainWindow::dropEvent(QDropEvent *event) {
QString filename = event->mimeData()->text();
// if the filename is like "file://path" we accept it
QRegExp rx("file://(.*)");
if (rx.exactMatch(filename)) {
filename = rx.cap(1);
if (QFile::exists(filename)) {
sourceFileEdit->setText(QDir::convertSeparators(filename));
preprocess();
}
}
event->acceptProposedAction();
}