Subversion Repositories Kolibri OS

Rev

Rev 5674 | Blame | Last modification | View Log | Download | RSS feed

  1. #define MEMSIZE 0x100000
  2. //libraries
  3. #include "..\lib\kolibri.h"
  4. #include "..\lib\strings.h"
  5. #include "..\lib\gui.h"
  6. #include "..\lib\draw_buf.h"
  7. #include "..\lib\file_system.h"
  8. #include "..\lib\mem.h"
  9. #include "..\lib\dll.h"
  10. #include "..\lib\list_box.h"
  11. //*.obj libraries
  12. #include "..\lib\obj\box_lib.h"
  13. #include "..\lib\obj\libio_lib.h"
  14. #include "..\lib\obj\http.h"
  15.  
  16. char header[]="New Downloader v0.6";
  17.  
  18. #ifdef LANG_RUS
  19.         char accept_language[]= "Accept-Language: ru\n";
  20. #else
  21.         char accept_language[]= "Accept-Language: en\n";       
  22. #endif
  23.  
  24. proc_info Form;
  25. #define WIN_W 400
  26. #define WIN_H 220
  27.  
  28. #define URL param
  29.  
  30. int     mouse_twb;
  31. edit_box address_box= {250,20,20,0xffffff,0x94AECE,0xffffff,0xffffff,0,sizeof(URL),#URL,#mouse_twb,2,19,19};
  32.  
  33. dword speed[2000];
  34. dword speed_position;
  35. dword bufpointer;
  36. dword bufsize;
  37.  
  38. dword http_transfer = 0;
  39. dword http_buffer;
  40.  
  41. DrawBufer diagram;
  42.  
  43. void main()
  44. {
  45.         int key, btn;
  46.  
  47.         char filepath[4096];
  48.         char notify_message[4296];
  49.        
  50.         load_dll(boxlib, #box_lib_init,0);
  51.         load_dll(libio, #libio_init,1);
  52.         load_dll(libHTTP, #http_lib_init,1);   
  53.         if (!URL) strcpy(#URL, "http://builds.kolibrios.org/eng/latest-iso.7z");
  54.         address_box.size = address_box.pos = strlen(#URL);
  55.  
  56.         SetEventMask(0x27); //a7
  57.         loop()
  58.         {
  59.                 WaitEventTimeout(40);
  60.                 switch(EAX & 0xFF)
  61.                 {
  62.                         CASE evMouse:
  63.                                 if (!CheckActiveProcess(Form.ID)) break;
  64.                                 if (http_transfer <= 0) edit_box_mouse stdcall (#address_box);
  65.                                 break;
  66.  
  67.                         case evButton:
  68.                                 btn=GetButtonID();
  69.                                 if (btn==1)     ExitProcess();
  70.                                 Scan(btn);
  71.                                 break;
  72.  
  73.                         case evKey:
  74.                                 key = GetKey();
  75.                                 if (address_box.flags & 0b10)
  76.                                 {
  77.                                         EAX=key<<8;
  78.                                         edit_box_key stdcall(#address_box);
  79.                                 }
  80.                                 if (key==13) Scan(301);
  81.                                 break;
  82.  
  83.                         case evReDraw:
  84.                                 system.color.get();
  85.                                 DefineAndDrawWindow(215,100,WIN_W,WIN_H,0x73,system.color.work,#header,0);
  86.                                 GetProcessInfo(#Form, SelfInfo);
  87.                                 if (Form.status_window>2) break;
  88.                                 if (Form.height<120) MoveSize(OLD,OLD,OLD,120);
  89.                                 if (Form.width<280) MoveSize(OLD,OLD,280,OLD);
  90.                                 diagram.Init(20, 87, Form.cwidth - 40, Form.cheight - 87 - 28);
  91.                                 Draw_Window();
  92.                                 break;
  93.                                
  94.                         default:
  95.                                 if (Form.width==0) break;
  96.                                 if (http_transfer <= 0) break;
  97.                                 http_receive stdcall (http_transfer);
  98.                                 if (EAX == 0) {
  99.                                         ESI = http_transfer;
  100.                                         bufpointer = ESI.http_msg.content_ptr;
  101.                                         bufsize = ESI.http_msg.content_received;
  102.                                         http_free stdcall (http_transfer);
  103.                                         http_transfer=0;
  104.                                         strcpy(#filepath, "/tmp0/1/");
  105.                                         strcat(#filepath, #URL+strrchr(#URL, '/'));
  106.                                         if (!WriteFile(bufsize, bufpointer, #filepath))
  107.                                         {
  108.                                                 strcpy(#notify_message, "File saved as ");
  109.                                         }
  110.                                         else
  111.                                         {
  112.                                                 strcpy(#notify_message, "Error! Can't save file as ");
  113.                                         }
  114.                                         strcat(#notify_message, #filepath);    
  115.                                         notify(#notify_message);
  116.                                         address_box.color = address_box.blur_border_color = address_box.focus_border_color = 0xFFFfff;
  117.                                         Draw_Window();
  118.                                 }
  119.                                 ESI = http_transfer;
  120.                                 speed[speed_position] = ESI.http_msg.content_received;
  121.                                 DrawSpeed();
  122.                                 speed_position++;
  123.                 }
  124.         }
  125. }
  126.  
  127. void DrawSpeed()
  128. {
  129.         int i;
  130.         int speed_in_position, speed_string;
  131.         int max_speed, start_from;
  132.         char bytes_received[70];
  133.  
  134.         diagram.Fill(0xFCF8F7);
  135.         max_speed = speed[speed_position];
  136.         if (speed_position < diagram.bufw) start_from = 0; else start_from = speed_position - diagram.bufw + 1;
  137.         if (speed_position>0) for (i = 0; i <= speed_position-start_from; i++)
  138.         {
  139.                 if (max_speed>0)
  140.                 {
  141.                         speed_in_position = diagram.bufh - 1 * speed[i+start_from] / max_speed;
  142.                         diagram.DrawBar(i, diagram.bufh - speed_in_position, 1, speed_in_position, 0x00A3CB);
  143.                 }
  144.         }
  145.         diagram.Show();
  146.         if (speed_position==0) return;
  147.         if (http_transfer > 0)
  148.         {
  149.                 strcpy(#bytes_received, "Downloading... ");    
  150.         }
  151.         else
  152.         {
  153.                 strcpy(#bytes_received, "Downloading competle. ");     
  154.         }
  155.         speed_string = ConvertSize(speed[speed_position-1]);
  156.         strcat(#bytes_received, speed_string);
  157.         strcat(#bytes_received, " received.");
  158.         DrawBar(diagram.bufx, diagram.bufy + diagram.bufh + 10, diagram.bufw, 9, system.color.work);
  159.         WriteText(diagram.bufx, diagram.bufy + diagram.bufh + 10, 0x80, system.color.work_text, #bytes_received);
  160. }
  161.  
  162. void Draw_Window()
  163. {      
  164.         DrawBar(0,0,Form.cwidth,Form.cheight,system.color.work); //bg
  165.         if (http_transfer <= 0)
  166.         {
  167.                 DrawCaptButton(diagram.bufx, 50, 120, 20, 301, system.color.work_button, system.color.work_button_text, "Start downloading");  
  168.         }
  169.         else
  170.         {
  171.                 DrawCaptButton(diagram.bufx, 50, 120, 20, 302, system.color.work_button, system.color.work_button_text, "Stop downloading");
  172.         }
  173.         if (http_transfer <= 0) && (speed_position>0)
  174.         {
  175.                 DrawCaptButton(diagram.bufx+130, 50, 120, 20, 305, system.color.work_button, system.color.work_button_text, "Show in folder"); 
  176.         }
  177.         WriteText(diagram.bufx, address_box.top + 4, 0x80, system.color.work_text, "URL:");
  178.         address_box.left = strlen("URL:")*6 + 10 + diagram.bufx;
  179.         address_box.width = Form.cwidth - address_box.left - diagram.bufx - 3;
  180.         address_box.offset=0;
  181.         edit_box_draw stdcall(#address_box);
  182.         DrawRectangle(address_box.left-1, address_box.top-1, address_box.width+2, 16,address_box.color);
  183.         DrawRectangle(address_box.left-2, address_box.top-2, address_box.width+4, 18,system.color.work_graph);
  184.  
  185.         DrawRectangle(diagram.bufx-2, diagram.bufy-2, diagram.bufw+2, diagram.bufh+2, system.color.work_graph);
  186.         DrawSpeed();
  187. }
  188.  
  189. void Scan(int id)
  190. {
  191.         if (id==301) && (http_transfer <= 0) StartDownloading();
  192.         if (id==302) StopDownloading();
  193.         if (id==305) RunProgram("/sys/File managers/Eolite", "/tmp0/1/");
  194. }
  195.  
  196.  
  197. void StopDownloading()
  198. {
  199.         if (http_transfer<>0)
  200.         {
  201.                 EAX = 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 (http_transfer);      // abort connection
  205.                 $pop    EAX                                                    
  206.                 mem_Free(EAX);                                          // free data
  207.                 http_transfer=0;
  208.                 bufsize = 0;
  209.                 bufpointer = mem_Free(bufpointer);
  210.         }
  211.         address_box.color = address_box.blur_border_color = address_box.focus_border_color = 0xFFFfff;
  212.         speed_position = 0;
  213.         Draw_Window();
  214. }
  215.  
  216. void StartDownloading()
  217. {
  218.         StopDownloading();
  219.         speed_position = 0;
  220.         if (strncmp(#URL,"http:",5)==0)
  221.         {
  222.                 address_box.color = address_box.blur_border_color = address_box.focus_border_color = 0xededed;
  223.                 http_get stdcall (#URL, 0, 0, #accept_language);
  224.                 http_transfer = EAX;
  225.                 Draw_Window();
  226.                 if (http_transfer == 0)
  227.                 {
  228.                         StopDownloading();
  229.                         bufsize = 0;
  230.                         bufpointer = mem_Free(bufpointer);
  231.                         return;
  232.                 }
  233.         }
  234.         else
  235.         {
  236.                 notify("File adress should starts from http://");
  237.         }
  238. }
  239.  
  240.  
  241.  
  242. stop:
  243.