require "sync"

test0=1;

local function readlist1()
	local fname=GetFolder(ProfileFolder).."\\CharList.txt";
	local f = assert(io.open(fname,"r"))
	local g = f:read("*all")
	f:close()	
	km.setpref(TYPE_STRING,"kmeleon.character.list",g)
end

local function readlist2()
	local fname=GetFolder(ProfileFolder).."\\IETabList.txt";
	local f = assert(io.open(fname,"r"))
	local g = f:read("*all")
	f:close()	
	km.setpref(TYPE_STRING,"kmeleon.IETab.list",g)
end

function OnStartup(hwnd)
	sync()
	-- if getpref(TYPE_BOOL,"kmeleon.startupgroups.enabled")==true then
	--	gstartup()
	-- end
	proxy_menu()
	group_menu()
	undo_menu()
	useragent_sync()
	
	readlist1()
	readlist2()
end

function OnInit(hwnd)
--	if getpref(TYPE_BOOL,"general.firststart")==true then
--		skinSelect()
--		setpref(TYPE_BOOL,"general.firststart",false)
--	end
end	

function OnOpenWindow(hwnd)
--	title, url = GetDocInfo(hwnd)
--	if url=="about:blank" then
--		id("ID_SELECT_URL")
--	else
--		id("ID_SELECT_DOC")
--	end
end


function OnQuit()
--	setpref(TYPE_INT,"kmeleon.session.number",0)
	km.delpref("kmeleon.character.list");
	km.delpref("kmeleon.IETab.list")
end

--[[function OnExit()
	km.delpref("kmeleon.character.list");
	km.delpref("kmeleon.IETab.list");
end --]]
	
--[[ function OnLoad(hwnd)
		title, url = GetDocInfo(hwnd)
		if url~="about:blank" then
			plugin("gestures","view")
		end
end --]]

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)
	f:close()
end

local function readCharList()
	--local fname=GetFolder(ProfileFolder).."\\CharList.txt";
	--local f = assert(io.open(fname,"r"))
	--local g = f:read("*all")
	--f:close()
	g=km.getpref(TYPE_STRING,"kmeleon.character.list")
	return g;	
end

local function readIETabList()
	--local fname=GetFolder(ProfileFolder).."\\IETabList.txt";
	--local f = assert(io.open(fname,"r"))
	--local g = f:read("*all")
	--f:close()
	g=km.getpref(TYPE_STRING,"kmeleon.IETab.list")
	return g;	
end

-- Two-handle append helper: read the whole file, append one line, write back.
-- Was: `g = g..url.."\n"` which materialises a brand-new string concatenation
-- of the entire file (potentially many KB) per call. Now: read+append via
-- io.read("*a") returns one string, we write that string + the appended bytes
-- in two write() calls - no concatenation, one fewer transient allocation
-- per call. (The reads/opens are unchanged.)
local function append_line_to(fname, line)
	local fr = assert(io.open(fname, "r"))
	local existing = fr:read("*a") or ""
	fr:close()
	local fw = assert(io.open(fname, "w"))
	fw:write(existing)
	fw:write(line)
	fw:write("\n")
	fw:close()
end

local function writeCharList(url)
	append_line_to(GetFolder(ProfileFolder).."\\CharList.txt", url)
end

local function writeIETabList(url)
	append_line_to(GetFolder(ProfileFolder).."\\IETabList.txt", url)
end

function OnLoad()
	if(getpref(TYPE_BOOL,"kmeleon.prefetch.on")==true) then
		inJS("fetch.js")
	end
	--if km.GetGlobalVar(TYPE_STRING,"CHARSET")~="gb18030" then
	--	km.SetForceCharset("gb18030")
	--end
	local title, url = GetDocInfo(hwnd)
	local g = readCharList()
	if (string.find(g,url) and km.GetGlobalVar(TYPE_STRING,"CHARSET")~="gb18030") then 
		SetForceCharset("gb18030")
	end
end

function ForcePageChar(hwnd)
	local title,url = GetDocInfo(hwnd)
	writeCharList(url)
	km.SetForceCharset("gb2312")
	readlist1()
end

function AddIEtab(hwnd)
	local title,url = GetDocInfo(hwnd)
	writeIETabList(url)
	alert("This address is added into IETab Auto List","IETab", ICON_INFO) 
	readlist2()
end

function ForcePageChar1(hwnd)
	local title,url = GetDocInfo(hwnd)
	writeCharList(url)
	km.SetForceCharset("gb18030")
end

--function OnCreateTab(hwnd)
--	title, url = GetDocInfo(hwnd)
--	alert(url)
--end

--function OnLoading()
--end

function OnLocationChange()
	local title,url=GetDocInfo(hwnd)
	local g=readIETabList()
	if string.find(g,url) then
    		local url1="chrome://plugin/ietab.html?url="..url
    		km.NavigateTo(url1,OPEN_NORMAL)		
		--km.SetGlobalVar(TYPE_STRING,"URLBAR",url)
	end
	--if string.find(url,"chrome://plugin/ietab.html?url=") then
	--	alert(km.GetWindowVar(Window_LinkURL))
	--end
end
