Subversion Repositories Kolibri OS

Rev

Rev 7227 | 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_TEXT "Žâªàëâì ä ©«"
  7.         #define FILE_SAVED_AS "'Œ¥­¥¤¦¥à § £à㧮ª\n” ©« á®åà ­¥­ ª ª "
  8.         #define KB_RECEIVED "ˆ¤¥â ᪠稢 ­¨¥... %s ¯®«ã祭®"
  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_TEXT "Open file"
  15.         #define FILE_SAVED_AS "'Download manager\nFile saved as "
  16.         #define KB_RECEIVED "Downloading... %s received"
  17. #endif
  18. char save_to[4096] = "/tmp0/1/Downloads";
  19.  
  20. proc_info DL_Form;
  21. char downloader_edit[10000];
  22. char filepath[4096];
  23. edit_box ed = {NULL,57,20,0xffffff,0x94AECE,0xffffff,0xffffff,0x10000000,sizeof(downloader_edit),#downloader_edit,0,2,19,19};
  24. progress_bar pb = {0, 20, 58, 350, 17, 0, 0, 100, 0xFFFfff, 0x74DA00, 0x9F9F9F};
  25. //progress_bar pb = {0, 180, 55, 225, 12, 0, 0, 100, 0xFFFfff, 0x74DA00, 0x9F9F9F};
  26. //progress_bar: value, left, top, width, height, style, min, max, back_color, progress_color, frame_color;
  27.        
  28.  
  29. bool downloader_opened;
  30. char downloader_stak[4096];
  31.  
  32.  
  33.  
  34. void Downloader()  
  35. {
  36.         if (!dir_exists(#save_to)) CreateDir(#save_to);
  37.         downloader_opened = true;
  38.         SetEventMask(EVM_REDRAW + EVM_KEY + EVM_BUTTON + EVM_MOUSE + EVM_MOUSE_FILTER + EVM_STACK);
  39.  
  40.         system.color.get();
  41.         pb.frame_color = system.color.work_dark;
  42.  
  43.         filepath[0] = NULL;
  44.        
  45.         downloader.Stop();
  46.         if (downloader_edit[0]) StartDownloading(); else strcpy(#downloader_edit, "http://");
  47.         ed.size = ed.pos = ed.shift = ed.shift_old = strlen(#downloader_edit);
  48.  
  49.         loop() switch(WaitEvent())
  50.         {
  51.                 case evMouse:
  52.                         edit_box_mouse stdcall (#ed);
  53.                         break;
  54.  
  55.                 case evButton:
  56.                         Key_Scan(GetButtonID());
  57.                         break;
  58.  
  59.                 case evKey:
  60.                         GetKeys();
  61.                         edit_box_key stdcall(#ed);
  62.                         if (key_scancode==SCAN_CODE_ENTER) Key_Scan(301);
  63.                         break;
  64.  
  65.                 case evReDraw:
  66.                         DefineAndDrawWindow(215, 100, 580, 130, 0x74, system.color.work, DL_WINDOW_HEADER, 0);
  67.                         GetProcessInfo(#DL_Form, SelfInfo);
  68.                         if (DL_Form.status_window>2) break;
  69.                         if (DL_Form.height<120) MoveSize(OLD,OLD,OLD,120);
  70.                         if (DL_Form.width<280) MoveSize(OLD,OLD,280,OLD);
  71.                         DL_Draw_Window();
  72.                         break;
  73.                    
  74.                 default:
  75.                         if (!downloader.MonitorProgress()) break;
  76.                         pb.max = downloader.data_full_size;
  77.                         if (pb.value != downloader.data_downloaded_size)
  78.                         {
  79.                                 pb.value = downloader.data_downloaded_size;
  80.                                 progressbar_draw stdcall(#pb);
  81.                                 DrawDownloading();
  82.                         }
  83.                         if (downloader.state == STATE_COMPLETED)
  84.                         {
  85.                                 SaveDownloadedFile();
  86.                                 StopDownloading();
  87.                                 DL_Draw_Window();
  88.                                 break;
  89.                         }          
  90.         }
  91. }
  92.  
  93. void Key_Scan(int id)
  94. {
  95.         if (id==001) { downloader_opened=false; StopDownloading(); ExitProcess(); }
  96.         if (id==301) && (downloader.httpd.transfer <= 0) StartDownloading();
  97.         if (id==302) StopDownloading();
  98.         if (id==305) RunProgram("/sys/File managers/Eolite", #save_to);
  99.         if (id==306) {
  100.                 SetCurDir(#save_to);
  101.                 RunProgram("/sys/@open", #filepath);
  102.         }
  103. }
  104.  
  105. void DL_Draw_Window()
  106. {  
  107.         int cleft = 15;
  108.         int but_x = 0;
  109.         int but_y = 58;
  110.         DrawBar(0,0, DL_Form.cwidth, DL_Form.cheight, system.color.work);
  111.         DeleteButton(301);
  112.         DeleteButton(302);
  113.         DeleteButton(305);
  114.         DeleteButton(306);
  115.         if (downloader.state == STATE_NOT_STARTED) || (downloader.state == STATE_COMPLETED)
  116.         {
  117.                 but_x = cleft + DrawStandartCaptButton(cleft, but_y, 301, START_DOWNLOADING);  
  118.                 if (filepath[0])
  119.                 {
  120.                         but_x += DrawStandartCaptButton(but_x, but_y, 305, SHOW_IN_FOLDER);
  121.                         DrawStandartCaptButton(but_x, but_y, 306, OPEN_FILE_TEXT);  
  122.                 }
  123.         }
  124.         if (downloader.state == STATE_IN_PROGRESS)
  125.         {
  126.                 DrawStandartCaptButton(DL_Form.width - 190, but_y, 302, STOP_DOWNLOADING);
  127.                 DrawDownloading();
  128.         }
  129.         WriteText(cleft, ed.top + 4, 0x90, system.color.work_text, "URL:");
  130.     ed.width = DL_Form.cwidth - ed.left - cleft - 3;
  131.         ed.offset=0;
  132.         DrawEditBox(#ed);
  133. }
  134.  
  135. void StartDownloading()
  136. {
  137.         StopDownloading();
  138.         if (strncmp(#downloader_edit,"http://",7)!=0) {
  139.                 notify("'File address should start from http://' -E");
  140.                 return;
  141.         }
  142.         if (!downloader.Start(#downloader_edit)) {
  143.                 notify("'Error while starting download process.\nPlease, check entered path and internet connection.' -E");
  144.                 StopDownloading();
  145.                 return;
  146.         }
  147.         ed.blur_border_color = 0xCACACA;
  148.         ed.flags = 100000000000b;
  149.         pb.value = 0;
  150.         DL_Draw_Window();
  151. }
  152.  
  153. void DrawDownloading()
  154. {
  155.         char bytes_received[70];
  156.         sprintf(#bytes_received, KB_RECEIVED, ConvertSizeToKb(downloader.data_downloaded_size) );
  157.         DrawBar(15, pb.top + 22, strlen(#bytes_received+4)*12, 16, system.color.work);
  158.         WriteText(15, pb.top + 22, 0x90, system.color.work_text, #bytes_received);
  159.         progressbar_draw stdcall(#pb);
  160. }
  161.  
  162. void StopDownloading()
  163. {
  164.         downloader.Stop();
  165.         ed.blur_border_color = 0xFFFfff;
  166.         ed.flags = 10b;
  167.         DL_Draw_Window();
  168. }
  169.  
  170. void SaveDownloadedFile()
  171. {
  172.         int i;
  173.         char aux[2048];
  174.         char notify_message[4296];
  175.  
  176.         // Clean all slashes at the end
  177.         strcpy(#aux,  #downloader_edit);
  178.         while (aux[strlen(#aux)-1] == '/') {
  179.                 aux[strlen(#aux)-1] = 0;
  180.         }
  181.         sprintf(#filepath, "%s/%s", #save_to, #aux+strrchr(#aux, '/'));
  182.        
  183.         for (i=0; i<strlen(#filepath); i++) if(filepath[i]==':')||(filepath[i]=='?')filepath[i]='-';
  184.  
  185.         if (CreateFile(downloader.data_downloaded_size, downloader.bufpointer, #filepath)==0)
  186.                 sprintf(#notify_message, "%s%s%s",FILE_SAVED_AS,#filepath,"' -Dt");
  187.         else
  188.                 sprintf(#notify_message, "%s%s%s","'Download manager\nError! Can\96t save file as ",#filepath,"' -Et");
  189.        
  190.         notify(#notify_message);
  191. }