mirror of https://github.com/ethereum/go-ethereum
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.
148 lines
3.1 KiB
148 lines
3.1 KiB
11 years ago
|
package main
|
||
11 years ago
|
|
||
|
import (
|
||
10 years ago
|
"encoding/json"
|
||
11 years ago
|
"errors"
|
||
10 years ago
|
"fmt"
|
||
10 years ago
|
"io/ioutil"
|
||
|
"net/url"
|
||
|
"os"
|
||
|
"path"
|
||
|
"path/filepath"
|
||
|
|
||
11 years ago
|
"github.com/ethereum/eth-go/ethchain"
|
||
10 years ago
|
"github.com/ethereum/eth-go/ethpipe"
|
||
10 years ago
|
"github.com/ethereum/eth-go/ethstate"
|
||
11 years ago
|
"github.com/ethereum/eth-go/ethutil"
|
||
10 years ago
|
"github.com/ethereum/go-ethereum/javascript"
|
||
11 years ago
|
"github.com/howeyc/fsnotify"
|
||
10 years ago
|
"gopkg.in/qml.v1"
|
||
11 years ago
|
)
|
||
|
|
||
|
type HtmlApplication struct {
|
||
|
win *qml.Window
|
||
|
webView qml.Object
|
||
|
engine *qml.Engine
|
||
|
lib *UiLib
|
||
|
path string
|
||
11 years ago
|
watcher *fsnotify.Watcher
|
||
11 years ago
|
}
|
||
|
|
||
|
func NewHtmlApplication(path string, lib *UiLib) *HtmlApplication {
|
||
|
engine := qml.NewEngine()
|
||
|
|
||
|
return &HtmlApplication{engine: engine, lib: lib, path: path}
|
||
|
|
||
|
}
|
||
|
|
||
|
func (app *HtmlApplication) Create() error {
|
||
|
component, err := app.engine.LoadFile(app.lib.AssetPath("qml/webapp.qml"))
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
if filepath.Ext(app.path) == "eth" {
|
||
|
return errors.New("Ethereum package not yet supported")
|
||
|
|
||
|
// TODO
|
||
10 years ago
|
//ethutil.OpenPackage(app.path)
|
||
11 years ago
|
}
|
||
|
|
||
|
win := component.CreateWindow(nil)
|
||
|
win.Set("url", app.path)
|
||
|
webView := win.ObjectByName("webView")
|
||
|
|
||
|
app.win = win
|
||
|
app.webView = webView
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
11 years ago
|
func (app *HtmlApplication) RootFolder() string {
|
||
|
folder, err := url.Parse(app.path)
|
||
|
if err != nil {
|
||
|
return ""
|
||
|
}
|
||
11 years ago
|
return path.Dir(ethutil.WindonizePath(folder.RequestURI()))
|
||
11 years ago
|
}
|
||
|
func (app *HtmlApplication) RecursiveFolders() []os.FileInfo {
|
||
|
files, _ := ioutil.ReadDir(app.RootFolder())
|
||
|
var folders []os.FileInfo
|
||
|
for _, file := range files {
|
||
|
if file.IsDir() {
|
||
|
folders = append(folders, file)
|
||
|
}
|
||
|
}
|
||
|
return folders
|
||
|
}
|
||
|
|
||
|
func (app *HtmlApplication) NewWatcher(quitChan chan bool) {
|
||
|
var err error
|
||
|
|
||
|
app.watcher, err = fsnotify.NewWatcher()
|
||
|
if err != nil {
|
||
11 years ago
|
logger.Infoln("Could not create new auto-reload watcher:", err)
|
||
11 years ago
|
return
|
||
|
}
|
||
|
err = app.watcher.Watch(app.RootFolder())
|
||
|
if err != nil {
|
||
11 years ago
|
logger.Infoln("Could not start auto-reload watcher:", err)
|
||
|
return
|
||
11 years ago
|
}
|
||
|
for _, folder := range app.RecursiveFolders() {
|
||
|
fullPath := app.RootFolder() + "/" + folder.Name()
|
||
|
app.watcher.Watch(fullPath)
|
||
|
}
|
||
|
|
||
|
go func() {
|
||
|
out:
|
||
|
for {
|
||
|
select {
|
||
|
case <-quitChan:
|
||
|
app.watcher.Close()
|
||
|
break out
|
||
|
case <-app.watcher.Event:
|
||
11 years ago
|
//logger.Debugln("Got event:", ev)
|
||
11 years ago
|
app.webView.Call("reload")
|
||
|
case err := <-app.watcher.Error:
|
||
|
// TODO: Do something here
|
||
11 years ago
|
logger.Infoln("Watcher error:", err)
|
||
11 years ago
|
}
|
||
|
}
|
||
|
}()
|
||
|
|
||
|
}
|
||
|
|
||
11 years ago
|
func (app *HtmlApplication) Engine() *qml.Engine {
|
||
|
return app.engine
|
||
|
}
|
||
|
|
||
|
func (app *HtmlApplication) Window() *qml.Window {
|
||
|
return app.win
|
||
|
}
|
||
|
|
||
|
func (app *HtmlApplication) NewBlock(block *ethchain.Block) {
|
||
10 years ago
|
b := ðpipe.JSBlock{Number: int(block.BlockInfo().Number), Hash: ethutil.Bytes2Hex(block.Hash())}
|
||
11 years ago
|
app.webView.Call("onNewBlockCb", b)
|
||
|
}
|
||
|
|
||
10 years ago
|
func (self *HtmlApplication) Messages(messages ethstate.Messages, id string) {
|
||
|
var msgs []javascript.JSMessage
|
||
|
for _, m := range messages {
|
||
|
msgs = append(msgs, javascript.NewJSMessage(m))
|
||
|
}
|
||
|
|
||
|
b, _ := json.Marshal(msgs)
|
||
|
|
||
|
self.webView.Call("onWatchedCb", string(b), id)
|
||
|
}
|
||
|
|
||
11 years ago
|
func (app *HtmlApplication) Destroy() {
|
||
|
app.engine.Destroy()
|
||
|
}
|
||
10 years ago
|
|
||
|
func (app *HtmlApplication) Post(data string, seed int) {
|
||
|
fmt.Println("about to call 'post'")
|
||
|
app.webView.Call("post", seed, data)
|
||
|
}
|