1 | /* |
2 | SPDX-FileCopyrightText: 1999 Carsten Pfeiffer <pfeiffer@kde.org> |
3 | SPDX-FileCopyrightText: 1999-2001 David Faure <faure@kde.org> |
4 | SPDX-FileCopyrightText: 2005 Till Adam <adam@kde.org> |
5 | SPDX-FileCopyrightText: 2014 Alex Richardson <arichardson@kde.org> |
6 | SPDX-FileCopyrightText: 2018 Friedrich W. H. Kossebau <kossebau@kde.org> |
7 | SPDX-FileCopyrightText: 2020 Stefan BrĂ¼ns <bruns@kde.org> |
8 | |
9 | SPDX-License-Identifier: LGPL-2.0-or-later |
10 | */ |
11 | |
12 | #ifndef BALOO_KIO_COMMON_USERGROUPCACHE_H_ |
13 | #define BALOO_KIO_COMMON_USERGROUPCACHE_H_ |
14 | |
15 | #include <KUser> |
16 | |
17 | #include <QHash> |
18 | #include <QString> |
19 | |
20 | namespace Baloo { |
21 | |
22 | class UserGroupCache |
23 | { |
24 | public: |
25 | QString getUserName(const KUserId &uid) const; |
26 | QString getGroupName(const KGroupId &gid) const; |
27 | |
28 | private: |
29 | mutable QHash<KUserId, QString> mUsercache; |
30 | mutable QHash<KGroupId, QString> mGroupcache; |
31 | }; |
32 | |
33 | inline QString |
34 | UserGroupCache::getUserName(const KUserId &uid) const |
35 | { |
36 | if (Q_UNLIKELY(!uid.isValid())) { |
37 | return QString(); |
38 | } |
39 | if (!mUsercache.contains(key: uid)) { |
40 | KUser user(uid); |
41 | QString name = user.loginName(); |
42 | if (name.isEmpty()) { |
43 | name = uid.toString(); |
44 | } |
45 | mUsercache.insert(key: uid, value: name); |
46 | return name; |
47 | } |
48 | return mUsercache[uid]; |
49 | } |
50 | |
51 | inline QString |
52 | UserGroupCache::getGroupName(const KGroupId &gid) const |
53 | { |
54 | if (Q_UNLIKELY(!gid.isValid())) { |
55 | return QString(); |
56 | } |
57 | if (!mGroupcache.contains(key: gid)) { |
58 | KUserGroup group(gid); |
59 | QString name = group.name(); |
60 | if (name.isEmpty()) { |
61 | name = gid.toString(); |
62 | } |
63 | mGroupcache.insert(key: gid, value: name); |
64 | return name; |
65 | } |
66 | return mGroupcache[gid]; |
67 | } |
68 | |
69 | } |
70 | #endif |
71 | |