aboutsummaryrefslogtreecommitdiffstats
path: root/ui/gui.go
blob: fad7e95911cddb0742a916631e8e6d982c8d49df (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
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"
    "time"
)

type Gui struct {
    win       *qml.Window
    engine    *qml.Engine
    component *qml.Common
    eth       *eth.Ethereum

    // The Ethereum library
    lib *EthLib
}

func New(ethereum *eth.Ethereum) *Gui {
    lib := &EthLib{blockManager: ethereum.BlockManager, blockChain: ethereum.BlockManager.BlockChain(), txPool: ethereum.TxPool}

    return &Gui{eth: ethereum, lib: lib}
}

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()
    context.SetVar("eth", ui.lib)
    context.SetVar("ui", &UiLib{engine: ui.engine, eth: ui.eth})

    ui.eth.BlockManager.SecondaryBlockProcessor = ui

    ethutil.Config.Log.AddLogSystem(ui)

    go ui.setInitialBlockChain()
    go ui.updatePeers()

    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)
    }

}

func (ui *Gui) ProcessBlock(block *ethchain.Block) {
    ui.win.Root().Call("addBlock", NewBlockFromBlock(block))
}

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)
}

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)
    }
}

type UiLib struct {
    engine *qml.Engine
    eth    *eth.Ethereum
}

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()
    }()
}

func (ui *UiLib) Connect() {
    ui.eth.Start()
}

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())
    }
}