// SiaDriveDlg.cpp : implementation file // #include "stdafx.h" #include "SiaDriveApp.h" #include "SiaDriveDlg.h" #include "afxdialogex.h" #include #ifdef _DEBUG #define new DEBUG_NEW #endif /* * Home Test wept saxophone dialect depth update jaunt loincloth asleep lush gnome laptop upper olive itches essential neither feel fewest siblings brunt tasked upwards coal niece faked dating hedgehog magically abort * Work Test lush poverty vary fudge ongoing taboo awakened unnoticed piloted ultimate below bikini inundate afraid itches domestic tender poker awoken vitals opus ammo itself alerts brunt wobbly poetry daft addicted */ // CAboutDlg dialog used for App About class CAboutDlg : public CDialogEx { public: CAboutDlg(); // Dialog Data #ifdef AFX_DESIGN_TIME enum { IDD = IDD_ABOUTBOX }; #endif protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support // Implementation protected: DECLARE_MESSAGE_MAP() }; CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX) { } void CAboutDlg::DoDataExchange(CDataExchange* pDX) { CDialogEx::DoDataExchange(pDX); } BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx) END_MESSAGE_MAP() // CSiaDriveDlg dialog BEGIN_DHTML_EVENT_MAP(CSiaDriveDlg) DHTML_EVENT_ONCLICK(_T("ButtonOK"), OnButtonOK) DHTML_EVENT_ONCLICK(_T("ButtonCancel"), OnButtonCancel) DHTML_EVENT_ONCLICK(_T("CreateWalletButton"), OnButtonCreateWallet) DHTML_EVENT_ONCLICK(_T("ConfirmSeedButton"), OnButtonConfirmSeed) DHTML_EVENT_ONCLICK(_T("UnlockWalletButton"), OnButtonUnlockWallet) END_DHTML_EVENT_MAP() CSiaDriveDlg::CSiaDriveDlg(CWnd* pParent /*=NULL*/) : CDHtmlDialog(IDD_SIADRIVE_DIALOG, IDR_HTML_SIADRIVE_DIALOG, pParent), _siaApi({L"localhost", 9980, L"1.1.0"}) { m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CSiaDriveDlg::DoDataExchange(CDataExchange* pDX) { CDHtmlDialog::DoDataExchange(pDX); DDX_DHtml_ElementInnerText(pDX, _T("ServerVersion"), _serverVersion); DDX_DHtml_ElementInnerText(pDX, _T("ClientVersion"), _clientVersion); DDX_DHtml_ElementInnerText(pDX, _T("WalletBalanceTotal"), _walletBalanceTotal); DDX_DHtml_ElementInnerText(pDX, _T("WalletBalanceConfirmed"), _walletBalanceConfirmed); DDX_DHtml_ElementInnerText(pDX, _T("WalletBalanceUnconfirmed"), _walletBalanceUnconfirmed); DDX_DHtml_ElementInnerText(pDX, _T("WalletAddress"), _walletAddress); DDX_DHtml_ElementInnerText(pDX, _T("WalletCreatedSeed"), _walletCreatedSeed); DDX_DHtml_ElementValue(pDX, _T("WalletUnlockPwd"), _unlockWalletPwd); } BEGIN_MESSAGE_MAP(CSiaDriveDlg, CDHtmlDialog) ON_WM_SYSCOMMAND() ON_WM_TIMER() END_MESSAGE_MAP() // CSiaDriveDlg message handlers BOOL CSiaDriveDlg::OnInitDialog() { CDHtmlDialog::OnInitDialog(); // Add "About..." menu item to system menu. // IDM_ABOUTBOX must be in the system command range. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE); if (pSysMenu != NULL) { BOOL bNameValid; CString strAboutMenu; bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX); ASSERT(bNameValid); if (!strAboutMenu.IsEmpty()) { pSysMenu->AppendMenu(MF_SEPARATOR); pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); } } // Set the icon for this dialog. The framework does this automatically // when the application's main window is not a dialog SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon return TRUE; // return TRUE unless you set the focus to a control } void CSiaDriveDlg::OnSysCommand(UINT nID, LPARAM lParam) { if ((nID & 0xFFF0) == IDM_ABOUTBOX) { CAboutDlg dlgAbout; dlgAbout.DoModal(); } else { CDHtmlDialog::OnSysCommand(nID, lParam); } } // If you add a minimize button to your dialog, you will need the code below // to draw the icon. For MFC applications using the document/view model, // this is automatically done for you by the framework. void CSiaDriveDlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, reinterpret_cast(dc.GetSafeHdc()), 0); // Center icon in client rectangle int cxIcon = GetSystemMetrics(SM_CXICON); int cyIcon = GetSystemMetrics(SM_CYICON); CRect rect; GetClientRect(&rect); int x = (rect.Width() - cxIcon + 1) / 2; int y = (rect.Height() - cyIcon + 1) / 2; // Draw the icon dc.DrawIcon(x, y, m_hIcon); } else { CDHtmlDialog::OnPaint(); } } // The system calls this function to obtain the cursor to display while the user drags // the minimized window. HCURSOR CSiaDriveDlg::OnQueryDragIcon() { return static_cast(m_hIcon); } HRESULT CSiaDriveDlg::OnButtonOK(IHTMLElement* /*pElement*/) { OnOK(); return S_OK; } HRESULT CSiaDriveDlg::OnButtonCancel(IHTMLElement* /*pElement*/) { OnCancel(); return S_OK; } HRESULT CSiaDriveDlg::OnButtonConfirmSeed(IHTMLElement* /*pElement*/) { _seedCreation = false; _walletCreatedSeed = L""; UpdateData(FALSE); this->Navigate(this->m_strCurrentUrl); return S_OK; } HRESULT CSiaDriveDlg::OnButtonUnlockWallet(IHTMLElement* /*pElement*/) { UpdateData(TRUE); if (API_SUCCESS(SiaApiError, _siaApi.GetWallet()->Unlock(_unlockWalletPwd.GetBuffer()))) { _unlockWalletPwd = L""; UpdateData(FALSE); this->Navigate(this->m_strCurrentUrl); } else { _unlockWalletPwd = L""; UpdateData(FALSE); AfxMessageBox(L"Invalid password entered"); SetFocusToElement(L"WalletUnlockPwd"); } return S_OK; } bool IsRefreshKeyMessage(const MSG *message) { return message && ((message->message == WM_KEYDOWN) || (message->message == WM_KEYUP)) && (message->wParam == VK_F5); } BOOL CSiaDriveDlg::PreTranslateMessage(MSG* pMsg) { //TODO: Implement copy/paste context menu if (IsRefreshKeyMessage(pMsg) || (pMsg->message == WM_RBUTTONDOWN) || (pMsg->message == WM_RBUTTONDBLCLK)) { return TRUE; } return CDHtmlDialog::PreTranslateMessage(pMsg); } void CSiaDriveDlg::OnDocumentComplete(LPDISPATCH, LPCTSTR) { KillTimer(IDT_UPDATE); _serverVersion = L"x.x.x"; _clientVersion = L"1.0.0"; _walletBalanceConfirmed = L""; _walletBalanceUnconfirmed = L""; _walletBalanceTotal = L""; _walletAddress = L""; SetTimer(IDT_UPDATE, 2000, nullptr); if ((_connected = UpdateUi(false))) { ConfigureWallet(); } else { UpdateData(FALSE); } } void CSiaDriveDlg::OnTimer(UINT_PTR nIDEvent) { switch (nIDEvent) { case IDT_UPDATE: { UpdateUi(); } break; } } bool CSiaDriveDlg::UpdateSiaInfo() { _serverVersion = _siaApi.GetServerVersion().c_str(); if (_serverVersion.GetLength()) { if (_siaApi.GetWallet()->Refresh()) { SiaCurrency confirmed; if (API_SUCCESS(SiaApiError, _siaApi.GetWallet()->GetConfirmedBalance(confirmed))) { SiaCurrency unconfirmed; if (API_SUCCESS(SiaApiError, _siaApi.GetWallet()->GetUnonfirmedBalance(unconfirmed))) { SiaCurrency total = confirmed + unconfirmed; _walletBalanceConfirmed = SiaCurrencyToString(confirmed).c_str(); _walletBalanceUnconfirmed = SiaCurrencyToString(unconfirmed).c_str(); _walletBalanceTotal = SiaCurrencyToString(total).c_str(); if (!_walletAddress.GetLength()) { String address; _siaApi.GetWallet()->GetAddress(address); _walletAddress = address.c_str(); } return true; } } } } _serverVersion = L"x.x.x"; _walletAddress = _walletBalanceConfirmed = _walletBalanceUnconfirmed = _walletBalanceTotal = L""; return false; } bool CSiaDriveDlg::UpdateUi(const bool& refresh) { bool ret = UpdateSiaInfo(); if (ret) { if (_connected) { UpdateData(FALSE); } else if (!_seedCreation) { KillTimer(IDT_UPDATE); this->Navigate(this->m_strCurrentUrl); } } else if (refresh && !_seedCreation) { _connected = false; KillTimer(IDT_UPDATE); this->Navigate(this->m_strCurrentUrl); } return ret; } HRESULT CSiaDriveDlg::GetDomNodeById(const String& id, CComPtr& node) { CComPtr elem; return GetDomNodeAndElementById(id, node, elem); } HRESULT CSiaDriveDlg::GetDomNodeAndElementById(const String& id, CComPtr& node, CComPtr& elem) { HRESULT hr; if (SUCCEEDED((hr = GetElement(id.c_str(), &elem)))) { hr = elem->QueryInterface(IID_IHTMLDOMNode, reinterpret_cast(&node)); } return hr; } void CSiaDriveDlg::RemoveDomNodeById(const String& id) { CComPtr element; if (SUCCEEDED(GetDomNodeById(id.c_str(), element))) { CComPtr parent; if (SUCCEEDED(element->get_parentNode(&parent))) { CComPtr removed; parent->removeChild(element, &removed); } } } void CSiaDriveDlg::RemoveCreateWalletItems() { RemoveDomNodeById(L"create_wallet"); RemoveDomNodeById(L"disp_wallet_seed"); } void CSiaDriveDlg::DisplayCreateWallet() { SetMainWindow(L"create_wallet"); } void CSiaDriveDlg::ConfigureWallet() { if (_siaApi.GetWallet()->GetCreated()) { RemoveCreateWalletItems(); if (_siaApi.GetWallet()->GetLocked()) { DisplayUnlockWallet(); } else { SetMainWindow(L"tab_view"); switch (_siaConfig.GetUI_Main_TabIndex()) { case WALLET_TAB: { //DisplayWalletTab(); } break; } } } else { DisplayCreateWallet(); } } void CSiaDriveDlg::DisplaySeedCreated(const String& seed) { SetMainWindow(L"disp_wallet_seed"); _walletCreatedSeed = seed.c_str(); UpdateData(FALSE); } void CSiaDriveDlg::DisplayUnlockWallet() { KillTimer(IDT_UPDATE); SetMainWindow(L"unlock_wallet"); SetFocusToElement(L"WalletUnlockPwd"); } void CSiaDriveDlg::SetMainWindow(const String& name) { CComPtr mainNode; if (SUCCEEDED(GetDomNodeById(L"main_window", mainNode))) { CComPtr child; if (SUCCEEDED(mainNode->get_firstChild(&child))) { CComPtr removed; if (SUCCEEDED(mainNode->removeChild(child, &removed))) { CComPtr body; if (SUCCEEDED(GetDomNodeById(L"CSiaDriveDlg", body))) { CComPtr element; if (SUCCEEDED(removed->QueryInterface(IID_IHTMLElement, reinterpret_cast(&element)))) { CComPtr style; if (SUCCEEDED(element->get_style(&style))) { style->put_display(L"none"); } CComPtr added; body->appendChild(removed, &added); } } } } CComPtr divNode; CComPtr divElement; if (SUCCEEDED(GetDomNodeAndElementById(name, divNode, divElement))) { CComPtr parent; if (SUCCEEDED(divNode->get_parentNode(&parent))) { CComPtr removedNode; if (SUCCEEDED(parent->removeChild(divNode, &removedNode))) { CComPtr appendedNode; if (SUCCEEDED(mainNode->appendChild(removedNode, &appendedNode))) { CComPtr style; if (SUCCEEDED(divElement->get_style(&style))) { style->put_display(L"block"); } } } } } } } HRESULT CSiaDriveDlg::OnButtonCreateWallet(IHTMLElement* pElement) { if (!_seedCreation) { _seedCreation = true; KillTimer(IDT_UPDATE); String seed; if (API_SUCCESS(SiaApiError, _siaApi.GetWallet()->Create(SiaSeedLanguage::English, seed))) { DisplaySeedCreated(seed); } } return S_OK; }