File indexing completed on 2025-01-05 04:58:17

0001 /*
0002  * SPDX-FileCopyrightText: 2010 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
0003  * SPDX-FileCopyrightText: 2010 Tobias Koenig <tokoe@kdab.com>
0004  *
0005  * SPDX-License-Identifier: GPL-2.0-or-later
0006  */
0007 
0008 #include "aclutils_p.h"
0009 
0010 #include <KLazyLocalizedString>
0011 #include <KLocalizedString>
0012 
0013 using namespace PimCommon;
0014 
0015 static const struct {
0016     KIMAP::Acl::Rights permissions;
0017     const KLazyLocalizedString userString;
0018 } standardPermissions[] = {
0019     {KIMAP::Acl::None, kli18nc("Permissions", "None")},
0020 
0021     {KIMAP::Acl::Lookup | KIMAP::Acl::Read | KIMAP::Acl::KeepSeen, kli18nc("Permissions", "Read")},
0022 
0023     {KIMAP::Acl::Lookup | KIMAP::Acl::Read | KIMAP::Acl::KeepSeen | KIMAP::Acl::Insert | KIMAP::Acl::Post, kli18nc("Permissions", "Append")},
0024 
0025     {KIMAP::Acl::Lookup | KIMAP::Acl::Read | KIMAP::Acl::KeepSeen | KIMAP::Acl::Insert | KIMAP::Acl::Post | KIMAP::Acl::Write | KIMAP::Acl::CreateMailbox
0026          | KIMAP::Acl::DeleteMailbox | KIMAP::Acl::DeleteMessage | KIMAP::Acl::Expunge,
0027      kli18nc("Permissions", "Write")},
0028 
0029     {KIMAP::Acl::Lookup | KIMAP::Acl::Read | KIMAP::Acl::KeepSeen | KIMAP::Acl::Insert | KIMAP::Acl::Post | KIMAP::Acl::Write | KIMAP::Acl::CreateMailbox
0030          | KIMAP::Acl::DeleteMailbox | KIMAP::Acl::DeleteMessage | KIMAP::Acl::Expunge | KIMAP::Acl::Admin,
0031      kli18nc("Permissions", "All")}};
0032 
0033 uint AclUtils::standardPermissionsCount()
0034 {
0035     return sizeof(standardPermissions) / sizeof(*standardPermissions);
0036 }
0037 
0038 KIMAP::Acl::Rights AclUtils::permissionsForIndex(uint index)
0039 {
0040     Q_ASSERT(index < standardPermissionsCount());
0041 
0042     return standardPermissions[index].permissions;
0043 }
0044 
0045 int AclUtils::indexForPermissions(KIMAP::Acl::Rights permissions)
0046 {
0047     const uint maxSize(sizeof(standardPermissions) / sizeof(*standardPermissions));
0048     for (uint i = 0; i < maxSize; ++i) {
0049         if (KIMAP::Acl::normalizedRights(permissions) == standardPermissions[i].permissions) {
0050             return i;
0051         }
0052     }
0053 
0054     return -1;
0055 }
0056 
0057 QString AclUtils::permissionsToUserString(KIMAP::Acl::Rights permissions)
0058 {
0059     const uint maxSize(sizeof(standardPermissions) / sizeof(*standardPermissions));
0060     for (uint i = 0; i < maxSize; ++i) {
0061         if (KIMAP::Acl::normalizedRights(permissions) == standardPermissions[i].permissions) {
0062             return KLocalizedString(standardPermissions[i].userString).toString();
0063         }
0064     }
0065 
0066     return i18n("Custom Permissions (%1)", QString::fromLatin1(KIMAP::Acl::rightsToString(permissions)));
0067 }
0068 
0069 QString AclUtils::guessUserName(const QString &loginName, const QString &serverName)
0070 {
0071     int pos = loginName.indexOf(QLatin1Char('@'));
0072     if (pos != -1) {
0073         // strip of the domain part and use user name only
0074         return loginName.left(pos);
0075     } else {
0076         pos = serverName.lastIndexOf(QLatin1Char('.'));
0077         if (pos == -1) { // no qualified domain name, only hostname
0078             return QStringLiteral("%1@%2").arg(loginName, serverName);
0079         }
0080 
0081         pos = serverName.lastIndexOf(QLatin1Char('.'), pos - 1);
0082         if (pos == -1) { // a simple domain name e.g. mydomain.org
0083             return QStringLiteral("%1@%2").arg(loginName, serverName);
0084         } else {
0085             return QStringLiteral("%1@%2").arg(loginName, serverName.mid(pos + 1));
0086         }
0087     }
0088 }