Messagelib

iconnamecache.cpp
1/* SPDX-FileCopyrightText: 2009 Thomas McGuire <mcguire@kde.org>
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
11using namespace MessageViewer;
12
13Q_GLOBAL_STATIC(IconNameCache, s_iconNameCache)
14
15IconNameCache *IconNameCache::instance()
16{
17 return s_iconNameCache;
18}
19
20bool 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
30QString 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 QString fileName = KIconLoader::global()->iconPath(name, size);
42 if (fileName.startsWith(QLatin1StringView(":/"))) {
43 fileName = QStringLiteral("qrc") + fileName;
44 }
45 mCachedEntries.insert(entry, fileName);
46 return fileName;
47}
48
49QString IconNameCache::iconPathFromLocal(const QString &name) const
50{
51 Entry entry;
52 entry.fileName = name;
53 entry.size = 0;
54
55 const QString val = mCachedEntries.value(entry);
56 if (!val.isEmpty()) {
57 return val;
58 }
59
60 const QString fileName = picsPath() + name;
61 mCachedEntries.insert(entry, fileName);
62 return fileName;
63}
64
65QString IconNameCache::picsPath() const
66{
67 if (mPicsPath.isEmpty()) {
68 mPicsPath = QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("libmessageviewer/pics/"), QStandardPaths::LocateDirectory);
69 }
70 return mPicsPath;
71}
static KIconLoader * global()
QString iconPath(const QString &name, int group_or_size, bool canReturnNull, qreal scale) const
This class is a replacement for KIconLoader::iconPath(), because the iconPath() function can be slow ...
QString name(GameStandardAction id)
iterator insert(const Key &key, const T &value)
T value(const Key &key, const T &defaultValue) const const
QString locate(StandardLocation type, const QString &fileName, LocateOptions options)
int compare(QLatin1StringView s1, const QString &s2, Qt::CaseSensitivity cs)
bool isEmpty() const const
qsizetype size() const const
bool startsWith(QChar c, Qt::CaseSensitivity cs) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:33:26 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.