data/SieveMailboxName.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "SieveMailboxName.h" | ||
| 2 | |||
| 3 | #include <QMap> | ||
| 4 | |||
| 5 | namespace { | ||
| 6 | |||
| 7 | const QStringList kSpecialUses = {QStringLiteral("\\Junk"), QStringLiteral("\\Trash"), | ||
| 8 | QStringLiteral("\\Sent"), QStringLiteral("\\Drafts"), | ||
| 9 | QStringLiteral("\\Archive"), QStringLiteral("\\All"), | ||
| 10 | QStringLiteral("\\Flagged")}; | ||
| 11 | |||
| 12 | 11277 | bool hasInboxPrefix(const QString &name) { | |
| 13 |
7/9✓ Branch 3 → 4 taken 9012 times.
✓ Branch 3 → 19 taken 2265 times.
✓ Branch 4 → 5 taken 9012 times.
✓ Branch 7 → 8 taken 181 times.
✓ Branch 7 → 19 taken 8831 times.
✓ Branch 12 → 13 taken 3 times.
✓ Branch 12 → 18 taken 178 times.
✗ Branch 31 → 32 not taken.
✗ Branch 31 → 33 not taken.
|
22738 | return name.size() > 5 && name.left(5).compare(QLatin1String("INBOX"), Qt::CaseInsensitive) == 0 && |
| 14 |
3/4✓ Branch 17 → 18 taken 3 times.
✗ Branch 17 → 19 not taken.
✓ Branch 20 → 21 taken 9012 times.
✓ Branch 20 → 22 taken 2265 times.
|
22738 | (name.at(5) == QLatin1Char('.') || name.at(5) == QLatin1Char('/')); |
| 15 | } | ||
| 16 | |||
| 17 | } // namespace | ||
| 18 | |||
| 19 | 10493 | QString SieveMailboxDirectory::canonical(const QString &name) { | |
| 20 | // RFC 3501: INBOX is case-insensitive, also as a hierarchy prefix. | ||
| 21 |
2/2✓ Branch 4 → 5 taken 1367 times.
✓ Branch 4 → 10 taken 9126 times.
|
10493 | if (name.compare(QLatin1String("INBOX"), Qt::CaseInsensitive) == 0) |
| 22 | 1367 | return QStringLiteral("INBOX"); | |
| 23 |
2/2✓ Branch 11 → 12 taken 177 times.
✓ Branch 11 → 22 taken 8949 times.
|
9126 | if (hasInboxPrefix(name)) |
| 24 |
2/2✓ Branch 12 → 13 taken 177 times.
✓ Branch 16 → 17 taken 177 times.
|
354 | return QStringLiteral("INBOX") + name.mid(5); |
| 25 | 8949 | return name; | |
| 26 | } | ||
| 27 | |||
| 28 | 2151 | QString SieveMailboxDirectory::normalizedKey(const QString &name) { | |
| 29 | 2151 | QString key = canonical(name); | |
| 30 |
3/3✓ Branch 3 → 4 taken 2151 times.
✓ Branch 4 → 5 taken 4 times.
✓ Branch 4 → 9 taken 2147 times.
|
2151 | if (hasInboxPrefix(key)) |
| 31 |
1/1✓ Branch 5 → 6 taken 4 times.
|
4 | key = key.mid(6); |
| 32 |
1/1✓ Branch 13 → 14 taken 2151 times.
|
2151 | key.replace(QLatin1Char('.'), QLatin1Char('\x1f')); |
| 33 |
1/1✓ Branch 18 → 19 taken 2151 times.
|
2151 | key.replace(QLatin1Char('/'), QLatin1Char('\x1f')); |
| 34 | 2151 | return key; | |
| 35 | ✗ | } | |
| 36 | |||
| 37 | 7004 | void SieveMailboxDirectory::addFolder(const QString &imapPath, const QString &decodedPath, | |
| 38 | const QString &delimiter, const QStringList &flags) { | ||
| 39 | 7004 | Folder folder{imapPath, decodedPath, delimiter, {}}; | |
| 40 |
2/2✓ Branch 13 → 7 taken 49028 times.
✓ Branch 13 → 14 taken 7004 times.
|
56032 | for (const QString &use : kSpecialUses) |
| 41 |
2/2✓ Branch 9 → 10 taken 653 times.
✓ Branch 9 → 11 taken 48375 times.
|
49028 | if (flags.contains(use, Qt::CaseInsensitive)) |
| 42 | 653 | folder.specialUse = use; | |
| 43 |
1/1✓ Branch 15 → 16 taken 7004 times.
|
7004 | m_byImapPath.insert(imapPath, m_folders.size()); |
| 44 |
2/2✓ Branch 17 → 18 taken 7004 times.
✓ Branch 18 → 19 taken 7004 times.
|
7004 | m_bySieveName.insert(canonical(decodedPath), m_folders.size()); |
| 45 |
1/1✓ Branch 20 → 21 taken 7004 times.
|
7004 | m_folders.append(folder); |
| 46 | 7004 | } | |
| 47 | |||
| 48 | 534 | QString SieveMailboxDirectory::sieveName(const QString &imapPath) const { | |
| 49 | 534 | const int index = m_byImapPath.value(imapPath, -1); | |
| 50 |
2/2✓ Branch 3 → 4 taken 8 times.
✓ Branch 3 → 5 taken 526 times.
|
534 | return index < 0 ? QString() : m_folders.at(index).sieveName; |
| 51 | } | ||
| 52 | |||
| 53 | 1338 | QString SieveMailboxDirectory::imapPath(const QString &sieveName) const { | |
| 54 |
1/1✓ Branch 2 → 3 taken 1338 times.
|
1338 | const int index = m_bySieveName.value(canonical(sieveName), -1); |
| 55 |
2/2✓ Branch 5 → 6 taken 525 times.
✓ Branch 5 → 7 taken 813 times.
|
1338 | return index < 0 ? QString() : m_folders.at(index).imapPath; |
| 56 | } | ||
| 57 | |||
| 58 | 1 | QString SieveMailboxDirectory::specialUseOf(const QString &imapPath) const { | |
| 59 | 1 | const int index = m_byImapPath.value(imapPath, -1); | |
| 60 |
1/2✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 1 time.
|
1 | return index < 0 ? QString() : m_folders.at(index).specialUse; |
| 61 | } | ||
| 62 | |||
| 63 | 11 | QString SieveMailboxDirectory::folderForSpecialUse(const QString &specialUse) const { | |
| 64 |
2/2✓ Branch 11 → 4 taken 55 times.
✓ Branch 11 → 12 taken 1 time.
|
56 | for (const Folder &folder : m_folders) |
| 65 |
2/2✓ Branch 6 → 7 taken 10 times.
✓ Branch 6 → 9 taken 45 times.
|
55 | if (folder.specialUse.compare(specialUse, Qt::CaseInsensitive) == 0) |
| 66 | 10 | return folder.imapPath; | |
| 67 | 1 | return {}; | |
| 68 | } | ||
| 69 | |||
| 70 | 266 | QString SieveMailboxDirectory::suggestionFor(const QString &missingSieveName) const { | |
| 71 |
3/3✓ Branch 2 → 3 taken 266 times.
✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 5 taken 265 times.
|
266 | if (exists(missingSieveName)) |
| 72 | 1 | return {}; | |
| 73 |
1/1✓ Branch 5 → 6 taken 265 times.
|
265 | const QString wanted = normalizedKey(missingSieveName); |
| 74 | 265 | QStringList matches; | |
| 75 |
2/2✓ Branch 16 → 8 taken 1886 times.
✓ Branch 16 → 17 taken 265 times.
|
2151 | for (const Folder &folder : m_folders) |
| 76 |
3/3✓ Branch 9 → 10 taken 1886 times.
✓ Branch 12 → 13 taken 121 times.
✓ Branch 12 → 14 taken 1765 times.
|
1886 | if (normalizedKey(folder.sieveName) == wanted) |
| 77 |
1/1✓ Branch 13 → 14 taken 121 times.
|
121 | matches.append(folder.sieveName); |
| 78 |
3/3✓ Branch 18 → 19 taken 119 times.
✓ Branch 18 → 21 taken 146 times.
✓ Branch 19 → 20 taken 119 times.
|
265 | return matches.size() == 1 ? matches.first() : QString(); |
| 79 | 265 | } | |
| 80 | |||
| 81 | 12 | QString SieveMailboxDirectory::delimiter() const { | |
| 82 | 12 | QMap<QString, int> counts; | |
| 83 |
2/2✓ Branch 11 → 4 taken 61 times.
✓ Branch 11 → 12 taken 12 times.
|
73 | for (const Folder &folder : m_folders) |
| 84 |
1/2✓ Branch 6 → 7 taken 61 times.
✗ Branch 6 → 9 not taken.
|
61 | if (!folder.delimiter.isEmpty()) |
| 85 |
1/1✓ Branch 7 → 8 taken 61 times.
|
61 | ++counts[folder.delimiter]; |
| 86 | 12 | QString best = QStringLiteral("/"); | |
| 87 | 12 | int bestCount = 0; | |
| 88 |
4/4✓ Branch 16 → 17 taken 12 times.
✓ Branch 25 → 26 taken 23 times.
✓ Branch 27 → 18 taken 11 times.
✓ Branch 27 → 28 taken 12 times.
|
23 | for (auto it = counts.cbegin(); it != counts.cend(); ++it) |
| 89 |
1/2✓ Branch 19 → 20 taken 11 times.
✗ Branch 19 → 24 not taken.
|
11 | if (it.value() > bestCount) { |
| 90 | 11 | best = it.key(); | |
| 91 | 11 | bestCount = it.value(); | |
| 92 | } | ||
| 93 | 12 | return best; | |
| 94 | 12 | } | |
| 95 | |||
| 96 | 779 | QStringList SieveMailboxDirectory::sieveNames() const { | |
| 97 | 779 | QStringList names; | |
| 98 |
2/2✓ Branch 8 → 4 taken 2225 times.
✓ Branch 8 → 9 taken 779 times.
|
3004 | for (const Folder &folder : m_folders) |
| 99 |
1/1✓ Branch 5 → 6 taken 2225 times.
|
2225 | names.append(folder.sieveName); |
| 100 | 779 | return names; | |
| 101 | ✗ | } | |
| 102 |