firstlaunch_moc.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. /*
  2. * firstlaunch_moc.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "firstlaunch_moc.h"
  12. #include "ui_firstlaunch_moc.h"
  13. #include "mainwindow_moc.h"
  14. #include "modManager/cmodlistview_moc.h"
  15. #include "../../lib/CConfigHandler.h"
  16. #include "../../lib/texts/CGeneralTextHandler.h"
  17. #include "../../lib/texts/Languages.h"
  18. #include "../../lib/VCMIDirs.h"
  19. #include "../../lib/filesystem/Filesystem.h"
  20. #include "../../vcmiqt/MessageBox.h"
  21. #include "../helper.h"
  22. #include "../languages.h"
  23. #include "../innoextract.h"
  24. #ifdef VCMI_IOS
  25. #include "ios/selectdirectory.h"
  26. #include "iOS_utils.h"
  27. #elif defined(VCMI_ANDROID)
  28. #include <QAndroidJniObject>
  29. #include <QtAndroid>
  30. static FirstLaunchView * thiz;
  31. extern "C" JNIEXPORT void JNICALL Java_eu_vcmi_vcmi_NativeMethods_heroesDataUpdate(JNIEnv * env, jclass cls)
  32. {
  33. thiz->heroesDataUpdate();
  34. }
  35. #endif
  36. FirstLaunchView::FirstLaunchView(QWidget * parent)
  37. : QWidget(parent)
  38. , ui(new Ui::FirstLaunchView)
  39. {
  40. ui->setupUi(this);
  41. enterSetup();
  42. activateTabLanguage();
  43. ui->lineEditDataSystem->setText(pathToQString(boost::filesystem::absolute(VCMIDirs::get().dataPaths().front())));
  44. ui->lineEditDataUser->setText(pathToQString(boost::filesystem::absolute(VCMIDirs::get().userDataPath())));
  45. Helper::enableScrollBySwiping(ui->listWidgetLanguage);
  46. #ifdef VCMI_MOBILE
  47. // This directory is not accessible to players without rooting of their device
  48. ui->lineEditDataSystem->hide();
  49. #endif
  50. #ifndef ENABLE_INNOEXTRACT
  51. ui->pushButtonGogInstall->hide();
  52. ui->labelDataGogTitle->hide();
  53. ui->labelDataGogDescr->hide();
  54. #endif
  55. }
  56. void FirstLaunchView::on_buttonTabLanguage_clicked()
  57. {
  58. activateTabLanguage();
  59. }
  60. void FirstLaunchView::on_buttonTabHeroesData_clicked()
  61. {
  62. activateTabHeroesData();
  63. }
  64. void FirstLaunchView::on_buttonTabModPreset_clicked()
  65. {
  66. activateTabModPreset();
  67. }
  68. void FirstLaunchView::on_listWidgetLanguage_currentRowChanged(int currentRow)
  69. {
  70. languageSelected(ui->listWidgetLanguage->item(currentRow)->data(Qt::UserRole).toString());
  71. }
  72. void FirstLaunchView::changeEvent(QEvent * event)
  73. {
  74. if(event->type() == QEvent::LanguageChange)
  75. {
  76. ui->retranslateUi(this);
  77. Languages::fillLanguages(ui->listWidgetLanguage, false);
  78. }
  79. QWidget::changeEvent(event);
  80. }
  81. void FirstLaunchView::on_pushButtonLanguageNext_clicked()
  82. {
  83. activateTabHeroesData();
  84. }
  85. void FirstLaunchView::on_pushButtonDataNext_clicked()
  86. {
  87. activateTabModPreset();
  88. }
  89. void FirstLaunchView::on_pushButtonDataBack_clicked()
  90. {
  91. activateTabLanguage();
  92. }
  93. void FirstLaunchView::on_pushButtonDataSearch_clicked()
  94. {
  95. heroesDataUpdate();
  96. }
  97. void FirstLaunchView::on_pushButtonDataCopy_clicked()
  98. {
  99. #ifdef VCMI_ANDROID
  100. thiz = this;
  101. QtAndroid::androidActivity().callMethod<void>("copyHeroesData");
  102. #else
  103. // iOS can't display modal dialogs when called directly on button press
  104. // https://bugreports.qt.io/browse/QTBUG-98651
  105. MessageBoxCustom::showDialog(this, [this]{ copyHeroesData(); });
  106. #endif
  107. }
  108. void FirstLaunchView::on_pushButtonGogInstall_clicked()
  109. {
  110. // iOS can't display modal dialogs when called directly on button press
  111. // https://bugreports.qt.io/browse/QTBUG-98651
  112. MessageBoxCustom::showDialog(this, [this]{extractGogData();});
  113. }
  114. void FirstLaunchView::enterSetup()
  115. {
  116. Languages::fillLanguages(ui->listWidgetLanguage, false);
  117. }
  118. void FirstLaunchView::setSetupProgress(int progress)
  119. {
  120. ui->buttonTabLanguage->setDisabled(progress < 1);
  121. ui->buttonTabHeroesData->setDisabled(progress < 2);
  122. ui->buttonTabModPreset->setDisabled(progress < 3);
  123. }
  124. void FirstLaunchView::activateTabLanguage()
  125. {
  126. setSetupProgress(1);
  127. ui->installerTabs->setCurrentIndex(0);
  128. ui->buttonTabLanguage->setChecked(true);
  129. ui->buttonTabHeroesData->setChecked(false);
  130. ui->buttonTabModPreset->setChecked(false);
  131. }
  132. void FirstLaunchView::activateTabHeroesData()
  133. {
  134. setSetupProgress(2);
  135. ui->installerTabs->setCurrentIndex(1);
  136. ui->buttonTabLanguage->setChecked(false);
  137. ui->buttonTabHeroesData->setChecked(true);
  138. ui->buttonTabModPreset->setChecked(false);
  139. if(heroesDataUpdate())
  140. {
  141. activateTabModPreset();
  142. return;
  143. }
  144. QString installPath = getHeroesInstallDir();
  145. if(!installPath.isEmpty())
  146. {
  147. auto reply = QMessageBox::question(this, tr("Heroes III installation found!"), tr("Copy data to VCMI folder?"), QMessageBox::Yes | QMessageBox::No);
  148. if(reply == QMessageBox::Yes)
  149. copyHeroesData(installPath);
  150. }
  151. }
  152. void FirstLaunchView::activateTabModPreset()
  153. {
  154. setSetupProgress(3);
  155. ui->installerTabs->setCurrentIndex(2);
  156. ui->buttonTabLanguage->setChecked(false);
  157. ui->buttonTabHeroesData->setChecked(false);
  158. ui->buttonTabModPreset->setChecked(true);
  159. modPresetUpdate();
  160. }
  161. void FirstLaunchView::exitSetup(bool goToMods)
  162. {
  163. if(auto * mainWindow = dynamic_cast<MainWindow *>(QApplication::activeWindow()))
  164. mainWindow->exitSetup(goToMods);
  165. }
  166. // Tab Language
  167. void FirstLaunchView::languageSelected(const QString & selectedLanguage)
  168. {
  169. Settings node = settings.write["general"]["language"];
  170. node->String() = selectedLanguage.toStdString();
  171. if(auto * mainWindow = dynamic_cast<MainWindow *>(QApplication::activeWindow()))
  172. mainWindow->updateTranslation();
  173. }
  174. bool FirstLaunchView::heroesDataUpdate()
  175. {
  176. bool detected = heroesDataDetect();
  177. if(detected)
  178. heroesDataDetected();
  179. else
  180. heroesDataMissing();
  181. return detected;
  182. }
  183. void FirstLaunchView::heroesDataMissing()
  184. {
  185. QPalette newPalette = palette();
  186. newPalette.setColor(QPalette::Base, QColor(200, 50, 50));
  187. ui->lineEditDataSystem->setPalette(newPalette);
  188. ui->lineEditDataUser->setPalette(newPalette);
  189. ui->labelDataManualTitle->setVisible(true);
  190. ui->labelDataManualDescr->setVisible(true);
  191. ui->pushButtonDataSearch->setVisible(true);
  192. #ifdef VCMI_ANDROID
  193. // selecting directory with ACTION_OPEN_DOCUMENT_TREE is available only since API level 21
  194. const bool canUseDataCopy = QtAndroid::androidSdkVersion() >= 21;
  195. #elif defined(VCMI_IOS)
  196. // selecting directory through UIDocumentPickerViewController is available only since iOS 13
  197. const bool canUseDataCopy = iOS_utils::isOsVersionAtLeast(13);
  198. #else
  199. const bool canUseDataCopy = true;
  200. #endif
  201. ui->labelDataCopyTitle->setVisible(canUseDataCopy);
  202. ui->labelDataCopyDescr->setVisible(canUseDataCopy);
  203. ui->pushButtonDataCopy->setVisible(canUseDataCopy);
  204. #ifdef ENABLE_INNOEXTRACT
  205. ui->pushButtonGogInstall->setVisible(true);
  206. ui->labelDataGogTitle->setVisible(true);
  207. ui->labelDataGogDescr->setVisible(true);
  208. #endif
  209. ui->labelDataFound->setVisible(false);
  210. ui->pushButtonDataNext->setEnabled(false);
  211. }
  212. void FirstLaunchView::heroesDataDetected()
  213. {
  214. QPalette newPalette = palette();
  215. newPalette.setColor(QPalette::Base, QColor(50, 200, 50));
  216. ui->lineEditDataSystem->setPalette(newPalette);
  217. ui->lineEditDataUser->setPalette(newPalette);
  218. ui->pushButtonDataSearch->setVisible(false);
  219. ui->pushButtonDataCopy->setVisible(false);
  220. ui->labelDataManualTitle->setVisible(false);
  221. ui->labelDataManualDescr->setVisible(false);
  222. ui->labelDataCopyTitle->setVisible(false);
  223. ui->labelDataCopyDescr->setVisible(false);
  224. #ifdef ENABLE_INNOEXTRACT
  225. ui->pushButtonGogInstall->setVisible(false);
  226. ui->labelDataGogTitle->setVisible(false);
  227. ui->labelDataGogDescr->setVisible(false);
  228. #endif
  229. ui->labelDataFound->setVisible(true);
  230. ui->pushButtonDataNext->setEnabled(true);
  231. CGeneralTextHandler::detectInstallParameters();
  232. }
  233. // Tab Heroes III Data
  234. bool FirstLaunchView::heroesDataDetect()
  235. {
  236. // user might have copied files to one of our data path.
  237. // perform full reinitialization of virtual filesystem
  238. CResourceHandler::destroy();
  239. CResourceHandler::initialize();
  240. CResourceHandler::load("config/filesystem.json");
  241. // use file from lod archive to check presence of H3 data. Very rough estimate, but will work in majority of cases
  242. bool heroesDataFoundROE = CResourceHandler::get()->existsResource(ResourcePath("DATA/GENRLTXT.TXT"));
  243. bool heroesDataFoundSOD = CResourceHandler::get()->existsResource(ResourcePath("DATA/TENTCOLR.TXT"));
  244. return heroesDataFoundROE && heroesDataFoundSOD;
  245. }
  246. QString FirstLaunchView::getHeroesInstallDir()
  247. {
  248. #ifdef VCMI_WINDOWS
  249. QVector<QPair<QString, QString>> regKeys = {
  250. { "HKEY_LOCAL_MACHINE\\SOFTWARE\\GOG.com\\Games\\1207658787", "path" }, // Gog on x86 system
  251. { "HKEY_LOCAL_MACHINE\\SOFTWARE\\WOW6432Node\\GOG.com\\Games\\1207658787", "path" }, // Gog on x64 system
  252. { "HKEY_LOCAL_MACHINE\\SOFTWARE\\New World Computing\\Heroes of Might and Magic® III\\1.0", "AppPath" }, // H3 Complete on x86 system
  253. { "HKEY_LOCAL_MACHINE\\SOFTWARE\\WOW6432Node\\New World Computing\\Heroes of Might and Magic® III\\1.0", "AppPath" }, // H3 Complete on x64 system
  254. { "HKEY_LOCAL_MACHINE\\SOFTWARE\\New World Computing\\Heroes of Might and Magic III\\1.0", "AppPath" }, // some localized H3 on x86 system
  255. { "HKEY_LOCAL_MACHINE\\SOFTWARE\\WOW6432Node\\New World Computing\\Heroes of Might and Magic III\\1.0", "AppPath" }, // some localized H3 on x64 system
  256. };
  257. for(auto & regKey : regKeys)
  258. {
  259. QString path = QSettings(regKey.first, QSettings::NativeFormat).value(regKey.second).toString();
  260. if(!path.isEmpty())
  261. return path;
  262. }
  263. #endif
  264. return QString{};
  265. }
  266. void FirstLaunchView::extractGogData()
  267. {
  268. #ifdef ENABLE_INNOEXTRACT
  269. auto fileSelection = [this](QByteArray magic, QString filter, QString startPath = {}) {
  270. QString titleSel = tr("Select %1 file...", "param is file extension").arg(filter);
  271. QString titleErr = tr("You have to select %1 file!", "param is file extension").arg(filter);
  272. #if defined(VCMI_MOBILE)
  273. filter = tr("GOG file (*.*)");
  274. QMessageBox::information(this, tr("File selection"), titleSel);
  275. #endif
  276. QString file = QFileDialog::getOpenFileName(this, titleSel, startPath.isEmpty() ? QDir::homePath() : startPath, filter);
  277. if(file.isEmpty())
  278. return QString{};
  279. QFile tmpFile(file);
  280. if(!tmpFile.open(QIODevice::ReadOnly))
  281. {
  282. QMessageBox::critical(this, tr("File cannot be opened"), tmpFile.errorString());
  283. return QString{};
  284. }
  285. QByteArray magicFile = tmpFile.read(magic.length());
  286. if(!magicFile.startsWith(magic))
  287. {
  288. QMessageBox::critical(this, tr("Invalid file selected"), titleErr);
  289. return QString{};
  290. }
  291. return file;
  292. };
  293. QString fileBin = fileSelection(QByteArray{"idska32"}, tr("GOG data") + " (*.bin)");
  294. if(fileBin.isEmpty())
  295. return;
  296. QString fileExe = fileSelection(QByteArray{"MZ"}, tr("GOG installer") + " (*.exe)", QFileInfo(fileBin).absolutePath());
  297. if(fileExe.isEmpty())
  298. return;
  299. ui->progressBarGog->setVisible(true);
  300. ui->pushButtonGogInstall->setVisible(false);
  301. setEnabled(false);
  302. QTimer::singleShot(100, this, [this, fileExe, fileBin](){ // background to make sure FileDialog is closed...
  303. QDir tempDir(pathToQString(VCMIDirs::get().userDataPath()));
  304. if(tempDir.cd("tmp"))
  305. {
  306. tempDir.removeRecursively(); // remove if already exists (e.g. previous crash)
  307. tempDir.cdUp();
  308. }
  309. tempDir.mkdir("tmp");
  310. if(!tempDir.cd("tmp"))
  311. return; // should not happen - but avoid deleting wrong folder in any case
  312. QString tmpFileExe = tempDir.filePath("h3_gog.exe");
  313. QString tmpFileBin = tempDir.filePath("h3_gog-1.bin");
  314. #ifdef VCMI_ANDROID
  315. auto copy = [](QString src, QString dst)
  316. {
  317. auto srcStr = QAndroidJniObject::fromString(src);
  318. auto dstStr = QAndroidJniObject::fromString(dst);
  319. QAndroidJniObject::callStaticObjectMethod("eu/vcmi/vcmi/util/FileUtil", "copyFileFromUri", "(Ljava/lang/String;Ljava/lang/String;)V", srcStr.object<jstring>(), dstStr.object<jstring>());
  320. };
  321. copy(fileExe, tmpFileExe);
  322. copy(fileBin, tmpFileBin);
  323. #else
  324. QFile(fileExe).copy(tmpFileExe);
  325. QFile(fileBin).copy(tmpFileBin);
  326. #endif
  327. logGlobal->info("Installing exe '%s' ('%s')", tmpFileExe.toStdString(), fileExe.toStdString());
  328. logGlobal->info("Installing bin '%s' ('%s')", tmpFileBin.toStdString(), fileBin.toStdString());
  329. QString errorText{};
  330. auto isGogGalaxyExe = [](QString fileToTest) {
  331. QFile file(fileToTest);
  332. quint64 fileSize = file.size();
  333. if(fileSize > 10 * 1024 * 1024)
  334. return false; // avoid to load big files; galaxy exe is smaller...
  335. if(!file.open(QIODevice::ReadOnly))
  336. return false;
  337. QByteArray data = file.readAll();
  338. const QByteArray magicId{reinterpret_cast<const char*>(u"GOG Galaxy"), 20};
  339. return data.contains(magicId);
  340. };
  341. if(isGogGalaxyExe(tmpFileExe))
  342. errorText = tr("You've provided a GOG Galaxy installer! This file doesn't contain the game. Please download the offline backup game installer!");
  343. if(errorText.isEmpty())
  344. errorText = Innoextract::extract(tmpFileExe, tempDir.path(), [this](float progress) {
  345. ui->progressBarGog->setValue(progress * 100);
  346. qApp->processEvents();
  347. });
  348. QString hashError;
  349. if(!errorText.isEmpty())
  350. hashError = Innoextract::getHashError(tmpFileExe, tmpFileBin, fileExe, fileBin);
  351. ui->progressBarGog->setVisible(false);
  352. ui->pushButtonGogInstall->setVisible(true);
  353. setEnabled(true);
  354. QStringList dirData = tempDir.entryList({"data"}, QDir::Filter::Dirs);
  355. if(!errorText.isEmpty() || dirData.empty() || QDir(tempDir.filePath(dirData.front())).entryList({"*.lod"}, QDir::Filter::Files).empty())
  356. {
  357. if(!errorText.isEmpty())
  358. {
  359. logGlobal->error("Gog installer extraction failure! Reason: %s", errorText.toStdString());
  360. QMessageBox::critical(this, tr("Extracting error!"), errorText, QMessageBox::Ok, QMessageBox::Ok);
  361. if(!hashError.isEmpty())
  362. {
  363. logGlobal->error("Hash error: %s", hashError.toStdString());
  364. QMessageBox::critical(this, tr("Hash error!"), hashError, QMessageBox::Ok, QMessageBox::Ok);
  365. }
  366. }
  367. else
  368. QMessageBox::critical(this, tr("No Heroes III data!"), tr("Selected files do not contain Heroes III data!"), QMessageBox::Ok, QMessageBox::Ok);
  369. tempDir.removeRecursively();
  370. return;
  371. }
  372. copyHeroesData(tempDir.path(), true);
  373. tempDir.removeRecursively();
  374. });
  375. #endif
  376. }
  377. void FirstLaunchView::copyHeroesData(const QString & path, bool move)
  378. {
  379. QDir sourceRoot{path};
  380. #ifdef VCMI_IOS
  381. // TODO: Qt 6.5 can select directories https://codereview.qt-project.org/c/qt/qtbase/+/446449
  382. SelectDirectory iosDirectorySelector;
  383. if(path.isEmpty())
  384. sourceRoot.setPath(iosDirectorySelector.getExistingDirectory());
  385. #else
  386. if(path.isEmpty())
  387. sourceRoot.setPath(QFileDialog::getExistingDirectory(this, {}, {}, QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks));
  388. #endif
  389. if(!sourceRoot.exists())
  390. return;
  391. if (sourceRoot.dirName().compare("data", Qt::CaseInsensitive) == 0)
  392. {
  393. // We got Data folder. Possibly user selected "Data" folder of Heroes III install. Check whether valid data might exist 1 level above
  394. QStringList dirData = sourceRoot.entryList({"data"}, QDir::Filter::Dirs);
  395. if (dirData.empty())
  396. {
  397. // This is "Data" folder without any "Data" folders inside. Try to check for data 1 level above
  398. sourceRoot.cdUp();
  399. }
  400. }
  401. QStringList dirData = sourceRoot.entryList({"data"}, QDir::Filter::Dirs);
  402. QStringList dirMaps = sourceRoot.entryList({"maps"}, QDir::Filter::Dirs);
  403. QStringList dirMp3 = sourceRoot.entryList({"mp3"}, QDir::Filter::Dirs);
  404. const auto noDataMessage = tr("Failed to detect valid Heroes III data in chosen directory.\nPlease select the directory with installed Heroes III data.");
  405. if(dirData.empty())
  406. {
  407. QMessageBox::critical(this, tr("Heroes III data not found!"), noDataMessage);
  408. return;
  409. }
  410. QDir sourceData = sourceRoot.filePath(dirData.front());
  411. QStringList roeFiles = sourceData.entryList({"*.lod"}, QDir::Filter::Files);
  412. QStringList sodFiles = sourceData.entryList({"H3ab*.lod"}, QDir::Filter::Files);
  413. QStringList hdFiles = sourceData.entryList({"*.pak"}, QDir::Filter::Files);
  414. if(sodFiles.empty())
  415. {
  416. if (roeFiles.empty())
  417. {
  418. // Directory structure is correct (Data/Maps/Mp3) but no .lod archives that should be present in any install
  419. QMessageBox::critical(this, tr("Heroes III data not found!"), noDataMessage);
  420. return;
  421. }
  422. if (!hdFiles.empty())
  423. {
  424. // HD Edition contains only RoE data so we can't use even unmodified files from it
  425. QMessageBox::critical(this, tr("Heroes III data not found!"), tr("Heroes III: HD Edition files are not supported by VCMI.\nPlease select the directory with Heroes III: Complete Edition or Heroes III: Shadow of Death."));
  426. return;
  427. }
  428. // RoE or some other unsupported edition. Demo version?
  429. QMessageBox::critical(this, tr("Heroes III data not found!"), tr("Unknown or unsupported Heroes III version found.\nPlease select the directory with Heroes III: Complete Edition or Heroes III: Shadow of Death."));
  430. return;
  431. }
  432. QStringList copyDirectories;
  433. copyDirectories += dirData.front();
  434. if (!dirMaps.empty())
  435. copyDirectories += dirMaps.front();
  436. if (!dirMp3.empty())
  437. copyDirectories += dirMp3.front();
  438. QDir targetRoot = pathToQString(VCMIDirs::get().userDataPath());
  439. for(const QString & dirName : copyDirectories)
  440. {
  441. QDir sourceDir = sourceRoot.filePath(dirName);
  442. QDir targetDir = targetRoot.filePath(dirName);
  443. if(!targetRoot.exists(dirName))
  444. targetRoot.mkdir(dirName);
  445. for(const QString & filename : sourceDir.entryList(QDir::Filter::Files))
  446. {
  447. QFile sourceFile(sourceDir.filePath(filename));
  448. if(move)
  449. sourceFile.rename(targetDir.filePath(filename));
  450. else
  451. sourceFile.copy(targetDir.filePath(filename));
  452. }
  453. }
  454. heroesDataUpdate();
  455. }
  456. // Tab Mod Preset
  457. void FirstLaunchView::modPresetUpdate()
  458. {
  459. bool translationExists = !findTranslationModName().isEmpty();
  460. ui->labelPresetLanguageDescr->setVisible(translationExists);
  461. ui->buttonPresetLanguage->setVisible(translationExists);
  462. ui->buttonPresetLanguage->setVisible(checkCanInstallTranslation());
  463. ui->buttonPresetExtras->setVisible(checkCanInstallExtras());
  464. ui->buttonPresetHota->setVisible(checkCanInstallHota());
  465. ui->buttonPresetWog->setVisible(checkCanInstallWog());
  466. ui->labelPresetLanguageDescr->setVisible(checkCanInstallTranslation());
  467. ui->labelPresetExtrasDescr->setVisible(checkCanInstallExtras());
  468. ui->labelPresetHotaDescr->setVisible(checkCanInstallHota());
  469. ui->labelPresetWogDescr->setVisible(checkCanInstallWog());
  470. // we can't install anything - either repository checkout is off or all recommended mods are already installed
  471. if (!checkCanInstallTranslation() && !checkCanInstallExtras() && !checkCanInstallHota() && !checkCanInstallWog())
  472. exitSetup(false);
  473. }
  474. QString FirstLaunchView::findTranslationModName()
  475. {
  476. auto * mainWindow = dynamic_cast<MainWindow *>(QApplication::activeWindow());
  477. auto status = mainWindow->getTranslationStatus();
  478. if (status == ETranslationStatus::ACTIVE || status == ETranslationStatus::NOT_AVAILABLE)
  479. return QString();
  480. QString preferredlanguage = QString::fromStdString(settings["general"]["language"].String());
  481. return getModView()->getTranslationModName(preferredlanguage);
  482. }
  483. bool FirstLaunchView::checkCanInstallTranslation()
  484. {
  485. QString modName = findTranslationModName();
  486. if(modName.isEmpty())
  487. return false;
  488. return checkCanInstallMod(modName);
  489. }
  490. bool FirstLaunchView::checkCanInstallWog()
  491. {
  492. return checkCanInstallMod("wake-of-gods");
  493. }
  494. bool FirstLaunchView::checkCanInstallHota()
  495. {
  496. return checkCanInstallMod("hota");
  497. }
  498. bool FirstLaunchView::checkCanInstallExtras()
  499. {
  500. return checkCanInstallMod("vcmi-extras");
  501. }
  502. CModListView * FirstLaunchView::getModView()
  503. {
  504. auto * mainWindow = dynamic_cast<MainWindow *>(QApplication::activeWindow());
  505. assert(mainWindow);
  506. if (!mainWindow)
  507. return nullptr;
  508. return mainWindow->getModView();
  509. }
  510. bool FirstLaunchView::checkCanInstallMod(const QString & modID)
  511. {
  512. return getModView() && getModView()->isModAvailable(modID);
  513. }
  514. void FirstLaunchView::on_pushButtonPresetBack_clicked()
  515. {
  516. activateTabHeroesData();
  517. }
  518. void FirstLaunchView::on_pushButtonPresetNext_clicked()
  519. {
  520. QStringList modsToInstall;
  521. if (ui->buttonPresetLanguage->isChecked() && checkCanInstallTranslation())
  522. modsToInstall.push_back(findTranslationModName());
  523. if (ui->buttonPresetExtras->isChecked() && checkCanInstallExtras())
  524. modsToInstall.push_back("vcmi-extras");
  525. if (ui->buttonPresetWog->isChecked() && checkCanInstallWog())
  526. modsToInstall.push_back("wake-of-gods");
  527. if (ui->buttonPresetHota->isChecked() && checkCanInstallHota())
  528. modsToInstall.push_back("hota");
  529. bool goToMods = !modsToInstall.empty();
  530. exitSetup(goToMods);
  531. for (auto const & modName : modsToInstall)
  532. getModView()->doInstallMod(modName);
  533. }
  534. void FirstLaunchView::on_pushButtonDiscord_clicked()
  535. {
  536. QDesktopServices::openUrl(QUrl("https://discord.gg/chBT42V"));
  537. }
  538. void FirstLaunchView::on_pushButtonGithub_clicked()
  539. {
  540. QDesktopServices::openUrl(QUrl("https://github.com/vcmi/vcmi"));
  541. }