Subversion Repositories Kolibri OS

Rev

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

  1. //Copyright 2020 by Leency
  2.  
  3. #define MEMSIZE 1024 * 100
  4. #include "../lib/gui.h"
  5. #include "../lib/random.h"
  6. #include "../lib/obj/box_lib.h"
  7. #include "../lib/obj/http.h"
  8. #include "../lib/patterns/http_downloader.h"
  9.  
  10. #include "const.h"
  11.  
  12. DOWNLOADER downloader;
  13.  
  14. char downloader_edit[4000];
  15. char filepath[4096];
  16. edit_box ed = {WIN_W-GAPX-GAPX,GAPX,20,0xffffff,0x94AECE,0xffffff,0xffffff,0x10000000,
  17.         sizeof(downloader_edit)-2,#downloader_edit,0,ed_focus,19,19};
  18. progress_bar pb = {0, GAPX, 58, 350, 17, 0, 0, 100, 0xFFFfff, 0x74DA00, 0x9F9F9F};
  19. //progress_bar: value, left, top, width, height, style, min, max, back_color, progress_color, frame_color;
  20.  
  21. bool exit_when_done = false;
  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_to)) CreateDir(#save_to);
  31.  
  32.         if (param) {
  33.                 if (!strncmp(#param, "-exit ", 6)) {
  34.                         exit_when_done = true;
  35.                         param += 6;
  36.                 }
  37.  
  38.                 if (!strncmp(#param, "-mem", 5)) {
  39.                         shared_url = memopen(#dl_shared, URL_SIZE+1, SHM_OPEN + SHM_WRITE);
  40.                         strcpy(#downloader_edit, shared_url);
  41.                 } else {
  42.                         strcpy(#downloader_edit, #param);
  43.                 }
  44.         }
  45.         if (downloader_edit[0]) StartDownloading(); else strcpy(#downloader_edit, "http://");
  46.         ed.size = ed.pos = ed.shift = ed.shift_old = strlen(#downloader_edit);
  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:
  53.                         edit_box_mouse stdcall (#ed);
  54.                         break;
  55.  
  56.                 case evButton:
  57.                         ProcessEvent(GetButtonID());
  58.                         break;
  59.  
  60.                 case evKey:
  61.                         GetKeys();
  62.                         edit_box_key stdcall(#ed);
  63.                         if (key_scancode==SCAN_CODE_ENTER) ProcessEvent(301);
  64.                         break;
  65.  
  66.                 case evReDraw:
  67.                         DrawWindow();
  68.                         break;
  69.                    
  70.                 default:
  71.                         if (!downloader.MonitorProgress()) break;
  72.                         pb.max = downloader.httpd.content_length / 100;
  73.                         EDI = downloader.httpd.content_received/100;
  74.                         if (pb.value != EDI)
  75.                         {
  76.                                 pb.value = EDI;
  77.                                 progressbar_draw stdcall(#pb);
  78.                                 DrawDownloading();
  79.                         }
  80.                         if (downloader.state == STATE_COMPLETED)
  81.                         {
  82.                                 SaveDownloadedFile();
  83.                                 if (exit_when_done) ExitProcess();
  84.                                 StopDownloading();
  85.                                 DrawWindow();
  86.                                 break;
  87.                         }          
  88.         }
  89. }
  90.  
  91. void ProcessEvent(int id)
  92. {
  93.         if (id==001) { StopDownloading(); ExitProcess(); }
  94.         if (id==301) && (downloader.httpd.transfer <= 0) StartDownloading();
  95.         if (id==302) StopDownloading();
  96.         if (id==305) RunProgram("/sys/File managers/Eolite", #filepath);
  97.         if (id==306) {
  98.                 SetCurDir(#save_to);
  99.                 RunProgram("/sys/@open", #filepath);
  100.         }
  101. }
  102.  
  103. void DrawWindow()
  104. {  
  105.         int but_x = 0;
  106.         int but_y = 58;
  107.  
  108.         sc.get();
  109.         pb.frame_color = sc.work_dark;
  110.         DefineAndDrawWindow(110 + random(300), 100 + random(300), WIN_W+9, WIN_H + 5 + skin_height, 0x34, sc.work, DL_WINDOW_HEADER, 0);
  111.  
  112.         if (downloader.state == STATE_NOT_STARTED) || (downloader.state == STATE_COMPLETED)
  113.         {
  114.                 but_x = GAPX + DrawStandartCaptButton(GAPX, but_y, 301, START_DOWNLOADING);  
  115.                 if (filepath[0])
  116.                 {
  117.                         but_x += DrawStandartCaptButton(but_x, but_y, 305, SHOW_IN_FOLDER);
  118.                         DrawStandartCaptButton(but_x, but_y, 306, OPEN_FILE_TEXT);  
  119.                 }
  120.         }
  121.         if (downloader.state == STATE_IN_PROGRESS)
  122.         {
  123.                 DrawStandartCaptButton(WIN_W - 190, but_y, 302, STOP_DOWNLOADING);
  124.                 DrawDownloading();
  125.         }
  126.         ed.offset=0;
  127.         DrawEditBox(#ed);
  128. }
  129.  
  130. void StartDownloading()
  131. {
  132.         char http_url[URL_SIZE];
  133.         char proxy_url[URL_SIZE];
  134.         StopDownloading();
  135.         if (!strncmp(#downloader_edit,"https://",7)) {
  136.                 notify("'HTTPS for download is not supported, trying to download the file via HTTP' -W");
  137.                 miniprintf(#http_url, "http://%s", #downloader_edit+8);
  138.                 if (!downloader.Start(#http_url)) {
  139.                         notify("'Download failed.' -E");
  140.                         StopDownloading();
  141.                 }
  142.                 //sprintf(#proxy_url, "http://gate.aspero.pro/?site=%s", #downloader_edit);
  143.                 //if (!downloader.Start(#proxy_url)) {
  144.                 //      notify("'Download failed.' -E");
  145.                 //      StopDownloading();
  146.                 //}
  147.                 DrawWindow();
  148.                 return;
  149.         }
  150.         if (!downloader.Start(#downloader_edit)) {
  151.                 if (exit_when_done) ExitProcess();
  152.                 notify(T_ERROR_STARTING_DOWNLOAD);
  153.                 StopDownloading();
  154.                 return;
  155.         }
  156.         ed.blur_border_color = 0xCACACA;
  157.         ed.flags = 100000000000b;
  158.         pb.value = 0;
  159.         DrawWindow();
  160. }
  161.  
  162. /*
  163. struct TIME
  164. {
  165.         dword old;
  166.         dword cur;
  167.         dword gone;
  168. } time = {0,0,0};
  169.  
  170. dword netdata_received;
  171. dword speed;
  172.  
  173. void CalculateSpeed()
  174. {
  175.         time.cur = GetStartTime();
  176.  
  177.         if (time.old) {
  178.                 time.gone = time.cur - time.old;
  179.                 if (time.gone > 200) {
  180.                         speed = downloader.httpd.content_received - netdata_received / time.gone * 100;
  181.                         debugval("speed", speed);
  182.                         debugln(ConvertSizeToKb(speed) );
  183.                         time.old = time.cur;
  184.                         netdata_received = downloader.httpd.content_received;
  185.                 }
  186.         }
  187.         else time.old = time.cur;
  188. }
  189. */
  190.  
  191. void DrawDownloading()
  192. {
  193.         char bytes_received[70];
  194.         miniprintf(#bytes_received, KB_RECEIVED, ConvertSizeToKb(downloader.httpd.content_received) );
  195.         WriteTextWithBg(GAPX, pb.top + 22, 0xD0, sc.work_text, #bytes_received, sc.work);
  196.         //CalculateSpeed();
  197.         progressbar_draw stdcall(#pb);
  198. }
  199.  
  200. void StopDownloading()
  201. {
  202.         downloader.Stop();
  203.         ed.blur_border_color = 0xFFFfff;
  204.         ed.flags = 10b;
  205.         DrawWindow();
  206. }
  207.  
  208. void SaveDownloadedFile()
  209. {
  210.         int i;
  211.         char aux[2048];
  212.         char notify_message[4296];
  213.  
  214.         // Clean all slashes at the end
  215.         strcpy(#aux,  #downloader_edit);
  216.         while (aux[strlen(#aux)-1] == '/') {
  217.                 aux[strlen(#aux)-1] = 0;
  218.         }
  219.  
  220.         //miniprintf(#filepath, "%s/", #save_to);
  221.         strcpy(#filepath, #save_to);
  222.         chrcat(#filepath, '/');
  223.         strcat(#filepath, #aux+strrchr(#aux, '/'));
  224.        
  225.         for (i=0; i<strlen(#filepath); i++) if(filepath[i]==':')||(filepath[i]=='?')filepath[i]='-';
  226.  
  227.         if (CreateFile(downloader.httpd.content_received, downloader.bufpointer, #filepath)==0) {
  228.                 miniprintf(#notify_message, FILE_SAVED_AS, #filepath);
  229.         } else {
  230.                 miniprintf(#notify_message, FILE_NOT_SAVED, #filepath);
  231.         }
  232.  
  233.         /*
  234.         if (CreateFile(downloader.httpd.content_received, downloader.bufpointer, #filepath)==0) {
  235.                 strcpy(#notify_message, "'Download complete' -Dt");
  236.         } else {
  237.                 strcpy(#notify_message, "'Error saving downloaded file!' -Et");
  238.         }
  239.         */
  240.        
  241.         if (!exit_when_done) notify(#notify_message);
  242. }