void CMapiFolderList::DumpList( void)
{
  CMapiFolder *pFolder;
  nsString  str;
  int      depth;
  char    prefix[256];

  MAPI_TRACE0( "Folder List ---------------------------------\n");
  for (int i = 0; i < m_array.Count(); i++) {
    pFolder = (CMapiFolder *)GetAt( i);
    depth = pFolder->GetDepth();
    pFolder->GetDisplayName( str);
    depth *= 2;
    if (depth > 255)
      depth = 255;
    memset( prefix, ' ', depth);
    prefix[depth] = 0;
#ifdef MAPI_DEBUG
        char *ansiStr = ToNewCString(str);
    MAPI_TRACE2( "%s%s: ", prefix, ansiStr);
    NS_Free(ansiStr);
#endif
    pFolder->GetFilePath( str);
#ifdef MAPI_DEBUG
        ansiStr = ToNewCString(str);
    MAPI_TRACE2( "depth=%d, filePath=%s\n", pFolder->GetDepth(), ansiStr);
    NS_Free(ansiStr);
#endif
  }
  MAPI_TRACE0( "---------------------------------------------\n");
}
void CMapiFolderList::EnsureUniqueName( CMapiFolder *pFolder)
{
  // For everybody in the array before me with the SAME
  // depth, my name must be unique
  CMapiFolder *  pCurrent;
  int        i;
  BOOL      done;
  nsString    name;
  nsString    cName;

  pFolder->GetDisplayName( name);
  do {
    done = TRUE;
    i = m_array.Count() - 1;
    while (i >= 0) {
      pCurrent = (CMapiFolder *)GetAt(i);
      if (pCurrent->GetDepth() == pFolder->GetDepth()) {
        pCurrent->GetDisplayName(cName);
        if (cName.Equals(name, nsCaseInsensitiveStringComparator())) {
          ChangeName(name);
          pFolder->SetDisplayName(name.get());
          done = FALSE;
          break;
        }
      }
      else if (pCurrent->GetDepth() < pFolder->GetDepth())
        break;
      i--;
    }
  } while (!done);
}
void CMapiFolderList::GenerateFilePath( CMapiFolder *pFolder)
{
  // A file path, includes all of my parent's path, plus mine
  nsString    name;
  nsString    path;
  if (!pFolder->GetDepth()) {
    pFolder->GetDisplayName( name);
    pFolder->SetFilePath(name.get());
    return;
  }

  CMapiFolder *  pCurrent;
  int        i = m_array.Count() - 1;
  while (i >= 0) {
    pCurrent = (CMapiFolder *) GetAt( i);
    if (pCurrent->GetDepth() == (pFolder->GetDepth() - 1)) {
      pCurrent->GetFilePath( path);
      path.AppendLiteral(".sbd\\");
      pFolder->GetDisplayName( name);
      path += name;
      pFolder->SetFilePath(path.get());
      return;
    }
    i--;
  }
  pFolder->GetDisplayName( name);
  pFolder->SetFilePath(name.get());
}
nsresult nsOutlookMail::GetMailFolders(nsISupportsArray **pArray)
{
  if (!m_haveMapi) {
    IMPORT_LOG0("GetMailFolders called before Mapi is initialized\n");
    return NS_ERROR_FAILURE;
  }

  nsresult rv = NS_NewISupportsArray(pArray);
  if (NS_FAILED(rv)) {
    IMPORT_LOG0("FAILED to allocate the nsISupportsArray for the mail folder list\n");
    return rv;
  }

  nsCOMPtr<nsIImportService> impSvc(do_GetService(NS_IMPORTSERVICE_CONTRACTID, &rv));
  if (NS_FAILED(rv))
    return rv;

  m_gotFolders = true;

  m_folderList.ClearAll();

  m_mapi.Initialize();
  m_mapi.LogOn();

  if (m_storeList.GetSize() == 0)
    m_mapi.IterateStores(m_storeList);

  int i = 0;
  CMapiFolder *pFolder;
  if (m_storeList.GetSize() > 1) {
    while ((pFolder = m_storeList.GetItem(i))) {
      CMapiFolder *pItem = new CMapiFolder(pFolder);
      pItem->SetDepth(1);
      m_folderList.AddItem(pItem);
      if (!m_mapi.GetStoreFolders(pItem->GetCBEntryID(), pItem->GetEntryID(), m_folderList, 2)) {
        IMPORT_LOG1("GetStoreFolders for index %d failed.\n", i);
      }
      i++;
    }
  }
  else {
    if ((pFolder = m_storeList.GetItem(i))) {
      if (!m_mapi.GetStoreFolders(pFolder->GetCBEntryID(), pFolder->GetEntryID(), m_folderList, 1)) {
        IMPORT_LOG1("GetStoreFolders for index %d failed.\n", i);
      }
    }
  }

  // Create the mailbox descriptors for the list of folders
  nsIImportMailboxDescriptor *  pID;
  nsISupports *          pInterface;
  nsString            name;
  nsString            uniName;

  for (i = 0; i < m_folderList.GetSize(); i++) {
    pFolder = m_folderList.GetItem(i);
    rv = impSvc->CreateNewMailboxDescriptor(&pID);
    if (NS_SUCCEEDED(rv)) {
      pID->SetDepth(pFolder->GetDepth());
      pID->SetIdentifier(i);

      pFolder->GetDisplayName(name);
      pID->SetDisplayName(name.get());

      pID->SetSize(1000);
      rv = pID->QueryInterface(kISupportsIID, (void **) &pInterface);
      (*pArray)->AppendElement(pInterface);
      pInterface->Release();
      pID->Release();
    }
  }

  return NS_OK;
}