image_download_process_page.go 4.4 KB

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