SOURCE CODE BELOW
'Activity module
' Libraries: FTP, Core
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Type DLD (RemoteFile As List, RemoteFolder As List, LocalFile As List, LocalFolder As List, Size As List)
Dim Download As DLD
' FTP
Dim MyFTP As FTP
Dim FTPFiles() As FTPEntry
Dim WiFiAvailable As Int '0 - Maybe, don't know, -1 - yes it is, we've checked 1 - No it is not, we've checked
End Sub
Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Dim DownloadFilePtr As Int
Dim FolderLocation As String
Dim DownloadFolderLocation As String
Dim pbProgress As ProgressBar
Dim btnOK As Button
Dim btnCancel As Button
Dim btnDownload As Button
Dim lblDownload As Label
Dim lblDownloaded As Label
End Sub
Sub Activity_Create(FirstTime As Boolean)
If FirstTime Then
Activity.LoadLayout("Installer")
Download.Initialize
DownLoad.Remotefolder.Initialize
Download.Remotefile.Initialize
Download.Localfolder.Initialize
download.LocalFile.Initialize
Download.Size.Initialize
End If
End Sub
' Via FTP, lists all files and folders on the FTP site. list complete will cause downloads to begin
Sub btnOK_Click
lbldownload.Text = ""
pbProgress.Visible = True
ftpconnect
'VERY IMPORTANT: case is important on folder names (upper/lower)
folderlocation = "your/FTPFOLDER/location/" 'your FTP location in the format "fffff/fffff/fffff/" (note: no beginning slash, but an ending slash (/))
downloadfolderlocation = "YOUR/localfolder/location/" 'your local file location in the format "fffff/fffff/fffff/" same as above
Myftp.List(folderlocation) 'you can repeat this as you wish adding more FTP folders to download,
'they will asynchronously add folders/files to the list
End Sub
'now we have a complete list of folders/files, let's download them
Sub btnDownload_Click
If Download.RemoteFile.Size > 0 Then 'if there are no files, we can't download
pbProgress.Visible = True
DownloadFilePtr = 0
If myftp.IsInitialized Then
ftpdisconnect
End If
ftpconnect
If File.Exists(download.LocalFolder.Get(Downloadfileptr), Download.LocalFile.Get(downloadfileptr)) Then 'delete the file, if it already exists
File.Delete(download.LocalFolder.Get(Downloadfileptr), Download.LocalFile.Get(downloadfileptr))
End If
'the below will start the download and then download complete will start the next download of a file, if one exists within the download list
myftp.DownloadFile(Download.RemoteFolder.Get(DownloadFilePtr) & Download.RemoteFile.get(DownloadFilePtr), False, Download.LocalFolder.get(DownloadFilePtr), Download.LocalFile.get(DownloadFilePtr)) 'download complete will disconnect
Else
Msgbox ("No new files to install","Notice")
btnCancel_Click
End If
End Sub
Sub btnCancel_Click
If myftp.IsInitialized Then
ftpdisconnect
End If
activity.Finish
ExitApplication
End Sub
Sub FTPConnect
txtFTP = "your ftp location"
txtPort = 21
txtLogin = "your user id here"
txtPassword = "your password"
' connect with the ftp
myFTP.Initialize("MyFTP", txtFtp, txtPort, txtLogin, txtPassword)
End Sub
Sub FTPDisconnect
myftp.CloseNow
End Sub
Sub MyFTP_ListCompleted (ServerPath As String, Success As Boolean, Folders() As FTPEntry, Files() As FTPEntry)
If Success Then
pbprogress.Visible = True
Dim i As Int
Dim SubFolder As String
SubFolder = serverpath.Replace(downloadfolderlocation.Replace(" ","_"), "") 'won't work if there are embedded spaces
For i = 0 To folders.Length -1 'roll through and find folders
If Folders(i).Name <> "." AND Folders(i).Name <> ".." Then 'don't add folder control
myftp.List(serverpath & "/" & folders(i).name) 'add them to the list (function occurs on list complete
End If
Next
If files.Length >= 1 Then 'we are listing files to go get
AddFileToDownloadList(serverpath, File.DirRootExternal & "/" & Subfolder, Files) 'add the files to the queue (list) to download
End If
btndownload.Enabled = True
pbprogress.Visible = False
Else
Msgbox("Online folder not found, check spelling..." & ServerPath, " Installer")
End If
End Sub
Sub AddFileToDownloadList(RemoteFolder As String, LocalFolder As String, Files() As FTPEntry)
Dim i As Int
For i = 0 To files.Length -1 'roll through the files array and add them to the download list
DownLoad.RemoteFile.add(Files(i).Name)
DownLoad.RemoteFolder.add(RemoteFolder & "/")
Download.LocalFile.Add(Files(i).Name)
Download.LocalFolder.Add(LocalFolder)
Download.Size.Add(Files(i).Size)
DownLoadListLength = download.RemoteFile.Size
Next
lblDownload.Text = download.LocalFile.Size & " files identified."
DoEvents
End Sub
Sub MyFTP_DownloadCompleted (ServerPath As String, Success As Boolean)
' Downloadfilelist.size is the number of entries ie 1 entry 1, no entry 0
If Download.RemoteFile.Size - 1 > DownloadFilePtr Then 'we have more files to transmit
DownloadFilePtr = DownloadFilePtr + 1
If File.Exists(download.LocalFolder.Get(downloadfileptr),"") = False Then 'if the folder doesn't exist, let's create it
File.MakeDir(download.LocalFolder.Get(downloadfileptr),"")
End If
If File.Exists(download.LocalFolder.Get(Downloadfileptr), Download.LocalFile.Get(downloadfileptr)) Then 'delete the existing file if any
File.Delete(download.LocalFolder.Get(Downloadfileptr), Download.LocalFile.Get(downloadfileptr))
End If
myftp.DownloadFile(Download.RemoteFolder.Get(DownloadFilePtr) & Download.RemoteFile.get(DownloadFilePtr), False, Download.LocalFolder.get(DownloadFilePtr), Download.LocalFile.get(DownloadFilePtr)) 'download complete will disconnect
Else
Dim FileCt As Int
Filect = Downloadfileptr + 1
Msgbox("Transmission complete " & FileCt & " files received", "Download Status")
pbProgress.Enabled = False
pbProgress.Visible = False
pbProgress.Indeterminate = False
lblDownloaded.Visible = False
lblDownload.Visible = False
Download.LocalFile.Initialize 'don't really need to do all this
Download.LocalFolder.Initialize
Download.RemoteFile.Initialize
Download.RemoteFolder.Initialize
DownloadFilePtr = 0
FTPDisconnect
End If
End Sub
Sub MyFTP_DownloadProgress (ServerPath As String, TotalDownloaded As Long, Total As Long)
lblDownload.Text = "Downloading: " & serverpath.SubString(serverpath.LastIndexOf("/") + 1)
lblDownloaded.text = Totaldownloaded
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub