1 // Copyright (c) 2011-2018 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 #include <qt/bitcoinunits.h>
6 
7 #include <primitives/transaction.h>
8 
9 #include <QStringList>
10 
BitcoinUnits(QObject * parent)11 BitcoinUnits::BitcoinUnits(QObject *parent):
12         QAbstractListModel(parent),
13         unitlist(availableUnits())
14 {
15 }
16 
availableUnits()17 QList<BitcoinUnits::Unit> BitcoinUnits::availableUnits()
18 {
19     QList<BitcoinUnits::Unit> unitlist;
20     unitlist.append(BTC);
21     unitlist.append(mBTC);
22     unitlist.append(uBTC);
23     unitlist.append(SAT);
24     return unitlist;
25 }
26 
valid(int unit)27 bool BitcoinUnits::valid(int unit)
28 {
29     switch(unit)
30     {
31     case BTC:
32     case mBTC:
33     case uBTC:
34     case SAT:
35         return true;
36     default:
37         return false;
38     }
39 }
40 
longName(int unit)41 QString BitcoinUnits::longName(int unit)
42 {
43     switch(unit)
44     {
45     case BTC: return QString("LTC");
46     case mBTC: return QString("lites");
47     case uBTC: return QString("photons");
48     case SAT: return QString("litoshi");
49     default: return QString("???");
50     }
51 }
52 
shortName(int unit)53 QString BitcoinUnits::shortName(int unit)
54 {
55     switch(unit)
56     {
57     case uBTC: return QString::fromUtf8("bits");
58     case SAT: return QString("sat");
59     default: return longName(unit);
60     }
61 }
62 
description(int unit)63 QString BitcoinUnits::description(int unit)
64 {
65     switch(unit)
66     {
67     case BTC: return QString("Litecoins");
68     case mBTC: return QString("Lites (1 / 1" THIN_SP_UTF8 "000)");
69     case uBTC: return QString("Photons (1 / 1" THIN_SP_UTF8 "000" THIN_SP_UTF8 "000)");
70     case SAT: return QString("Litoshis (sat) (1 / 100" THIN_SP_UTF8 "000" THIN_SP_UTF8 "000)");
71     default: return QString("???");
72     }
73 }
74 
factor(int unit)75 qint64 BitcoinUnits::factor(int unit)
76 {
77     switch(unit)
78     {
79     case BTC: return 100000000;
80     case mBTC: return 100000;
81     case uBTC: return 100;
82     case SAT: return 1;
83     default: return 100000000;
84     }
85 }
86 
decimals(int unit)87 int BitcoinUnits::decimals(int unit)
88 {
89     switch(unit)
90     {
91     case BTC: return 8;
92     case mBTC: return 5;
93     case uBTC: return 2;
94     case SAT: return 0;
95     default: return 0;
96     }
97 }
98 
format(int unit,const CAmount & nIn,bool fPlus,SeparatorStyle separators)99 QString BitcoinUnits::format(int unit, const CAmount& nIn, bool fPlus, SeparatorStyle separators)
100 {
101     // Note: not using straight sprintf here because we do NOT want
102     // localized number formatting.
103     if(!valid(unit))
104         return QString(); // Refuse to format invalid unit
105     qint64 n = (qint64)nIn;
106     qint64 coin = factor(unit);
107     int num_decimals = decimals(unit);
108     qint64 n_abs = (n > 0 ? n : -n);
109     qint64 quotient = n_abs / coin;
110     QString quotient_str = QString::number(quotient);
111 
112     // Use SI-style thin space separators as these are locale independent and can't be
113     // confused with the decimal marker.
114     QChar thin_sp(THIN_SP_CP);
115     int q_size = quotient_str.size();
116     if (separators == separatorAlways || (separators == separatorStandard && q_size > 4))
117         for (int i = 3; i < q_size; i += 3)
118             quotient_str.insert(q_size - i, thin_sp);
119 
120     if (n < 0)
121         quotient_str.insert(0, '-');
122     else if (fPlus && n > 0)
123         quotient_str.insert(0, '+');
124 
125     if (num_decimals > 0) {
126         qint64 remainder = n_abs % coin;
127         QString remainder_str = QString::number(remainder).rightJustified(num_decimals, '0');
128         return quotient_str + QString(".") + remainder_str;
129     } else {
130         return quotient_str;
131     }
132 }
133 
134 
135 // NOTE: Using formatWithUnit in an HTML context risks wrapping
136 // quantities at the thousands separator. More subtly, it also results
137 // in a standard space rather than a thin space, due to a bug in Qt's
138 // XML whitespace canonicalisation
139 //
140 // Please take care to use formatHtmlWithUnit instead, when
141 // appropriate.
142 
formatWithUnit(int unit,const CAmount & amount,bool plussign,SeparatorStyle separators)143 QString BitcoinUnits::formatWithUnit(int unit, const CAmount& amount, bool plussign, SeparatorStyle separators)
144 {
145     return format(unit, amount, plussign, separators) + QString(" ") + shortName(unit);
146 }
147 
formatHtmlWithUnit(int unit,const CAmount & amount,bool plussign,SeparatorStyle separators)148 QString BitcoinUnits::formatHtmlWithUnit(int unit, const CAmount& amount, bool plussign, SeparatorStyle separators)
149 {
150     QString str(formatWithUnit(unit, amount, plussign, separators));
151     str.replace(QChar(THIN_SP_CP), QString(THIN_SP_HTML));
152     return QString("<span style='white-space: nowrap;'>%1</span>").arg(str);
153 }
154 
155 
parse(int unit,const QString & value,CAmount * val_out)156 bool BitcoinUnits::parse(int unit, const QString &value, CAmount *val_out)
157 {
158     if(!valid(unit) || value.isEmpty())
159         return false; // Refuse to parse invalid unit or empty string
160     int num_decimals = decimals(unit);
161 
162     // Ignore spaces and thin spaces when parsing
163     QStringList parts = removeSpaces(value).split(".");
164 
165     if(parts.size() > 2)
166     {
167         return false; // More than one dot
168     }
169     QString whole = parts[0];
170     QString decimals;
171 
172     if(parts.size() > 1)
173     {
174         decimals = parts[1];
175     }
176     if(decimals.size() > num_decimals)
177     {
178         return false; // Exceeds max precision
179     }
180     bool ok = false;
181     QString str = whole + decimals.leftJustified(num_decimals, '0');
182 
183     if(str.size() > 18)
184     {
185         return false; // Longer numbers will exceed 63 bits
186     }
187     CAmount retvalue(str.toLongLong(&ok));
188     if(val_out)
189     {
190         *val_out = retvalue;
191     }
192     return ok;
193 }
194 
getAmountColumnTitle(int unit)195 QString BitcoinUnits::getAmountColumnTitle(int unit)
196 {
197     QString amountTitle = QObject::tr("Amount");
198     if (BitcoinUnits::valid(unit))
199     {
200         amountTitle += " ("+BitcoinUnits::shortName(unit) + ")";
201     }
202     return amountTitle;
203 }
204 
rowCount(const QModelIndex & parent) const205 int BitcoinUnits::rowCount(const QModelIndex &parent) const
206 {
207     Q_UNUSED(parent);
208     return unitlist.size();
209 }
210 
data(const QModelIndex & index,int role) const211 QVariant BitcoinUnits::data(const QModelIndex &index, int role) const
212 {
213     int row = index.row();
214     if(row >= 0 && row < unitlist.size())
215     {
216         Unit unit = unitlist.at(row);
217         switch(role)
218         {
219         case Qt::EditRole:
220         case Qt::DisplayRole:
221             return QVariant(longName(unit));
222         case Qt::ToolTipRole:
223             return QVariant(description(unit));
224         case UnitRole:
225             return QVariant(static_cast<int>(unit));
226         }
227     }
228     return QVariant();
229 }
230 
maxMoney()231 CAmount BitcoinUnits::maxMoney()
232 {
233     return MAX_MONEY;
234 }
235