You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

intro.cpp 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. // Copyright (c) 2011-2016 The Starwels developers
  2. // Distributed under the MIT software license, see the accompanying
  3. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  4. #if defined(HAVE_CONFIG_H)
  5. #include "config/starwels-config.h"
  6. #endif
  7. #include "fs.h"
  8. #include "intro.h"
  9. #include "ui_intro.h"
  10. #include "guiutil.h"
  11. #include "util.h"
  12. #include <QFileDialog>
  13. #include <QSettings>
  14. #include <QMessageBox>
  15. #include <cmath>
  16. static const uint64_t GB_BYTES = 1000000000LL;
  17. /* Minimum free space (in GB) needed for data directory */
  18. static const uint64_t BLOCK_CHAIN_SIZE = 1;
  19. /* Minimum free space (in GB) needed for data directory when pruned; Does not include prune target */
  20. static const uint64_t CHAIN_STATE_SIZE = 3;
  21. /* Total required space (in GB) depending on user choice (prune, not prune) */
  22. static uint64_t requiredSpace;
  23. /* Check free space asynchronously to prevent hanging the UI thread.
  24. Up to one request to check a path is in flight to this thread; when the check()
  25. function runs, the current path is requested from the associated Intro object.
  26. The reply is sent back through a signal.
  27. This ensures that no queue of checking requests is built up while the user is
  28. still entering the path, and that always the most recently entered path is checked as
  29. soon as the thread becomes available.
  30. */
  31. class FreespaceChecker : public QObject
  32. {
  33. Q_OBJECT
  34. public:
  35. FreespaceChecker(Intro *intro);
  36. enum Status {
  37. ST_OK,
  38. ST_ERROR
  39. };
  40. public Q_SLOTS:
  41. void check();
  42. Q_SIGNALS:
  43. void reply(int status, const QString &message, quint64 available);
  44. private:
  45. Intro *intro;
  46. };
  47. #include "intro.moc"
  48. FreespaceChecker::FreespaceChecker(Intro *_intro)
  49. {
  50. this->intro = _intro;
  51. }
  52. void FreespaceChecker::check()
  53. {
  54. QString dataDirStr = intro->getPathToCheck();
  55. fs::path dataDir = GUIUtil::qstringToBoostPath(dataDirStr);
  56. uint64_t freeBytesAvailable = 0;
  57. int replyStatus = ST_OK;
  58. QString replyMessage = tr("A new data directory will be created.");
  59. /* Find first parent that exists, so that fs::space does not fail */
  60. fs::path parentDir = dataDir;
  61. fs::path parentDirOld = fs::path();
  62. while(parentDir.has_parent_path() && !fs::exists(parentDir))
  63. {
  64. parentDir = parentDir.parent_path();
  65. /* Check if we make any progress, break if not to prevent an infinite loop here */
  66. if (parentDirOld == parentDir)
  67. break;
  68. parentDirOld = parentDir;
  69. }
  70. try {
  71. freeBytesAvailable = fs::space(parentDir).available;
  72. if(fs::exists(dataDir))
  73. {
  74. if(fs::is_directory(dataDir))
  75. {
  76. QString separator = "<code>" + QDir::toNativeSeparators("/") + tr("name") + "</code>";
  77. replyStatus = ST_OK;
  78. replyMessage = tr("Directory already exists. Add %1 if you intend to create a new directory here.").arg(separator);
  79. } else {
  80. replyStatus = ST_ERROR;
  81. replyMessage = tr("Path already exists, and is not a directory.");
  82. }
  83. }
  84. } catch (const fs::filesystem_error&)
  85. {
  86. /* Parent directory does not exist or is not accessible */
  87. replyStatus = ST_ERROR;
  88. replyMessage = tr("Cannot create data directory here.");
  89. }
  90. Q_EMIT reply(replyStatus, replyMessage, freeBytesAvailable);
  91. }
  92. Intro::Intro(QWidget *parent) :
  93. QDialog(parent),
  94. ui(new Ui::Intro),
  95. thread(0),
  96. signalled(false)
  97. {
  98. ui->setupUi(this);
  99. ui->welcomeLabel->setText(ui->welcomeLabel->text().arg(tr(PACKAGE_NAME)));
  100. ui->storageLabel->setText(ui->storageLabel->text().arg(tr(PACKAGE_NAME)));
  101. ui->lblExplanation1->setText(ui->lblExplanation1->text()
  102. .arg(tr(PACKAGE_NAME))
  103. .arg(BLOCK_CHAIN_SIZE)
  104. .arg(2009)
  105. .arg(tr("Starwels"))
  106. );
  107. ui->lblExplanation2->setText(ui->lblExplanation2->text().arg(tr(PACKAGE_NAME)));
  108. uint64_t pruneTarget = std::max<int64_t>(0, gArgs.GetArg("-prune", 0));
  109. requiredSpace = BLOCK_CHAIN_SIZE;
  110. QString storageRequiresMsg = tr("At least %1 GB of data will be stored in this directory, and it will grow over time.");
  111. if (pruneTarget) {
  112. uint64_t prunedGBs = std::ceil(pruneTarget * 1024 * 1024.0 / GB_BYTES);
  113. if (prunedGBs <= requiredSpace) {
  114. requiredSpace = prunedGBs;
  115. storageRequiresMsg = tr("Approximately %1 GB of data will be stored in this directory.");
  116. }
  117. ui->lblExplanation3->setVisible(true);
  118. } else {
  119. ui->lblExplanation3->setVisible(false);
  120. }
  121. requiredSpace += CHAIN_STATE_SIZE;
  122. ui->sizeWarningLabel->setText(
  123. tr("%1 will download and store a copy of the blockchain.").arg(tr(PACKAGE_NAME)) + " " +
  124. storageRequiresMsg.arg(requiredSpace) + " " +
  125. tr("The wallet will also be stored in this directory.")
  126. );
  127. startThread();
  128. }
  129. Intro::~Intro()
  130. {
  131. delete ui;
  132. /* Ensure thread is finished before it is deleted */
  133. Q_EMIT stopThread();
  134. thread->wait();
  135. }
  136. QString Intro::getDataDirectory()
  137. {
  138. return ui->dataDirectory->text();
  139. }
  140. void Intro::setDataDirectory(const QString &dataDir)
  141. {
  142. ui->dataDirectory->setText(dataDir);
  143. if(dataDir == getDefaultDataDirectory())
  144. {
  145. ui->dataDirDefault->setChecked(true);
  146. ui->dataDirectory->setEnabled(false);
  147. ui->ellipsisButton->setEnabled(false);
  148. } else {
  149. ui->dataDirCustom->setChecked(true);
  150. ui->dataDirectory->setEnabled(true);
  151. ui->ellipsisButton->setEnabled(true);
  152. }
  153. }
  154. QString Intro::getDefaultDataDirectory()
  155. {
  156. return GUIUtil::boostPathToQString(GetDefaultDataDir());
  157. }
  158. bool Intro::pickDataDirectory()
  159. {
  160. QSettings settings;
  161. /* If data directory provided on command line, no need to look at settings
  162. or show a picking dialog */
  163. if(!gArgs.GetArg("-datadir", "").empty())
  164. return true;
  165. /* 1) Default data directory for operating system */
  166. QString dataDir = getDefaultDataDirectory();
  167. /* 2) Allow QSettings to override default dir */
  168. dataDir = settings.value("strDataDir", dataDir).toString();
  169. if(!fs::exists(GUIUtil::qstringToBoostPath(dataDir)) || gArgs.GetBoolArg("-choosedatadir", DEFAULT_CHOOSE_DATADIR) || settings.value("fReset", false).toBool() || gArgs.GetBoolArg("-resetguisettings", false))
  170. {
  171. /* If current default data directory does not exist, let the user choose one */
  172. Intro intro;
  173. intro.setDataDirectory(dataDir);
  174. intro.setWindowIcon(QIcon(":icons/starwels"));
  175. while(true)
  176. {
  177. if(!intro.exec())
  178. {
  179. /* Cancel clicked */
  180. return false;
  181. }
  182. dataDir = intro.getDataDirectory();
  183. try {
  184. TryCreateDirectories(GUIUtil::qstringToBoostPath(dataDir));
  185. break;
  186. } catch (const fs::filesystem_error&) {
  187. QMessageBox::critical(0, tr(PACKAGE_NAME),
  188. tr("Error: Specified data directory \"%1\" cannot be created.").arg(dataDir));
  189. /* fall through, back to choosing screen */
  190. }
  191. }
  192. settings.setValue("strDataDir", dataDir);
  193. settings.setValue("fReset", false);
  194. }
  195. /* Only override -datadir if different from the default, to make it possible to
  196. * override -datadir in the starwels.conf file in the default data directory
  197. * (to be consistent with starwelsd behavior)
  198. */
  199. if(dataDir != getDefaultDataDirectory())
  200. gArgs.SoftSetArg("-datadir", GUIUtil::qstringToBoostPath(dataDir).string()); // use OS locale for path setting
  201. return true;
  202. }
  203. void Intro::setStatus(int status, const QString &message, quint64 bytesAvailable)
  204. {
  205. switch(status)
  206. {
  207. case FreespaceChecker::ST_OK:
  208. ui->errorMessage->setText(message);
  209. ui->errorMessage->setStyleSheet("");
  210. break;
  211. case FreespaceChecker::ST_ERROR:
  212. ui->errorMessage->setText(tr("Error") + ": " + message);
  213. ui->errorMessage->setStyleSheet("QLabel { color: #800000 }");
  214. break;
  215. }
  216. /* Indicate number of bytes available */
  217. if(status == FreespaceChecker::ST_ERROR)
  218. {
  219. ui->freeSpace->setText("");
  220. } else {
  221. QString freeString = tr("%n GB of free space available", "", bytesAvailable/GB_BYTES);
  222. if(bytesAvailable < requiredSpace * GB_BYTES)
  223. {
  224. freeString += " " + tr("(of %n GB needed)", "", requiredSpace);
  225. ui->freeSpace->setStyleSheet("QLabel { color: #800000 }");
  226. } else {
  227. ui->freeSpace->setStyleSheet("");
  228. }
  229. ui->freeSpace->setText(freeString + ".");
  230. }
  231. /* Don't allow confirm in ERROR state */
  232. ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(status != FreespaceChecker::ST_ERROR);
  233. }
  234. void Intro::on_dataDirectory_textChanged(const QString &dataDirStr)
  235. {
  236. /* Disable OK button until check result comes in */
  237. ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
  238. checkPath(dataDirStr);
  239. }
  240. void Intro::on_ellipsisButton_clicked()
  241. {
  242. QString dir = QDir::toNativeSeparators(QFileDialog::getExistingDirectory(0, "Choose data directory", ui->dataDirectory->text()));
  243. if(!dir.isEmpty())
  244. ui->dataDirectory->setText(dir);
  245. }
  246. void Intro::on_dataDirDefault_clicked()
  247. {
  248. setDataDirectory(getDefaultDataDirectory());
  249. }
  250. void Intro::on_dataDirCustom_clicked()
  251. {
  252. ui->dataDirectory->setEnabled(true);
  253. ui->ellipsisButton->setEnabled(true);
  254. }
  255. void Intro::startThread()
  256. {
  257. thread = new QThread(this);
  258. FreespaceChecker *executor = new FreespaceChecker(this);
  259. executor->moveToThread(thread);
  260. connect(executor, SIGNAL(reply(int,QString,quint64)), this, SLOT(setStatus(int,QString,quint64)));
  261. connect(this, SIGNAL(requestCheck()), executor, SLOT(check()));
  262. /* make sure executor object is deleted in its own thread */
  263. connect(this, SIGNAL(stopThread()), executor, SLOT(deleteLater()));
  264. connect(this, SIGNAL(stopThread()), thread, SLOT(quit()));
  265. thread->start();
  266. }
  267. void Intro::checkPath(const QString &dataDir)
  268. {
  269. mutex.lock();
  270. pathToCheck = dataDir;
  271. if(!signalled)
  272. {
  273. signalled = true;
  274. Q_EMIT requestCheck();
  275. }
  276. mutex.unlock();
  277. }
  278. QString Intro::getPathToCheck()
  279. {
  280. QString retval;
  281. mutex.lock();
  282. retval = pathToCheck;
  283. signalled = false; /* new request can be queued now */
  284. mutex.unlock();
  285. return retval;
  286. }