Fix Minimize to the tray instead of the taskbar
[novacoin.git] / src / qt / addresstablemodel.cpp
index c8329bb..8fd6d52 100644 (file)
@@ -39,8 +39,7 @@ struct AddressTablePriv
     {
         cachedAddressTable.clear();
 
-        CRITICAL_BLOCK(wallet->cs_KeyStore)
-        CRITICAL_BLOCK(wallet->cs_mapAddressBook)
+        CRITICAL_BLOCK(wallet->cs_wallet)
         {
             BOOST_FOREACH(const PAIRTYPE(CBitcoinAddress, std::string)& item, wallet->mapAddressBook)
             {
@@ -161,16 +160,16 @@ 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)
+                CRITICAL_BLOCK(wallet->cs_wallet)
                 {
                     // Remove old entry
                     wallet->DelAddressBookName(rec->address.toStdString());
@@ -234,7 +233,7 @@ QModelIndex AddressTableModel::index(int row, int column, const QModelIndex & pa
 
 void AddressTableModel::updateList()
 {
-    // Update internal model from Bitcoin core
+    // Update address book model from Bitcoin core
     beginResetModel();
     priv->refreshAddressTable();
     endResetModel();
@@ -247,7 +246,6 @@ QString AddressTableModel::addRow(const QString &type, const QString &label, con
 
     editStatus = OK;
 
-
     if(type == Send)
     {
         if(!walletModel->validateAddress(address))
@@ -255,8 +253,8 @@ 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
+        CRITICAL_BLOCK(wallet->cs_wallet)
         {
             if(wallet->mapAddressBook.count(strAddress))
             {
@@ -268,14 +266,27 @@ QString AddressTableModel::addRow(const QString &type, const QString &label, con
     else if(type == Receive)
     {
         // Generate a new address to associate with given label
-        strAddress = CBitcoinAddress(wallet->GetOrReuseKeyFromPool()).ToString();
+        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)
+    CRITICAL_BLOCK(wallet->cs_wallet)
         wallet->SetAddressBookName(strAddress, strLabel);
     updateList();
     return QString::fromStdString(strAddress);
@@ -291,7 +302,7 @@ bool AddressTableModel::removeRows(int row, int count, const QModelIndex & paren
         // Also refuse to remove receiving addresses.
         return false;
     }
-    CRITICAL_BLOCK(wallet->cs_mapAddressBook)
+    CRITICAL_BLOCK(wallet->cs_wallet)
     {
         wallet->DelAddressBookName(rec->address.toStdString());
     }
@@ -308,7 +319,7 @@ void AddressTableModel::update()
  */
 QString AddressTableModel::labelForAddress(const QString &address) const
 {
-    CRITICAL_BLOCK(wallet->cs_mapAddressBook)
+    CRITICAL_BLOCK(wallet->cs_wallet)
     {
         CBitcoinAddress address_parsed(address.toStdString());
         std::map<CBitcoinAddress, std::string>::iterator mi = wallet->mapAddressBook.find(address_parsed);