Subversion Repositories Kolibri OS

Rev

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

  1. #define MEMSIZE 1024 * 40
  2. //Copyright 2020 - 2021 by Leency
  3. #include "../lib/gui.h"
  4. #include "../lib/random.h"
  5. #include "../lib/obj/box_lib.h"
  6. #include "../lib/obj/http.h"
  7.  
  8. #include "const.h"
  9.  
  10. bool exit_param = false;
  11. bool open_file = false;
  12.  
  13. dword speed;
  14.  
  15. _http http;
  16.  
  17. checkbox autoclose = { T_AUTOCLOSE, false };
  18.  
  19. char uEdit[URL_SIZE];
  20. char filepath[URL_SIZE+96];
  21.  
  22. progress_bar pb = {0, GAPX, 58, 380, 17, 0, NULL, NULL, 0xFFFfff, 0x74DA00, NULL};
  23. edit_box ed = {WIN_W-GAPX-GAPX,GAPX,20,0xffffff,0x94AECE,0xffffff,0xffffff,
  24.         0x10000000, sizeof(uEdit)-2,#uEdit,0,ed_focus,19,19};
  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_dir)) CreateDir(#save_dir);
  34.         SetCurDir(#save_dir);
  35.  
  36.         if (param) {
  37.                 if (streqrp(#param, "-e ")) {
  38.                         exit_param = true;
  39.                         param += 3;
  40.                 }
  41.                 if (streqrp(#param, "-eo ")) {
  42.                         exit_param = true;
  43.                         open_file = true;
  44.                         param += 4;
  45.                 }
  46.  
  47.                 if (!strncmp(#param, "-mem", 5)) {
  48.                         //shared_url = memopen(#dl_shared, URL_SIZE+1, SHM_OPEN + SHM_WRITE);
  49.                         strcpy(#uEdit, shared_url);
  50.                 } else {
  51.                         strcpy(#uEdit, #param);
  52.                 }
  53.  
  54.                 if (streq(#param, "-test")) {
  55.                         strcpy(#uEdit, URL_SPEED_TEST);
  56.                 }
  57.         }
  58.         if (uEdit[0]) StartDownloading(); else {
  59.                 edit_box_set_text stdcall (#ed, "http://");
  60.         }
  61.  
  62.         @SetEventMask(EVM_REDRAW + EVM_KEY + EVM_BUTTON + EVM_MOUSE + EVM_MOUSE_FILTER + EVM_STACK);
  63.         loop() switch(@WaitEvent())
  64.         {
  65.                 case evMouse:  edit_box_mouse stdcall (#ed); break;
  66.                 case evButton: ProcessButtonClick(@GetButtonID()); break;
  67.                 case evKey:    ProcessKeyPress(); break;
  68.                 case evReDraw: DrawWindow(); break;
  69.                 default:       MonitorProgress();
  70.         }
  71. }
  72.  
  73. void ProcessButtonClick(int id)
  74. {
  75.         autoclose.click(id);
  76.         if (id==BTN_EXIT) { StopDownloading(); ExitProcess(); }
  77.         if (id==BTN_START) StartDownloading();
  78.         if (id==BTN_STOP) StopDownloading();
  79.         if (id==BTN_DIR) { RunProgram("/sys/File managers/Eolite", #filepath); ExitProcess(); }
  80.         if (id==BTN_RUN) { RunProgram("/sys/@open", #filepath); ExitProcess(); }
  81. }
  82.  
  83. void ProcessKeyPress()
  84. {
  85.         @GetKey();
  86.         edit_box_key stdcall(#ed);
  87.         EAX >>= 16;
  88.         if (AL == SCAN_CODE_ENTER) StartDownloading();
  89.         if (AL == SCAN_CODE_ESC) StopDownloading();
  90. }
  91.  
  92. void DrawWindow()
  93. {  
  94.         sc.get();
  95.         pb.frame_color = sc.dark;
  96.         DefineAndDrawWindow(110 + random(300), 100 + random(300), WIN_W+9,
  97.                 WIN_H + 5 + skin_h, 0x34, sc.work, DL_WINDOW_HEADER, 0);
  98.  
  99.         #define BUT_Y 58;
  100.         //autoclose.draw(WIN_W-135, BUT_Y+6);
  101.         if (!http.transfer)
  102.         {
  103.                 DrawStandartCaptButton(GAPX, BUT_Y, BTN_START, T_DOWNLOAD);  
  104.                 if (filepath)
  105.                 {
  106.                         DrawStandartCaptButton(GAPX+102, BUT_Y, BTN_DIR, T_OPEN_DIR);
  107.                         DrawStandartCaptButton(GAPX+276, BUT_Y, BTN_RUN, T_RUN);  
  108.                 }
  109.         } else {
  110.                 DrawStandartCaptButton(WIN_W - 120, BUT_Y, BTN_STOP, T_CANCEL);
  111.                 DrawDownloadingProgress();
  112.         }
  113.         //ed.offset=0; //DEL?
  114.         DrawEditBox(#ed);
  115. }
  116.  
  117. void StartDownloading()
  118. {
  119.         char get_url[URL_SIZE+33];
  120.         if (http.transfer > 0) return;
  121.         ResetDownloadSpeed();
  122.         filepath = '\0';
  123.         if (!strncmp(#uEdit,"https:",6)) {
  124.                 miniprintf(#get_url, "http://gate.aspero.pro/?site=%s", #uEdit);
  125.                 //notify("'HTTPS for download is not supported, trying to download the file via HTTP' -W");
  126.                 //miniprintf(#http_url, "http://%s", #uEdit+8);
  127.                 //if (!downloader.Start(#http_url)) {
  128.                 //      notify("'Download failed.' -E");
  129.                 //      StopDownloading();
  130.                 //}
  131.         } else {
  132.                 strcpy(#get_url, #uEdit);
  133.         }
  134.         if (http.get(#get_url)) {
  135.                 ed.blur_border_color = 0xCACACA;
  136.                 EditBox_UpdateText(#ed, ed_disabled);
  137.                 pb.value = 0;
  138.                 DrawWindow();
  139.         } else {
  140.                 notify(T_ERROR_STARTING_DOWNLOAD);
  141.                 StopDownloading();
  142.                 if (exit_param) ExitProcess();
  143.         }
  144. }
  145.  
  146.  
  147. void DrawDownloadingProgress()
  148. {
  149.         char bytes_received[70];
  150.         dword gotkb = http.content_received/1024;
  151.  
  152.         EDI = http.content_received / 100;
  153.         if (pb.value == EDI) return;
  154.  
  155.         pb.value = EDI;
  156.         pb.max = http.content_length / 100;
  157.         progressbar_draw stdcall(#pb);
  158.         CalculateDownloadSpeed();
  159.         sprintf(#bytes_received, KB_RECEIVED, gotkb/1024, gotkb%1024/103, speed);
  160.         WriteTextWithBg(GAPX, pb.top + 22, 0xD0, sc.work_text, #bytes_received, sc.work);
  161. }
  162.  
  163. void StopDownloading()
  164. {
  165.         http.stop();
  166.         if (http.content_pointer) http.content_pointer = free(http.content_pointer);
  167.         http.content_received = http.content_length = 0;
  168.  
  169.         ed.blur_border_color = 0xFFFfff;
  170.         EditBox_UpdateText(#ed, ed_focus);
  171.         DrawWindow();
  172. }
  173.  
  174. void MonitorProgress()
  175. {
  176.         char redirect_url[URL_SIZE];
  177.         if (http.transfer <= 0) return;
  178.         http.receive();
  179.         if (!http.content_length) http.content_length = http.content_received * 20; //MOVE?
  180.  
  181.         if (http.receive_result) {
  182.                 DrawDownloadingProgress();  
  183.         } else {
  184.                 if (http.status_code >= 300) && (http.status_code < 400) {
  185.                         http.header_field("location", #redirect_url, URL_SIZE);
  186.                         get_absolute_url(#redirect_url, #uEdit);
  187.                         edit_box_set_text stdcall (#ed, #redirect_url);
  188.                         StopDownloading();
  189.                         StartDownloading();
  190.                         return;
  191.                 }
  192.                 SaveFile(0);
  193.                 if (exit_param) ExitProcess();
  194.                 StopDownloading();
  195.                 DrawWindow();
  196.         }
  197. }
  198.  
  199. void SaveFile(int attempt)
  200. {
  201.         int i, fi=0;
  202.         char notify_message[4296];
  203.         char file_name[URL_SIZE+96];
  204.  
  205.         strcpy(#filepath, #save_dir);
  206.         chrcat(#filepath, '/');
  207.  
  208.         if (attempt > 9) {
  209.                 notify("'Too many saving attempts' -E");
  210.                 return;
  211.         }
  212.  
  213.         if (attempt > 0) {
  214.                 chrcat(#filepath, attempt+'0');
  215.                 chrcat(#filepath, '_');
  216.         }
  217.  
  218.         //Content-Disposition: attachment; filename="RealFootball_2018_Nokia_5800_EN_IGP_EU_TS_101.zip"
  219.         if (http.header_field("content-disposition", #file_name, sizeof(file_name))) {
  220.                 if (EDX = strstr(#file_name,"filename=\"")) {
  221.                         strcat(#filepath, EDX+10);
  222.                         ESBYTE[strchr(#filepath,'\"')] = '\0';
  223.                 }
  224.         } else {
  225.                 // Clean all slashes at the end
  226.                 strcpy(#file_name, #uEdit);
  227.                 while (file_name[strlen(#file_name)-1] == '/') {
  228.                         file_name[strlen(#file_name)-1] = 0;
  229.                 }
  230.                 strcat(#filepath, #file_name+strrchr(#file_name, '/'));
  231.         }
  232.        
  233.         for (i=0; i<strlen(#filepath); i++) if(filepath[i]==':')||(filepath[i]=='?')filepath[i]='-';
  234.  
  235.         while (file_exists(#filepath)) {
  236.                 SaveFile(attempt+1);
  237.                 return;
  238.         }
  239.  
  240.         if (CreateFile(http.content_received, http.content_pointer, #filepath)==0) {
  241.                 miniprintf(#notify_message, FILE_SAVED_AS, #filepath);
  242.         } else {
  243.                 miniprintf(#notify_message, FILE_NOT_SAVED, #filepath);
  244.         }
  245.  
  246.         if (!exit_param) notify(#notify_message);
  247.  
  248.         if (!strcmpi(#filepath+strlen(#filepath)-4, ".zip"))
  249.         || (!strcmpi(#filepath+strlen(#filepath)-3, ".7z")) {
  250.                 Unarchive(#filepath);
  251.         }
  252.  
  253.         if (open_file) ProcessButtonClick(BTN_RUN);
  254.         if (autoclose.checked) ExitProcess();
  255. }
  256.  
  257. void Unarchive(dword _arc)
  258. {
  259.         char folder_name[4096];
  260.         strcpy(#folder_name, "/tmp0/1/Downloads/");
  261.         strcpy(#folder_name, #filepath+strrchr(#filepath, '/'));
  262.         folder_name[strlen(#folder_name)-4] = '\0';
  263.         CreateDir(#folder_name);
  264.  
  265.         strcpy(#param, "-o \"");
  266.         strcat(#param, #folder_name);
  267.         strcat(#param, "\" -h \"");
  268.         strcat(#param, #filepath);
  269.         chrcat(#param, '\"');
  270.         RunProgram("/sys/unz", #param);
  271. }
  272.  
  273.  
  274. struct TIME
  275. {
  276.         dword old;
  277.         dword cur;
  278.         dword gone;
  279. } time = {0,0,0};
  280.  
  281. dword netdata_received;
  282.  
  283. void ResetDownloadSpeed()
  284. {
  285.         time.old = 0;
  286.         netdata_received = 0;
  287. }
  288.  
  289. void CalculateDownloadSpeed()
  290. {
  291.         time.cur = GetStartTime();
  292.  
  293.         if (time.old) {
  294.                 time.gone = time.cur - time.old;
  295.                 if (time.gone >= 200) {
  296.                         speed = http.content_received - netdata_received / time.gone / 10;
  297.                         time.old = time.cur;
  298.                         netdata_received = http.content_received;
  299.                 }
  300.         } else {
  301.                 time.old = time.cur;
  302.         }
  303. }