'
' Example script for 2BrightSparks SyncBackPro V8 that adds an extra
' column in the main window that shows if a profile is using
' versioning or not.
'
' This script is an example of how to use caching to avoid
' performance problems in the main user interface. It also shows
' how to use the new PollingRefresh and RefreshDisplayEx
' functions.
'
' You need to install this script and enable it on the Main Interface
' tab.
'
' SBLang=Basic
'
' http://www.2BrightSparks.com/
'

Dim gCache

'
' This is a main interface script
'
Function Description(ByRef ScriptType)
  Description = "Show versioning information"
  ScriptType = SCRIPTTYPE_MAIN
End Function

'
' One column only showing history count.
'
Function MainColumnsCount
  MainColumnsCount = 1
End Function

'
' Column title.
'
Function MainColumnTitle(Col, ByRef Width)
  MainColumnTitle = "Versioning"
End Function

'
' Column text.
'
Function MainColumnText(Col, IsGroup, ProfileName)
  ColKey = ProfileName & "." & Col

  if gCache.Exists(ColKey) then
    ' Use the value in the cache
    MainColumnText = gCache.Item(ColKey)
  else
    ' Get the value and add it to the cache
    MainColumnText = ""
    SrcVer = SBVariables.GetProfileProperty(ProfileName, "VersionSrc", "", True)
    DestVer = SBVariables.GetProfileProperty(ProfileName, "VersionDest", "", True)
    if SrcVer = "1" or SrcVer = "2" then
      MainColumnText = "S"
    end if
    if DestVer = "1" or DestVer = "2" then
      MainColumnText = MainColumnText  & "D"
    end if

    gCache.Add(ColKey, MainColumnText)
  end if
End Function

'
' Column sorting.
'
Function MainColumnSort(Col, IsGroup1, IsGroup2, ProfileName1, ProfileName2)
  ' Use the MainColumnText() function so we can make use
  ' of the caching.
  Val1 = MainColumnText(Col, IsGroup1, ProfileName1)
  Val2 = MainColumnText(Col, IsGroup2, ProfileName2)
  
  MainColumnSort = Val1 - Val2
End Function

'
' We do not want RefreshDisplayEx to be called unless there are profiles
' being reloaded or refreshed.
'
Function PollingRefresh
  PollingRefresh = False
End Function

'
' We are using the new RefreshDisplayEx() function which is only
' available in SyncBackPro V6 and newer.
'
' When the display needs refreshing we clear the cache as appropriate.
'
Sub RefreshDisplayEx(Reloading, Refreshing)
  Dim i
  
  if Reloading then
    ' All profiles are being reloaded
    gCache.RemoveAll
    Exit Sub
  End If

  For i = 0 to Refreshing.Count - 1
    if TStringList(Refreshing).Strings[i] = "" then
      ' All profiles are being refreshed
      gCache.RemoveAll
      Exit For
    End If

    ColKey = TStringList(Refreshing).Strings[i] & ".0"
    if gCache.Exists(ColKey) then
  	  gCache.Remove(ColKey)
  	end if
  Next
End Sub

'
' Create the dictionary object for caching
'
Sub MainStarted(Unattended)
  Set gCache = CreateObject("Scripting.Dictionary")
  gCache.CompareMode = vbTextCompare
End Sub

'
' Free the dictionary object for caching
'
Sub MainEnded
  Set gCache = nothing
End Sub