html_container.go 3.9 KB

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