Subversion Repositories Kolibri OS

Rev

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