html_container.go 2.79 KB
Newer Older
obscuren's avatar
obscuren committed
1
package main
2 3 4 5

import (
	"errors"
	"github.com/ethereum/eth-go/ethchain"
obscuren's avatar
obscuren committed
6
	"github.com/ethereum/eth-go/ethpub"
7 8
	"github.com/ethereum/eth-go/ethutil"
	"github.com/go-qml/qml"
9 10 11 12 13 14
	"github.com/howeyc/fsnotify"
	"io/ioutil"
	"log"
	"net/url"
	"os"
	"path"
15 16 17 18 19 20 21 22 23
	"path/filepath"
)

type HtmlApplication struct {
	win     *qml.Window
	webView qml.Object
	engine  *qml.Engine
	lib     *UiLib
	path    string
24
	watcher *fsnotify.Watcher
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
}

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
}

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
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:
99
				//logger.Debugln("Got event:", ev)
100 101 102
				app.webView.Call("reload")
			case err := <-app.watcher.Error:
				// TODO: Do something here
103
				logger.Infoln("Watcher error:", err)
104 105 106 107 108 109
			}
		}
	}()

}

110 111 112 113 114 115 116 117 118
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) {
119
	b := &ethpub.PBlock{Number: int(block.BlockInfo().Number), Hash: ethutil.Bytes2Hex(block.Hash())}
120 121 122 123
	app.webView.Call("onNewBlockCb", b)
}

func (app *HtmlApplication) ObjectChanged(stateObject *ethchain.StateObject) {
obscuren's avatar
obscuren committed
124
	app.webView.Call("onObjectChangeCb", ethpub.NewPStateObject(stateObject))
125 126
}

obscuren's avatar
obscuren committed
127 128
func (app *HtmlApplication) StorageChanged(storageObject *ethchain.StorageState) {
	app.webView.Call("onStorageChangeCb", ethpub.NewPStorageState(storageObject))
129 130 131 132 133
}

func (app *HtmlApplication) Destroy() {
	app.engine.Destroy()
}