update core to d0d80170a2ca73004e08fb85007fe055cbf4e411 (CWallet class)
[novacoin.git] / src / qt / addresstablemodel.cpp
index 91b87fb..eece092 100644 (file)
@@ -1,8 +1,10 @@
 #include "addresstablemodel.h"
 #include "guiutil.h"
-#include "main.h"
+
+#include "headers.h"
 
 #include <QFont>
+#include <QColor>
 
 const QString AddressTableModel::Send = "S";
 const QString AddressTableModel::Receive = "R";
@@ -23,19 +25,23 @@ 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_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;
@@ -64,13 +70,18 @@ struct AddressTablePriv
             return 0;
         }
     }
+
+    bool isDefaultAddress(const AddressTableEntry *rec)
+    {
+        return rec->address == QString::fromStdString(wallet->GetDefaultAddress());
+    }
 };
 
-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();
 }
 
@@ -106,13 +117,36 @@ QVariant AddressTableModel::data(const QModelIndex &index, int role) const
             return rec->label;
         case Address:
             return rec->address;
+        case IsDefaultAddress:
+            return priv->isDefaultAddress(rec);
         }
     }
     else if (role == Qt::FontRole)
     {
+        QFont font;
         if(index.column() == Address)
         {
-            return GUIUtil::bitcoinAddressFont();
+            font = GUIUtil::bitcoinAddressFont();
+        }
+        if(priv->isDefaultAddress(rec))
+        {
+            font.setBold(true);
+        }
+        return font;
+    }
+    else if (role == Qt::ForegroundRole)
+    {
+        // Show default address in alternative color
+        if(priv->isDefaultAddress(rec))
+        {
+            return QColor(0,0,255);
+        }
+    }
+    else if (role == Qt::ToolTipRole)
+    {
+        if(priv->isDefaultAddress(rec))
+        {
+            return tr("Default receiving address");
         }
     }
     else if (role == TypeRole)
@@ -140,21 +174,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:
-            /* Double-check that we're not overwriting receiving address */
+            // 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());
+                // Remove old entry
+                wallet->EraseAddressBookName(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);
 
@@ -191,23 +231,23 @@ QModelIndex AddressTableModel::index(int row, int column, const QModelIndex & pa
 
 void AddressTableModel::updateList()
 {
-    /* Update internal model from Bitcoin core */
+    // Update internal model from Bitcoin core
     beginResetModel();
     priv->refreshAddressTable();
     endResetModel();
 }
 
-QString AddressTableModel::addRow(const QString &type, const QString &label, const QString &address)
+QString AddressTableModel::addRow(const QString &type, const QString &label, const QString &address, bool setAsDefault)
 {
     std::string strLabel = label.toStdString();
     std::string strAddress = address.toStdString();
 
     if(type == Send)
     {
-        /* Check for duplicate */
-        CRITICAL_BLOCK(cs_mapAddressBook)
+        // Check for duplicate
+        CRITICAL_BLOCK(wallet->cs_mapAddressBook)
         {
-            if(mapAddressBook.count(strAddress))
+            if(wallet->mapAddressBook.count(strAddress))
             {
                 return QString();
             }
@@ -215,15 +255,20 @@ 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());
+        if(setAsDefault)
+        {
+            setDefaultAddress(QString::fromStdString(strAddress));
+        }
     }
     else
     {
         return QString();
     }
-    /* Add entry and update list */
-    SetAddressBookName(strAddress, strLabel);
+    // Add entry and update list
+    wallet->SetAddressBookName(strAddress, strLabel);
     updateList();
     return QString::fromStdString(strAddress);
 }
@@ -234,12 +279,26 @@ 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());
+    wallet->EraseAddressBookName(rec->address.toStdString());
     updateList();
     return true;
 }
+
+QString AddressTableModel::getDefaultAddress() const
+{
+    return QString::fromStdString(wallet->GetDefaultAddress());
+}
+
+void AddressTableModel::setDefaultAddress(const QString &defaultAddress)
+{
+    wallet->SetDefaultAddress(defaultAddress.toStdString());
+}
+
+void AddressTableModel::update()
+{
+    emit defaultAddressChanged(getDefaultAddress());
+}