aboutsummaryrefslogtreecommitdiffstats
path: root/cmd/mist/html_container.go
diff options
context:
space:
mode:
authorFelix Lange <fjl@twurst.com>2014-10-23 21:01:27 +0800
committerFelix Lange <fjl@twurst.com>2014-10-23 21:01:27 +0800
commit69baa465ea69ae60eed802445cf0132b9eb69934 (patch)
treeb09da7582b5c4850d4db13aee808f2fef2f97de0 /cmd/mist/html_container.go
parent50fd46924900869e7210217c6a07979b544991c8 (diff)
parentfeef194829b07570e91873ed5d1e8cc51e8fa430 (diff)
downloaddexon-69baa465ea69ae60eed802445cf0132b9eb69934.tar.gz
dexon-69baa465ea69ae60eed802445cf0132b9eb69934.tar.zst
dexon-69baa465ea69ae60eed802445cf0132b9eb69934.zip
Merge eth-go repository into go-ethereum
mist, etheruem have been moved to cmd/
Diffstat (limited to 'cmd/mist/html_container.go')
-rw-r--r--cmd/mist/html_container.go147
1 files changed, 147 insertions, 0 deletions
diff --git a/cmd/mist/html_container.go b/cmd/mist/html_container.go
new file mode 100644
index 000000000..7dafa025d
--- /dev/null
+++ b/cmd/mist/html_container.go
@@ -0,0 +1,147 @@
+package main
+
+import (
+ "encoding/json"
+ "errors"
+ "fmt"
+ "io/ioutil"
+ "net/url"
+ "os"
+ "path"
+ "path/filepath"
+
+ "github.com/ethereum/go-ethereum/ethchain"
+ "github.com/ethereum/go-ethereum/ethpipe"
+ "github.com/ethereum/go-ethereum/ethstate"
+ "github.com/ethereum/go-ethereum/ethutil"
+ "github.com/ethereum/go-ethereum/javascript"
+ "github.com/howeyc/fsnotify"
+ "gopkg.in/qml.v1"
+)
+
+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
+ //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 := &ethpipe.JSBlock{Number: int(block.BlockInfo().Number), Hash: ethutil.Bytes2Hex(block.Hash())}
+ app.webView.Call("onNewBlockCb", b)
+}
+
+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)
+}
+
+func (app *HtmlApplication) Destroy() {
+ app.engine.Destroy()
+}
+
+func (app *HtmlApplication) Post(data string, seed int) {
+ fmt.Println("about to call 'post'")
+ app.webView.Call("post", seed, data)
+}