'****************************************************************************
'*  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.1                                                            *
'*  Date: November 26, 2005                                                 *
'*                                                                          *
'****************************************************************************


Declare Function GetFileAttributes_Qfolder Lib "kernel32" Alias "GetFileAttributesA" _
    (lpFileName As String) As Long
    
Declare Sub OnFileFound_QfolderEventTemplate
Declare Sub OnFolderFound_QfolderEventTemplate

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_QfolderEventTemplate)
    OnFolderFound as event (OnFolderFound_QfolderEventTemplate)
    
  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(mid$(binfattrib, x, 1)) - val(mid$(binpattrib, x, 1)) < 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
                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
            end if
            
            doevents
            filename = dir$
            
            if QFolder.CancelOperation <> 0 then 
                exit sub
            end if
        wend
    end sub

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

