Subversion Repositories Kolibri OS

Rev

Rev 6978 | Rev 7281 | Go to most recent revision | Blame | Compare with Previous | 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(EVM_REDRAW + EVM_KEY + EVM_BUTTON + EVM_MOUSE + EVM_MOUSE_FILTER + EVM_STACK);
  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() switch(WaitEvent())
  54.     {
  55.         case evMouse:
  56.             if (!CheckActiveProcess(DL_Form.ID)) break;
  57.             edit_box_mouse stdcall (#ed);
  58.             break;
  59.  
  60.         case evButton:
  61.             Key_Scan(GetButtonID());
  62.             break;
  63.  
  64.         case evKey:
  65.             GetKeys();
  66.             edit_box_key stdcall(#ed);
  67.             if (key_scancode==SCAN_CODE_ENTER) Key_Scan(301);
  68.             break;
  69.  
  70.         case evReDraw:
  71.             DefineAndDrawWindow(215, 100, 580, 130, 0x74, system.color.work, DL_WINDOW_HEADER, 0);
  72.             GetProcessInfo(#DL_Form, SelfInfo);
  73.             if (DL_Form.status_window>2) break;
  74.             if (DL_Form.height<120) MoveSize(OLD,OLD,OLD,120);
  75.             if (DL_Form.width<280) MoveSize(OLD,OLD,280,OLD);
  76.             DL_Draw_Window();
  77.             break;
  78.            
  79.         default:
  80.             if (!downloader.MonitorProgress()) break;
  81.             pb.max = downloader.data_full_size;
  82.             if (pb.value != downloader.data_downloaded_size)
  83.             {
  84.                 pb.value = downloader.data_downloaded_size;
  85.                 progressbar_draw stdcall(#pb);
  86.                 DrawDownloading();
  87.             }
  88.             if (downloader.state == STATE_COMPLETED)
  89.             {
  90.                 if (!dir_exists(#save_to)) CreateDir(#save_to);
  91.                 strcpy(#filepath, #save_to);
  92.                 chrcat(#filepath, '/');
  93.                                 // Clean all slashes at the end
  94.                         strcpy(#aux,  #downloader_edit);
  95.                         while (aux[strlen(#aux)-1] == '/') {
  96.                     aux[strlen(#aux)-1] = 0;
  97.                 }
  98.                 strcat(#filepath,  #aux+strrchr(#aux, '/'));
  99.                                
  100.                                 for (i=0; i<strlen(#filepath); i++) if(filepath[i]==':')||(filepath[i]=='?')filepath[i]='-';
  101.  
  102.                 if (CreateFile(downloader.data_downloaded_size, downloader.bufpointer, #filepath)==0)
  103.                     sprintf(#notify_message, "%s%s%s",FILE_SAVED_AS,#filepath,"' -Dt");
  104.                 else
  105.                     sprintf(#notify_message, "%s%s%s","'Download manager\nError! Can\96t save file as ",#filepath,"' -Et");
  106.                
  107.                 notify(#notify_message);
  108.                 StopDownloading();
  109.                 DL_Draw_Window();
  110.                 break;
  111.             }          
  112.     }
  113. }
  114.  
  115. void Key_Scan(int id)
  116. {
  117.     if (id==001) { downloader_opened=0; StopDownloading(); ExitProcess(); }
  118.     if (id==301) && (downloader.http_transfer <= 0) StartDownloading();
  119.     if (id==302) StopDownloading();
  120.     if (id==305) RunProgram("/sys/File managers/Eolite", #save_to);
  121.     if (id==306) RunProgram("/sys/@open", #filepath);
  122. }
  123.  
  124. void DL_Draw_Window()
  125. {  
  126.     int cleft = 15;
  127.     int but_x = 0;
  128.     int but_y = 58;
  129.     DrawBar(0,0, DL_Form.cwidth, DL_Form.cheight, system.color.work);
  130.     DeleteButton(305);
  131.     DeleteButton(306);
  132.     if (downloader.state == STATE_NOT_STARTED) || (downloader.state == STATE_COMPLETED)
  133.     {
  134.         but_x = cleft + DrawStandartCaptButton(cleft, but_y, 301, START_DOWNLOADING);  
  135.         if (filepath[0])
  136.         {
  137.             but_x += DrawStandartCaptButton(but_x, but_y, 305, SHOW_IN_FOLDER);
  138.             DrawStandartCaptButton(but_x, but_y, 306, OPEN_FILE_TEXT);  
  139.         }
  140.     }
  141.     if (downloader.state == STATE_IN_PROGRESS)
  142.     {
  143.         DrawCaptButton(DL_Form.width - 190, but_y, 167, 26, 302, system.color.work_button, system.color.work_button_text, STOP_DOWNLOADING);
  144.         DrawDownloading();
  145.     }
  146.     WriteText(cleft, ed.top + 4, 0x90, system.color.work_text, "URL:");
  147.     ed.left = strlen("URL:")*8 + 10 + cleft;
  148.     ed.width = DL_Form.cwidth - ed.left - cleft - 3;
  149.     ed.offset=0;
  150.     DrawEditBox(#ed);
  151. }
  152.  
  153. void StartDownloading()
  154. {
  155.     StopDownloading();
  156.     if (strncmp(#downloader_edit,"http://",7)!=0) {
  157.         notify("'File address should start from http://' -E");
  158.         return;
  159.     }
  160.     if (!downloader.Start(#downloader_edit)) {
  161.         notify("'Error while starting download process.\nPlease, check entered path and internet connection.' -E");
  162.         StopDownloading();
  163.         return;
  164.     }
  165.     ed.blur_border_color = 0xCACACA;
  166.     ed.flags = 100000000000b;
  167.     pb.value = 0;
  168.     DL_Draw_Window();
  169. }
  170.  
  171. void DrawDownloading()
  172. {
  173.     char bytes_received[70];
  174.     sprintf(#bytes_received, KB_RECEIVED, ConvertSizeToKb(downloader.data_downloaded_size) );
  175.     DrawBar(15, pb.top + 22, strlen(#bytes_received+4)*12, 16, system.color.work);
  176.     WriteText(15, pb.top + 22, 0x90, system.color.work_text, #bytes_received);
  177.     progressbar_draw stdcall(#pb);
  178. }
  179.  
  180. void StopDownloading()
  181. {
  182.     downloader.Stop();
  183.     ed.blur_border_color = 0xFFFfff;
  184.     ed.flags = 10b;
  185.     DL_Draw_Window();
  186. }