image_download_process_page.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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)
  57. }
  58. func (self *ImageDownloadProcessPage) OnLoadCb() {
  59. if len(self.URL) < 10 {
  60. return
  61. }
  62. self.ClearCanvas()
  63. self.Screen.Draw()
  64. self.Screen.SwapAndShow()
  65. //parts := strings.Split(self.URL,"/")
  66. //filename := strings.TrimSpace(parts[len(parts)-1])
  67. local_dir := strings.Split(self.URL,"raw.githubusercontent.com")
  68. home_path, _ := os.UserHomeDir()
  69. if len(local_dir) > 1 {
  70. menu_file := local_dir[1]
  71. local_menu_file := fmt.Sprintf("%s/aria2downloads%s",
  72. home_path,menu_file)
  73. if UI.FileExists(local_menu_file) {
  74. self.Img = image.Load(local_menu_file)
  75. self.Screen.Draw()
  76. self.Screen.SwapAndShow()
  77. }else {
  78. self.req,_ = grab.NewRequest("/tmp",self.URL)
  79. self.resp = self.Downloader.Do(self.req)
  80. for len(self.Downloading) > 0 {
  81. <-self.Downloading
  82. }
  83. self.Downloading <- true
  84. go self.UpdateProcessInterval(400)
  85. }
  86. }
  87. }
  88. func (self *ImageDownloadProcessPage) UpdateProcessInterval(ms int) {
  89. t := gotime.NewTicker(gotime.Duration(ms) * gotime.Millisecond)
  90. defer t.Stop()
  91. for {
  92. select {
  93. case <-t.C:
  94. fmt.Printf(" transferred %v / %v bytes (%.2f%%)\n",
  95. self.resp.BytesComplete(),
  96. self.resp.Size,
  97. 100*self.resp.Progress())
  98. case <-self.resp.Done:
  99. // download is complete
  100. break
  101. case v:= <-self.Downloading:
  102. if v == false {
  103. t.Stop()
  104. break
  105. }
  106. }
  107. }
  108. dst_filename := self.resp.Filename
  109. if err := self.resp.Err(); err == nil {//download successfully
  110. home_path, _ := os.UserHomeDir()
  111. parts := strings.Split(self.URL,"/")
  112. filename := strings.TrimSpace(parts[len(parts)-1])
  113. local_dir := strings.Split(self.URL,"raw.githubusercontent.com")
  114. local_menu_file := ""
  115. menu_file := ""
  116. if len(local_dir) > 1 {
  117. menu_file = local_dir[1]
  118. local_menu_file = fmt.Sprintf("%s/aria2downloads%s",
  119. home_path,menu_file)
  120. }
  121. dl_file := path.Join("/tmp",filename)
  122. if UI.IsDirectory( path.Base(local_menu_file) ) == false {
  123. merr := os.MkdirAll( path.Base(local_menu_file), os.ModePerm)
  124. if merr != nil {
  125. panic(merr)
  126. }
  127. }
  128. UI.CopyFile(dl_file,local_menu_file)
  129. }
  130. if UI.FileExists(dst_filename) {
  131. if self.Screen.CurPage() == self {
  132. self.Img = image.Load(dst_filename)
  133. self.Screen.Draw()
  134. self.Screen.SwapAndShow()
  135. }
  136. }
  137. }
  138. func (self *ImageDownloadProcessPage) KeyDown(ev *event.Event) {
  139. if UI.IsKeyMenuOrB(ev.Data["Key"]) {
  140. self.Downloading <- false
  141. self.ReturnToUpLevelPage()
  142. self.Screen.Draw()
  143. self.Screen.SwapAndShow()
  144. self.URL = ""
  145. }
  146. }
  147. func (self *ImageDownloadProcessPage) Draw() {
  148. self.ClearCanvas()
  149. w,_ := self.LoadingLabel.Size()
  150. self.LoadingLabel.NewCoord( (UI.Width - w)/2,(UI.Height-44)/2);
  151. self.LoadingLabel.Draw()
  152. if self.Img != nil {
  153. surface.Blit(self.CanvasHWND,
  154. self.Img,
  155. draw.MidRect(UI.Width/2,(UI.Height-44)/2,int(self.Img.W),int(self.Img.H),UI.Width,UI.Height-44),
  156. nil)
  157. }
  158. }