Subversion Repositories Kolibri OS

Rev

Rev 7284 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

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