require "preferenceGUI\\locals"

-- K-MeleonCCF hostperm.1 filter tools version 0.2

require "dialog"

local width = 395
local height = 185
local ffont="Arial"
local fpoint=10

-- local path = getpref(TYPE_STRING, "kmeleon.general.profileDir")
-- local path = "Profiles\\Profile\\"
-- local fname = path.."hostperm.1"

local folder=GetFolder(ProfileFolder)

local fname=folder.."\\hostperm.1"
local cssname=folder.."\\chrome\\ad.css"

--local fname=PREFPATH.."hostperm.1"

local function refresh(hwnd)
	local folder=GetFolder(ProfileFolder)
	local fname=folder.."\\chrome\\ad.css"
	local f = assert(io.open(fname,"r"))
	local css = f:read("*all")
	-- alert(css)
	km.InjectCSS(css,true)
	km.LoadCSS(folder.."\\chrome\\userContent.css")
end

local d = {

	{
		width = width,
		height = height,
		caption = "Filter",
		font = ffont, point = fpoint, -- font 
		{
			type = "combobox",  
			style = w32dlg.WS_BORDER +
			        w32dlg.WS_VSCROLL + w32dlg.WS_HSCROLL +
			        w32dlg.CBS_SIMPLE,
			height=130,
			width=280,			
			x = 5,  
			y = 5,  
			id = "IDtest",  
		},  
		{type="button",style=w32dlg.BS_RADIOBUTTON+w32dlg.WS_GROUP,x=300,y=5,width=80,height=10,id="IDRADIO1",caption=_ADADD}, 
		{type="button",style=w32dlg.BS_RADIOBUTTON,x=300,y=15,width=80,height=10,id="IDRADIO2",caption=_ADREMOVE},
		{type="button",style=w32dlg.BS_RADIOBUTTON,x=300,y=25,width=80,height=10,id="IDRADIO3",caption=_ADKEEP},              
	},
	{
		width = width,
		height = height,
		style = w32dlg.DS_CENTER, -- style 
		caption = _ADIMAGE ,
		font = ffont, point = fpoint, -- font 
		{
			type = "combobox",  
			style = w32dlg.WS_BORDER +
			        w32dlg.WS_VSCROLL + w32dlg.WS_HSCROLL +
			        w32dlg.CBS_SIMPLE,
			height=130,
			width=280,			
			x = 5,  
			y = 5,  
			id = "IDIMAGE",  
		},
		{type="button",style=w32dlg.BS_RADIOBUTTON+w32dlg.WS_GROUP,x=300,y=5,width=80,height=10,font="MS UI Gothic",point=20,id="IDRADIO1",caption=_ADADD}, 
		{type="button",style=w32dlg.BS_RADIOBUTTON,x=300,y=15,width=80,height=10,id="IDRADIO2",caption=_ADREMOVE},
		{type="button",style=w32dlg.BS_RADIOBUTTON,x=300,y=25,width=80,height=10,id="IDRADIO3",caption=_ADKEEP},  
         
	},
	{
		width = width,
		height = height,
		caption = _ADFRAME,
		font = ffont, point = fpoint, -- font 
		{
			type = "combobox",  
			style = w32dlg.WS_BORDER +
			        w32dlg.WS_VSCROLL + w32dlg.WS_HSCROLL +
			        w32dlg.CBS_SIMPLE,
			height=130,
			width=280,			
			x = 5,  
			y = 5,  
			id = "IDFRAME",  
		},    
		{type="button",style=w32dlg.BS_RADIOBUTTON+w32dlg.WS_GROUP,x=300,y=5,width=80,height=10,id="IDRADIO1",caption=_ADADD}, 
		{type="button",style=w32dlg.BS_RADIOBUTTON,x=300,y=15,width=80,height=10,id="IDRADIO2",caption=_ADREMOVE},
		{type="button",style=w32dlg.BS_RADIOBUTTON,x=300,y=25,width=80,height=10,id="IDRADIO3",caption=_ADKEEP},    
         
	},
	{
		width = width,
		height = height,
		caption = _ADFLASH,
		font = ffont, point = fpoint, -- font 
		{
			type = "combobox",  
			style = w32dlg.WS_BORDER +
			        w32dlg.WS_VSCROLL + w32dlg.WS_HSCROLL +
			        w32dlg.CBS_SIMPLE,
			height=130,
			width=280,			
			x = 5,  
			y = 5,  
			id = "IDFLASH",  
		},  
		{type="button",style=w32dlg.BS_RADIOBUTTON+w32dlg.WS_GROUP,x=300,y=5,width=80,height=10,id="IDRADIO1",caption=_ADADD}, 
		{type="button",style=w32dlg.BS_RADIOBUTTON,x=300,y=15,width=80,height=10,id="IDRADIO2",caption=_ADREMOVE},
		{type="button",style=w32dlg.BS_RADIOBUTTON,x=300,y=25,width=80,height=10,id="IDRADIO3",caption=_ADKEEP},              
	},
	{
		width = width,
		height = height,
		caption = _ADSCRIPT,
		font = ffont, point = fpoint, -- font 
		{
			type = "combobox",  
			style = w32dlg.WS_BORDER +
			        w32dlg.WS_VSCROLL + w32dlg.WS_HSCROLL +
			        w32dlg.CBS_SIMPLE,
			height=130,
			width=280,			
			x = 5,  
			y = 5,  
			id = "IDSCRIPT",  
		},  
		{type="button",style=w32dlg.BS_RADIOBUTTON+w32dlg.WS_GROUP,x=300,y=5,width=80,height=10,id="IDRADIO1",caption=_ADADD}, 
		{type="button",style=w32dlg.BS_RADIOBUTTON,x=300,y=15,width=80,height=10,id="IDRADIO2",caption=_ADREMOVE},
		{type="button",style=w32dlg.BS_RADIOBUTTON,x=300,y=25,width=80,height=10,id="IDRADIO3",caption=_ADKEEP},              
	},
	{
		width = width,
		height = height,
		caption = "CSS",
		font = ffont, point = fpoint, -- font 
		{
			type = "combobox",  
			style = w32dlg.WS_BORDER +
			        w32dlg.WS_VSCROLL + w32dlg.WS_HSCROLL +
			        w32dlg.CBS_SIMPLE,
			height=130,
			width=280,			
			x = 5,  
			y = 5,  
			id = "IDCSS",  
		},  
		{type="button",style=w32dlg.BS_RADIOBUTTON+w32dlg.WS_GROUP,x=300,y=5,width=80,height=10,id="IDRADIO1",caption=_ADADD}, 
		{type="button",style=w32dlg.BS_RADIOBUTTON,x=300,y=15,width=80,height=10,id="IDRADIO2",caption=_ADREMOVE},
		{type="button",style=w32dlg.BS_RADIOBUTTON,x=300,y=25,width=80,height=10,id="IDRADIO3",caption=_ADKEEP},              
	},



}

local function readfile()
	--local hostperm = pluginmsgex("utils","read",fname,TYPE_STRING)
	--io.input(fname)
	--local hostperm=io.read("*all")
	local f = assert(io.open(fname,"r"))
	local hostperm = f:read("*all")
	f:close()
	local f = assert(io.open(cssname,"r"))
	local css=f:read("*all")
	f:close()
	local g={}
	local i=1
	while(string.find(css,"src")) do
		local p=string.find(css,"src")
		css=string.sub(css,p+5)
		local p2=string.find(css,"]")
		g[i]=string.sub(css,2,p2-2)
		i=i+1		
	end
	local len=string.len(hostperm)
	local i=1
	local j=1
	local k=1
	local l=1
	local m=1
	local n=1
	local a={}
	local b={}
	local c={}
	local d={}
	local e={}
	local f={}
	while (len>1) do
		local p=string.find(hostperm,"host	")
		if p then
			hostperm=string.sub(hostperm,p+5)
			p=string.find(hostperm,"host	")
			if p then
				a[i]=string.sub(hostperm,1,p-2)
				local p1=string.find(a[i],"image	2")
				local p2=string.find(a[i],"subdocument	2")
				local p3=string.find(a[i],"object	2")
				local p4=string.find(a[i],"script	2")
				local p5=string.find(a[i],"stylesheet	2") 
				if p1 then
					b[j]=string.sub(a[i],p1+8)
					j=j+1
				elseif p2 then
					c[k]=string.sub(a[i],p2+14)
					k=k+1
				elseif p3 then
					d[l]=string.sub(a[i],p3+9)
					l=l+1
				elseif p4 then
					e[m]=string.sub(a[i],p4+9)
					m=m+1
				elseif p5 then
					f[n]=string.sub(a[i],p5+13)
					n=n+1
				end
				hostperm=string.sub(hostperm,p)
				i=i+1
				len=string.len(hostperm)
			else
				len=1
				a[i]=string.sub(hostperm,1,string.len(hostperm)-1)
				local p1=string.find(a[i],"image	2")
				local p2=string.find(a[i],"subdocument	2")
				local p3=string.find(a[i],"object	2")
				local p4=string.find(a[i],"script	2")
				local p5=string.find(a[i],"stylesheet	2") 
				if p1 then
					b[j]=string.sub(a[i],p1+8)
					j=j+1
				elseif p2 then
					c[k]=string.sub(a[i],p2+14)
					k=k+1
				elseif p3 then
					d[l]=string.sub(a[i],p3+9)
					l=l+1
				elseif p4 then
					e[m]=string.sub(a[i],p4+9)
					m=m+1
				elseif p5 then
					f[n]=string.sub(a[i],p5+13)
				end
			end
		end
	end
	return b,c,d,e,f,g
end

local function write_list(b,c,d,e,f,g)
	local hostperm=""
	local css=""
	for i,v in ipairs(b) do
		hostperm=hostperm.."host	image	2	"..b[i].."\n"
	end
	for i,v in ipairs(c) do
		hostperm=hostperm.."host	subdocument	2	"..c[i].."\n"
	end
	for i,v in ipairs(d) do
		hostperm=hostperm.."host	object	2	"..d[i].."\n"
	end
	for i,v in ipairs(e) do
		hostperm=hostperm.."host	script	2	"..e[i].."\n"
	end
	for i,v in ipairs(f) do
		hostperm=hostperm.."host	stylesheet	2	"..f[i].."\n"
	end
	for i,v in ipairs(g) do
		css=css.."\[src\*\=\""..g[i].."\"\]"..",\n"
	end
	css=css.."\[href\*\=\"ad4\.sina\.com\.cn\"\]".."\n"
	css=css.."\{ display: none \! important \}"
	if css then
		local f=assert(io.open(cssname,"w"))
		f:write(css)
		f:close()
	end
	if hostperm then
		--pluginmsg("utils","write",fname,hostperm)
		--io.output(fname)
		--io.write(hostperm)
		local f = assert(io.open(fname,"w"))
		f:write(hostperm)
		f:close()
	end
end

function addcss(hwnd)
	local b,c,d,e,f,g = readfile()
	local url1=GetGlobalVar(TYPE_STRING, "ImageURL")
	if url1 then
		if string.find(url1,"http://") then
			url1=string.sub(url1,string.find(url1,"http://")+7)
		end
		url1=prompt("block item in userContent.css?","Block item",url1)
		table.insert(g,url1)
		write_list(b,c,d,e,f,g)
		refresh(hwnd)
	end
end

function addcss2(hwnd)
	local b,c,d,e,f,g = readfile()
	local url1=GetGlobalVar(TYPE_STRING, "FrameURL")
	if url1 then
		if string.find(url1,"http://") then
			url1=string.sub(url1,string.find(url1,"http://")+7)
		end
		url1=prompt("block item in userContent.css?","Block item",url1)
		table.insert(g,url1)
		write_list(b,c,d,e,f,g)
		refresh(hwnd)
	end
end

function addcss3(hwnd)
	local b,c,d,e,f,g = readfile()
	local word = getclipboard()
	id("ID_EDIT_COPY")
	url1 = getclipboard()
	setclipboard(word)
	if url1 then
		if string.find(url1,"http://") then
			url1=string.sub(url1,string.find(url1,"http://")+7)
		end
		url1=prompt("block item in userContent.css?","Block item",url1)
		table.insert(g,url1)
		write_list(b,c,d,e,f,g)
		refresh(hwnd)
	end
end


function addcss4(hwnd)
	local b,c,d,e,f,g = readfile()
	local word = getclipboard()
	local url1=GetGlobalVar(TYPE_STRING, "LinkURL")
	if url1 then
		if string.find(url1,"http://") then
			url1=string.sub(url1,string.find(url1,"http://")+7)
		end
		url1=prompt("block item in userContent.css?","Block item",url1)
		table.insert(g,url1)
		write_list(b,c,d,e,f,g)
		refresh(hwnd)
	end
end

local p = create_propertysheet(d)

function property_adblock()
	local b,c,d,e,f,g = readfile()	
	r,rr=p("Filter",0,{
		{IDtest={nil,g},IDRADIO3=1},
		{IDIMAGE={nil,b},IDRADIO3=1},
		{IDFRAME={nil,c},IDRADIO3=1},
		{IDFLASH={nil,d},IDRADIO3=1},
		{IDSCRIPT={nil,e},IDRADIO3=1},
		{IDCSS={nil,f},IDRADIO3=1},		
	})
	local i=rr[2].IDIMAGE
	local j=rr[3].IDFRAME
	local k=rr[4].IDFLASH
	local l=rr[5].IDSCRIPT
	local m=rr[6].IDCSS
	local n=rr[1].IDtest
	if rr[2].IDRADIO1==1 and i then
		-- b[table.getn(b)+1]=i
		table.insert(b,i)
		write_list(b,c,d,e,f,g)
		--property_adblock()
	elseif rr[2].IDRADIO2==1 then
		for h,v in ipairs(b) do
			if b[h]==i then
				table.remove(b,h)
			end
		end
		write_list(b,c,d,e,f,g)
		--property_adblock()
	end
	if rr[3].IDRADIO1==1 and j then
		table.insert(c,j)
		write_list(b,c,d,e,f,g)
		--property_adblock()
	elseif rr[3].IDRADIO2==1 then
		for h,v in ipairs(c) do
			if c[h]==j then
				table.remove(c,h)
			end
		end
		write_list(b,c,d,e,f,g)
		--property_adblock()
	end
	if rr[4].IDRADIO1==1 and k then
		table.insert(d,k)
		write_list(b,c,d,e,f,g)
		--property_adblock()
	elseif rr[4].IDRADIO2==1 then
		for h,v in ipairs(d) do
			if d[h]==k then
				table.remove(d,h)
			end
		end
	write_list(b,c,d,e,f,g)
	--property_adblock()
	end
	if rr[5].IDRADIO1==1 and l then
		table.insert(e,l)
		write_list(b,c,d,e,f,g)
		--property_adblock()
	elseif rr[5].IDRADIO2==1 then
		for h,v in ipairs(e) do
			if e[h]==l then
				table.remove(e,h)
			end
		end
	write_list(b,c,d,e,f,g)
	--property_adblock()
	end
	if rr[6].IDRADIO1==1 and m then
		table.insert(f,m)
		write_list(b,c,d,e,f,g)
	elseif rr[6].IDRADIO2==1 then
		for h,v in ipairs(f) do
			if f[h]==m then
				table.remove(f,h)
			end
		end
	write_list(b,c,d,e,f,g)
	end
	if rr[1].IDRADIO1==1 and n then
		table.insert(g,n)
		write_list(b,c,d,e,f,g)
		refresh(hwnd)
 	elseif rr[1].IDRADIO2==1 then
		for h,v in ipairs(g) do
			if g[h]==n then
				table.remove(g,h)
				write_list(b,c,d,e,f,g)
				refresh(hwnd)
			end
		end
	write_list(b,c,d,e,f,g)
	end 
	write_list(b,c,d,e,f,g)
end


