*--------------------------------------------------------------------------------------------------------------------------------------------------------
* (EN) AUTOGENERATED - ATTENTION!! - NOT INTENDED FOR EXECUTION!! USE ONLY FOR MERGING CHANGES AND STORING WITH SCM TOOLS!!
*--------------------------------------------------------------------------------------------------------------------------------------------------------
*< FOXBIN2PRG: Version="1.21" SourceFile="updateapp.scx" CPID="1252" /> (Solo para binarios VFP 9 / Only for VFP 9 binaries)
*
*
DEFINE CLASS dataenvironment AS dataenvironment 
 	*< CLASSDATA: Baseclass="dataenvironment" Timestamp="" Scale="" Uniqueid="" ClassIcon="1" />

	*<PropValue>
		DataSource = .NULL.
		Height = 0
		Left = 0
		Name = "Dataenvironment"
		Top = 0
		Width = 0
	*</PropValue>

ENDDEFINE

DEFINE CLASS frmcodeupdate AS form 
 	*< CLASSDATA: Baseclass="form" Timestamp="" Scale="" Uniqueid="" />

	*-- OBJECTDATA items order determines ZOrder / El orden de los items OBJECTDATA determina el ZOrder 
	*< OBJECTDATA: ObjPath="Label1" UniqueID="" Timestamp="" />
	*< OBJECTDATA: ObjPath="lblVersion" UniqueID="" Timestamp="" />
	*< OBJECTDATA: ObjPath="edtNews" UniqueID="" Timestamp="" />
	*< OBJECTDATA: ObjPath="Label2" UniqueID="" Timestamp="" />

	*<DefinedPropArrayMethod>
		*m: checkfornewversion		&& Checks for new version and optionally downloads the new version.
		*m: makevbsfile
		*m: setexefile
		*m: setinifile
		*m: setversionnumber
		*m: update
		*m: updateversion
		*p: cdownloadpath
		*p: cexefile
		*p: cinifile
		*p: cnewsmessage
		*p: csuperpath
		*p: cversion
	*</DefinedPropArrayMethod>

	*<PropValue>
		AutoCenter = .T.
		BorderStyle = 1
		Caption = "Auto Update"
		cdownloadpath = 
		cexefile = 
		cinifile = 
		cnewsmessage = 
		csuperpath = 
		cversion = 1.00
		DoCreate = .T.
		Height = 421
		Name = "frmCodeUpdate"
		ShowWindow = 2
		Width = 432
	*</PropValue>

	ADD OBJECT 'edtNews' AS editbox WITH ;
		ControlSource = "thisform.cNewsMessage", ;
		Height = 288, ;
		Left = 12, ;
		Name = "edtNews", ;
		ReadOnly = .T., ;
		TabIndex = 5, ;
		Top = 120, ;
		Width = 408
		*< END OBJECT: BaseClass="editbox" />

	ADD OBJECT 'Label1' AS label WITH ;
		Alignment = 2, ;
		BackStyle = 0, ;
		Caption = "Auto Update", ;
		FontBold = .T., ;
		FontName = "Verdana", ;
		FontSize = 18, ;
		Height = 36, ;
		Left = 12, ;
		Name = "Label1", ;
		TabIndex = 8, ;
		Top = 24, ;
		Width = 408
		*< END OBJECT: BaseClass="label" />

	ADD OBJECT 'Label2' AS label WITH ;
		AutoSize = .T., ;
		BackStyle = 0, ;
		Caption = "Modificari", ;
		FontBold = .T., ;
		Height = 17, ;
		Left = 12, ;
		Name = "Label2", ;
		TabIndex = 10, ;
		Top = 102, ;
		Width = 58
		*< END OBJECT: BaseClass="label" />

	ADD OBJECT 'lblVersion' AS label WITH ;
		Alignment = 2, ;
		BackStyle = 0, ;
		Caption = "Version Number here", ;
		FontName = "Tahoma", ;
		FontSize = 8, ;
		Height = 24, ;
		Left = 72, ;
		Name = "lblVersion", ;
		TabIndex = 9, ;
		Top = 60, ;
		Width = 289
		*< END OBJECT: BaseClass="label" />
	
	PROCEDURE checkfornewversion		&& Checks for new version and optionally downloads the new version.
		LOCAL loUpdate, loAPI, lcURL, lcVersion, lcDownloadPath, lnPos, lcUpdateDirectory
		
		
		*** Retrieve the URL to use for version checks
		*** This is an XML file
		loAPI = CREATEOBJECT("wwAPI")
		
		ldLastCheckDate = NVL(CTOD(loApi.GetProfileString(this.cInifile, "Main", "LastVersionCheck")), {})
		
		IF ISNULL(loApi.GetProfileString(this.cInifile, "Main", "UpdateFrequency"))
			loAPI.WriteProfileString(this.cInifile, "Main", "UpdateFrequency", "-1")
		ENDIF
		
		lnUpdateFrequency = VAL(loApi.GetProfileString(this.cInifile, "Main", "UpdateFrequency"))
		
		DO CASE 
			CASE lnUpdateFrequency = -1 && check every time
				ldCheckDate = DATE()
			CASE lnUpdateFrequency = 0 && never check
				ldCheckDate = {}	
			OTHERWISE 	
				ldCheckDate = ldLastCheckDate + lnUpdateFrequency		
		ENDCASE 	
			
		
		*** Check only on UpdateFrequency
		IF ldCheckDate # DATE()
		  *** Skip version check
			RETURN  
		ENDIF                    
		
		*** Update the LastCheckDate
		*** And write out the updated version number
		loAPI.WriteProfileString(this.cInifile, "Main", "LastVersionCheck", TRANSFORM(DATE()))
		
		lcUrl = loAPI.GetProfileString(this.cInifile,"Main","VersionUpdateUrl")
		IF isnull(lcUrl)
			loAPI.WriteProfileString(this.cInifile, "Main", "VersionUpdateUrl", "http://localhost/codeupdate/")
			lcUrl = loAPI.GetProfileString(this.cInifile,"Main","VersionUpdateUrl")
		ENDIF 
		
		lcDownloadPath = loAPI.GetProfileString(this.cInifile,"Main","DownloadPath")
		
		lcSuperPath = ADDBS(JUSTPATH(this.cExeFile))
		lnPos = AT('\', lcSuperPath, 2)
		IF lnPos > 0
			lcSuperPath = LEFT(lcSuperPath, lnPos)
		ENDIF 
		
		IF EMPTY(ALLTRIM(TRANSFORM(NVL(lcDownloadPath,"")))) OR !DIRECTORY(NVL(lcDownloadPath,""))
			lcDownloadPath = ADDBS(UPPER(lcSuperPath)) + 'CODEUPDATE\'
			IF !DIRECTORY(lcDownloadPath)
				md(lcDownloadPath)
			ENDIF
			loAPI.WriteProfileString(this.cInifile, "Main", "DownloadPath", lcDownloadPath)
		ENDIF 	
		
		this.cDownloadPath = ADDBS(lcDownloadPath)
		this.cSuperPath = ADDBS(lcSuperPath)
		loUpdate=CREATEOBJECT("wwCodeUpdate")
		
		*** Url where to check for new versions (XML file)
		loUpdate.cVersionUrl = lcUrl
		loUpdate.cVersionType = "C"
		
		*** DownloadPath
		loUpdate.cDownloadPath = this.cDownloadPath
		
		*** Name of the application's EXE file to hotswap
		loUpdate.cEXEFile = this.cExeFile && "sampleapp.exe"
		
		*** Returns the current available version from the XML file
		lcVersion = loUpdate.GetVersionInfo()
		this.cNewsMessage = loUpdate.cNewsMessage
		this.edtNews.Refresh()
		
		
		*** If no version we couldn't get the info from 
		*** the Web Server
		IF EMPTY(lcVersion)
		   *** Couldn't retrieve version info 
		   *** so just exit
		   RETURN 
		ENDIF
		
		*** Compare the version number against 
		*** the app's version number - here a property of the form
		*** Usually you'd use a .H setting, database field or Config object
		
		IF lcVersion # THIS.cVersion
		   *** Display message for the user and ask to download
		   IF aMessageBox(loUpdate.cUserMessage + CHR(13) + CHR(13) +;
		                 "Doriti sa descarcati acum? (" + ;
		                 LTRIM(TRANSFORM(loUpdate.nFileSize,"999,999,999")) + ;
		                 "kb)",32+4,"Romfast") = 6                
		
		*      loUpdate.CreateHTTPClient()
		*     loUpdate.oHTTP.cCaption = "Download SampleApp Update"
		      
		      IF !loUpdate.DownloadUpdate(.T., FORCEEXT(JUSTFNAME(this.cExeFile),'xml'))
		         aMessageBox(loUpdate.cErrorMsg,48,"Eroare download")
		      ELSE 
		         lcFileVBS = this.cSuperPath + [codeupdate\CodeUpdate.vbs]
		         IF !FILE(lcFileVBS)
		         	this.makeVBSFile(lcFileVBS)
		         ENDIF 
		
		         lcUpdateDirectory = JUSTPATH(SYS(16,0))
		         CD (lcUpdateDirectory)
		         *** Run the generic CodeUpdate Exe and quit application
		         *---------------------
		         lcNumeArhiva = JUSTFNAME(loUpdate.cOnlineFileUrl)
		         lcUpdateExe = ADDBS(JUSTPATH(ShortPath(lcFileVBS))) + lcNumeArhiva
		         *MESSAGEBOX(lcUpdateExe,0,"lcUpdateExe")
		*!*	         lcCodeUpdate = [WSCRIPT ] + SHORTPATH(lcFileVBS) + [ "] + this.cExeFile + [" "] + ;
		*!*	                               SHORTPATH(this.cSuperPath + [codeupdate\] + JUSTSTEM(this.cExeFile) + [.sfx.exe])  + ; 
		*!*	                               [ /auto ] + ADDBS(JUSTPATH(this.cExeFile)) + [" ] +;
		*!*	                               ["] + UPPER(JUSTSTEM(this.cExeFile)) + [" "] + IIF(TYPE("gcParameters")="C",gcParameters,"") + ["]  
				lcCodeUpdate = [WSCRIPT ] + SHORTPATH(lcFileVBS) + [ "] + this.cExeFile + [" "] + ;
		                               lcUpdateExe  + ; 
		                               [ -o] + ADDBS(JUSTPATH(this.cExeFile)) + [ -y " ] +;
		                               ["] + UPPER(JUSTSTEM(this.cExeFile)) + [" "] + IIF(TYPE("gcParameters")="C",gcParameters,"") + ["]                    
		 		*===============================
		   		 *MESSAGEBOX(lcCodeUpdate)
		         _cliptext = lcCodeUpdate                      
		         loUpdate.RunUpdateExe(lcCodeUpdate)
		         this.Release()
		         QUIT && Just to be safe                              
		       ENDIF
		   ENDIF
		ENDIF
		
		RETURN
		
	ENDPROC

	PROCEDURE Init
		LPARAMETERS tcExeFileName, tcIniFileName
		
		*** Load the class libraries required
		DO wwCodeUpdate
		
		*** Profile INI usage
		SET PROCEDURE TO wwAPI Additive
		
		THIS.setExeFile(tcExeFileName)
		THIS.setIniFile(tcIniFileName)
		
		*** Update the version label
		THIS.SetVersionNumber()
		
		*** And simply check for a new version
		THIS.CheckForNewVersion()
		
		*** Check for Data File Update requirements and update
		THIS.UpdateVersion()
	ENDPROC

	PROCEDURE makevbsfile
		LPARAMETERS tcVBSFile
		
		LOCAL lcVBS
		IF EMPTY(tcVBSFile)
			RETURN
		ENDIF 
		
		TEXT TO lcVBS NOSHOW   
		if WScript.Arguments.Length < 3 then
		   WScript.Echo("Numar invalid de parametri." + vbcrlf + _
		                "Sintaxa: WSCRIPT CodeUpdate.vbs <ExeFile> <UpdateFile> <ApplicationName>")
		   Wscript.Quit(1)
		end if
		
		lcExe = WScript.Arguments(0)
		lcUpdateExe = WScript.Arguments(1)
		lcAppName = WScript.Arguments(2)
		lcCommandLineParameters = WScript.Arguments(3)
		
		Set oShell = Wscript.CreateObject("Wscript.Shell")
		
		'*** Wait for 5 seconds for app to shut down
		lnResult = oShell.Popup("Astept inchiderea aplicatiei...    ",5,lcAppName + " Update              ",64)
		
		'*** Run the Update Exe
		oShell.Exec(lcUpdateExe)
		
		lnResult = oShell.Popup("Actualizez programul... ",5,lcAppName + " Update              ",64)
		
		'*** Check that the Exe exists
		Set oFile = CreateObject("Scripting.FileSystemObject")
		if not oFile.FileExists(lcExe) then
			lnResult = oShell.Popup("Actualizez programul......  ",5,lcAppName + " Update              ",64)
		end if
		
		lcExeParam = lcExe + " " + lcCommandLineParameters
		oShell.Exec(lcExeParam)
		
		ENDTEXT 
		
		STRTOFILE(lcVBS, tcVBSFile)
	ENDPROC

	PROCEDURE Release
		ies=.t.
		QUIT
		
	ENDPROC

	PROCEDURE setexefile
		LPARAMETERS tcExeFileName
		
		IF PCOUNT() = 0 OR TYPE('tcExeFileName') # 'C'
			lcExeFileName = SYS(16,0)
		ELSE
			lcExeFileName = tcExeFileName
		ENDIF
		THIS.cExefile = lcExeFileName
		
	ENDPROC

	PROCEDURE setinifile
		LPARAMETERS tcExeFileName
		
		IF PCOUNT() = 0 OR TYPE('tcIniFileName') # 'C'
			lcIniFileName = FORCEEXT(SYS(16,0), 'ini')
		ELSE
			lcIniFileName = tcIniFileName
		ENDIF
		THIS.cInifile = lcIniFileName
	ENDPROC

	PROCEDURE setversionnumber
		LOCAL laFiles, lcVersion, lcExeFile, lnElements
		DIMENSION laFiles[1]
		
		lcExeFile = ADDBS(JUSTPATH(SYS(16,0))) + JUSTFNAME(this.cExeFile)
		IF FILE(lcExeFile)
			lnElements = AGETFILEVERSION(laFiles, lcExeFile)
			IF lnElements >= 4
				lcVersion = laFiles[4]
			ELSE
				lcVersion = ""
			ENDIF 
		ELSE
			lcVersion = ""
		ENDIF 	
				
		this.cversion = lcVersion
		
		THIS.lblVersion.Caption = "Versiunea " + TRANSFORM(lcVersion)
		
		
	ENDPROC

	PROCEDURE update
		*** And simply check for a new version
		THIS.CheckForNewVersion()
		
		*** Check for Data File Update requirements and update
		THIS.UpdateVersion()
	ENDPROC

	PROCEDURE updateversion
		loAPI = CREATEOBJECT("wwAPI")
		
		lcVersion = loAPI.GetProfileString(THIS.cInifile, "Main", "Version")
		IF lcVersion = THISFORM.cVersion
		*** Nothing to do
			RETURN
		ELSE
		*** Code to perform data updates goes here
			WAIT WINDOW "Numarul versiunii nu este actualizat. Actualizare..." NOWAIT 
		*** COD DE EFECTUAT LA PRIMA RULARE A UNEI VERSIUNI ACTUALIZATE
		
		*** And write out the updated version number
			loAPI.WriteProfileString(THIS.cInifile, "Main", "Version",  LTRIM(TRANSFORM(THISFORM.cVersion)))
		
			RETURN
		
		ENDIF
		
		
	ENDPROC

ENDDEFINE
