1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
crate::ix!();

//-------------------------------------------[.cpp/bitcoin/src/qt/optionsdialog.h]

/**
  | Proxy address widget validator, checks
  | for a valid proxy address.
  |
  */
#[Q_OBJECT]
pub struct ProxyAddressValidator {
    base: QValidator,
}

impl ProxyAddressValidator {
    
    pub fn new(parent: *mut QObject) -> Self {
    
        todo!();
        /*
           :
           QValidator(parent)
        */
    }
    
    pub fn validate(&self, 
        input: &mut String,
        pos:   &mut i32) -> QValidatorState {
        
        todo!();
        /*
            Q_UNUSED(pos);
        // Validate the proxy
        CService serv(LookupNumeric(input.toStdString(), DEFAULT_GUI_PROXY_PORT));
        proxyType addrProxy = proxyType(serv, true);
        if (addrProxy.IsValid())
            return QValidatorAcceptable;

        return QValidatorInvalid;
        */
    }
}

/**
  | Preferences dialog.
  |
  */
#[Q_OBJECT]
pub struct OptionsDialog {
    base:   QDialog,
    ui:     *mut UiOptionsDialog,
    model:  *mut OptionsModel,
    mapper: *mut QDataWidgetMapper,
}

pub mod options_dialog {

    pub enum Tab {
        TAB_MAIN,
        TAB_NETWORK,
    }
}

//-------------------------------------------[.cpp/bitcoin/src/qt/optionsdialog.cpp]
impl Drop for OptionsDialog {
    fn drop(&mut self) {
        todo!();
        /*
            delete ui;
        */
    }
}

impl OptionsDialog {

    #[Q_SIGNAL]
    pub fn proxy_ip_checks(&mut self, 
        ui_proxy_ip:  *mut QValidatedLineEdit,
        n_proxy_port: u16)  {
        
        todo!();
        /*
        
        */
    }
    
    #[Q_SIGNAL]
    pub fn quit_on_reset(&mut self)  {
        
        todo!();
        /*
        
        */
    }
    
    pub fn new(
        parent:        *mut QWidget,
        enable_wallet: bool) -> Self {
    
        todo!();
        /*
           :
        QDialog(parent, typename gui_util::dialog_flags),
        ui(new UiOptionsDialog),
        model(nullptr),
        mapper(nullptr)

        ui->setupUi(this);

        /* Main elements init */
        ui->databaseCache->setMinimum(nMinDbCache);
        ui->databaseCache->setMaximum(nMaxDbCache);
        ui->threadsScriptVerif->setMinimum(-GetNumCores());
        ui->threadsScriptVerif->setMaximum(MAX_SCRIPTCHECK_THREADS);
        ui->pruneWarning->setVisible(false);
        ui->pruneWarning->setStyleSheet("QLabel { color: red; }");

        ui->pruneSize->setEnabled(false);
        connect(ui->prune, &QPushButton::toggled, ui->pruneSize, &QWidget::setEnabled);

        /* Network elements init */
    #ifndef USE_UPNP
        ui->mapPortUpnp->setEnabled(false);
    #endif
    #ifndef USE_NATPMP
        ui->mapPortNatpmp->setEnabled(false);
    #endif
        connect(this, &QDialog::accepted, [this](){
            QSettings settings;
            model->node().mapPort(settings.value("fUseUPnP").toBool(), settings.value("fUseNatpmp").toBool());
        });

        ui->proxyIp->setEnabled(false);
        ui->proxyPort->setEnabled(false);
        ui->proxyPort->setValidator(new QIntValidator(1, 65535, this));

        ui->proxyIpTor->setEnabled(false);
        ui->proxyPortTor->setEnabled(false);
        ui->proxyPortTor->setValidator(new QIntValidator(1, 65535, this));

        connect(ui->connectSocks, &QPushButton::toggled, ui->proxyIp, &QWidget::setEnabled);
        connect(ui->connectSocks, &QPushButton::toggled, ui->proxyPort, &QWidget::setEnabled);
        connect(ui->connectSocks, &QPushButton::toggled, this, &OptionsDialog::updateProxyValidationState);

        connect(ui->connectSocksTor, &QPushButton::toggled, ui->proxyIpTor, &QWidget::setEnabled);
        connect(ui->connectSocksTor, &QPushButton::toggled, ui->proxyPortTor, &QWidget::setEnabled);
        connect(ui->connectSocksTor, &QPushButton::toggled, this, &OptionsDialog::updateProxyValidationState);

        /* Window elements init */
    #ifdef Q_OS_MAC
        /* remove Window tab on Mac */
        ui->tabWidget->removeTab(ui->tabWidget->indexOf(ui->tabWindow));
        /* hide launch at startup option on macOS */
        ui->bitcoinAtStartup->setVisible(false);
        ui->verticalLayout_Main->removeWidget(ui->bitcoinAtStartup);
        ui->verticalLayout_Main->removeItem(ui->horizontalSpacer_0_Main);
    #endif

        /* remove Wallet tab and 3rd party-URL textbox in case of -disablewallet */
        if (!enableWallet) {
            ui->tabWidget->removeTab(ui->tabWidget->indexOf(ui->tabWallet));
            ui->thirdPartyTxUrlsLabel->setVisible(false);
            ui->thirdPartyTxUrls->setVisible(false);
        }

    #ifndef ENABLE_EXTERNAL_SIGNER
        //: "External signing" means using devices such as hardware wallets.
        ui->externalSignerPath->setToolTip(tr("Compiled without external signing support (required for external signing)"));
        ui->externalSignerPath->setEnabled(false);
    #endif
        /* Display elements init */
        QDir translations(":translations");

        ui->bitcoinAtStartup->setToolTip(ui->bitcoinAtStartup->toolTip().arg(PACKAGE_NAME));
        ui->bitcoinAtStartup->setText(ui->bitcoinAtStartup->text().arg(PACKAGE_NAME));

        ui->openBitcoinConfButton->setToolTip(ui->openBitcoinConfButton->toolTip().arg(PACKAGE_NAME));

        ui->lang->setToolTip(ui->lang->toolTip().arg(PACKAGE_NAME));
        ui->lang->addItem(QString("(") + tr("default") + QString(")"), QVariant(""));
        for (const QString &langStr : translations.entryList())
        {
            QLocale locale(langStr);

            /** check if the locale name consists of 2 parts (language_country) */
            if(langStr.contains("_"))
            {
                /** display language strings as "native language - native country (locale name)", e.g. "Deutsch - Deutschland (de)" */
                ui->lang->addItem(locale.nativeLanguageName() + QString(" - ") + locale.nativeCountryName() + QString(" (") + langStr + QString(")"), QVariant(langStr));
            }
            else
            {
                /** display language strings as "native language (locale name)", e.g. "Deutsch (de)" */
                ui->lang->addItem(locale.nativeLanguageName() + QString(" (") + langStr + QString(")"), QVariant(langStr));
            }
        }
        ui->unit->setModel(new BitcoinUnits(this));

        /* Widget-to-option mapper */
        mapper = new QDataWidgetMapper(this);
        mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
        mapper->setOrientation(QtVertical);

        typename gui_util::ItemDelegate* delegate = new typename gui_util::ItemDelegate(mapper);
        connect(delegate, &typename gui_util::ItemDelegate::keyEscapePressed, this, &OptionsDialog::reject);
        mapper->setItemDelegate(delegate);

        /* setup/change UI elements when proxy IPs are invalid/valid */
        ui->proxyIp->setCheckValidator(new ProxyAddressValidator(parent));
        ui->proxyIpTor->setCheckValidator(new ProxyAddressValidator(parent));
        connect(ui->proxyIp, &QValidatedLineEdit::validationDidChange, this, &OptionsDialog::updateProxyValidationState);
        connect(ui->proxyIpTor, &QValidatedLineEdit::validationDidChange, this, &OptionsDialog::updateProxyValidationState);
        connect(ui->proxyPort, &QLineEdit::textChanged, this, &OptionsDialog::updateProxyValidationState);
        connect(ui->proxyPortTor, &QLineEdit::textChanged, this, &OptionsDialog::updateProxyValidationState);

        if (!QSystemTrayIcon::isSystemTrayAvailable()) {
            ui->showTrayIcon->setChecked(false);
            ui->showTrayIcon->setEnabled(false);
            ui->minimizeToTray->setChecked(false);
            ui->minimizeToTray->setEnabled(false);
        }

        QFont embedded_font{typename gui_util::fixedPitchFont(true)};
        ui->embeddedFont_radioButton->setText(ui->embeddedFont_radioButton->text().arg(QFontInfo(embedded_font).family()));
        embedded_font.setWeight(QFont::Bold);
        ui->embeddedFont_label_1->setFont(embedded_font);
        ui->embeddedFont_label_9->setFont(embedded_font);

        QFont system_font{typename gui_util::fixedPitchFont(false)};
        ui->systemFont_radioButton->setText(ui->systemFont_radioButton->text().arg(QFontInfo(system_font).family()));
        system_font.setWeight(QFont::Bold);
        ui->systemFont_label_1->setFont(system_font);
        ui->systemFont_label_9->setFont(system_font);
        // Checking the embeddedFont_radioButton automatically unchecks the systemFont_radioButton.
        ui->systemFont_radioButton->setChecked(true);

        typename gui_util::handleCloseWindowShortcut(this);
        */
    }
    
    pub fn set_model(&mut self, model: *mut OptionsModel)  {
        
        todo!();
        /*
            this->model = _model;

        if(_model)
        {
            /* check if client restart is needed and show persistent message */
            if (_model->isRestartRequired())
                showRestartWarning(true);

            // Prune values are in GB to be consistent with intro.cpp
            static constexpr uint64_t nMinDiskSpace = (MIN_DISK_SPACE_FOR_BLOCK_FILES / GB_BYTES) + (MIN_DISK_SPACE_FOR_BLOCK_FILES % GB_BYTES) ? 1 : 0;
            ui->pruneSize->setRange(nMinDiskSpace, std::numeric_limits<int>::max());

            QString strLabel = _model->getOverriddenByCommandLine();
            if (strLabel.isEmpty())
                strLabel = tr("none");
            ui->overriddenByCommandLineLabel->setText(strLabel);

            mapper->setModel(_model);
            setMapper();
            mapper->toFirst();

            updateDefaultProxyNets();
        }

        /* warn when one of the following settings changes by user action (placed here so init via mapper doesn't trigger them) */

        /* Main */
        connect(ui->prune, &QCheckBox::clicked, this, &OptionsDialog::showRestartWarning);
        connect(ui->prune, &QCheckBox::clicked, this, &OptionsDialog::togglePruneWarning);
        connect(ui->pruneSize, qOverload<int>(&QSpinBox::valueChanged), this, &OptionsDialog::showRestartWarning);
        connect(ui->databaseCache, qOverload<int>(&QSpinBox::valueChanged), this, &OptionsDialog::showRestartWarning);
        connect(ui->externalSignerPath, &QLineEdit::textChanged, [this]{ showRestartWarning(); });
        connect(ui->threadsScriptVerif, qOverload<int>(&QSpinBox::valueChanged), this, &OptionsDialog::showRestartWarning);
        /* Wallet */
        connect(ui->spendZeroConfChange, &QCheckBox::clicked, this, &OptionsDialog::showRestartWarning);
        /* Network */
        connect(ui->allowIncoming, &QCheckBox::clicked, this, &OptionsDialog::showRestartWarning);
        connect(ui->enableServer, &QCheckBox::clicked, this, &OptionsDialog::showRestartWarning);
        connect(ui->connectSocks, &QCheckBox::clicked, this, &OptionsDialog::showRestartWarning);
        connect(ui->connectSocksTor, &QCheckBox::clicked, this, &OptionsDialog::showRestartWarning);
        /* Display */
        connect(ui->lang, qOverload<>(&QValueComboBox::valueChanged), [this]{ showRestartWarning(); });
        connect(ui->thirdPartyTxUrls, &QLineEdit::textChanged, [this]{ showRestartWarning(); });
        */
    }
    
    pub fn set_current_tab(&mut self, tab: OptionsDialogTab)  {
        
        todo!();
        /*
            QWidget *tab_widget = nullptr;
        if (tab == OptionsDialog::Tab::TAB_NETWORK) tab_widget = ui->tabNetwork;
        if (tab == OptionsDialog::Tab::TAB_MAIN) tab_widget = ui->tabMain;
        if (tab_widget && ui->tabWidget->currentWidget() != tab_widget) {
            ui->tabWidget->setCurrentWidget(tab_widget);
        }
        */
    }
    
    pub fn set_mapper(&mut self)  {
        
        todo!();
        /*
            /* Main */
        mapper->addMapping(ui->bitcoinAtStartup, OptionsModel::StartAtStartup);
        mapper->addMapping(ui->threadsScriptVerif, OptionsModel::ThreadsScriptVerif);
        mapper->addMapping(ui->databaseCache, OptionsModel::DatabaseCache);
        mapper->addMapping(ui->prune, OptionsModel::Prune);
        mapper->addMapping(ui->pruneSize, OptionsModel::PruneSize);

        /* Wallet */
        mapper->addMapping(ui->spendZeroConfChange, OptionsModel::SpendZeroConfChange);
        mapper->addMapping(ui->coinControlFeatures, OptionsModel::CoinControlFeatures);
        mapper->addMapping(ui->subFeeFromAmount, OptionsModel::SubFeeFromAmount);
        mapper->addMapping(ui->externalSignerPath, OptionsModel::ExternalSignerPath);

        /* Network */
        mapper->addMapping(ui->mapPortUpnp, OptionsModel::MapPortUPnP);
        mapper->addMapping(ui->mapPortNatpmp, OptionsModel::MapPortNatpmp);
        mapper->addMapping(ui->allowIncoming, OptionsModel::Listen);
        mapper->addMapping(ui->enableServer, OptionsModel::Server);

        mapper->addMapping(ui->connectSocks, OptionsModel::ProxyUse);
        mapper->addMapping(ui->proxyIp, OptionsModel::ProxyIP);
        mapper->addMapping(ui->proxyPort, OptionsModel::ProxyPort);

        mapper->addMapping(ui->connectSocksTor, OptionsModel::ProxyUseTor);
        mapper->addMapping(ui->proxyIpTor, OptionsModel::ProxyIPTor);
        mapper->addMapping(ui->proxyPortTor, OptionsModel::ProxyPortTor);

        /* Window */
    #ifndef Q_OS_MAC
        if (QSystemTrayIcon::isSystemTrayAvailable()) {
            mapper->addMapping(ui->showTrayIcon, OptionsModel::ShowTrayIcon);
            mapper->addMapping(ui->minimizeToTray, OptionsModel::MinimizeToTray);
        }
        mapper->addMapping(ui->minimizeOnClose, OptionsModel::MinimizeOnClose);
    #endif

        /* Display */
        mapper->addMapping(ui->lang, OptionsModel::Language);
        mapper->addMapping(ui->unit, OptionsModel::DisplayUnit);
        mapper->addMapping(ui->thirdPartyTxUrls, OptionsModel::ThirdPartyTxUrls);
        mapper->addMapping(ui->embeddedFont_radioButton, OptionsModel::UseEmbeddedMonospacedFont);
        */
    }
    
    /**
      | set OK button state (enabled / disabled)
      |
      */
    #[Q_SLOT]
    pub fn set_ok_button_state(&mut self, state: bool)  {
        
        todo!();
        /*
            ui->okButton->setEnabled(fState);
        */
    }
    
    #[Q_SLOT]
    pub fn on_reset_button_clicked(&mut self)  {
        
        todo!();
        /*
            if(model)
        {
            // confirmation dialog
            QMessageBox::StandardButton btnRetVal = QMessageBox::question(this, tr("Confirm options reset"),
                tr("Client restart required to activate changes.") + "<br><br>" + tr("Client will be shut down. Do you want to proceed?"),
                QMessageBox::Yes | QMessageBox::Cancel, QMessageBox::Cancel);

            if(btnRetVal == QMessageBox::Cancel)
                return;

            /* reset all options and close GUI */
            model->Reset();
            close();
            Q_EMIT quitOnReset();
        }
        */
    }
    
    #[Q_SLOT]
    pub fn on_open_bitcoin_conf_button_clicked(&mut self)  {
        
        todo!();
        /*
            QMessageBox config_msgbox(this);
        config_msgbox.setIcon(QMessageBox::Information);
        //: Window title text of pop-up box that allows opening up of configuration file.
        config_msgbox.setWindowTitle(tr("Configuration options"));
        /*: Explanatory text about the priority order of instructions considered by client.
            The order from high to low being: command-line, configuration file, GUI settings. */
        config_msgbox.setText(tr("The configuration file is used to specify advanced user options which override GUI settings. "
                                 "Additionally, any command-line options will override this configuration file."));

        QPushButton* open_button = config_msgbox.addButton(tr("Continue"), QMessageBox::ActionRole);
        config_msgbox.addButton(tr("Cancel"), QMessageBox::RejectRole);
        open_button->setDefault(true);

        config_msgbox.exec();

        if (config_msgbox.clickedButton() != open_button) return;

        /* show an error if there was some problem opening the file */
        if (!typename gui_util::openBitcoinConf())
            QMessageBox::critical(this, tr("Error"), tr("The configuration file could not be opened."));
        */
    }
    
    #[Q_SLOT]
    pub fn on_ok_button_clicked(&mut self)  {
        
        todo!();
        /*
            mapper->submit();
        accept();
        updateDefaultProxyNets();
        */
    }
    
    #[Q_SLOT]
    pub fn on_cancel_button_clicked(&mut self)  {
        
        todo!();
        /*
            reject();
        */
    }
    
    #[Q_SLOT]
    pub fn on_show_tray_icon_state_changed(&mut self, state: i32)  {
        
        todo!();
        /*
            if (state == QtChecked) {
            ui->minimizeToTray->setEnabled(true);
        } else {
            ui->minimizeToTray->setChecked(false);
            ui->minimizeToTray->setEnabled(false);
        }
        */
    }
    
    #[Q_SLOT]
    pub fn toggle_prune_warning(&mut self, enabled: bool)  {
        
        todo!();
        /*
            ui->pruneWarning->setVisible(!ui->pruneWarning->isVisible());
        */
    }
    
    #[Q_SLOT]
    pub fn show_restart_warning(&mut self, persistent: Option<bool>)  {

        let persistent: bool = persistent.unwrap_or(false);
        
        todo!();
        /*
            ui->statusLabel->setStyleSheet("QLabel { color: red; }");

        if(fPersistent)
        {
            ui->statusLabel->setText(tr("Client restart required to activate changes."));
        }
        else
        {
            ui->statusLabel->setText(tr("This change would require a client restart."));
            // clear non-persistent status label after 10 seconds
            // Todo: should perhaps be a class attribute, if we extend the use of statusLabel
            QTimer::singleShot(10000, this, &OptionsDialog::clearStatusLabel);
        }
        */
    }
    
    #[Q_SLOT]
    pub fn clear_status_label(&mut self)  {
        
        todo!();
        /*
            ui->statusLabel->clear();
        if (model && model->isRestartRequired()) {
            showRestartWarning(true);
        }
        */
    }
    
    #[Q_SLOT]
    pub fn update_proxy_validation_state(&mut self)  {
        
        todo!();
        /*
            QValidatedLineEdit *pUiProxyIp = ui->proxyIp;
        QValidatedLineEdit *otherProxyWidget = (pUiProxyIp == ui->proxyIpTor) ? ui->proxyIp : ui->proxyIpTor;
        if (pUiProxyIp->isValid() && (!ui->proxyPort->isEnabled() || ui->proxyPort->text().toInt() > 0) && (!ui->proxyPortTor->isEnabled() || ui->proxyPortTor->text().toInt() > 0))
        {
            setOkButtonState(otherProxyWidget->isValid()); //only enable ok button if both proxys are valid
            clearStatusLabel();
        }
        else
        {
            setOkButtonState(false);
            ui->statusLabel->setStyleSheet("QLabel { color: red; }");
            ui->statusLabel->setText(tr("The supplied proxy address is invalid."));
        }
        */
    }
    
    /**
      | query the networks, for which the default
      | proxy is used
      |
      */
    #[Q_SLOT]
    pub fn update_default_proxy_nets(&mut self)  {
        
        todo!();
        /*
            proxyType proxy;
        std::string strProxy;
        QString strDefaultProxyGUI;

        model->node().getProxy(NET_IPV4, proxy);
        strProxy = proxy.proxy.ToStringIP() + ":" + proxy.proxy.ToStringPort();
        strDefaultProxyGUI = ui->proxyIp->text() + ":" + ui->proxyPort->text();
        (strProxy == strDefaultProxyGUI.toStdString()) ? ui->proxyReachIPv4->setChecked(true) : ui->proxyReachIPv4->setChecked(false);

        model->node().getProxy(NET_IPV6, proxy);
        strProxy = proxy.proxy.ToStringIP() + ":" + proxy.proxy.ToStringPort();
        strDefaultProxyGUI = ui->proxyIp->text() + ":" + ui->proxyPort->text();
        (strProxy == strDefaultProxyGUI.toStdString()) ? ui->proxyReachIPv6->setChecked(true) : ui->proxyReachIPv6->setChecked(false);

        model->node().getProxy(NET_ONION, proxy);
        strProxy = proxy.proxy.ToStringIP() + ":" + proxy.proxy.ToStringPort();
        strDefaultProxyGUI = ui->proxyIp->text() + ":" + ui->proxyPort->text();
        (strProxy == strDefaultProxyGUI.toStdString()) ? ui->proxyReachTor->setChecked(true) : ui->proxyReachTor->setChecked(false);
        */
    }
}