Subversion Repositories Kolibri OS

Rev

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