#include "StdAfx.h"
#include "SKOKO.h"
#include "FileListing.h"

CFileListing::CFileListing( void )
{
  m_storageDir = theApp.m_tempDir;
  reset();
}

CFileListing::~CFileListing( void )
{
}

void CFileListing::load( const char* path )
{
  reset();
  CLog tempLog;
  tempLog.Open(path);
  for (int i = 0; i < tempLog.Count(); ++i)
  {
    CFileInfo entry;
    tempLog.GetEntry(i, &entry);
    addFile(entry);
  }
}

void CFileListing::reset( void )
{
  resetFiles();
  resetErrors();
}

void CFileListing::resetFiles( void )
{
  m_ownerMap.RemoveAll();
  m_files.Close();
  CString fileLogPath = m_storageDir;
  if (!fileLogPath.IsEmpty())
  {
    TCHAR lastChar = fileLogPath[fileLogPath.GetLength()-1];
    if (!_tcschr(_T("\\/"), lastChar))
      fileLogPath += _T("\\");
  }
  fileLogPath += _T("$$$SKOKO.log");
  m_files.Create(fileLogPath, TRUE);
}

void CFileListing::resetErrors( void )
{
  m_errors.Close();
  CString errLogPath = m_storageDir;
  if (!errLogPath.IsEmpty())
  {
    TCHAR lastChar = errLogPath[errLogPath.GetLength()-1];
    if (!_tcschr(_T("\\/"), lastChar))
      errLogPath += _T("\\");
  }
  errLogPath += _T("$$$SKOKO.err");
  m_errors.Create(errLogPath, TRUE);
}

void CFileListing::addFile( const CFileInfo& info )
{
  m_files.Append(info);
  m_ownerMap[info.ownerName] = NULL;
}

void CFileListing::addError( const CFileInfo& info )
{
  m_errors.Append(info);
}

int CFileListing::fileCount( void ) const
{
  return m_files.Count();
}

int CFileListing::errCount( void ) const
{
  return m_errors.Count();
}

void CFileListing::fileAt( int n, CFileInfo* pInfo ) const
{
  m_files.GetEntry(n, pInfo);
}

void CFileListing::updateFileAt( int n, const CFileInfo& info )
{
  m_files.SetEntry(n, info);
  m_ownerMap[info.ownerName] = NULL;
}

void CFileListing::errAt( int n, CFileInfo* pInfo ) const
{
  m_errors.GetEntry(n, pInfo);
}

POSITION CFileListing::firstOwnerPos( void ) const
{
  return m_ownerMap.GetStartPosition();
}

CString CFileListing::nextOwnerName( POSITION& pos ) const
{
  CString name;
  void* p;
  m_ownerMap.GetNextAssoc(pos, name, p);
  return name;
}

void CFileListing::storageDir( LPCTSTR path )
{
  m_storageDir = path;
}

CString CFileListing::storageDir( void ) const
{
  return m_storageDir;
}
