-- K-MeleonCCF Advanced Preference lua macros version 0.2

require "dialog";

require "preferenceGUI\\GUI";

require "preferenceGUI\\GetPref";

local p = create_propertysheet(d)

local profilepath=km.GetFolder(ProfileFolder)

-- Adblock helper: rewrite <profile>\adblock.txt with the given list of rules
-- (one per line, LF). The kplugin watches this file and re-parses on save -
-- changes take effect on the very next request, no restart needed.
local function write_adblock_rules(rules)
	local path = profilepath .. "\\adblock.txt"
	local f, err = io.open(path, "w")
	if not f then return false, err end
	for _, rule in ipairs(rules) do f:write(rule, "\n") end
	f:close()
	return true
end


function property_CCF()
	local r,rr;
	
	local tabopen,tabclose,tablast,tabdouble,tabmiddle,tabright,tabmax,tabmin,tabhide,tabconfirm,tabtitle,tabbottom=get_tabs();
	local home,start,newdisplay,skin,fontsize,skinList,background,siteicon,smoothscroll,menudisplay,font,scrollspeed,closebutton,togglesize,titlebar=get_display();
	local adblock_rules=get_adblock();
	local g_pref,g_op,m,tdrag,drawline=get_gestures();
	local hotkey=get_hotkey();
	local r=get_behavior();
	local source,setfolder,pluginfolder,httpver,java,js,password,resize=get_general();
	local image,cookie,useragent,useragent_string,popup,cachefav,expiredays,agent=get_privacy();
	local memory,disk,cachedir,cachecomparison=get_cache();
	local type,http,hport,ftp,ftp_port,ssl,ssl_port,gopher,gopher_port,socks,socks_port,socks_type,noproxy=get_proxy();
	local savedir,downloaddir,askopen,uselocation,minimize,usetitle,closedialog,flashdialog,useoget=get_download();
	local treacemonkey,prefetch=get_advanced();

	r,rr=p("Preference",0,{
		{
			IDHOME=home,
			IDRADIOSTARTDISPLAY1=start[1],IDRADIOSTARTDISPLAY2=start[2],
			IDRADIONEWDISPLAY1=newdisplay[1],IDRADIONEWDISPLAY2=newdisplay[2],IDRADIONEWDISPLAY3=newdisplay[3],
			IDSKIN={skin,skinList},
			IDBACKGROUND=background,
			IDFONTSIZE=fontsize,
			--IDSITEICON=siteicon,
			IDSMOOTHSCROLL=smoothscroll,
			IDDISPLAYMENU=menudisplay,
			IDSFONT=font,
			IDTOGGLESIZE=togglesize,
			IDSCROLLSPEED=scrollspeed,
			IDCLOSEBUTTON=closebutton,
			IDTITLEBAR=titlebar,
			IDFINDBAR=getpref(TYPE_INT,"kmeleon.display.findbartop"),
		},
		{
			IDSOURCE=source,
			IDSETTINGSFOLDER=setfolder,
			IDPLUGINSFOLDER=pluginfolder,
			IDHTTPVERSION=httpver,
			IDJAVA=java,
			IDJAVASCRIPT=js,
			IDPASSWORD=password,
			IDWINDOWRESIZE=resize,
			IDLANG=getpref(TYPE_STRING,"intl.accept_languages"),
			IDENCODING=getpref(TYPE_STRING,"intl.charset.default"),
		},
		{
			IDRADIOIMAGEDISPLAY1=image[1],IDRADIOCOOKIEDISPLAY2=image[2],IDRADIOCOOKIEDISPLAY3=image[3],
			IDRADIOCOOKIEDISPLAY1=cookie[1],IDRADIOCOOKIEDISPLAY2=cookie[2],IDRADIOCOOKIEDISPLAY3=cookie[3],
			IDIDENTIFYBROWSER={useragent,agent},
			IDUSERAGENTSTRING=useragent_string,
			IDPOPUP=popup,
			IDCACHEFAV=cachefav,
			IDREMEMBERDAY=expiredays,	
		},
		{
			IDMEMORYCACHE=memory,
			IDDISKCACHE=disk,
			IDCACHEDIR=cachedir,
			IDRADIOCACHE1=cachecomparison[1],IDRADIOCACHE2=cachecomparison[2],IDRADIOCACHE3=cachecomparison[3],IDRADIOCACHE4=cachecomparison[4],	
		},
		{
			IDDIRECT=type[1],IDMANUAL=type[2],IDUSEPAC=type[3],IDPROXYHTTP=http,IDHTTPPORT=hport,IDPROXYFTP=ftp,IDPROXYFTPPORT=ftp_port,
				      IDPROXYSSL=ssl,IDPROXYSSLPORT=ssl_port,IDPROXYGOPHER=gopher,IDGOPHERPORT=gopher_port,
				      IDPROXYSOCKS=socks,IDPROXYSOCKSPORT=socks_port,IDSOCKSTYPE1=socks_type[1],IDSOCKSTYPE2=socks_type[2],
				      IDNOPROXY=noproxy,
			IDPAC=getpref(TYPE_STRING,"network.proxy.autoconfig_url"),
		},
		{
			IDOPENOPTIONS1=tabopen[2],IDOPENOPTIONS2=tabopen[1],
			IDCLOSEOPTIONS1=tabclose[1],IDCLOSEOPTIONS2=tabclose[2],IDCLOSEOPTIONS3=tabclose[3],
			IDLASTTAB1=tablast[1],IDLASTTAB2=tablast[2],IDLASTTAB3=tablast[3],
			IDDOUBLECLICK1=tabdouble[1],IDDOUBLECLICK2=tabdouble[2],IDDOUBLECLICK3=tabdouble[3],
			IDMIDDLECLICK1=tabmiddle[1],IDMIDDLECLICK2=tabmiddle[2],IDMIDDLECLICK3=tabmiddle[3],
			IDRIGHTCLICK1=tabright[1],IDRIGHTCLICK2=tabright[2],IDRIGHTCLICK3=tabright[3],
			IDTABHIDE=tabhide,
			IDTABCONFIRM=tabconfirm,
			IDTABMAX=tabmax,
			IDTABMIN=tabmin,
			IDTABTITLE=tabtitle,
			IDTABBOTTOM=tabbottom,
		},
		{
			IDSAVEFOLDER=savedir,IDDOWNLOADFOLDER=downloaddir,
			IDASKOPEN=askopen,IDPROMPTLOCATION=uselocation[1],IDUSEDDOWNLOADDIR=uselocation[2],
			IDMINIMIZE=minimize,IDUSEWEBTITLE=usetitle,IDCLOSEDIALOG=closedialog,IDFLASHDIALOG=flashdialog,
			IDOGET=useoget,
		},
		{
			IDLEFT = {g_pref[1], m},
			IDRIGHT = {g_pref[2], m},
			IDUP = {g_pref[3], m},
			IDDOWN = {g_pref[4], m},
			IDUPLEFT = {g_pref[5], m},
			IDUPRIGHT = {g_pref[6], m},
			IDDOWNLEFT = {g_pref[7], m},
			IDDOWNRIGHT = {g_pref[8], m},
			IDLeftToRight = {g_pref[9], m},
			IDRightToLeft = {g_pref[10], m},
			IDTEXTDRAG = tdrag,
			IDDRAWLINE = drawline,
			IDMIDDLE = getpref(TYPE_INT,"kmeleon.plugins.gestures.middle"),
			
		},
		{
			IDAREA=getpref(TYPE_STRING,"kmeleon.weather.area.code"),
			IDCAPATH=getpref(TYPE_STRING, "cyberarticle.path.position"),
			IDBROWSERPATH=getpref(TYPE_STRING, "kmeleon.external.browser.position"),
			IDSET=getpref(TYPE_INT,"nglayout.initialpaint.delay"),
			IDTREACE=treacemonkey,
			IDFETCH=prefetch,
		},
		{
			IDCTRL1=hotkey[1],IDCTRL2=hotkey[2],IDCTRL3=hotkey[3],IDCTRL4=hotkey[4],IDCTRL5=hotkey[5],IDCTRL6=hotkey[6],
			IDCTRL7=hotkey[7],IDCTRL8=hotkey[8],IDCTRL9=hotkey[9],
		},
		{
			IDRADIO1=r[1],IDRADIO2=r[2],IDRADIO3=r[3],
			IDRADIO4=r[4],IDRADIO5=r[5],IDRADIO6=r[6],
			IDRADIO10=r[10],IDRADIO11=r[11],IDRADIO12=r[12],
			IDRADIO13=r[13],IDRADIO14=r[14],
		},
		{
			IDSEARCHNAME1=getpref(TYPE_STRING,"kmeleon.plugins.macros.search.engine0.name");
			IDSEARCH1=getpref(TYPE_STRING,"kmeleon.plugins.macros.search.engine0.url");
			IDSEARCHNAME2=getpref(TYPE_STRING,"kmeleon.plugins.macros.search.engine1.name");
			IDSEARCH2=getpref(TYPE_STRING,"kmeleon.plugins.macros.search.engine1.url");
			IDSEARCHNAME3=getpref(TYPE_STRING,"kmeleon.plugins.macros.search.engine2.name");
			IDSEARCH3=getpref(TYPE_STRING,"kmeleon.plugins.macros.search.engine2.url");
			IDSEARCHNAME4=getpref(TYPE_STRING,"kmeleon.plugins.macros.search.engine3.name");
			IDSEARCH4=getpref(TYPE_STRING,"kmeleon.plugins.macros.search.engine3.url");
			IDSEARCHNAME5=getpref(TYPE_STRING,"kmeleon.plugins.macros.search.engine4.name");
			IDSEARCH5=getpref(TYPE_STRING,"kmeleon.plugins.macros.search.engine4.url");
			IDSEARCHNAME6=getpref(TYPE_STRING,"kmeleon.plugins.macros.search.engine5.name");
			IDSEARCH6=getpref(TYPE_STRING,"kmeleon.plugins.macros.search.engine5.url");
			IDSEARCHNAME7=getpref(TYPE_STRING,"kmeleon.plugins.macros.search.engine6.name");
			IDSEARCH7=getpref(TYPE_STRING,"kmeleon.plugins.macros.search.engine6.url");
			IDSEARCHNAME8=getpref(TYPE_STRING,"kmeleon.plugins.macros.search.engine7.name");
			IDSEARCH8=getpref(TYPE_STRING,"kmeleon.plugins.macros.search.engine7.url");
			IDSEARCHNAME9=getpref(TYPE_STRING,"kmeleon.plugins.macros.search.engine8.name");
			IDSEARCH9=getpref(TYPE_STRING,"kmeleon.plugins.macros.search.engine8.url");
		},
		-- Adblock tab (rr[13]): multi-line edit field holds the whole
		-- adblock.txt content, one rule per line. CRLF separators because
		-- Windows EDIT controls use CR/LF internally; we normalize on apply.
		{
			IDADBLOCKRULES = table.concat(adblock_rules, "\r\n"),
		},
	})
	if r==1 then
			setpref(TYPE_STRING,"browser.startup.homepage",rr[1].IDHOME);
			setpref(TYPE_STRING,"kmeleon.general.homePage",rr[1].IDHOME);
			if (rr[1].IDRADIOSTARTDISPLAY1==1) then setpref(TYPE_BOOL,"kmeleon.general.startHome",false) else setpref(TYPE_BOOL,"kmeleon.general.startHome",true) end
			if (rr[1].IDRADIONEWDISPLAY1==1) then setpref(TYPE_INT,"kmeleon.display.newWindowOpenAs",0)
				elseif (rr[1].IDRADIONEWDISPLAY2==1) then setpref(TYPE_INT,"kmeleon.display.newWindowOpenAs",1)
				else setpref(TYPE_INT,"kmeleon.display.newWindowOpenAs",2)
			end
			setpref(TYPE_STRING,"kmeleon.general.skinsCurrent",rr[1].IDSKIN);
			if (rr[1].IDBACKGROUND==1) then setpref(TYPE_BOOL,"kmeleon.display.backgroundImageEnabled",true) else setpref(TYPE_BOOL,"kmeleon.display.backgroundImageEnabled",false) end
			local fontsize=rr[1].IDFONTSIZE
			setpref(TYPE_INT,"font.minimum-size.x-western",fontsize)
			setpref(TYPE_INT,"font.minimum-size.x-unicode", fontsize)
			setpref(TYPE_INT,"font.minimum-size.zh-CN",fontsize)
			setpref(TYPE_INT,"font.minimum-size.zh-TW",fontsize)
			setpref(TYPE_INT,"font.toggle.size",rr[1].IDTOGGLESIZE)
			--if (rr[1].IDSITEICON==1) then setpref(TYPE_BOOL,"kmeleon.favicons.show",true) else setpref(TYPE_BOOL,"kmeleon.favicons.show",false) end	
			if (rr[1].IDSMOOTHSCROLL==1) then setpref(TYPE_BOOL,"general.smoothScroll",true) else setpref(TYPE_BOOL,"general.smoothScroll",false) end
			if(rr[1].IDDISPLAYMENU==1) then setpref(TYPE_BOOL,"kmeleon.toolband.Menu.visibility",true) else setpref(TYPE_BOOL,"kmeleon.toolband.Menu.visibility",false) end	
			setpref(TYPE_STRING,"font.default.zh-CN",rr[1].IDSFONT)
			setpref(TYPE_INT,"mousewheel.withnokey.numlines",rr[1].IDSCROLLSPEED);
			if(rr[1].IDCLOSEBUTTON==1) then setpref(TYPE_INT,"browser.tabs.closeButtons",1) else setpref(TYPE_INT,"browser.tabs.closeButtons",0) end
			if(rr[1].IDTITLEBAR==1) then setpref(TYPE_BOOL,"kmeleon.display.WindowTitleBar",true) else setpref(TYPE_BOOL,"kmeleon.display.WindowTitleBar",false) end
			setpref(TYPE_INT,"kmeleon.display.findbartop",rr[1].IDFINDBAR);
			--
			setpref(TYPE_STRING,"kmeleon.general.sourceCommand",rr[2].IDSOURCE);
			setpref(TYPE_STRING,"kmeleon.general.settingsDir",rr[2].IDSETTINGSFOLDER);
			setpref(TYPE_STRING,"kmeleon.general.pluginsDir",rr[2].IDPLUGINSFOLDER);
			setpref(TYPE_STRING,"network.http.version",rr[2].IDHTTPVERSION);
			if (rr[2].IDJAVA==1) then setpref(TYPE_BOOL,"security.enable_java",true) else setpref(TYPE_BOOL,"security.enable_java",false) end
			if (rr[2].IDJAVASCRIPT==1) then setpref(TYPE_BOOL,"javascript.enabled",true) else setpref(TYPE_BOOL,"javascript.enabled",false) end
			if (rr[2].IDPASSWORD==1) then setpref(TYPE_BOOL,"signon.rememberSignons",true) else setpref(TYPE_BOOL,"signon.rememberSignons",false) end
			if(rr[2].IDWINDOWRESIZE==1) then setpref(TYPE_BOOL,"kmeleon.display.disableResize",false) else setpref(TYPE_BOOL,"kmeleon.display.disableResize",true) end
			setpref(TYPE_STRING,"intl.accept_languages",rr[2].IDLANG);
			setpref(TYPE_STRING,"intl.charset.default",rr[2].IDENCODING);
			--
			if (rr[3].IDRADIOIMAGEDISPLAY1==1) then setpref(TYPE_INT,"permissions.default.image",0) 
				elseif (rr[3].IDRADIOIMAGEDISPLAY2==1) then setpref(TYPE_INT,"permissions.default.image",1) 
				else setpref(TYPE_INT,"permissions.default.image",2) 
			end
			if (rr[3].IDRADIOCOOKIEDISPLAY1==1) then setpref(TYPE_INT,"network.cookie.cookieBehavior",0) 
				elseif (rr[3].IDRADIOCOOKIEDISPLAY2==1) then setpref(TYPE_INT,"network.cookie.cookieBehavior",1) 
				else setpref(TYPE_INT,"network.cookie.cookieBehavior",2) 
			end
			setpref(TYPE_STRING,"general.useragent.override",rr[3].IDIDENTIFYBROWSER);
			if(rr[3].IDPOPUP==1) then setpref(TYPE_BOOL,"browser.popups.showPopupBlocker",false) else setpref(TYPE_BOOL,"browser.popups.showPopupBlocker",true) end
			if(rr[3].IDCACHEFAV==1) then setpref(TYPE_BOOL,"kmeleon.favicons.cached",true) else setpref(TYPE_BOOL,"kmeleon.favicons.cached",false) end
			setpref(TYPE_INT,"browser.history_expire_days",rr[3].IDREMEMBERDAY);
			--
			setpref(TYPE_INT,"browser.cache.memory.capacity",rr[4].IDMEMORYCACHE);
			setpref(TYPE_INT,"browser.cache.disk.capacity",rr[4].IDDISKCACHE);
			setpref(TYPE_STRING,"browser.cache.disk.parent_directory",rr[4].IDCACHEDIR);
			if(rr[4].IDRADIOCACHE1==1) then setpref(TYPE_INT,"browser.cache.check_doc_frequency",0)
				elseif(rr[4].IDRADIOCACHE2==1) then setpref(TYPE_INT,"browser.cache.check_doc_frequency",1)
				elseif(rr[4].IDRADIOCACHE3==1) then setpref(TYPE_INT,"browser.cache.check_doc_frequency",2)
				else setpref(TYPE_INT,"browser.cache.check_doc_frequency",3)
			end
			--
			if(rr[5].IDDIRECT==1) then setpref(TYPE_INT,"network.proxy.type",0) elseif (rr[5].IDMANUAL==1) then setpref(TYPE_INT,"network.proxy.type",1) else setpref(TYPE_INT,"network.proxy.type",2) end
			setpref(TYPE_STRING,"network.proxy.http",rr[5].IDPROXYHTTP);
			setpref(TYPE_INT,"network.proxy.http_port",rr[5].IDHTTPPORT);
			setpref(TYPE_STRING,"network.proxy.ftp",rr[5].IDPROXYFTP);
			setpref(TYPE_INT,"network.proxy.ftp_port",rr[5].IDPROXYFTPPORT);
			setpref(TYPE_STRING,"network.proxy.ssl",rr[5].IDPROXYSSL);
			setpref(TYPE_INT,"network.proxy.ssl_port",rr[5].IDPROXYSSLPORT);
			setpref(TYPE_STRING,"network.proxy.gopher",rr[5].IDPROXYGOPHER);
			setpref(TYPE_INT,"network.proxy.gopher_port",rr[5].IDGOPHERPORT);
			setpref(TYPE_STRING,"network.proxy.socks",rr[5].IDPROXYSOCKS);
			setpref(TYPE_INT,"network.proxy.socks_port",rr[5].IDPROXYSOCKSPORT);
			if(rr[5].IDSOCKSTYPE1==1) then setpref(TYPE_INT,"network.proxy.socks_version",4) else setpref(TYPE_INT,"network.proxy.socks_version",5) end
			setpref(TYPE_STRING,"network.proxy.no_proxies_on",rr[5].IDNOPROXY);
			setpref(TYPE_STRING,"network.proxy.autoconfig_url",rr[5].IDPAC);
			--
			if(rr[6].IDOPENOPTIONS1==1) then setpref(TYPE_INT,"kmeleon.tabs.onOpenOption",1) else setpref(TYPE_INT,"kmeleon.tabs.onOpenOption",0) end
			if(rr[6].IDCLOSEOPTIONS1==1) then setpref(TYPE_INT,"kmeleon.tabs.onCloseOption",0)
				elseif(rr[6].IDCLOSEOPTIONS2==1) then setpref(TYPE_INT,"kmeleon.tabs.onCloseOption",1)
				else setpref(TYPE_INT,"kmeleon.tabs.onCloseOption",2)
			end
			if(rr[6].IDLASTTAB1==1) then setpref(TYPE_INT,"kmeleon.tabs.onCloseLast",0)
				elseif(rr[6].IDLASTTAB2==1) then setpref(TYPE_INT,"kmeleon.tabs.onCloseLast",1)
				else setpref(TYPE_INT,"kmeleon.tabs.onCloseLast",2)
			end
			if(rr[6].IDDOUBLECLICK1==1) then setpref(TYPE_INT,"kmeleon.tabs.OnDoubleClick",0)
				elseif(rr[6].IDDOUBLECLICK2==1) then setpref(TYPE_INT,"kmeleon.tabs.OnDoubleClick",3)
				else setpref(TYPE_INT,"kmeleon.tabs.OnDoubleClick",2)
			end
			if(rr[6].IDMIDDLECLICK1==1) then setpref(TYPE_INT,"kmeleon.tabs.OnMiddleClick",0)
				elseif(rr[6].IDMIDDLECLICK2==1) then setpref(TYPE_INT,"kmeleon.tabs.OnMiddleClick",1)
				else setpref(TYPE_INT,"kmeleon.tabs.OnMiddleClick",2)
			end
			if(rr[6].IDRIGHTCLICK1==1) then setpref(TYPE_INT,"kmeleon.tabs.OnRightClick",0)
				elseif(rr[6].IDRIGHTCLICK2==1) then setpref(TYPE_INT,"kmeleon.tabs.OnRightClick",1)
				else setpref(TYPE_INT,"kmeleon.tabs.OnRightClick",2)
			end
			setpref(TYPE_INT,"kmeleon.tabs.maxWidth",rr[6].IDTABMAX);
			setpref(TYPE_INT,"kmeleon.tabs.minWidth",rr[6].IDTABMIN);
			if(rr[6].IDTABHIDE==1) then setpref(TYPE_BOOL,"browser.tabs.autoHide",true) else setpref(TYPE_BOOL,"browser.tabs.autoHide",false) end
			if(rr[6].IDTABCONFIRM==1) then setpref(TYPE_BOOL,"browser.tabs.warnOnClose",true) else setpref(TYPE_BOOL,"browser.tabs.warnOnClose",false) end
			if(rr[6].IDTABTITLE==1) then setpref(TYPE_BOOL,"kmeleon.tabs.useLoadingTitle",true) else setpref(TYPE_BOOL,"kmeleon.tabs.useLoadingTitle",false) end
			if(rr[6].IDTABBOTTOM==1) then setpref(TYPE_BOOL,"kmeleon.tabs.bottomBar",true) else setpref(TYPE_BOOL,"kmeleon.tabs.bottomBar",false) end
			--))
			
			setpref(TYPE_STRING,g_op[1],rr[8].IDLEFT)
			setpref(TYPE_STRING,g_op[2],rr[8].IDRIGHT)
			setpref(TYPE_STRING,g_op[3],rr[8].IDUP)
			setpref(TYPE_STRING,g_op[4],rr[8].IDDOWN)
			setpref(TYPE_STRING,g_op[5],rr[8].IDUPLEFT)
			setpref(TYPE_STRING,g_op[6],rr[8].IDUPRIGHT)
			setpref(TYPE_STRING,g_op[7],rr[8].IDDOWNLEFT)
			setpref(TYPE_STRING,g_op[8],rr[8].IDDOWNRIGHT)
			setpref(TYPE_STRING,g_op[9],rr[8].IDLeftToRight)
			setpref(TYPE_STRING,g_op[10],rr[8].IDRightToLeft)
			setpref(TYPE_INT,"kmeleon.plugins.gestures.tdrag",rr[8].IDTEXTDRAG)
			setpref(TYPE_INT,"kmeleon.plugins.gestures.drawline",rr[8].IDDRAWLINE)
			setpref(TYPE_INT,"kmeleon.plugins.gestures.middle",rr[8].IDMIDDLE)
			--
			setpref(TYPE_STRING,"kmeleon.plugins.macros.hotlink1.url",rr[10].IDCTRL1)
			setpref(TYPE_STRING,"kmeleon.plugins.macros.hotlink2.url",rr[10].IDCTRL2)
			setpref(TYPE_STRING,"kmeleon.plugins.macros.hotlink3.url",rr[10].IDCTRL3)
			setpref(TYPE_STRING,"kmeleon.plugins.macros.hotlink4.url",rr[10].IDCTRL4)
			setpref(TYPE_STRING,"kmeleon.plugins.macros.hotlink5.url",rr[10].IDCTRL5)
			setpref(TYPE_STRING,"kmeleon.plugins.macros.hotlink6.url",rr[10].IDCTRL6)
			setpref(TYPE_STRING,"kmeleon.plugins.macros.hotlink7.url",rr[10].IDCTRL7)
			setpref(TYPE_STRING,"kmeleon.plugins.macros.hotlink8.url",rr[10].IDCTRL8)
			setpref(TYPE_STRING,"kmeleon.plugins.macros.hotlink9.url",rr[10].IDCTRL9)
			--
			setpref(TYPE_INT,"kmeleon.plugins.layers.maxWidth",rr[6].IDTABMAX)
			setpref(TYPE_INT,"kmeleon.plugins.layers.minWidth",rr[6].IDTABMIN)
			setpref(TYPE_STRING,"kmeleon.weather.area.code",rr[9].IDAREA)
			setpref(TYPE_STRING,"cyberarticle.path.position",rr[9].IDCAPATH)
			setpref(TYPE_STRING,"kmeleon.external.browser.position",rr[9].IDBROWSERPATH)
			setpref(TYPE_INT,"nglayout.initialpaint.delay",rr[9].IDSET)
			if (rr[9].IDTREACE==1) then setpref(TYPE_BOOL,"javascript.options.jit.content",true) else setpref(TYPE_BOOL,"javascript.options.jit.content",false) end
			if (rr[9].IDFETCH==1) then setpref(TYPE_BOOL,"kmeleon.prefetch.on",true) else setpref(TYPE_BOOL,"kmeleon.prefetch.on",false) end
			--
			if rr[11].IDRADIO1==1 then setpref(TYPE_STRING,"kmeleon.plugins.bookmarks.openurl", "ID_OPEN_LINK")
			elseif rr[11].IDRADIO2==1 then setpref(TYPE_STRING,"kmeleon.plugins.bookmarks.openurl", "ID_OPEN_LINK_IN_NEW_TAB")
			elseif rr[11].IDRADIO3==1 then setpref(TYPE_STRING,"kmeleon.plugins.bookmarks.openurl", "ID_OPEN_LINK_IN_BACKGROUNDTAB") end
			if rr[11].IDRADIO4==1 then setpref(TYPE_STRING,"kmeleon.general.openurl", "ID_OPEN_LINK")
			elseif rr[11].IDRADIO5==1 then setpref(TYPE_STRING,"kmeleon.general.openurl", "ID_OPEN_LINK_IN_NEW_TAB")
			elseif rr[11].IDRADIO6==1 then setpref(TYPE_STRING,"kmeleon.general.openurl", "ID_OPEN_LINK_IN_BACKGROUNDTAB") end
			if rr[11].IDRADIO7==1 then setpref(TYPE_STRING,"kmeleon.plugins.searchbar.openurl", "ID_OPEN_LINK")
			elseif rr[11].IDRADIO8==1 then setpref(TYPE_STRING,"kmeleon.plugins.searchbar.openurl", "ID_OPEN_LINK_IN_NEW_TAB")
			elseif rr[11].IDRADIO9==1 then setpref(TYPE_STRING,"kmeleon.plugins.searchbar.openurl", "ID_OPEN_LINK_IN_BACKGROUNDTAB") end
			if rr[11].IDRADIO10==1 then setpref(TYPE_INT,"kmeleon.plugins.gestures.links", 0)
			elseif rr[11].IDRADIO11==1 then setpref(TYPE_INT,"kmeleon.plugins.gestures.links", 2)
			elseif rr[11].IDRADIO12==1 then setpref(TYPE_INT,"kmeleon.plugins.gestures.links", 1) end
			if rr[11].IDRADIO13==1 then setpref(TYPE_INT,"kmeleon.plugins.gestures.drag", 1)
			elseif rr[11].IDRADIO14==1 then setpref(TYPE_INT,"kmeleon.plugins.gestures.drag", 0) end
			--
			setpref(TYPE_STRING,"kmeleon.download.saveDir",rr[7].IDSAVEFOLDER)
			setpref(TYPE_STRING,"kmeleon.download.dir",rr[7].IDDOWNLOADFOLDER)
			if (rr[7].IDASKOPEN==1) then setpref(TYPE_BOOL,"kmeleon.download.askOpenSave",true) else setpref(TYPE_BOOL,"kmeleon.download.askOpenSave",false) end
			if (rr[7].IDPROMPTLOCATION==1) then setpref(TYPE_BOOL, "kmeleon.download.useDownloadDir", false) end
			if (rr[7].IDUSEDDOWNLOADDIR==1) then setpref(TYPE_BOOL, "kmeleon.download.useDownloadDir", true) end
			if (rr[7].IDMINIMIZE==1) then setpref(TYPE_BOOL,"kmeleon.download.showMinimizedDialog",true) else setpref(TYPE_BOOL,"kmeleon.download.showMinimizedDialog",false) end 
			if (rr[7].IDUSEWEBTITLE==1) then setpref(TYPE_BOOL,"kmeleon.download.saveUseTitle",true) else setpref(TYPE_BOOL,"kmeleon.download.saveUseTitle",false) end
			if (rr[7].IDCLOSEDIALOG==1) then setpref(TYPE_BOOL,"kmeleon.download.closeDownloadDialog",true) else setpref(TYPE_BOOL,"kmeleon.download.closeDownloadDialog",false) end
			if (rr[7].IDFLASHDIALOG==1) then setpref(TYPE_BOOL,"kmeleon.download.flashWhenCompleted",true) else setpref(TYPE_BOOL,"kmeleon.download.flashWhenCompleted",false) end
			if (rr[7].IDOGET==1) then setpref(TYPE_BOOL,"kmeleon.download.useoget",true);os.rename(BROWSERPATH.."plugins\\xnpoget.dll",BROWSERPATH.."plugins\\npoget.dll"); else setpref(TYPE_BOOL,"kmeleon.download.useoget",false);os.rename(BROWSERPATH.."plugins\\npoget.dll",BROWSERPATH.."plugins\\xnpoget.dll"); end
			--
			setpref(TYPE_STRING,"kmeleon.plugins.macros.search.engine0.name",rr[12].IDSEARCHNAME1);
			setpref(TYPE_STRING,"kmeleon.plugins.macros.search.engine0.url",rr[12].IDSEARCH1);
			setpref(TYPE_STRING,"kmeleon.plugins.macros.search.engine1.name",rr[12].IDSEARCHNAME2);
			setpref(TYPE_STRING,"kmeleon.plugins.macros.search.engine1.url",rr[12].IDSEARCH2);
			setpref(TYPE_STRING,"kmeleon.plugins.macros.search.engine2.name",rr[12].IDSEARCHNAME3);
			setpref(TYPE_STRING,"kmeleon.plugins.macros.search.engine2.url",rr[12].IDSEARCH3);
			setpref(TYPE_STRING,"kmeleon.plugins.macros.search.engine3.name",rr[12].IDSEARCHNAME4);
			setpref(TYPE_STRING,"kmeleon.plugins.macros.search.engine3.url",rr[12].IDSEARCH4);
			setpref(TYPE_STRING,"kmeleon.plugins.macros.search.engine4.name",rr[12].IDSEARCHNAME5);
			setpref(TYPE_STRING,"kmeleon.plugins.macros.search.engine4.url",rr[12].IDSEARCH5);
			setpref(TYPE_STRING,"kmeleon.plugins.macros.search.engine5.name",rr[12].IDSEARCHNAME6);
			setpref(TYPE_STRING,"kmeleon.plugins.macros.search.engine5.url",rr[12].IDSEARCH6);
			setpref(TYPE_STRING,"kmeleon.plugins.macros.search.engine6.name",rr[12].IDSEARCHNAME7);
			setpref(TYPE_STRING,"kmeleon.plugins.macros.search.engine6.url",rr[12].IDSEARCH7);
			setpref(TYPE_STRING,"kmeleon.plugins.macros.search.engine7.name",rr[12].IDSEARCHNAME8);
			setpref(TYPE_STRING,"kmeleon.plugins.macros.search.engine7.url",rr[12].IDSEARCH8);
			setpref(TYPE_STRING,"kmeleon.plugins.macros.search.engine8.name",rr[12].IDSEARCHNAME9);
			setpref(TYPE_STRING,"kmeleon.plugins.macros.search.engine8.url",rr[12].IDSEARCH9);
			--
			-- Adblock tab (rr[13]): the multi-line edit holds the whole file
			-- as text. Split it into lines, trim trailing empties, write back.
			-- The kplugin's file watcher then hot-reloads the rules - changes
			-- take effect within ~150ms, no restart needed.
			--
			-- Skip the write if the edit text matches the on-disk snapshot we
			-- loaded - avoids touching the file (and triggering the watcher)
			-- on a "no-op" OK. Useful when the user just clicked through tabs.
			local txt = rr[13].IDADBLOCKRULES or ""
			local lines = {}
			local n = 0
			-- EDIT controls store \r\n internally; tolerate either.
			-- Direct indexing (n+=1; lines[n] = …) instead of table.insert:
			-- avoids the per-call length-lookup that table.insert does, and
			-- also drops the post-loop trailing-empty trim by simply not
			-- appending empties at the very end.
			for line in (txt .. "\n"):gmatch("([^\n]*)\n") do
				if line:sub(-1) == "\r" then line = line:sub(1, -2) end
				n = n + 1
				lines[n] = line
			end
			while n > 0 and lines[n] == "" do lines[n] = nil; n = n - 1 end
			-- Compare to the snapshot loaded at dialog open.
			local same = (#lines == #adblock_rules)
			if same then
				for i = 1, #lines do
					if lines[i] ~= adblock_rules[i] then same = false; break end
				end
			end
			if not same then
				write_adblock_rules(lines)
			end
		end
end