'
' Example script for 2BrightSparks SyncBackPro that adds an extra
' column in the main window that shows history information for
' the profile.
'
' 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=VBScript
'
' http://www.2BrightSparks.com/
'

Dim gCache

'
' This is a main interface script
'
Function Description(ByRef ScriptType)
  Description = "Show history count information"
  ScriptType = 4
End Function

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

'
' Column title.
'
Function MainColumnTitle(Col, ByRef Width)
  MainColumnTitle = "History Count"
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
    if SBHistory.ProfileName = ProfileName then
      SBHistory.Refresh
    else
      SBHistory.ProfileName = ProfileName
    end if
    
    MainColumnText = SBHistory.RecordCount
    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.
'
' When the display needs refreshing we clear the cache as appropriate.
'
Sub RefreshDisplayEx(Reloading, Refreshing)
  Dim i, Keys
  
  if Reloading then
    ' All profiles are being reloaded
    gCache.RemoveAll
    Exit Sub
  End If

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

    ColKey = Keys(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