1
0

Get this puppy started

This commit is contained in:
Scott Graves
2017-03-20 18:34:41 -05:00
parent d9f60ae14f
commit 92cc4bd769
12 changed files with 285 additions and 72 deletions

1
.gitignore vendored
View File

@@ -251,3 +251,4 @@ ModelManifest.xml
/3rd_party/CEF/automate
.idea/
3rd_party/CEF/depot_tools
3rd_party/CEF/depot_tools

Binary file not shown.

View File

@@ -1,14 +1,15 @@
@echo off
pushd "%~dp0%"
set ROOT=%~dp0%
pushd "%ROOT%"
set CUR_PATH=%PATH%
call 3rd_party\CEF\create_debug.cmd
REM call 3rd_party\CEF\create_debug.cmd
set PATH=%CUR_PATH%
mkdir build
mkdir build\debug
pushd build\debug
cmake -G "Visual Studio 14 2015 Win64" -DCMAKE_BUILD_TYPE=Debug -DSIADRIVE_INSTALL_FOLDER=dist ..\..
cmake -G "Visual Studio 14 2015 Win64" -DCMAKE_BUILD_TYPE=Debug -DSIADRIVE_INSTALL_FOLDER="%ROOT%build\Debug\dist" ..\..
cmake --build . --config Debug
popd

View File

@@ -8,7 +8,7 @@ set PATH=%CUR_PATH%
mkdir build
mkdir build\release
pushd build\release
cmake -G "Visual Studio 14 2015 Win64" -DCMAKE_BUILD_TYPE=Release -DSIADRIVE_INSTALL_FOLDER=dist ..\..
cmake -G "Visual Studio 14 2015 Win64" -DCMAKE_BUILD_TYPE=Release -DSIADRIVE_INSTALL_FOLDER="%ROOT%build\Release\dist" ..\..
cmake --build . --config Release
popd

View File

@@ -6,12 +6,11 @@
<link rel="stylesheet" type="text/css" href="css/general.css">
<link rel="stylesheet" type="text/css" href="css/sidenav.css">
<link rel="stylesheet" type="text/css" href="css/tab_page.css">
<script src="js/actions.js"></script>
<script src="js/index.js"></script>
</head>
<body>
<div class="padded" style="padding-bottom: 0;">
<h1 style="font-size: 24px;padding-bottom: 0;">&nbsp;SiaDrive v0.0.1</h1>
<h1 style="font-size: 24px;padding-bottom: 0;" id="ID_SiaDrive"></h1>
</div>
<div class="padded" id="main_window" style="padding-top: 0;"></div>
<div class="hidden-element" id="app_window">
@@ -19,10 +18,6 @@
<h1>Sia Information</h1>
<div style="float: right;">
<table>
<tr>
<td style="text-align: right">Client Version:</td>
<td id="ID_ClientVersion"></td>
</tr>
<tr>
<td style="text-align: right">Server Version:</td>
<td id="ID_ServerVersion"></td>
@@ -33,15 +28,15 @@
<table>
<tr>
<td style="text-align: right">Available:</td>
<td id="ID_WalletConfirmedBalance"></td>
<td id="ID_WalletConfirmedBalance" class="amount"></td>
</tr>
<tr>
<td style="text-align: right">Pending:</td>
<td id="ID_WalletBalanceUnconfirmed"></td>
<td id="ID_WalletBalanceUnconfirmed" class="amount"></td>
</tr>
<tr>
<td style="text-align: right">Total:</td>
<td id="ID_WalletTotalBalance"></td>
<td id="ID_WalletTotalBalance" class="amount"></td>
</tr>
</table>
</div>
@@ -84,7 +79,7 @@
<td id="ID_Renter_AvailablSpace"></td>
<td colspan="50%"></td>
<td style="text-align: right">Download (per GB):</td>
<td id="ID_Renter_EstimatedDownlodCost"></td>
<td id="ID_Renter_EstimatedDownloadCost"></td>
</tr>
<tr>
<td style="text-align: right">Used GB:</td>
@@ -135,6 +130,14 @@
<button id="ID_UnlockWalletButton">Unlock</button>
</div>
</div>
<div class="hidden-element" id="wallet_created_window">
<div class="box">
<h1>Wallet Seed</h1>
<p>Please backup your generated wallet seed in a safe place. The seed is required to unlock your wallet and you will loose everything if this is lost. Click 'OK' when you are ready.</p>
<textarea id="ID_WalletSeed"></textarea>
<button id="ID_WalletCreatedButton">OK</button>
</div>
</div>
<div class="hidden-element" id="unlocking_window">
<div class="box">
<h1>Please Wait</h1>

View File

@@ -1,4 +0,0 @@
// Application to/from HTML
(() => {
})();

View File

@@ -1,40 +1,122 @@
// Main
(() => {
const UiState = (()=> {
function _isWalletLocked() {
return window.uiState.isWalletLocked;
window.uiUpdate = (()=> {
function setInnerText(id, balance) {
document.getElementById(id).innerText = balance;
}
function _isWalletConfigured() {
return window.uiState.isWalletConfigured;
const _renter = (()=> {
return {
setAllocatedFunds: (currency)=> {
setInnerText('ID_Renter_AllocatedFunds', currency);
},
setUsedFunds: (currency)=> {
setInnerText('ID_Renter_UsedFunds', currency);
},
setAvailableFunds: (currency)=> {
setInnerText('ID_Renter_AvailableFunds', currency);
},
setHostCount: (count)=> {
setInnerText('ID_Renter_HostCount', count);
},
setEstimatedSpace: (space)=> {
setInnerText('ID_Renter_EstimatedSpace', space);
},
setEstimatedCost: (currency)=> {
setInnerText('ID_Renter_EstimatedCost', currency);
},
setAvailableSpace: (space)=> {
setInnerText('ID_Renter_AvailablSpace', space);
},
setDownloadCost: (currency)=> {
setInnerText('ID_Renter_EstimatedDownloadCost', currency);
},
setUploadCost: (currency)=> {
setInnerText('ID_Renter_EstimatedUploadCost', currency);
}
};
})();
const _wallet = (()=> {
return {
setConfirmedBalance: (balance)=> {
setInnerText('ID_WalletConfirmedBalance', balance);
},
setUnconfirmedBalance: (balance)=> {
setInnerText('ID_WalletBalanceUnconfirmed', balance);
},
setTotalBalance: (balance)=> {
setInnerText('ID_WalletTotalBalance', balance);
},
setReceiveAddress: (address)=> {
setInnerText('ID_WalletReceiveAddress', address);
}
};
});
return {
Renter: _renter,
Wallet: _wallet,
setBlockHeight: (height) => {
setInnerText('ID_BlockHeight', height);
}
};
})();
const UiState = (()=> {
function _clientVersion() {
return window.uiState.clientVersion;
}
function _isOnline() {
return window.uiState.isOnline;
}
function _isWalletConfigured() {
return window.uiState.isWalletConfigured;
}
function _isWalletLocked() {
return window.uiState.isWalletLocked;
}
function _serverVersion() {
return window.uiState.serverVersion;
}
return {
isWalletLocked: _isWalletLocked,
clientVersion: _clientVersion,
isOnline: _isOnline,
isWalletConfigured: _isWalletConfigured,
isOnline: _isOnline
isWalletLocked: _isWalletLocked,
serverVersion: _serverVersion
};
})();
const UiActions = (() => {
function _unlockWallet(pwd, cb) {
console.log('Unlock wallet');
return window.uiActions.unlockWallet(pwd, cb);
}
const AppActions = (() => {
function _createWallet(cb) {
console.log('Create wallet');
return window.uiActions.createWallet(cb);
return window.appActions.createWallet(cb);
}
function _startApp() {
window.appActions.startApp();
}
function _stopApp() {
window.appActions.stopApp();
}
function _unlockWallet(pwd, cb) {
console.log('Unlock wallet');
return window.appActions.unlockWallet(pwd, cb);
}
return {
unlockWallet: _unlockWallet,
createWallet: _createWallet
createWallet: _createWallet,
startApp: _startApp,
stopApp: _stopApp,
unlockWallet: _unlockWallet
};
})();
@@ -55,54 +137,83 @@
}
function displayErrorPopup(title, msg, cb) {
alert(msg);
if (cb) {
cb();
}
}
function beginMainApplication() {
AppActions.startApp();
setMainWindow('app_window');
}
function handleUnlockWallet() {
setMainWindow('unlock_window');
const unlockButton = document.getElementById('ID_UnlockWalletButton');
unlockButton.onclick = () => {
unlockButton.onclick = null;
const password = document.getElementById('ID_WalletUnlockPwd');
if (AppActions.unlockWallet(password.value, (success, reason) => {
password.value = '';
if (success) {
beginMainApplication();
} else {
displayErrorPopup('Error', reason, () => {
handleUnlockWallet();
});
}
})) {
setMainWindow('unlocking_window');
}
};
}
function handleWalletCreated(seed) {
setMainWindow('wallet_created_window');
document.getElementById('ID_WalletSeed').innerText = seed;
const button = document.getElementById('ID_WalletCreatedButton');
button.onclick = ()=> {
button.onclick = null;
handleUnlockWallet();
};
}
function handleCreateWallet() {
setMainWindow('create_window');
const createButton = document.getElementById('ID_CreateWalletButton');
createButton.onclick = () => {
createButton.onclick = null;
AppActions.createWallet((success, reasonOrSeed) => {
if (success) {
handleWalletCreated(reasonOrSeed);
} else {
displayErrorPopup('Error', reasonOrSeed, () => {
handleCreateWallet();
});
}
});
};
}
window.addEventListener('load', ()=> {
console.log('Main window load');
AppActions.stopApp();
document.getElementById('ID_SiaDrive').innerText = 'SiaDrive ' + UiState.clientVersion();
document.getElementById('ID_ServerVersion').innerText = UiState.serverVersion();
if (UiState.isOnline()) {
if (UiState.isWalletConfigured()) {
if (UiState.isWalletLocked()) {
setMainWindow('unlock_window');
const unlockButton = document.getElementById('ID_UnlockWalletButton');
unlockButton.onclick = ()=> {
const password = document.getElementById('ID_WalletUnlockPwd');
if (UiActions.unlockWallet(password.value, (success, reason) => {
password.value = '';
if (success) {
setMainWindow('app_window');
} else {
displayErrorPopup('Error', reason, ()=> {
location.reload();
});
}
})) {
setMainWindow('unlocking_window');
}
};
handleUnlockWallet();
} else {
setMainWindow('app_window');
beginMainApplication();
}
} else {
setMainWindow('create_window');
const createButton = document.getElementById('ID_CreateWalletButton');
createButton.onclick = () => {
UiActions.createWallet((success, reasonOrSeed) => {
if (success) {
setMainWindow('display_seed_window');
} else {
displayErrorPopup('Error', reasonOrSeed, ()=> {
location.reload();
});
}
});
};
handleCreateWallet();
}
} else {
setMainWindow('offline_window');
}
});
})();

View File

@@ -26,6 +26,7 @@ private:
std::unique_ptr<Api::CSiaApi> _siaApi;
std::unique_ptr<Api::CSiaCurl> _siaCurl;
std::unique_ptr<Api::CSiaDriveConfig> _siaDriveConfig;
bool _appStarted = false;
public:
// CefApp methods:

View File

@@ -1,6 +1,8 @@
#ifndef _SIACOMMON_H
#define _SIACOMMON_H
#define SIDRIVE_VERSION_STRING "0.0.1"
#ifdef _WIN32
// Unicode for Win32
#define UNICODE

View File

@@ -121,6 +121,36 @@ public:
return std::move(SString(std::to_string(value)));
}
static inline std::uint32_t ToUInt32(const SString &str)
{
return std::stoul(str.str());
}
static inline SString FromUInt32(const std::uint32_t &value)
{
return std::move(SString(std::to_string(value)));
}
static inline std::int64_t ToInt64(const SString &str)
{
return std::stoi(str.str());
}
static inline SString FromInt64(const std::int64_t &value)
{
return std::move(SString(std::to_string(value)));
}
static inline SString FromUInt64(const std::uint64_t &value)
{
return std::move(SString(std::to_string(value)));
}
static inline std::uint64_t ToUInt64(const SString &str)
{
return std::stoull(str.str());
}
static inline std::uint8_t ToUInt8(const SString &str)
{
return (std::uint8_t) std::stoi(str.str());

View File

@@ -30,7 +30,9 @@ int APIENTRY wWinMain(HINSTANCE hInstance,
CefSettings settings;
settings.no_sandbox = true;
settings.remote_debugging_port = 8080;
settings.single_process = true;
#ifdef _DEBUG
settings.single_process = false;
#endif
CefInitialize(mainArgs, settings, app, nullptr);
CefRunMessageLoop();

View File

@@ -16,13 +16,15 @@ class FunctionHandler :
public CefV8Handler
{
public:
FunctionHandler(const CSiaApi& siaApi) :
_siaApi(siaApi)
FunctionHandler(const CSiaApi& siaApi, bool& appStarted) :
_siaApi(siaApi),
_appStarted(appStarted)
{
}
private:
const CSiaApi& _siaApi;
bool& _appStarted;
private:
void UnlockCallback(CefRefPtr<CefV8Context> context, CefRefPtr<CefV8Value> cb, const SString& password) const
@@ -81,6 +83,16 @@ public:
CefPostTask(TID_RENDERER, base::Bind(&FunctionHandler::CreateWalletCallback, this, context, cb));
return true;
}
else if (name == "startApp")
{
_appStarted = true;
return true;
}
else if (name == "stopApp")
{
_appStarted = false;
return true;
}
// Function does not exist.
return false;
@@ -153,13 +165,16 @@ void CSiaDriveApp::OnContextCreated(
obj->SetValue("isWalletLocked", CefV8Value::CreateBool(_siaApi->GetWallet()->GetLocked()), V8_PROPERTY_ATTRIBUTE_NONE);
obj->SetValue("isWalletConfigured", CefV8Value::CreateBool(_siaApi->GetWallet()->GetCreated()), V8_PROPERTY_ATTRIBUTE_NONE);
obj->SetValue("isOnline", CefV8Value::CreateBool(_siaApi->GetWallet()->GetConnected()), V8_PROPERTY_ATTRIBUTE_NONE);
obj->SetValue("clientVersion", CefV8Value::CreateString(SIDRIVE_VERSION_STRING), V8_PROPERTY_ATTRIBUTE_NONE);
global->SetValue("uiState", obj, V8_PROPERTY_ATTRIBUTE_NONE);
CefRefPtr<FunctionHandler> handler(new FunctionHandler(*_siaApi));
CefRefPtr<FunctionHandler> handler(new FunctionHandler(*_siaApi, _appStarted));
obj = CefV8Value::CreateObject(nullptr, nullptr);
obj->SetValue("unlockWallet", CefV8Value::CreateFunction("unlockWallet", handler), V8_PROPERTY_ATTRIBUTE_NONE);
obj->SetValue("createWallet", CefV8Value::CreateFunction("createWallet", handler), V8_PROPERTY_ATTRIBUTE_NONE);
global->SetValue("uiActions", obj, V8_PROPERTY_ATTRIBUTE_NONE);
obj->SetValue("startApp", CefV8Value::CreateFunction("startApp", handler), V8_PROPERTY_ATTRIBUTE_NONE);
obj->SetValue("stopApp", CefV8Value::CreateFunction("stopApp", handler), V8_PROPERTY_ATTRIBUTE_NONE);
global->SetValue("appActions", obj, V8_PROPERTY_ATTRIBUTE_NONE);
_refreshThread.reset(new CAutoThread(*_siaCurl, _siaDriveConfig.get(), [this, context](const CSiaCurl& siaCurl, CSiaDriveConfig* siaDriveConfig)
{
@@ -237,8 +252,59 @@ void CSiaDriveApp::SiaApiRefreshCallback(CefRefPtr<CefV8Context> context, const
if (wasOnline != isOnline)
{
_appStarted = false;
context->GetBrowser()->Reload();
}
else if (_appStarted)
{
auto uiActions = global->GetValue("uiUpdate");
// Display wallet data
auto walletActions = uiActions->GetValue("Wallet");
auto confirmedBalance = _siaApi->GetWallet()->GetConfirmedBalance();
auto unconfirmedBalance = _siaApi->GetWallet()->GetUnconfirmedBalance();
auto totalBalance = confirmedBalance + unconfirmedBalance;
{
auto setConfirmed = walletActions->GetValue("setConfirmedBalance");
CefV8ValueList args;
args.push_back(CefV8Value::CreateString(SiaCurrencyToString(confirmedBalance).str()));
setConfirmed->ExecuteFunctionWithContext(context, nullptr, args);
}
{
auto setUnconfirmed = walletActions->GetValue("setUnconfirmedBalance");
CefV8ValueList args;
args.push_back(CefV8Value::CreateString(SiaCurrencyToString(unconfirmedBalance).str()));
setUnconfirmed->ExecuteFunctionWithContext(context, nullptr, args);
}
{
auto setTotalBal = walletActions->GetValue("setTotalBalance");
CefV8ValueList args;
args.push_back(CefV8Value::CreateString(SiaCurrencyToString(totalBalance).str()));
setTotalBal->ExecuteFunctionWithContext(context, nullptr, args);
}
{
auto setReceiveAddress = walletActions->GetValue("setReceiveAddress");
CefV8ValueList args;
args.push_back(CefV8Value::CreateString(_siaApi->GetWallet()->GetReceiveAddress().str()));
setReceiveAddress->ExecuteFunctionWithContext(context, nullptr, args);
}
// Display renter data
auto renterActions = uiActions->GetValue("Renter");
// Display block height
{
auto setBlockHeight = uiActions->GetValue("setBlockHeight");
CefV8ValueList args;
args.push_back(CefV8Value::CreateString(SString::FromUInt64(_siaApi->GetConsensus()->GetHeight()).str()));
setBlockHeight->ExecuteFunctionWithContext(context, nullptr, args);
}
}
context->Exit();
}
else