Bugfix: Replace "URL" with "URI" where we aren't actually working with URLs
[novacoin.git] / src / qt / guiutil.cpp
index 22d9cc7..2a3063b 100644 (file)
@@ -15,6 +15,9 @@
 #include <QAbstractItemView>
 #include <QApplication>
 #include <QClipboard>
+#include <QFileDialog>
+#include <QDesktopServices>
+#include <QThread>
 
 QString GUIUtil::dateTimeStr(qint64 nTime)
 {
@@ -49,26 +52,43 @@ void GUIUtil::setupAmountWidget(QLineEdit *widget, QWidget *parent)
     widget->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
 }
 
-bool GUIUtil::parseBitcoinURL(const QUrl *url, SendCoinsRecipient *out)
+bool GUIUtil::parseBitcoinURI(const QUrl &uri, SendCoinsRecipient *out)
 {
-    if(url->scheme() != QString("bitcoin"))
+    if(uri.scheme() != QString("bitcoin"))
         return false;
 
     SendCoinsRecipient rv;
-    rv.address = url->path();
-    rv.label = url->queryItemValue("label");
-
-    QString amount = url->queryItemValue("amount");
-    if(amount.isEmpty())
-    {
-        rv.amount = 0;
-    }
-    else // Amount is non-empty
+    rv.address = uri.path();
+    rv.amount = 0;
+    QList<QPair<QString, QString> > items = uri.queryItems();
+    for (QList<QPair<QString, QString> >::iterator i = items.begin(); i != items.end(); i++)
     {
-        if(!BitcoinUnits::parse(BitcoinUnits::BTC, amount, &rv.amount))
+        bool fShouldReturnFalse = false;
+        if (i->first.startsWith("req-"))
         {
-            return false;
+            i->first.remove(0, 4);
+            fShouldReturnFalse = true;
         }
+
+        if (i->first == "label")
+        {
+            rv.label = i->second;
+            fShouldReturnFalse = false;
+        }
+        else if (i->first == "amount")
+        {
+            if(!i->second.isEmpty())
+            {
+                if(!BitcoinUnits::parse(BitcoinUnits::BTC, i->second, &rv.amount))
+                {
+                    return false;
+                }
+            }
+            fShouldReturnFalse = false;
+        }
+
+        if (fShouldReturnFalse)
+            return false;
     }
     if(out)
     {
@@ -77,6 +97,20 @@ bool GUIUtil::parseBitcoinURL(const QUrl *url, SendCoinsRecipient *out)
     return true;
 }
 
+bool GUIUtil::parseBitcoinURI(QString uri, SendCoinsRecipient *out)
+{
+    // Convert bitcoin:// to bitcoin:
+    //
+    //    Cannot handle this later, because bitcoin:// will cause Qt to see the part after // as host,
+    //    which will lowercase it (and thus invalidate the address).
+    if(uri.startsWith("bitcoin://"))
+    {
+        uri.replace(0, 10, "bitcoin:");
+    }
+    QUrl uriInstance(uri);
+    return parseBitcoinURI(uriInstance, out);
+}
+
 QString GUIUtil::HtmlEscape(const QString& str, bool fMultiLine)
 {
     QString escaped = Qt::escape(str);
@@ -104,3 +138,61 @@ void GUIUtil::copyEntryData(QAbstractItemView *view, int column, int role)
         QApplication::clipboard()->setText(selection.at(0).data(role).toString());
     }
 }
+
+QString GUIUtil::getSaveFileName(QWidget *parent, const QString &caption,
+                                 const QString &dir,
+                                 const QString &filter,
+                                 QString *selectedSuffixOut)
+{
+    QString selectedFilter;
+    QString myDir;
+    if(dir.isEmpty()) // Default to user documents location
+    {
+        myDir = QDesktopServices::storageLocation(QDesktopServices::DocumentsLocation);
+    }
+    else
+    {
+        myDir = dir;
+    }
+    QString result = QFileDialog::getSaveFileName(parent, caption, myDir, filter, &selectedFilter);
+
+    /* Extract first suffix from filter pattern "Description (*.foo)" or "Description (*.foo *.bar ...) */
+    QRegExp filter_re(".* \\(\\*\\.(.*)[ \\)]");
+    QString selectedSuffix;
+    if(filter_re.exactMatch(selectedFilter))
+    {
+        selectedSuffix = filter_re.cap(1);
+    }
+
+    /* Add suffix if needed */
+    QFileInfo info(result);
+    if(!result.isEmpty())
+    {
+        if(info.suffix().isEmpty() && !selectedSuffix.isEmpty())
+        {
+            /* No suffix specified, add selected suffix */
+            if(!result.endsWith("."))
+                result.append(".");
+            result.append(selectedSuffix);
+        }
+    }
+
+    /* Return selected suffix if asked to */
+    if(selectedSuffixOut)
+    {
+        *selectedSuffixOut = selectedSuffix;
+    }
+    return result;
+}
+
+Qt::ConnectionType GUIUtil::blockingGUIThreadConnection()
+{
+    if(QThread::currentThread() != QCoreApplication::instance()->thread())
+    {
+        return Qt::BlockingQueuedConnection;
+    }
+    else
+    {
+        return Qt::DirectConnection;
+    }
+}