image_download_process_page.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. package Warehouse
  2. import (
  3. "fmt"
  4. "os"
  5. gotime "time"
  6. "strings"
  7. "path"
  8. //"encoding/json"
  9. "github.com/veandco/go-sdl2/ttf"
  10. "github.com/veandco/go-sdl2/sdl"
  11. "github.com/clockworkpi/LauncherGoDev/sysgo/UI"
  12. "github.com/cuu/gogame/image"
  13. "github.com/cuu/gogame/draw"
  14. "github.com/cuu/gogame/color"
  15. "github.com/cuu/gogame/event"
  16. "github.com/cuu/gogame/surface"
  17. "github.com/cuu/grab"
  18. )
  19. type WareHouseIndex struct {
  20. List []map[string]string `json:"list"`
  21. }
  22. type ImageDownloadProcessPage struct {
  23. UI.Page
  24. ListFontObj *ttf.Font
  25. URLColor *color.Color
  26. TextColor *color.Color
  27. Downloader *grab.Client
  28. resp *grab.Response
  29. req *grab.Request
  30. URL string
  31. Value int
  32. LoadingLabel UI.LabelInterface
  33. Img *sdl.Surface
  34. Downloading chan bool
  35. Parent *WareHouse
  36. }
  37. func NewImageDownloadProcessPage() *ImageDownloadProcessPage {
  38. p := &ImageDownloadProcessPage{}
  39. p.ListFontObj = UI.MyLangManager.TrFont("varela13")
  40. p.URLColor = UI.MySkinManager.GiveColor("URL")
  41. p.TextColor = UI.MySkinManager.GiveColor("Text")
  42. p.FootMsg = [5]string{"Nav.","","","Back",""}
  43. return p
  44. }
  45. func (self *ImageDownloadProcessPage) Init() {
  46. self.PosX = self.Index * self.Screen.Width
  47. self.Width = self.Screen.Width
  48. self.Height = self.Screen.Height
  49. self.CanvasHWND = self.Screen.CanvasHWND
  50. LoadingLabel := UI.NewLabel()
  51. LoadingLabel.SetCanvasHWND(self.CanvasHWND)
  52. LoadingLabel.Init("Loading",self.ListFontObj,nil)
  53. LoadingLabel.SetColor(self.TextColor)
  54. self.LoadingLabel = LoadingLabel
  55. self.Downloader = grab.NewClient()
  56. self.Downloading = make(chan bool,1)
  57. }
  58. func (self *ImageDownloadProcessPage) SetDownloading(v bool) {
  59. for len(self.Downloading) > 0 {
  60. <- self.Downloading
  61. }
  62. self.Downloading <- v
  63. }
  64. func (self *ImageDownloadProcessPage) OnLoadCb() {
  65. if len(self.URL) < 10 {
  66. return
  67. }
  68. self.ClearCanvas()
  69. self.Screen.Draw()
  70. self.Screen.SwapAndShow()
  71. //parts := strings.Split(self.URL,"/")
  72. //filename := strings.TrimSpace(parts[len(parts)-1])
  73. local_dir := strings.Split(self.URL,"raw.githubusercontent.com")
  74. home_path, _ := os.UserHomeDir()
  75. if len(local_dir) > 1 {
  76. menu_file := local_dir[1]
  77. local_menu_file := fmt.Sprintf("%s/aria2downloads%s",
  78. home_path,menu_file)
  79. if UI.FileExists(local_menu_file) {
  80. self.Img = image.Load(local_menu_file)
  81. self.Screen.Draw()
  82. self.Screen.SwapAndShow()
  83. }else {
  84. self.req,_ = grab.NewRequest("/tmp",self.URL)
  85. self.resp = self.Downloader.Do(self.req)
  86. self.SetDownloading(true)
  87. go self.UpdateProcessInterval(400)
  88. }
  89. }
  90. }
  91. func (self *ImageDownloadProcessPage) UpdateProcessInterval(ms int) {
  92. ms_total := 0
  93. t := gotime.NewTicker(gotime.Duration(ms) * gotime.Millisecond)
  94. defer t.Stop()
  95. L:
  96. for {
  97. select {
  98. case <-t.C:
  99. fmt.Printf(" transferred %v / %v bytes (%.2f%%)\n",
  100. self.resp.BytesComplete(),
  101. self.resp.Size,
  102. 100*self.resp.Progress())
  103. ms_total += ms
  104. if(ms_total > 10000) {
  105. fmt.Println("Get preview image timeout")
  106. break L
  107. }
  108. case <-self.resp.Done:
  109. // download is complete
  110. break L
  111. case v:= <-self.Downloading:
  112. if v == false {
  113. break L
  114. }
  115. }
  116. }
  117. dst_filename := self.resp.Filename
  118. if err := self.resp.Err(); err == nil {//download successfully
  119. home_path, _ := os.UserHomeDir()
  120. parts := strings.Split(self.URL,"/")
  121. filename := strings.TrimSpace(parts[len(parts)-1])
  122. local_dir := strings.Split(self.URL,"raw.githubusercontent.com")
  123. local_menu_file := ""
  124. menu_file := ""
  125. if len(local_dir) > 1 {
  126. menu_file = local_dir[1]
  127. local_menu_file = fmt.Sprintf("%s/aria2downloads%s",
  128. home_path,menu_file)
  129. }
  130. dl_file := path.Join("/tmp",filename)
  131. if UI.IsDirectory( path.Base(local_menu_file) ) == false {
  132. merr := os.MkdirAll( path.Base(local_menu_file), os.ModePerm)
  133. if merr != nil {
  134. panic(merr)
  135. }
  136. }
  137. UI.CopyFile(dl_file,local_menu_file)
  138. }
  139. if UI.FileExists(dst_filename) {
  140. if self.Screen.CurPage() == self {
  141. self.Img = image.Load(dst_filename)
  142. self.Screen.Draw()
  143. self.Screen.SwapAndShow()
  144. }
  145. }
  146. }
  147. func (self *ImageDownloadProcessPage) KeyDown(ev *event.Event) {
  148. if UI.IsKeyMenuOrB(ev.Data["Key"]) {
  149. self.SetDownloading(false)
  150. self.ReturnToUpLevelPage()
  151. self.Screen.Draw()
  152. self.Screen.SwapAndShow()
  153. self.URL = ""
  154. }
  155. }
  156. func (self *ImageDownloadProcessPage) Draw() {
  157. self.ClearCanvas()
  158. w,_ := self.LoadingLabel.Size()
  159. self.LoadingLabel.NewCoord( (UI.Width - w)/2,(UI.Height-44)/2);
  160. self.LoadingLabel.Draw()
  161. if self.Img != nil {
  162. surface.Blit(self.CanvasHWND,
  163. self.Img,
  164. draw.MidRect(UI.Width/2,(UI.Height-44)/2,int(self.Img.W),int(self.Img.H),UI.Width,UI.Height-44),
  165. nil)
  166. }
  167. }