Subversion Repositories Kolibri OS

Rev

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

  1. #define MEMSIZE 1024 * 40
  2. //Copyright 2020 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.         @SetWindowLayerBehaviour(-1, ZPOS_ALWAYS_TOP);
  58.         loop() switch(@WaitEvent())
  59.         {
  60.                 case evMouse:  edit_box_mouse stdcall (#ed); break;
  61.                 case evButton: ProcessButtonClick(@GetButtonID()); break;
  62.                 case evKey:    ProcessKeyPress(); break;
  63.                 case evReDraw: DrawWindow(); break;
  64.                 default:       MonitorProgress();
  65.         }
  66. }
  67.  
  68. void ProcessButtonClick(int id)
  69. {
  70.         autoclose.click(id);
  71.         if (id==BTN_EXIT) { StopDownloading(); ExitProcess(); }
  72.         if (id==BTN_START) StartDownloading();
  73.         if (id==BTN_STOP) StopDownloading();
  74.         if (id==BTN_DIR) RunProgram("/sys/File managers/Eolite", #filepath);
  75.         if (id==BTN_RUN) RunProgram("/sys/@open", #filepath);
  76. }
  77.  
  78. void ProcessKeyPress()
  79. {
  80.         @GetKey();
  81.         edit_box_key stdcall(#ed);
  82.         EAX >>= 16;
  83.         if (AL == SCAN_CODE_ENTER) StartDownloading();
  84.         if (AL == SCAN_CODE_ESC) StopDownloading();
  85. }
  86.  
  87. void DrawWindow()
  88. {  
  89.         sc.get();
  90.         pb.frame_color = sc.work_dark;
  91.         DefineAndDrawWindow(110 + random(300), 100 + random(300), WIN_W+9,
  92.                 WIN_H + 5 + skin_height, 0x34, sc.work, DL_WINDOW_HEADER, 0);
  93.  
  94.         #define BUT_Y 58;
  95.         //autoclose.draw(WIN_W-135, BUT_Y+6);
  96.         if (!http.transfer)
  97.         {
  98.                 DrawStandartCaptButton(GAPX, BUT_Y, BTN_START, T_DOWNLOAD);  
  99.                 if (filepath)
  100.                 {
  101.                         DrawStandartCaptButton(GAPX+102, BUT_Y, BTN_DIR, T_OPEN_DIR);
  102.                         DrawStandartCaptButton(GAPX+276, BUT_Y, BTN_RUN, T_RUN);  
  103.                 }
  104.         } else {
  105.                 DrawStandartCaptButton(WIN_W - 240, BUT_Y, BTN_STOP, T_CANCEL);
  106.                 DrawDownloadingProgress();
  107.         }
  108.         //ed.offset=0; //DEL?
  109.         DrawEditBox(#ed);
  110. }
  111.  
  112. void StartDownloading()
  113. {
  114.         char get_url[URL_SIZE+33];
  115.         if (http.transfer > 0) return;
  116.         filepath = '\0';
  117.         if (!strncmp(#uEdit,"https:",6)) {
  118.                 miniprintf(#get_url, "http://gate.aspero.pro/?site=%s", #uEdit);
  119.                 //notify("'HTTPS for download is not supported, trying to download the file via HTTP' -W");
  120.                 //miniprintf(#http_url, "http://%s", #uEdit+8);
  121.                 //if (!downloader.Start(#http_url)) {
  122.                 //      notify("'Download failed.' -E");
  123.                 //      StopDownloading();
  124.                 //}
  125.         } else {
  126.                 strcpy(#get_url, #uEdit);
  127.         }
  128.         if (http.get(#get_url)) {
  129.                 ed.blur_border_color = 0xCACACA;
  130.                 EditBox_UpdateText(#ed, ed_disabled);
  131.                 pb.value = 0;
  132.                 DrawWindow();
  133.         } else {
  134.                 notify(T_ERROR_STARTING_DOWNLOAD);
  135.                 StopDownloading();
  136.                 if (exit_param) ExitProcess();
  137.         }
  138. }
  139.  
  140.  
  141. void DrawDownloadingProgress()
  142. {
  143.         char bytes_received[70];
  144.  
  145.         EDI = http.content_received / 100;
  146.         if (pb.value == EDI) return;
  147.  
  148.         pb.value = EDI;
  149.         pb.max = http.content_length / 100;
  150.         progressbar_draw stdcall(#pb);
  151.         miniprintf(#bytes_received, KB_RECEIVED, ConvertSizeToKb(http.content_received) );
  152.         WriteTextWithBg(GAPX, pb.top + 22, 0xD0, sc.work_text, #bytes_received, sc.work);
  153.         //CalculateSpeed();
  154. }
  155.  
  156. void StopDownloading()
  157. {
  158.         http.stop();
  159.         if (http.content_pointer) http.content_pointer = free(http.content_pointer);
  160.         http.content_received = http.content_length = 0;
  161.  
  162.         ed.blur_border_color = 0xFFFfff;
  163.         EditBox_UpdateText(#ed, ed_focus);
  164.         DrawWindow();
  165. }
  166.  
  167. void MonitorProgress()
  168. {
  169.         char redirect_url[URL_SIZE];
  170.         if (http.transfer <= 0) return;
  171.         http.receive();
  172.         if (!http.content_length) http.content_length = http.content_received * 20; //MOVE?
  173.  
  174.         if (http.receive_result) {
  175.                 DrawDownloadingProgress();  
  176.         } else {
  177.                 if (http.status_code >= 300) && (http.status_code < 400) {
  178.                         http.header_field("location", #redirect_url, URL_SIZE);
  179.                         get_absolute_url(#redirect_url, #uEdit);
  180.                         edit_box_set_text stdcall (#ed, #redirect_url);
  181.                         StopDownloading();
  182.                         StartDownloading();
  183.                         return;
  184.                 }
  185.                 SaveFile();
  186.                 if (exit_param) ExitProcess();
  187.                 StopDownloading();
  188.                 DrawWindow();
  189.         }
  190. }
  191.  
  192. void SaveFile()
  193. {
  194.         int i;
  195.         char notify_message[4296];
  196.         char file_name[URL_SIZE+96];
  197.  
  198.         strcpy(#filepath, #save_dir);
  199.         chrcat(#filepath, '/');
  200.  
  201.         //Content-Disposition: attachment; filename="RealFootball_2018_Nokia_5800_EN_IGP_EU_TS_101.zip"
  202.         if (http.header_field("content-disposition", #file_name, sizeof(file_name))) {
  203.                 if (EDX = strstr(#file_name,"filename=\"")) {
  204.                         strcat(#filepath, EDX+10);
  205.                         ESBYTE[strchr(#filepath,'\"')] = '\0';
  206.                 }
  207.         } else {
  208.                 // Clean all slashes at the end
  209.                 strcpy(#file_name, #uEdit);
  210.                 while (file_name[strlen(#file_name)-1] == '/') {
  211.                         file_name[strlen(#file_name)-1] = 0;
  212.                 }
  213.                 strcat(#filepath, #file_name+strrchr(#file_name, '/'));
  214.         }
  215.        
  216.         for (i=0; i<strlen(#filepath); i++) if(filepath[i]==':')||(filepath[i]=='?')filepath[i]='-';
  217.  
  218.         if (CreateFile(http.content_received, http.content_pointer, #filepath)==0) {
  219.                 miniprintf(#notify_message, FILE_SAVED_AS, #filepath);
  220.         } else {
  221.                 miniprintf(#notify_message, FILE_NOT_SAVED, #filepath);
  222.         }
  223.        
  224.         if (!exit_param) notify(#notify_message);
  225.         if (open_file) ProcessButtonClick(BTN_RUN);
  226.         if (autoclose.checked) ExitProcess();
  227. }
  228.  
  229.  
  230. /*
  231. struct TIME
  232. {
  233.         dword old;
  234.         dword cur;
  235.         dword gone;
  236. } time = {0,0,0};
  237.  
  238. dword netdata_received;
  239. dword speed;
  240.  
  241. void CalculateSpeed()
  242. {
  243.         time.cur = GetStartTime();
  244.  
  245.         if (time.old) {
  246.                 time.gone = time.cur - time.old;
  247.                 if (time.gone > 200) {
  248.                         speed = http.content_received - netdata_received / time.gone * 100;
  249.                         debugval("speed", speed);
  250.                         debugln(ConvertSizeToKb(speed) );
  251.                         time.old = time.cur;
  252.                         netdata_received = http.content_received;
  253.                 }
  254.         }
  255.         else time.old = time.cur;
  256. }
  257. */