Subversion Repositories Kolibri OS

Rev

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