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.

optionsdialog.cpp 10.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. // Copyright (c) 2011-2013 The Bitcoin developers
  2. // Distributed under the MIT/X11 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 "bitcoin-config.h"
  6. #endif
  7. #include "optionsdialog.h"
  8. #include "ui_optionsdialog.h"
  9. #include "bitcoinunits.h"
  10. #include "guiutil.h"
  11. #include "monitoreddatamapper.h"
  12. #include "optionsmodel.h"
  13. #include "main.h" // for CTransaction::nMinTxFee and MAX_SCRIPTCHECK_THREADS
  14. #include "netbase.h"
  15. #include "txdb.h" // for -dbcache defaults
  16. #include <QDir>
  17. #include <QIntValidator>
  18. #include <QLocale>
  19. #include <QMessageBox>
  20. #include <QTimer>
  21. OptionsDialog::OptionsDialog(QWidget *parent) :
  22. QDialog(parent),
  23. ui(new Ui::OptionsDialog),
  24. model(0),
  25. mapper(0),
  26. fProxyIpValid(true)
  27. {
  28. ui->setupUi(this);
  29. GUIUtil::restoreWindowGeometry("nOptionsDialogWindow", this->size(), this);
  30. /* Main elements init */
  31. ui->databaseCache->setMinimum(nMinDbCache);
  32. ui->databaseCache->setMaximum(nMaxDbCache);
  33. ui->threadsScriptVerif->setMinimum(-(int)boost::thread::hardware_concurrency());
  34. ui->threadsScriptVerif->setMaximum(MAX_SCRIPTCHECK_THREADS);
  35. /* Network elements init */
  36. #ifndef USE_UPNP
  37. ui->mapPortUpnp->setEnabled(false);
  38. #endif
  39. ui->proxyIp->setEnabled(false);
  40. ui->proxyPort->setEnabled(false);
  41. ui->proxyPort->setValidator(new QIntValidator(1, 65535, this));
  42. /** SOCKS version is only selectable for default proxy and is always 5 for IPv6 and Tor */
  43. ui->socksVersion->setEnabled(false);
  44. ui->socksVersion->addItem("5", 5);
  45. ui->socksVersion->addItem("4", 4);
  46. ui->socksVersion->setCurrentIndex(0);
  47. connect(ui->connectSocks, SIGNAL(toggled(bool)), ui->proxyIp, SLOT(setEnabled(bool)));
  48. connect(ui->connectSocks, SIGNAL(toggled(bool)), ui->proxyPort, SLOT(setEnabled(bool)));
  49. connect(ui->connectSocks, SIGNAL(toggled(bool)), ui->socksVersion, SLOT(setEnabled(bool)));
  50. ui->proxyIp->installEventFilter(this);
  51. /* Window elements init */
  52. #ifdef Q_OS_MAC
  53. /* remove Window tab on Mac */
  54. ui->tabWidget->removeTab(ui->tabWidget->indexOf(ui->tabWindow));
  55. #endif
  56. /* Display elements init */
  57. QDir translations(":translations");
  58. ui->lang->addItem(QString("(") + tr("default") + QString(")"), QVariant(""));
  59. foreach(const QString &langStr, translations.entryList())
  60. {
  61. QLocale locale(langStr);
  62. /** check if the locale name consists of 2 parts (language_country) */
  63. if(langStr.contains("_"))
  64. {
  65. #if QT_VERSION >= 0x040800
  66. /** display language strings as "native language - native country (locale name)", e.g. "Deutsch - Deutschland (de)" */
  67. ui->lang->addItem(locale.nativeLanguageName() + QString(" - ") + locale.nativeCountryName() + QString(" (") + langStr + QString(")"), QVariant(langStr));
  68. #else
  69. /** display language strings as "language - country (locale name)", e.g. "German - Germany (de)" */
  70. ui->lang->addItem(QLocale::languageToString(locale.language()) + QString(" - ") + QLocale::countryToString(locale.country()) + QString(" (") + langStr + QString(")"), QVariant(langStr));
  71. #endif
  72. }
  73. else
  74. {
  75. #if QT_VERSION >= 0x040800
  76. /** display language strings as "native language (locale name)", e.g. "Deutsch (de)" */
  77. ui->lang->addItem(locale.nativeLanguageName() + QString(" (") + langStr + QString(")"), QVariant(langStr));
  78. #else
  79. /** display language strings as "language (locale name)", e.g. "German (de)" */
  80. ui->lang->addItem(QLocale::languageToString(locale.language()) + QString(" (") + langStr + QString(")"), QVariant(langStr));
  81. #endif
  82. }
  83. }
  84. #if QT_VERSION >= 0x040700
  85. ui->thirdPartyTxUrls->setPlaceholderText("https://example.com/tx/%s");
  86. #endif
  87. ui->unit->setModel(new BitcoinUnits(this));
  88. ui->transactionFee->setSingleStep(CTransaction::nMinTxFee);
  89. /* Widget-to-option mapper */
  90. mapper = new MonitoredDataMapper(this);
  91. mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
  92. mapper->setOrientation(Qt::Vertical);
  93. /* setup/change UI elements when proxy IP is invalid/valid */
  94. connect(this, SIGNAL(proxyIpChecks(QValidatedLineEdit *, int)), this, SLOT(doProxyIpChecks(QValidatedLineEdit *, int)));
  95. }
  96. OptionsDialog::~OptionsDialog()
  97. {
  98. GUIUtil::saveWindowGeometry("nOptionsDialogWindow", this);
  99. delete ui;
  100. }
  101. void OptionsDialog::setModel(OptionsModel *model)
  102. {
  103. this->model = model;
  104. if(model)
  105. {
  106. /* check if client restart is needed and show persistent message */
  107. if (model->isRestartRequired())
  108. showRestartWarning(true);
  109. QString strLabel = model->getOverriddenByCommandLine();
  110. if (strLabel.isEmpty())
  111. strLabel = tr("none");
  112. ui->overriddenByCommandLineLabel->setText(strLabel);
  113. connect(model, SIGNAL(displayUnitChanged(int)), this, SLOT(updateDisplayUnit()));
  114. mapper->setModel(model);
  115. setMapper();
  116. mapper->toFirst();
  117. }
  118. /* update the display unit, to not use the default ("BTC") */
  119. updateDisplayUnit();
  120. /* warn when one of the following settings changes by user action (placed here so init via mapper doesn't trigger them) */
  121. /* Main */
  122. connect(ui->databaseCache, SIGNAL(valueChanged(int)), this, SLOT(showRestartWarning()));
  123. connect(ui->threadsScriptVerif, SIGNAL(valueChanged(int)), this, SLOT(showRestartWarning()));
  124. /* Wallet */
  125. connect(ui->spendZeroConfChange, SIGNAL(clicked(bool)), this, SLOT(showRestartWarning()));
  126. /* Network */
  127. connect(ui->connectSocks, SIGNAL(clicked(bool)), this, SLOT(showRestartWarning()));
  128. /* Display */
  129. connect(ui->lang, SIGNAL(valueChanged()), this, SLOT(showRestartWarning()));
  130. connect(ui->thirdPartyTxUrls, SIGNAL(textChanged(const QString &)), this, SLOT(showRestartWarning()));
  131. }
  132. void OptionsDialog::setMapper()
  133. {
  134. /* Main */
  135. mapper->addMapping(ui->bitcoinAtStartup, OptionsModel::StartAtStartup);
  136. mapper->addMapping(ui->threadsScriptVerif, OptionsModel::ThreadsScriptVerif);
  137. mapper->addMapping(ui->databaseCache, OptionsModel::DatabaseCache);
  138. /* Wallet */
  139. mapper->addMapping(ui->transactionFee, OptionsModel::Fee);
  140. mapper->addMapping(ui->spendZeroConfChange, OptionsModel::SpendZeroConfChange);
  141. mapper->addMapping(ui->coinControlFeatures, OptionsModel::CoinControlFeatures);
  142. /* Network */
  143. mapper->addMapping(ui->mapPortUpnp, OptionsModel::MapPortUPnP);
  144. mapper->addMapping(ui->connectSocks, OptionsModel::ProxyUse);
  145. mapper->addMapping(ui->proxyIp, OptionsModel::ProxyIP);
  146. mapper->addMapping(ui->proxyPort, OptionsModel::ProxyPort);
  147. mapper->addMapping(ui->socksVersion, OptionsModel::ProxySocksVersion);
  148. /* Window */
  149. #ifndef Q_OS_MAC
  150. mapper->addMapping(ui->minimizeToTray, OptionsModel::MinimizeToTray);
  151. mapper->addMapping(ui->minimizeOnClose, OptionsModel::MinimizeOnClose);
  152. #endif
  153. /* Display */
  154. mapper->addMapping(ui->lang, OptionsModel::Language);
  155. mapper->addMapping(ui->unit, OptionsModel::DisplayUnit);
  156. mapper->addMapping(ui->displayAddresses, OptionsModel::DisplayAddresses);
  157. mapper->addMapping(ui->thirdPartyTxUrls, OptionsModel::ThirdPartyTxUrls);
  158. }
  159. void OptionsDialog::enableOkButton()
  160. {
  161. /* prevent enabling of the OK button when data modified, if there is an invalid proxy address present */
  162. if(fProxyIpValid)
  163. setOkButtonState(true);
  164. }
  165. void OptionsDialog::disableOkButton()
  166. {
  167. setOkButtonState(false);
  168. }
  169. void OptionsDialog::setOkButtonState(bool fState)
  170. {
  171. ui->okButton->setEnabled(fState);
  172. }
  173. void OptionsDialog::on_resetButton_clicked()
  174. {
  175. if(model)
  176. {
  177. // confirmation dialog
  178. QMessageBox::StandardButton btnRetVal = QMessageBox::question(this, tr("Confirm options reset"),
  179. tr("Client restart required to activate changes.") + "<br><br>" + tr("Client will be shutdown, do you want to proceed?"),
  180. QMessageBox::Yes | QMessageBox::Cancel, QMessageBox::Cancel);
  181. if(btnRetVal == QMessageBox::Cancel)
  182. return;
  183. /* reset all options and close GUI */
  184. model->Reset();
  185. QApplication::quit();
  186. }
  187. }
  188. void OptionsDialog::on_okButton_clicked()
  189. {
  190. mapper->submit();
  191. accept();
  192. }
  193. void OptionsDialog::on_cancelButton_clicked()
  194. {
  195. reject();
  196. }
  197. void OptionsDialog::showRestartWarning(bool fPersistent)
  198. {
  199. ui->statusLabel->setStyleSheet("QLabel { color: red; }");
  200. if(fPersistent)
  201. {
  202. ui->statusLabel->setText(tr("Client restart required to activate changes."));
  203. }
  204. else
  205. {
  206. ui->statusLabel->setText(tr("This change would require a client restart."));
  207. // clear non-persistent status label after 10 seconds
  208. // Todo: should perhaps be a class attribute, if we extend the use of statusLabel
  209. QTimer::singleShot(10000, this, SLOT(clearStatusLabel()));
  210. }
  211. }
  212. void OptionsDialog::clearStatusLabel()
  213. {
  214. ui->statusLabel->clear();
  215. }
  216. void OptionsDialog::updateDisplayUnit()
  217. {
  218. if(model)
  219. {
  220. /* Update transactionFee with the current unit */
  221. ui->transactionFee->setDisplayUnit(model->getDisplayUnit());
  222. }
  223. }
  224. void OptionsDialog::doProxyIpChecks(QValidatedLineEdit *pUiProxyIp, int nProxyPort)
  225. {
  226. Q_UNUSED(nProxyPort);
  227. const std::string strAddrProxy = pUiProxyIp->text().toStdString();
  228. CService addrProxy;
  229. /* Check for a valid IPv4 / IPv6 address */
  230. if (!(fProxyIpValid = LookupNumeric(strAddrProxy.c_str(), addrProxy)))
  231. {
  232. disableOkButton();
  233. pUiProxyIp->setValid(false);
  234. ui->statusLabel->setStyleSheet("QLabel { color: red; }");
  235. ui->statusLabel->setText(tr("The supplied proxy address is invalid."));
  236. }
  237. else
  238. {
  239. enableOkButton();
  240. ui->statusLabel->clear();
  241. }
  242. }
  243. bool OptionsDialog::eventFilter(QObject *object, QEvent *event)
  244. {
  245. if(event->type() == QEvent::FocusOut)
  246. {
  247. if(object == ui->proxyIp)
  248. {
  249. emit proxyIpChecks(ui->proxyIp, ui->proxyPort->text().toInt());
  250. }
  251. }
  252. return QDialog::eventFilter(object, event);
  253. }