cmodlistview_moc.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  1. #include "StdInc.h"
  2. #include "cmodlistview_moc.h"
  3. #include "ui_cmodlistview_moc.h"
  4. #include "imageviewer_moc.h"
  5. #include <QJsonArray>
  6. #include <QCryptographicHash>
  7. #include "cmodlistmodel_moc.h"
  8. #include "cmodmanager.h"
  9. #include "cdownloadmanager_moc.h"
  10. #include "../launcherdirs.h"
  11. #include "../../lib/CConfigHandler.h"
  12. void CModListView::setupModModel()
  13. {
  14. modModel = new CModListModel();
  15. manager = new CModManager(modModel);
  16. }
  17. void CModListView::setupFilterModel()
  18. {
  19. filterModel = new CModFilterModel(modModel);
  20. filterModel->setFilterKeyColumn(-1); // filter across all columns
  21. filterModel->setSortCaseSensitivity(Qt::CaseInsensitive); // to make it more user-friendly
  22. filterModel->setDynamicSortFilter(true);
  23. }
  24. void CModListView::setupModsView()
  25. {
  26. ui->allModsView->setModel(filterModel);
  27. // input data is not sorted - sort it before display
  28. ui->allModsView->sortByColumn(ModFields::TYPE, Qt::AscendingOrder);
  29. ui->allModsView->setColumnWidth(ModFields::NAME, 185);
  30. ui->allModsView->setColumnWidth(ModFields::STATUS_ENABLED, 30);
  31. ui->allModsView->setColumnWidth(ModFields::STATUS_UPDATE, 30);
  32. ui->allModsView->setColumnWidth(ModFields::TYPE, 75);
  33. ui->allModsView->setColumnWidth(ModFields::SIZE, 80);
  34. ui->allModsView->setColumnWidth(ModFields::VERSION, 60);
  35. ui->allModsView->header()->setSectionResizeMode(ModFields::STATUS_ENABLED, QHeaderView::Fixed);
  36. ui->allModsView->header()->setSectionResizeMode(ModFields::STATUS_UPDATE, QHeaderView::Fixed);
  37. ui->allModsView->setUniformRowHeights(true);
  38. connect( ui->allModsView->selectionModel(), SIGNAL( currentRowChanged( const QModelIndex &, const QModelIndex & )),
  39. this, SLOT( modSelected( const QModelIndex &, const QModelIndex & )));
  40. connect( filterModel, SIGNAL( modelReset()),
  41. this, SLOT( modelReset()));
  42. connect( modModel, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
  43. this, SLOT(dataChanged(QModelIndex,QModelIndex)));
  44. }
  45. CModListView::CModListView(QWidget *parent) :
  46. QWidget(parent),
  47. settingsListener(settings.listen["launcher"]["repositoryURL"]),
  48. ui(new Ui::CModListView)
  49. {
  50. settingsListener([&](const JsonNode &){ repositoriesChanged = true; });
  51. ui->setupUi(this);
  52. setupModModel();
  53. setupFilterModel();
  54. setupModsView();
  55. ui->progressWidget->setVisible(false);
  56. dlManager = nullptr;
  57. disableModInfo();
  58. loadRepositories();
  59. }
  60. void CModListView::loadRepositories()
  61. {
  62. manager->resetRepositories();
  63. for (auto entry : settings["launcher"]["repositoryURL"].Vector())
  64. {
  65. QString str = QString::fromUtf8(entry.String().c_str());
  66. // URL must be encoded to something else to get rid of symbols illegal in file names
  67. auto hashed = QCryptographicHash::hash(str.toUtf8(), QCryptographicHash::Md5);
  68. auto hashedStr = QString::fromUtf8(hashed.toHex());
  69. downloadFile(hashedStr + ".json", str, "repository index");
  70. }
  71. }
  72. CModListView::~CModListView()
  73. {
  74. delete ui;
  75. }
  76. void CModListView::showEvent(QShowEvent * event)
  77. {
  78. QWidget::showEvent(event);
  79. if (repositoriesChanged)
  80. {
  81. repositoriesChanged = false;
  82. loadRepositories();
  83. }
  84. }
  85. void CModListView::showModInfo()
  86. {
  87. enableModInfo();
  88. ui->modInfoWidget->show();
  89. ui->hideModInfoButton->setArrowType(Qt::RightArrow);
  90. ui->showInfoButton->setVisible(false);
  91. loadScreenshots();
  92. }
  93. void CModListView::hideModInfo()
  94. {
  95. ui->modInfoWidget->hide();
  96. ui->hideModInfoButton->setArrowType(Qt::LeftArrow);
  97. ui->hideModInfoButton->setEnabled(true);
  98. ui->showInfoButton->setVisible(true);
  99. }
  100. static QString replaceIfNotEmpty(QVariant value, QString pattern)
  101. {
  102. if (value.canConvert<QStringList>())
  103. return pattern.arg(value.toStringList().join(", "));
  104. if (value.canConvert<QString>())
  105. return pattern.arg(value.toString());
  106. // all valid types of data should have been filtered by code above
  107. assert(!value.isValid());
  108. return "";
  109. }
  110. static QString replaceIfNotEmpty(QStringList value, QString pattern)
  111. {
  112. if (!value.empty())
  113. return pattern.arg(value.join(", "));
  114. return "";
  115. }
  116. QString CModListView::genChangelogText(CModEntry &mod)
  117. {
  118. QString headerTemplate = "<p><span style=\" font-weight:600;\">%1: </span></p>";
  119. QString entryBegin = "<p align=\"justify\"><ul>";
  120. QString entryEnd = "</ul></p>";
  121. QString entryLine = "<li>%1</li>";
  122. //QString versionSeparator = "<hr/>";
  123. QString result;
  124. QVariantMap changelog = mod.getValue("changelog").toMap();
  125. QList<QString> versions = changelog.keys();
  126. std::sort(versions.begin(), versions.end(), [](QString lesser, QString greater)
  127. {
  128. return !CModEntry::compareVersions(lesser, greater);
  129. });
  130. for (auto & version : versions)
  131. {
  132. result += headerTemplate.arg(version);
  133. result += entryBegin;
  134. for (auto & line : changelog.value(version).toStringList())
  135. result += entryLine.arg(line);
  136. result += entryEnd;
  137. }
  138. return result;
  139. }
  140. QString CModListView::genModInfoText(CModEntry &mod)
  141. {
  142. QString prefix = "<p><span style=\" font-weight:600;\">%1: </span>"; // shared prefix
  143. QString lineTemplate = prefix + "%2</p>";
  144. QString urlTemplate = prefix + "<a href=\"%2\">%3</a></p>";
  145. QString textTemplate = prefix + "</p><p align=\"justify\">%2</p>";
  146. QString listTemplate = "<p align=\"justify\">%1: %2</p>";
  147. QString noteTemplate = "<p align=\"justify\">%1</p>";
  148. QString result;
  149. result += replaceIfNotEmpty(mod.getValue("name"), lineTemplate.arg(tr("Mod name")));
  150. result += replaceIfNotEmpty(mod.getValue("installedVersion"), lineTemplate.arg(tr("Installed version")));
  151. result += replaceIfNotEmpty(mod.getValue("latestVersion"), lineTemplate.arg(tr("Latest version")));
  152. if (mod.getValue("size").isValid())
  153. result += replaceIfNotEmpty(CModEntry::sizeToString(mod.getValue("size").toDouble()), lineTemplate.arg(tr("Download size")));
  154. result += replaceIfNotEmpty(mod.getValue("author"), lineTemplate.arg(tr("Authors")));
  155. if (mod.getValue("licenseURL").isValid())
  156. result += urlTemplate.arg(tr("License")).arg(mod.getValue("licenseURL").toString()).arg(mod.getValue("licenseName").toString());
  157. if (mod.getValue("contact").isValid())
  158. result += urlTemplate.arg(tr("Home")).arg(mod.getValue("contact").toString()).arg(mod.getValue("contact").toString());
  159. result += replaceIfNotEmpty(mod.getValue("depends"), lineTemplate.arg(tr("Required mods")));
  160. result += replaceIfNotEmpty(mod.getValue("conflicts"), lineTemplate.arg(tr("Conflicting mods")));
  161. result += replaceIfNotEmpty(mod.getValue("description"), textTemplate.arg(tr("Description")));
  162. result += "<p></p>"; // to get some empty space
  163. QString unknownDeps = tr("This mod can not be installed or enabled because following dependencies are not present");
  164. QString blockingMods = tr("This mod can not be enabled because following mods are incompatible with this mod");
  165. QString hasActiveDependentMods = tr("This mod can not be disabled because it is required to run following mods");
  166. QString hasDependentMods = tr("This mod can not be uninstalled or updated because it is required to run following mods");
  167. QString thisIsSubmod = tr("This is submod and it can not be installed or uninstalled separately from parent mod");
  168. QString notes;
  169. notes += replaceIfNotEmpty(findInvalidDependencies(mod.getName()), listTemplate.arg(unknownDeps));
  170. notes += replaceIfNotEmpty(findBlockingMods(mod.getName()), listTemplate.arg(blockingMods));
  171. if (mod.isEnabled())
  172. notes += replaceIfNotEmpty(findDependentMods(mod.getName(), true), listTemplate.arg(hasActiveDependentMods));
  173. if (mod.isInstalled())
  174. notes += replaceIfNotEmpty(findDependentMods(mod.getName(), false), listTemplate.arg(hasDependentMods));
  175. if (mod.getName().contains('.'))
  176. notes += noteTemplate.arg(thisIsSubmod);
  177. if (notes.size())
  178. result += textTemplate.arg(tr("Notes")).arg(notes);
  179. return result;
  180. }
  181. void CModListView::enableModInfo()
  182. {
  183. ui->hideModInfoButton->setEnabled(true);
  184. ui->showInfoButton->setVisible(true);
  185. }
  186. void CModListView::disableModInfo()
  187. {
  188. hideModInfo();
  189. ui->hideModInfoButton->setEnabled(false);
  190. ui->showInfoButton->setVisible(false);
  191. ui->disableButton->setVisible(false);
  192. ui->enableButton->setVisible(false);
  193. ui->installButton->setVisible(false);
  194. ui->uninstallButton->setVisible(false);
  195. ui->updateButton->setVisible(false);
  196. }
  197. void CModListView::dataChanged(const QModelIndex & topleft, const QModelIndex & bottomRight)
  198. {
  199. selectMod(ui->allModsView->currentIndex());
  200. }
  201. void CModListView::selectMod(const QModelIndex & index)
  202. {
  203. if (!index.isValid())
  204. {
  205. disableModInfo();
  206. }
  207. else
  208. {
  209. auto mod = modModel->getMod(index.data(ModRoles::ModNameRole).toString());
  210. ui->modInfoBrowser->setHtml(genModInfoText(mod));
  211. ui->changelogBrowser->setHtml(genChangelogText(mod));
  212. bool hasInvalidDeps = !findInvalidDependencies(index.data(ModRoles::ModNameRole).toString()).empty();
  213. bool hasBlockingMods = !findBlockingMods(index.data(ModRoles::ModNameRole).toString()).empty();
  214. bool hasDependentMods = !findDependentMods(index.data(ModRoles::ModNameRole).toString(), true).empty();
  215. ui->hideModInfoButton->setEnabled(true);
  216. ui->showInfoButton->setVisible(!ui->modInfoWidget->isVisible());
  217. ui->disableButton->setVisible(mod.isEnabled());
  218. ui->enableButton->setVisible(mod.isDisabled());
  219. ui->installButton->setVisible(mod.isAvailable() && !mod.getName().contains('.'));
  220. ui->uninstallButton->setVisible(mod.isInstalled() && !mod.getName().contains('.'));
  221. ui->updateButton->setVisible(mod.isUpdateable());
  222. // Block buttons if action is not allowed at this time
  223. // TODO: automate handling of some of these cases instead of forcing player
  224. // to resolve all conflicts manually.
  225. ui->disableButton->setEnabled(!hasDependentMods);
  226. ui->enableButton->setEnabled(!hasBlockingMods && !hasInvalidDeps);
  227. ui->installButton->setEnabled(!hasInvalidDeps);
  228. ui->uninstallButton->setEnabled(!hasDependentMods);
  229. ui->updateButton->setEnabled(!hasInvalidDeps && !hasDependentMods);
  230. loadScreenshots();
  231. }
  232. }
  233. void CModListView::keyPressEvent(QKeyEvent * event)
  234. {
  235. if (event->key() == Qt::Key_Escape && ui->modInfoWidget->isVisible() )
  236. {
  237. hideModInfo();
  238. }
  239. else
  240. {
  241. return QWidget::keyPressEvent(event);
  242. }
  243. }
  244. void CModListView::modSelected(const QModelIndex & current, const QModelIndex & )
  245. {
  246. selectMod(current);
  247. }
  248. void CModListView::on_hideModInfoButton_clicked()
  249. {
  250. if (ui->modInfoWidget->isVisible())
  251. hideModInfo();
  252. else
  253. showModInfo();
  254. }
  255. void CModListView::on_allModsView_activated(const QModelIndex &index)
  256. {
  257. showModInfo();
  258. selectMod(index);
  259. }
  260. void CModListView::on_lineEdit_textChanged(const QString &arg1)
  261. {
  262. QRegExp regExp(arg1, Qt::CaseInsensitive, QRegExp::Wildcard);
  263. filterModel->setFilterRegExp(regExp);
  264. }
  265. void CModListView::on_comboBox_currentIndexChanged(int index)
  266. {
  267. switch (index)
  268. {
  269. break; case 0: filterModel->setTypeFilter(ModStatus::MASK_NONE, ModStatus::MASK_NONE);
  270. break; case 1: filterModel->setTypeFilter(ModStatus::MASK_NONE, ModStatus::INSTALLED);
  271. break; case 2: filterModel->setTypeFilter(ModStatus::INSTALLED, ModStatus::INSTALLED);
  272. break; case 3: filterModel->setTypeFilter(ModStatus::UPDATEABLE, ModStatus::UPDATEABLE);
  273. break; case 4: filterModel->setTypeFilter(ModStatus::ENABLED | ModStatus::INSTALLED, ModStatus::ENABLED | ModStatus::INSTALLED);
  274. break; case 5: filterModel->setTypeFilter(ModStatus::INSTALLED, ModStatus::ENABLED | ModStatus::INSTALLED);
  275. }
  276. }
  277. QStringList CModListView::findInvalidDependencies(QString mod)
  278. {
  279. QStringList ret;
  280. for (QString requrement : modModel->getRequirements(mod))
  281. {
  282. if (!modModel->hasMod(requrement))
  283. ret += requrement;
  284. }
  285. return ret;
  286. }
  287. QStringList CModListView::findBlockingMods(QString mod)
  288. {
  289. QStringList ret;
  290. auto required = modModel->getRequirements(mod);
  291. for (QString name : modModel->getModList())
  292. {
  293. auto mod = modModel->getMod(name);
  294. if (mod.isEnabled())
  295. {
  296. // one of enabled mods have requirement (or this mod) marked as conflict
  297. for (auto conflict : mod.getValue("conflicts").toStringList())
  298. if (required.contains(conflict))
  299. ret.push_back(name);
  300. }
  301. }
  302. return ret;
  303. }
  304. QStringList CModListView::findDependentMods(QString mod, bool excludeDisabled)
  305. {
  306. QStringList ret;
  307. for (QString modName : modModel->getModList())
  308. {
  309. auto current = modModel->getMod(modName);
  310. if (!current.isInstalled())
  311. continue;
  312. if (current.getValue("depends").toStringList().contains(mod) &&
  313. !(current.isDisabled() && excludeDisabled))
  314. ret += modName;
  315. }
  316. return ret;
  317. }
  318. void CModListView::on_enableButton_clicked()
  319. {
  320. QString modName = ui->allModsView->currentIndex().data(ModRoles::ModNameRole).toString();
  321. assert(findBlockingMods(modName).empty());
  322. assert(findInvalidDependencies(modName).empty());
  323. for (auto & name : modModel->getRequirements(modName))
  324. if (modModel->getMod(name).isDisabled())
  325. manager->enableMod(name);
  326. checkManagerErrors();
  327. }
  328. void CModListView::on_disableButton_clicked()
  329. {
  330. QString modName = ui->allModsView->currentIndex().data(ModRoles::ModNameRole).toString();
  331. if (modModel->hasMod(modName) &&
  332. modModel->getMod(modName).isEnabled())
  333. manager->disableMod(modName);
  334. checkManagerErrors();
  335. }
  336. void CModListView::on_updateButton_clicked()
  337. {
  338. QString modName = ui->allModsView->currentIndex().data(ModRoles::ModNameRole).toString();
  339. assert(findInvalidDependencies(modName).empty());
  340. for (auto & name : modModel->getRequirements(modName))
  341. {
  342. auto mod = modModel->getMod(name);
  343. // update required mod, install missing (can be new dependency)
  344. if (mod.isUpdateable() || !mod.isInstalled())
  345. downloadFile(name + ".zip", mod.getValue("download").toString(), "mods");
  346. }
  347. }
  348. void CModListView::on_uninstallButton_clicked()
  349. {
  350. QString modName = ui->allModsView->currentIndex().data(ModRoles::ModNameRole).toString();
  351. // NOTE: perhaps add "manually installed" flag and uninstall those dependencies that don't have it?
  352. if (modModel->hasMod(modName) &&
  353. modModel->getMod(modName).isInstalled())
  354. {
  355. if (modModel->getMod(modName).isEnabled())
  356. manager->disableMod(modName);
  357. manager->uninstallMod(modName);
  358. }
  359. checkManagerErrors();
  360. }
  361. void CModListView::on_installButton_clicked()
  362. {
  363. QString modName = ui->allModsView->currentIndex().data(ModRoles::ModNameRole).toString();
  364. assert(findInvalidDependencies(modName).empty());
  365. for (auto & name : modModel->getRequirements(modName))
  366. {
  367. auto mod = modModel->getMod(name);
  368. if (!mod.isInstalled())
  369. downloadFile(name + ".zip", mod.getValue("download").toString(), "mods");
  370. }
  371. }
  372. void CModListView::downloadFile(QString file, QString url, QString description)
  373. {
  374. if (!dlManager)
  375. {
  376. dlManager = new CDownloadManager();
  377. ui->progressWidget->setVisible(true);
  378. connect(dlManager, SIGNAL(downloadProgress(qint64,qint64)),
  379. this, SLOT(downloadProgress(qint64,qint64)));
  380. connect(dlManager, SIGNAL(finished(QStringList,QStringList,QStringList)),
  381. this, SLOT(downloadFinished(QStringList,QStringList,QStringList)));
  382. QString progressBarFormat = "Downloading %s%. %p% (%v KB out of %m KB) finished";
  383. progressBarFormat.replace("%s%", description);
  384. ui->progressBar->setFormat(progressBarFormat);
  385. }
  386. dlManager->downloadFile(QUrl(url), file);
  387. }
  388. void CModListView::downloadProgress(qint64 current, qint64 max)
  389. {
  390. // display progress, in kilobytes
  391. ui->progressBar->setValue(current/1024);
  392. ui->progressBar->setMaximum(max/1024);
  393. }
  394. void CModListView::downloadFinished(QStringList savedFiles, QStringList failedFiles, QStringList errors)
  395. {
  396. QString title = "Download failed";
  397. QString firstLine = "Unable to download all files.\n\nEncountered errors:\n\n";
  398. QString lastLine = "\n\nInstall successfully downloaded?";
  399. // if all files were d/loaded there should be no errors. And on failure there must be an error
  400. assert(failedFiles.empty() == errors.empty());
  401. if (savedFiles.empty())
  402. {
  403. // no successfully downloaded mods
  404. QMessageBox::warning(this, title, firstLine + errors.join("\n"), QMessageBox::Ok, QMessageBox::Ok );
  405. }
  406. else if (!failedFiles.empty())
  407. {
  408. // some mods were not downloaded
  409. int result = QMessageBox::warning (this, title, firstLine + errors.join("\n") + lastLine,
  410. QMessageBox::Yes | QMessageBox::No, QMessageBox::No );
  411. if (result == QMessageBox::Yes)
  412. installFiles(savedFiles);
  413. }
  414. else
  415. {
  416. // everything OK
  417. installFiles(savedFiles);
  418. }
  419. // remove progress bar after some delay so user can see that download was complete and not interrupted.
  420. QTimer::singleShot(1000, this, SLOT(hideProgressBar()));
  421. dlManager->deleteLater();
  422. dlManager = nullptr;
  423. }
  424. void CModListView::hideProgressBar()
  425. {
  426. if (dlManager == nullptr) // it was not recreated meanwhile
  427. {
  428. ui->progressWidget->setVisible(false);
  429. ui->progressBar->setMaximum(0);
  430. ui->progressBar->setValue(0);
  431. }
  432. }
  433. void CModListView::installFiles(QStringList files)
  434. {
  435. QStringList mods;
  436. QStringList images;
  437. // TODO: some better way to separate zip's with mods and downloaded repository files
  438. for (QString filename : files)
  439. {
  440. if (filename.endsWith(".zip"))
  441. mods.push_back(filename);
  442. if (filename.endsWith(".json"))
  443. manager->loadRepository(filename);
  444. if (filename.endsWith(".png"))
  445. images.push_back(filename);
  446. }
  447. if (!mods.empty())
  448. installMods(mods);
  449. if (!images.empty())
  450. loadScreenshots();
  451. }
  452. void CModListView::installMods(QStringList archives)
  453. {
  454. QStringList modNames;
  455. for (QString archive : archives)
  456. {
  457. // get basename out of full file name
  458. // remove path remove extension
  459. QString modName = archive.section('/', -1, -1).section('.', 0, 0);
  460. modNames.push_back(modName);
  461. }
  462. QStringList modsToEnable;
  463. // disable mod(s), to properly recalculate dependencies, if changed
  464. for (QString mod : boost::adaptors::reverse(modNames))
  465. {
  466. CModEntry entry = modModel->getMod(mod);
  467. if (entry.isInstalled())
  468. {
  469. // enable mod if installed and enabled
  470. if (entry.isEnabled())
  471. modsToEnable.push_back(mod);
  472. }
  473. else
  474. {
  475. // enable mod if m
  476. if (settings["launcher"]["enableInstalledMods"].Bool())
  477. modsToEnable.push_back(mod);
  478. }
  479. }
  480. // uninstall old version of mod, if installed
  481. for (QString mod : boost::adaptors::reverse(modNames))
  482. {
  483. if (modModel->getMod(mod).isInstalled())
  484. manager->uninstallMod(mod);
  485. }
  486. for (int i=0; i<modNames.size(); i++)
  487. manager->installMod(modNames[i], archives[i]);
  488. std::function<void(QString)> enableMod;
  489. enableMod = [&](QString modName)
  490. {
  491. auto mod = modModel->getMod(modName);
  492. if (mod.isInstalled() && !mod.getValue("keepDisabled").toBool())
  493. {
  494. if (manager->enableMod(modName))
  495. {
  496. for (QString child : modModel->getChildren(modName))
  497. enableMod(child);
  498. }
  499. }
  500. };
  501. for (QString mod : modsToEnable)
  502. {
  503. enableMod(mod);
  504. }
  505. for (QString archive : archives)
  506. QFile::remove(archive);
  507. checkManagerErrors();
  508. }
  509. void CModListView::on_pushButton_clicked()
  510. {
  511. delete dlManager;
  512. dlManager = nullptr;
  513. hideProgressBar();
  514. }
  515. void CModListView::modelReset()
  516. {
  517. if (ui->modInfoWidget->isVisible())
  518. selectMod(filterModel->rowCount() > 0 ? filterModel->index(0,0) : QModelIndex());
  519. }
  520. void CModListView::checkManagerErrors()
  521. {
  522. QString errors = manager->getErrors().join('\n');
  523. if (errors.size() != 0)
  524. {
  525. QString title = "Operation failed";
  526. QString description = "Encountered errors:\n" + errors;
  527. QMessageBox::warning(this, title, description, QMessageBox::Ok, QMessageBox::Ok );
  528. }
  529. }
  530. void CModListView::on_tabWidget_currentChanged(int index)
  531. {
  532. loadScreenshots();
  533. }
  534. void CModListView::loadScreenshots()
  535. {
  536. if (ui->tabWidget->currentIndex() == 2 && ui->modInfoWidget->isVisible())
  537. {
  538. ui->screenshotsList->clear();
  539. QString modName = ui->allModsView->currentIndex().data(ModRoles::ModNameRole).toString();
  540. assert(modModel->hasMod(modName)); //should be filtered out by check above
  541. for (QString & url : modModel->getMod(modName).getValue("screenshots").toStringList())
  542. {
  543. // URL must be encoded to something else to get rid of symbols illegal in file names
  544. auto hashed = QCryptographicHash::hash(url.toUtf8(), QCryptographicHash::Md5);
  545. auto hashedStr = QString::fromUtf8(hashed.toHex());
  546. QString fullPath = CLauncherDirs::get().downloadsPath() + '/' + hashedStr + ".png";
  547. QPixmap pixmap(fullPath);
  548. if (pixmap.isNull())
  549. {
  550. // image file not exists or corrupted - try to redownload
  551. downloadFile(hashedStr + ".png", url, "screenshots");
  552. }
  553. else
  554. {
  555. // managed to load cached image
  556. QIcon icon(pixmap);
  557. QListWidgetItem * item = new QListWidgetItem(icon, QString(tr("Screenshot %1")).arg(ui->screenshotsList->count() + 1));
  558. ui->screenshotsList->addItem(item);
  559. }
  560. }
  561. }
  562. }
  563. void CModListView::on_screenshotsList_clicked(const QModelIndex &index)
  564. {
  565. if (index.isValid())
  566. {
  567. QIcon icon = ui->screenshotsList->item(index.row())->icon();
  568. auto pixmap = icon.pixmap(icon.availableSizes()[0]);
  569. ImageViewer::showPixmap(pixmap, this);
  570. }
  571. }
  572. void CModListView::on_showInfoButton_clicked()
  573. {
  574. showModInfo();
  575. }