--[[
--extensions.rdf中描述的插件信息
  <RDF:Description RDF:about="urn:mozilla:item:{1B33E42F-EF14-4cd3-B6DC-174571C4349C}"
                   NS1:name="Thunder Extension"
                   NS1:version="3.6"
                   NS1:newVersion="3.6"
                   NS1:installLocation="app-profile"
                   NS1:appDisabled="true">
--]]

function CheckFirefox()
	
	local FirefoxObj = XLGetObject("Xunlei.FirefoxSupport.Obj")
	local bExist = FirefoxObj:IsInstallFirefox()
	
	--检测用户是否安装firefox
	if bExist ~= true then
		return 0
	end
	local exeName = FirefoxObj:GetFirefoxPathFromRegistry()
	if not exeName then
		return 0
	end
	--检测firefox.exe的文件版本号
	exeName = exeName .. "\\firefox.exe"
	local Obj = XLGetObject("Xunlei.OSShell.Obj")	
	local ver = Obj:GetFileVerStr(exeName)
	local i, j= string.find(ver, "%.")
	if i==nil or i<1 then return 0 end
	local nVer = string.sub(ver, 1, i-1)	
	local nver = nVer + 0
	if nver>1 then return 0 end
	
	--获取profiles.ini配置文件
	local path = Obj:GetAppDataPath()
	local iniPath = path.."\\Mozilla\\Firefox\\profiles.ini"
	local Sections={}
	Sections=Obj:ReadSections(iniPath)
	local section="Profile0"
	for i=1,#Sections do 
		--XLMessageBox(Sections[i])
		local nDefault = Obj:ReadInteger(iniPath, Sections[i], "default", 0)
		if nDefault==1 then
			section=Sections[i]
			break
		end
	end
	local strValue = Obj:ReadStringUtf8(iniPath, section, "Path", "")
	if strValue=="" then return 0 end
	local binstall = false
	local i= string.match(strValue, "^%a:\\")
	local dir=""	
	if i~=nil then		
		dir=strValue
	else		
	 	dir= path.."\\Mozilla\\Firefox\\"..strValue
	end
	
	--检测install.rdf和extensions.rdf是否存在
	local instrdf=dir.."\\extensions\\{1B33E42F-EF14-4cd3-B6DC-174571C4349C}\\install.rdf"  --是否安装
	if Obj:FileExists(instrdf) then binstall=true end
	local extrdf=dir.."\\extensions.rdf"
	if Obj:FileExists(extrdf)==false then 
		return 0 
	end
	
	
	local tabHead = Obj:GetXmlTable(extrdf)
	local result=1
	if tabHead~=nil and tabHead['RDF:RDF']~=nil then
		for i=0, #tabHead['RDF:RDF'] do
			if tabHead['RDF:RDF'][i]~=nil  and tabHead['RDF:RDF'][i]['RDF:Description']~=nil then
				local about = tabHead['RDF:RDF'][i]['RDF:Description@']['RDF:about']
				--判断插件是否被卸载
				if about~=nil and string.find(about, "{1B33E42F%-EF14%-4cd3%-B6DC%-174571C4349C}") then	
					local disable = tabHead['RDF:RDF'][i]['RDF:Description@']['NS1:userDisabled']
					if disable~=nil then 
						----判断插件是否被禁用
						if disable=="true" or disable=="needs-disable" then
							return 1
						end
					end
					local installLocation = tabHead['RDF:RDF'][i]['RDF:Description@']['NS1:installLocation']
					if installLocation=="app-profile" then 
						return 0
					elseif binstall==false then
						return 1
					end
					result=0
					break
				end
			end
		end
	else
		return 0
	end
	return result
	
end

function RepairFirefox()
	local FirefoxObj = XLGetObject("Xunlei.FirefoxSupport.Obj")
	local bExist = FirefoxObj:FirefoxNeedsFilesExists()		--检测BHO\thunder.xpi是否存在
	--文件修复未能成功，所以不再修复firefox，直接返回失败
	if bExist ~= true then
		return 1
	end
	
	--从注册表获取firefox.exe路径
	local exeName = FirefoxObj:GetFirefoxPathFromRegistry()
	if not exeName then
		return 1
	end
	exeName = exeName .. "\\firefox.exe"
	
	local FixExePath = FirefoxObj:GetFixFilePath()
	
	local Obj = XLGetObject("Xunlei.OSShell.Obj")		
	local ProcessName = string.match(exeName, "[^\\]+$")	--匹配路径中的文件名，类似“firefox.exe”
	
	local path = Obj:GetAppDataPath()
	local iniPath = path.."\\Mozilla\\Firefox\\profiles.ini"
	local Sections={}
	Sections=Obj:ReadSections(iniPath)
	local section="Profile0"
	for i=1,#Sections do 
		--XLMessageBox(Sections[i])
		local nDefault = Obj:ReadInteger(iniPath, Sections[i], "default", 0)
		if nDefault==1 then
			section=Sections[i]
			break
		end
	end
	local strValue = Obj:ReadStringUtf8(iniPath, section, "Path", "")
	if strValue=="" then return 0 end
	
	local i= string.match(strValue, "^%a:\\")		--匹配类似“C:\”
	local dir=""	
	if i~=nil then		
		dir=strValue
	else		
	 	dir= path.."\\Mozilla\\Firefox\\"..strValue
	end
	
	local instrdf=dir.."\\extensions\\{1B33E42F-EF14-4cd3-B6DC-174571C4349C}\\install.rdf"  --是否安装
	local extrdf=dir.."\\extensions.rdf"
	if Obj:FileExists(instrdf) and Obj:FileExists(extrdf) then 		
		local tabHead = Obj:GetXmlTable(extrdf)
		--XLMessageBox("wait!")
		--local extrdf="D:\\xl_doctor_client\\bin\\debug\\DefaultRule\\Config.xml"		
		local result=0
		if tabHead~=nil and tabHead['RDF:RDF']~=nil then
			for i=0, #tabHead['RDF:RDF'] do
				if tabHead['RDF:RDF'][i]~=nil  and tabHead['RDF:RDF'][i]['RDF:Description']~=nil then
					local about = tabHead['RDF:RDF'][i]['RDF:Description@']['RDF:about']
					if about~=nil and string.find(about, "{1B33E42F%-EF14%-4cd3%-B6DC%-174571C4349C}") then
						local disable = tabHead['RDF:RDF'][i]['RDF:Description@']['NS1:userDisabled']
						if disable~=nil  then
							--如果开启了firefox，则提示“关闭浏览器”
							if Obj:IsProcessExist(ProcessName)==true then
								return 4
							end		
							if disable=="true" then
								local cache=Obj:PathAppendLua(extrdf, "..\\extensions.cache")
								Obj:DeleteFile(cache)
								tabHead['RDF:RDF'][i]['RDF:Description@']['NS1:userDisabled']="needs-enable"
							elseif disable=="needs-disable" then
								tabHead['RDF:RDF'][i]['RDF:Description@']['NS1:userDisabled']=nil 
							end
						end
						break
					end
				end
			end
			local ret = Obj:SetXml(extrdf, tabHead)
			return 0		
		end
	end
	
	--若插件被卸载，则调用thunder.xpi重新安装
	local thunderxpi = Obj:PathAppendLua(FixExePath, ".\\thunder.xpi")
	if Obj:FileExists(thunderxpi)==false then return 1 end
	local cmdline = "\""..thunderxpi.."\""
	Obj:ShellExecute(exeName, cmdline, 0, 0)
	if Obj:IsProcessExist(ProcessName)==true then
		return 0
	else 
		return 1
	end		

	return 1
	
end