require "lfs"

local function readskinlist(hwnd)
	local folder=GetFolder(SkinsFolder)
	local a, i, f
	local skins={}
	for i in lfs.dir(folder) do
		if (i~=".") and (i~="..")  then
			skins[i] = i	
		end	
		-- alert(skins[i])
	end
	return(skins)
end

local skins=readskinlist(hwnd);
local tabsOnOpenOption={"Open next current tab","Open last"};
local tabsOnCloseOption={"Select next tab","Select previous tab","Select previous selected tab"};
local tabsClosingLast={"Close the window","Blank the tab","Do nothing"};
local tabsOnClick={"Close tab","Open a new tab","Show menu"};
local startDisplay={"Blank Page","Home Page"};
local newDisplay={"Blank Page","Home Page","Current Page"};
--local skinList={"Shiny&Bright\\","Silverbird\\","Milk\\","Tango\\","MSN_EXP\\","Escape the Net\\","mezich\\"};
local skinList=skins;
--[[local m = {"ID_NAV_BACK","ID_NAV_FORWARD","ID_NAV_STOP","ID_NAV_RELOAD","ID_NAV_FORCE_RELOAD","ID_NEW_TAB","ID_TAB_NEXT","ID_TAB_PREV",
         "ID_NAV_HOME","ID_FILE_CLOSE","ID_CLOSE_TAB","ID_APP_EXIT","ID_OPEN_LINK_IN_NEWTAB","ID_OPEN_LINK_IN_BACKGROUNDTAB",
         "ID_EDIT_FINDNEXT","ID_EDIT_FINDPREV","ID_FONT_INCREASE","ID_FONT_DECREASE","bookmarks(Add)","favorites(Add)","hotlist(Add)",
         "luamacro(gotoNext)","luamacro(gotoPrevious)","luamacro(UpDir)","luamacro(TopDir)","luamacro(undo(1))","luamacro(URL)","luamacro(search.Search)","luamacro(pageup)","luamacro(pagedown)",
	 "luamacro(uphome)","luamacro(downend)"};
--]]

m={};
n={};

function get_gestures_list()
	local fname=BROWSERPATH.."lua\\gestures.cfg"
	local f = assert(io.open(fname,"r"))
	local gestures = f:read("*all")
	f:close()
	local len=string.len(gestures)
	local i=1
	while (len>1) do
		local a=string.find(gestures,",")
		m[i]=string.sub(gestures,1,a-1)
		gestures=string.sub(gestures,a+1)
		local b=string.find(gestures,";")
		n[i]=string.sub(gestures,1,b-1)
		gestures=string.sub(gestures,b+2)
		len=string.len(gestures)
		i=i+1
	end
	return m,n
end

m,n=get_gestures_list()

function get_display()
	local home=getpref(TYPE_STRING,"browser.startup.homepage");
	local start=getpref(TYPE_BOOL,"kmeleon.general.startHome");
	local newdisplay=getpref(TYPE_INT,"kmeleon.display.newWindowOpenAs");
	local skin=getpref(TYPE_STRING,"kmeleon.general.skinsCurrent");
	local background=getpref(TYPE_BOOL,"kmeleon.display.backgroundImageEnabled");
	local fontsize=getpref(TYPE_INT,"font.minimum-size.zh-CN");
	local font=getpref(TYPE_STRING,"font.default.zh-CN");
	local togglesize=getpref(TYPE_INT,"font.toggle.size");
	local siteicon=getpref(TYPE_BOOL,"kmeleon.favicons.show");
	local smoothscroll=getpref(TYPE_BOOL,"general.smoothScroll");
	local menudisplay=getpref(TYPE_BOOL,"kmeleon.toolband.Menu.visibility");
	local flashblock=getpref(TYPE_BOOL,"kmeleon.flash.on");
	local scrollspeed=getpref(TYPE_INT,"mousewheel.withnokey.numlines");
	local closebutton=getpref(TYPE_INT,"browser.tabs.closeButtons");	
  	local adblock=getpref(TYPE_BOOL,"extensions.adblockplus.enabled");
	local titlebar=getpref(TYPE_BOOL,"kmeleon.display.WindowTitleBar");
	if (start) then
		start={0,1};
	else
		start={1,0};
	end
	if (background) then background=1 else background=0 end
	if (siteicon) then siteicon=1 else siteicon=0 end
	if (smoothscroll) then smoothscroll=1 else smoothscroll=0 end
	if (menudisplay) then menudisplay=1 else menudisplay=0 end  
  if (adblock) then adblock=1 else adblock=0 end
	local temp1={{1,0,0},{0,1,0},{0,0,1}};
	newdisplay=temp1[newdisplay+1];
	if (flashblock==true) then flashblock=1 else flashblock=0 end
	if (titlebar==true) then titlebar=1 else titlebar=0 end
	return home,start,newdisplay,skin,fontsize,skinList,background,siteicon,smoothscroll,menudisplay,font,flashblock,scrollspeed,closebutton,togglesize,adblock,titlebar;
end

function get_general()
	local source=getpref(TYPE_STRING,"kmeleon.general.sourceCommand");
	local setfolder=getpref(TYPE_STRING,"kmeleon.general.settingsDir");
	local pluginfolder=getpref(TYPE_STRING,"kmeleon.general.pluginsDir");
	local httpver=getpref(TYPE_STRING,"network.http.version");
	local java=getpref(TYPE_BOOL,"security.enable_java");
	local js=getpref(TYPE_BOOL,"javascript.enabled");
	local password=getpref(TYPE_BOOL,"signon.rememberSignons");
	local resize=getpref(TYPE_BOOL,"kmeleon.display.disableResize");
	if (java) then java=1 else java=0 end
	if (js) then js=1 else js=0 end
	if (resize) then resize=0 else resize=1 end
	if (password) then password=1 else password=0 end
	return source,setfolder,pluginfolder,httpver,java,js,password,resize;
end

function get_privacy()
	local image=getpref(TYPE_INT,"permissions.default.image");
	local cookie=getpref(TYPE_INT,"network.cookie.cookieBehavior");
	local useragent=getpref(TYPE_STRING,"general.useragent.override");
	local useragent_string=getpref(TYPE_STRING,"general.useragent.override");
	local popup=getpref(TYPE_BOOL,"browser.popups.showPopupBlocker");
	local cachefav=getpref(TYPE_BOOL,"kmeleon.favicons.cached");
	local expiredays=getpref(TYPE_INT,"browser.history_expire_days");
	local agent={};
	agent[1]=getpref(TYPE_STRING,"kmeleon.privacy.useragent1.string");
	agent[2]=getpref(TYPE_STRING,"kmeleon.privacy.useragent2.string");
	agent[3]=getpref(TYPE_STRING,"kmeleon.privacy.useragent3.string");
	agent[4]=getpref(TYPE_STRING,"kmeleon.privacy.useragent4.string");
	agent[5]=getpref(TYPE_STRING,"kmeleon.privacy.useragent5.string");
	local temp1={{1,0,0},{0,1,0},{0,0,1}};
	image=temp1[image+1];
	local temp2={{1,0,0},{0,1,0},{0,0,1}};
	cookie=temp2[cookie+1];
	if (popup) then popup=0 else popup=1 end
	if (cachefav) then cachefav=1 else cachefav=0 end
	return image,cookie,useragent,useragent_string,popup,cachefav,expiredays,agent;
end

function get_cache()
	local memory=getpref(TYPE_INT,"browser.cache.memory.capacity");
	local disk=getpref(TYPE_INT,"browser.cache.disk.capacity");
	local cachedir=getpref(TYPE_STRING,"browser.cache.disk.parent_directory");
	local cachecomparison=getpref(TYPE_INT,"browser.cache.check_doc_frequency");
	local temp1={{1,0,0,0},{0,1,0,0},{0,0,1,0},{0,0,0,1}};
	cachecomparison=temp1[cachecomparison+1];
	return memory,disk,cachedir,cachecomparison;
end

function get_proxy()
	local type=getpref(TYPE_INT,"network.proxy.type");
	local http=getpref(TYPE_STRING,"network.proxy.http");
	local hport=getpref(TYPE_INT,"network.proxy.http_port");
	local ftp=getpref(TYPE_STRING,"network.proxy.ftp");
	local ftp_port=getpref(TYPE_INT,"network.proxy.ftp_port");
	local ssl=getpref(TYPE_STRING,"network.proxy.ssl");
	local ssl_port=getpref(TYPE_INT,"network.proxy.ssl_port");
	local gopher=getpref(TYPE_STRING,"network.proxy.gopher");
	local gopher_port=getpref(TYPE_INT,"network.proxy.gopher_port");
	local socks=getpref(TYPE_STRING,"network.proxy.socks");
	local socks_port=getpref(TYPE_INT,"network.proxy.socks_port");
	local socks_type=getpref(TYPE_INT,"network.proxy.socks_version");
	local noproxy=getpref(TYPE_STRING,"network.proxy.no_proxies_on");
	local temp1={{1,0,0},{0,1,0},{0,0,1}};
	type=temp1[type+1];
	socks_type=temp1[socks_type-4+1];
	return type,http,hport,ftp,ftp_port,ssl,ssl_port,gopher,gopher_port,socks,socks_port,socks_type,noproxy;
end
  
function get_tabs()
	local tabopen=getpref(TYPE_INT,"kmeleon.tabs.onOpenOption");
	local tabclose=getpref(TYPE_INT,"kmeleon.tabs.onCloseOption");
	local tablast=getpref(TYPE_INT,"kmeleon.tabs.onCloseLast");
	local tabdouble=getpref(TYPE_INT,"kmeleon.tabs.OnDoubleClick");
	local tabmiddle=getpref(TYPE_INT,"kmeleon.tabs.OnMiddleClick");
	local tabright=getpref(TYPE_INT,"kmeleon.tabs.OnRightClick");
	local tabmax=getpref(TYPE_INT,"kmeleon.tabs.maxWidth");
	local tabmin=getpref(TYPE_INT,"kmeleon.tabs.minWidth");
	local tabhide=getpref(TYPE_BOOL,"browser.tabs.autoHide");
	local tabconfirm=getpref(TYPE_BOOL,"browser.tabs.warnOnClose");
	local tabtitle=getpref(TYPE_BOOL,"kmeleon.tabs.useLoadingTitle");
	local tabbottom=getpref(TYPE_BOOL,"kmeleon.tabs.bottomBar");
	if(tabhide==true) then tabhide=1 else tabhide=0 end
	if(tabconfirm==true) then tabconfirm=1 else tabconfirm=0 end
	if(tabtitle==true) then tabtitle=1 else tabtitle=0 end
	if(tabbottom==true) then tabbottom=1 else tabbottom=0 end
	local temp1={{1,0,0},{0,1,0},{0,0,1}};
	--local temp2={{1,0,0},{0,0,1},{1,0,0}};
	tabopen=temp1[tabopen+1];
	tabclose=temp1[tabclose+1];
	tablast=temp1[tablast+1];
	if tabdouble==0 then tabdouble={1,0,0} elseif tabdouble==3 then tabdouble={0,1,0} elseif tabdouble==2 then tabdouble={0,0,1} end
	--tabdouble= temp2[tabdouble];
	tabmiddle= temp1[tabmiddle+1];
	tabright= temp1[tabright+1];
	return tabopen,tabclose,tablast,tabdouble,tabmiddle,tabright,tabmax,tabmin,tabhide,tabconfirm,tabtitle,tabbottom;
end

function get_download()
	local savedir=getpref(TYPE_STRING,"kmeleon.download.saveDir");
	local downloaddir=getpref(TYPE_STRING,"kmeleon.download.dir");
	local askopen=getpref(TYPE_BOOL,"kmeleon.download.askOpenSave");
	local uselocation=getpref(TYPE_BOOL,"kmeleon.download.useDownloadDir");
	local minimize=getpref(TYPE_BOOL,"kmeleon.download.showMinimizedDialog");
	local usetitle=getpref(TYPE_BOOL,"kmeleon.download.saveUseTitle");
	local closedialog=getpref(TYPE_BOOL,"kmeleon.download.closeDownloadDialog");
	local flashdialog=getpref(TYPE_BOOL,"kmeleon.download.flashWhenCompleted");
	local useoget=getpref(TYPE_BOOL,"kmeleon.download.useoget");
	if(askopen==true) then askopen=1 else askopen=0 end
	if(uselocation==true) then uselocation={0,1} else uselocation={1,0} end
	if(minimize==true) then minimize=1 else minimize=0 end
	if(usetitle==true) then usetitle=1 else usetitle=0 end
	if(closedialog==true) then closedialog=1 else closedialog=0 end
	if(flashdialog==true) then flashdialog=1 else flashdialog=0 end
	if(useoget==true) then useoget=1 else useoget=0 end 
	return savedir,downloaddir,askopen,uselocation,minimize,usetitle,closedialog,flashdialog,useoget;
end

function get_gestures()
	g = {"left","right","up","down","upleft","upright","downleft","downright","RL","LR"};
	local g_op={};
	local g_pref={};
	local g_id={"IDLEFT","IDRIGHT","IDUP","IDDOWN","IDUPLEFT","IDUPRIGHT","IDDOWNLEFT","IDDOWNRIGHT","IDLeftToRight","IDRightToLeft"};
	for i,v in ipairs(g) do
		g_op[i]="kmeleon.plugins.gestures."..v
		g_pref[i]=getpref(TYPE_STRING,g_op[i])
	end
	local tdrag=getpref(TYPE_INT,"kmeleon.plugins.gestures.tdrag");
	local drawline=getpref(TYPE_INT,"kmeleon.plugins.gestures.drawline");
	return g_pref,g_op,m,tdrag,drawline;
end

function get_hotkey()
	local h={"Ctrl+1","Ctrl+2","Ctrl+3","Ctrl+4","Ctrl+5","Ctrl+6","Ctrl+7","Ctrl+8","Ctrl+9"};
	local hotkey={"none","none","none","none","none","none","none","none","none",};
	for i,v in ipairs(hotkey) do
		hotkey[i]=getpref(TYPE_STRING,"kmeleon.plugins.macros.hotlink"..i..".url");
	end
	return hotkey;
end

function get_behavior()
	local r={},op;
	op="kmeleon.plugins.bookmarks.openurl"
	if getpref(TYPE_STRING,op)=="ID_OPEN_LINK" then r[1],r[2],r[3]=1,0,0
	elseif getpref(TYPE_STRING,op)=="ID_OPEN_LINK_IN_NEW_TAB" then r[1],r[2],r[3]=0,1,0
	elseif getpref(TYPE_STRING,op)=="ID_OPEN_LINK_IN_BACKGROUNDTAB" then r[1],r[2],r[3]=0,0,1 end
	op="kmeleon.general.openurl"
	if getpref(TYPE_STRING,op)=="ID_OPEN_LINK" then r[4],r[5],r[6]=1,0,0
	elseif getpref(TYPE_STRING,op)=="ID_OPEN_LINK_IN_NEW_TAB" then r[4],r[5],r[6]=0,1,0
	elseif getpref(TYPE_STRING,op)=="ID_OPEN_LINK_IN_BACKGROUNDTAB" then r[4],r[5],r[6]=0,0,1 end
	op="kmeleon.plugins.gestures.links"
	if getpref(TYPE_INT,op)==0 then r[10],r[11],r[12]=1,0,0
	elseif getpref(TYPE_INT,op)==2 then r[10],r[11],r[12]=0,1,0
	elseif getpref(TYPE_INT,op)==1 then r[10],r[11],r[12]=0,0,1 end
	op="kmeleon.plugins.gestures.drag"
	if getpref(TYPE_INT,op)==1 then r[13],r[14]=1,0
	elseif getpref(TYPE_INT,op)==0 then r[13],r[14]=0,1 end
	return r;
end

function get_advanced()
	local treacemonkey=getpref(TYPE_BOOL,"javascript.options.jit.content")
	local prefetch=getpref(TYPE_BOOL,"kmeleon.prefetch.on")
	if(treacemonkey) then treacemonkey=1 else treacemonkey=0 end
	if(prefetch) then prefetch=1 else prefetch=0 end
	return treacemonkey,prefetch;
end