2014-04-30 02:44:02 +03:00
|
|
|
package ethui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"github.com/ethereum/eth-go/ethchain"
|
2014-05-02 14:55:58 +03:00
|
|
|
"github.com/ethereum/eth-go/ethpub"
|
2014-04-30 02:44:02 +03:00
|
|
|
"github.com/ethereum/eth-go/ethutil"
|
|
|
|
"github.com/go-qml/qml"
|
2014-05-20 17:58:13 +03:00
|
|
|
"github.com/howeyc/fsnotify"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"path"
|
2014-04-30 02:44:02 +03:00
|
|
|
"path/filepath"
|
|
|
|
)
|
|
|
|
|
|
|
|
type HtmlApplication struct {
|
|
|
|
win *qml.Window
|
|
|
|
webView qml.Object
|
|
|
|
engine *qml.Engine
|
|
|
|
lib *UiLib
|
|
|
|
path string
|
2014-05-20 17:58:13 +03:00
|
|
|
watcher *fsnotify.Watcher
|
2014-04-30 02:44:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2014-05-20 17:58:13 +03:00
|
|
|
func (app *HtmlApplication) RootFolder() string {
|
|
|
|
folder, err := url.Parse(app.path)
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return path.Dir(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 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = app.watcher.Watch(app.RootFolder())
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
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:
|
2014-06-23 13:41:11 +03:00
|
|
|
//logger.Debugln("Got event:", ev)
|
2014-05-20 17:58:13 +03:00
|
|
|
app.webView.Call("reload")
|
|
|
|
case err := <-app.watcher.Error:
|
|
|
|
// TODO: Do something here
|
2014-06-23 13:41:11 +03:00
|
|
|
logger.Infoln("Watcher error:", err)
|
2014-05-20 17:58:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-04-30 02:44:02 +03:00
|
|
|
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) {
|
2014-06-29 22:39:45 +03:00
|
|
|
b := ðpub.PBlock{Number: int(block.BlockInfo().Number), Hash: ethutil.Bytes2Hex(block.Hash())}
|
2014-04-30 02:44:02 +03:00
|
|
|
app.webView.Call("onNewBlockCb", b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (app *HtmlApplication) ObjectChanged(stateObject *ethchain.StateObject) {
|
2014-05-02 14:55:58 +03:00
|
|
|
app.webView.Call("onObjectChangeCb", ethpub.NewPStateObject(stateObject))
|
2014-04-30 02:44:02 +03:00
|
|
|
}
|
|
|
|
|
2014-05-05 12:56:05 +03:00
|
|
|
func (app *HtmlApplication) StorageChanged(storageObject *ethchain.StorageState) {
|
|
|
|
app.webView.Call("onStorageChangeCb", ethpub.NewPStorageState(storageObject))
|
2014-04-30 02:44:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (app *HtmlApplication) Destroy() {
|
|
|
|
app.engine.Destroy()
|
|
|
|
}
|