html_container.go 3.8 KB

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