Subversion Repositories Kolibri OS

Rev

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