Subversion Repositories Kolibri OS

Rev

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