Subversion Repositories Kolibri OS

Rev

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\figures.h"
  6. #include "..\lib\file_system.h"
  7. #include "..\lib\mem.h"
  8. #include "..\lib\dll.h"
  9. #include "..\lib\list_box.h"
  10. //*.obj libraries
  11. #include "..\lib\lib.obj\box_lib.h"
  12. #include "..\lib\lib.obj\libio_lib.h"
  13. #include "..\lib\lib.obj\http.h"
  14.  
  15. char header[]="New Downloader v0.4";
  16.  
  17. #ifdef LANG_RUS
  18.         char accept_language[]= "Accept-Language: ru\n";
  19. #else
  20.         char accept_language[]= "Accept-Language: en\n";       
  21. #endif
  22.  
  23. proc_info Form;
  24. #define WIN_W 400
  25. #define WIN_H 240
  26. system_colors sc;
  27. #define URL param
  28.  
  29. int     mouse_twb;
  30. edit_box address_box= {250,20,20,0xffffff,0x94AECE,0xffffff,0xffffff,0,sizeof(URL),#URL,#mouse_twb,2,19,19};
  31.  
  32. dword speed[2000];
  33. dword speed_position;
  34. dword bufpointer;
  35. dword bufsize;
  36.  
  37. dword http_transfer = 0;
  38. dword http_buffer;
  39.  
  40. struct diagram {
  41.         int x,y,w,h;
  42. } diagram;
  43.  
  44. void main()
  45. {
  46.         int key, btn;
  47.         mouse m;
  48.         char filepath[4096];
  49.         char notify_message[4296];
  50.        
  51.         mem_Init();
  52.         if (load_dll2(boxlib, #box_lib_init,0)!=0) {notify("System Error: library doesn't exists /rd/1/lib/box_lib.obj"); ExitProcess();}
  53.         if (load_dll2(libio, #libio_init,1)!=0) notify("Error: library doesn't exists - libio");
  54.         if (load_dll2(libHTTP, #http_lib_init,1)!=0) notify("Error: library doesn't exists - http");   
  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(0xa7);
  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) return;
  90.                                 if (Form.height<120) MoveSize(OLD,OLD,OLD,120);
  91.                                 if (Form.width<280) MoveSize(OLD,OLD,280,OLD);
  92.                                 diagram.x = 20;
  93.                                 diagram.y = 87;
  94.                                 diagram.w = Form.cwidth - diagram.x - diagram.x;
  95.                                 diagram.h = Form.cheight - diagram.y - 28;
  96.                                 Draw_Window();
  97.                                 break;
  98.                                
  99.                         default:
  100.                                 if (Form.width==0) break;
  101.                                 if (http_transfer <= 0) break;
  102.                                 http_process stdcall (http_transfer);
  103.                                 if (EAX == 0) {
  104.                                         ESI = http_transfer;
  105.                                         bufpointer = ESI.http_msg.content_ptr;
  106.                                         bufsize = ESI.http_msg.content_received;
  107.                                         http_free stdcall (http_transfer);
  108.                                         http_transfer=0;
  109.                                         strcpy(#filepath, "/tmp0/1/");
  110.                                         strcat(#filepath, #URL+strrchr(#URL, '/'));
  111.                                         if (WriteFile(bufsize, bufpointer, #filepath))
  112.                                         {
  113.                                                 strcpy(#notify_message, "File saved as ");
  114.                                         }
  115.                                         else
  116.                                         {
  117.                                                 strcpy(#notify_message, "Error! Can't save file as ");
  118.                                         }
  119.                                         strcat(#notify_message, #filepath);    
  120.                                         notify(#notify_message);
  121.                                         address_box.color = address_box.blur_border_color = address_box.focus_border_color = 0xFFFfff;
  122.                                         Draw_Window();
  123.                                 }
  124.                                 ESI = http_transfer;
  125.                                 speed[speed_position] = ESI.http_msg.content_received;
  126.                                 DrawSpeed();
  127.                                 speed_position++;
  128.                 }
  129.         }
  130. }
  131.  
  132. void DrawSpeed()
  133. {
  134.         int i;
  135.         int speed_in_position;
  136.         int max_speed, start_from;
  137.         char bytes_received[70];
  138.  
  139.         DrawBar(diagram.x, diagram.y, diagram.w, diagram.h+1, 0xFCF8F7);
  140.         max_speed = speed[speed_position];
  141.         if (speed_position < diagram.w) start_from = 0; else start_from = speed_position - diagram.w;
  142.         for (i = 0; i <= speed_position-start_from; i++)
  143.         {
  144.                 if (max_speed>0)
  145.                 {
  146.                         speed_in_position = diagram.h - 2 * speed[i+start_from] / max_speed;
  147.                         PutPixel(diagram.x+i, diagram.h - speed_in_position + diagram.y, 0x00A3CB);
  148.                         ECX++;
  149.                         $int 64;
  150.                 }
  151.         }
  152.         if (speed_position==0) return;
  153.         if (http_transfer > 0)
  154.         {
  155.                 strcpy(#bytes_received, "Downloading... ");    
  156.         }
  157.         else
  158.         {
  159.                 strcpy(#bytes_received, "Downloading competle. ");     
  160.         }
  161.         strcat(#bytes_received, itoa(speed[speed_position-1]));
  162.         strcat(#bytes_received, " bytes received.");
  163.         DrawBar(diagram.x, diagram.y + diagram.h + 10, diagram.w, 9, sc.work);
  164.         WriteText(diagram.x, diagram.y + diagram.h + 10, 0x80, sc.work_text, #bytes_received);
  165. }
  166.  
  167. void Draw_Window()
  168. {      
  169.         DrawBar(0,0,Form.cwidth,Form.cheight,sc.work); //bg
  170.         DrawCaptButton(diagram.x, 50, 120, 20, 301, sc.work_button, sc.work_button_text, "Start downloading");
  171.         if (http_transfer <= 0) && (speed_position>0)
  172.         {
  173.                 DrawCaptButton(diagram.x+130, 50, 120, 20, 305, sc.work_button, sc.work_button_text, "Show in folder");
  174.         }
  175.         WriteText(diagram.x, address_box.top + 4, 0x80, sc.work_text, "URL:");
  176.         address_box.left = strlen("URL:")*6 + 10 + diagram.x;
  177.         address_box.width = Form.cwidth - address_box.left - diagram.x - 3;
  178.         address_box.offset=0;
  179.         edit_box_draw stdcall(#address_box);
  180.         DrawRectangle(address_box.left-1, address_box.top-1, address_box.width+2, 16,address_box.color);
  181.         DrawRectangle(address_box.left-2, address_box.top-2, address_box.width+4, 18,sc.work_graph);
  182.  
  183.         DrawRectangle(diagram.x-2, diagram.y-2, diagram.w+2, diagram.h+3, sc.work_graph);
  184.         DrawSpeed();
  185. }
  186.  
  187. void Scan(int id)
  188. {
  189.         if (id==301) StartDownloading();
  190.         if (id==302) StopDownloading();
  191.         if (id==305) RunProgram("/sys/File managers/Eolite", "/tmp0/1/");
  192. }
  193.  
  194.  
  195. void StopDownloading()
  196. {
  197.         if (http_transfer<>0)
  198.         {
  199.                 EAX = http_transfer;
  200.                 EAX = EAX.http_msg.content_ptr;         // get pointer to data
  201.                 $push   EAX                                                     // save it on the stack
  202.                 http_free stdcall (http_transfer);      // abort connection
  203.                 $pop    EAX                                                    
  204.                 mem_Free(EAX);                                          // free data
  205.                 http_transfer=0;
  206.                 bufsize = 0;
  207.                 bufpointer = mem_Free(bufpointer);
  208.         }
  209.         address_box.color = address_box.blur_border_color = address_box.focus_border_color = 0xFFFfff;
  210.         Draw_Window();
  211. }
  212.  
  213. void StartDownloading()
  214. {
  215.         StopDownloading();
  216.         speed_position = 0;
  217.         if (strncmp(#URL,"http:",5)==0)
  218.         {
  219.                 address_box.color = address_box.blur_border_color = address_box.focus_border_color = 0xededed;
  220.                 Draw_Window();
  221.                 http_get stdcall (#URL, #accept_language);
  222.                 http_transfer = EAX;
  223.                 if (http_transfer == 0)
  224.                 {
  225.                         StopDownloading();
  226.                         bufsize = 0;
  227.                         bufpointer = mem_Free(bufpointer);
  228.                         return;
  229.                 }
  230.         }
  231.         else
  232.         {
  233.                 notify("File adress should starts form http://");
  234.         }
  235. }
  236.  
  237.  
  238.  
  239. stop:
  240.