1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844
|
/*
* cmodlistview_moc.cpp, part of VCMI engine
*
* Authors: listed in file AUTHORS in main folder
*
* License: GNU General Public License v2.0 or later
* Full text of license available in license.txt file, in main folder
*
*/
#include "StdInc.h"
#include "cmodlistview_moc.h"
#include "ui_cmodlistview_moc.h"
#include "imageviewer_moc.h"
#include "../mainwindow_moc.h"
#include <QJsonArray>
#include <QCryptographicHash>
#include <QRegularExpression>
#include "cmodlistmodel_moc.h"
#include "cmodmanager.h"
#include "cdownloadmanager_moc.h"
#include "../launcherdirs.h"
#include "../jsonutils.h"
#include "../../lib/CConfigHandler.h"
void CModListView::setupModModel()
{
modModel = new CModListModel(this);
manager = vstd::make_unique<CModManager>(modModel);
connect(manager.get(), &CModManager::extraResolutionsEnabledChanged,
this, &CModListView::extraResolutionsEnabledChanged);
}
void CModListView::setupFilterModel()
{
filterModel = new CModFilterModel(modModel, this);
filterModel->setFilterKeyColumn(-1); // filter across all columns
filterModel->setSortCaseSensitivity(Qt::CaseInsensitive); // to make it more user-friendly
filterModel->setDynamicSortFilter(true);
}
void CModListView::setupModsView()
{
ui->allModsView->setModel(filterModel);
// input data is not sorted - sort it before display
ui->allModsView->sortByColumn(ModFields::TYPE, Qt::AscendingOrder);
ui->allModsView->header()->setSectionResizeMode(ModFields::STATUS_ENABLED, QHeaderView::Fixed);
ui->allModsView->header()->setSectionResizeMode(ModFields::STATUS_UPDATE, QHeaderView::Fixed);
QSettings s(Ui::teamName, Ui::appName);
auto state = s.value("AllModsView/State").toByteArray();
if(!state.isNull()) //read last saved settings
{
ui->allModsView->header()->restoreState(state);
}
else //default //TODO: default high-DPI scaling
{
ui->allModsView->setColumnWidth(ModFields::NAME, 185);
ui->allModsView->setColumnWidth(ModFields::STATUS_ENABLED, 30);
ui->allModsView->setColumnWidth(ModFields::STATUS_UPDATE, 30);
ui->allModsView->setColumnWidth(ModFields::TYPE, 75);
ui->allModsView->setColumnWidth(ModFields::SIZE, 80);
ui->allModsView->setColumnWidth(ModFields::VERSION, 60);
}
ui->allModsView->setUniformRowHeights(true);
connect(ui->allModsView->selectionModel(), SIGNAL(currentRowChanged(const QModelIndex&,const QModelIndex&)),
this, SLOT(modSelected(const QModelIndex&,const QModelIndex&)));
connect(filterModel, SIGNAL(modelReset()),
this, SLOT(modelReset()));
connect(modModel, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
this, SLOT(dataChanged(QModelIndex,QModelIndex)));
}
CModListView::CModListView(QWidget * parent)
: QWidget(parent), settingsListener(settings.listen["launcher"]["repositoryURL"]), ui(new Ui::CModListView)
{
settingsListener([&](const JsonNode &){ repositoriesChanged = true; });
ui->setupUi(this);
setupModModel();
setupFilterModel();
setupModsView();
ui->progressWidget->setVisible(false);
dlManager = nullptr;
disableModInfo();
if(settings["launcher"]["autoCheckRepositories"].Bool())
{
loadRepositories();
}
else
{
manager->resetRepositories();
}
#ifdef Q_OS_IOS
for(auto * scrollWidget : {
(QAbstractItemView*)ui->allModsView,
(QAbstractItemView*)ui->screenshotsList})
{
QScroller::grabGesture(scrollWidget, QScroller::LeftMouseButtonGesture);
scrollWidget->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
scrollWidget->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
}
#endif
}
void CModListView::loadRepositories()
{
manager->resetRepositories();
for(auto entry : settings["launcher"]["repositoryURL"].Vector())
{
QString str = QString::fromUtf8(entry.String().c_str());
// URL must be encoded to something else to get rid of symbols illegal in file names
auto hashed = QCryptographicHash::hash(str.toUtf8(), QCryptographicHash::Md5);
auto hashedStr = QString::fromUtf8(hashed.toHex());
downloadFile(hashedStr + ".json", str, "repository index");
}
}
CModListView::~CModListView()
{
QSettings s(Ui::teamName, Ui::appName);
s.setValue("AllModsView/State", ui->allModsView->header()->saveState());
delete ui;
}
void CModListView::showEvent(QShowEvent * event)
{
QWidget::showEvent(event);
if(repositoriesChanged)
{
repositoriesChanged = false;
if(settings["launcher"]["autoCheckRepositories"].Bool())
{
loadRepositories();
}
}
}
void CModListView::showModInfo()
{
enableModInfo();
ui->modInfoWidget->show();
ui->hideModInfoButton->setArrowType(Qt::RightArrow);
ui->showInfoButton->setVisible(false);
loadScreenshots();
}
void CModListView::hideModInfo()
{
ui->modInfoWidget->hide();
ui->hideModInfoButton->setArrowType(Qt::LeftArrow);
ui->hideModInfoButton->setEnabled(true);
ui->showInfoButton->setVisible(true);
}
static QString replaceIfNotEmpty(QVariant value, QString pattern)
{
if(value.canConvert<QStringList>())
return pattern.arg(value.toStringList().join(", "));
if(value.canConvert<QString>())
return pattern.arg(value.toString());
// all valid types of data should have been filtered by code above
assert(!value.isValid());
return "";
}
static QString replaceIfNotEmpty(QStringList value, QString pattern)
{
if(!value.empty())
return pattern.arg(value.join(", "));
return "";
}
QString CModListView::genChangelogText(CModEntry & mod)
{
QString headerTemplate = "<p><span style=\" font-weight:600;\">%1: </span></p>";
QString entryBegin = "<p align=\"justify\"><ul>";
QString entryEnd = "</ul></p>";
QString entryLine = "<li>%1</li>";
//QString versionSeparator = "<hr/>";
QString result;
QVariantMap changelog = mod.getValue("changelog").toMap();
QList<QString> versions = changelog.keys();
std::sort(versions.begin(), versions.end(), [](QString lesser, QString greater)
{
return !CModEntry::compareVersions(lesser, greater);
});
for(auto & version : versions)
{
result += headerTemplate.arg(version);
result += entryBegin;
for(auto & line : changelog.value(version).toStringList())
result += entryLine.arg(line);
result += entryEnd;
}
return result;
}
QString CModListView::genModInfoText(CModEntry & mod)
{
QString prefix = "<p><span style=\" font-weight:600;\">%1: </span>"; // shared prefix
QString redPrefix = "<p><span style=\" font-weight:600; color:red\">%1: </span>"; // shared prefix
QString lineTemplate = prefix + "%2</p>";
QString urlTemplate = prefix + "<a href=\"%2\">%3</a></p>";
QString textTemplate = prefix + "</p><p align=\"justify\">%2</p>";
QString listTemplate = "<p align=\"justify\">%1: %2</p>";
QString noteTemplate = "<p align=\"justify\">%1</p>";
QString compatibleString = prefix + "Mod is compatible</p>";
QString incompatibleString = redPrefix + "Mod is incompatible</p>";
QString supportedVersions = redPrefix + "%2 %3 %4</p>";
QString result;
result += replaceIfNotEmpty(mod.getValue("name"), lineTemplate.arg(tr("Mod name")));
result += replaceIfNotEmpty(mod.getValue("installedVersion"), lineTemplate.arg(tr("Installed version")));
result += replaceIfNotEmpty(mod.getValue("latestVersion"), lineTemplate.arg(tr("Latest version")));
if(mod.getValue("size").isValid())
result += replaceIfNotEmpty(CModEntry::sizeToString(mod.getValue("size").toDouble()), lineTemplate.arg(tr("Download size")));
result += replaceIfNotEmpty(mod.getValue("author"), lineTemplate.arg(tr("Authors")));
if(mod.getValue("licenseURL").isValid())
result += urlTemplate.arg(tr("License")).arg(mod.getValue("licenseURL").toString()).arg(mod.getValue("licenseName").toString());
if(mod.getValue("contact").isValid())
result += urlTemplate.arg(tr("Home")).arg(mod.getValue("contact").toString()).arg(mod.getValue("contact").toString());
//compatibility info
if(mod.isCompatible())
result += compatibleString.arg(tr("Compatibility"));
else
{
auto compatibilityInfo = mod.getValue("compatibility").toMap();
auto minStr = compatibilityInfo.value("min").toString();
auto maxStr = compatibilityInfo.value("max").toString();
result += incompatibleString.arg(tr("Compatibility"));
if(minStr == maxStr)
result += supportedVersions.arg(tr("Required VCMI version"), minStr, "", "");
else
{
if(minStr.isEmpty() || maxStr.isEmpty())
{
if(minStr.isEmpty())
result += supportedVersions.arg(tr("Supported VCMI version"), maxStr, ", ", "please upgrade mod");
else
result += supportedVersions.arg(tr("Required VCMI version"), minStr, " ", "or above");
}
else
result += supportedVersions.arg(tr("Supported VCMI versions"), minStr, " - ", maxStr);
}
}
result += replaceIfNotEmpty(mod.getValue("depends"), lineTemplate.arg(tr("Required mods")));
result += replaceIfNotEmpty(mod.getValue("conflicts"), lineTemplate.arg(tr("Conflicting mods")));
result += replaceIfNotEmpty(mod.getValue("description"), textTemplate.arg(tr("Description")));
result += "<p></p>"; // to get some empty space
QString unknownDeps = tr("This mod can not be installed or enabled because following dependencies are not present");
QString blockingMods = tr("This mod can not be enabled because following mods are incompatible with this mod");
QString hasActiveDependentMods = tr("This mod can not be disabled because it is required to run following mods");
QString hasDependentMods = tr("This mod can not be uninstalled or updated because it is required to run following mods");
QString thisIsSubmod = tr("This is submod and it can not be installed or uninstalled separately from parent mod");
QString notes;
notes += replaceIfNotEmpty(findInvalidDependencies(mod.getName()), listTemplate.arg(unknownDeps));
notes += replaceIfNotEmpty(findBlockingMods(mod.getName()), listTemplate.arg(blockingMods));
if(mod.isEnabled())
notes += replaceIfNotEmpty(findDependentMods(mod.getName(), true), listTemplate.arg(hasActiveDependentMods));
if(mod.isInstalled())
notes += replaceIfNotEmpty(findDependentMods(mod.getName(), false), listTemplate.arg(hasDependentMods));
if(mod.getName().contains('.'))
notes += noteTemplate.arg(thisIsSubmod);
if(notes.size())
result += textTemplate.arg(tr("Notes")).arg(notes);
return result;
}
void CModListView::enableModInfo()
{
ui->hideModInfoButton->setEnabled(true);
ui->showInfoButton->setVisible(true);
}
void CModListView::disableModInfo()
{
hideModInfo();
ui->hideModInfoButton->setEnabled(false);
ui->showInfoButton->setVisible(false);
ui->disableButton->setVisible(false);
ui->enableButton->setVisible(false);
ui->installButton->setVisible(false);
ui->uninstallButton->setVisible(false);
ui->updateButton->setVisible(false);
}
void CModListView::dataChanged(const QModelIndex & topleft, const QModelIndex & bottomRight)
{
selectMod(ui->allModsView->currentIndex());
}
void CModListView::selectMod(const QModelIndex & index)
{
if(!index.isValid())
{
disableModInfo();
}
else
{
auto mod = modModel->getMod(index.data(ModRoles::ModNameRole).toString());
ui->modInfoBrowser->setHtml(genModInfoText(mod));
ui->changelogBrowser->setHtml(genChangelogText(mod));
bool hasInvalidDeps = !findInvalidDependencies(index.data(ModRoles::ModNameRole).toString()).empty();
bool hasBlockingMods = !findBlockingMods(index.data(ModRoles::ModNameRole).toString()).empty();
bool hasDependentMods = !findDependentMods(index.data(ModRoles::ModNameRole).toString(), true).empty();
ui->hideModInfoButton->setEnabled(true);
ui->showInfoButton->setVisible(!ui->modInfoWidget->isVisible());
ui->disableButton->setVisible(mod.isEnabled());
ui->enableButton->setVisible(mod.isDisabled());
ui->installButton->setVisible(mod.isAvailable() && !mod.getName().contains('.'));
ui->uninstallButton->setVisible(mod.isInstalled() && !mod.getName().contains('.'));
ui->updateButton->setVisible(mod.isUpdateable());
// Block buttons if action is not allowed at this time
// TODO: automate handling of some of these cases instead of forcing player
// to resolve all conflicts manually.
ui->disableButton->setEnabled(!hasDependentMods && !mod.isEssential());
ui->enableButton->setEnabled(!hasBlockingMods && !hasInvalidDeps);
ui->installButton->setEnabled(!hasInvalidDeps);
ui->uninstallButton->setEnabled(!hasDependentMods && !mod.isEssential());
ui->updateButton->setEnabled(!hasInvalidDeps && !hasDependentMods);
loadScreenshots();
}
}
bool CModListView::isExtraResolutionsModEnabled() const
{
return manager->isExtraResolutionsModEnabled();
}
void CModListView::keyPressEvent(QKeyEvent * event)
{
if(event->key() == Qt::Key_Escape && ui->modInfoWidget->isVisible())
{
hideModInfo();
}
else
{
return QWidget::keyPressEvent(event);
}
}
void CModListView::modSelected(const QModelIndex & current, const QModelIndex &)
{
selectMod(current);
}
void CModListView::on_hideModInfoButton_clicked()
{
if(ui->modInfoWidget->isVisible())
hideModInfo();
else
showModInfo();
}
void CModListView::on_allModsView_activated(const QModelIndex & index)
{
showModInfo();
selectMod(index);
}
void CModListView::on_lineEdit_textChanged(const QString & arg1)
{
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
auto baseStr = QRegularExpression::wildcardToRegularExpression(arg1, QRegularExpression::UnanchoredWildcardConversion);
#else
auto baseStr = QRegularExpression::wildcardToRegularExpression(arg1);
//Hack due to lack QRegularExpression::UnanchoredWildcardConversion in Qt5
baseStr.chop(3);
baseStr.remove(0,5);
#endif
QRegularExpression regExp{baseStr, QRegularExpression::CaseInsensitiveOption};
filterModel->setFilterRegularExpression(regExp);
}
void CModListView::on_comboBox_currentIndexChanged(int index)
{
switch(index)
{
case 0:
filterModel->setTypeFilter(ModStatus::MASK_NONE, ModStatus::MASK_NONE);
break;
case 1:
filterModel->setTypeFilter(ModStatus::MASK_NONE, ModStatus::INSTALLED);
break;
case 2:
filterModel->setTypeFilter(ModStatus::INSTALLED, ModStatus::INSTALLED);
break;
case 3:
filterModel->setTypeFilter(ModStatus::UPDATEABLE, ModStatus::UPDATEABLE);
break;
case 4:
filterModel->setTypeFilter(ModStatus::ENABLED | ModStatus::INSTALLED, ModStatus::ENABLED | ModStatus::INSTALLED);
break;
case 5:
filterModel->setTypeFilter(ModStatus::INSTALLED, ModStatus::ENABLED | ModStatus::INSTALLED);
break;
}
}
QStringList CModListView::findInvalidDependencies(QString mod)
{
QStringList ret;
for(QString requrement : modModel->getRequirements(mod))
{
if(!modModel->hasMod(requrement))
ret += requrement;
}
return ret;
}
QStringList CModListView::findBlockingMods(QString modUnderTest)
{
QStringList ret;
auto required = modModel->getRequirements(modUnderTest);
for(QString name : modModel->getModList())
{
auto mod = modModel->getMod(name);
if(mod.isEnabled())
{
// one of enabled mods have requirement (or this mod) marked as conflict
for(auto conflict : mod.getValue("conflicts").toStringList())
{
if(required.contains(conflict))
ret.push_back(name);
}
}
}
return ret;
}
QStringList CModListView::findDependentMods(QString mod, bool excludeDisabled)
{
QStringList ret;
for(QString modName : modModel->getModList())
{
auto current = modModel->getMod(modName);
if(!current.isInstalled())
continue;
if(current.getValue("depends").toStringList().contains(mod))
{
if(!(current.isDisabled() && excludeDisabled))
ret += modName;
}
}
return ret;
}
void CModListView::on_enableButton_clicked()
{
QString modName = ui->allModsView->currentIndex().data(ModRoles::ModNameRole).toString();
assert(findBlockingMods(modName).empty());
assert(findInvalidDependencies(modName).empty());
for(auto & name : modModel->getRequirements(modName))
{
if(modModel->getMod(name).isDisabled())
manager->enableMod(name);
}
checkManagerErrors();
}
void CModListView::on_disableButton_clicked()
{
QString modName = ui->allModsView->currentIndex().data(ModRoles::ModNameRole).toString();
if(modModel->hasMod(modName) && modModel->getMod(modName).isEnabled())
manager->disableMod(modName);
checkManagerErrors();
}
void CModListView::on_updateButton_clicked()
{
QString modName = ui->allModsView->currentIndex().data(ModRoles::ModNameRole).toString();
assert(findInvalidDependencies(modName).empty());
for(auto & name : modModel->getRequirements(modName))
{
auto mod = modModel->getMod(name);
// update required mod, install missing (can be new dependency)
if(mod.isUpdateable() || !mod.isInstalled())
downloadFile(name + ".zip", mod.getValue("download").toString(), "mods");
}
}
void CModListView::on_uninstallButton_clicked()
{
QString modName = ui->allModsView->currentIndex().data(ModRoles::ModNameRole).toString();
// NOTE: perhaps add "manually installed" flag and uninstall those dependencies that don't have it?
if(modModel->hasMod(modName) && modModel->getMod(modName).isInstalled())
{
if(modModel->getMod(modName).isEnabled())
manager->disableMod(modName);
manager->uninstallMod(modName);
}
checkManagerErrors();
}
void CModListView::on_installButton_clicked()
{
QString modName = ui->allModsView->currentIndex().data(ModRoles::ModNameRole).toString();
assert(findInvalidDependencies(modName).empty());
for(auto & name : modModel->getRequirements(modName))
{
auto mod = modModel->getMod(name);
if(!mod.isInstalled())
downloadFile(name + ".zip", mod.getValue("download").toString(), "mods");
}
}
void CModListView::downloadFile(QString file, QString url, QString description)
{
if(!dlManager)
{
dlManager = new CDownloadManager();
ui->progressWidget->setVisible(true);
connect(dlManager, SIGNAL(downloadProgress(qint64,qint64)),
this, SLOT(downloadProgress(qint64,qint64)));
connect(dlManager, SIGNAL(finished(QStringList,QStringList,QStringList)),
this, SLOT(downloadFinished(QStringList,QStringList,QStringList)));
connect(modModel, &CModListModel::dataChanged, filterModel, &QAbstractItemModel::dataChanged);
QString progressBarFormat = "Downloading %s%. %p% (%v KB out of %m KB) finished";
progressBarFormat.replace("%s%", description);
ui->progressBar->setFormat(progressBarFormat);
}
dlManager->downloadFile(QUrl(url), file);
}
void CModListView::downloadProgress(qint64 current, qint64 max)
{
// display progress, in kilobytes
ui->progressBar->setValue(current / 1024);
ui->progressBar->setMaximum(max / 1024);
}
void CModListView::downloadFinished(QStringList savedFiles, QStringList failedFiles, QStringList errors)
{
QString title = "Download failed";
QString firstLine = "Unable to download all files.\n\nEncountered errors:\n\n";
QString lastLine = "\n\nInstall successfully downloaded?";
bool doInstallFiles = false;
// if all files were d/loaded there should be no errors. And on failure there must be an error
assert(failedFiles.empty() == errors.empty());
if(savedFiles.empty())
{
// no successfully downloaded mods
QMessageBox::warning(this, title, firstLine + errors.join("\n"), QMessageBox::Ok, QMessageBox::Ok);
}
else if(!failedFiles.empty())
{
// some mods were not downloaded
int result = QMessageBox::warning (this, title, firstLine + errors.join("\n") + lastLine,
QMessageBox::Yes | QMessageBox::No, QMessageBox::No );
if(result == QMessageBox::Yes)
doInstallFiles = true;
}
else
{
// everything OK
doInstallFiles = true;
}
// remove progress bar after some delay so user can see that download was complete and not interrupted.
QTimer::singleShot(1000, this, SLOT(hideProgressBar()));
dlManager->deleteLater();
dlManager = nullptr;
if(doInstallFiles)
installFiles(savedFiles);
}
void CModListView::hideProgressBar()
{
if(dlManager == nullptr) // it was not recreated meanwhile
{
ui->progressWidget->setVisible(false);
ui->progressBar->setMaximum(0);
ui->progressBar->setValue(0);
}
}
void CModListView::installFiles(QStringList files)
{
QStringList mods;
QStringList images;
// TODO: some better way to separate zip's with mods and downloaded repository files
for(QString filename : files)
{
if(filename.endsWith(".zip"))
mods.push_back(filename);
if(filename.endsWith(".json"))
{
//download and merge additional files
auto repodata = JsonUtils::JsonFromFile(filename).toMap();
if(repodata.value("name").isNull())
{
for(const auto & key : repodata.keys())
{
auto modjson = repodata[key].toMap().value("mod");
if(!modjson.isNull())
{
downloadFile(key + ".json", modjson.toString(), "mod json");
}
}
}
else
{
auto modn = QFileInfo(filename).baseName();
QVariantMap temp;
temp[modn] = repodata;
repodata = temp;
}
manager->loadRepository(repodata);
}
if(filename.endsWith(".png"))
images.push_back(filename);
}
if(!mods.empty())
installMods(mods);
if(!images.empty())
loadScreenshots();
}
void CModListView::installMods(QStringList archives)
{
QStringList modNames;
for(QString archive : archives)
{
// get basename out of full file name
// remove path remove extension
QString modName = archive.section('/', -1, -1).section('.', 0, 0);
modNames.push_back(modName);
}
QStringList modsToEnable;
// disable mod(s), to properly recalculate dependencies, if changed
for(QString mod : boost::adaptors::reverse(modNames))
{
CModEntry entry = modModel->getMod(mod);
if(entry.isInstalled())
{
// enable mod if installed and enabled
if(entry.isEnabled())
modsToEnable.push_back(mod);
}
else
{
// enable mod if m
if(settings["launcher"]["enableInstalledMods"].Bool())
modsToEnable.push_back(mod);
}
}
// uninstall old version of mod, if installed
for(QString mod : boost::adaptors::reverse(modNames))
{
if(modModel->getMod(mod).isInstalled())
manager->uninstallMod(mod);
}
for(int i = 0; i < modNames.size(); i++)
manager->installMod(modNames[i], archives[i]);
std::function<void(QString)> enableMod;
enableMod = [&](QString modName)
{
auto mod = modModel->getMod(modName);
if(mod.isInstalled() && !mod.getValue("keepDisabled").toBool())
{
if(mod.isDisabled() && manager->enableMod(modName))
{
for(QString child : modModel->getChildren(modName))
enableMod(child);
}
}
};
for(QString mod : modsToEnable)
{
enableMod(mod);
}
for(QString archive : archives)
QFile::remove(archive);
checkManagerErrors();
}
void CModListView::on_refreshButton_clicked()
{
loadRepositories();
}
void CModListView::on_pushButton_clicked()
{
delete dlManager;
dlManager = nullptr;
hideProgressBar();
}
void CModListView::modelReset()
{
if(ui->modInfoWidget->isVisible())
selectMod(filterModel->rowCount() > 0 ? filterModel->index(0, 0) : QModelIndex());
}
void CModListView::checkManagerErrors()
{
QString errors = manager->getErrors().join('\n');
if(errors.size() != 0)
{
QString title = "Operation failed";
QString description = "Encountered errors:\n" + errors;
QMessageBox::warning(this, title, description, QMessageBox::Ok, QMessageBox::Ok);
}
}
void CModListView::on_tabWidget_currentChanged(int index)
{
loadScreenshots();
}
void CModListView::loadScreenshots()
{
if(ui->tabWidget->currentIndex() == 2 && ui->modInfoWidget->isVisible())
{
ui->screenshotsList->clear();
QString modName = ui->allModsView->currentIndex().data(ModRoles::ModNameRole).toString();
assert(modModel->hasMod(modName)); //should be filtered out by check above
for(QString & url : modModel->getMod(modName).getValue("screenshots").toStringList())
{
// URL must be encoded to something else to get rid of symbols illegal in file names
auto hashed = QCryptographicHash::hash(url.toUtf8(), QCryptographicHash::Md5);
auto hashedStr = QString::fromUtf8(hashed.toHex());
QString fullPath = CLauncherDirs::get().downloadsPath() + '/' + hashedStr + ".png";
QPixmap pixmap(fullPath);
if(pixmap.isNull())
{
// image file not exists or corrupted - try to redownload
downloadFile(hashedStr + ".png", url, "screenshots");
}
else
{
// managed to load cached image
QIcon icon(pixmap);
QListWidgetItem * item = new QListWidgetItem(icon, QString(tr("Screenshot %1")).arg(ui->screenshotsList->count() + 1));
ui->screenshotsList->addItem(item);
}
}
}
}
void CModListView::on_screenshotsList_clicked(const QModelIndex & index)
{
if(index.isValid())
{
QIcon icon = ui->screenshotsList->item(index.row())->icon();
auto pixmap = icon.pixmap(icon.availableSizes()[0]);
ImageViewer::showPixmap(pixmap, this);
}
}
void CModListView::on_showInfoButton_clicked()
{
showModInfo();
}
const CModList & CModListView::getModList() const
{
assert(modModel);
return *modModel;
}
|