Remove headers.h
[novacoin.git] / src / qt / addresstablemodel.cpp
index 125ceeb..7b95f51 100644 (file)
@@ -2,7 +2,7 @@
 #include "guiutil.h"
 #include "walletmodel.h"
 
-#include "headers.h"
+#include "wallet.h"
 
 #include <QFont>
 #include <QColor>
@@ -27,8 +27,9 @@ struct AddressTableEntry
 };
 
 // Private implementation
-struct AddressTablePriv
+class AddressTablePriv
 {
+public:
     CWallet *wallet;
     QList<AddressTableEntry> cachedAddressTable;
 
@@ -39,18 +40,16 @@ struct AddressTablePriv
     {
         cachedAddressTable.clear();
 
-        CRITICAL_BLOCK(cs_mapPubKeys)
-        CRITICAL_BLOCK(wallet->cs_mapAddressBook)
         {
-            BOOST_FOREACH(const PAIRTYPE(std::string, std::string)& item, wallet->mapAddressBook)
+            LOCK(wallet->cs_wallet);
+            BOOST_FOREACH(const PAIRTYPE(CBitcoinAddress, std::string)& item, wallet->mapAddressBook)
             {
-                std::string strAddress = item.first;
-                std::string strName = item.second;
-                uint160 hash160;
-                bool fMine = (AddressToHash160(strAddress, hash160) && mapPubKeys.count(hash160));
+                const CBitcoinAddress& address = item.first;
+                const std::string& strName = item.second;
+                bool fMine = wallet->HaveKey(address);
                 cachedAddressTable.append(AddressTableEntry(fMine ? AddressTableEntry::Receiving : AddressTableEntry::Sending,
                                   QString::fromStdString(strName),
-                                  QString::fromStdString(strAddress)));
+                                  QString::fromStdString(address.ToString())));
             }
         }
     }
@@ -162,17 +161,17 @@ bool AddressTableModel::setData(const QModelIndex & index, const QVariant & valu
             rec->label = value.toString();
             break;
         case Address:
-            // Refuse to set invalid address
+            // Refuse to set invalid address, set error status and return false
             if(!walletModel->validateAddress(value.toString()))
             {
                 editStatus = INVALID_ADDRESS;
                 return false;
             }
-            // Double-check that we're not overwriting receiving address
+            // Double-check that we're not overwriting a receiving address
             if(rec->type == AddressTableEntry::Sending)
             {
-                CRITICAL_BLOCK(wallet->cs_mapAddressBook)
                 {
+                    LOCK(wallet->cs_wallet);
                     // Remove old entry
                     wallet->DelAddressBookName(rec->address.toStdString());
                     // Add new entry with new address
@@ -233,9 +232,9 @@ QModelIndex AddressTableModel::index(int row, int column, const QModelIndex & pa
     }
 }
 
-void AddressTableModel::updateList()
+void AddressTableModel::update()
 {
-    // Update internal model from Bitcoin core
+    // Update address book model from Bitcoin core
     beginResetModel();
     priv->refreshAddressTable();
     endResetModel();
@@ -248,7 +247,6 @@ QString AddressTableModel::addRow(const QString &type, const QString &label, con
 
     editStatus = OK;
 
-
     if(type == Send)
     {
         if(!walletModel->validateAddress(address))
@@ -256,9 +254,9 @@ QString AddressTableModel::addRow(const QString &type, const QString &label, con
             editStatus = INVALID_ADDRESS;
             return QString();
         }
-        // Check for duplicate
-        CRITICAL_BLOCK(wallet->cs_mapAddressBook)
+        // Check for duplicate addresses
         {
+            LOCK(wallet->cs_wallet);
             if(wallet->mapAddressBook.count(strAddress))
             {
                 editStatus = DUPLICATE_ADDRESS;
@@ -268,18 +266,31 @@ QString AddressTableModel::addRow(const QString &type, const QString &label, con
     }
     else if(type == Receive)
     {
-        // Generate a new address to associate with given label, optionally
-        // set as default receiving address.
-        strAddress = PubKeyToAddress(wallet->GetOrReuseKeyFromPool());
+        // Generate a new address to associate with given label
+        WalletModel::UnlockContext ctx(walletModel->requestUnlock());
+        if(!ctx.isValid())
+        {
+            // Unlock wallet failed or was cancelled
+            editStatus = WALLET_UNLOCK_FAILURE;
+            return QString();
+        }
+        std::vector<unsigned char> newKey;
+        if(!wallet->GetKeyFromPool(newKey, true))
+        {
+            editStatus = KEY_GENERATION_FAILURE;
+            return QString();
+        }
+        strAddress = CBitcoinAddress(newKey).ToString();
     }
     else
     {
         return QString();
     }
-    // Add entry and update list
-    CRITICAL_BLOCK(wallet->cs_mapAddressBook)
+    // Add entry
+    {
+        LOCK(wallet->cs_wallet);
         wallet->SetAddressBookName(strAddress, strLabel);
-    updateList();
+    }
     return QString::fromStdString(strAddress);
 }
 
@@ -293,26 +304,21 @@ bool AddressTableModel::removeRows(int row, int count, const QModelIndex & paren
         // Also refuse to remove receiving addresses.
         return false;
     }
-    CRITICAL_BLOCK(wallet->cs_mapAddressBook)
     {
+        LOCK(wallet->cs_wallet);
         wallet->DelAddressBookName(rec->address.toStdString());
     }
-    updateList();
     return true;
 }
 
-void AddressTableModel::update()
-{
-
-}
-
 /* Look up label for address in address book, if not found return empty string.
  */
 QString AddressTableModel::labelForAddress(const QString &address) const
 {
-    CRITICAL_BLOCK(wallet->cs_mapAddressBook)
     {
-        std::map<std::string, std::string>::iterator mi = wallet->mapAddressBook.find(address.toStdString());
+        LOCK(wallet->cs_wallet);
+        CBitcoinAddress address_parsed(address.toStdString());
+        std::map<CBitcoinAddress, std::string>::iterator mi = wallet->mapAddressBook.find(address_parsed);
         if (mi != wallet->mapAddressBook.end())
         {
             return QString::fromStdString(mi->second);