#Define CRLF Chr(13) + Chr(10)

Define Class xmlAccess As Custom

	oXml = Null
	** init : din connecttoXml doar partea care citeste fisierul XML de pe url/network path
	** destroy : release This.oXml

	Procedure getXmlItems
		Lparameters tcNumeCursor
		This.oXml.getCursorItems(tcNumeCursor)
	Endproc

	Function  getVersion
		Lparameters tcProgram
		Return This.oXml.selectnodes(tcProgram)
	Endfunc

	Procedure Init
		Lparameters tcFisier
		Local loex As Exception
		myinihandler=Createobject("iniaccess")
		clocal=myinihandler.getNetworkPath()
		curlstring=myinihandler.getURL()
		cdefault=myinihandler.getDefault()

		This.oXml = Createobject('xmlparserclass')
		lcsucces=""

		loUpdate=Createobject("wwCodeUpdate")
		If Isnull(loUpdate.oHTTP)
			loIP = loUpdate.CreateHTTPClient()
		Else
			loIP = loUpdate.oHTTP
		Endif
		If cdefault="NetworkPath"
			cconnectlocation=Addbs(ShortPath(clocal))+tcFisier+".xml"
			polog.Log("Locatia "+cconnectlocation,Program())
			*!*				crets=This.connecttoXml(cconnectlocation,"network")
		Else
			If Right(curlstring,1)#"/"
				curlstring=curlstring +"/"
			Endif
			cconnectlocation=curlstring+tcFisier+".xml"
			polog.Log("Locatia "+cconnectlocation,Program())
			*!*				crets=This.connecttoXml(cconnectlocation,"URL")
		Endif

		*!*			IF cdefault="NetworkPath"
		*!*				cconnectlocation=curlstring+"/"+lcprogram+".xml"
		*!*				polog.Log("Locatia "+cconnectlocation,Program())
		*!*	*!*				crets=This.connecttoXml(cconnectlocation,"URL")
		*!*			Endif
		*!*			Release myinihandler
		If cdefault="URL"
			loIP.lShowDialog = .F.
			loUrl = loIP.InternetCrackUrl(cconnectlocation)
			If Isnull(loUrl)
				polog.Log("informatii despre server invalide :" + cconnectlocation ,Program())
				Return .F.
			Endif
			loIP.nhttpport=Val(loUrl.cPort)
			If loIP.HTTPConnect(loUrl.cServer,myinihandler.getUsername(),myinihandler.getPassword(),;
					IIF(Lower(loUrl.cProtocol)="https",.T.,.F.)) # 0
				loUpdate.SetError(loIP.cErrorMsg)
				Release myinihanler
				polog.Log("Nu s-a putut conecta la "+loUrl.cServer + " cu "+loUrl.cProtocol,Program())
				Return .F.
			Else
				polog.Log("S-a conectat la "+loUrl.cServer ,Program())
				Release myinihanler
				lcTFile = "temporary.xml"
				lcData = ""
				lnSize = 0
				polog.Log("Se descarca "+loUrl.cpath,Program())
				If(loIP.HTTPGetEx( Trim(loUrl.cpath),@lcData,@lnSize,,lcTFile)=0)
					TransferHandlerResult=lnSize
				Endif

				If(loIP.cErrorMsg <>'')
					If(loIP.cErrorMsg="Not found")
						polog.Log("NU s-a gasit fisierul la locatia respectiva",Program())
					Else
						polog.Log(loIP.cErrorMsg,Program())
					Endif
					lcsucces="false"
					amessagebox("Nu s-a putut descarca fisierul xml.Verificati 'Configurare actualizari'!",0+48,"Atentie")
				Endif

			Endif
		Else
			Try
				Delete File temporary.XML
				polog.Log("Se copiaza" + cconnectlocation + " in temporar",Program())
				Copy File (cconnectlocation) To temporary.XML
			Catch
				polog.Log("NU s-a putut descarca " + cconnectlocation + " in temporar",Program())
				lcsucces="false"
				amessagebox("Nu s-a putut descarca fisierul xml.Verificati 'Configurare actualizari'!",0+48,"Atentie")
			Endtry
		Endif
		*!*		LOCAL LOeX as Exception
		If lcsucces<>"false"
			loex = This.oXml.Load("temporary.xml")
			If !Isnull(loex)

				lcMesaj = 'Eroarea nr. ' + Alltrim(Transform(loex.ErrorNo)) + CRLF + ;
					ALLTRIM(loex.Message)  + CRLF + ;
					'Linia ' + Alltrim(Transform(loex.Lineno)) + CRLF + ;
					'Codul ' + Alltrim(loex.LineContents) + CRLF + ;
					'Metoda ' + Alltrim(loex.Procedure)

				*!*				Strtofile(Dtoc(Date())+" "+Time()+"  " + m.lcMesaj, gcDirMare + "updates.log",1)
				polog.Log(m.lcMesaj, Program())
				lcsucces="false"
			Endif

			If !Empty(Alltrim(This.oXml.ParseError))
				polog.Log("Eroare la parsarea xml-ului",Program())
			Endif
		Endif
		If lcsucces="false"
			Return .F.
		Endif
	Endproc

	Procedure Destroy
		This.oXml = Null
		Try
			Delete File "temporary.xml"
		Endtry
	Endproc


	Function ReadXmlFile As Character
		Lparameters lcprogram
		Local lcversiune
		lcversiune = This.oXml.selectnodes(lcprogram)
		If Empty(lcversiune)
			polog.Log("nu s-a putut conecta",Program())
			Return "nu s-a putut conecta"
		Else
			polog.Log(lcversiune,Program())
			Return lcversiune
		Endif
	Endfunc

Enddefine
