Subversion Repositories Kolibri OS

Rev

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