Subversion Repositories Kolibri OS

Rev

Rev 5674 | 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.                                 EAX=key<<8;
  71.                                 edit_box_key stdcall(#DL_address_box);
  72.                                 if (key==13) DL_Scan(301);
  73.                                 break;
  74.  
  75.                         case evReDraw:
  76.                                 system.color.get();
  77.                                 DefineAndDrawWindow(215, 100, 420, 120, 0x74, system.color.work, DL_WINDOW_HEADER, 0);
  78.                                 GetProcessInfo(#DL_Form, SelfInfo);
  79.                                 if (DL_Form.status_window>2) break;
  80.                                 if (DL_Form.height<120) MoveSize(OLD,OLD,OLD,120);
  81.                                 if (DL_Form.width<280) MoveSize(OLD,OLD,280,OLD);
  82.                                 DL_Draw_Window();
  83.                                 break;
  84.                                
  85.                         default:
  86.                                 if (DL_Form.width==0) || (DL_http_transfer <= 0) break;
  87.                                 http_receive stdcall (DL_http_transfer);
  88.                                 $push EAX
  89.                                 ESI = DL_http_transfer;
  90.                                 DL_progress_bar.max = ESI.http_msg.content_length;
  91.                                 if (DL_progress_bar.value != ESI.http_msg.content_received)
  92.                                 {
  93.                                         DL_progress_bar.value = ESI.http_msg.content_received; 
  94.                                         progressbar_draw stdcall(#DL_progress_bar);
  95.                                 }
  96.                                 $pop EAX
  97.                                 if (EAX == 0) {
  98.                                         ESI = DL_http_transfer;
  99.                                         DL_bufpointer = ESI.http_msg.content_ptr;
  100.                                         DL_bufsize = ESI.http_msg.content_received;
  101.                                         http_free stdcall (DL_http_transfer);
  102.                                         DL_http_transfer=0;
  103.                                         strcpy(#filepath, #save_to);
  104.                                         strcat(#filepath, #DL_URL+strrchr(#DL_URL, '/'));
  105.                                         if (WriteFile(DL_bufsize, DL_bufpointer, #filepath)==0)
  106.                                         {
  107.                                                 strcpy(#notify_message, FILE_SAVED_AS);
  108.                                                 strcat(#notify_message, #filepath);
  109.                                                 strcat(#notify_message, "' -Dt");      
  110.                                         }
  111.                                         else
  112.                                         {
  113.                                                 strcpy(#notify_message, "'Download manager\nError! Can\96t save file as ");
  114.                                                 strcat(#notify_message, #filepath);
  115.                                                 strcat(#notify_message, "' -Et");
  116.                                         }
  117.                                         notify(#notify_message);
  118.                                         DL_address_box.color = DL_address_box.blur_border_color = DL_address_box.focus_border_color = 0xFFFfff;
  119.                                         download_state = STATE_COMPLETED;
  120.                                         DL_Draw_Window();
  121.                                         break;
  122.                                 }
  123.                                 ESI = DL_http_transfer;
  124.                                 downloaded_size = ESI.http_msg.content_received;
  125.                                 full_size = ESI.http_msg.content_length;
  126.                                 DrawDownloading();
  127.                 }
  128.         }
  129. }
  130.  
  131.  
  132.  
  133. void DL_Draw_Window()
  134. {      
  135.         DrawBar(0,0, DL_Form.cwidth, DL_Form.cheight, system.color.work);
  136.         DeleteButton(305);
  137.         DeleteButton(306);
  138.         if (download_state == STATE_NOT_STARTED) ||  (download_state == STATE_COMPLETED)
  139.         {
  140.                 DrawCaptButton(cleft, 50, 130, 27, 301, system.color.work_button, system.color.work_button_text, START_DOWNLOADING);   
  141.         }
  142.         if (download_state == STATE_IN_PROGRESS)
  143.         {
  144.                 DrawCaptButton(cleft, 50, 140, 27, 302, system.color.work_button, system.color.work_button_text, STOP_DOWNLOADING);
  145.                 DrawDownloading();
  146.         }
  147.         if (download_state == STATE_COMPLETED)
  148.         {
  149.                 DrawCaptButton(cleft+140, 50, 110, 27, 305, system.color.work_button, system.color.work_button_text, SHOW_IN_FOLDER);
  150.                 DrawCaptButton(cleft+260, 50, 120, 27, 306, system.color.work_button, system.color.work_button_text, OPEN_FILE);       
  151.         }
  152.         WriteText(cleft, DL_address_box.top + 4, 0x80, system.color.work_text, "URL:");
  153.         DL_address_box.left = strlen("URL:")*6 + 10 + cleft;
  154.         DL_address_box.width = DL_Form.cwidth - DL_address_box.left - cleft - 3;
  155.         DL_address_box.offset=0;
  156.         edit_box_draw stdcall(#DL_address_box);
  157.         DrawRectangle(DL_address_box.left-1, DL_address_box.top-1, DL_address_box.width+2, 16, DL_address_box.color);
  158.         DrawRectangle(DL_address_box.left-2, DL_address_box.top-2, DL_address_box.width+4, 18, border_color);
  159. }
  160.  
  161.  
  162. void DrawDownloading()
  163. {
  164.         dword tmp;
  165.         char bytes_received[70];
  166.  
  167.         tmp = ConvertSizeToKb(downloaded_size);
  168.         strcpy(#bytes_received, tmp);
  169.         strcat(#bytes_received, KB_RECEIVED);
  170.         DrawBar(DL_progress_bar.left, DL_progress_bar.top + 17, DL_Form.cwidth - DL_progress_bar.left, 9, system.color.work);
  171.         WriteText(DL_progress_bar.left, DL_progress_bar.top + 17, 0x80, system.color.work_text, #bytes_received);
  172.         progressbar_draw stdcall(#DL_progress_bar);
  173. }
  174.  
  175.  
  176.  
  177.  
  178. void DL_Scan(int id)
  179. {
  180.         if (id==001) {
  181.                 downloader_opened=0;
  182.                 StopDownloading();
  183.                 ExitProcess();
  184.         }
  185.         if (id==301) && (DL_http_transfer <= 0) StartDownloading();
  186.         if (id==302) StopDownloading();
  187.         if (id==305) RunProgram("/sys/File managers/Eolite", #save_to);
  188.         if (id==306) RunProgram("@open", #filepath);
  189. }
  190.  
  191.  
  192. void StopDownloading()
  193. {
  194.         download_state = STATE_NOT_STARTED;
  195.         if (DL_http_transfer<>0)
  196.         {
  197.                 EAX = DL_http_transfer;
  198.                 EAX = EAX.http_msg.content_ptr;         // get pointer to data
  199.                 $push   EAX                                                     // save it on the stack
  200.                 http_free stdcall (DL_http_transfer);   // abort connection
  201.                 $pop    EAX                                                    
  202.                 mem_Free(EAX);                                          // free data
  203.                 DL_http_transfer=0;
  204.                 DL_bufsize = 0;
  205.                 DL_bufpointer = mem_Free(DL_bufpointer);
  206.                 downloaded_size = full_size = 0;
  207.         }
  208.         DL_address_box.color = DL_address_box.blur_border_color = 0xFFFfff;
  209.         DL_address_box.flags = 10b;
  210.         DL_Draw_Window();
  211. }
  212.  
  213. void StartDownloading()
  214. {
  215.         StopDownloading();
  216.         if (strncmp(#DL_URL,"http:",5)==0)
  217.         {
  218.                 download_state = STATE_IN_PROGRESS;
  219.                 DL_address_box.color = DL_address_box.blur_border_color = 0xCACACA;
  220.                 DL_address_box.flags = 100000000000b;
  221.                 http_get stdcall (#DL_URL, 0, 0, #accept_language);
  222.                 DL_http_transfer = EAX;
  223.                 DL_progress_bar.value = 0;
  224.                 DL_Draw_Window();
  225.                 if (DL_http_transfer == 0)
  226.                 {
  227.                         StopDownloading();
  228.                         DL_bufsize = 0;
  229.                         DL_bufpointer = mem_Free(DL_bufpointer);
  230.                         return;
  231.                 }
  232.         }
  233.         else
  234.         {
  235.                 notify("File adress should starts from http://");
  236.         }
  237. }
  238.