| 
									
										
										
										
											2015-01-06 12:13:57 +01:00
										 |  |  | /* | 
					
						
							|  |  |  | 	This file is part of go-ethereum | 
					
						
							| 
									
										
										
										
											2014-10-23 15:48:53 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-06 12:13:57 +01:00
										 |  |  | 	go-ethereum is free software: you can redistribute it and/or modify | 
					
						
							|  |  |  | 	it under the terms of the GNU General Public License as published by | 
					
						
							|  |  |  | 	the Free Software Foundation, either version 3 of the License, or | 
					
						
							|  |  |  | 	(at your option) any later version. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	go-ethereum is distributed in the hope that it will be useful, | 
					
						
							|  |  |  | 	but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  | 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  | 	GNU General Public License for more details. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  | 	along with go-ethereum.  If not, see <http://www.gnu.org/licenses/>. | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * @authors | 
					
						
							|  |  |  |  * 	Jeffrey Wilcke <i@jev.io> | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2014-07-02 00:13:50 +02:00
										 |  |  | package main | 
					
						
							| 
									
										
										
										
											2014-04-30 01:44:02 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"errors" | 
					
						
							| 
									
										
										
										
											2014-08-17 12:41:23 +02:00
										 |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2014-07-30 00:17:23 +02:00
										 |  |  | 	"io/ioutil" | 
					
						
							|  |  |  | 	"net/url" | 
					
						
							|  |  |  | 	"os" | 
					
						
							|  |  |  | 	"path" | 
					
						
							|  |  |  | 	"path/filepath" | 
					
						
							| 
									
										
										
										
											2014-12-04 10:28:02 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-16 11:27:38 +01:00
										 |  |  | 	"github.com/ethereum/go-ethereum/common" | 
					
						
							| 
									
										
										
										
											2015-03-22 14:25:33 +01:00
										 |  |  | 	"github.com/ethereum/go-ethereum/core/types" | 
					
						
							| 
									
										
										
										
											2014-10-31 14:30:08 +01:00
										 |  |  | 	"github.com/ethereum/go-ethereum/xeth" | 
					
						
							| 
									
										
										
										
											2014-05-20 16:58:13 +02:00
										 |  |  | 	"github.com/howeyc/fsnotify" | 
					
						
							| 
									
										
										
										
											2015-01-28 14:51:54 +01:00
										 |  |  | 	"github.com/obscuren/qml" | 
					
						
							| 
									
										
										
										
											2014-04-30 01:44:02 +02:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type HtmlApplication struct { | 
					
						
							|  |  |  | 	win     *qml.Window | 
					
						
							|  |  |  | 	webView qml.Object | 
					
						
							|  |  |  | 	engine  *qml.Engine | 
					
						
							|  |  |  | 	lib     *UiLib | 
					
						
							|  |  |  | 	path    string | 
					
						
							| 
									
										
										
										
											2014-05-20 16:58:13 +02:00
										 |  |  | 	watcher *fsnotify.Watcher | 
					
						
							| 
									
										
										
										
											2014-04-30 01:44:02 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func NewHtmlApplication(path string, lib *UiLib) *HtmlApplication { | 
					
						
							|  |  |  | 	engine := qml.NewEngine() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return &HtmlApplication{engine: engine, lib: lib, path: path} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (app *HtmlApplication) Create() error { | 
					
						
							|  |  |  | 	component, err := app.engine.LoadFile(app.lib.AssetPath("qml/webapp.qml")) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if filepath.Ext(app.path) == "eth" { | 
					
						
							|  |  |  | 		return errors.New("Ethereum package not yet supported") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// TODO | 
					
						
							| 
									
										
										
										
											2015-03-16 11:27:38 +01:00
										 |  |  | 		//common.OpenPackage(app.path) | 
					
						
							| 
									
										
										
										
											2014-04-30 01:44:02 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	win := component.CreateWindow(nil) | 
					
						
							|  |  |  | 	win.Set("url", app.path) | 
					
						
							|  |  |  | 	webView := win.ObjectByName("webView") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	app.win = win | 
					
						
							|  |  |  | 	app.webView = webView | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-20 16:58:13 +02:00
										 |  |  | func (app *HtmlApplication) RootFolder() string { | 
					
						
							|  |  |  | 	folder, err := url.Parse(app.path) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return "" | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-03-16 11:27:38 +01:00
										 |  |  | 	return path.Dir(common.WindonizePath(folder.RequestURI())) | 
					
						
							| 
									
										
										
										
											2014-05-20 16:58:13 +02:00
										 |  |  | } | 
					
						
							|  |  |  | func (app *HtmlApplication) RecursiveFolders() []os.FileInfo { | 
					
						
							|  |  |  | 	files, _ := ioutil.ReadDir(app.RootFolder()) | 
					
						
							|  |  |  | 	var folders []os.FileInfo | 
					
						
							|  |  |  | 	for _, file := range files { | 
					
						
							|  |  |  | 		if file.IsDir() { | 
					
						
							|  |  |  | 			folders = append(folders, file) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return folders | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (app *HtmlApplication) NewWatcher(quitChan chan bool) { | 
					
						
							|  |  |  | 	var err error | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	app.watcher, err = fsnotify.NewWatcher() | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2014-10-31 12:56:05 +01:00
										 |  |  | 		guilogger.Infoln("Could not create new auto-reload watcher:", err) | 
					
						
							| 
									
										
										
										
											2014-05-20 16:58:13 +02:00
										 |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	err = app.watcher.Watch(app.RootFolder()) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2014-10-31 12:56:05 +01:00
										 |  |  | 		guilogger.Infoln("Could not start auto-reload watcher:", err) | 
					
						
							| 
									
										
										
										
											2014-07-14 15:29:02 +02:00
										 |  |  | 		return | 
					
						
							| 
									
										
										
										
											2014-05-20 16:58:13 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	for _, folder := range app.RecursiveFolders() { | 
					
						
							|  |  |  | 		fullPath := app.RootFolder() + "/" + folder.Name() | 
					
						
							|  |  |  | 		app.watcher.Watch(fullPath) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	go func() { | 
					
						
							|  |  |  | 	out: | 
					
						
							|  |  |  | 		for { | 
					
						
							|  |  |  | 			select { | 
					
						
							|  |  |  | 			case <-quitChan: | 
					
						
							|  |  |  | 				app.watcher.Close() | 
					
						
							|  |  |  | 				break out | 
					
						
							|  |  |  | 			case <-app.watcher.Event: | 
					
						
							| 
									
										
										
										
											2014-10-31 12:56:05 +01:00
										 |  |  | 				//guilogger.Debugln("Got event:", ev) | 
					
						
							| 
									
										
										
										
											2014-05-20 16:58:13 +02:00
										 |  |  | 				app.webView.Call("reload") | 
					
						
							|  |  |  | 			case err := <-app.watcher.Error: | 
					
						
							|  |  |  | 				// TODO: Do something here | 
					
						
							| 
									
										
										
										
											2014-10-31 12:56:05 +01:00
										 |  |  | 				guilogger.Infoln("Watcher error:", err) | 
					
						
							| 
									
										
										
										
											2014-05-20 16:58:13 +02:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	}() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-30 01:44:02 +02:00
										 |  |  | func (app *HtmlApplication) Engine() *qml.Engine { | 
					
						
							|  |  |  | 	return app.engine | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (app *HtmlApplication) Window() *qml.Window { | 
					
						
							|  |  |  | 	return app.win | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-18 16:58:22 +01:00
										 |  |  | func (app *HtmlApplication) NewBlock(block *types.Block) { | 
					
						
							| 
									
										
										
										
											2015-03-22 14:25:33 +01:00
										 |  |  | 	b := &xeth.Block{Number: int(block.NumberU64()), Hash: block.Hash().Hex()} | 
					
						
							| 
									
										
										
										
											2014-04-30 01:44:02 +02:00
										 |  |  | 	app.webView.Call("onNewBlockCb", b) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (app *HtmlApplication) Destroy() { | 
					
						
							|  |  |  | 	app.engine.Destroy() | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2014-08-17 12:41:23 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | func (app *HtmlApplication) Post(data string, seed int) { | 
					
						
							|  |  |  | 	fmt.Println("about to call 'post'") | 
					
						
							|  |  |  | 	app.webView.Call("post", seed, data) | 
					
						
							|  |  |  | } |