Subversion Repositories Kolibri OS

Rev

Rev 8281 | Rev 8305 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. //Copyright 2020 by Leency
  2.  
  3. #define MEMSIZE 1024 * 100
  4. #include "../lib/gui.h"
  5. #include "../lib/random.h"
  6.  
  7. #include "../lib/obj/box_lib.h"
  8. #include "../lib/obj/http.h"
  9.  
  10. #include "../lib/patterns/http_downloader.h"
  11.  
  12. #include "const.h"
  13.  
  14. DOWNLOADER downloader;
  15. checkbox autoclose = { T_AUTOCLOSE, false };
  16.  
  17. char downloader_edit[4000];
  18. char filepath[4096];
  19. edit_box ed = {WIN_W-GAPX-GAPX,GAPX,20,0xffffff,0x94AECE,0xffffff,0xffffff,0x10000000,
  20.         sizeof(downloader_edit)-2,#downloader_edit,0,ed_focus,19,19};
  21. progress_bar pb = {0, GAPX, 58, 315, 17, 0, 0, 100, 0xFFFfff, 0x74DA00, 0x9F9F9F};
  22. //progress_bar: value, left, top, width, height, style, min, max, back_color, progress_color, frame_color;
  23.  
  24. bool exit_when_done = false;
  25.  
  26.  
  27. void main()  
  28. {
  29.         dword shared_url;
  30.         load_dll(boxlib,  #box_lib_init,0);
  31.         load_dll(libHTTP, #http_lib_init,1);
  32.  
  33.         if (!dir_exists(#save_to)) CreateDir(#save_to);
  34.  
  35.         if (param) {
  36.                 if (!strncmp(#param, "-exit ", 6)) {
  37.                         exit_when_done = true;
  38.                         param += 6;
  39.                 }
  40.  
  41.                 if (!strncmp(#param, "-mem", 5)) {
  42.                         //shared_url = memopen(#dl_shared, URL_SIZE+1, SHM_OPEN + SHM_WRITE);
  43.                         strcpy(#downloader_edit, shared_url);
  44.                 } else {
  45.                         strcpy(#downloader_edit, #param);
  46.                 }
  47.         }
  48.         if (downloader_edit[0]) StartDownloading(); else strcpy(#downloader_edit, "http://");
  49.         ed.size = ed.pos = ed.shift = ed.shift_old = strlen(#downloader_edit);
  50.  
  51.         @SetEventMask(EVM_REDRAW + EVM_KEY + EVM_BUTTON + EVM_MOUSE + EVM_MOUSE_FILTER + EVM_STACK);
  52.         @SetWindowLayerBehaviour(-1, ZPOS_ALWAYS_TOP);
  53.         loop() switch(@WaitEvent())
  54.         {
  55.                 case evMouse:
  56.                         edit_box_mouse stdcall (#ed);
  57.                         break;
  58.  
  59.                 case evButton:
  60.                         ProcessEvent(GetButtonID());
  61.                         break;
  62.  
  63.                 case evKey:
  64.                         GetKeys();
  65.                         edit_box_key stdcall(#ed);
  66.                         if (key_scancode==SCAN_CODE_ENTER) ProcessEvent(301);
  67.                         break;
  68.  
  69.                 case evReDraw:
  70.                         DrawWindow();
  71.                         break;
  72.                    
  73.                 default:
  74.                         if (!downloader.MonitorProgress()) break;
  75.                         pb.max = downloader.content_length / 100;
  76.                         EDI = downloader.content_received/100;
  77.                         if (pb.value != EDI)
  78.                         {
  79.                                 pb.value = EDI;
  80.                                 progressbar_draw stdcall(#pb);
  81.                                 DrawDownloading();
  82.                         }
  83.                         if (downloader.state == STATE_COMPLETED)
  84.                         {
  85.                                 SaveDownloadedFile();
  86.                                 if (exit_when_done) ExitProcess();
  87.                                 StopDownloading();
  88.                                 DrawWindow();
  89.                                 break;
  90.                         }          
  91.         }
  92. }
  93.  
  94. void ProcessEvent(int id)
  95. {
  96.         autoclose.click(id);
  97.         if (id==001) { StopDownloading(); ExitProcess(); }
  98.         if (id==301) && (downloader.transfer <= 0) StartDownloading();
  99.         if (id==302) StopDownloading();
  100.         if (id==305) RunProgram("/sys/File managers/Eolite", #filepath);
  101.         if (id==306) {
  102.                 SetCurDir(#save_to);
  103.                 RunProgram("/sys/@open", #filepath);
  104.         }
  105. }
  106.  
  107. void DrawWindow()
  108. {  
  109.         int but_x = 0;
  110.         #define BUT_Y 58;
  111.  
  112.         sc.get();
  113.         pb.frame_color = sc.work_dark;
  114.         DefineAndDrawWindow(110 + random(300), 100 + random(300), WIN_W+9, WIN_H + 5 + skin_height, 0x34, sc.work, DL_WINDOW_HEADER, 0);
  115.  
  116.         autoclose.draw(WIN_W-135, BUT_Y+6);
  117.        
  118.         if (downloader.state == STATE_NOT_STARTED) || (downloader.state == STATE_COMPLETED)
  119.         {
  120.                 but_x = GAPX + DrawStandartCaptButton(GAPX, BUT_Y, 301, START_DOWNLOADING);  
  121.                 if (filepath[0])
  122.                 {
  123.                         but_x += DrawStandartCaptButton(but_x, BUT_Y, 305, SHOW_IN_FOLDER);
  124.                         DrawStandartCaptButton(but_x, BUT_Y, 306, OPEN_FILE_TEXT);  
  125.                 }
  126.         }
  127.         if (downloader.state == STATE_IN_PROGRESS)
  128.         {
  129.                 DrawStandartCaptButton(WIN_W - 240, BUT_Y, 302, STOP_DOWNLOADING);
  130.                 DrawDownloading();
  131.         }
  132.         ed.offset=0;
  133.         DrawEditBox(#ed);
  134. }
  135.  
  136. void StartDownloading()
  137. {
  138.         char http_url[URL_SIZE];
  139.         char proxy_url[URL_SIZE];
  140.         StopDownloading();
  141.         if (!strncmp(#downloader_edit,"https://",7)) {
  142.                 //notify("'HTTPS for download is not supported, trying to download the file via HTTP' -W");
  143.                 //miniprintf(#http_url, "http://%s", #downloader_edit+8);
  144.                 //if (!downloader.Start(#http_url)) {
  145.                 //      notify("'Download failed.' -E");
  146.                 //      StopDownloading();
  147.                 //}
  148.                 miniprintf(#proxy_url, "http://gate.aspero.pro/?site=%s", #downloader_edit);
  149.                 if (!downloader.Start(#proxy_url)) {
  150.                         notify("'Download failed.' -E");
  151.                         StopDownloading();
  152.                 }
  153.                 DrawWindow();
  154.                 return;
  155.         }
  156.         if (!downloader.Start(#downloader_edit)) {
  157.                 if (exit_when_done) ExitProcess();
  158.                 notify(T_ERROR_STARTING_DOWNLOAD);
  159.                 StopDownloading();
  160.                 return;
  161.         }
  162.         ed.blur_border_color = 0xCACACA;
  163.         ed.flags = 100000000000b;
  164.         pb.value = 0;
  165.         DrawWindow();
  166. }
  167.  
  168. /*
  169. struct TIME
  170. {
  171.         dword old;
  172.         dword cur;
  173.         dword gone;
  174. } time = {0,0,0};
  175.  
  176. dword netdata_received;
  177. dword speed;
  178.  
  179. void CalculateSpeed()
  180. {
  181.         time.cur = GetStartTime();
  182.  
  183.         if (time.old) {
  184.                 time.gone = time.cur - time.old;
  185.                 if (time.gone > 200) {
  186.                         speed = downloader.content_received - netdata_received / time.gone * 100;
  187.                         debugval("speed", speed);
  188.                         debugln(ConvertSizeToKb(speed) );
  189.                         time.old = time.cur;
  190.                         netdata_received = downloader.content_received;
  191.                 }
  192.         }
  193.         else time.old = time.cur;
  194. }
  195. */
  196.  
  197. void DrawDownloading()
  198. {
  199.         char bytes_received[70];
  200.         miniprintf(#bytes_received, KB_RECEIVED, ConvertSizeToKb(downloader.content_received) );
  201.         WriteTextWithBg(GAPX, pb.top + 22, 0xD0, sc.work_text, #bytes_received, sc.work);
  202.         //CalculateSpeed();
  203.         progressbar_draw stdcall(#pb);
  204. }
  205.  
  206. void StopDownloading()
  207. {
  208.         downloader.Stop();
  209.         ed.blur_border_color = 0xFFFfff;
  210.         ed.flags = 10b;
  211.         DrawWindow();
  212. }
  213.  
  214. void SaveDownloadedFile()
  215. {
  216.         int i;
  217.         char aux[2048];
  218.         char notify_message[4296];
  219.  
  220.         // Clean all slashes at the end
  221.         strcpy(#aux,  #downloader_edit);
  222.         while (aux[strlen(#aux)-1] == '/') {
  223.                 aux[strlen(#aux)-1] = 0;
  224.         }
  225.  
  226.         //miniprintf(#filepath, "%s/", #save_to);
  227.         strcpy(#filepath, #save_to);
  228.         chrcat(#filepath, '/');
  229.         strcat(#filepath, #aux+strrchr(#aux, '/'));
  230.        
  231.         for (i=0; i<strlen(#filepath); i++) if(filepath[i]==':')||(filepath[i]=='?')filepath[i]='-';
  232.  
  233.         if (CreateFile(downloader.content_received, downloader.bufpointer, #filepath)==0) {
  234.                 miniprintf(#notify_message, FILE_SAVED_AS, #filepath);
  235.         } else {
  236.                 miniprintf(#notify_message, FILE_NOT_SAVED, #filepath);
  237.         }
  238.  
  239.         /*
  240.         if (CreateFile(downloader.content_received, downloader.bufpointer, #filepath)==0) {
  241.                 strcpy(#notify_message, "'Download complete' -Dt");
  242.         } else {
  243.                 strcpy(#notify_message, "'Error saving downloaded file!' -Et");
  244.         }
  245.         */
  246.        
  247.         if (!exit_when_done) notify(#notify_message);
  248.         if (autoclose.checked) ExitProcess();
  249. }