Baloo

usergroupcache.h
1 /*
2  SPDX-FileCopyrightText: 1999 Carsten Pfeiffer <[email protected]>
3  SPDX-FileCopyrightText: 1999-2001 David Faure <[email protected]>
4  SPDX-FileCopyrightText: 2005 Till Adam <[email protected]>
5  SPDX-FileCopyrightText: 2014 Alex Richardson <[email protected]>
6  SPDX-FileCopyrightText: 2018 Friedrich W. H. Kossebau <[email protected]>
7  SPDX-FileCopyrightText: 2020 Stefan BrĂ¼ns <[email protected]>
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(uid)) {
40  KUser user(uid);
41  QString name = user.loginName();
42  if (name.isEmpty()) {
43  name = uid.toString();
44  }
45  mUsercache.insert(uid, 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(gid)) {
58  KUserGroup group(gid);
59  QString name = group.name();
60  if (name.isEmpty()) {
61  name = gid.toString();
62  }
63  mGroupcache.insert(gid, name);
64  return name;
65  }
66  return mGroupcache[gid];
67 }
68 
69 }
70 #endif
Implements storage for docIds without any associated data Instantiated for:
Definition: coding.cpp:11
QString toString() const
QString & insert(int position, QChar ch)
const char * name(StandardAction id)
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Wed Nov 29 2023 03:56:26 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.