add sendmany support
[novacoin.git] / src / qt / addresstablemodel.cpp
index e375ff8..125ceeb 100644 (file)
@@ -1,5 +1,6 @@
 #include "addresstablemodel.h"
 #include "guiutil.h"
+#include "walletmodel.h"
 
 #include "headers.h"
 
@@ -38,7 +39,7 @@ struct AddressTablePriv
     {
         cachedAddressTable.clear();
 
-        CRITICAL_BLOCK(wallet->cs_mapKeys)
+        CRITICAL_BLOCK(cs_mapPubKeys)
         CRITICAL_BLOCK(wallet->cs_mapAddressBook)
         {
             BOOST_FOREACH(const PAIRTYPE(std::string, std::string)& item, wallet->mapAddressBook)
@@ -72,8 +73,8 @@ struct AddressTablePriv
     }
 };
 
-AddressTableModel::AddressTableModel(CWallet *wallet, QObject *parent) :
-    QAbstractTableModel(parent),wallet(wallet),priv(0)
+AddressTableModel::AddressTableModel(CWallet *wallet, WalletModel *parent) :
+    QAbstractTableModel(parent),walletModel(parent),wallet(wallet),priv(0)
 {
     columns << tr("Label") << tr("Address");
     priv = new AddressTablePriv(wallet);
@@ -150,6 +151,8 @@ bool AddressTableModel::setData(const QModelIndex & index, const QVariant & valu
         return false;
     AddressTableEntry *rec = static_cast<AddressTableEntry*>(index.internalPointer());
 
+    editStatus = OK;
+
     if(role == Qt::EditRole)
     {
         switch(index.column())
@@ -160,15 +163,21 @@ bool AddressTableModel::setData(const QModelIndex & index, const QVariant & valu
             break;
         case Address:
             // Refuse to set invalid address
-            if(!validateAddress(value.toString()))
+            if(!walletModel->validateAddress(value.toString()))
+            {
+                editStatus = INVALID_ADDRESS;
                 return false;
+            }
             // Double-check that we're not overwriting receiving address
             if(rec->type == AddressTableEntry::Sending)
             {
-                // Remove old entry
-                wallet->EraseAddressBookName(rec->address.toStdString());
-                // Add new entry with new address
-                wallet->SetAddressBookName(value.toString().toStdString(), rec->label.toStdString());
+                CRITICAL_BLOCK(wallet->cs_mapAddressBook)
+                {
+                    // Remove old entry
+                    wallet->DelAddressBookName(rec->address.toStdString());
+                    // Add new entry with new address
+                    wallet->SetAddressBookName(value.toString().toStdString(), rec->label.toStdString());
+                }
 
                 rec->address = value.toString();
             }
@@ -237,13 +246,22 @@ QString AddressTableModel::addRow(const QString &type, const QString &label, con
     std::string strLabel = label.toStdString();
     std::string strAddress = address.toStdString();
 
+    editStatus = OK;
+
+
     if(type == Send)
     {
+        if(!walletModel->validateAddress(address))
+        {
+            editStatus = INVALID_ADDRESS;
+            return QString();
+        }
         // Check for duplicate
         CRITICAL_BLOCK(wallet->cs_mapAddressBook)
         {
             if(wallet->mapAddressBook.count(strAddress))
             {
+                editStatus = DUPLICATE_ADDRESS;
                 return QString();
             }
         }
@@ -252,14 +270,15 @@ QString AddressTableModel::addRow(const QString &type, const QString &label, con
     {
         // Generate a new address to associate with given label, optionally
         // set as default receiving address.
-        strAddress = PubKeyToAddress(wallet->GetKeyFromKeyPool());
+        strAddress = PubKeyToAddress(wallet->GetOrReuseKeyFromPool());
     }
     else
     {
         return QString();
     }
     // Add entry and update list
-    wallet->SetAddressBookName(strAddress, strLabel);
+    CRITICAL_BLOCK(wallet->cs_mapAddressBook)
+        wallet->SetAddressBookName(strAddress, strLabel);
     updateList();
     return QString::fromStdString(strAddress);
 }
@@ -274,7 +293,10 @@ bool AddressTableModel::removeRows(int row, int count, const QModelIndex & paren
         // Also refuse to remove receiving addresses.
         return false;
     }
-    wallet->EraseAddressBookName(rec->address.toStdString());
+    CRITICAL_BLOCK(wallet->cs_mapAddressBook)
+    {
+        wallet->DelAddressBookName(rec->address.toStdString());
+    }
     updateList();
     return true;
 }
@@ -284,9 +306,32 @@ void AddressTableModel::update()
 
 }
 
-bool AddressTableModel::validateAddress(const QString &address)
+/* Look up label for address in address book, if not found return empty string.
+ */
+QString AddressTableModel::labelForAddress(const QString &address) const
 {
-    uint160 hash160 = 0;
+    CRITICAL_BLOCK(wallet->cs_mapAddressBook)
+    {
+        std::map<std::string, std::string>::iterator mi = wallet->mapAddressBook.find(address.toStdString());
+        if (mi != wallet->mapAddressBook.end())
+        {
+            return QString::fromStdString(mi->second);
+        }
+    }
+    return QString();
+}
 
-    return AddressToHash160(address.toStdString(), hash160);
+int AddressTableModel::lookupAddress(const QString &address) const
+{
+    QModelIndexList lst = match(index(0, Address, QModelIndex()),
+                                Qt::EditRole, address, 1, Qt::MatchExactly);
+    if(lst.isEmpty())
+    {
+        return -1;
+    }
+    else
+    {
+        return lst.at(0).row();
+    }
 }
+