Subversion Repositories Kolibri OS

Rev

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

  1. #ifdef LANG_RUS
  2.         #define DL_WINDOW_HEADER "Œ¥­¥¤¦¥à § £à㧮ª"
  3.         #define START_DOWNLOADING " ç âì § ª çªã"
  4.         #define STOP_DOWNLOADING "Žáâ ­®¢¨âì ᪠稢 ­¨¥"
  5.         #define SHOW_IN_FOLDER "®ª § âì ¢ ¯ ¯ª¥"
  6.         #define OPEN_FILE "Žâªàëâì ä ©«"
  7.         #define FILE_SAVED_AS "'Œ¥­¥¤¦¥à § £à㧮ª\n” ©« á®åà ­¥­ ª ª "
  8.         #define KB_RECEIVED " ¯®«ã祭®"
  9. #else
  10.         #define DL_WINDOW_HEADER "Download Manager"
  11.         #define START_DOWNLOADING "Start downloading"
  12.         #define STOP_DOWNLOADING "Stop downloading"
  13.         #define SHOW_IN_FOLDER "Show in folder"
  14.         #define OPEN_FILE "Open file"
  15.         #define FILE_SAVED_AS "'Download manager\nFile saved as "
  16.         #define KB_RECEIVED " received"
  17. #endif
  18.  
  19. proc_info DL_Form;
  20. char DL_URL[10000];
  21. dword DL_bufpointer, DL_bufsize, DL_http_transfer, DL_http_buffer;
  22. char filepath[4096];
  23. int downloaded_size, full_size;
  24. int     mouse_twbi;
  25. edit_box DL_address_box = {250,20,20,0xffffff,0x94AECE,0xffffff,0xffffff,0,sizeof(DL_URL),#DL_URL,#mouse_twbi,2,19,19};
  26. progress_bar DL_progress_bar = {0, 170, 51, 225, 12, 0, 0, 100, 0xFFFfff, 0x74DA00, 0x9F9F9F};
  27.  
  28. char save_to[4096] = "/tmp0/1/Downloads/";
  29. byte cleft = 15;
  30.  
  31. byte downloader_opened;
  32.  
  33. byte download_state;
  34. enum { STATE_NOT_STARTED, STATE_IN_PROGRESS, STATE_COMPLETED };
  35.  
  36.  
  37. void Downloader()
  38. {
  39.         int key, btn;
  40.        
  41.         char notify_message[4296];
  42.        
  43.         if (DL_URL[0]) {
  44.                 StartDownloading();
  45.         }
  46.         else strcpy(#DL_URL, "http://");
  47.         DL_address_box.size = DL_address_box.pos = DL_address_box.shift = DL_address_box.shift_old = strlen(#DL_URL);
  48.  
  49.         downloaded_size = full_size = 0;
  50.         downloader_opened = 1;
  51.  
  52.         SetEventMask(0x27);
  53.         loop()
  54.         {
  55.                 WaitEventTimeout(40);
  56.                 switch(EAX & 0xFF)
  57.                 {
  58.                         CASE evMouse:
  59.                                 if (!CheckActiveProcess(DL_Form.ID)) break;
  60.                                 if (DL_http_transfer <= 0) edit_box_mouse stdcall (#DL_address_box);
  61.                                 break;
  62.  
  63.                         case evButton:
  64.                                 btn=GetButtonID();
  65.                                 DL_Scan(btn);
  66.                                 break;
  67.  
  68.                         case evKey:
  69.                                 key = GetKey();
  70.                                 if (DL_address_box.flags & 0b10)
  71.                                 {
  72.                                         EAX=key<<8;
  73.                                         edit_box_key stdcall(#DL_address_box);
  74.                                 }
  75.                                 if (key==13) DL_Scan(301);
  76.                                 break;
  77.  
  78.                         case evReDraw:
  79.                                 system.color.get();
  80.                                 DefineAndDrawWindow(215, 100, 420, 120, 0x74, system.color.work, DL_WINDOW_HEADER, 0);
  81.                                 GetProcessInfo(#DL_Form, SelfInfo);
  82.                                 if (DL_Form.status_window>2) break;
  83.                                 if (DL_Form.height<120) MoveSize(OLD,OLD,OLD,120);
  84.                                 if (DL_Form.width<280) MoveSize(OLD,OLD,280,OLD);
  85.                                 DL_Draw_Window();
  86.                                 break;
  87.                                
  88.                         default:
  89.                                 if (DL_Form.width==0) || (DL_http_transfer <= 0) break;
  90.                                 http_receive stdcall (DL_http_transfer);
  91.                                 $push EAX
  92.                                 ESI = DL_http_transfer;
  93.                                 DL_progress_bar.max = ESI.http_msg.content_length;
  94.                                 if (DL_progress_bar.value != ESI.http_msg.content_received)
  95.                                 {
  96.                                         DL_progress_bar.value = ESI.http_msg.content_received; 
  97.                                         progressbar_draw stdcall(#DL_progress_bar);
  98.                                 }
  99.                                 $pop EAX
  100.                                 if (EAX == 0) {
  101.                                         ESI = DL_http_transfer;
  102.                                         DL_bufpointer = ESI.http_msg.content_ptr;
  103.                                         DL_bufsize = ESI.http_msg.content_received;
  104.                                         http_free stdcall (DL_http_transfer);
  105.                                         DL_http_transfer=0;
  106.                                         strcpy(#filepath, #save_to);
  107.                                         strcat(#filepath, #DL_URL+strrchr(#DL_URL, '/'));
  108.                                         if (WriteFile(DL_bufsize, DL_bufpointer, #filepath)==0)
  109.                                         {
  110.                                                 strcpy(#notify_message, FILE_SAVED_AS);
  111.                                                 strcat(#notify_message, #filepath);
  112.                                                 strcat(#notify_message, "' -Dt");      
  113.                                         }
  114.                                         else
  115.                                         {
  116.                                                 strcpy(#notify_message, "'Download manager\nError! Can\96t save file as ");
  117.                                                 strcat(#notify_message, #filepath);
  118.                                                 strcat(#notify_message, "' -Et");
  119.                                         }
  120.                                         notify(#notify_message);
  121.                                         DL_address_box.color = DL_address_box.blur_border_color = DL_address_box.focus_border_color = 0xFFFfff;
  122.                                         download_state = STATE_COMPLETED;
  123.                                         DL_Draw_Window();
  124.                                         break;
  125.                                 }
  126.                                 ESI = DL_http_transfer;
  127.                                 downloaded_size = ESI.http_msg.content_received;
  128.                                 full_size = ESI.http_msg.content_length;
  129.                                 DrawDownloading();
  130.                 }
  131.         }
  132. }
  133.  
  134.  
  135.  
  136. void DL_Draw_Window()
  137. {      
  138.         DrawBar(0,0, DL_Form.cwidth, DL_Form.cheight, system.color.work);
  139.         DeleteButton(305);
  140.         DeleteButton(306);
  141.         if (download_state == STATE_NOT_STARTED) ||  (download_state == STATE_COMPLETED)
  142.         {
  143.                 DrawCaptButton(cleft, 50, 130, 27, 301, system.color.work_button, system.color.work_button_text, START_DOWNLOADING);   
  144.         }
  145.         if (download_state == STATE_IN_PROGRESS)
  146.         {
  147.                 DrawCaptButton(cleft, 50, 140, 27, 302, system.color.work_button, system.color.work_button_text, STOP_DOWNLOADING);
  148.                 DrawDownloading();
  149.         }
  150.         if (download_state == STATE_COMPLETED)
  151.         {
  152.                 DrawCaptButton(cleft+140, 50, 110, 27, 305, system.color.work_button, system.color.work_button_text, SHOW_IN_FOLDER);
  153.                 DrawCaptButton(cleft+260, 50, 120, 27, 306, system.color.work_button, system.color.work_button_text, OPEN_FILE);       
  154.         }
  155.         WriteText(cleft, DL_address_box.top + 4, 0x80, system.color.work_text, "URL:");
  156.         DL_address_box.left = strlen("URL:")*6 + 10 + cleft;
  157.         DL_address_box.width = DL_Form.cwidth - DL_address_box.left - cleft - 3;
  158.         DL_address_box.offset=0;
  159.         edit_box_draw stdcall(#DL_address_box);
  160.         DrawRectangle(DL_address_box.left-1, DL_address_box.top-1, DL_address_box.width+2, 16, DL_address_box.color);
  161.         DrawRectangle(DL_address_box.left-2, DL_address_box.top-2, DL_address_box.width+4, 18, border_color);
  162. }
  163.  
  164.  
  165. void DrawDownloading()
  166. {
  167.         dword tmp;
  168.         char bytes_received[70];
  169.  
  170.         tmp = ConvertSizeToKb(downloaded_size);
  171.         strcpy(#bytes_received, tmp);
  172.         strcat(#bytes_received, KB_RECEIVED);
  173.         DrawBar(DL_progress_bar.left, DL_progress_bar.top + 17, DL_Form.cwidth - DL_progress_bar.left, 9, system.color.work);
  174.         WriteText(DL_progress_bar.left, DL_progress_bar.top + 17, 0x80, system.color.work_text, #bytes_received);
  175.         progressbar_draw stdcall(#DL_progress_bar);
  176. }
  177.  
  178.  
  179.  
  180.  
  181. void DL_Scan(int id)
  182. {
  183.         if (id==001) {
  184.                 downloader_opened=0;
  185.                 StopDownloading();
  186.                 ExitProcess();
  187.         }
  188.         if (id==301) && (DL_http_transfer <= 0) StartDownloading();
  189.         if (id==302) StopDownloading();
  190.         if (id==305) RunProgram("/sys/File managers/Eolite", #save_to);
  191.         if (id==306) RunProgram("@open", #filepath);
  192. }
  193.  
  194.  
  195. void StopDownloading()
  196. {
  197.         download_state = STATE_NOT_STARTED;
  198.         if (DL_http_transfer<>0)
  199.         {
  200.                 EAX = DL_http_transfer;
  201.                 EAX = EAX.http_msg.content_ptr;         // get pointer to data
  202.                 $push   EAX                                                     // save it on the stack
  203.                 http_free stdcall (DL_http_transfer);   // abort connection
  204.                 $pop    EAX                                                    
  205.                 mem_Free(EAX);                                          // free data
  206.                 DL_http_transfer=0;
  207.                 DL_bufsize = 0;
  208.                 DL_bufpointer = mem_Free(DL_bufpointer);
  209.                 downloaded_size = full_size = 0;
  210.         }
  211.         DL_address_box.color = DL_address_box.blur_border_color = DL_address_box.focus_border_color = 0xFFFfff;
  212.         DL_Draw_Window();
  213. }
  214.  
  215. void StartDownloading()
  216. {
  217.         StopDownloading();
  218.         if (strncmp(#DL_URL,"http:",5)==0)
  219.         {
  220.                 download_state = STATE_IN_PROGRESS;
  221.                 DL_address_box.color = DL_address_box.blur_border_color = DL_address_box.focus_border_color = 0xdddDDD;
  222.                 http_get stdcall (#DL_URL, 0, 0, #accept_language);
  223.                 DL_http_transfer = EAX;
  224.                 DL_progress_bar.value = 0;
  225.                 DL_Draw_Window();
  226.                 if (DL_http_transfer == 0)
  227.                 {
  228.                         StopDownloading();
  229.                         DL_bufsize = 0;
  230.                         DL_bufpointer = mem_Free(DL_bufpointer);
  231.                         return;
  232.                 }
  233.         }
  234.         else
  235.         {
  236.                 notify("File adress should starts from http://");
  237.         }
  238. }
  239.