//多线程多任务下载
import win.ui;
/*DSG{{*/
var winform = win.form(text="多线程多文件断点续传下载";right=765;bottom=399)
winform.add(
button={cls="button";text="多线程多文件断点续传下载";left=478;top=356;right=703;bottom=392;db=1;dr=1;z=2};
listview={cls="listview";left=10;top=16;right=757;bottom=346;bgcolor=16777215;db=1;dl=1;dr=1;dt=1;edge=1;z=1}
)
/*}}*/

//初始化listview控件
winform.listview.insertColumn("网址",180);
winform.listview.insertColumn("文件名",180);
winform.listview.insertColumn("状态",100);
winform.listview.insertColumn("大小",80);
winform.listview.insertColumn("速度",80);
winform.listview.insertColumn("已下载",120);
winform.listview.adjust = function(cx,cy){
    winform.listview.fillParent(1);
}

//创建下载线程管理器
import thread.dlManager;
var dlmgr = thread.dlManager(5/*最多允许五个线程同时下载*/);

//响应下载事件
dlmgr.onReceiveBegin = function(id,url,filename,statusText,httpStatusCode,totalSize,downSize){
    winform.listview.setItemText( {url;filename;statusText;fsys.formatSize(totalSize);fsys.formatSize(downSize) },id )
}
dlmgr.onReceive = function(id,sizePs,downSize){
    winform.listview.setItemText( fsys.formatSize(downSize),id,6);
    winform.listview.setItemText( fsys.formatSize(sizePs) + "/s" ,id,5);
}
dlmgr.onEnd = function(id,savepath,resumePath,contentLength){ 
	if( savepath ){ 
        winform.listview.setItemText(  "已完成"  ,id,3);
        winform.listview.setItemText( fsys.formatSize(contentLength),id,4);
    }
    else {
        winform.listview.setItemText(  "已停止"  ,id,3);
    }
    winform.listview.setItemText(  "0KB/s"  ,id,5); 
    //fsys.delete(resumePath)
}
dlmgr.onError = function(id,err){
    winform.listview.setItemText( err,id,3);
}

winform.button.oncommand = function(id,event){	

    //添加下载任务非常简单,push下载参数就可以了
    //注意这里为了简化示例,任务id使用了listview控件的行号,如果需要删除行则需要使用一个表保持id与行号的映射关系
    dlmgr.push( 
        id = winform.listview.addItem( "http://ide.update.aardio.com/releases/aardio.7z" );
        url = "http://ide.update.aardio.com/releases/aardio.7z";
        filename="aardio.7z"; //文件名可以省略
        savedir = "/download/"; 
    )
    
    dlmgr.push( 
        id = winform.listview.addItem( "http://wubi.aardio.com/update/wubiLex.7z" );
        url = "http://wubi.aardio.com/update/wubiLex.7z"; 
        savedir = "/download/"; //文件名可以省略
    )
    
    winform.button.disabled = true; 
}

//下载任务右键管理菜单
import win.ui.menu;
winform.listview.onnotify = function(id,code,ptr){  
      if( code = 0xFFFFFFFB/*_NM_RCLICK*/ ){
      
          var x,y = win.getMessagePos();  
          var nmListView = winform.listview.getNotifyMessage(code,ptr);
          
          //创建弹出菜单
        var popmenu = win.ui.popmenu(winform); 
        popmenu.add('取消',function(id){ 
            dlmgr.cancel( nmListView.iItem )
        } )
        popmenu.popup(x,y,true);
        popmenu.close();
      }
}

//关闭窗体时停止所有下载
import thread.event;
winform.onClose = function(hwnd,message,wParam,lParam){
    winform.text = "正在等待关闭"; 
    dlmgr.quit();
}
 
//显示主窗体
winform.show() 
win.loopMessage();