Counting Items and Working with Categories
Two small but frequently needed tasks are covered here: finding out how many items an archive or a folder holds - for progress bars, reports or sanity checks - and reading the color categories that Outlook for Mac stored in the file.
The samples below use the following headers and namespaces:
#include <Storage/Olm/OlmStorage.h>
#include <Storage/Olm/OlmFolder.h>
#include <Storage/Olm/OlmItemCategory.h>
#include <Mapi/MapiMessage.h>
#include <system/collections/list_ext.h>
#include <system/string_comparison.h>
#include <system/convert.h>
using namespace System;
using namespace Aspose::Email;
using namespace Aspose::Email::Mapi;
using namespace Aspose::Email::Storage::Olm;
Check and Count the Messages of a Folder
HasMessages tells whether a folder contains anything; MessageCount gives the exact number.
SharedPtr<OlmStorage> olm = OlmStorage::FromFile(u"SampleOLM.olm");
SharedPtr<OlmFolder> folder = olm->GetFolder(u"Inbox", true);
if (folder->get_HasMessages())
{
Console::WriteLine(u"Message count: " + Convert::ToString(folder->get_MessageCount()));
}
olm->Dispose();
Count Items per Folder and in the Whole Archive
Walking the tree and printing MessageCount for each node produces a quick inventory of the file. GetTotalItemsCount on the storage returns the grand total in one call.
void PrintMessageCount(SharedPtr<Collections::Generic::ListExt<SharedPtr<OlmFolder>>> folders)
{
for (SharedPtr<OlmFolder> folder : folders)
{
Console::WriteLine(u"Message count [" + folder->get_Name() + u"]: " + Convert::ToString(folder->get_MessageCount()));
PrintMessageCount(folder->get_SubFolders());
}
}
SharedPtr<OlmStorage> storage = MakeObject<OlmStorage>(u"SampleOLM.olm");
PrintMessageCount(storage->get_FolderHierarchy());
// total across the whole storage
Console::WriteLine(u"Total items: " + Convert::ToString(storage->GetTotalItemsCount()));
storage->Dispose();
Read the Category List
Outlook categories are stored in the archive as name/color pairs. GetCategories returns them as OlmItemCategory objects; Color is an HTML-style #rrggbb string.
SharedPtr<OlmStorage> olm = OlmStorage::FromFile(u"SampleOLM.olm");
auto categories = olm->GetCategories();
for (SharedPtr<OlmItemCategory> category : categories)
{
Console::WriteLine(u"Category name: " + category->get_Name());
// the color is a hexadecimal string: #rrggbb
Console::WriteLine(u"Category color: " + category->get_Color());
}
olm->Dispose();
Resolve the Colors of a Message’s Categories
A message only carries category names (the Categories array of MapiMessage). To show a colored label, match each name against the storage list, ignoring case:
SharedPtr<OlmStorage> olm = OlmStorage::FromFile(u"SampleOLM.olm");
auto categories = olm->GetCategories();
SharedPtr<OlmFolder> folder = olm->GetFolder(u"Inbox", true);
for (SharedPtr<MapiMessage> msg : folder->EnumerateMapiMessages())
{
ArrayPtr<String> msgCategories = msg->get_Categories();
if (msgCategories == nullptr)
{
continue;
}
for (String msgCategory : msgCategories)
{
Console::WriteLine(u"Category name: " + msgCategory);
// resolve the color by matching the name against the storage category list
String color = u"Not found";
for (SharedPtr<OlmItemCategory> category : categories)
{
if (category->get_Name().Equals(msgCategory, StringComparison::OrdinalIgnoreCase))
{
color = category->get_Color();
break;
}
}
Console::WriteLine(u"Category color: " + color);
}
}
olm->Dispose();