Fix small bug when the url has a slash at the end

git-svn-id: svn://kolibrios.org@6374 a494cfbc-eb01-0410-851d-a64ba20cac60
This commit is contained in:
guillem 2016-03-25 15:35:48 +00:00
parent 50fdbc1f54
commit 955ed8254c

View File

@ -1,176 +1,184 @@
#ifdef LANG_RUS #ifdef LANG_RUS
#define DL_WINDOW_HEADER "Œ¥­¥¤¦¥à § £à㧮ª" #define DL_WINDOW_HEADER "Œ¥­¥¤¦¥à § £à㧮ª"
#define START_DOWNLOADING "<22> ç âì § ª çªã" #define START_DOWNLOADING "<22> ç âì § ª çªã"
#define STOP_DOWNLOADING "Žáâ ­®¢¨âì" #define STOP_DOWNLOADING "Žáâ ­®¢¨âì"
#define SHOW_IN_FOLDER "<22>®ª § âì ¢ ¯ ¯ª¥" #define SHOW_IN_FOLDER "<22>®ª § âì ¢ ¯ ¯ª¥"
#define OPEN_FILE_TEXT "Žâªàëâì ä ©«" #define OPEN_FILE_TEXT "Žâªàëâì ä ©«"
#define FILE_SAVED_AS "'Œ¥­¥¤¦¥à § £à㧮ª\n” ©« á®åà ­¥­ ª ª " #define FILE_SAVED_AS "'Œ¥­¥¤¦¥à § £à㧮ª\n” ©« á®åà ­¥­ ª ª "
#define KB_RECEIVED " ¯®«ã祭®" #define KB_RECEIVED " ¯®«ã祭®"
#else #else
#define DL_WINDOW_HEADER "Download Manager" #define DL_WINDOW_HEADER "Download Manager"
#define START_DOWNLOADING "Start downloading" #define START_DOWNLOADING "Start downloading"
#define STOP_DOWNLOADING "Stop downloading" #define STOP_DOWNLOADING "Stop downloading"
#define SHOW_IN_FOLDER "Show in folder" #define SHOW_IN_FOLDER "Show in folder"
#define OPEN_FILE_TEXT "Open file" #define OPEN_FILE_TEXT "Open file"
#define FILE_SAVED_AS "'Download manager\nFile saved as " #define FILE_SAVED_AS "'Download manager\nFile saved as "
#define KB_RECEIVED " received" #define KB_RECEIVED " received"
#endif #endif
char save_to[4096] = "/tmp0/1/Downloads"; char save_to[4096] = "/tmp0/1/Downloads";
proc_info DL_Form; proc_info DL_Form;
char downloader_edit[10000]; char downloader_edit[10000];
char filepath[4096]; char filepath[4096];
int mouse_twbi; int mouse_twbi;
edit_box ed = {250,20,20,0xffffff,0x94AECE,0xffffff,0xffffff,0,sizeof(downloader_edit),#downloader_edit,#mouse_twbi,2,19,19}; edit_box ed = {250,20,20,0xffffff,0x94AECE,0xffffff,0xffffff,0,sizeof(downloader_edit),#downloader_edit,#mouse_twbi,2,19,19};
progress_bar pb = {0, 170, 51, 225, 12, 0, 0, 100, 0xFFFfff, 0x74DA00, 0x9F9F9F}; progress_bar pb = {0, 170, 51, 225, 12, 0, 0, 100, 0xFFFfff, 0x74DA00, 0x9F9F9F};
byte downloader_opened; byte downloader_opened;
char downloader_stak[4096]; char downloader_stak[4096];
char str[2048];
char aux[2048];
void Downloader()
void Downloader()
{ {
int key; int key;
char notify_message[4296]; char notify_message[4296];
downloader_opened = 1; downloader_opened = 1;
SetEventMask(0x27); SetEventMask(0x27);
downloader.Stop(); downloader.Stop();
if (downloader_edit[0]) StartDownloading(); else strcpy(#downloader_edit, "http://"); if (downloader_edit[0]) StartDownloading(); else strcpy(#downloader_edit, "http://");
ed.size = ed.pos = ed.shift = ed.shift_old = strlen(#downloader_edit); ed.size = ed.pos = ed.shift = ed.shift_old = strlen(#downloader_edit);
loop() loop()
{ {
WaitEventTimeout(40); WaitEventTimeout(30);
switch(EAX & 0xFF) switch(EAX & 0xFF)
{ {
CASE evMouse: CASE evMouse:
if (!CheckActiveProcess(DL_Form.ID)) break; if (!CheckActiveProcess(DL_Form.ID)) break;
edit_box_mouse stdcall (#ed); edit_box_mouse stdcall (#ed);
break; break;
case evButton: case evButton:
Key_Scan(GetButtonID()); Key_Scan(GetButtonID());
break; break;
case evKey: case evKey:
GetKeys(); GetKeys();
EAX = key_ascii << 8; EAX = key_ascii << 8;
edit_box_key stdcall(#ed); edit_box_key stdcall(#ed);
if (key_scancode==SCAN_CODE_ENTER) Key_Scan(301); if (key_scancode==SCAN_CODE_ENTER) Key_Scan(301);
break; break;
case evReDraw: case evReDraw:
system.color.get(); system.color.get();
DefineAndDrawWindow(215, 100, 420, 120, 0x74, system.color.work, DL_WINDOW_HEADER, 0); DefineAndDrawWindow(215, 100, 420, 120, 0x74, system.color.work, DL_WINDOW_HEADER, 0);
GetProcessInfo(#DL_Form, SelfInfo); GetProcessInfo(#DL_Form, SelfInfo);
if (DL_Form.status_window>2) break; if (DL_Form.status_window>2) break;
if (DL_Form.height<120) MoveSize(OLD,OLD,OLD,120); if (DL_Form.height<120) MoveSize(OLD,OLD,OLD,120);
if (DL_Form.width<280) MoveSize(OLD,OLD,280,OLD); if (DL_Form.width<280) MoveSize(OLD,OLD,280,OLD);
DL_Draw_Window(); DL_Draw_Window();
break; break;
default: default:
if (!downloader.MonitorProgress()) break; if (!downloader.MonitorProgress()) break;
pb.max = downloader.data_full_size; pb.max = downloader.data_full_size;
if (pb.value != downloader.data_downloaded_size) if (pb.value != downloader.data_downloaded_size)
{ {
pb.value = downloader.data_downloaded_size; pb.value = downloader.data_downloaded_size;
progressbar_draw stdcall(#pb); progressbar_draw stdcall(#pb);
DrawDownloading(); DrawDownloading();
} }
if (downloader.state == STATE_COMPLETED) if (downloader.state == STATE_COMPLETED)
{ {
if (!dir_exists(#save_to)) CreateDir(#save_to); if (!dir_exists(#save_to)) CreateDir(#save_to);
strcpy(#filepath, #save_to); strcpy(#filepath, #save_to);
chrcat(#filepath, '/'); chrcat(#filepath, '/');
strcat(#filepath, #downloader_edit+strrchr(#downloader_edit, '/')); // Clean all slashes at the end
if (WriteFile(downloader.data_downloaded_size, downloader.bufpointer, #filepath)==0) strcpy(#aux, #downloader_edit);
sprintf(#notify_message, "%s%s%s",FILE_SAVED_AS,#filepath,"' -Dt"); while (aux[strlen(#aux)-1] == '/') {
else aux[strlen(#aux)-1] = 0;
sprintf(#notify_message, "%s%s%s","'Download manager\nError! Can\96t save file as ",#filepath,"' -Et"); }
notify(#notify_message); strcat(#filepath, #aux+strrchr(#aux, '/'));
StopDownloading();
DL_Draw_Window(); if (WriteFile(downloader.data_downloaded_size, downloader.bufpointer, #filepath)==0)
break; sprintf(#notify_message, "%s%s%s",FILE_SAVED_AS,#filepath,"' -Dt");
} else
} sprintf(#notify_message, "%s%s%s","'Download manager\nError! Can\96t save file as ",#filepath,"' -Et");
}
notify(#notify_message);
StopDownloading();
DL_Draw_Window();
break;
}
}
}
} }
void Key_Scan(int id) void Key_Scan(int id)
{ {
if (id==001) { downloader_opened=0; StopDownloading(); ExitProcess(); } if (id==001) { downloader_opened=0; StopDownloading(); ExitProcess(); }
if (id==301) && (downloader.http_transfer <= 0) StartDownloading(); if (id==301) && (downloader.http_transfer <= 0) StartDownloading();
if (id==302) StopDownloading(); if (id==302) StopDownloading();
if (id==305) RunProgram("/sys/File managers/Eolite", #save_to); if (id==305) RunProgram("/sys/File managers/Eolite", #save_to);
if (id==306) RunProgram("@open", #filepath); if (id==306) RunProgram("@open", #filepath);
} }
void DL_Draw_Window() void DL_Draw_Window()
{ {
byte cleft = 15; byte cleft = 15;
DrawBar(0,0, DL_Form.cwidth, DL_Form.cheight, system.color.work); DrawBar(0,0, DL_Form.cwidth, DL_Form.cheight, system.color.work);
DeleteButton(305); DeleteButton(305);
DeleteButton(306); DeleteButton(306);
if (downloader.state == STATE_NOT_STARTED) || (downloader.state == STATE_COMPLETED) if (downloader.state == STATE_NOT_STARTED) || (downloader.state == STATE_COMPLETED)
{ {
DrawCaptButton(cleft, 50, 140, 27, 301, system.color.work_button, system.color.work_button_text, START_DOWNLOADING); DrawCaptButton(cleft, 50, 140, 27, 301, system.color.work_button, system.color.work_button_text, START_DOWNLOADING);
} }
if (downloader.state == STATE_IN_PROGRESS) if (downloader.state == STATE_IN_PROGRESS)
{ {
DrawCaptButton(cleft, 50, 140, 27, 302, system.color.work_button, system.color.work_button_text, STOP_DOWNLOADING); DrawCaptButton(cleft, 50, 140, 27, 302, system.color.work_button, system.color.work_button_text, STOP_DOWNLOADING);
DrawDownloading(); DrawDownloading();
} }
if (downloader.state == STATE_COMPLETED) if (downloader.state == STATE_COMPLETED)
{ {
DrawCaptButton(cleft+140, 50, 110, 27, 305, system.color.work_button, system.color.work_button_text, SHOW_IN_FOLDER); DrawCaptButton(cleft+140, 50, 110, 27, 305, system.color.work_button, system.color.work_button_text, SHOW_IN_FOLDER);
DrawCaptButton(cleft+260, 50, 120, 27, 306, system.color.work_button, system.color.work_button_text, OPEN_FILE_TEXT); DrawCaptButton(cleft+260, 50, 120, 27, 306, system.color.work_button, system.color.work_button_text, OPEN_FILE_TEXT);
} }
WriteText(cleft, ed.top + 4, 0x80, system.color.work_text, "URL:"); WriteText(cleft, ed.top + 4, 0x80, system.color.work_text, "URL:");
ed.left = strlen("URL:")*6 + 10 + cleft; ed.left = strlen("URL:")*6 + 10 + cleft;
ed.width = DL_Form.cwidth - ed.left - cleft - 3; ed.width = DL_Form.cwidth - ed.left - cleft - 3;
ed.offset=0; ed.offset=0;
//edit_box_draw stdcall(#ed); //edit_box_draw stdcall(#ed);
DrawEditBox(#ed); DrawEditBox(#ed);
//DrawRectangle(ed.left-1, ed.top-1, ed.width+2, 16, ed.blur_border_color); //DrawRectangle(ed.left-1, ed.top-1, ed.width+2, 16, ed.blur_border_color);
//DrawRectangle(ed.left-2, ed.top-2, ed.width+4, 18, border_color); //DrawRectangle(ed.left-2, ed.top-2, ed.width+4, 18, border_color);
} }
void StartDownloading() void StartDownloading()
{ {
StopDownloading(); StopDownloading();
if (strncmp(#downloader_edit,"http://",7)!=0) { if (strncmp(#downloader_edit,"http://",7)!=0) {
notify("'File address should start from http://' -E"); notify("'File address should start from http://' -E");
return; return;
} }
if (!downloader.Start(#downloader_edit)) { if (!downloader.Start(#downloader_edit)) {
notify("'Error while starting download process.\nPlease, check entered path and internet connection.' -E"); notify("'Error while starting download process.\nPlease, check entered path and internet connection.' -E");
StopDownloading(); StopDownloading();
return; return;
} }
ed.blur_border_color = 0xCACACA; ed.blur_border_color = 0xCACACA;
ed.flags = 100000000000b; ed.flags = 100000000000b;
pb.value = 0; pb.value = 0;
DL_Draw_Window(); DL_Draw_Window();
} }
void DrawDownloading() void DrawDownloading()
{ {
char bytes_received[70]; char bytes_received[70];
dword tmp = ConvertSizeToKb(downloader.data_downloaded_size); dword tmp = ConvertSizeToKb(downloader.data_downloaded_size);
sprintf(#bytes_received, "%s%s", tmp, KB_RECEIVED); sprintf(#bytes_received, "%s%s", tmp, KB_RECEIVED);
DrawBar(pb.left, pb.top + 17, DL_Form.cwidth - pb.left, 9, system.color.work); DrawBar(pb.left, pb.top + 17, DL_Form.cwidth - pb.left, 9, system.color.work);
WriteText(pb.left, pb.top + 17, 0x80, system.color.work_text, #bytes_received); WriteText(pb.left, pb.top + 17, 0x80, system.color.work_text, #bytes_received);
progressbar_draw stdcall(#pb); progressbar_draw stdcall(#pb);
} }
void StopDownloading() void StopDownloading()
{ {
downloader.Stop(); downloader.Stop();
ed.blur_border_color = 0xFFFfff; ed.blur_border_color = 0xFFFfff;
ed.flags = 10b; ed.flags = 10b;
DL_Draw_Window(); DL_Draw_Window();
} }