1 // Copyright (c) 2011-2020 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #ifndef BITCOIN_QT_OPTIONSMODEL_H
6 #define BITCOIN_QT_OPTIONSMODEL_H
7 
8 #include <amount.h>
9 #include <cstdint>
10 #include <qt/guiconstants.h>
11 
12 #include <QAbstractListModel>
13 
14 #include <assert.h>
15 
16 namespace interfaces {
17 class Node;
18 }
19 
20 extern const char *DEFAULT_GUI_PROXY_HOST;
21 static constexpr uint16_t DEFAULT_GUI_PROXY_PORT = 9050;
22 
23 /**
24  * Convert configured prune target MiB to displayed GB. Round up to avoid underestimating max disk usage.
25  */
PruneMiBtoGB(int64_t mib)26 static inline int PruneMiBtoGB(int64_t mib) { return (mib * 1024 * 1024 + GB_BYTES - 1) / GB_BYTES; }
27 
28 /**
29  * Convert displayed prune target GB to configured MiB. Round down so roundtrip GB -> MiB -> GB conversion is stable.
30  */
PruneGBtoMiB(int gb)31 static inline int64_t PruneGBtoMiB(int gb) { return gb * GB_BYTES / 1024 / 1024; }
32 
33 /** Interface from Qt to configuration data structure for Bitcoin client.
34    To Qt, the options are presented as a list with the different options
35    laid out vertically.
36    This can be changed to a tree once the settings become sufficiently
37    complex.
38  */
39 class OptionsModel : public QAbstractListModel
40 {
41     Q_OBJECT
42 
43 public:
44     explicit OptionsModel(QObject *parent = nullptr, bool resetSettings = false);
45 
46     enum OptionID {
47         StartAtStartup,         // bool
48         HideTrayIcon,           // bool
49         MinimizeToTray,         // bool
50         MapPortUPnP,            // bool
51         MinimizeOnClose,        // bool
52         ProxyUse,               // bool
53         ProxyIP,                // QString
54         ProxyPort,              // int
55         ProxyUseTor,            // bool
56         ProxyIPTor,             // QString
57         ProxyPortTor,           // int
58         DisplayUnit,            // BitcoinUnits::Unit
59         ThirdPartyTxUrls,       // QString
60         Language,               // QString
61         CoinControlFeatures,    // bool
62         ThreadsScriptVerif,     // int
63         Prune,                  // bool
64         PruneSize,              // int
65         DatabaseCache,          // int
66         SpendZeroConfChange,    // bool
67         Listen,                 // bool
68         OptionIDRowCount,
69     };
70 
71     void Init(bool resetSettings = false);
72     void Reset();
73 
74     int rowCount(const QModelIndex & parent = QModelIndex()) const override;
75     QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const override;
76     bool setData(const QModelIndex & index, const QVariant & value, int role = Qt::EditRole) override;
77     /** Updates current unit in memory, settings and emits displayUnitChanged(newUnit) signal */
78     void setDisplayUnit(const QVariant &value);
79 
80     /* Explicit getters */
getHideTrayIcon()81     bool getHideTrayIcon() const { return fHideTrayIcon; }
getMinimizeToTray()82     bool getMinimizeToTray() const { return fMinimizeToTray; }
getMinimizeOnClose()83     bool getMinimizeOnClose() const { return fMinimizeOnClose; }
getDisplayUnit()84     int getDisplayUnit() const { return nDisplayUnit; }
getThirdPartyTxUrls()85     QString getThirdPartyTxUrls() const { return strThirdPartyTxUrls; }
getCoinControlFeatures()86     bool getCoinControlFeatures() const { return fCoinControlFeatures; }
getOverriddenByCommandLine()87     const QString& getOverriddenByCommandLine() { return strOverriddenByCommandLine; }
88 
89     /* Explicit setters */
90     void SetPruneEnabled(bool prune, bool force = false);
91     void SetPruneTargetGB(int prune_target_gb, bool force = false);
92 
93     /* Restart flag helper */
94     void setRestartRequired(bool fRequired);
95     bool isRestartRequired() const;
96 
node()97     interfaces::Node& node() const { assert(m_node); return *m_node; }
setNode(interfaces::Node & node)98     void setNode(interfaces::Node& node) { assert(!m_node); m_node = &node; }
99 
100 private:
101     interfaces::Node* m_node = nullptr;
102     /* Qt-only settings */
103     bool fHideTrayIcon;
104     bool fMinimizeToTray;
105     bool fMinimizeOnClose;
106     QString language;
107     int nDisplayUnit;
108     QString strThirdPartyTxUrls;
109     bool fCoinControlFeatures;
110     /* settings that were overridden by command-line */
111     QString strOverriddenByCommandLine;
112 
113     // Add option to list of GUI options overridden through command line/config file
114     void addOverriddenOption(const std::string &option);
115 
116     // Check settings version and upgrade default values if required
117     void checkAndMigrate();
118 Q_SIGNALS:
119     void displayUnitChanged(int unit);
120     void coinControlFeaturesChanged(bool);
121     void hideTrayIconChanged(bool);
122 };
123 
124 #endif // BITCOIN_QT_OPTIONSMODEL_H
125