op-geth/ui/gui.go

142 lines
2.9 KiB
Go
Raw Normal View History

2014-02-21 11:37:40 +00:00
package ethui
import (
"bufio"
"encoding/hex"
"fmt"
"github.com/ethereum/eth-go"
"github.com/ethereum/eth-go/ethchain"
"github.com/ethereum/eth-go/ethutil"
"github.com/niemeyer/qml"
"strings"
2014-02-21 12:23:35 +00:00
"time"
2014-02-21 11:37:40 +00:00
)
type Gui struct {
win *qml.Window
engine *qml.Engine
component *qml.Common
eth *eth.Ethereum
2014-02-21 16:29:59 +00:00
// The Ethereum library
lib *EthLib
2014-02-21 11:37:40 +00:00
}
func New(ethereum *eth.Ethereum) *Gui {
2014-02-21 16:29:59 +00:00
lib := &EthLib{blockManager: ethereum.BlockManager, blockChain: ethereum.BlockManager.BlockChain(), txPool: ethereum.TxPool}
return &Gui{eth: ethereum, lib: lib}
2014-02-21 11:37:40 +00:00
}
type Block struct {
Number int
Hash string
}
func NewBlockFromBlock(block *ethchain.Block) *Block {
info := block.BlockInfo()
hash := hex.EncodeToString(block.Hash())
return &Block{Number: int(info.Number), Hash: hash}
}
func (ui *Gui) Start() {
qml.RegisterTypes("GoExtensions", 1, 0, []qml.TypeSpec{{
Init: func(p *Block, obj qml.Object) { p.Number = 0; p.Hash = "" },
}})
ethutil.Config.Log.Infoln("[GUI] Starting GUI")
ui.engine = qml.NewEngine()
component, err := ui.engine.LoadFile("wallet.qml")
if err != nil {
panic(err)
}
ui.win = component.CreateWindow(nil)
context := ui.engine.Context()
2014-02-21 16:29:59 +00:00
context.SetVar("eth", ui.lib)
2014-02-22 00:52:47 +00:00
context.SetVar("ui", &UiLib{engine: ui.engine, eth: ui.eth})
2014-02-21 11:37:40 +00:00
ui.eth.BlockManager.SecondaryBlockProcessor = ui
2014-02-22 00:52:47 +00:00
ethutil.Config.Log.AddLogSystem(ui)
go ui.setInitialBlockChain()
2014-02-21 12:23:35 +00:00
go ui.updatePeers()
2014-02-21 11:37:40 +00:00
ui.win.Show()
ui.win.Wait()
}
func (ui *Gui) setInitialBlockChain() {
chain := ui.eth.BlockManager.BlockChain().GetChain(ui.eth.BlockManager.BlockChain().CurrentBlock.Hash(), 10)
for _, block := range chain {
ui.ProcessBlock(block)
}
}
2014-02-21 11:37:40 +00:00
func (ui *Gui) ProcessBlock(block *ethchain.Block) {
ui.win.Root().Call("addBlock", NewBlockFromBlock(block))
}
2014-02-22 00:52:47 +00:00
func (ui *Gui) Println(v ...interface{}) {
str := fmt.Sprintln(v...)
// remove last \n
ui.win.Root().Call("addLog", str[:len(str)-1])
}
func (ui *Gui) Printf(format string, v ...interface{}) {
str := strings.TrimRight(fmt.Sprintf(format, v...), "\n")
ui.win.Root().Call("addLog", str)
}
2014-02-21 12:23:35 +00:00
func (ui *Gui) updatePeers() {
for {
ui.win.Root().Call("setPeers", fmt.Sprintf("%d / %d", ui.eth.Peers().Len(), ui.eth.MaxPeers))
time.Sleep(1 * time.Second)
}
}
2014-02-21 16:29:59 +00:00
type UiLib struct {
engine *qml.Engine
2014-02-22 00:52:47 +00:00
eth *eth.Ethereum
2014-02-21 16:29:59 +00:00
}
func (ui *UiLib) Open(path string) {
component, err := ui.engine.LoadFile(path[7:])
if err != nil {
ethutil.Config.Log.Debugln(err)
}
win := component.CreateWindow(nil)
go func() {
win.Show()
win.Wait()
}()
}
2014-02-22 00:52:47 +00:00
func (ui *UiLib) Connect() {
ui.eth.Start()
}
2014-02-21 11:37:40 +00:00
type Tester struct {
root qml.Object
}
func (t *Tester) Compile(area qml.Object) {
fmt.Println(area)
ethutil.Config.Log.Infoln("[TESTER] Compiling")
code := area.String("text")
scanner := bufio.NewScanner(strings.NewReader(code))
scanner.Split(bufio.ScanLines)
var lines []string
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
}