You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

3518 lines
103 KiB

// Copyright (c) 2009 Satoshi Nakamoto
// Distributed under the MIT/X11 software license, see the accompanying
// file license.txt or http://www.opensource.org/licenses/mit-license.php.
#include "headers.h"
#ifdef _MSC_VER
#include <crtdbg.h>
#endif
DEFINE_EVENT_TYPE(wxEVT_CROSSTHREADCALL)
DEFINE_EVENT_TYPE(wxEVT_REPLY1)
DEFINE_EVENT_TYPE(wxEVT_REPLY2)
DEFINE_EVENT_TYPE(wxEVT_REPLY3)
DEFINE_EVENT_TYPE(wxEVT_TABLEADDED)
DEFINE_EVENT_TYPE(wxEVT_TABLEUPDATED)
DEFINE_EVENT_TYPE(wxEVT_TABLEDELETED)
CMainFrame* pframeMain = NULL;
map<string, string> mapAddressBook;
CBitcoinTBIcon* taskBarIcon = NULL; // Tray icon
void ThreadRequestProductDetails(void* parg);
void ThreadRandSendTest(void* parg);
bool fRandSendTest = false;
void RandSend();
extern int g_isPainting;
// UI settings and their default values
int minimizeToTray = 1;
int closeToTray = 1;
int startOnSysBoot = 1;
int askBeforeClosing = 1;
int alwaysShowTrayIcon = 1;
//////////////////////////////////////////////////////////////////////////////
//
// Util
//
void HandleCtrlA(wxKeyEvent& event)
{
// Ctrl-a select all
wxTextCtrl* textCtrl = (wxTextCtrl*)event.GetEventObject();
if (event.GetModifiers() == wxMOD_CONTROL && event.GetKeyCode() == 'A')
textCtrl->SetSelection(-1, -1);
event.Skip();
}
bool Is24HourTime()
{
//char pszHourFormat[256];
//pszHourFormat[0] = '\0';
//GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_ITIME, pszHourFormat, 256);
//return (pszHourFormat[0] != '0');
return true;
}
string DateStr(int64 nTime)
{
return (string)wxDateTime((time_t)nTime).FormatDate();
}
string DateTimeStr(int64 nTime)
{
wxDateTime datetime((time_t)nTime);
if (Is24HourTime())
return (string)datetime.Format("%x %H:%M");
else
return (string)datetime.Format("%x ") + itostr((datetime.GetHour() + 11) % 12 + 1) + (string)datetime.Format(":%M %p");
}
wxString GetItemText(wxListCtrl* listCtrl, int nIndex, int nColumn)
{
// Helper to simplify access to listctrl
wxListItem item;
item.m_itemId = nIndex;
item.m_col = nColumn;
item.m_mask = wxLIST_MASK_TEXT;
if (!listCtrl->GetItem(item))
return "";
return item.GetText();
}
int InsertLine(wxListCtrl* listCtrl, const wxString& str0, const wxString& str1)
{
int nIndex = listCtrl->InsertItem(listCtrl->GetItemCount(), str0);
listCtrl->SetItem(nIndex, 1, str1);
return nIndex;
}
int InsertLine(wxListCtrl* listCtrl, const wxString& str0, const wxString& str1, const wxString& str2, const wxString& str3, const wxString& str4)
{
int nIndex = listCtrl->InsertItem(listCtrl->GetItemCount(), str0);
listCtrl->SetItem(nIndex, 1, str1);
listCtrl->SetItem(nIndex, 2, str2);
listCtrl->SetItem(nIndex, 3, str3);
listCtrl->SetItem(nIndex, 4, str4);
return nIndex;
}
int InsertLine(wxListCtrl* listCtrl, void* pdata, const wxString& str0, const wxString& str1, const wxString& str2, const wxString& str3, const wxString& str4)
{
int nIndex = listCtrl->InsertItem(listCtrl->GetItemCount(), str0);
listCtrl->SetItemPtrData(nIndex, (wxUIntPtr)pdata);
listCtrl->SetItem(nIndex, 1, str1);
listCtrl->SetItem(nIndex, 2, str2);
listCtrl->SetItem(nIndex, 3, str3);
listCtrl->SetItem(nIndex, 4, str4);
return nIndex;
}
void SetSelection(wxListCtrl* listCtrl, int nIndex)
{
int nSize = listCtrl->GetItemCount();
long nState = (wxLIST_STATE_SELECTED|wxLIST_STATE_FOCUSED);
for (int i = 0; i < nSize; i++)
listCtrl->SetItemState(i, (i == nIndex ? nState : 0), nState);
}
int GetSelection(wxListCtrl* listCtrl)
{
int nSize = listCtrl->GetItemCount();
for (int i = 0; i < nSize; i++)
if (listCtrl->GetItemState(i, wxLIST_STATE_FOCUSED))
return i;
return -1;
}
string HtmlEscape(const char* psz, bool fMultiLine=false)
{
int len = 0;
for (const char* p = psz; *p; p++)
{
if (*p == '<') len += 4;
else if (*p == '>') len += 4;
else if (*p == '&') len += 5;
else if (*p == '"') len += 6;
else if (*p == ' ' && p > psz && p[-1] == ' ' && p[1] == ' ') len += 6;
else if (*p == '\n' && fMultiLine) len += 5;
else
len++;
}
string str;
str.reserve(len);
for (const char* p = psz; *p; p++)
{
if (*p == '<') str += "&lt;";
else if (*p == '>') str += "&gt;";
else if (*p == '&') str += "&amp;";
else if (*p == '"') str += "&quot;";
else if (*p == ' ' && p > psz && p[-1] == ' ' && p[1] == ' ') str += "&nbsp;";
else if (*p == '\n' && fMultiLine) str += "<br>\n";
else
str += *p;
}
return str;
}
string HtmlEscape(const string& str, bool fMultiLine=false)
{
return HtmlEscape(str.c_str(), fMultiLine);
}
void AddToMyProducts(CProduct product)
{
CProduct& productInsert = mapMyProducts[product.GetHash()];
productInsert = product;
InsertLine(pframeMain->m_listCtrlProductsSent, &productInsert,
product.mapValue["category"],
product.mapValue["title"].substr(0, 100),
product.mapValue["description"].substr(0, 100),
product.mapValue["price"],
"");
}
//////////////////////////////////////////////////////////////////////////////
//
// Custom events
//
set<void*> setCallbackAvailable;
CCriticalSection cs_setCallbackAvailable;
void AddCallbackAvailable(void* p)
{
CRITICAL_BLOCK(cs_setCallbackAvailable)
setCallbackAvailable.insert(p);
}
void RemoveCallbackAvailable(void* p)
{
CRITICAL_BLOCK(cs_setCallbackAvailable)
setCallbackAvailable.erase(p);
}
bool IsCallbackAvailable(void* p)
{
CRITICAL_BLOCK(cs_setCallbackAvailable)
return setCallbackAvailable.count(p);
return false;
}
template<typename T>
void AddPendingCustomEvent(wxEvtHandler* pevthandler, int nEventID, const T pbeginIn, const T pendIn)
{
if (!pevthandler)
return;
const char* pbegin = (pendIn != pbeginIn) ? &pbeginIn[0] : NULL;
const char* pend = pbegin + (pendIn - pbeginIn) * sizeof(pbeginIn[0]);
wxCommandEvent event(nEventID);
wxString strData(wxChar(0), (pend - pbegin) / sizeof(wxChar) + 1);
memcpy(&strData[0], pbegin, pend - pbegin);
event.SetString(strData);
event.SetInt(pend - pbegin);
pevthandler->AddPendingEvent(event);
}
template<class T>
void AddPendingCustomEvent(wxEvtHandler* pevthandler, int nEventID, const T& obj)
{
CDataStream ss;
ss << obj;
AddPendingCustomEvent(pevthandler, nEventID, ss.begin(), ss.end());
}
void AddPendingReplyEvent1(void* pevthandler, CDataStream& vRecv)
{
if (IsCallbackAvailable(pevthandler))
AddPendingCustomEvent((wxEvtHandler*)pevthandler, wxEVT_REPLY1, vRecv.begin(), vRecv.end());
}
void AddPendingReplyEvent2(void* pevthandler, CDataStream& vRecv)
{
if (IsCallbackAvailable(pevthandler))
AddPendingCustomEvent((wxEvtHandler*)pevthandler, wxEVT_REPLY2, vRecv.begin(), vRecv.end());
}
void AddPendingReplyEvent3(void* pevthandler, CDataStream& vRecv)
{
if (IsCallbackAvailable(pevthandler))
AddPendingCustomEvent((wxEvtHandler*)pevthandler, wxEVT_REPLY3, vRecv.begin(), vRecv.end());
}
CDataStream GetStreamFromEvent(const wxCommandEvent& event)
{
wxString strData = event.GetString();
return CDataStream(strData.begin(), strData.begin() + event.GetInt(), SER_NETWORK);
}
//////////////////////////////////////////////////////////////////////////////
//
// CMainFrame
//
CMainFrame::CMainFrame(wxWindow* parent) : CMainFrameBase(parent)
{
Connect(wxEVT_CROSSTHREADCALL, wxCommandEventHandler(CMainFrame::OnCrossThreadCall), NULL, this);
// Init
fRefreshListCtrl = false;
fRefreshListCtrlRunning = false;
fOnSetFocusAddress = false;
pindexBestLast = NULL;
m_choiceFilter->SetSelection(0);
m_staticTextBalance->SetLabel(FormatMoney(GetBalance()) + " ");
m_listCtrl->SetFocus();
SetIcon(wxICON(bitcoin));
m_menuOptions->Check(wxID_OPTIONSGENERATEBITCOINS, fGenerateBitcoins);
// Init toolbar with transparency masked bitmaps
m_toolBar->ClearTools();
//// shouldn't have to do mask separately anymore, bitmap alpha support added in wx 2.8.9,
wxBitmap bmpSend(wxT("send20"), wxBITMAP_TYPE_RESOURCE);
bmpSend.SetMask(new wxMask(wxBitmap(wxT("send20mask"), wxBITMAP_TYPE_RESOURCE)));
m_toolBar->AddTool(wxID_BUTTONSEND, wxT("&Send Coins"), bmpSend, wxNullBitmap, wxITEM_NORMAL, wxEmptyString, wxEmptyString);
wxBitmap bmpAddressBook(wxT("addressbook20"), wxBITMAP_TYPE_RESOURCE);
bmpAddressBook.SetMask(new wxMask(wxBitmap(wxT("addressbook20mask"), wxBITMAP_TYPE_RESOURCE)));
m_toolBar->AddTool(wxID_BUTTONRECEIVE, wxT("&Address Book"), bmpAddressBook, wxNullBitmap, wxITEM_NORMAL, wxEmptyString, wxEmptyString);
m_toolBar->Realize();
// Init column headers
int nDateWidth = DateTimeStr(1229413914).size() * 6 + 8;
m_listCtrl->InsertColumn(0, "", wxLIST_FORMAT_LEFT, 0);
m_listCtrl->InsertColumn(1, "", wxLIST_FORMAT_LEFT, 0);
m_listCtrl->InsertColumn(2, "Status", wxLIST_FORMAT_LEFT, 90);
m_listCtrl->InsertColumn(3, "Date", wxLIST_FORMAT_LEFT, nDateWidth);
m_listCtrl->InsertColumn(4, "Description", wxLIST_FORMAT_LEFT, 409 - nDateWidth);
m_listCtrl->InsertColumn(5, "Debit", wxLIST_FORMAT_RIGHT, 79);
m_listCtrl->InsertColumn(6, "Credit", wxLIST_FORMAT_RIGHT, 79);
//m_listCtrlProductsSent->InsertColumn(0, "Category", wxLIST_FORMAT_LEFT, 100);
//m_listCtrlProductsSent->InsertColumn(1, "Title", wxLIST_FORMAT_LEFT, 100);
//m_listCtrlProductsSent->InsertColumn(2, "Description", wxLIST_FORMAT_LEFT, 100);
//m_listCtrlProductsSent->InsertColumn(3, "Price", wxLIST_FORMAT_LEFT, 100);
//m_listCtrlProductsSent->InsertColumn(4, "", wxLIST_FORMAT_LEFT, 100);
//m_listCtrlOrdersSent->InsertColumn(0, "Time", wxLIST_FORMAT_LEFT, 100);
//m_listCtrlOrdersSent->InsertColumn(1, "Price", wxLIST_FORMAT_LEFT, 100);
//m_listCtrlOrdersSent->InsertColumn(2, "", wxLIST_FORMAT_LEFT, 100);
//m_listCtrlOrdersSent->InsertColumn(3, "", wxLIST_FORMAT_LEFT, 100);
//m_listCtrlOrdersSent->InsertColumn(4, "", wxLIST_FORMAT_LEFT, 100);
//m_listCtrlOrdersReceived->InsertColumn(0, "Time", wxLIST_FORMAT_LEFT, 100);
//m_listCtrlOrdersReceived->InsertColumn(1, "Price", wxLIST_FORMAT_LEFT, 100);
//m_listCtrlOrdersReceived->InsertColumn(2, "Payment Status", wxLIST_FORMAT_LEFT, 100);
//m_listCtrlOrdersReceived->InsertColumn(3, "", wxLIST_FORMAT_LEFT, 100);
//m_listCtrlOrdersReceived->InsertColumn(4, "", wxLIST_FORMAT_LEFT, 100);
// Init status bar
int pnWidths[3] = { -100, 81, 286 };
m_statusBar->SetFieldsCount(3, pnWidths);
// Fill your address text box
vector<unsigned char> vchPubKey;
if (CWalletDB("r").ReadDefaultKey(vchPubKey))
m_textCtrlAddress->SetValue(PubKeyToAddress(vchPubKey));
// Fill listctrl with wallet transactions
RefreshListCtrl();
}
CMainFrame::~CMainFrame()
{
pframeMain = NULL;
}
void Shutdown(void* parg)
{
static CCriticalSection cs_Shutdown;
CRITICAL_BLOCK(cs_Shutdown)
{
fShutdown = true;
nTransactionsUpdated++;
DBFlush(false);
StopNode();
DBFlush(true);
printf("Bitcoin exiting\n");
exit(0);
}
}
void CMainFrame::OnClose(wxCloseEvent& event)
{
if (closeToTray && event.CanVeto()) {
event.Veto();
SendToTray();
}
else if (!event.CanVeto() || !askBeforeClosing || wxMessageBox("Quit program?", "Confirm", wxYES_NO, this) == wxYES) {
delete taskBarIcon;
Destroy();
_beginthread(Shutdown, 0, NULL);
}
}
void CMainFrame::OnIconize(wxIconizeEvent& event)
{
if (minimizeToTray) {
SendToTray();
}
}
void CMainFrame::SendToTray()
{
Hide();
taskBarIcon->Show();
}
void CMainFrame::OnMouseEvents(wxMouseEvent& event)
{
RandAddSeed();
RAND_add(&event.m_x, sizeof(event.m_x), 0.25);
RAND_add(&event.m_y, sizeof(event.m_y), 0.25);
}
void CMainFrame::OnListColBeginDrag(wxListEvent& event)
{
// Hidden columns not resizeable
if (event.GetColumn() <= 1 && !fDebug)
event.Veto();
}
void CMainFrame::InsertLine(bool fNew, int nIndex, uint256 hashKey, string strSort, const wxString& str2, const wxString& str3, const wxString& str4, const wxString& str5, const wxString& str6)
{
string str0 = strSort;
long nData = *(long*)&hashKey;
if (fNew)
{
nIndex = m_listCtrl->InsertItem(0, str0);
}
else
{
if (nIndex == -1)
{
// Find item
while ((nIndex = m_listCtrl->FindItem(nIndex, nData)) != -1)
if (GetItemText(m_listCtrl, nIndex, 1) == hashKey.ToString())
break;
if (nIndex == -1)
{
printf("CMainFrame::InsertLine : Couldn't find item to be updated\n");
return;
}
}
// If sort key changed, must delete and reinsert to make it relocate
if (GetItemText(m_listCtrl, nIndex, 0) != str0)
{
m_listCtrl->DeleteItem(nIndex);
nIndex = m_listCtrl->InsertItem(0, str0);
}
}
m_listCtrl->SetItem(nIndex, 1, hashKey.ToString());
m_listCtrl->SetItem(nIndex, 2, str2);
m_listCtrl->SetItem(nIndex, 3, str3);
m_listCtrl->SetItem(nIndex, 4, str4);
m_listCtrl->SetItem(nIndex, 5, str5);
m_listCtrl->SetItem(nIndex, 6, str6);
m_listCtrl->SetItemData(nIndex, nData);
}
string FormatTxStatus(const CWalletTx& wtx)
{
// Status
int nDepth = wtx.GetDepthInMainChain();
if (!wtx.IsFinal())
return strprintf("Open for %d blocks", nBestHeight - wtx.nLockTime);
else if (nDepth < 6)
return strprintf("%d/unconfirmed", nDepth);
else
return strprintf("%d blocks", nDepth);
}
string SingleLine(const string& strIn)
{
string strOut;
bool fOneSpace = false;
foreach(int c, strIn)
{
if (isspace(c))
{
fOneSpace = true;
}
else if (c > ' ')
{
if (fOneSpace && !strOut.empty())
strOut += ' ';
strOut += c;
fOneSpace = false;
}
}
return strOut;
}
void CMainFrame::InsertTransaction(const CWalletTx& wtx, bool fNew, int nIndex)
{
int64 nTime = wtx.nTimeDisplayed = wtx.GetTxTime();
int64 nCredit = wtx.GetCredit();
int64 nDebit = wtx.GetDebit();
int64 nNet = nCredit - nDebit;
uint256 hash = wtx.GetHash();
string strStatus = FormatTxStatus(wtx);
map<string, string> mapValue = wtx.mapValue;
// Find the block the tx is in
CBlockIndex* pindex = NULL;
map<uint256, CBlockIndex*>::iterator mi = mapBlockIndex.find(wtx.hashBlock);
if (mi != mapBlockIndex.end())
pindex = (*mi).second;
// Sort order, unrecorded transactions sort to the top
string strSort = strprintf("%010d-%01d-%010u",
(pindex ? pindex->nHeight : INT_MAX),
(wtx.IsCoinBase() ? 1 : 0),
wtx.nTimeReceived);
// Insert line
if (nNet > 0 || wtx.IsCoinBase())
{
//
// Credit
//
string strDescription;
if (wtx.IsCoinBase())
{
// Coinbase
strDescription = "Generated";
if (nCredit == 0)
{
int64 nUnmatured = 0;
foreach(const CTxOut& txout, wtx.vout)
nUnmatured += txout.GetCredit();
if (wtx.IsInMainChain())
strDescription += strprintf(" (%s matures in %d more blocks)", FormatMoney(nUnmatured).c_str(), wtx.GetBlocksToMaturity());
else
strDescription += " (not accepted)";
}
}
else if (!mapValue["from"].empty() || !mapValue["message"].empty())
{
// Online transaction
if (!mapValue["from"].empty())
strDescription += "From: " + mapValue["from"];
if (!mapValue["message"].empty())
{
if (!strDescription.empty())
strDescription += " - ";
strDescription += mapValue["message"];
}
}
else
{
// Offline transaction
foreach(const CTxOut& txout, wtx.vout)
{
if (txout.IsMine())
{
vector<unsigned char> vchPubKey;
if (ExtractPubKey(txout.scriptPubKey, true, vchPubKey))
{
string strAddress = PubKeyToAddress(vchPubKey);
if (mapAddressBook.count(strAddress))
{
//strDescription += "Received payment to ";
//strDescription += "Received with address ";
strDescription += "From: unknown, To: ";
strDescription += strAddress;
/// The labeling feature is just too confusing, so I hid it
/// by putting it at the end where it runs off the screen.
/// It can still be seen by widening the column, or in the
/// details dialog.
if (!mapAddressBook[strAddress].empty())
strDescription += " (" + mapAddressBook[strAddress] + ")";
}
}
break;
}
}
}
InsertLine(fNew, nIndex, hash, strSort,
strStatus,
nTime ? DateTimeStr(nTime) : "",
SingleLine(strDescription),
"",
FormatMoney(nNet, true));
}
else
{
bool fAllFromMe = true;
foreach(const CTxIn& txin, wtx.vin)
fAllFromMe = fAllFromMe && txin.IsMine();
bool fAllToMe = true;
foreach(const CTxOut& txout, wtx.vout)
fAllToMe = fAllToMe && txout.IsMine();
if (fAllFromMe && fAllToMe)
{
// Payment to self
int64 nValue = wtx.vout[0].nValue;
InsertLine(fNew, nIndex, hash, strSort,
strStatus,
nTime ? DateTimeStr(nTime) : "",
"Payment to yourself",
FormatMoney(nNet - nValue, true),
FormatMoney(nValue, true));
}
else if (fAllFromMe)
{
//
// Debit
//
int64 nTxFee = nDebit - wtx.GetValueOut();
for (int nOut = 0; nOut < wtx.vout.size(); nOut++)
{
const CTxOut& txout = wtx.vout[nOut];
if (txout.IsMine())
continue;
string strAddress;
if (!mapValue["to"].empty())
{
// Online transaction
strAddress = mapValue["to"];
}
else
{
// Offline transaction
uint160 hash160;
if (ExtractHash160(txout.scriptPubKey, hash160))
strAddress = Hash160ToAddress(hash160);
}
string strDescription = "To: ";
if (mapAddressBook.count(strAddress) && !mapAddressBook[strAddress].empty())
strDescription += mapAddressBook[strAddress] + " ";
strDescription += strAddress;
if (!mapValue["message"].empty())
{
if (!strDescription.empty())
strDescription += " - ";
strDescription += mapValue["message"];
}
int64 nValue = txout.nValue;
if (nOut == 0 && nTxFee > 0)
nValue += nTxFee;
InsertLine(fNew, nIndex, hash, strprintf("%s-%d", strSort.c_str(), nOut),
strStatus,
nTime ? DateTimeStr(nTime) : "",
SingleLine(strDescription),
FormatMoney(-nValue, true),
"");
}
}
else
{
//
// Mixed debit transaction, can't break down payees
//
bool fAllMine = true;
foreach(const CTxOut& txout, wtx.vout)
fAllMine = fAllMine && txout.IsMine();
foreach(const CTxIn& txin, wtx.vin)
fAllMine = fAllMine && txin.IsMine();
InsertLine(fNew, nIndex, hash, strSort,
strStatus,
nTime ? DateTimeStr(nTime) : "",
"",
FormatMoney(nNet, true),
"");
}
}
}
void CMainFrame::RefreshStatus()
{
static int nLastTop;
int nTop = m_listCtrl->GetTopItem();
if (nTop == nLastTop && pindexBestLast == pindexBest)
return;
TRY_CRITICAL_BLOCK(cs_mapWallet)
{
int nStart = nTop;
int nEnd = min(nStart + 100, m_listCtrl->GetItemCount());
if (pindexBestLast == pindexBest)
{
if (nStart >= nLastTop && nStart < nLastTop + 100)
nStart = nLastTop + 100;
if (nEnd >= nLastTop && nEnd < nLastTop + 100)
nEnd = nLastTop;
}
nLastTop = nTop;
pindexBestLast = pindexBest;
for (int nIndex = nStart; nIndex < nEnd; nIndex++)
{
uint256 hash((string)GetItemText(m_listCtrl, nIndex, 1));
map<uint256, CWalletTx>::iterator mi = mapWallet.find(hash);
if (mi == mapWallet.end())
{
printf("CMainFrame::RefreshStatus() : tx not found in mapWallet\n");
continue;
}
const CWalletTx& wtx = (*mi).second;
if (wtx.IsCoinBase() || wtx.GetTxTime() != wtx.nTimeDisplayed)
InsertTransaction(wtx, false, nIndex);
else
m_listCtrl->SetItem(nIndex, 2, FormatTxStatus(wtx));
}
}
}
void CMainFrame::RefreshListCtrl()
{
fRefreshListCtrl = true;
::wxWakeUpIdle();
}
void CMainFrame::OnIdle(wxIdleEvent& event)
{
if (fRefreshListCtrl)
{
// Collect list of wallet transactions and sort newest first
bool fEntered = false;
vector<pair<unsigned int, uint256> > vSorted;
TRY_CRITICAL_BLOCK(cs_mapWallet)
{
printf("RefreshListCtrl starting\n");
fEntered = true;
fRefreshListCtrl = false;
vWalletUpdated.clear();
// Do the newest transactions first
vSorted.reserve(mapWallet.size());
for (map<uint256, CWalletTx>::iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
{
const CWalletTx& wtx = (*it).second;
unsigned int nTime = UINT_MAX - wtx.GetTxTime();
vSorted.push_back(make_pair(nTime, (*it).first));
}
m_listCtrl->DeleteAllItems();
}
if (!fEntered)
return;
sort(vSorted.begin(), vSorted.end());
// Fill list control
for (int i = 0; i < vSorted.size();)
{
if (fShutdown)
return;
bool fEntered = false;
TRY_CRITICAL_BLOCK(cs_mapWallet)
{
fEntered = true;
uint256& hash = vSorted[i++].second;
map<uint256, CWalletTx>::iterator mi = mapWallet.find(hash);
if (mi != mapWallet.end())
InsertTransaction((*mi).second, true);
}
if (!fEntered || i == 100 || i % 500 == 0)
wxYield();
}
printf("RefreshListCtrl done\n");
}
else
{
// Check for time updates
static int64 nLastTime;
if (GetTime() > nLastTime + 30)
{
TRY_CRITICAL_BLOCK(cs_mapWallet)
{
nLastTime = GetTime();
for (map<uint256, CWalletTx>::iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
{
CWalletTx& wtx = (*it).second;
if (wtx.nTimeDisplayed && wtx.nTimeDisplayed != wtx.GetTxTime())
InsertTransaction(wtx, false);
}
}
}
}
}
void CMainFrame::OnPaint(wxPaintEvent& event)
{
event.Skip();
}
void CMainFrame::OnPaintListCtrl(wxPaintEvent& event)
{
// Update listctrl contents
if (!vWalletUpdated.empty())
{
TRY_CRITICAL_BLOCK(cs_mapWallet)
{
pair<uint256, bool> item;
foreach(item, vWalletUpdated)
{
bool fNew = item.second;
map<uint256, CWalletTx>::iterator mi = mapWallet.find(item.first);
if (mi != mapWallet.end())
{
printf("vWalletUpdated: %s %s\n", (*mi).second.GetHash().ToString().substr(0,6).c_str(), fNew ? "new" : "");
InsertTransaction((*mi).second, fNew);
}
}
m_listCtrl->ScrollList(0, INT_MAX);
vWalletUpdated.clear();
}
}
// Update status column of visible items only
RefreshStatus();
// Update status bar
string strGen = "";
if (fGenerateBitcoins)
strGen = " Generating";
if (fGenerateBitcoins && vNodes.empty())
strGen = "(not connected)";
m_statusBar->SetStatusText(strGen, 1);
string strStatus = strprintf(" %d connections %d blocks %d transactions", vNodes.size(), nBestHeight + 1, m_listCtrl->GetItemCount());
m_statusBar->SetStatusText(strStatus, 2);
// Balance total
TRY_CRITICAL_BLOCK(cs_mapWallet)
m_staticTextBalance->SetLabel(FormatMoney(GetBalance()) + " ");
m_listCtrl->OnPaint(event);
}
void CrossThreadCall(wxCommandEvent& event)
{
if (pframeMain)
pframeMain->GetEventHandler()->AddPendingEvent(event);
}
void CrossThreadCall(int nID, void* pdata)
{
wxCommandEvent event;
event.SetInt(nID);
event.SetClientData(pdata);
if (pframeMain)
pframeMain->GetEventHandler()->AddPendingEvent(event);
}
void CMainFrame::OnCrossThreadCall(wxCommandEvent& event)
{
void* pdata = event.GetClientData();
switch (event.GetInt())
{
case UICALL_ADDORDER:
{
break;
}
case UICALL_UPDATEORDER:
{
break;
}
}
}
void CMainFrame::OnMenuFileExit(wxCommandEvent& event)
{
Close(true);
}
void CMainFrame::OnMenuOptionsGenerate(wxCommandEvent& event)
{
GenerateBitcoins(event.IsChecked());
Refresh();
wxPaintEvent eventPaint;
AddPendingEvent(eventPaint);
}
void CMainFrame::OnMenuOptionsChangeYourAddress(wxCommandEvent& event)
{
OnButtonChange(event);
}
void CMainFrame::OnMenuOptionsOptions(wxCommandEvent& event)
{
COptionsDialog dialog(this);
dialog.ShowModal();
}
void CMainFrame::OnMenuHelpAbout(wxCommandEvent& event)
{
CAboutDialog dialog(this);
dialog.ShowModal();
}
void CMainFrame::OnUpdateMenuGenerate( wxUpdateUIEvent& event ) {
event.Check(fGenerateBitcoins);
}
void CMainFrame::OnButtonSend(wxCommandEvent& event)
{
/// debug test
if (fRandSendTest)
{
RandSend();
return;
}
// Toolbar: Send
CSendDialog dialog(this);
dialog.ShowModal();
}
void CMainFrame::OnButtonAddressBook(wxCommandEvent& event)
{
// Toolbar: Address Book
CAddressBookDialog dialogAddr(this, "", false);
if (dialogAddr.ShowModal() == 2)
{
// Send
CSendDialog dialogSend(this, dialogAddr.GetAddress());
dialogSend.ShowModal();
}
}
void CMainFrame::OnSetFocusAddress(wxFocusEvent& event)
{
// Automatically select-all when entering window
m_textCtrlAddress->SetSelection(-1, -1);
fOnSetFocusAddress = true;
event.Skip();
}
void CMainFrame::OnMouseEventsAddress(wxMouseEvent& event)
{
if (fOnSetFocusAddress)
m_textCtrlAddress->SetSelection(-1, -1);
fOnSetFocusAddress = false;
event.Skip();
}
void CMainFrame::OnButtonCopy(wxCommandEvent& event)
{
// Copy address box to clipboard
if (wxTheClipboard->Open())
{
wxTheClipboard->SetData(new wxTextDataObject(m_textCtrlAddress->GetValue()));
wxTheClipboard->Close();
}
}
void CMainFrame::OnButtonChange(wxCommandEvent& event)
{
CYourAddressDialog dialog(this, string(m_textCtrlAddress->GetValue()));
if (!dialog.ShowModal())
return;
string strAddress = (string)dialog.GetAddress();
if (strAddress != m_textCtrlAddress->GetValue())
{
uint160 hash160;
if (!AddressToHash160(strAddress, hash160))
return;
if (!mapPubKeys.count(hash160))
return;
CWalletDB().WriteDefaultKey(mapPubKeys[hash160]);
m_textCtrlAddress->SetValue(strAddress);
}
}
void CMainFrame::OnListItemActivatedAllTransactions(wxListEvent& event)
{
uint256 hash((string)GetItemText(m_listCtrl, event.GetIndex(), 1));
CWalletTx wtx;
CRITICAL_BLOCK(cs_mapWallet)
{
map<uint256, CWalletTx>::iterator mi = mapWallet.find(hash);
if (mi == mapWallet.end())
{
printf("CMainFrame::OnListItemActivatedAllTransactions() : tx not found in mapWallet\n");
return;
}
wtx = (*mi).second;
}
CTxDetailsDialog dialog(this, wtx);
dialog.ShowModal();
//CTxDetailsDialog* pdialog = new CTxDetailsDialog(this, wtx);
//pdialog->Show();
}
void CMainFrame::OnListItemActivatedProductsSent(wxListEvent& event)
{
CProduct& product = *(CProduct*)event.GetItem().GetData();
CEditProductDialog* pdialog = new CEditProductDialog(this);
pdialog->SetProduct(product);
pdialog->Show();
}
void CMainFrame::OnListItemActivatedOrdersSent(wxListEvent& event)
{
CWalletTx& order = *(CWalletTx*)event.GetItem().GetData();
CViewOrderDialog* pdialog = new CViewOrderDialog(this, order, false);
pdialog->Show();
}
void CMainFrame::OnListItemActivatedOrdersReceived(wxListEvent& event)
{
CWalletTx& order = *(CWalletTx*)event.GetItem().GetData();
CViewOrderDialog* pdialog = new CViewOrderDialog(this, order, true);
pdialog->Show();
}
//////////////////////////////////////////////////////////////////////////////
//
// CTxDetailsDialog
//
CTxDetailsDialog::CTxDetailsDialog(wxWindow* parent, CWalletTx wtx) : CTxDetailsDialogBase(parent)
{
string strHTML;
strHTML.reserve(4000);
strHTML += "<html><font face='verdana, arial, helvetica, sans-serif'>";
int64 nTime = wtx.GetTxTime();
int64 nCredit = wtx.GetCredit();
int64 nDebit = wtx.GetDebit();
int64 nNet = nCredit - nDebit;
strHTML += "<b>Status:</b> " + FormatTxStatus(wtx) + "<br>";
strHTML += "<b>Date:</b> " + (nTime ? DateTimeStr(nTime) : "") + "<br>";
//
// From
//
if (wtx.IsCoinBase())
{
strHTML += "<b>Source:</b> Generated<br>";
}
else if (!wtx.mapValue["from"].empty())
{
// Online transaction
if (!wtx.mapValue["from"].empty())
strHTML += "<b>From:</b> " + HtmlEscape(wtx.mapValue["from"]) + "<br>";
}
else
{
// Offline transaction
if (nNet > 0)
{
// Credit
foreach(const CTxOut& txout, wtx.vout)
{
if (txout.IsMine())
{
vector<unsigned char> vchPubKey;
if (ExtractPubKey(txout.scriptPubKey, true, vchPubKey))
{
string strAddress = PubKeyToAddress(vchPubKey);
if (mapAddressBook.count(strAddress))
{
strHTML += "<b>From:</b> unknown<br>";
strHTML += "<b>To:</b> ";
strHTML += HtmlEscape(strAddress);
if (!mapAddressBook[strAddress].empty())
strHTML += " (yours, label: " + mapAddressBook[strAddress] + ")";
else
strHTML += " (yours)";
strHTML += "<br>";
}
}
break;
}
}
}
}
//
// To
//
string strAddress;
if (!wtx.mapValue["to"].empty())
{
// Online transaction
strAddress = wtx.mapValue["to"];
strHTML += "<b>To:</b> ";
if (mapAddressBook.count(strAddress) && !mapAddressBook[strAddress].empty())
strHTML += mapAddressBook[strAddress] + " ";
strHTML += HtmlEscape(strAddress) + "<br>";
}
//
// Amount
//
if (wtx.IsCoinBase() && nCredit == 0)
{
//
// Coinbase
//
int64 nUnmatured = 0;
foreach(const CTxOut& txout, wtx.vout)
nUnmatured += txout.GetCredit();
if (wtx.IsInMainChain())
strHTML += strprintf("<b>Credit:</b> (%s matures in %d more blocks)<br>", FormatMoney(nUnmatured).c_str(), wtx.GetBlocksToMaturity());
else
strHTML += "<b>Credit:</b> (not accepted)<br>";
}
else if (nNet > 0)
{
//
// Credit
//
strHTML += "<b>Credit:</b> " + FormatMoney(nNet) + "<br>";
}
else
{
bool fAllFromMe = true;
foreach(const CTxIn& txin, wtx.vin)
fAllFromMe = fAllFromMe && txin.IsMine();
bool fAllToMe = true;
foreach(const CTxOut& txout, wtx.vout)
fAllToMe = fAllToMe && txout.IsMine();
if (fAllFromMe)
{
//
// Debit
//
foreach(const CTxOut& txout, wtx.vout)
{
if (txout.IsMine())
continue;
if (wtx.mapValue["to"].empty())
{
// Offline transaction
uint160 hash160;
if (ExtractHash160(txout.scriptPubKey, hash160))
{
string strAddress = Hash160ToAddress(hash160);
strHTML += "<b>To:</b> ";
if (mapAddressBook.count(strAddress) && !mapAddressBook[strAddress].empty())
strHTML += mapAddressBook[strAddress] + " ";
strHTML += strAddress;
strHTML += "<br>";
}
}
strHTML += "<b>Debit:</b> " + FormatMoney(-txout.nValue) + "<br>";
}
if (fAllToMe)
{
// Payment to self
int64 nValue = wtx.vout[0].nValue;
strHTML += "<b>Debit:</b> " + FormatMoney(-nValue) + "<br>";
strHTML += "<b>Credit:</b> " + FormatMoney(nValue) + "<br>";
}
int64 nTxFee = nDebit - wtx.GetValueOut();
if (nTxFee > 0)
strHTML += "<b>Transaction fee:</b> " + FormatMoney(-nTxFee) + "<br>";
}
else
{
//
// Mixed debit transaction
//
foreach(const CTxIn& txin, wtx.vin)
if (txin.IsMine())
strHTML += "<b>Debit:</b> " + FormatMoney(-txin.GetDebit()) + "<br>";
foreach(const CTxOut& txout, wtx.vout)
if (txout.IsMine())
strHTML += "<b>Credit:</b> " + FormatMoney(txout.GetCredit()) + "<br>";
}
}
strHTML += "<b>Net amount:</b> " + FormatMoney(nNet, true) + "<br>";
//
// Message
//
if (!wtx.mapValue["message"].empty())
strHTML += "<br><b>Message:</b><br>" + HtmlEscape(wtx.mapValue["message"], true) + "<br>";
if (wtx.IsCoinBase())
strHTML += "<br>Generated coins must wait 120 blocks before they can be spent. When you generated this block, it was broadcast to the network to be added to the block chain. If it fails to get into the chain, it will change to \"not accepted\" and not be spendable. This may occasionally happen if another node generates a block within a few seconds of yours.<br>";
//
// Debug view
//
if (fDebug)
{
strHTML += "<hr><br>debug print<br><br>";
foreach(const CTxIn& txin, wtx.vin)
if (txin.IsMine())
strHTML += "<b>Debit:</b> " + FormatMoney(-txin.GetDebit()) + "<br>";
foreach(const CTxOut& txout, wtx.vout)
if (txout.IsMine())
strHTML += "<b>Credit:</b> " + FormatMoney(txout.GetCredit()) + "<br>";
strHTML += "<b>Inputs:</b><br>";
CRITICAL_BLOCK(cs_mapWallet)
{
foreach(const CTxIn& txin, wtx.vin)
{
COutPoint prevout = txin.prevout;
map<uint256, CWalletTx>::iterator mi = mapWallet.find(prevout.hash);
if (mi != mapWallet.end())
{
const CWalletTx& prev = (*mi).second;
if (prevout.n < prev.vout.size())
{
strHTML += HtmlEscape(prev.ToString(), true);
strHTML += " &nbsp;&nbsp; " + FormatTxStatus(prev) + ", ";
strHTML = strHTML + "IsMine=" + (prev.vout[prevout.n].IsMine() ? "true" : "false") + "<br>";
}
}
}
}
strHTML += "<br><hr><br><b>Transaction:</b><br>";
strHTML += HtmlEscape(wtx.ToString(), true);
}
strHTML += "</font></html>";
string(strHTML.begin(), strHTML.end()).swap(strHTML);
m_htmlWin->SetPage(strHTML);
m_buttonOK->SetFocus();
}
void CTxDetailsDialog::OnButtonOK(wxCommandEvent& event)
{
Close();
//Destroy();
}
//////////////////////////////////////////////////////////////////////////////
//
// COptionsDialog
//
COptionsDialog::COptionsDialog(wxWindow* parent) : COptionsDialogBase(parent)
{
m_buttonOK->SetFocus();
m_treeCtrl->AddRoot(wxT("Settings"));
m_treeCtrl->AppendItem(m_treeCtrl->GetRootItem(), wxT("Bitcoin"));
m_treeCtrl->AppendItem(m_treeCtrl->GetRootItem(), wxT("UI"));
panelUI = new COptionsPanelUI(this);
panelBitcoin = new COptionsPanelBitcoin(this);
currentPanel = panelBitcoin;
panelSizer->Add(panelUI);
panelSizer->Hide(panelUI);
panelSizer->Add(panelBitcoin);
panelSizer->Layout();
}
void COptionsDialog::MenuSelChanged( wxTreeEvent& event )
{
panelSizer->Hide(currentPanel);
wxString text = m_treeCtrl->GetItemText(event.GetItem());
if (text == "Bitcoin") {
panelSizer->Show(panelBitcoin);
currentPanel = panelBitcoin;
}
else {
panelSizer->Show(panelUI);
currentPanel = panelUI;
}
panelSizer->Layout();
}
void COptionsDialog::OnButtonOK(wxCommandEvent& event)
{
// nTransactionFee
int64 nPrevTransactionFee = nTransactionFee;
if (ParseMoney(panelBitcoin->m_textCtrlTransactionFee->GetValue(), nTransactionFee) && nTransactionFee != nPrevTransactionFee)
CWalletDB().WriteSetting("transactionFee", nTransactionFee);
minimizeToTray = panelUI->m_checkMinToTray->IsChecked();
closeToTray = panelUI->m_checkCloseToTray->IsChecked();
startOnSysBoot = panelUI->m_checkStartOnSysBoot->IsChecked();
askBeforeClosing = panelUI->m_checkAskBeforeClosing->IsChecked();
alwaysShowTrayIcon = panelUI->m_checkAlwaysShowTray->IsChecked();
CWalletDB().WriteSetting("minimizeToTray", minimizeToTray);
CWalletDB().WriteSetting("closeToTray", closeToTray);
CWalletDB().WriteSetting("startOnSysBoot", startOnSysBoot);
CWalletDB().WriteSetting("askBeforeClosing", askBeforeClosing);
CWalletDB().WriteSetting("alwaysShowTrayIcon", alwaysShowTrayIcon);
ApplyUISettings();
Close();
}
void COptionsDialog::OnButtonCancel(wxCommandEvent& event)
{
Close();
}
//////////////////////////////////////////////////////////////////////////////
//
// COptionsPanelBitcoin
//
COptionsPanelBitcoin::COptionsPanelBitcoin(wxWindow* parent) : COptionsPanelBitcoinBase(parent)
{
m_textCtrlTransactionFee->SetValue(FormatMoney(nTransactionFee));
}
void COptionsPanelBitcoin::OnKillFocusTransactionFee(wxFocusEvent& event)
{
int64 nTmp = nTransactionFee;
ParseMoney(m_textCtrlTransactionFee->GetValue(), nTmp);
m_textCtrlTransactionFee->SetValue(FormatMoney(nTmp));
}
//////////////////////////////////////////////////////////////////////////////
//
// COptionsPanelUI
//
COptionsPanelUI::COptionsPanelUI(wxWindow* parent) : COptionsPanelUIBase(parent)
{
m_checkMinToTray->SetValue(minimizeToTray);
m_checkCloseToTray->SetValue(closeToTray);
m_checkStartOnSysBoot->SetValue(startOnSysBoot);
m_checkAskBeforeClosing->SetValue(askBeforeClosing);
m_checkAlwaysShowTray->SetValue(alwaysShowTrayIcon);
}
//////////////////////////////////////////////////////////////////////////////
//
// CAboutDialog
//
CAboutDialog::CAboutDialog(wxWindow* parent) : CAboutDialogBase(parent)
{
m_staticTextVersion->SetLabel(strprintf("version 0.%d.%d Alpha", VERSION/100, VERSION%100));
// Workaround until upgrade to wxWidgets supporting UTF-8
wxString str = m_staticTextMain->GetLabel();
if (str.Find('<EFBFBD>') != wxNOT_FOUND)
str.Remove(str.Find('<EFBFBD>'), 1);
m_staticTextMain->SetLabel(str);
}
void CAboutDialog::OnButtonOK(wxCommandEvent& event)
{
Close();
}
//////////////////////////////////////////////////////////////////////////////
//
// CSendDialog
//
CSendDialog::CSendDialog(wxWindow* parent, const wxString& strAddress) : CSendDialogBase(parent)
{
// Init
m_textCtrlAddress->SetValue(strAddress);
m_choiceTransferType->SetSelection(0);
m_bitmapCheckMark->Show(false);
//// todo: should add a display of your balance for convenience
// Set Icon
wxBitmap bmpSend(wxT("send16"), wxBITMAP_TYPE_RESOURCE);
bmpSend.SetMask(new wxMask(wxBitmap(wxT("send16masknoshadow"), wxBITMAP_TYPE_RESOURCE)));
wxIcon iconSend;
iconSend.CopyFromBitmap(bmpSend);
SetIcon(iconSend);
wxCommandEvent event;
OnTextAddress(event);
// Fixup the tab order
m_buttonPaste->MoveAfterInTabOrder(m_buttonCancel);
m_buttonAddress->MoveAfterInTabOrder(m_buttonPaste);
this->Layout();
}
void CSendDialog::OnTextAddress(wxCommandEvent& event)
{
// Check mark
bool fBitcoinAddress = IsValidBitcoinAddress(m_textCtrlAddress->GetValue());
m_bitmapCheckMark->Show(fBitcoinAddress);
// Grey out message if bitcoin address
bool fEnable = !fBitcoinAddress;
m_staticTextFrom->Enable(fEnable);
m_textCtrlFrom->Enable(fEnable);
m_staticTextMessage->Enable(fEnable);
m_textCtrlMessage->Enable(fEnable);
m_textCtrlMessage->SetBackgroundColour(wxSystemSettings::GetColour(fEnable ? wxSYS_COLOUR_WINDOW : wxSYS_COLOUR_BTNFACE));
}
void CSendDialog::OnKillFocusAmount(wxFocusEvent& event)
{
// Reformat the amount
if (m_textCtrlAmount->GetValue().Trim().empty())
return;
int64 nTmp;
if (ParseMoney(m_textCtrlAmount->GetValue(), nTmp))
m_textCtrlAmount->SetValue(FormatMoney(nTmp));
}
void CSendDialog::OnButtonAddressBook(wxCommandEvent& event)
{
// Open address book
CAddressBookDialog dialog(this, m_textCtrlAddress->GetValue(), true);
if (dialog.ShowModal())
m_textCtrlAddress->SetValue(dialog.GetAddress());
}
void CSendDialog::OnButtonPaste(wxCommandEvent& event)
{
// Copy clipboard to address box
if (wxTheClipboard->Open())
{
if (wxTheClipboard->IsSupported(wxDF_TEXT))
{
wxTextDataObject data;
wxTheClipboard->GetData(data);
m_textCtrlAddress->SetValue(data.GetText());
}
wxTheClipboard->Close();
}
}
void CSendDialog::OnButtonSend(wxCommandEvent& event)
{
CWalletTx wtx;
string strAddress = (string)m_textCtrlAddress->GetValue();
// Parse amount
int64 nValue = 0;
if (!ParseMoney(m_textCtrlAmount->GetValue(), nValue) || nValue <= 0)
{
wxMessageBox("Error in amount ", "Send Coins");
return;
}
if (nValue > GetBalance())
{
wxMessageBox("Amount exceeds your balance ", "Send Coins");
return;
}
if (nValue + nTransactionFee > GetBalance())
{
wxMessageBox(string("Total exceeds your balance when the ") + FormatMoney(nTransactionFee) + " transaction fee is included ", "Send Coins");
return;
}
// Parse bitcoin address
uint160 hash160;
bool fBitcoinAddress = AddressToHash160(strAddress, hash160);
if (fBitcoinAddress)
{
// Send to bitcoin address
CScript scriptPubKey;
scriptPubKey << OP_DUP << OP_HASH160 << hash160 << OP_EQUALVERIFY << OP_CHECKSIG;
if (!SendMoney(scriptPubKey, nValue, wtx))
return;
wxMessageBox("Payment sent ", "Sending...");
}
else
{
// Parse IP address
CAddress addr(strAddress.c_str());
if (addr.ip == 0)
{
wxMessageBox("Invalid address ", "Send Coins");
return;
}
// Message
wtx.mapValue["to"] = strAddress;
wtx.mapValue["from"] = m_textCtrlFrom->GetValue();
wtx.mapValue["message"] = m_textCtrlMessage->GetValue();
// Send to IP address
CSendingDialog* pdialog = new CSendingDialog(this, addr, nValue, wtx);
if (!pdialog->ShowModal())
return;
}
if (!mapAddressBook.count(strAddress))
SetAddressBookName(strAddress, "");
EndModal(true);
}
void CSendDialog::OnButtonCancel(wxCommandEvent& event)
{
// Cancel
EndModal(false);
}
//////////////////////////////////////////////////////////////////////////////
//
// CSendingDialog
//
CSendingDialog::CSendingDialog(wxWindow* parent, const CAddress& addrIn, int64 nPriceIn, const CWalletTx& wtxIn) : CSendingDialogBase(NULL) // we have to give null so parent can't destroy us
{
addr = addrIn;
nPrice = nPriceIn;
wtx = wtxIn;
start = wxDateTime::UNow();
strStatus = "";
fCanCancel = true;
fAbort = false;
fSuccess = false;
fUIDone = false;
fWorkDone = false;
SetTitle(strprintf("Sending %s to %s...", FormatMoney(nPrice).c_str(), wtx.mapValue["to"].c_str()));
m_textCtrlStatus->SetValue("");
_beginthread(SendingDialogStartTransfer, 0, this);
}
CSendingDialog::~CSendingDialog()
{
printf("~CSendingDialog()\n");
}
void CSendingDialog::Close()
{
// Last one out turn out the lights.
// fWorkDone signals that work side is done and UI thread should call destroy.
// fUIDone signals that UI window has closed and work thread should call destroy.
// This allows the window to disappear and end modality when cancelled
// without making the user wait for ConnectNode to return. The dialog object
// hangs around in the background until the work thread exits.
if (IsModal())
EndModal(fSuccess);
else
Show(false);
if (fWorkDone)
Destroy();
else
fUIDone = true;
}
void CSendingDialog::OnClose(wxCloseEvent& event)
{
if (!event.CanVeto() || fWorkDone || fAbort || !fCanCancel)
{
Close();
}
else
{
event.Veto();
wxCommandEvent cmdevent;
OnButtonCancel(cmdevent);
}
}
void CSendingDialog::OnButtonOK(wxCommandEvent& event)
{
if (fWorkDone)
Close();
}
void CSendingDialog::OnButtonCancel(wxCommandEvent& event)
{
if (fCanCancel)
fAbort = true;
}
void CSendingDialog::OnPaint(wxPaintEvent& event)
{
if (strStatus.size() > 130)
m_textCtrlStatus->SetValue(string("\n") + strStatus);
else
m_textCtrlStatus->SetValue(string("\n\n") + strStatus);
m_staticTextSending->SetFocus();
if (!fCanCancel)
m_buttonCancel->Enable(false);
if (fWorkDone)
{
m_buttonOK->Enable(true);
m_buttonOK->SetFocus();
m_buttonCancel->Enable(false);
}
if (fAbort && fCanCancel && IsShown())
{
strStatus = "CANCELLED";
m_buttonOK->Enable(true);
m_buttonOK->SetFocus();
m_buttonCancel->Enable(false);
m_buttonCancel->SetLabel("Cancelled");
Close();
wxMessageBox("Transfer cancelled ", "Sending...", wxOK, this);
}
event.Skip();
/// debug test
if (fRandSendTest && fWorkDone && fSuccess)
{
Close();
Sleep(1000);
RandSend();
}
}
//
// Everything from here on is not in the UI thread and must only communicate
// with the rest of the dialog through variables and calling repaint.
//
void CSendingDialog::Repaint()
{
Refresh();
wxPaintEvent event;
AddPendingEvent(event);
}
bool CSendingDialog::Status()
{
if (fUIDone)
{
Destroy();
return false;
}
if (fAbort && fCanCancel)
{
strStatus = "CANCELLED";
Repaint();
fWorkDone = true;
return false;
}
return true;
}
bool CSendingDialog::Status(const string& str)
{
if (!Status())
return false;
strStatus = str;
Repaint();
return true;
}
bool CSendingDialog::Error(const string& str)
{
fCanCancel = false;
fWorkDone = true;
Status(string("Error: ") + str);
return false;
}
void SendingDialogStartTransfer(void* parg)
{
((CSendingDialog*)parg)->StartTransfer();
}
void CSendingDialog::StartTransfer()
{
// Make sure we have enough money
if (nPrice + nTransactionFee > GetBalance())
{
Error("You don't have enough money");
return;
}
// We may have connected already for product details
if (!Status("Connecting..."))
return;
CNode* pnode = ConnectNode(addr, 5 * 60);
if (!pnode)
{
Error("Unable to connect");
return;
}
// Send order to seller, with response going to OnReply2 via event handler
if (!Status("Requesting public key..."))
return;
pnode->PushRequest("checkorder", wtx, SendingDialogOnReply2, this);
}
void SendingDialogOnReply2(void* parg, CDataStream& vRecv)
{
((CSendingDialog*)parg)->OnReply2(vRecv);
}
void CSendingDialog::OnReply2(CDataStream& vRecv)
{
if (!Status("Received public key..."))
return;
CScript scriptPubKey;
int nRet;
try
{
vRecv >> nRet;
if (nRet > 0)
{
string strMessage;
vRecv >> strMessage;
Error("Transfer was not accepted");
//// todo: enlarge the window and enable a hidden white box to put seller's message
return;
}
vRecv >> scriptPubKey;
}
catch (...)
{
//// what do we want to do about this?
Error("Invalid response received");
return;
}
// Should already be connected
CNode* pnode = ConnectNode(addr, 5 * 60);
if (!pnode)
{
Error("Lost connection");
return;
}
// Pause to give the user a chance to cancel
while (wxDateTime::UNow() < start + wxTimeSpan(0, 0, 0, 2 * 1000))
{
Sleep(200);
if (!Status())
return;
}
CRITICAL_BLOCK(cs_main)
{
// Pay
if (!Status("Creating transaction..."))
return;
if (nPrice + nTransactionFee > GetBalance())
{
Error("You don't have enough money");
return;
}
int64 nFeeRequired;
if (!CreateTransaction(scriptPubKey, nPrice, wtx, nFeeRequired))
{
if (nPrice + nFeeRequired > GetBalance())
Error(strprintf("This is an oversized transaction that requires a transaction fee of %s", FormatMoney(nFeeRequired).c_str()));
else
Error("Transaction creation failed");
return;
}
// Last chance to cancel
Sleep(50);
if (!Status())
return;
fCanCancel = false;
if (fAbort)
{
fCanCancel = true;
if (!Status())
return;
fCanCancel = false;
}
if (!Status("Sending payment..."))
return;
// Commit
if (!CommitTransactionSpent(wtx))
{
Error("Error finalizing payment");
return;
}
// Send payment tx to seller, with response going to OnReply3 via event handler
pnode->PushRequest("submitorder", wtx, SendingDialogOnReply3, this);
// Accept and broadcast transaction
if (!wtx.AcceptTransaction())
printf("ERROR: CSendingDialog : wtxNew.AcceptTransaction() %s failed\n", wtx.GetHash().ToString().c_str());
wtx.RelayWalletTransaction();
Status("Waiting for confirmation...");
MainFrameRepaint();
}
}
void SendingDialogOnReply3(void* parg, CDataStream& vRecv)
{
((CSendingDialog*)parg)->OnReply3(vRecv);
}
void CSendingDialog::OnReply3(CDataStream& vRecv)
{
int nRet;
try
{
vRecv >> nRet;
if (nRet > 0)
{
Error("The payment was sent, but the recipient was unable to verify it.\n"
"The transaction is recorded and will credit to the recipient if it is valid,\n"
"but without comment information.");
return;
}
}
catch (...)
{
//// what do we want to do about this?
Error("Payment was sent, but an invalid response was received");
return;
}
fSuccess = true;
fWorkDone = true;
Status("Payment completed");
}
//////////////////////////////////////////////////////////////////////////////
//
// CYourAddressDialog
//
CYourAddressDialog::CYourAddressDialog(wxWindow* parent, const string& strInitSelected) : CYourAddressDialogBase(parent)
{
// Init column headers
m_listCtrl->InsertColumn(0, "Label", wxLIST_FORMAT_LEFT, 200);
m_listCtrl->InsertColumn(1, "Bitcoin Address", wxLIST_FORMAT_LEFT, 350);
m_listCtrl->SetFocus();
// Fill listctrl with address book data
CRITICAL_BLOCK(cs_mapKeys)
{
foreach(const PAIRTYPE(string, string)& item, mapAddressBook)
{
string strAddress = item.first;
string strName = item.second;
uint160 hash160;
bool fMine = (AddressToHash160(strAddress, hash160) && mapPubKeys.count(hash160));
if (fMine)
{
int nIndex = InsertLine(m_listCtrl, strName, strAddress);
if (strAddress == strInitSelected)
m_listCtrl->SetItemState(nIndex, wxLIST_STATE_SELECTED|wxLIST_STATE_FOCUSED, wxLIST_STATE_SELECTED|wxLIST_STATE_FOCUSED);
}
}
}
}
wxString CYourAddressDialog::GetAddress()
{
int nIndex = GetSelection(m_listCtrl);
if (nIndex == -1)
return "";
return GetItemText(m_listCtrl, nIndex, 1);
}
void CYourAddressDialog::OnListEndLabelEdit(wxListEvent& event)
{
// Update address book with edited name
if (event.IsEditCancelled())
return;
string strAddress = (string)GetItemText(m_listCtrl, event.GetIndex(), 1);
SetAddressBookName(strAddress, string(event.GetText()));
pframeMain->RefreshListCtrl();
}
void CYourAddressDialog::OnListItemSelected(wxListEvent& event)
{
}
void CYourAddressDialog::OnListItemActivated(wxListEvent& event)
{
// Doubleclick edits item
wxCommandEvent event2;
OnButtonRename(event2);
}
void CYourAddressDialog::OnButtonRename(wxCommandEvent& event)
{
// Ask new name
int nIndex = GetSelection(m_listCtrl);
if (nIndex == -1)
return;
string strName = (string)m_listCtrl->GetItemText(nIndex);
string strAddress = (string)GetItemText(m_listCtrl, nIndex, 1);
CGetTextFromUserDialog dialog(this, "Edit Address Label", "New Label", strName);
if (!dialog.ShowModal())
return;
strName = dialog.GetValue();
// Change name
SetAddressBookName(strAddress, strName);
m_listCtrl->SetItemText(nIndex, strName);
pframeMain->RefreshListCtrl();
}
void CYourAddressDialog::OnButtonNew(wxCommandEvent& event)
{
// Ask name
CGetTextFromUserDialog dialog(this, "New Bitcoin Address", "Label", "");
if (!dialog.ShowModal())
return;
string strName = dialog.GetValue();
// Generate new key
string strAddress = PubKeyToAddress(GenerateNewKey());
SetAddressBookName(strAddress, strName);
// Add to list and select it
int nIndex = InsertLine(m_listCtrl, strName, strAddress);
SetSelection(m_listCtrl, nIndex);
m_listCtrl->SetFocus();
}
void CYourAddressDialog::OnButtonCopy(wxCommandEvent& event)
{
// Copy address box to clipboard
if (wxTheClipboard->Open())
{
wxTheClipboard->SetData(new wxTextDataObject(GetAddress()));
wxTheClipboard->Close();
}
}
void CYourAddressDialog::OnButtonOK(wxCommandEvent& event)
{
// OK
EndModal(true);
}
void CYourAddressDialog::OnButtonCancel(wxCommandEvent& event)
{
// Cancel
EndModal(false);
}
void CYourAddressDialog::OnClose(wxCloseEvent& event)
{
// Close
EndModal(false);
}
//////////////////////////////////////////////////////////////////////////////
//
// CAddressBookDialog
//
CAddressBookDialog::CAddressBookDialog(wxWindow* parent, const wxString& strInitSelected, bool fSendingIn) : CAddressBookDialogBase(parent)
{
fSending = fSendingIn;
if (!fSending)
m_buttonCancel->Show(false);
// Init column headers
m_listCtrl->InsertColumn(0, "Name", wxLIST_FORMAT_LEFT, 200);
m_listCtrl->InsertColumn(1, "Address", wxLIST_FORMAT_LEFT, 350);
m_listCtrl->SetFocus();
// Set Icon
wxBitmap bmpAddressBook(wxT("addressbook16"), wxBITMAP_TYPE_RESOURCE);
bmpAddressBook.SetMask(new wxMask(wxBitmap(wxT("addressbook16mask"), wxBITMAP_TYPE_RESOURCE)));
wxIcon iconAddressBook;
iconAddressBook.CopyFromBitmap(bmpAddressBook);
SetIcon(iconAddressBook);
// Fill listctrl with address book data
CRITICAL_BLOCK(cs_mapKeys)
{
foreach(const PAIRTYPE(string, string)& item, mapAddressBook)
{
string strAddress = item.first;
string strName = item.second;
uint160 hash160;
bool fMine = (AddressToHash160(strAddress, hash160) && mapPubKeys.count(hash160));
if (!fMine)
{
int nIndex = InsertLine(m_listCtrl, strName, strAddress);
if (strAddress == strInitSelected)
m_listCtrl->SetItemState(nIndex, wxLIST_STATE_SELECTED|wxLIST_STATE_FOCUSED, wxLIST_STATE_SELECTED|wxLIST_STATE_FOCUSED);
}
}
}
}
wxString CAddressBookDialog::GetAddress()
{
int nIndex = GetSelection(m_listCtrl);
if (nIndex == -1)
return "";
return GetItemText(m_listCtrl, nIndex, 1);
}
void CAddressBookDialog::OnListEndLabelEdit(wxListEvent& event)
{
// Update address book with edited name
if (event.IsEditCancelled())
return;
string strAddress = (string)GetItemText(m_listCtrl, event.GetIndex(), 1);
SetAddressBookName(strAddress, string(event.GetText()));
pframeMain->RefreshListCtrl();
}
void CAddressBookDialog::OnListItemSelected(wxListEvent& event)
{
}
void CAddressBookDialog::OnListItemActivated(wxListEvent& event)
{
if (fSending)
{
// Doubleclick returns selection
EndModal(GetAddress() != "" ? 2 : 0);
}
else
{
// Doubleclick edits item
wxCommandEvent event2;
OnButtonEdit(event2);
}
}
bool CAddressBookDialog::CheckIfMine(const string& strAddress, const string& strTitle)
{
uint160 hash160;
bool fMine = (AddressToHash160(strAddress, hash160) && mapPubKeys.count(hash160));
if (fMine)
wxMessageBox("This is one of your own addresses for receiving payments and cannot be entered in the address book. ", strTitle);
return fMine;
}
void CAddressBookDialog::OnButtonEdit(wxCommandEvent& event)
{
// Ask new name
int nIndex = GetSelection(m_listCtrl);
if (nIndex == -1)
return;
string strName = (string)m_listCtrl->GetItemText(nIndex);
string strAddress = (string)GetItemText(m_listCtrl, nIndex, 1);
string strAddressOrg = strAddress;
do
{
CGetTextFromUserDialog dialog(this, "Edit Address", "Name", strName, "Address", strAddress);
if (!dialog.ShowModal())
return;
strName = dialog.GetValue1();
strAddress = dialog.GetValue2();
}
while (CheckIfMine(strAddress, "Edit Address"));
// Change name
if (strAddress != strAddressOrg)
CWalletDB().EraseName(strAddressOrg);
SetAddressBookName(strAddress, strName);
m_listCtrl->SetItem(nIndex, 1, strAddress);
m_listCtrl->SetItemText(nIndex, strName);
pframeMain->RefreshListCtrl();
}
void CAddressBookDialog::OnButtonNew(wxCommandEvent& event)
{
// Ask name
string strName;
string strAddress;
do
{
CGetTextFromUserDialog dialog(this, "New Address", "Name", strName, "Address", strAddress);
if (!dialog.ShowModal())
return;
strName = dialog.GetValue1();
strAddress = dialog.GetValue2();
}
while (CheckIfMine(strAddress, "New Address"));
// Add to list and select it
SetAddressBookName(strAddress, strName);
int nIndex = InsertLine(m_listCtrl, strName, strAddress);
SetSelection(m_listCtrl, nIndex);
m_listCtrl->SetFocus();
pframeMain->RefreshListCtrl();
}
void CAddressBookDialog::OnButtonDelete(wxCommandEvent& event)
{
for (int nIndex = m_listCtrl->GetItemCount()-1; nIndex >= 0; nIndex--)
{
if (m_listCtrl->GetItemState(nIndex, wxLIST_STATE_SELECTED))
{
string strAddress = (string)GetItemText(m_listCtrl, nIndex, 1);
CWalletDB().EraseName(strAddress);
m_listCtrl->DeleteItem(nIndex);
}
}
pframeMain->RefreshListCtrl();
}
void CAddressBookDialog::OnButtonCopy(wxCommandEvent& event)
{
// Copy address box to clipboard
if (wxTheClipboard->Open())
{
wxTheClipboard->SetData(new wxTextDataObject(GetAddress()));
wxTheClipboard->Close();
}
}
void CAddressBookDialog::OnButtonOK(wxCommandEvent& event)
{
// OK
EndModal(GetAddress() != "" ? 1 : 0);
}
void CAddressBookDialog::OnButtonCancel(wxCommandEvent& event)
{
// Cancel
EndModal(0);
}
void CAddressBookDialog::OnClose(wxCloseEvent& event)
{
// Close
EndModal(0);
}
//////////////////////////////////////////////////////////////////////////////
//
// CProductsDialog
//
bool CompareIntStringPairBestFirst(const pair<int, string>& item1, const pair<int, string>& item2)
{
return (item1.first > item2.first);
}
CProductsDialog::CProductsDialog(wxWindow* parent) : CProductsDialogBase(parent)
{
// Init column headers
m_listCtrl->InsertColumn(0, "Title", wxLIST_FORMAT_LEFT, 200);
m_listCtrl->InsertColumn(1, "Price", wxLIST_FORMAT_LEFT, 80);
m_listCtrl->InsertColumn(2, "Seller", wxLIST_FORMAT_LEFT, 80);
m_listCtrl->InsertColumn(3, "Stars", wxLIST_FORMAT_LEFT, 50);
m_listCtrl->InsertColumn(4, "Power", wxLIST_FORMAT_LEFT, 50);
// Tally top categories
map<string, int> mapTopCategories;
CRITICAL_BLOCK(cs_mapProducts)
for (map<uint256, CProduct>::iterator mi = mapProducts.begin(); mi != mapProducts.end(); ++mi)
mapTopCategories[(*mi).second.mapValue["category"]]++;
// Sort top categories
vector<pair<int, string> > vTopCategories;
for (map<string, int>::iterator mi = mapTopCategories.begin(); mi != mapTopCategories.end(); ++mi)
vTopCategories.push_back(make_pair((*mi).second, (*mi).first));
sort(vTopCategories.begin(), vTopCategories.end(), CompareIntStringPairBestFirst);
// Fill categories combo box
int nLimit = 250;
for (vector<pair<int, string> >::iterator it = vTopCategories.begin(); it != vTopCategories.end() && nLimit-- > 0; ++it)
m_comboBoxCategory->Append((*it).second);
// Fill window with initial search
//wxCommandEvent event;
//OnButtonSearch(event);
}
void CProductsDialog::OnCombobox(wxCommandEvent& event)
{
OnButtonSearch(event);
}
bool CompareProductsBestFirst(const CProduct* p1, const CProduct* p2)
{
return (p1->nAtoms > p2->nAtoms);
}
void CProductsDialog::OnButtonSearch(wxCommandEvent& event)
{
string strCategory = (string)m_comboBoxCategory->GetValue();
string strSearch = (string)m_textCtrlSearch->GetValue();
// Search products
vector<CProduct*> vProductsFound;
CRITICAL_BLOCK(cs_mapProducts)
{
for (map<uint256, CProduct>::iterator mi = mapProducts.begin(); mi != mapProducts.end(); ++mi)
{
CProduct& product = (*mi).second;
if (product.mapValue["category"].find(strCategory) != -1)
{
if (product.mapValue["title"].find(strSearch) != -1 ||
product.mapValue["description"].find(strSearch) != -1 ||
product.mapValue["seller"].find(strSearch) != -1)
{
vProductsFound.push_back(&product);
}
}
}
}
// Sort
sort(vProductsFound.begin(), vProductsFound.end(), CompareProductsBestFirst);
// Display
foreach(CProduct* pproduct, vProductsFound)
{
InsertLine(m_listCtrl,
pproduct->mapValue["title"],
pproduct->mapValue["price"],
pproduct->mapValue["seller"],
pproduct->mapValue["stars"],
itostr(pproduct->nAtoms));
}
}
void CProductsDialog::OnListItemActivated(wxListEvent& event)
{
// Doubleclick opens product
CViewProductDialog* pdialog = new CViewProductDialog(this, m_vProduct[event.GetIndex()]);
pdialog->Show();
}
//////////////////////////////////////////////////////////////////////////////
//
// CEditProductDialog
//
CEditProductDialog::CEditProductDialog(wxWindow* parent) : CEditProductDialogBase(parent)
{
m_textCtrlLabel[0 ] = m_textCtrlLabel0;
m_textCtrlLabel[1 ] = m_textCtrlLabel1;
m_textCtrlLabel[2 ] = m_textCtrlLabel2;
m_textCtrlLabel[3 ] = m_textCtrlLabel3;
m_textCtrlLabel[4 ] = m_textCtrlLabel4;
m_textCtrlLabel[5 ] = m_textCtrlLabel5;
m_textCtrlLabel[6 ] = m_textCtrlLabel6;
m_textCtrlLabel[7 ] = m_textCtrlLabel7;
m_textCtrlLabel[8 ] = m_textCtrlLabel8;
m_textCtrlLabel[9 ] = m_textCtrlLabel9;
m_textCtrlLabel[10] = m_textCtrlLabel10;
m_textCtrlLabel[11] = m_textCtrlLabel11;
m_textCtrlLabel[12] = m_textCtrlLabel12;
m_textCtrlLabel[13] = m_textCtrlLabel13;
m_textCtrlLabel[14] = m_textCtrlLabel14;
m_textCtrlLabel[15] = m_textCtrlLabel15;
m_textCtrlLabel[16] = m_textCtrlLabel16;
m_textCtrlLabel[17] = m_textCtrlLabel17;
m_textCtrlLabel[18] = m_textCtrlLabel18;
m_textCtrlLabel[19] = m_textCtrlLabel19;
m_textCtrlField[0 ] = m_textCtrlField0;
m_textCtrlField[1 ] = m_textCtrlField1;
m_textCtrlField[2 ] = m_textCtrlField2;
m_textCtrlField[3 ] = m_textCtrlField3;
m_textCtrlField[4 ] = m_textCtrlField4;
m_textCtrlField[5 ] = m_textCtrlField5;
m_textCtrlField[6 ] = m_textCtrlField6;
m_textCtrlField[7 ] = m_textCtrlField7;
m_textCtrlField[8 ] = m_textCtrlField8;
m_textCtrlField[9 ] = m_textCtrlField9;
m_textCtrlField[10] = m_textCtrlField10;
m_textCtrlField[11] = m_textCtrlField11;
m_textCtrlField[12] = m_textCtrlField12;
m_textCtrlField[13] = m_textCtrlField13;
m_textCtrlField[14] = m_textCtrlField14;
m_textCtrlField[15] = m_textCtrlField15;
m_textCtrlField[16] = m_textCtrlField16;
m_textCtrlField[17] = m_textCtrlField17;
m_textCtrlField[18] = m_textCtrlField18;
m_textCtrlField[19] = m_textCtrlField19;
m_buttonDel[0 ] = m_buttonDel0;
m_buttonDel[1 ] = m_buttonDel1;
m_buttonDel[2 ] = m_buttonDel2;
m_buttonDel[3 ] = m_buttonDel3;
m_buttonDel[4 ] = m_buttonDel4;
m_buttonDel[5 ] = m_buttonDel5;
m_buttonDel[6 ] = m_buttonDel6;
m_buttonDel[7 ] = m_buttonDel7;
m_buttonDel[8 ] = m_buttonDel8;
m_buttonDel[9 ] = m_buttonDel9;
m_buttonDel[10] = m_buttonDel10;
m_buttonDel[11] = m_buttonDel11;
m_buttonDel[12] = m_buttonDel12;
m_buttonDel[13] = m_buttonDel13;
m_buttonDel[14] = m_buttonDel14;
m_buttonDel[15] = m_buttonDel15;
m_buttonDel[16] = m_buttonDel16;
m_buttonDel[17] = m_buttonDel17;
m_buttonDel[18] = m_buttonDel18;
m_buttonDel[19] = m_buttonDel19;
for (int i = 1; i < FIELDS_MAX; i++)
ShowLine(i, false);
LayoutAll();
}
void CEditProductDialog::LayoutAll()
{
m_scrolledWindow->Layout();
m_scrolledWindow->GetSizer()->Fit(m_scrolledWindow);
this->Layout();
}
void CEditProductDialog::ShowLine(int i, bool fShow)
{
m_textCtrlLabel[i]->Show(fShow);
m_textCtrlField[i]->Show(fShow);
m_buttonDel[i]->Show(fShow);
}
void CEditProductDialog::OnButtonDel0(wxCommandEvent& event) { OnButtonDel(event, 0); }
void CEditProductDialog::OnButtonDel1(wxCommandEvent& event) { OnButtonDel(event, 1); }
void CEditProductDialog::OnButtonDel2(wxCommandEvent& event) { OnButtonDel(event, 2); }
void CEditProductDialog::OnButtonDel3(wxCommandEvent& event) { OnButtonDel(event, 3); }
void CEditProductDialog::OnButtonDel4(wxCommandEvent& event) { OnButtonDel(event, 4); }
void CEditProductDialog::OnButtonDel5(wxCommandEvent& event) { OnButtonDel(event, 5); }
void CEditProductDialog::OnButtonDel6(wxCommandEvent& event) { OnButtonDel(event, 6); }
void CEditProductDialog::OnButtonDel7(wxCommandEvent& event) { OnButtonDel(event, 7); }
void CEditProductDialog::OnButtonDel8(wxCommandEvent& event