Wallet encryption part 2: ask passphrase when needed, add menu options
[novacoin.git] / src / qt / addresstablemodel.cpp
index 2550076..6bda1e7 100644 (file)
@@ -1,5 +1,6 @@
 #include "addresstablemodel.h"
 #include "guiutil.h"
+#include "walletmodel.h"
 
 #include "headers.h"
 
@@ -38,18 +39,17 @@ struct AddressTablePriv
     {
         cachedAddressTable.clear();
 
-        CRITICAL_BLOCK(wallet->cs_mapKeys)
+        CRITICAL_BLOCK(wallet->cs_KeyStore)
         CRITICAL_BLOCK(wallet->cs_mapAddressBook)
         {
-            BOOST_FOREACH(const PAIRTYPE(std::string, std::string)& item, wallet->mapAddressBook)
+            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())));
             }
         }
     }
@@ -70,15 +70,10 @@ struct AddressTablePriv
             return 0;
         }
     }
-
-    bool isDefaultAddress(const AddressTableEntry *rec)
-    {
-        return rec->address == QString::fromStdString(wallet->GetDefaultAddress());
-    }
 };
 
-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);
@@ -114,7 +109,7 @@ QVariant AddressTableModel::data(const QModelIndex &index, int role) const
         switch(index.column())
         {
         case Label:
-            if(rec->label.isEmpty())
+            if(rec->label.isEmpty() && role == Qt::DisplayRole)
             {
                 return tr("(no label)");
             }
@@ -124,8 +119,6 @@ QVariant AddressTableModel::data(const QModelIndex &index, int role) const
             }
         case Address:
             return rec->address;
-        case IsDefaultAddress:
-            return priv->isDefaultAddress(rec);
         }
     }
     else if (role == Qt::FontRole)
@@ -135,27 +128,8 @@ QVariant AddressTableModel::data(const QModelIndex &index, int role) const
         {
             font = GUIUtil::bitcoinAddressFont();
         }
-        if(priv->isDefaultAddress(rec))
-        {
-            font.setBold(true);
-        }
         return font;
     }
-    else if (role == Qt::BackgroundRole)
-    {
-        // Show default address in alternative color
-        if(priv->isDefaultAddress(rec))
-        {
-            return QColor(255,255,128);
-        }
-    }
-    else if (role == Qt::ToolTipRole)
-    {
-        if(priv->isDefaultAddress(rec))
-        {
-            return tr("Default receiving address");
-        }
-    }
     else if (role == TypeRole)
     {
         switch(rec->type)
@@ -176,6 +150,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())
@@ -185,23 +161,26 @@ bool AddressTableModel::setData(const QModelIndex & index, const QVariant & valu
             rec->label = value.toString();
             break;
         case Address:
-            // Double-check that we're not overwriting receiving 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 a 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();
             }
             break;
-        case IsDefaultAddress:
-            if(value.toBool())
-            {
-                setDefaultAddress(rec->address);
-            }
-            break;
         }
         emit dataChanged(index, index);
 
@@ -222,6 +201,23 @@ QVariant AddressTableModel::headerData(int section, Qt::Orientation orientation,
     return QVariant();
 }
 
+Qt::ItemFlags AddressTableModel::flags(const QModelIndex & index) const
+{
+    if(!index.isValid())
+        return 0;
+    AddressTableEntry *rec = static_cast<AddressTableEntry*>(index.internalPointer());
+
+    Qt::ItemFlags retval = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
+    // Can edit address and label for sending addresses,
+    // and only label for receiving addresses.
+    if(rec->type == AddressTableEntry::Sending ||
+      (rec->type == AddressTableEntry::Receiving && index.column()==Label))
+    {
+        retval |= Qt::ItemIsEditable;
+    }
+    return retval;
+}
+
 QModelIndex AddressTableModel::index(int row, int column, const QModelIndex & parent) const
 {
     Q_UNUSED(parent);
@@ -238,44 +234,56 @@ 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();
 }
 
-QString AddressTableModel::addRow(const QString &type, const QString &label, const QString &address, bool setAsDefault)
+QString AddressTableModel::addRow(const QString &type, const QString &label, const QString &address)
 {
     std::string strLabel = label.toStdString();
     std::string strAddress = address.toStdString();
 
+    editStatus = OK;
+
     if(type == Send)
     {
-        // Check for duplicate
+        if(!walletModel->validateAddress(address))
+        {
+            editStatus = INVALID_ADDRESS;
+            return QString();
+        }
+        // Check for duplicate addresses
         CRITICAL_BLOCK(wallet->cs_mapAddressBook)
         {
             if(wallet->mapAddressBook.count(strAddress))
             {
+                editStatus = DUPLICATE_ADDRESS;
                 return QString();
             }
         }
     }
     else if(type == Receive)
     {
-        // Generate a new address to associate with given label, optionally
-        // set as default receiving address.
-        strAddress = PubKeyToAddress(wallet->GetKeyFromKeyPool());
-        if(setAsDefault)
+        // Generate a new address to associate with given label
+        WalletModel::UnlockContext ctx(walletModel->requestUnlock());
+        if(!ctx.isValid())
         {
-            setDefaultAddress(QString::fromStdString(strAddress));
+            // Unlock wallet failed or was cancelled
+            editStatus = WALLET_UNLOCK_FAILURE;
+            return QString();
         }
+
+        strAddress = CBitcoinAddress(wallet->GetOrReuseKeyFromPool()).ToString();
     }
     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);
 }
@@ -290,22 +298,46 @@ 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;
 }
 
-QString AddressTableModel::getDefaultAddress() const
+void AddressTableModel::update()
 {
-    return QString::fromStdString(wallet->GetDefaultAddress());
+
 }
 
-void AddressTableModel::setDefaultAddress(const QString &defaultAddress)
+/* Look up label for address in address book, if not found return empty string.
+ */
+QString AddressTableModel::labelForAddress(const QString &address) const
 {
-    wallet->SetDefaultAddress(defaultAddress.toStdString());
+    CRITICAL_BLOCK(wallet->cs_mapAddressBook)
+    {
+        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);
+        }
+    }
+    return QString();
 }
 
-void AddressTableModel::update()
+int AddressTableModel::lookupAddress(const QString &address) const
 {
-    emit defaultAddressChanged(getDefaultAddress());
+    QModelIndexList lst = match(index(0, Address, QModelIndex()),
+                                Qt::EditRole, address, 1, Qt::MatchExactly);
+    if(lst.isEmpty())
+    {
+        return -1;
+    }
+    else
+    {
+        return lst.at(0).row();
+    }
 }
+