html_container.go 3.4 KB

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