Navigating OLM Folders

Items in an OLM archive live in a tree of folders, and each node of that tree is an OlmFolder. This article explains how to obtain the tree, how the two ways of opening a file affect it, and how to reach a particular folder.

The samples below use the following headers and namespaces:

#include <Storage/Olm/OlmStorage.h>
#include <Storage/Olm/OlmFolder.h>
#include <system/collections/list_ext.h>
#include <system/convert.h>

using namespace System;
using namespace Aspose::Email;
using namespace Aspose::Email::Storage::Olm;

Walk the Folder Tree

FolderHierarchy returns the top-level folders as a ListExt<SharedPtr<OlmFolder>>; each folder exposes its children through SubFolders. A short recursive function is enough to print the whole structure with indentation:

void PrintAllFolders(SharedPtr<Collections::Generic::ListExt<SharedPtr<OlmFolder>>> folders, String indent)
{
    for (SharedPtr<OlmFolder> folder : folders)
    {
        Console::WriteLine(indent + folder->get_Name());
        PrintAllFolders(folder->get_SubFolders(), indent + u"-");
    }
}
SharedPtr<OlmStorage> olm = MakeObject<OlmStorage>(u"SampleOLM.olm");
PrintAllFolders(olm->get_FolderHierarchy(), String::Empty);
olm->Dispose();

FolderHierarchy after FromFile

The property is filled only when the storage is created with a constructor. After FromFile or FromStream it stays nullptr until you call GetFolders, which builds the tree and returns it:

SharedPtr<OlmStorage> olm = OlmStorage::FromFile(u"SampleOLM.olm");

// FolderHierarchy is not populated by FromFile; GetFolders builds and returns it
auto folders = olm->GetFolders();
Console::WriteLine(u"Top-level folders: " + Convert::ToString(folders->get_Count()));

olm->Dispose();

Find a Folder by Name

GetFolder looks a folder up among the top-level folders of the storage; the second argument tells whether the comparison ignores case. The returned OlmFolder can then be asked for a child with GetSubFolder, which searches only the immediate children of that folder. Both methods return nullptr when nothing matches.

SharedPtr<OlmStorage> olm = OlmStorage::FromFile(u"SampleOLM.olm");

// Locate the Inbox folder by name; the second argument makes the search case-insensitive
SharedPtr<OlmFolder> inbox = olm->GetFolder(u"Inbox", true);

if (inbox != nullptr)
{
    Console::WriteLine(u"Found: " + inbox->get_Name() + u" (" + Convert::ToString(inbox->get_MessageCount()) + u" messages)");

    // Drill down to a child folder without rescanning the whole hierarchy
    SharedPtr<OlmFolder> archive = inbox->GetSubFolder(u"Archive", true);
    if (archive != nullptr)
    {
        Console::WriteLine(u"Found sub-folder: " + archive->get_Name());
    }
}

olm->Dispose();

Path gives the location of a folder inside the archive as a backslash-separated string. The recursive helper below prints it for every folder:

void PrintPath(SharedPtr<Collections::Generic::ListExt<SharedPtr<OlmFolder>>> folders)
{
    for (SharedPtr<OlmFolder> folder : folders)
    {
        // full path of the folder inside the storage
        Console::WriteLine(folder->get_Path());
        PrintPath(folder->get_SubFolders());
    }
}
SharedPtr<OlmStorage> storage = MakeObject<OlmStorage>(u"SampleOLM.olm");
PrintPath(storage->get_FolderHierarchy());
storage->Dispose();