Subversion Repositories Kolibri OS

Rev

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

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