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 9.2KB

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