Subversion Repositories Kolibri OS

Rev

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