Add context menu on transaction list: copy label, copy address, edit label, show...
[novacoin.git] / src / qt / addresstablemodel.cpp
index 1cd82b7..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,11 +109,16 @@ 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;
-        case IsDefaultAddress:
-            return rec->isDefaultAddress();
         }
     }
     else if (role == Qt::FontRole)
@@ -128,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)
@@ -174,27 +155,27 @@ 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();
             }
             break;
-        case IsDefaultAddress:
-            if(value.toBool())
-            {
-                setDefaultAddress(rec->address);
-            }
-            break;
         }
         emit dataChanged(index, index);
 
@@ -215,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,7 +235,7 @@ void AddressTableModel::updateList()
     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();
@@ -245,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();
             }
@@ -257,18 +255,14 @@ QString AddressTableModel::addRow(const QString &type, const QString &label, con
     {
         // Generate a new address to associate with given label, optionally
         // set as default receiving address.
-        strAddress = PubKeyToAddress(GetKeyFromKeyPool());
-        if(setAsDefault)
-        {
-            setDefaultAddress(QString::fromStdString(strAddress));
-        }
+        strAddress = PubKeyToAddress(wallet->GetKeyFromKeyPool());
     }
     else
     {
         return QString();
     }
     // Add entry and update list
-    SetAddressBookName(strAddress, strLabel);
+    wallet->SetAddressBookName(strAddress, strLabel);
     updateList();
     return QString::fromStdString(strAddress);
 }
@@ -283,36 +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;
 }
 
-QString AddressTableModel::getDefaultAddress() const
+void AddressTableModel::update()
 {
-    std::vector<unsigned char> vchPubKey;
-    if (CWalletDB("r").ReadDefaultKey(vchPubKey))
-    {
-        return QString::fromStdString(PubKeyToAddress(vchPubKey));
-    }
-    else
-    {
-        return QString();
-    }
+
 }
 
-void AddressTableModel::setDefaultAddress(const QString &defaultAddress)
+bool AddressTableModel::validateAddress(const QString &address)
 {
-    uint160 hash160;
-    std::string strAddress = defaultAddress.toStdString();
-    if (!AddressToHash160(strAddress, hash160))
-        return;
-    if (!mapPubKeys.count(hash160))
-        return;
-    CWalletDB().WriteDefaultKey(mapPubKeys[hash160]);
+    uint160 hash160 = 0;
+
+    return AddressToHash160(address.toStdString(), hash160);
 }
 
-void AddressTableModel::update()
+/* Look up label for address in address book, if not found return empty string.
+ */
+QString AddressTableModel::labelForAddress(const QString &address) const
 {
-    emit defaultAddressChanged(getDefaultAddress());
+    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();
+    }
+}
+