fix default suffixes in save dialog in GNOME, make it more clear that PNG is used...
[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 <QFileDialog>
15 #include <QDesktopServices>
16
17 QString GUIUtil::dateTimeStr(qint64 nTime)
18 {
19     return dateTimeStr(QDateTime::fromTime_t((qint32)nTime));
20 }
21
22 QString GUIUtil::dateTimeStr(const QDateTime &date)
23 {
24     return date.date().toString(Qt::SystemLocaleShortDate) + QString(" ") + date.toString("hh:mm");
25 }
26
27 QFont GUIUtil::bitcoinAddressFont()
28 {
29     QFont font("Monospace");
30     font.setStyleHint(QFont::TypeWriter);
31     return font;
32 }
33
34 void GUIUtil::setupAddressWidget(QLineEdit *widget, QWidget *parent)
35 {
36     widget->setMaxLength(BitcoinAddressValidator::MaxAddressLength);
37     widget->setValidator(new BitcoinAddressValidator(parent));
38     widget->setFont(bitcoinAddressFont());
39 }
40
41 void GUIUtil::setupAmountWidget(QLineEdit *widget, QWidget *parent)
42 {
43     QDoubleValidator *amountValidator = new QDoubleValidator(parent);
44     amountValidator->setDecimals(8);
45     amountValidator->setBottom(0.0);
46     widget->setValidator(amountValidator);
47     widget->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
48 }
49
50 bool GUIUtil::parseBitcoinURL(const QUrl *url, SendCoinsRecipient *out)
51 {
52     if(url->scheme() != QString("bitcoin"))
53         return false;
54
55     SendCoinsRecipient rv;
56     rv.address = url->path();
57     rv.label = url->queryItemValue("label");
58
59     QString amount = url->queryItemValue("amount");
60     if(amount.isEmpty())
61     {
62         rv.amount = 0;
63     }
64     else // Amount is non-empty
65     {
66         if(!BitcoinUnits::parse(BitcoinUnits::BTC, amount, &rv.amount))
67         {
68             return false;
69         }
70     }
71     if(out)
72     {
73         *out = rv;
74     }
75     return true;
76 }
77
78 bool GUIUtil::parseBitcoinURL(QString url, SendCoinsRecipient *out)
79 {
80     // Convert bitcoin:// to bitcoin:
81     //
82     //    Cannot handle this later, because bitcoin:// will cause Qt to see the part after // as host,
83     //    which will lowercase it (and thus invalidate the address).
84     if(url.startsWith("bitcoin://"))
85     {
86         url.replace(0, 10, "bitcoin:");
87     }
88     QUrl urlInstance(url);
89     return parseBitcoinURL(&urlInstance, out);
90 }
91
92 QString GUIUtil::getSaveFileName(QWidget *parent, const QString &caption,
93                                  const QString &dir,
94                                  const QString &filter,
95                                  QString *selectedSuffixOut)
96 {
97     QString selectedFilter;
98     QString myDir;
99     if(dir.isEmpty()) // Default to user documents location
100     {
101         myDir = QDesktopServices::storageLocation(QDesktopServices::DocumentsLocation);
102     }
103     else
104     {
105         myDir = dir;
106     }
107     QString result = QFileDialog::getSaveFileName(parent, caption, myDir, filter, &selectedFilter);
108
109     /* Extract first suffix from filter pattern "Description (*.foo)" or "Description (*.foo *.bar ...) */
110     QRegExp filter_re(".* \\(\\*\\.(.*)[ \\)]");
111     QString selectedSuffix;
112     if(filter_re.exactMatch(selectedFilter))
113     {
114         selectedSuffix = filter_re.cap(1);
115     }
116
117     /* Add suffix if needed */
118     QFileInfo info(result);
119     if(!result.isEmpty())
120     {
121         if(info.suffix().isEmpty() && !selectedSuffix.isEmpty())
122         {
123             /* No suffix specified, add selected suffix */
124             if(!result.endsWith("."))
125                 result.append(".");
126             result.append(selectedSuffix);
127         }
128     }
129
130     /* Return selected suffix if asked to */
131     if(selectedSuffixOut)
132     {
133         *selectedSuffixOut = selectedSuffix;
134     }
135     return result;
136 }
137