'****************************************************************************
'*  QFolder component: scans a folder and all subfolders for files          *
'*                                                                          *
'*  Properties:                                                             *
'*    FolderList: QStringlist object containing all subfolders after a scan *
'*    FileList: QStringlist object containing all files after a scan        *
'*    FolderName: Name of the current scanned file                          *
'*    FileName: Filename of the current scanned file                        *
'*    FileDate: FileDate as string of the current scanned file              *
'*    FileTime: FileTime as string of the current scanned file              *
'*    FileSize: FileSize as integer of the current scanned file             *
'*    FileIntTime: FileTime as integer of the current scanned file          *
'*    SubFolders: Scan all folders if true, Scan only basefolder if false   *
'*    CancelOperation: stops scanning                                       *
'*                                                                          *
'*  Methodes:                                                               *
'*    GetFolderList: start scan operation - stores results in dir/filelist  *
'*    FileCount: returns number of files in FilList (after a scan)          *
'*    FolderCount: returns number of folders in FolderList (after a scan)   *
'*                                                                          *
'*  Events:                                                                 *
'*    OnFileFound: Occurs while scanning when a file is found               *
'*    OnFolderFound: Occurs while scanning when a folder is found           *
'*                                                                          *
'*  Author: Alain Besseleer                                                 *
'*  Version: 1.2                                                            *
'*  Date: November 26, 2005                                                 *
'*  Updated: February 10, 2016                                              *
'*                                                                          *
'****************************************************************************


Declare Function GetFileAttributes_Qfolder Lib "kernel32" Alias "GetFileAttributesA" _
    (lpFileName As string) As Long

   
Declare Sub OnFileFound_Qfolder (filename as string)
Declare Sub OnFolderFound_Qfolder (Foldername as string)

type QFolder extends QObject

  public:
  
    FolderList as qstringlist
    FileList as qstringlist
    
    FolderName as string
    
    FileName as string
    FileDate as string
    FileTime as string
    FileSize as integer
    FileIntTime as integer
    
    CancelOperation as integer
    
    SubFolders as integer
    OnFileFound as event (OnFileFound_Qfolder)
    OnFolderFound as event (OnFolderFound_Qfolder)
    
  private:
  
    index as integer
        
    function GetFileAttrib (filename as string, attributes as long) as long
        dim binfattrib as string
        dim binpattrib as string
        dim x as integer
    
        binfattrib = bin$(GetFileAttributes_Qfolder(filename))   'get file attributes binary
        binfattrib = string$(8 - len(binfattrib), "0") + binfattrib
    
        binpattrib = bin$(attributes)                          'get parameter attributes binary
        binpattrib = string$(8 - len(binpattrib), "0") + binpattrib
    
        result = 1

        for x = 1 to 8
            if val(binfattrib[x]) - val(binpattrib[x]) < 0 then result = 0
            'check bit per bit: if parameter bit is set, but file bit is not, then
            'the attribute asked for is not set.  To check this, simply substract the 
            'parameter bit from the file bit: if negative, result = 0
        next x
    end function
  
    sub folderfilelist (folder as string)
        dim filename as string
        filename = dir$(folder + "*.*", &h3f)
        QFolder.CancelOperation = 0
        
        while len(filename) > 0 

            if QFolder.getfileattrib (folder + filename, &h10) then  'if folder

                if direxists (folder + filename) and filename <> "." and filename <> ".." then 
                    QFolder.FolderList.additems (folder + filename + "\")
                    callfunc QFolder.OnFolderFound, folder + filename
                end if
            else
                QFolder.filelist.additems (folder + filename)
                QFolder.FileName = (folder + filename)
                QFolder.FileDate = fileRec.Date
                QFolder.FileTime = fileRec.Time
                QFolder.FileSize = fileRec.Size
                QFolder.FileIntTime = fileRec.FileTime
                QFolder.FolderName = folder
                callfunc QFolder.OnFileFound, QFolder.FileName
            end if
            
            doevents
            filename = dir$
            
            if QFolder.CancelOperation <> 0 then exit sub
        wend
    end sub

  public:
  
    function GetFolderList (basefolder as string) as long
        if direxists (basefolder) then
            This.index = 0
            This.FolderList.clear
            This.filelist.clear
        
            basefolder = replacesubstr$(basefolder, "/", "\")
            if right$(basefolder, 1) <> "\" then basefolder = basefolder + "\"
    
            This.folderfilelist(basefolder)
   
            If This.Subfolders then
                while This.index < This.FolderList.itemcount
                   This.folderfilelist(This.FolderList.item(This.index))
                   This.index = This.index + 1
                   
                   if This.CancelOperation <> 0 then 
                       exit function
                   end if
                wend 
            End if
            
            This.FolderList.sort
            This.filelist.sort
            
            Result = 1
        else
            Result = 0
        end if
    end function
    
    Function FileCount as integer
        result = This.FileList.ItemCount
    end Function
    
    Function FolderCount as integer
        result = This.FolderList.ItemCount
    end Function
    
    Constructor
        SubFolders = 1
    end constructor
    
end type

