erigon-pulse/ethereal/html_container.go

137 lines
3.0 KiB
Go
Raw Normal View History

2014-07-01 22:13:50 +00:00
package main
import (
"errors"
2014-07-29 22:17:23 +00:00
"io/ioutil"
"net/url"
"os"
"path"
"path/filepath"
"github.com/ethereum/eth-go/ethchain"
2014-05-02 11:55:58 +00:00
"github.com/ethereum/eth-go/ethpub"
"github.com/ethereum/eth-go/ethstate"
"github.com/ethereum/eth-go/ethutil"
"github.com/go-qml/qml"
"github.com/howeyc/fsnotify"
)
type HtmlApplication struct {
win *qml.Window
webView qml.Object
engine *qml.Engine
lib *UiLib
path string
watcher *fsnotify.Watcher
}
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
2014-07-29 22:17:23 +00:00
//ethutil.OpenPackage(app.path)
}
win := component.CreateWindow(nil)
win.Set("url", app.path)
webView := win.ObjectByName("webView")
app.win = win
app.webView = webView
return nil
}
func (app *HtmlApplication) RootFolder() string {
folder, err := url.Parse(app.path)
if err != nil {
return ""
}
return path.Dir(ethutil.WindonizePath(folder.RequestURI()))
}
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 {
logger.Infoln("Could not create new auto-reload watcher:", err)
return
}
err = app.watcher.Watch(app.RootFolder())
if err != nil {
logger.Infoln("Could not start auto-reload watcher:", err)
return
}
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:
//logger.Debugln("Got event:", ev)
app.webView.Call("reload")
case err := <-app.watcher.Error:
// TODO: Do something here
logger.Infoln("Watcher error:", err)
}
}
}()
}
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) {
b := &ethpub.PBlock{Number: int(block.BlockInfo().Number), Hash: ethutil.Bytes2Hex(block.Hash())}
app.webView.Call("onNewBlockCb", b)
}
func (app *HtmlApplication) ObjectChanged(stateObject *ethstate.StateObject) {
2014-05-02 11:55:58 +00:00
app.webView.Call("onObjectChangeCb", ethpub.NewPStateObject(stateObject))
}
func (app *HtmlApplication) StorageChanged(storageObject *ethstate.StorageState) {
2014-05-05 09:56:05 +00:00
app.webView.Call("onStorageChangeCb", ethpub.NewPStorageState(storageObject))
}
func (app *HtmlApplication) Destroy() {
app.engine.Destroy()
}