forked from AdrianTM/xdelta3-gui
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
748 lines (677 loc) · 27.9 KB
/
mainwindow.cpp
File metadata and controls
748 lines (677 loc) · 27.9 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
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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
/**********************************************************************
*
**********************************************************************
* Copyright (C) 2023 MX Authors
*
* Authors: Adrian <adrian@mxlinux.org>
* MX Linux <http://mxlinux.org>
*
* This 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 package. If not, see <http://www.gnu.org/licenses/>.
**********************************************************************/
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "droplineedit.h"
#include <QDebug>
#include <QElapsedTimer>
#include <QFile>
#include <QFileDialog>
#include <QLocale>
#include <QMessageBox>
#include <QProcess>
#include <QScreen>
#include <QRegularExpression>
#include <QStandardPaths>
#include <QTabBar>
#include <QtDBus/QtDBus>
#include <QTime>
#include <algorithm>
#include <chrono>
using namespace std::chrono_literals;
namespace
{
bool isNumericVersion(const QString &value)
{
if (value.isEmpty())
return false;
for (const QString &part : value.split('.', Qt::KeepEmptyParts)) {
if (part.isEmpty() || !std::all_of(part.cbegin(), part.cend(), [](QChar c) { return c.isDigit(); }))
return false;
}
return true;
}
} // namespace
MainWindow::MainWindow(const QString &patchFile, QWidget *parent)
: QDialog(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
progressProcess = new QProcess(this);
connect(progressProcess, &QProcess::readyReadStandardOutput, this, &MainWindow::handleProgressOutput);
connect(progressProcess, &QProcess::readyReadStandardError, this, &MainWindow::handleProgressOutput);
setConnections();
adjustSize();
const QSize size = this->size();
if (settings.contains("geometry")) {
restoreGeometry(settings.value("geometry").toByteArray());
if (isMaximized()) { // add option to resize if maximized
resize(size);
centerWindow();
}
}
ui->tabWidget->setCurrentWidget(ui->tabCreatePatch);
ui->spinCompressionLevel->setValue(settings.value("compression_level", 4).toInt());
int compIdx = ui->comboCompression->findText(settings.value("secondary_compression", "None").toString());
ui->comboCompression->setCurrentIndex(compIdx != -1 ? compIdx : 0);
if (QFile::exists(patchFile)) {
ui->tabWidget->setCurrentWidget(ui->tabApplyPatch);
ui->textApplyPatch->setText(patchFile);
}
ui->tabWidget->setTabVisible(2, false);
}
MainWindow::~MainWindow()
{
if (cmd.state() != QProcess::NotRunning) {
cmd.disconnect();
cmd.terminate();
if (!cmd.waitForFinished(1000)) {
cmd.kill();
}
}
settings.setValue("geometry", saveGeometry());
settings.setValue("compression_level", ui->spinCompressionLevel->value());
settings.setValue("secondary_compression", ui->comboCompression->currentText());
delete ui;
}
void MainWindow::centerWindow()
{
auto screenGeometry = QApplication::primaryScreen()->geometry();
auto x = (screenGeometry.width() - width()) / 2;
auto y = (screenGeometry.height() - height()) / 2;
move(x, y);
}
void MainWindow::cmdStart()
{
ui->progressBar->setValue(0);
lastProg = -1;
lastStatsLine.clear();
etaMs = -1;
etaTick = 0;
etaSizeStr.clear();
targetFileSize = -1;
ui->labelProgressFile->setText("");
ui->labelProgressStats->setText("");
// Switch to progress tab and hide others
ui->tabWidget->setTabVisible(0, false);
ui->tabWidget->setTabVisible(1, false);
ui->tabWidget->setTabVisible(2, true);
ui->tabWidget->setCurrentIndex(2);
// Hide tab bar for a "full page" feel
ui->tabWidget->findChild<QTabBar*>()->hide();
if (!timer.isActive()) {
timer.start(1s);
}
setCursor(QCursor(Qt::BusyCursor));
}
void MainWindow::handleProgressOutput()
{
const QString rawOutput = (progressProcess->readAllStandardOutput() + progressProcess->readAllStandardError()).trimmed();
if (rawOutput.isEmpty()) {
return;
}
static const QRegularExpression re(R"((\d+(?:\.\d+)?)%)");
const QStringList lines = rawOutput.split('\n', Qt::SkipEmptyParts);
for (const QString &line : lines) {
auto match = re.match(line);
if (match.hasMatch()) {
bool ok = false;
double val = match.captured(1).toDouble(&ok);
if (ok) {
lastProg = static_cast<int>(val);
lastStatsLine = line.trimmed();
break;
}
}
}
}
void MainWindow::cmdFinished(bool success, const QString &output)
{
updateTaskbar(0, false);
timer.stop();
setCursor(QCursor(Qt::ArrowCursor));
ui->progressBar->setValue(ui->progressBar->maximum());
// Restore selection interface
ui->tabWidget->setTabVisible(0, true);
ui->tabWidget->setTabVisible(1, true);
ui->tabWidget->setTabVisible(2, false);
ui->tabWidget->setCurrentIndex(currentOp == Operation::ApplyPatch ? 1 : 0);
ui->tabWidget->findChild<QTabBar*>()->show();
QString elapsedStr = formatElapsedTime(elapsedTimer.elapsed());
if (currentOp == Operation::ApplyPatch) {
QString location = QFileInfo(ui->textOutput->text()).absolutePath();
if (success) {
QMessageBox::information(
this, tr("Success", "information that file was successfully written"),
tr("File was successfully written to '%1' directory.", "information that file was successfully written")
.arg(location)
+ '\n'
+ tr("Took %1 to patch the file.", "elapsed time, leave %1 untranslated").arg(elapsedStr));
} else {
if (QFile::exists(ui->textOutput->text())) {
QFile::remove(ui->textOutput->text());
}
if (cancelled) {
QMessageBox::information(this, tr("Cancelled"), tr("Operation was cancelled."));
} else {
QMessageBox::critical(
this, tr("Error"),
tr("Error: Could not write the file.", "information that there was an error creating the file")
+ "\n\n" + output);
}
}
} else if (currentOp == Operation::CreatePatch) {
if (success) {
QString patchPath = QFileInfo(ui->textPatch->text()).absoluteFilePath();
QString patchSize = formatFileSize(QFileInfo(patchPath).size());
QMessageBox::information(
this, tr("Success", "information on file written successfully"),
tr("File '%1' was successfully written.", "information on file written successfully").arg(patchPath)
+ '\n'
+ tr("Patch size: %1", "size of the created patch file").arg(patchSize) + '\n'
+ tr("Took %1 to create the patch.", "elapsed time, leave %1 untranslated").arg(elapsedStr));
} else {
if (QFile::exists(ui->textPatch->text())) {
QFile::remove(ui->textPatch->text());
}
if (cancelled) {
QMessageBox::information(this, tr("Cancelled"), tr("Operation was cancelled."));
} else {
QMessageBox::critical(
this, tr("Error"),
tr("Error: Could not write the file.", "information that file was not written successfully")
+ "\n\n" + output);
}
}
}
currentOp = Operation::None;
}
QString MainWindow::dirSettingsKey(QLineEdit *lineEdit) const
{
if (lineEdit == ui->textSource) return "last_dir_source";
if (lineEdit == ui->textTarget) return "last_dir_target";
if (lineEdit == ui->textPatch) return "last_dir_patch";
if (lineEdit == ui->textInput) return "last_dir_input";
if (lineEdit == ui->textApplyPatch) return "last_dir_apply_patch";
if (lineEdit == ui->textOutput) return "last_dir_output";
return "last_dir";
}
void MainWindow::updateTaskbar(int percent, bool visible)
{
QDBusMessage signal = QDBusMessage::createSignal("/", "com.canonical.Unity.LauncherEntry", "Update");
signal << "application://xdelta3-gui.desktop";
QVariantMap properties;
properties.insert("progress", static_cast<double>(percent) / 100.0);
properties.insert("progress-visible", visible);
signal << properties;
QDBusConnection::sessionBus().send(signal);
}
void MainWindow::onSelectFile(QLineEdit *lineEdit, const QString &filter)
{
QString key = dirSettingsKey(lineEdit);
QString lastDir = settings.value(key, QDir::homePath()).toString();
QString selected = QFileDialog::getOpenFileName(this, tr("Select file", "choose a file"), lastDir, filter);
if (checkFile(selected)) {
lineEdit->setText(selected);
QString path = QFileInfo(selected).absolutePath();
settings.setValue(key, path);
} else {
checkAllinfo();
return;
}
if (lineEdit == ui->textTarget) {
setPatchName();
} else if (lineEdit == ui->textInput || lineEdit == ui->textApplyPatch) {
setOutputName();
}
checkAllinfo();
}
void MainWindow::onSelectDir()
{
bool isCreatePatch = ui->tabWidget->currentWidget() == ui->tabCreatePatch;
QLineEdit *le = isCreatePatch ? ui->textPatch : ui->textOutput;
QString currentPath = le->text();
QString key = dirSettingsKey(le);
QString initialPath;
if (QFileInfo(currentPath).isAbsolute()) {
initialPath = currentPath;
} else {
QString lastDir = settings.value(key, QDir::homePath()).toString();
initialPath = lastDir + "/" + QFileInfo(currentPath).fileName();
}
QString filter = isCreatePatch ? tr("Delta Files (*.xdelta3);;All Files (*)", "kinds of files to choose")
: tr("All Files (*)", "kinds of files to choose");
QString title = isCreatePatch ? tr("Select where to save the delta file", "dialog title")
: tr("Select where to save the patched file", "dialog title");
QString selected = QFileDialog::getSaveFileName(this, title, initialPath, filter);
if (selected.isEmpty()) {
return;
}
le->setText(selected);
settings.setValue(key, QFileInfo(selected).absolutePath());
checkAllinfo();
}
void MainWindow::applyPatch()
{
if (ui->textOutput->text().isEmpty()) {
ui->pushApplyPatch->setDisabled(true);
QMessageBox::warning(this, tr("Error"), tr("Please enter a name for the output file."));
return;
}
if (!checkFile(ui->textInput->text()) || !checkFile(ui->textApplyPatch->text())) {
return;
}
QFileInfo outInfo(ui->textOutput->text());
if (outInfo.exists()) {
if (!outInfo.isWritable()) {
QMessageBox::critical(this, tr("Error"), tr("Output file is not writable."));
return;
}
if (QMessageBox::No
== QMessageBox::question(
this, tr("File exists"),
tr("Output file exists, do you want to overwrite?", "warning about overwritting an existing file"))) {
return;
}
} else if (!QFileInfo(outInfo.absolutePath()).isWritable()) {
QMessageBox::critical(this, tr("Error"), tr("Output directory is not writable."));
return;
}
elapsedTimer.restart();
cancelled = false;
currentOp = Operation::ApplyPatch;
QStringList args;
args << "-f" << "decode" << "-s" << ui->textInput->text() << ui->textApplyPatch->text() << ui->textOutput->text();
cmd.runAsync("xdelta3", args);
auto *proc = new QProcess(this);
connect(proc, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), this,
[this, proc](int, QProcess::ExitStatus) {
qint64 total = 0;
const QString out = proc->readAllStandardOutput() + proc->readAllStandardError();
for (const QString &line : out.split('\n', Qt::SkipEmptyParts)) {
if (line.contains("VCDIFF target window length:")) {
bool ok;
qint64 n = line.section(':', -1).trimmed().toLongLong(&ok);
if (ok)
total += n;
}
}
if (total > 0)
targetFileSize = total;
proc->deleteLater();
});
proc->start("xdelta3", {"printhdrs", ui->textApplyPatch->text()});
}
void MainWindow::checkAllinfo()
{
if (ui->tabWidget->currentWidget() == ui->tabCreatePatch) {
bool sourceSet = !ui->textSource->text().isEmpty();
bool targetSet = !ui->textTarget->text().isEmpty();
bool patchSet = !ui->textPatch->text().isEmpty();
ui->pushCreatePatch->setDisabled(!sourceSet || !targetSet || !patchSet);
if (sourceSet && targetSet && QFileInfo(ui->textPatch->text()).fileName().isEmpty()) {
ui->pushCreatePatch->setDisabled(true);
QMessageBox::warning(this, tr("Error"),
tr("Please enter a name for the delta file.", "name of delta file being created"));
}
} else {
ui->pushApplyPatch->setDisabled(ui->textInput->text().isEmpty() || ui->textApplyPatch->text().isEmpty());
}
}
bool MainWindow::checkFile(const QString &fileName)
{
if (fileName.isEmpty()) {
return false;
}
QFileInfo info(fileName);
if (!info.isFile()) {
QMessageBox::warning(
this, tr("File not found", "warning about file not found"),
tr("File '%1' not found or not a file, please double-check the input.", "warning about file not found")
.arg(fileName));
return false;
}
if (!info.isReadable()) {
QMessageBox::warning(
this, tr("Permission denied"),
tr("File '%1' is not readable. Please check your permissions.").arg(fileName));
return false;
}
return true;
}
void MainWindow::createPatch()
{
if (!checkFile(ui->textSource->text()) || !checkFile(ui->textTarget->text())) {
return;
}
bool force = false;
QFileInfo patchInfo(ui->textPatch->text());
if (patchInfo.exists()) {
if (!patchInfo.isWritable()) {
QMessageBox::critical(this, tr("Error"), tr("Delta file is not writable."));
return;
}
if (QMessageBox::No
== QMessageBox::question(
this, tr("File exists", "warning about overwritting file"),
tr("Delta file exists, do you want to overwrite?", "warning about overwritting file"))) {
return;
} else {
force = true;
}
} else if (!QFileInfo(patchInfo.absolutePath()).isWritable()) {
QMessageBox::critical(this, tr("Error"), tr("Output directory is not writable."));
return;
}
elapsedTimer.restart();
cancelled = false;
currentOp = Operation::CreatePatch;
QStringList args;
if (force) {
args << "-f";
}
args << "encode" << ("-" + QString::number(ui->spinCompressionLevel->value()));
if (ui->comboCompression->currentIndex() != 0) {
args << "-S" << ui->comboCompression->currentText().toLower();
}
args << "-s" << ui->textSource->text()
<< ui->textTarget->text() << ui->textPatch->text();
cmd.runAsync("xdelta3", args);
}
void MainWindow::setConnections()
{
connect(ui->pushApplyPatch, &QPushButton::pressed, this, &MainWindow::applyPatch);
connect(ui->pushCreatePatch, &QPushButton::pressed, this, &MainWindow::createPatch);
connect(ui->pushPatchLocation, &QPushButton::pressed, this, &MainWindow::onSelectDir);
const QString isoFilter = tr("ISO Files (*.iso);;All Files (*)", "kinds of files to choose");
const QString deltaFilter = tr("Delta Files (*.xdelta3);;All Files (*)", "kinds of files to choose");
connect(ui->pushSelectInput, &QPushButton::pressed, this,
[this, isoFilter] { onSelectFile(ui->textInput, isoFilter); });
connect(ui->pushSelectOutput, &QPushButton::pressed, this, &MainWindow::onSelectDir);
connect(ui->pushSelectPatch, &QPushButton::pressed, this,
[this, deltaFilter] { onSelectFile(ui->textApplyPatch, deltaFilter); });
connect(ui->pushSelectSource, &QPushButton::pressed, this,
[this, isoFilter] { onSelectFile(ui->textSource, isoFilter); });
connect(ui->pushSelectTarget, &QPushButton::pressed, this,
[this, isoFilter] { onSelectFile(ui->textTarget, isoFilter); });
connect(ui->textPatch, &QLineEdit::editingFinished, this, [this] { checkAllinfo(); });
connect(ui->textSource, &QLineEdit::editingFinished, this, [this] {
if (checkFile(ui->textSource->text()) && !ui->textTarget->text().isEmpty()) {
setPatchName();
}
checkAllinfo();
});
connect(ui->textTarget, &QLineEdit::editingFinished, this, [this] {
if (checkFile(ui->textTarget->text()) && !ui->textSource->text().isEmpty()) {
setPatchName();
}
checkAllinfo();
});
connect(ui->textInput, &QLineEdit::editingFinished, this, [this] {
checkFile(ui->textInput->text());
checkAllinfo();
});
connect(ui->textApplyPatch, &QLineEdit::editingFinished, this, [this] {
if (checkFile(ui->textApplyPatch->text())) {
setOutputName();
}
checkAllinfo();
});
connect(ui->textOutput, &QLineEdit::editingFinished, this, [this] { checkAllinfo(); });
auto validate = [this](QLineEdit *le) {
QString error;
QString text = le->text();
if (text.isEmpty()) {
le->setStyleSheet("");
le->setToolTip("");
return;
}
QFileInfo info(text);
if (le == ui->textPatch || le == ui->textOutput) {
if (info.exists()) {
if (info.isDir()) {
error = tr("Path exists but is a directory.");
} else if (!info.isWritable()) {
error = tr("File is not writable.");
}
} else {
if (!info.absoluteDir().exists()) {
error = tr("Parent directory does not exist.");
} else if (!QFileInfo(info.absolutePath()).isWritable()) {
error = tr("Parent directory is not writable.");
} else if (info.fileName().isEmpty()) {
error = tr("Filename cannot be empty.");
}
}
} else {
if (!info.exists()) {
error = tr("File does not exist.");
} else if (!info.isFile()) {
error = tr("Path is a directory, not a file.");
} else if (!info.isReadable()) {
error = tr("File is not readable (check permissions).");
}
}
bool ok = error.isEmpty();
le->setStyleSheet(ok ? "" : "QLineEdit { color: red; }");
le->setToolTip(error);
};
connect(ui->textSource, &QLineEdit::textChanged, this, [this, validate] { validate(ui->textSource); });
connect(ui->textTarget, &QLineEdit::textChanged, this, [this, validate] { validate(ui->textTarget); });
connect(ui->textPatch, &QLineEdit::textChanged, this, [this, validate] { validate(ui->textPatch); });
connect(ui->textInput, &QLineEdit::textChanged, this, [this, validate] { validate(ui->textInput); });
connect(ui->textApplyPatch, &QLineEdit::textChanged, this, [this, validate] { validate(ui->textApplyPatch); });
connect(ui->textOutput, &QLineEdit::textChanged, this, [this, validate] { validate(ui->textOutput); });
// Drag and drop connections
connect(ui->textSource, &DropLineEdit::fileDropped, this, [this](const QString &filePath) {
if (checkFile(filePath)) {
QString path = QFileInfo(filePath).absolutePath();
settings.setValue(dirSettingsKey(ui->textSource), path);
if (!ui->textTarget->text().isEmpty()) {
setPatchName();
}
}
checkAllinfo();
});
connect(ui->textTarget, &DropLineEdit::fileDropped, this, [this](const QString &filePath) {
if (checkFile(filePath)) {
QString path = QFileInfo(filePath).absolutePath();
settings.setValue(dirSettingsKey(ui->textTarget), path);
if (!ui->textSource->text().isEmpty()) {
setPatchName();
}
}
checkAllinfo();
});
connect(ui->textInput, &DropLineEdit::fileDropped, this, [this](const QString &filePath) {
if (checkFile(filePath)) {
QString path = QFileInfo(filePath).absolutePath();
settings.setValue(dirSettingsKey(ui->textInput), path);
setOutputName();
}
checkAllinfo();
});
connect(ui->textApplyPatch, &DropLineEdit::fileDropped, this, [this](const QString &filePath) {
if (checkFile(filePath)) {
QString path = QFileInfo(filePath).absolutePath();
settings.setValue(dirSettingsKey(ui->textApplyPatch), path);
setOutputName();
}
checkAllinfo();
});
connect(ui->pushCancelProgress, &QPushButton::clicked, this, [this] {
cancelled = true;
cmd.terminate();
});
connect(&timer, &QTimer::timeout, this, &MainWindow::updateBar);
connect(&cmd, &Cmd::started, this, &MainWindow::cmdStart);
connect(&cmd, &Cmd::commandFinished, this, &MainWindow::cmdFinished);
}
void MainWindow::setPatchName()
{
QString sourceBase = QFileInfo(ui->textSource->text()).completeBaseName();
QString targetBase = QFileInfo(ui->textTarget->text()).completeBaseName();
QString targetDir = QFileInfo(ui->textTarget->text()).absolutePath();
QString patchName;
if (sourceBase == targetBase)
patchName = sourceBase + "-patch.xdelta3";
else
patchName = sourceBase + "_to_" + targetBase + ".xdelta3";
ui->textPatch->setText(targetDir + "/" + patchName);
}
QString MainWindow::formatElapsedTime(qint64 ms)
{
int seconds = static_cast<int>(ms / 1000) % 60;
int minutes = static_cast<int>(ms / (1000 * 60)) % 60;
int hours = static_cast<int>(ms / (1000 * 60 * 60));
if (hours > 0) {
return QString("%1:%2:%3")
.arg(hours)
.arg(minutes, 2, 10, QChar('0'))
.arg(seconds, 2, 10, QChar('0'));
}
return QLocale().toString(QTime(0, minutes, seconds), "mm:ss");
}
QString MainWindow::formatFileSize(qint64 bytes)
{
return QLocale().formattedDataSize(bytes);
}
void MainWindow::setOutputName()
{
QString inputPath = ui->textInput->text();
QString patchPath = ui->textApplyPatch->text();
if (inputPath.isEmpty()) {
return;
}
QString inputBase = QFileInfo(inputPath).completeBaseName();
QString inputSuffix = QFileInfo(inputPath).suffix();
QString inputDir = QFileInfo(inputPath).absolutePath();
QString outputName;
if (!patchPath.isEmpty()) {
QString patchBase = QFileInfo(patchPath).completeBaseName();
// If patch name contains "_to_VERSION", try to extract that version
if (patchBase.contains("_to_")) {
QString targetVersion = patchBase.section("_to_", -1);
// Remove .xdelta3 if it's there (shouldn't be in completeBaseName but just in case)
if (targetVersion.endsWith(".xdelta3")) {
targetVersion.chop(QStringLiteral(".xdelta3").size());
}
// Try to find if inputBase ends with a version we should replace
// e.g. app-1.0 -> app-1.1. Only '-' and '_' are valid separators;
// dots are part of version numbers, not separators.
int lastSep = qMax(inputBase.lastIndexOf('-'), inputBase.lastIndexOf('_'));
if (lastSep != -1 && isNumericVersion(inputBase.mid(lastSep + 1))) {
outputName = inputBase.left(lastSep + 1) + targetVersion;
} else if (isNumericVersion(inputBase)) {
outputName = targetVersion;
} else {
outputName = inputBase + "." + targetVersion;
}
if (!inputSuffix.isEmpty()) {
outputName += "." + inputSuffix;
}
}
}
if (outputName.isEmpty()) {
outputName = inputBase + ".patched";
if (!inputSuffix.isEmpty()) {
outputName += "." + inputSuffix;
}
}
ui->textOutput->setText(inputDir + "/" + outputName);
}
void MainWindow::updateBar()
{
QString statsLine;
int prog = -1;
// 1. Try native progress estimation for Apply Patch
if (currentOp == Operation::ApplyPatch && targetFileSize > 0) {
qint64 currentSize = QFileInfo(ui->textOutput->text()).size();
prog = static_cast<int>(currentSize * 100 / targetFileSize);
if (prog > 100) prog = 100;
qint64 elapsed = elapsedTimer.elapsed();
if (elapsed > 0) {
double speed = static_cast<double>(currentSize) / (elapsed / 1000.0);
statsLine = tr("%1% (%2 / %3) %4/s")
.arg(prog)
.arg(formatFileSize(currentSize))
.arg(formatFileSize(targetFileSize))
.arg(formatFileSize(static_cast<qint64>(speed)));
}
}
// 2. Fallback to external 'progress' tool if native estimation didn't work (e.g. Create Patch)
if (prog == -1) {
static bool progressMissing = false;
if (!progressMissing) {
if (progressProcess->state() == QProcess::NotRunning) {
progressProcess->start("progress", {"-c", "xdelta3"});
}
if (lastProg != -1) {
prog = lastProg;
statsLine = lastStatsLine;
}
}
if (!progressMissing && QStandardPaths::findExecutable("progress").isEmpty()) {
progressMissing = true;
}
}
if (prog != -1) {
ui->progressBar->setValue(prog);
ui->progressBar->setRange(0, 100);
updateTaskbar(prog, true);
} else {
ui->progressBar->setRange(0, 0); // Indeterminate mode
updateTaskbar(0, false);
}
// Recalculate ETA every 5 seconds; estimate output size only for Create Patch
if (etaTick % 5 == 0 && etaTick > 0 && prog > 0) {
qint64 elapsed = elapsedTimer.elapsed();
etaMs = qMax(0LL, elapsed * (100 - prog) / prog);
if (currentOp == Operation::CreatePatch) {
qint64 currentSize = QFileInfo(ui->textPatch->text()).size();
if (currentSize > 0)
etaSizeStr = formatFileSize(currentSize * 100LL / prog);
}
}
etaTick++;
QString outputPath = (currentOp == Operation::CreatePatch) ? ui->textPatch->text() : ui->textOutput->text();
QString fileName = QFileInfo(outputPath).fileName();
QString label;
if (etaMs >= 0 && prog > 0) {
label = tr("~%1 remaining", "estimated time remaining, leave %1 untranslated")
.arg(formatElapsedTime(etaMs));
}
if (currentOp == Operation::ApplyPatch && targetFileSize >= 0) {
label += tr(" · %1 output size", "exact output file size from delta, leave %1 untranslated")
.arg(formatFileSize(targetFileSize));
} else if (!etaSizeStr.isEmpty()) {
label += tr(" · ~%1 estimated size", "estimated output file size, leave %1 untranslated")
.arg(etaSizeStr);
}
if (!fileName.isEmpty()) {
ui->labelProgressFile->setText(tr("Creating file: %1").arg(fileName));
}
if (!statsLine.isEmpty()) {
label += '\n' + statsLine;
}
ui->labelProgressStats->setText(label);
}