import win.ui;
import win.ole.image;
import fsys;
import process;
 
/*DSG{{*/
var frmMain = win.form(parent=...; bgcolor=16777215;bottom=599;text="系统漏洞修复";border="resizable";right=799 )
frmMain.add( 
pic_loader={ bgcolor=16777215;bottom=508;text="";left=128;top=492;font=LOGFONT( name="宋体";h=-12 );z=7;transparent=1;right=144;image=$"\res\loader-1.bmp";hide=1;cls="picturebox" };
richedit={ dr=1;vscroll=1;right=784;left=600;dt=1;db=1;cls="richedit";link=1;wrap=1;bottom=480;multiline=1;top=136;font=LOGFONT( name="宋体";h=-12 );z=5;readonly=1;edge=1;text=" http:/www.yhhe.net" };
listview={ dr=1;dl=1;bgcolor=16777215;right=592;left=16;dt=1;db=1;fullRow=1;bottom=480;text="listview";cls="listview";top=136;font=LOGFONT( name="宋体";h=-12 );z=4;edge=1;
items={  }
};
static={ bottom=510;right=384;left=152;top=495;font=LOGFONT( name="宋体";h=-12 );transparent=1;text="正在扫描系统漏洞...";z=8;hide=1;cls="static" };
picturebox={ dl=1;bottom=120;text="picturebox";left=0;dt=1;top=0;image=$"\res\topLeft.gif";z=1;transparent=1;font=LOGFONT( name="宋体";h=-12 );right=203;cls="picturebox" };
button_scan={ dl=1;bgcolor=16777215;bottom=512;text="扫描系统漏洞";left=16;top=488;font=LOGFONT( name="宋体";h=-12 );z=6;db=1;right=120;cls="button" };
picturebox2={ dr=1;dl=1;bottom=120;text="picturebox";left=203;dt=1;top=0;font=LOGFONT( name="宋体";h=-12 );z=2;image=$"\res\topCenter.gif";right=529;transparent=1;cls="picturebox" };
picturebox3={ dr=1;bottom=120;right=800;left=529;dt=1;top=0;image=$"\res\topRight.gif";z=3;text="picturebox";font=LOGFONT( name="宋体";h=-12 );transparent=1;cls="picturebox" }
)
/*}}*/

frmMain.listview.insertColumn("漏洞大小",120,1, 0x2/*_LVCFMT_CENTER*/ ); 
frmMain.listview.insertColumn("漏洞名称",300);
   
frmMain.richedit.onlink=function(msg,title){
	if( msg == 0x201/*_WM_LBUTTONDOWN*/ )
		process.execute( title )	
}
 

/** 自定义结构 **/
HOTFIXINFO = class {
	int		nIndex;				/* 项目索引 */
	string	name;				/* 漏洞名称 */
	string	description;		/* 漏洞描述 */
	int		size;				/* 漏洞大小 */
	string	url;				/* 下载地址 */
	int		selected;			/* 是否选中 */
}

/** 建立一个变量，用于放置漏洞信息 **/
hotfix = {};

var scanHotFix = function (lParam) {
	import win;
	import com;
	HOTFIXINFO = class {
		int		nIndex;				/* 项目索引 */
		string	name;				/* 漏洞名称 */
		string	description;		/* 漏洞描述 */
		int		size;				/* 漏洞大小 */
		string	url;				/* 下载地址 */
		int		selected;			/* 是否选中 */
		int		preserved;
	} 
	var hotfixes = HOTFIXINFO();
	var hotfix_n = HOTFIXINFO();
	
	var objSession = com.CreateObject("Microsoft.Update.Session");
	var objSearcher = objSession.CreateUpdateSearcher();
	var objResults = objSearcher.Search("Type='SoftWare' And IsInstalled=0");
	var colUpdates = objResults.Updates;

	for(i = 0; colUpdates.Count - 1; 1){
		hotfix_n.name			= colUpdates.Item(i).Title;
		hotfix_n.description	= colUpdates.Item(i).Description;
		hotfix_n.size			= colUpdates.Item(i).MaxDownloadSize;
		hotfix_n.url			= colUpdates.Item(i).BundledUpdates.Item(0).DownloadContents.Item(0).DownloadURL;
		hotfix_n.selected		= colUpdates.Item(i).AutoSelectOnWebSites ? 1 : 0;
		if (!string.find(hotfix_n.name, "Advantage") && !string.find(hotfix_n.name, "正版"))	// 过滤正版验证
			::SendMessageByStruct(lParam.hWnd, lParam.uMsg, 0, hotfix_n);
	}
}

WM_MYADD			= 0x0400/*_WM_USER*/ + 100;
 
frmMain.button_scan.oncommand = function(id, event){
	hotfix = {};
	frmMain.button_scan.disabled = true;
	frmMain.pic_loader.hide = false;
	frmMain.static.hide = false;
	var hThread = thread.create(scanHotFix, { hWnd = frmMain.hwnd; uMsg = WM_MYADD });
	while (thread.wait(hThread, 1)) {
		win.delay(500);
	}
	frmMain.button_scan.disabled = false;
	frmMain.pic_loader.hide = true;
	frmMain.static.hide = true;
}

/* 初始化 loader */
for (i = 1; 8; 1)
	string.save("\loader-" + i + ".bmp", string.load("\res\loader-" + i + ".bmp"));

var loaderIndex = 1;	/* 加载图片索引 */
tmid = frmMain.addtimer(100, function(hwnd, msg, id, tick) {
	try { 
		frmMain.pic_loader.image = "\loader-" + loaderIndex + ".bmp";
	}
	catch (e) {
		frmMain.killtimer(tmid);
		io.print("WM_KILLTIMER");
	}
	loaderIndex = loaderIndex == 8 ? 1 : loaderIndex + 1;
});

 

frmMain.wndproc = function (hwnd, message, wparam, lparam){
	select (message){
		case WM_MYADD{
			var hotfix_n = HOTFIXINFO();
			raw.convert(topointer(lparam), hotfix_n);
			var nIndex = frmMain.listview.addItem(hotfix_n.name);
			
			if (hotfix_n.selected)
				frmMain.listview.select(nIndex) ;
			
			frmMain.listview.setItemText( fsys.formatSize(hotfix_n.size),nIndex,2); 
			table.push(hotfix, hotfix_n);
		} 
		case 0x2/*_WM_DESTROY*/{
			for (i = 1; 8; 1){
				frmMain.killtimer(tmid);
				frmMain.pic_loader.image = "";
				io.print(fsys.delete("\loader-" + i + ".bmp") );
			}
		}
	}
}

nIndexOld = -1;
tmid2 = frmMain.addtimer(100, function(hwnd, msg, id, tick){
	var nIndex = frmMain.listview.selIndex
	if ( nIndex? (nIndex != nIndexOld) ){
		nIndexOld = nIndex;
		frmMain.richedit.text =  '漏洞名称：' + hotfix[nIndex + 1].name + '\n\n';
		frmMain.richedit.text += '漏洞描述：\n' + hotfix[nIndex + 1].description + '\n\n';
		frmMain.richedit.text += '下载地址：' + hotfix[nIndex + 1].url;
	}
});

//自动换行
frmMain.richedit.wrap=true;
//支持超链接
frmMain.richedit.link=true;
 
frmMain.show(true);
win.loopMessage();
return winform;
