Add context menu on transaction list: copy label, copy address, edit label, show...
[novacoin.git] / src / qt / addresstablemodel.cpp
index 0558bfa..9ca7542 100644 (file)
@@ -1,6 +1,7 @@
 #include "addresstablemodel.h"
 #include "guiutil.h"
-#include "main.h"
+
+#include "headers.h"
 
 #include <QFont>
 #include <QColor>
@@ -22,31 +23,25 @@ struct AddressTableEntry
     AddressTableEntry() {}
     AddressTableEntry(Type type, const QString &label, const QString &address):
         type(type), label(label), address(address) {}
-
-    bool isDefaultAddress() const
-    {
-        std::vector<unsigned char> vchPubKey;
-        if (CWalletDB("r").ReadDefaultKey(vchPubKey))
-        {
-            return address == QString::fromStdString(PubKeyToAddress(vchPubKey));
-        }
-        return false;
-    }
 };
 
 // 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_mapKeys)
+        CRITICAL_BLOCK(wallet->cs_mapAddressBook)
         {
-            BOOST_FOREACH(const PAIRTYPE(std::string, std::string)& item, mapAddressBook)
+            BOOST_FOREACH(const PAIRTYPE(std::string, std::string)& item, wallet->mapAddressBook)
             {
                 std::string strAddress = item.first;
                 std::string strName = item.second;
@@ -77,11 +72,11 @@ struct AddressTablePriv
     }
 };
 
-AddressTableModel::AddressTableModel(QObject *parent) :
-    QAbstractTableModel(parent),priv(0)
+AddressTableModel::AddressTableModel(CWallet *wallet, QObject *parent) :
+    QAbstractTableModel(parent),wallet(wallet),priv(0)
 {
     columns << tr("Label") << tr("Address");
-    priv = new AddressTablePriv();
+    priv = new AddressTablePriv(wallet);
     priv->refreshAddressTable();
 }
 
@@ -114,7 +109,14 @@ 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;
         }
@@ -126,27 +128,8 @@ QVariant AddressTableModel::data(const QModelIndex &index, int role) const
         {
             font = GUIUtil::bitcoinAddressFont();
         }
-        if(rec->isDefaultAddress())
-        {
-            font.setBold(true);
-        }
         return font;
     }
-    else if (role == Qt::ForegroundRole)
-    {
-        // Show default address in alternative color
-        if(rec->isDefaultAddress())
-        {
-            return QColor(0,0,255);
-        }
-    }
-    else if (role == Qt::ToolTipRole)
-    {
-        if(rec->isDefaultAddress())
-        {
-            return tr("Default receiving address");
-        }
-    }
     else if (role == TypeRole)
     {
         switch(rec->type)
@@ -172,17 +155,23 @@ bool AddressTableModel::setData(const QModelIndex & index, const QVariant & valu
         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:
+            // Refuse to set invalid address
+            if(!validateAddress(value.toString()))
+                return false;
             // Double-check that we're not overwriting 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_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();
             }
@@ -207,6 +196,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);
@@ -237,9 +243,9 @@ QString AddressTableModel::addRow(const QString &type, const QString &label, con
     if(type == Send)
     {
         // Check for duplicate
-        CRITICAL_BLOCK(cs_mapAddressBook)
+        CRITICAL_BLOCK(wallet->cs_mapAddressBook)
         {
-            if(mapAddressBook.count(strAddress))
+            if(wallet->mapAddressBook.count(strAddress))
             {
                 return QString();
             }
@@ -247,15 +253,16 @@ QString AddressTableModel::addRow(const QString &type, const QString &label, con
     }
     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, optionally
+        // set as default receiving address.
+        strAddress = PubKeyToAddress(wallet->GetKeyFromKeyPool());
     }
     else
     {
         return QString();
     }
     // Add entry and update list
-    SetAddressBookName(strAddress, strLabel);
+    wallet->SetAddressBookName(strAddress, strLabel);
     updateList();
     return QString::fromStdString(strAddress);
 }
@@ -270,7 +277,52 @@ bool AddressTableModel::removeRows(int row, int count, const QModelIndex & paren
         // Also refuse to remove receiving addresses.
         return false;
     }
-    CWalletDB().EraseName(rec->address.toStdString());
+    CRITICAL_BLOCK(wallet->cs_mapAddressBook)
+    {
+        wallet->DelAddressBookName(rec->address.toStdString());
+    }
     updateList();
     return true;
 }
+
+void AddressTableModel::update()
+{
+
+}
+
+bool AddressTableModel::validateAddress(const QString &address)
+{
+    uint160 hash160 = 0;
+
+    return AddressToHash160(address.toStdString(), hash160);
+}
+
+/* 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());
+        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();
+    }
+}
+