cmodlistview_moc.cpp 20 KB

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