-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEmailAccountMap.cpp
More file actions
83 lines (63 loc) · 1.92 KB
/
Copy pathEmailAccountMap.cpp
File metadata and controls
83 lines (63 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
* EmailAccountMap.cpp - Implementation of account ID to name mapping
* Distributed under the terms of the MIT License.
*/
#include "EmailAccountMap.h"
#include <Directory.h>
#include <Entry.h>
#include <File.h>
#include <FindDirectory.h>
#include <Message.h>
#include <Path.h>
#include <cstdio>
EmailAccountMap::EmailAccountMap()
{
_LoadAccountMappings();
}
BString
EmailAccountMap::GetAccountName(int32 accountId)
{
auto it = fMap.find(accountId);
if (it != fMap.end())
return it->second;
// Not found - return empty string
return BString();
}
void
EmailAccountMap::_LoadAccountMappings()
{
// Find the Mail accounts directory
BPath accountsPath;
if (find_directory(B_USER_SETTINGS_DIRECTORY, &accountsPath) != B_OK)
return;
accountsPath.Append("Mail/accounts");
BDirectory accountsDir(accountsPath.Path());
if (accountsDir.InitCheck() != B_OK)
return;
// Each file in ~/config/settings/Mail/accounts/ is a flattened BMessage
// with "id" (int32) and "name" (string) fields, written by mail_daemon's
// Accounts settings.
BEntry entry;
while (accountsDir.GetNextEntry(&entry) == B_OK) {
// Skip non-files
if (!entry.IsFile())
continue;
// Try to read the account settings
BFile file(&entry, B_READ_ONLY);
if (file.InitCheck() != B_OK)
continue;
BMessage accountMsg;
if (accountMsg.Unflatten(&file) != B_OK)
continue;
// Extract id and name
int32 accountId;
const char* accountName;
if (accountMsg.FindInt32("id", &accountId) == B_OK &&
accountMsg.FindString("name", &accountName) == B_OK) {
fMap[accountId] = accountName;
}
}
#ifdef DEBUG
printf("EmailAccountMap: Loaded %ld account mappings\n", fMap.size());
#endif
}