github.com/jonasnick/go-ethereum@v0.7.12-0.20150216215225-22176f05d387/cmd/mist/html_container.go (about) 1 /* 2 This file is part of go-ethereum 3 4 go-ethereum is free software: you can redistribute it and/or modify 5 it under the terms of the GNU General Public License as published by 6 the Free Software Foundation, either version 3 of the License, or 7 (at your option) any later version. 8 9 go-ethereum is distributed in the hope that it will be useful, 10 but WITHOUT ANY WARRANTY; without even the implied warranty of 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 GNU General Public License for more details. 13 14 You should have received a copy of the GNU General Public License 15 along with go-ethereum. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 /** 18 * @authors 19 * Jeffrey Wilcke <i@jev.io> 20 */ 21 package main 22 23 import ( 24 "errors" 25 "fmt" 26 "io/ioutil" 27 "net/url" 28 "os" 29 "path" 30 "path/filepath" 31 32 "github.com/jonasnick/go-ethereum/core/types" 33 "github.com/jonasnick/go-ethereum/ethutil" 34 "github.com/jonasnick/go-ethereum/xeth" 35 "github.com/howeyc/fsnotify" 36 "github.com/obscuren/qml" 37 ) 38 39 type HtmlApplication struct { 40 win *qml.Window 41 webView qml.Object 42 engine *qml.Engine 43 lib *UiLib 44 path string 45 watcher *fsnotify.Watcher 46 } 47 48 func NewHtmlApplication(path string, lib *UiLib) *HtmlApplication { 49 engine := qml.NewEngine() 50 51 return &HtmlApplication{engine: engine, lib: lib, path: path} 52 53 } 54 55 func (app *HtmlApplication) Create() error { 56 component, err := app.engine.LoadFile(app.lib.AssetPath("qml/webapp.qml")) 57 if err != nil { 58 return err 59 } 60 61 if filepath.Ext(app.path) == "eth" { 62 return errors.New("Ethereum package not yet supported") 63 64 // TODO 65 //ethutil.OpenPackage(app.path) 66 } 67 68 win := component.CreateWindow(nil) 69 win.Set("url", app.path) 70 webView := win.ObjectByName("webView") 71 72 app.win = win 73 app.webView = webView 74 75 return nil 76 } 77 78 func (app *HtmlApplication) RootFolder() string { 79 folder, err := url.Parse(app.path) 80 if err != nil { 81 return "" 82 } 83 return path.Dir(ethutil.WindonizePath(folder.RequestURI())) 84 } 85 func (app *HtmlApplication) RecursiveFolders() []os.FileInfo { 86 files, _ := ioutil.ReadDir(app.RootFolder()) 87 var folders []os.FileInfo 88 for _, file := range files { 89 if file.IsDir() { 90 folders = append(folders, file) 91 } 92 } 93 return folders 94 } 95 96 func (app *HtmlApplication) NewWatcher(quitChan chan bool) { 97 var err error 98 99 app.watcher, err = fsnotify.NewWatcher() 100 if err != nil { 101 guilogger.Infoln("Could not create new auto-reload watcher:", err) 102 return 103 } 104 err = app.watcher.Watch(app.RootFolder()) 105 if err != nil { 106 guilogger.Infoln("Could not start auto-reload watcher:", err) 107 return 108 } 109 for _, folder := range app.RecursiveFolders() { 110 fullPath := app.RootFolder() + "/" + folder.Name() 111 app.watcher.Watch(fullPath) 112 } 113 114 go func() { 115 out: 116 for { 117 select { 118 case <-quitChan: 119 app.watcher.Close() 120 break out 121 case <-app.watcher.Event: 122 //guilogger.Debugln("Got event:", ev) 123 app.webView.Call("reload") 124 case err := <-app.watcher.Error: 125 // TODO: Do something here 126 guilogger.Infoln("Watcher error:", err) 127 } 128 } 129 }() 130 131 } 132 133 func (app *HtmlApplication) Engine() *qml.Engine { 134 return app.engine 135 } 136 137 func (app *HtmlApplication) Window() *qml.Window { 138 return app.win 139 } 140 141 func (app *HtmlApplication) NewBlock(block *types.Block) { 142 b := &xeth.Block{Number: int(block.NumberU64()), Hash: ethutil.Bytes2Hex(block.Hash())} 143 app.webView.Call("onNewBlockCb", b) 144 } 145 146 func (app *HtmlApplication) Destroy() { 147 app.engine.Destroy() 148 } 149 150 func (app *HtmlApplication) Post(data string, seed int) { 151 fmt.Println("about to call 'post'") 152 app.webView.Call("post", seed, data) 153 }