I sometimes need to update many files in the app and sometimes just one so allowed for that as well.
Here is the code for the module with a little test at the end:-
The code holds two modules the first is for the main application.
Can anyone with more knowledge than me check this out? I want it to be crossplatform (Path Seps OK). Any constructive suggestions are welcome.
CD
Code: Select all
DeclareModule MainApp
;Main application Global Variables and Procedures
Structure Download
Url.s
SaveFile.s
Active.i
EndStructure
Global Dim Updates.Download(0)
EndDeclareModule
Module MainApp
EndModule
DeclareModule DownloadUpdate
Declare Open()
EndDeclareModule
Module DownloadUpdate
Global winUpdateDownload.i,prgFileProgress.i,txtCurrentDownload.i,txtStatus.i
Procedure.i GetDownLoadSize(Url.s)
Define Header.s,Line.s
Define Size.i,x.i
;Get Filesize
Header = GetHTTPHeader(url)
Repeat
x + 1
Line = StringField(Header, x, Chr(10))
Until FindString(Line, "Content-Length:", 1, #PB_String_NoCase)
Size = Val(StringField(Line, 2, "Content-Length: "))
ProcedureReturn Size
EndProcedure
Procedure DownLoadFile(Url.s,SaveFileName.s)
Define FileSize.i,Finished.i
Finished = #False
FileSize = GetDownLoadSize(url)
If FileSize > 0
;Set Maximum On Progress Bar
SetGadgetAttribute(prgFileProgress,#PB_ProgressBar_Maximum ,FileSize)
Download = ReceiveHTTPMemory(url, #PB_HTTP_Asynchronous)
If Download
Repeat
Progress = HTTPProgress(Download)
Select Progress
Case #PB_Http_Success
*Buffer = FinishHTTP(Download)
If CreateFile(0, SaveFileName)
WriteData(0, *Buffer, MemorySize(*Buffer))
CloseFile(0)
EndIf
FreeMemory(*Buffer)
Finished = #True
Case #PB_Http_Failed
Finished = #True
Case #PB_Http_Aborted
Finished = #True
Default
SetGadgetText(txtStatus,Str(Progress) + " Bytes Downloaded")
SetGadgetState(prgFileProgress,Progress)
While WindowEvent()
Wend
EndSelect
Delay(5)
Until Finished = #True
Else
Debug "Download error"
EndIf
EndIf
EndProcedure
Procedure DealWithUpdates()
;General Procedure to copy new files into their places. Delete Old Files etc
;To Finish The Update Process
EndProcedure
Procedure Open()
Define iLoop.i
winUpdateDownload = OpenWindow(#PB_Any, 0, 0, 320, 100, "Downloading Updates", #PB_Window_SystemMenu)
prgFileProgress = ProgressBarGadget(#PB_Any, 10, 40, 300, 20, 0, 0)
txtCurrentDownload = TextGadget(#PB_Any, 10, 10, 190, 20, "Current Download")
txtStatus = TextGadget(#PB_Any, 10, 70, 300, 20, "DownloadStatus")
InitNetwork()
;Check All Files That Can be Updated
For iLoop = 0 To ArraySize(MainApp::Updates())
;Only Download New Files
If MainApp::Updates(iLoop)\Active = #True
SetGadgetText(txtCurrentDownload,MainApp::Updates(iLoop)\SaveFile)
DownLoadFile(MainApp::Updates(iLoop)\Url,MainApp::Updates(iLoop)\SaveFile)
EndIf
Next iLoop
DealWithUpdates()
EndProcedure
EndModule
;Just A quick few lines to test
;Load Structured Array With URL,FileName and Active = #False
MainApp::Updates(0)\Url = "http://www.purebasic.com/download/OgreAssimpConverter.zip"
MainApp::Updates(0)\SaveFile = "C:\Temp\OgreAssimpConverter.zip"
MainApp::Updates(0)\Active = #False
;Check For Updates
;I just have a small text file on the website listing files and version numbers which I download
;Check Each Current file against Web Version Number
;If newer file on web then
; Set this files Active Flag to #True
MainApp::Updates(0)\Active = #True ;Just To test
;Endif
;Stop Application. Close Databases etc
;Open The Update Module
DownloadUpdate::Open()
;After Complete
;Open databases and start app again