Bugfix: Replace "URL" with "URI" where we aren't actually working with URLs
[novacoin.git] / src / qt / guiutil.cpp
1 #include "guiutil.h"
2 #include "bitcoinaddressvalidator.h"
3 #include "walletmodel.h"
4 #include "bitcoinunits.h"
5
6 #include "headers.h"
7
8 #include <QString>
9 #include <QDateTime>
10 #include <QDoubleValidator>
11 #include <QFont>
12 #include <QLineEdit>
13 #include <QUrl>
14 #include <QTextDocument> // For Qt::escape
15 #include <QAbstractItemView>
16 #include <QApplication>
17 #include <QClipboard>
18 #include <QFileDialog>
19 #include <QDesktopServices>
20 #include <QThread>
21
22 QString GUIUtil::dateTimeStr(qint64 nTime)
23 {
24     return dateTimeStr(QDateTime::fromTime_t((qint32)nTime));
25 }
26
27 QString GUIUtil::dateTimeStr(const QDateTime &date)
28 {
29     return date.date().toString(Qt::SystemLocaleShortDate) + QString(" ") + date.toString("hh:mm");
30 }
31
32 QFont GUIUtil::bitcoinAddressFont()
33 {
34     QFont font("Monospace");
35     font.setStyleHint(QFont::TypeWriter);
36     return font;
37 }
38
39 void GUIUtil::setupAddressWidget(QLineEdit *widget, QWidget *parent)
40 {
41     widget->setMaxLength(BitcoinAddressValidator::MaxAddressLength);
42     widget->setValidator(new BitcoinAddressValidator(parent));
43     widget->setFont(bitcoinAddressFont());
44 }
45
46 void GUIUtil::setupAmountWidget(QLineEdit *widget, QWidget *parent)
47 {
48     QDoubleValidator *amountValidator = new QDoubleValidator(parent);
49     amountValidator->setDecimals(8);
50     amountValidator->setBottom(0.0);
51     widget->setValidator(amountValidator);
52     widget->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
53 }
54
55 bool GUIUtil::parseBitcoinURI(const QUrl &uri, SendCoinsRecipient *out)
56 {
57     if(uri.scheme() != QString("bitcoin"))
58         return false;
59
60     SendCoinsRecipient rv;
61     rv.address = uri.path();
62     rv.amount = 0;
63     QList<QPair<QString, QString> > items = uri.queryItems();
64     for (QList<QPair<QString, QString> >::iterator i = items.begin(); i != items.end(); i++)
65     {
66         bool fShouldReturnFalse = false;
67         if (i->first.startsWith("req-"))
68         {
69             i->first.remove(0, 4);
70             fShouldReturnFalse = true;
71         }
72
73         if (i->first == "label")
74         {
75             rv.label = i->second;
76             fShouldReturnFalse = false;
77         }
78         else if (i->first == "amount")
79         {
80             if(!i->second.isEmpty())
81             {
82                 if(!BitcoinUnits::parse(BitcoinUnits::BTC, i->second, &rv.amount))
83                 {
84                     return false;
85                 }
86             }
87             fShouldReturnFalse = false;
88         }
89
90         if (fShouldReturnFalse)
91             return false;
92     }
93     if(out)
94     {
95         *out = rv;
96     }
97     return true;
98 }
99
100 bool GUIUtil::parseBitcoinURI(QString uri, SendCoinsRecipient *out)
101 {
102     // Convert bitcoin:// to bitcoin:
103     //
104     //    Cannot handle this later, because bitcoin:// will cause Qt to see the part after // as host,
105     //    which will lowercase it (and thus invalidate the address).
106     if(uri.startsWith("bitcoin://"))
107     {
108         uri.replace(0, 10, "bitcoin:");
109     }
110     QUrl uriInstance(uri);
111     return parseBitcoinURI(uriInstance, out);
112 }
113
114 QString GUIUtil::HtmlEscape(const QString& str, bool fMultiLine)
115 {
116     QString escaped = Qt::escape(str);
117     if(fMultiLine)
118     {
119         escaped = escaped.replace("\n", "<br>\n");
120     }
121     return escaped;
122 }
123
124 QString GUIUtil::HtmlEscape(const std::string& str, bool fMultiLine)
125 {
126     return HtmlEscape(QString::fromStdString(str), fMultiLine);
127 }
128
129 void GUIUtil::copyEntryData(QAbstractItemView *view, int column, int role)
130 {
131     if(!view || !view->selectionModel())
132         return;
133     QModelIndexList selection = view->selectionModel()->selectedRows(column);
134
135     if(!selection.isEmpty())
136     {
137         // Copy first item
138         QApplication::clipboard()->setText(selection.at(0).data(role).toString());
139     }
140 }
141
142 QString GUIUtil::getSaveFileName(QWidget *parent, const QString &caption,
143                                  const QString &dir,
144                                  const QString &filter,
145                                  QString *selectedSuffixOut)
146 {
147     QString selectedFilter;
148     QString myDir;
149     if(dir.isEmpty()) // Default to user documents location
150     {
151         myDir = QDesktopServices::storageLocation(QDesktopServices::DocumentsLocation);
152     }
153     else
154     {
155         myDir = dir;
156     }
157     QString result = QFileDialog::getSaveFileName(parent, caption, myDir, filter, &selectedFilter);
158
159     /* Extract first suffix from filter pattern "Description (*.foo)" or "Description (*.foo *.bar ...) */
160     QRegExp filter_re(".* \\(\\*\\.(.*)[ \\)]");
161     QString selectedSuffix;
162     if(filter_re.exactMatch(selectedFilter))
163     {
164         selectedSuffix = filter_re.cap(1);
165     }
166
167     /* Add suffix if needed */
168     QFileInfo info(result);
169     if(!result.isEmpty())
170     {
171         if(info.suffix().isEmpty() && !selectedSuffix.isEmpty())
172         {
173             /* No suffix specified, add selected suffix */
174             if(!result.endsWith("."))
175                 result.append(".");
176             result.append(selectedSuffix);
177         }
178     }
179
180     /* Return selected suffix if asked to */
181     if(selectedSuffixOut)
182     {
183         *selectedSuffixOut = selectedSuffix;
184     }
185     return result;
186 }
187
188 Qt::ConnectionType GUIUtil::blockingGUIThreadConnection()
189 {
190     if(QThread::currentThread() != QCoreApplication::instance()->thread())
191     {
192         return Qt::BlockingQueuedConnection;
193     }
194     else
195     {
196         return Qt::DirectConnection;
197     }
198 }