Bugfix: Replace "URL" with "URI" where we aren't actually working with URLs
[novacoin.git] / src / qt / addresstablemodel.cpp
index 91b87fb..8fd6d52 100644 (file)
@@ -1,8 +1,11 @@
 #include "addresstablemodel.h"
 #include "guiutil.h"
-#include "main.h"
+#include "walletmodel.h"
+
+#include "headers.h"
 
 #include <QFont>
+#include <QColor>
 
 const QString AddressTableModel::Send = "S";
 const QString AddressTableModel::Receive = "R";
@@ -23,27 +26,29 @@ struct AddressTableEntry
         type(type), label(label), address(address) {}
 };
 
-/* Private implementation */
+// Private implementation
 struct AddressTablePriv
 {
+    CWallet *wallet;
     QList<AddressTableEntry> cachedAddressTable;
 
+    AddressTablePriv(CWallet *wallet):
+            wallet(wallet) {}
+
     void refreshAddressTable()
     {
         cachedAddressTable.clear();
 
-        CRITICAL_BLOCK(cs_mapKeys)
-        CRITICAL_BLOCK(cs_mapAddressBook)
+        CRITICAL_BLOCK(wallet->cs_wallet)
         {
-            BOOST_FOREACH(const PAIRTYPE(std::string, std::string)& item, 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())));
             }
         }
     }
@@ -66,11 +71,11 @@ struct AddressTablePriv
     }
 };
 
-AddressTableModel::AddressTableModel(QObject *parent) :
-    QAbstractTableModel(parent),priv(0)
+AddressTableModel::AddressTableModel(CWallet *wallet, WalletModel *parent) :
+    QAbstractTableModel(parent),walletModel(parent),wallet(wallet),priv(0)
 {
     columns << tr("Label") << tr("Address");
-    priv = new AddressTablePriv();
+    priv = new AddressTablePriv(wallet);
     priv->refreshAddressTable();
 }
 
@@ -103,17 +108,26 @@ QVariant AddressTableModel::data(const QModelIndex &index, int role) const
         switch(index.column())
         {
         case Label:
-            return rec->label;
+            if(rec->label.isEmpty() && role == Qt::DisplayRole)
+            {
+                return tr("(no label)");
+            }
+            else
+            {
+                return rec->label;
+            }
         case Address:
             return rec->address;
         }
     }
     else if (role == Qt::FontRole)
     {
+        QFont font;
         if(index.column() == Address)
         {
-            return GUIUtil::bitcoinAddressFont();
+            font = GUIUtil::bitcoinAddressFont();
         }
+        return font;
     }
     else if (role == TypeRole)
     {
@@ -135,22 +149,33 @@ 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())
         {
         case Label:
-            SetAddressBookName(rec->address.toStdString(), value.toString().toStdString());
+            wallet->SetAddressBookName(rec->address.toStdString(), value.toString().toStdString());
             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 */
-                CWalletDB().EraseName(rec->address.toStdString());
-                /* Add new entry with new address */
-                SetAddressBookName(value.toString().toStdString(), rec->label.toStdString());
+                CRITICAL_BLOCK(wallet->cs_wallet)
+                {
+                    // 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();
             }
@@ -175,6 +200,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);
@@ -191,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();
@@ -202,28 +244,50 @@ 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)
     {
-        /* Check for duplicate */
-        CRITICAL_BLOCK(cs_mapAddressBook)
+        if(!walletModel->validateAddress(address))
         {
-            if(mapAddressBook.count(strAddress))
+            editStatus = INVALID_ADDRESS;
+            return QString();
+        }
+        // Check for duplicate addresses
+        CRITICAL_BLOCK(wallet->cs_wallet)
+        {
+            if(wallet->mapAddressBook.count(strAddress))
             {
+                editStatus = DUPLICATE_ADDRESS;
                 return QString();
             }
         }
     }
     else if(type == Receive)
     {
-        /* Generate a new address to associate with given label */
-        strAddress = PubKeyToAddress(GetKeyFromKeyPool());
+        // 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 */
-    SetAddressBookName(strAddress, strLabel);
+    // Add entry and update list
+    CRITICAL_BLOCK(wallet->cs_wallet)
+        wallet->SetAddressBookName(strAddress, strLabel);
     updateList();
     return QString::fromStdString(strAddress);
 }
@@ -234,12 +298,50 @@ bool AddressTableModel::removeRows(int row, int count, const QModelIndex & paren
     AddressTableEntry *rec = priv->index(row);
     if(count != 1 || !rec || rec->type == AddressTableEntry::Receiving)
     {
-        /* Can only remove one row at a time, and cannot remove rows not in model.
-           Also refuse to remove receiving addresses.
-         */
+        // Can only remove one row at a time, and cannot remove rows not in model.
+        // Also refuse to remove receiving addresses.
         return false;
     }
-    CWalletDB().EraseName(rec->address.toStdString());
+    CRITICAL_BLOCK(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_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);
+        }
+    }
+    return QString();
+}
+
+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();
+    }
+}
+