Subversion Repositories Kolibri OS

Rev

Rev 7746 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. DOWNLOADER downloader;
  2.  
  3. #ifdef LANG_RUS
  4.         #define DL_WINDOW_HEADER "Œ¥­¥¤¦¥à § £à㧮ª"
  5.         #define START_DOWNLOADING " ç âì § ª çªã"
  6.         #define STOP_DOWNLOADING "Žáâ ­®¢¨âì"
  7.         #define SHOW_IN_FOLDER "®ª § âì ¢ ¯ ¯ª¥"
  8.         #define OPEN_FILE_TEXT "Žâªàëâì ä ©«"
  9.         #define FILE_SAVED_AS "'Œ¥­¥¤¦¥à § £à㧮ª\n” ©« á®åà ­¥­ ª ª "
  10.         #define KB_RECEIVED "ˆ¤¥â ᪠稢 ­¨¥... %s ¯®«ã祭®"
  11. #else
  12.         #define DL_WINDOW_HEADER "Download Manager"
  13.         #define START_DOWNLOADING "Start downloading"
  14.         #define STOP_DOWNLOADING "Stop downloading"
  15.         #define SHOW_IN_FOLDER "Show in folder"
  16.         #define OPEN_FILE_TEXT "Open file"
  17.         #define FILE_SAVED_AS "'Download manager\nFile saved as "
  18.         #define KB_RECEIVED "Downloading... %s received"
  19. #endif
  20. char save_to[4096] = "/tmp0/1/Downloads";
  21.  
  22. #define CONX 15
  23.  
  24. proc_info DL_Form;
  25. char downloader_edit[10000];
  26. char filepath[4096];
  27. edit_box ed = {NULL,57,20,0xffffff,0x94AECE,0xffffff,0xffffff,0x10000000,sizeof(downloader_edit)-2,#downloader_edit,0,ed_focus,19,19};
  28. progress_bar pb = {0, CONX, 58, 350, 17, 0, 0, 100, 0xFFFfff, 0x74DA00, 0x9F9F9F};
  29. //progress_bar pb = {0, 180, 55, 225, 12, 0, 0, 100, 0xFFFfff, 0x74DA00, 0x9F9F9F};
  30. //progress_bar: value, left, top, width, height, style, min, max, back_color, progress_color, frame_color;
  31.  
  32.        
  33.  
  34. bool downloader_opened;
  35. char downloader_stak[4096];
  36.  
  37.  
  38.  
  39. void Downloader()  
  40. {
  41.         if (!dir_exists(#save_to)) CreateDir(#save_to);
  42.         downloader_opened = true;
  43.         SetEventMask(EVM_REDRAW + EVM_KEY + EVM_BUTTON + EVM_MOUSE + EVM_MOUSE_FILTER + EVM_STACK);
  44.  
  45.         system.color.get();
  46.         pb.frame_color = system.color.work_dark;
  47.  
  48.         filepath[0] = NULL;
  49.        
  50.         downloader.Stop();
  51.         if (downloader_edit[0]) StartDownloading(); else strcpy(#downloader_edit, "http://");
  52.         ed.size = ed.pos = ed.shift = ed.shift_old = strlen(#downloader_edit);
  53.  
  54.         loop() switch(WaitEvent())
  55.         {
  56.                 case evMouse:
  57.                         edit_box_mouse stdcall (#ed);
  58.                         break;
  59.  
  60.                 case evButton:
  61.                         Key_Scan(GetButtonID());
  62.                         break;
  63.  
  64.                 case evKey:
  65.                         GetKeys();
  66.                         edit_box_key stdcall(#ed);
  67.                         if (key_scancode==SCAN_CODE_ENTER) Key_Scan(301);
  68.                         break;
  69.  
  70.                 case evReDraw:
  71.                         DefineAndDrawWindow(215, 100, 580, 130, 0x74, system.color.work, DL_WINDOW_HEADER, 0);
  72.                         GetProcessInfo(#DL_Form, SelfInfo);
  73.                         if (DL_Form.status_window>2) break;
  74.                         if (DL_Form.height<120) MoveSize(OLD,OLD,OLD,120);
  75.                         if (DL_Form.width<280) MoveSize(OLD,OLD,280,OLD);
  76.                         DL_Draw_Window();
  77.                         break;
  78.                    
  79.                 default:
  80.                         if (!downloader.MonitorProgress()) break;
  81.                         pb.max = downloader.httpd.content_length / 100;
  82.                         EDI = downloader.httpd.content_received/100;
  83.                         if (pb.value != EDI)
  84.                         {
  85.                                 pb.value = EDI;
  86.                                 progressbar_draw stdcall(#pb);
  87.                                 DrawDownloading();
  88.                         }
  89.                         if (downloader.state == STATE_COMPLETED)
  90.                         {
  91.                                 SaveDownloadedFile();
  92.                                 if (download_and_exit) ExitProcess();
  93.                                 StopDownloading();
  94.                                 DL_Draw_Window();
  95.                                 break;
  96.                         }          
  97.         }
  98. }
  99.  
  100. void Key_Scan(int id)
  101. {
  102.         if (id==001) { downloader_opened=false; StopDownloading(); ExitProcess(); }
  103.         if (id==301) && (downloader.httpd.transfer <= 0) StartDownloading();
  104.         if (id==302) StopDownloading();
  105.         if (id==305) RunProgram("/sys/File managers/Eolite", #save_to);
  106.         if (id==306) {
  107.                 SetCurDir(#save_to);
  108.                 RunProgram("/sys/@open", #filepath);
  109.         }
  110. }
  111.  
  112. void DL_Draw_Window()
  113. {  
  114.         int but_x = 0;
  115.         int but_y = 58;
  116.         DrawBar(0,0, DL_Form.cwidth, DL_Form.cheight, system.color.work);
  117.         DeleteButton(301);
  118.         DeleteButton(302);
  119.         DeleteButton(305);
  120.         DeleteButton(306);
  121.         if (downloader.state == STATE_NOT_STARTED) || (downloader.state == STATE_COMPLETED)
  122.         {
  123.                 but_x = CONX + DrawStandartCaptButton(CONX, but_y, 301, START_DOWNLOADING);  
  124.                 if (filepath[0])
  125.                 {
  126.                         but_x += DrawStandartCaptButton(but_x, but_y, 305, SHOW_IN_FOLDER);
  127.                         DrawStandartCaptButton(but_x, but_y, 306, OPEN_FILE_TEXT);  
  128.                 }
  129.         }
  130.         if (downloader.state == STATE_IN_PROGRESS)
  131.         {
  132.                 DrawStandartCaptButton(DL_Form.width - 190, but_y, 302, STOP_DOWNLOADING);
  133.                 DrawDownloading();
  134.         }
  135.         WriteText(CONX, ed.top + 4, 0x90, system.color.work_text, "URL:");
  136.     ed.width = DL_Form.cwidth - ed.left - CONX - 3;
  137.         ed.offset=0;
  138.         DrawEditBox(#ed);
  139. }
  140.  
  141. void StartDownloading()
  142. {
  143.         StopDownloading();
  144.         if (strncmp(#downloader_edit,"http://",7)!=0) {
  145.                 notify("'File address should start from http://' -E");
  146.                 return;
  147.         }
  148.         if (!downloader.Start(#downloader_edit)) {
  149.                 if (download_and_exit) ExitProcess();
  150.                 notify("'Error while starting download process.\nPlease, check entered path and internet connection.' -E");
  151.                 StopDownloading();
  152.                 return;
  153.         }
  154.         ed.blur_border_color = 0xCACACA;
  155.         ed.flags = 100000000000b;
  156.         pb.value = 0;
  157.         DL_Draw_Window();
  158. }
  159.  
  160. /*
  161. struct TIME
  162. {
  163.         dword old;
  164.         dword cur;
  165.         dword gone;
  166. } time = {0,0,0};
  167.  
  168. dword netdata_received;
  169. dword speed;
  170.  
  171. void CalculateSpeed()
  172. {
  173.         time.cur = GetStartTime();
  174.  
  175.         if (time.old) {
  176.                 time.gone = time.cur - time.old;
  177.                 if (time.gone > 200) {
  178.                         speed = downloader.httpd.content_received - netdata_received / time.gone * 100;
  179.                         debugval("speed", speed);
  180.                         debugln(ConvertSizeToKb(speed) );
  181.                         time.old = time.cur;
  182.                         netdata_received = downloader.httpd.content_received;
  183.                 }
  184.         }
  185.         else time.old = time.cur;
  186. }
  187. */
  188.  
  189. void DrawDownloading()
  190. {
  191.         char bytes_received[70];
  192.         sprintf(#bytes_received, KB_RECEIVED, ConvertSizeToKb(downloader.httpd.content_received) );
  193.         DrawBar(CONX, pb.top + 22, pb.width, 16, system.color.work);
  194.         WriteText(CONX, pb.top + 22, 0x90, system.color.work_text, #bytes_received);
  195.         //CalculateSpeed();
  196.         progressbar_draw stdcall(#pb);
  197. }
  198.  
  199. void StopDownloading()
  200. {
  201.         downloader.Stop();
  202.         ed.blur_border_color = 0xFFFfff;
  203.         ed.flags = 10b;
  204.         DL_Draw_Window();
  205. }
  206.  
  207. void SaveDownloadedFile()
  208. {
  209.         int i;
  210.         char aux[2048];
  211.         char notify_message[4296];
  212.  
  213.         // Clean all slashes at the end
  214.         strcpy(#aux,  #downloader_edit);
  215.         while (aux[strlen(#aux)-1] == '/') {
  216.                 aux[strlen(#aux)-1] = 0;
  217.         }
  218.         sprintf(#filepath, "%s/%s", #save_to, #aux+strrchr(#aux, '/'));
  219.        
  220.         for (i=0; i<strlen(#filepath); i++) if(filepath[i]==':')||(filepath[i]=='?')filepath[i]='-';
  221.  
  222.         if (CreateFile(downloader.httpd.content_received, downloader.bufpointer, #filepath)==0) {
  223.                 sprintf(#notify_message, "%s%s%s",FILE_SAVED_AS,#filepath,"' -Dt");
  224.         } else {
  225.                 sprintf(#notify_message, "%s%s%s","'Download manager\nError! Can\96t save file as ",#filepath,"' -Et");
  226.         }
  227.        
  228.         if (!download_and_exit) notify(#notify_message);
  229. }