Messagelib

iconnamecache.cpp
1 /* SPDX-FileCopyrightText: 2009 Thomas McGuire <[email protected]>
2 
3  SPDX-License-Identifier: LGPL-2.0-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
4 */
5 
6 #include "iconnamecache.h"
7 
8 #include <KIconLoader>
9 #include <QStandardPaths>
10 
11 using namespace MessageViewer;
12 
13 Q_GLOBAL_STATIC(IconNameCache, s_iconNameCache)
14 
15 IconNameCache *IconNameCache::instance()
16 {
17  return s_iconNameCache;
18 }
19 
20 bool IconNameCache::Entry::operator<(const Entry &other) const
21 {
22  const int fileNameCompare = fileName.compare(other.fileName);
23  if (fileNameCompare != 0) {
24  return fileNameCompare < 0;
25  } else {
26  return size < other.size;
27  }
28 }
29 
30 QString IconNameCache::iconPath(const QString &name, int size) const
31 {
32  Entry entry;
33  entry.fileName = name;
34  entry.size = size;
35 
36  const QString val = mCachedEntries.value(entry);
37  if (!val.isEmpty()) {
38  return val;
39  }
40 
41  const QString fileName = KIconLoader::global()->iconPath(name, size);
42  mCachedEntries.insert(entry, fileName);
43  return fileName;
44 }
45 
46 QString IconNameCache::iconPathFromLocal(const QString &name) const
47 {
48  Entry entry;
49  entry.fileName = name;
50  entry.size = 0;
51 
52  const QString val = mCachedEntries.value(entry);
53  if (!val.isEmpty()) {
54  return val;
55  }
56 
57  const QString fileName = picsPath() + name;
58  mCachedEntries.insert(entry, fileName);
59  return fileName;
60 }
61 
62 QString IconNameCache::picsPath() const
63 {
64  if (mPicsPath.isEmpty()) {
65  mPicsPath = QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("libmessageviewer/pics/"), QStandardPaths::LocateDirectory);
66  }
67  return mPicsPath;
68 }
int size() const const
Q_GLOBAL_STATIC(Internal::StaticControl, s_instance) class ControlPrivate
This class is a replacement for KIconLoader::iconPath(), because the iconPath() function can be slow ...
Definition: iconnamecache.h:20
bool isEmpty() const const
static KIconLoader * global()
QMap::iterator insert(const Key &key, const T &value)
QString locate(QStandardPaths::StandardLocation type, const QString &fileName, QStandardPaths::LocateOptions options)
const T value(const Key &key, const T &defaultValue) const const
QString iconPath(const QString &name, int group_or_size, bool canReturnNull=false) const
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Thu Dec 2 2021 23:06:08 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.