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 "ui_freedoomdialog.h"
34 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
35 #include <QFileSystemModel>
39 #include <QMessageBox>
40 #include <wadseeker/entities/checksum.h>
41 #include <wadseeker/entities/modset.h>
42 #include <wadseeker/freedoom.h>
43 #include <wadseeker/modinstall.h>
45 DClass<FreedoomDialog> :
public Ui::FreedoomDialog
49 ModInstall *modInstall;
60 d->freedoom =
new Freedoom(
this);
61 this->connect(d->freedoom, SIGNAL(finished()), SLOT(applyFreedoomVersionInfo()));
63 d->modInstall =
new ModInstall(
this);
64 this->connect(d->modInstall, SIGNAL(finished()), SLOT(onModInstallFinished()));
65 this->connect(d->modInstall, SIGNAL(fileDownloadProgress(QString,qint64,qint64)),
66 SLOT(showFileDownloadProgress(QString,qint64,qint64)));
68 d->btnInstall->setEnabled(
false);
74 FreedoomDialog::~FreedoomDialog()
78 void FreedoomDialog::accept()
80 ModSet modSet = selectedModFiles();
83 QMessageBox::critical(
this, tr(
"Install Freedoom"),
84 tr(
"Select at least one file."));
88 showStatus(tr(
"Downloading & installing ..."));
89 d->targetDir = d->cboInstallPath->currentText();
90 d->modInstall->install(d->targetDir, modSet);
93 void FreedoomDialog::onModInstallFinished()
95 if (!d->modInstall->isError())
101 showError(d->modInstall->error());
104 void FreedoomDialog::updateConfig()
106 gConfig.doomseeker.enableFreedoomInstallation(d->targetDir);
107 gConfig.saveToFile();
110 void FreedoomDialog::fetchInfo()
114 showStatus(tr(
"Downloading Freedoom version info ..."));
115 d->freedoom->requestModSet();
118 void FreedoomDialog::resetProgressBar()
120 d->progressBar->setFormat(tr(
"Working ..."));
121 d->progressBar->show();
122 d->progressBar->setMaximum(0);
123 d->progressBar->setValue(0);
126 void FreedoomDialog::applyFreedoomVersionInfo()
128 if (!d->freedoom->isError())
129 showModInfo(d->freedoom->modSet());
131 showError(tr(
"Error: %1").arg(d->freedoom->error()));
134 void FreedoomDialog::showModInfo(
const ModSet &modSet)
137 d->workInProgressArea->hide();
139 d->wadsTable->setSortingEnabled(
false);
140 d->wadsTable->clearContents();
141 while (d->wadsTable->rowCount() > 0)
143 d->wadsTable->removeRow(d->wadsTable->rowCount() - 1);
145 for (
const ModFile &file : modSet.modFiles())
149 d->wadsTable->setSortingEnabled(
true);
150 d->btnInstall->setEnabled(
true);
153 void FreedoomDialog::insertModFile(
const ModFile &file)
157 QString location = pathFinder.find(file.fileName()).path();
159 QString status = tr(
"OK");
160 bool needsInstall =
false;
161 if (location.isEmpty())
164 status = tr(
"Missing");
168 QByteArray md5 = FileUtils::md5(location);
169 for (Checksum checksum : file.checksums())
171 if (md5 != checksum.hash() && checksum.algorithm() == QCryptographicHash::Md5)
173 status = tr(
"Different");
179 if (file.fileName().compare(
"freedm.wad", Qt::CaseInsensitive) == 0)
180 needsInstall =
false;
182 d->wadsTable->insertRow(d->wadsTable->rowCount());
183 int row = d->wadsTable->rowCount() - 1;
184 d->wadsTable->setItem(row, ColName,
new QTableWidgetItem(file.fileName()));
185 d->wadsTable->setItem(row, ColStatus,
new QTableWidgetItem(status));
187 auto checkBox =
new QCheckBox();
188 checkBox->setChecked(needsInstall);
189 d->wadsTable->setCellWidget(row, ColInstall, checkBox);
191 QString tooltip = tr(
"<p>File: %1<br>Version: %2<br>"
192 "Description: %3<br>Location: %4</p>")
193 .arg(file.name(), file.version(), file.description(), location);
195 for (
int col = 0; col < d->wadsTable->columnCount(); ++col)
197 QTableWidgetItem *item = d->wadsTable->item(row, col);
199 item->setToolTip(tooltip);
203 void FreedoomDialog::showError(
const QString &text)
205 d->btnRetry->setVisible(
true);
207 d->progressBar->hide();
210 void FreedoomDialog::showStatus(
const QString &text)
213 d->workInProgressArea->show();
214 d->btnInstall->setEnabled(
false);
215 d->lblWorkInProgress->setText(text);
218 ModSet FreedoomDialog::selectedModFiles()
const
221 for (
int row = 0; row < d->wadsTable->rowCount(); ++row)
223 auto checkbox =
static_cast<QCheckBox *
>(d->wadsTable->cellWidget(row, ColInstall));
224 if (checkbox->isChecked())
226 QString fileName = d->wadsTable->item(row, ColName)->text();
227 modSet.addModFile(d->freedoom->modSet().findFileName(fileName));
233 void FreedoomDialog::setupInstallPaths()
235 auto completer =
new QCompleter(
this);
236 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
240 auto model =
new QFileSystemModel(completer);
241 model->setRootPath(gConfig.wadseeker.targetDirectory);
242 completer->setModel(model);
244 completer->setModel(
new QDirModel(completer));
246 d->cboInstallPath->setCompleter(completer);
250 d->cboInstallPath->addItem(path.path());
252 d->cboInstallPath->setCurrentText(gConfig.wadseeker.targetDirectory);
255 void FreedoomDialog::setupWadsTable()
257 QHeaderView *header = d->wadsTable->horizontalHeader();
258 header->setSectionResizeMode(ColName, QHeaderView::Stretch);
259 header->setSectionResizeMode(ColStatus, QHeaderView::ResizeToContents);
260 header->setSectionResizeMode(ColInstall, QHeaderView::ResizeToContents);
261 d->wadsTable->setColumnWidth(ColName, 150);
264 void FreedoomDialog::showFileDownloadProgress(
const QString &file,
265 qint64 current, qint64 total)
267 d->progressBar->setFormat(tr(
"%1 %p%").arg(file));
268 d->progressBar->setMaximum(total);
269 d->progressBar->setValue(current);