cmodlistview_moc.cpp 21 KB

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