MainWindow.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /*
  2. * Copyright (C) 2022 The V-Gears Team
  3. *
  4. * This file is part of V-Gears
  5. *
  6. * V-Gears is free software: you can redistribute it and/or modify it under
  7. * terms of the GNU General Public License as published by the Free Software
  8. * Foundation, version 3.0 (GPLv3) of the License.
  9. *
  10. * V-Gears is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #pragma once
  16. #include <QtWidgets/QMainWindow>
  17. #include <QtCore/QSettings>
  18. #include <atomic>
  19. #include <functional>
  20. #include <memory>
  21. #include <thread>
  22. #include <vector>
  23. #include "installer/steps/DataInstaller.h"
  24. #include "installer/formats/DiskImage.h"
  25. #include "installer/steps/InstallSteps.h"
  26. #include "installer/steps/ExternalTools.h"
  27. class QCheckBox;
  28. struct InstallOptions;
  29. namespace Ui {
  30. class MainWindow;
  31. }
  32. /**
  33. * The installer main window.
  34. */
  35. class MainWindow : public QMainWindow{
  36. public:
  37. /**
  38. * Constructor.
  39. *
  40. * @param[in] parent Widget to be made parent of the window.
  41. */
  42. explicit MainWindow(QWidget *parent = 0);
  43. /**
  44. * Destructor.
  45. */
  46. ~MainWindow();
  47. private slots:
  48. /**
  49. * Triggered when About is chosen in the menu. Shows the version and the license.
  50. */
  51. void on_actionABout_triggered();
  52. /**
  53. * Triggered when Help is chosen in the menu. Explains how to use the installer.
  54. */
  55. void on_actionHelp_triggered();
  56. /**
  57. * Triggered when Exit is chosen in the menu. Disabled while installing, like the close button.
  58. */
  59. void on_actionExit_triggered();
  60. /**
  61. * Triggered when the input data directory button is clicked.
  62. *
  63. * Opens a file manager for directory selection.
  64. */
  65. void on_btn_data_src_clicked();
  66. /**
  67. * Triggered when the output data directory has done being edited.
  68. *
  69. * Sets the selected directory.
  70. */
  71. void on_line_data_dst_editingFinished();
  72. /**
  73. * Triggered when the output data directory button is clicked.
  74. *
  75. * Opens a file manager for directory selection.
  76. */
  77. void on_btn_data_dst_clicked();
  78. /**
  79. * Triggered when the install button is clicked.
  80. *
  81. * Launches the installation.
  82. */
  83. void on_btn_data_run_clicked();
  84. /**
  85. * Triggered when the verify button is clicked.
  86. *
  87. * Checks that the installation has everything the game needs.
  88. */
  89. void on_btn_verify_clicked();
  90. /**
  91. * Triggered when the launch game button is clicked.
  92. */
  93. void on_btn_launch_clicked();
  94. /**
  95. * Triggered when the advanced options checkbox is checked or unchecked.
  96. */
  97. void on_chk_advanced_options_stateChanged();
  98. //private slots:
  99. /**
  100. * Makes the installation progress advance.
  101. */
  102. void DoProgress();
  103. private:
  104. /**
  105. * Enables or disables the UI.
  106. *
  107. * @param[in] enable True to enable the UI, false to disable it.
  108. */
  109. void EnableUi(bool enable);
  110. /**
  111. * Sets where the installer looks for FFmpeg and TiMidity.
  112. *
  113. * @param[in] install_dir Installation directory.
  114. */
  115. void UpdateToolLocations(const QString& install_dir);
  116. /**
  117. * Looks for FFmpeg and TiMidity and shows what was found in the window.
  118. */
  119. void RefreshToolStatus();
  120. /**
  121. * Asks the user for the path of FFmpeg or TiMidity. It is saved if it works.
  122. *
  123. * @param[in] ffmpeg True for FFmpeg, false for TiMidity.
  124. * @return True if a working program was chosen.
  125. */
  126. bool LocateTool(bool ffmpeg);
  127. /**
  128. * Checks that the programs the options need can be used and, if not, tells the user what would be lost and
  129. * offers to locate them, get them, check again, continue without them, or cancel.
  130. *
  131. * @param[in] options The installation options.
  132. * @return True to go on with the installation, false to cancel it.
  133. */
  134. bool ResolveMissingTools(const InstallOptions& options);
  135. /**
  136. * Starts the log of an installation: the file in the installation directory and the log window.
  137. *
  138. * @param[in] dir Installation directory, ending with '/'.
  139. * @param[in] append True to keep what a previous run wrote to the log file.
  140. */
  141. void OpenInstallLog(const QString& dir, bool append);
  142. /**
  143. * Finds the V-Gears data folder to copy, asking for it if it can't be found.
  144. *
  145. * @param[out] game_data The folder.
  146. * @return False if the user did not choose one.
  147. */
  148. bool FindDataToCopy(QString& game_data);
  149. /**
  150. * Checks that the installation can start, telling the problems and asking about the warnings.
  151. *
  152. * @param[in] output The installation directory.
  153. * @param[in] copy_data If the V-Gears data files are copied.
  154. * @param[in] game_data The folder with the data files.
  155. * @param[in] clean If the previous installation is deleted.
  156. * @return True if the installation can go on.
  157. */
  158. bool CheckInstallation(
  159. const QString& output, const bool copy_data, const QString& game_data, const bool clean
  160. );
  161. /**
  162. * Deletes the installation in a directory, if there is one, after asking.
  163. *
  164. * @param[in] output The installation directory.
  165. * @return True if the installation can go on.
  166. */
  167. bool DeletePreviousInstallation(const QString& output);
  168. /**
  169. * Creates a function that shows the progress of a part of the installation.
  170. *
  171. * @param[in] phase The part.
  172. * @param[in] text Description of what is done.
  173. * @return Function that takes the progress of the part.
  174. */
  175. std::function<void(float)> ShowPhase(const InstallSteps::Phase phase, const QString& text);
  176. /**
  177. * Extracts the install disk and checks that it has the files that are needed.
  178. *
  179. * @param[in] output The installation directory.
  180. * @return True if the installation can go on.
  181. */
  182. bool ExtractDisk(const QString& output);
  183. /**
  184. * Copies the data files to the installation directory and prepares it.
  185. *
  186. * @param[in] output The installation directory.
  187. * @param[in] copy_data If the V-Gears data files are copied.
  188. * @param[in] game_data The folder with the data files.
  189. * @return True if the installation can go on.
  190. */
  191. bool PrepareInstallationDir(
  192. const QString& output, const bool copy_data, const QString& game_data
  193. );
  194. /**
  195. * Starts the conversion of the data, which goes on after this returns.
  196. *
  197. * @param[in] output The installation directory.
  198. */
  199. void StartConversion(const QString& output);
  200. /**
  201. * Extracts the videos of the selected game disk, in the worker thread.
  202. *
  203. * @param[in] output Installation directory, ending with '/'.
  204. */
  205. void StartVideoExtraction(const QString& output);
  206. /**
  207. * The checkboxes of the options that choose what to install.
  208. */
  209. std::vector<QCheckBox*> OptionBoxes() const;
  210. /**
  211. * Reads the options from the window.
  212. */
  213. InstallOptions CurrentOptions() const;
  214. /**
  215. * Sets the options in the window.
  216. */
  217. void SetOptions(const InstallOptions& options);
  218. /**
  219. * Applies a preset.
  220. *
  221. * @param[in] index Index of the preset in the preset box.
  222. */
  223. void ApplyPreset(int index);
  224. /**
  225. * Enforces the rules between options (disabling what can't be chosen) and updates the space estimate.
  226. */
  227. void UpdateOptions();
  228. /**
  229. * Starts V-Gears from an installation directory.
  230. */
  231. void LaunchGame(const QString& dir);
  232. /**
  233. * Index of the "Custom" preset.
  234. */
  235. static const int PRESET_CUSTOM = 5;
  236. /**
  237. * Shows the installation progress.
  238. *
  239. * @param[in] progress Installation progress [0-100].
  240. */
  241. void ShowProgress(float progress);
  242. /**
  243. * Runs the whole installation. It is the body of the worker thread.
  244. *
  245. * @param[in] options The installation options.
  246. * @param[in] output_path The installation directory.
  247. * @param[in] write_line Receives the installer messages.
  248. */
  249. void RunInstallation(
  250. DataInstaller::AdvancedOptions options, std::string output_path,
  251. std::function<void(std::string, int, bool)> write_line
  252. );
  253. /**
  254. * Triggered when the installation starts.
  255. *
  256. * Disables the UI.
  257. */
  258. void OnInstallStarted();
  259. /**
  260. * Triggered when the installation stops.
  261. *
  262. * Enables the UI.
  263. */
  264. void OnInstallStopped();
  265. /**
  266. * Initializes the installer settings.
  267. */
  268. void InitSettings(void);
  269. // The Q_OBJECT macro must appear in the private section of a class
  270. // definition that declares its own signals and slots or that uses other
  271. // services provided by Qt's meta-object system.
  272. Q_OBJECT
  273. /**
  274. * The installer window.
  275. */
  276. Ui::MainWindow* main_window_;
  277. /**
  278. * A timer.
  279. */
  280. QTimer* timer_;
  281. /**
  282. * The installer settings.
  283. */
  284. QSettings *settings_;
  285. /**
  286. * The installer.
  287. */
  288. std::unique_ptr<class DataInstaller> installer_;
  289. /**
  290. * The thread that runs the installation.
  291. */
  292. std::thread worker_;
  293. /**
  294. * Set to ask the worker thread to stop as soon as possible.
  295. */
  296. std::atomic<bool> stop_requested_{false};
  297. /**
  298. * True if the current installation runs in the worker thread, false if it is advanced by the timer.
  299. */
  300. bool background_ = false;
  301. /**
  302. * True while the option rules are being applied, to avoid applying them recursively.
  303. */
  304. bool updating_options_ = false;
  305. /**
  306. * True if the game can be launched from the window (after an installation or a verification).
  307. */
  308. bool launch_available_ = false;
  309. /**
  310. * The conversion programs found by {@see RefreshToolStatus}.
  311. */
  312. ExternalTools::Tool ffmpeg_tool_;
  313. ExternalTools::Tool timidity_tool_;
  314. /**
  315. * The selected disk image.
  316. */
  317. DiskImage disk_image_;
  318. /**
  319. * The installation directory of the installation in progress, ending with '/'.
  320. */
  321. QString install_dir_;
  322. };