23 #include "freedoomdialog.h"
25 #include "configuration/doomseekerconfig.h"
26 #include "gui/commongui.h"
27 #include "fileutils.h"
28 #include "pathfinder/filesearchpath.h"
29 #include "pathfinder/pathfinder.h"
30 #include "pathfinder/wadpathfinder.h"
31 #include "templatedpathresolver.h"
32 #include "ui_freedoomdialog.h"
35 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
36 #include <QFileSystemModel>
40 #include <QMessageBox>
41 #include <QPushButton>
42 #include <wadseeker/entities/checksum.h>
43 #include <wadseeker/entities/modset.h>
44 #include <wadseeker/freedoom.h>
45 #include <wadseeker/modinstall.h>
47 DClass<FreedoomDialog> :
public Ui::FreedoomDialog
50 QPushButton *btnInstall;
51 QPushButton *btnRetry;
53 ModInstall *modInstall;
64 d->freedoom =
new Freedoom(
this);
65 this->connect(d->freedoom, SIGNAL(finished()), SLOT(applyFreedoomVersionInfo()));
67 d->modInstall =
new ModInstall(
this);
68 this->connect(d->modInstall, SIGNAL(finished()), SLOT(onModInstallFinished()));
69 this->connect(d->modInstall, SIGNAL(fileDownloadProgress(QString,qint64,qint64)),
70 SLOT(showFileDownloadProgress(QString,qint64,qint64)));
72 d->btnInstall = d->buttonBox->button(QDialogButtonBox::Yes);
73 d->btnInstall->setDefault(
true);
74 d->btnInstall->setEnabled(
false);
75 d->btnInstall->setText(FreedoomDialog::tr(
"Install"));
76 connect(d->btnInstall, &QAbstractButton::clicked,
77 this, &QDialog::accept);
78 d->btnRetry = d->buttonBox->button(QDialogButtonBox::Retry);
79 connect(d->btnRetry, &QAbstractButton::clicked,
80 this, &FreedoomDialog::fetchInfo);
87 FreedoomDialog::~FreedoomDialog()
91 void FreedoomDialog::accept()
93 ModSet modSet = selectedModFiles();
96 QMessageBox::critical(
this, tr(
"Install Freedoom"),
97 tr(
"Select at least one file."));
101 showStatus(tr(
"Downloading & installing ..."));
103 d->cboInstallPath->currentText());
104 d->modInstall->install(d->targetDir, modSet);
107 void FreedoomDialog::onModInstallFinished()
109 if (!d->modInstall->isError())
115 showError(d->modInstall->error());
118 void FreedoomDialog::updateConfig()
120 gConfig.doomseeker.enableFreedoomInstallation(d->targetDir);
121 gConfig.saveToFile();
124 void FreedoomDialog::fetchInfo()
128 showStatus(tr(
"Downloading Freedoom version info ..."));
129 d->freedoom->requestModSet();
132 void FreedoomDialog::resetProgressBar()
134 d->progressBar->setFormat(tr(
"Working ..."));
135 d->progressBar->show();
136 d->progressBar->setMaximum(0);
137 d->progressBar->setValue(0);
140 void FreedoomDialog::applyFreedoomVersionInfo()
142 if (!d->freedoom->isError())
143 showModInfo(d->freedoom->modSet());
145 showError(tr(
"Error: %1").arg(d->freedoom->error()));
148 void FreedoomDialog::showModInfo(
const ModSet &modSet)
151 d->workInProgressArea->hide();
153 d->wadsTable->setSortingEnabled(
false);
154 d->wadsTable->clearContents();
155 while (d->wadsTable->rowCount() > 0)
157 d->wadsTable->removeRow(d->wadsTable->rowCount() - 1);
159 for (
const ModFile &file : modSet.modFiles())
163 d->wadsTable->setSortingEnabled(
true);
164 d->btnInstall->setEnabled(
true);
165 d->btnInstall->setFocus();
168 void FreedoomDialog::insertModFile(
const ModFile &file)
172 QString location = pathFinder.find(file.fileName()).path();
174 QString status = tr(
"OK");
175 bool needsInstall =
false;
176 if (location.isEmpty())
179 status = tr(
"Missing");
183 QByteArray md5 = FileUtils::md5(location);
184 for (Checksum checksum : file.checksums())
186 if (md5 != checksum.hash() && checksum.algorithm() == QCryptographicHash::Md5)
188 status = tr(
"Different");
194 if (file.fileName().compare(
"freedm.wad", Qt::CaseInsensitive) == 0)
195 needsInstall =
false;
197 d->wadsTable->insertRow(d->wadsTable->rowCount());
198 int row = d->wadsTable->rowCount() - 1;
199 d->wadsTable->setItem(row, ColName,
new QTableWidgetItem(file.fileName()));
200 d->wadsTable->setItem(row, ColStatus,
new QTableWidgetItem(status));
202 auto checkBox =
new QCheckBox();
203 checkBox->setChecked(needsInstall);
204 d->wadsTable->setCellWidget(row, ColInstall, checkBox);
206 QString tooltip = tr(
"<p>File: %1<br>Version: %2<br>"
207 "Description: %3<br>Location: %4</p>")
208 .arg(file.name(), file.version(), file.description(), location);
210 for (
int col = 0; col < d->wadsTable->columnCount(); ++col)
212 QTableWidgetItem *item = d->wadsTable->item(row, col);
214 item->setToolTip(tooltip);
218 void FreedoomDialog::showError(
const QString &text)
220 d->btnRetry->setVisible(
true);
221 d->btnRetry->setFocus();
223 d->progressBar->hide();
226 void FreedoomDialog::showStatus(
const QString &text)
229 d->workInProgressArea->show();
230 d->btnInstall->setEnabled(
false);
231 d->lblWorkInProgress->setText(text);
234 ModSet FreedoomDialog::selectedModFiles()
const
237 for (
int row = 0; row < d->wadsTable->rowCount(); ++row)
239 auto checkbox =
static_cast<QCheckBox *
>(d->wadsTable->cellWidget(row, ColInstall));
240 if (checkbox->isChecked())
242 QString fileName = d->wadsTable->item(row, ColName)->text();
243 modSet.addModFile(d->freedoom->modSet().findFileName(fileName));
249 void FreedoomDialog::setupInstallPaths()
251 auto completer =
new QCompleter(
this);
252 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
256 auto model =
new QFileSystemModel(completer);
258 completer->setModel(model);
260 completer->setModel(
new QDirModel(completer));
262 d->cboInstallPath->setCompleter(completer);
266 d->cboInstallPath->addItem(path.path());
268 d->cboInstallPath->setCurrentText(gConfig.wadseeker.targetDirectory);
271 void FreedoomDialog::setupWadsTable()
273 QHeaderView *header = d->wadsTable->horizontalHeader();
274 header->setSectionResizeMode(ColName, QHeaderView::Stretch);
275 header->setSectionResizeMode(ColStatus, QHeaderView::ResizeToContents);
276 header->setSectionResizeMode(ColInstall, QHeaderView::ResizeToContents);
277 d->wadsTable->setColumnWidth(ColName, 150);
280 void FreedoomDialog::showFileDownloadProgress(
const QString &file,
281 qint64 current, qint64 total)
283 d->progressBar->setFormat(tr(
"%1 %p%").arg(file));
284 d->progressBar->setMaximum(total);
285 d->progressBar->setValue(current);