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,
		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 f = assert(io.open(fname,"r"))
	local hostperm = f:read("*all")
	f:close()

	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
end

local function write_list(b,c,d,e,f)
	local hostperm=""
	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

	if hostperm then
		local f = assert(io.open(fname,"w"))
		f:write(hostperm)
		f:close()
	end
end

local p = create_propertysheet(d)

function property_adblock()
	local b,c,d,e,f= readfile()	

	r,rr=p("Filter",0,{
		{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},		
	})

	if r=="IDLOADBUTTON" then
		importlist()
		property_adblock()
	end

	local i=rr[1].IDIMAGE
	local j=rr[2].IDFRAME
	local k=rr[3].IDFLASH
	local l=rr[4].IDSCRIPT
	local m=rr[5].IDCSS

	if rr[1].IDRADIO1==1 and i then
		-- b[table.getn(b)+1]=i
		table.insert(b,i)
		write_list(b,c,d,e,f)
		--property_adblock()
	elseif rr[1].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)
		--property_adblock()
	end
	if rr[2].IDRADIO1==1 and j then
		table.insert(c,j)
		write_list(b,c,d,e,f)
		--property_adblock()
	elseif rr[2].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)
		--property_adblock()
	end
	if rr[3].IDRADIO1==1 and k then
		table.insert(d,k)
		write_list(b,c,d,e,f)
		--property_adblock()
	elseif rr[3].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)
	--property_adblock()
	end
	if rr[4].IDRADIO1==1 and l then
		table.insert(e,l)
		write_list(b,c,d,e,f)
		--property_adblock()
	elseif rr[4].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)
	--property_adblock()
	end
	if rr[5].IDRADIO1==1 and m then
		table.insert(f,m)
		write_list(b,c,d,e,f)
	elseif rr[5].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)
	end
end
