'
'  PersistantFileDialog.lss
'
'  Time-stamp: <2002-02-03 20:39:26 deriksso>
'
'  Date        Author           Changes
'  ----------  ---------------  ------------------------------------
'  2002-02-03  Daniel Eriksson  Created
'
Option Explicit


Public Class PersistantFileDialog
' This class displays a file dialog for the user to select files.
' It "remebers" where the user was last time. The directory
' name is saved to Notes.ini.
' 
' Example:
' Dim file As String
' Dim dialog As New PersistantFileDialog("File to import and attach", _
' "", "saved_dir")
' file = dialog.getFile()
' 
' If (file <> "") Then
'   Messagebox file
' End If
' 
  Private m_message As String
  Private m_filter As String
  Private m_key As String
  
  Public Sub New(message As String, filter As String, keyName As String)
    m_message = message
    m_filter = filter
    m_key = keyName 
  End Sub
  
  Public Function getFile
    Dim workspace As New NotesUIWorkspace
    Dim multiple_selection As Variant
    multiple_selection = False
    Dim directory As String
    directory = getDirectory()
    
    Dim files As Variant
    files = workspace.OpenFileDialog(multiple_selection, _
    m_message, m_filter, directory)
    
    If (Not Isempty(files)) Then
      Dim file As String
      Forall f In files 
        file = f
      End Forall
      
      Call setDirectory(getLeftPart(file, "\"))
    End If
    getFile = file
  End Function
  
  Private Function getDirectory As String
    Dim directory As Variant
    Dim ses As New NotesSession
    directory = ses.GetEnvironmentString(m_key)
    If (directory = Null Or directory = "") Then
      getDirectory = "c:\"
    Else
      getDirectory = directory
    End If
  End Function
  
  Private Sub setDirectory(directory As String)
    Dim session As New NotesSession
    Call session.SetEnvironmentVar(m_key, directory)
  End Sub
  
  Private Function getLeftPart(src As String, delimiter As String) As String
     ' TODO: Check for empty src and delimiters that are longer than one
     ' character.
    Dim dest As String
    dest = Left$(src , Len(src) - 1)
    If Right$(src, 1) = delimiter Then 
      getLeftPart = dest
    Else
      getLeftPart = getLeftPart(dest, delimiter)
    End If
  End Function
End Class