html_container.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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/common"
  29. "github.com/ethereum/go-ethereum/xeth"
  30. "github.com/howeyc/fsnotify"
  31. "github.com/obscuren/qml"
  32. )
  33. type HtmlApplication struct {
  34. win *qml.Window
  35. webView qml.Object
  36. engine *qml.Engine
  37. lib *UiLib
  38. path string
  39. watcher *fsnotify.Watcher
  40. }
  41. func NewHtmlApplication(path string, lib *UiLib) *HtmlApplication {
  42. engine := qml.NewEngine()
  43. return &HtmlApplication{engine: engine, lib: lib, path: path}
  44. }
  45. func (app *HtmlApplication) Create() error {
  46. component, err := app.engine.LoadFile(app.lib.AssetPath("qml/webapp.qml"))
  47. if err != nil {
  48. return err
  49. }
  50. if filepath.Ext(app.path) == "eth" {
  51. return errors.New("Ethereum package not yet supported")
  52. // TODO
  53. //common.OpenPackage(app.path)
  54. }
  55. win := component.CreateWindow(nil)
  56. win.Set("url", app.path)
  57. webView := win.ObjectByName("webView")
  58. app.win = win
  59. app.webView = webView
  60. return nil
  61. }
  62. func (app *HtmlApplication) RootFolder() string {
  63. folder, err := url.Parse(app.path)
  64. if err != nil {
  65. return ""
  66. }
  67. return path.Dir(common.WindonizePath(folder.RequestURI()))
  68. }
  69. func (app *HtmlApplication) RecursiveFolders() []os.FileInfo {
  70. files, _ := ioutil.ReadDir(app.RootFolder())
  71. var folders []os.FileInfo
  72. for _, file := range files {
  73. if file.IsDir() {
  74. folders = append(folders, file)
  75. }
  76. }
  77. return folders
  78. }
  79. func (app *HtmlApplication) NewWatcher(quitChan chan bool) {
  80. var err error
  81. app.watcher, err = fsnotify.NewWatcher()
  82. if err != nil {
  83. guilogger.Infoln("Could not create new auto-reload watcher:", err)
  84. return
  85. }
  86. err = app.watcher.Watch(app.RootFolder())
  87. if err != nil {
  88. guilogger.Infoln("Could not start auto-reload watcher:", err)
  89. return
  90. }
  91. for _, folder := range app.RecursiveFolders() {
  92. fullPath := app.RootFolder() + "/" + folder.Name()
  93. app.watcher.Watch(fullPath)
  94. }
  95. go func() {
  96. out:
  97. for {
  98. select {
  99. case <-quitChan:
  100. app.watcher.Close()
  101. break out
  102. case <-app.watcher.Event:
  103. //guilogger.Debugln("Got event:", ev)
  104. app.webView.Call("reload")
  105. case err := <-app.watcher.Error:
  106. // TODO: Do something here
  107. guilogger.Infoln("Watcher error:", err)
  108. }
  109. }
  110. }()
  111. }
  112. func (app *HtmlApplication) Engine() *qml.Engine {
  113. return app.engine
  114. }
  115. func (app *HtmlApplication) Window() *qml.Window {
  116. return app.win
  117. }
  118. func (app *HtmlApplication) NewBlock(block *types.Block) {
  119. b := &xeth.Block{Number: int(block.NumberU64()), Hash: common.Bytes2Hex(block.Hash())}
  120. app.webView.Call("onNewBlockCb", b)
  121. }
  122. func (app *HtmlApplication) Destroy() {
  123. app.engine.Destroy()
  124. }
  125. func (app *HtmlApplication) Post(data string, seed int) {
  126. fmt.Println("about to call 'post'")
  127. app.webView.Call("post", seed, data)
  128. }