diff options
Diffstat (limited to 'src/gui/image/qiconloader.cpp')
-rw-r--r-- | src/gui/image/qiconloader.cpp | 207 |
1 files changed, 186 insertions, 21 deletions
diff --git a/src/gui/image/qiconloader.cpp b/src/gui/image/qiconloader.cpp index c7f1f2beb4..0233ccbf87 100644 --- a/src/gui/image/qiconloader.cpp +++ b/src/gui/image/qiconloader.cpp @@ -1,31 +1,37 @@ /**************************************************************************** ** -** Copyright (C) 2015 The Qt Company Ltd. -** Contact: http://www.qt.io/licensing/ +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtGui module of the Qt Toolkit. ** -** $QT_BEGIN_LICENSE:LGPL21$ +** $QT_BEGIN_LICENSE:LGPL$ ** Commercial License Usage ** Licensees holding valid commercial Qt licenses may use this file in ** accordance with the commercial license agreement provided with the ** Software or, alternatively, in accordance with the terms contained in ** a written agreement between you and The Qt Company. For licensing terms -** and conditions see http://www.qt.io/terms-conditions. For further -** information use the contact form at http://www.qt.io/contact-us. +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. ** ** GNU Lesser General Public License Usage ** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 or version 3 as published by the Free -** Software Foundation and appearing in the file LICENSE.LGPLv21 and -** LICENSE.LGPLv3 included in the packaging of this file. Please review the -** following information to ensure the GNU Lesser General Public License -** requirements will be met: https://www.gnu.org/licenses/lgpl.html and -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. ** -** As a special exception, The Qt Company gives you certain additional -** rights. These rights are described in The Qt Company LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. ** ** $QT_END_LICENSE$ ** @@ -88,9 +94,7 @@ static inline QStringList systemIconSearchPaths() return QStringList(); } -#ifndef QT_NO_LIBRARY extern QFactoryLoader *qt_iconEngineFactoryLoader(); // qicon.cpp -#endif void QIconLoader::ensureInitialized() { @@ -103,10 +107,8 @@ void QIconLoader::ensureInitialized() if (m_systemTheme.isEmpty()) m_systemTheme = fallbackTheme(); -#ifndef QT_NO_LIBRARY if (qt_iconEngineFactoryLoader()->keyMap().key(QLatin1String("svg"), -1) != -1) m_supportsSvg = true; -#endif //QT_NO_LIBRARY } } @@ -155,19 +157,155 @@ QStringList QIconLoader::themeSearchPaths() const return m_iconDirs; } +/*! + \internal + Helper class that reads and looks up into the icon-theme.cache generated with + gtk-update-icon-cache. If at any point we detect a corruption in the file + (because the offsets point at wrong locations for example), the reader + is marked as invalid. +*/ +class QIconCacheGtkReader +{ +public: + explicit QIconCacheGtkReader(const QString &themeDir); + QVector<const char *> lookup(const QString &); + bool isValid() const { return m_isValid; } +private: + QFile m_file; + const unsigned char *m_data; + quint64 m_size; + bool m_isValid; + + quint16 read16(uint offset) + { + if (offset > m_size - 2 || (offset & 0x1)) { + m_isValid = false; + return 0; + } + return m_data[offset+1] | m_data[offset] << 8; + } + quint32 read32(uint offset) + { + if (offset > m_size - 4 || (offset & 0x3)) { + m_isValid = false; + return 0; + } + return m_data[offset+3] | m_data[offset+2] << 8 + | m_data[offset+1] << 16 | m_data[offset] << 24; + } +}; + + +QIconCacheGtkReader::QIconCacheGtkReader(const QString &dirName) + : m_isValid(false) +{ + QFileInfo info(dirName + QLatin1Literal("/icon-theme.cache")); + if (!info.exists() || info.lastModified() < QFileInfo(dirName).lastModified()) + return; + m_file.setFileName(info.absoluteFilePath()); + if (!m_file.open(QFile::ReadOnly)) + return; + m_size = m_file.size(); + m_data = m_file.map(0, m_size); + if (!m_data) + return; + if (read16(0) != 1) // VERSION_MAJOR + return; + + m_isValid = true; + + // Check that all the directories are older than the cache + auto lastModified = info.lastModified(); + quint32 dirListOffset = read32(8); + quint32 dirListLen = read32(dirListOffset); + for (uint i = 0; i < dirListLen; ++i) { + quint32 offset = read32(dirListOffset + 4 + 4 * i); + if (!m_isValid || offset >= m_size || lastModified < QFileInfo(dirName + QLatin1Char('/') + + QString::fromUtf8(reinterpret_cast<const char*>(m_data + offset))).lastModified()) { + m_isValid = false; + return; + } + } +} + +static quint32 icon_name_hash(const char *p) +{ + quint32 h = static_cast<signed char>(*p); + for (p += 1; *p != '\0'; p++) + h = (h << 5) - h + *p; + return h; +} + +/*! \internal + lookup the icon name and return the list of subdirectories in which an icon + with this name is present. The char* are pointers to the mapped data. + For example, this would return { "32x32/apps", "24x24/apps" , ... } + */ +QVector<const char *> QIconCacheGtkReader::lookup(const QString &name) +{ + QVector<const char *> ret; + if (!isValid()) + return ret; + + QByteArray nameUtf8 = name.toUtf8(); + quint32 hash = icon_name_hash(nameUtf8); + + quint32 hashOffset = read32(4); + quint32 hashBucketCount = read32(hashOffset); + + if (!isValid() || hashBucketCount == 0) { + m_isValid = false; + return ret; + } + + quint32 bucketIndex = hash % hashBucketCount; + quint32 bucketOffset = read32(hashOffset + 4 + bucketIndex * 4); + while (bucketOffset > 0 && bucketOffset <= m_size - 12) { + quint32 nameOff = read32(bucketOffset + 4); + if (nameOff < m_size && strcmp(reinterpret_cast<const char*>(m_data + nameOff), nameUtf8) == 0) { + quint32 dirListOffset = read32(8); + quint32 dirListLen = read32(dirListOffset); + + quint32 listOffset = read32(bucketOffset+8); + quint32 listLen = read32(listOffset); + + if (!m_isValid || listOffset + 4 + 8 * listLen > m_size) { + m_isValid = false; + return ret; + } + + ret.reserve(listLen); + for (uint j = 0; j < listLen && m_isValid; ++j) { + quint32 dirIndex = read16(listOffset + 4 + 8 * j); + quint32 o = read32(dirListOffset + 4 + dirIndex*4); + if (!m_isValid || dirIndex >= dirListLen || o >= m_size) { + m_isValid = false; + return ret; + } + ret.append(reinterpret_cast<const char*>(m_data) + o); + } + return ret; + } + bucketOffset = read32(bucketOffset); + } + return ret; +} + QIconTheme::QIconTheme(const QString &themeName) : m_valid(false) { QFile themeIndex; - QStringList iconDirs = QIcon::themeSearchPaths(); + const QStringList iconDirs = QIcon::themeSearchPaths(); for ( int i = 0 ; i < iconDirs.size() ; ++i) { QDir iconDir(iconDirs[i]); QString themeDir = iconDir.path() + QLatin1Char('/') + themeName; QFileInfo themeDirInfo(themeDir); - if (themeDirInfo.isDir()) + if (themeDirInfo.isDir()) { m_contentDirs << themeDir; + m_gtkCaches << QSharedPointer<QIconCacheGtkReader>::create(themeDir); + } if (!m_valid) { themeIndex.setFileName(themeDir + QLatin1String("/index.theme")); @@ -257,7 +395,6 @@ QThemeIconInfo QIconLoader::findIconHelper(const QString &themeName, } const QStringList contentDirs = theme.contentDirs(); - const QVector<QIconDirInfo> subDirs = theme.keyList(); QString iconNameFallback = iconName; @@ -268,6 +405,29 @@ QThemeIconInfo QIconLoader::findIconHelper(const QString &themeName, // Add all relevant files for (int i = 0; i < contentDirs.size(); ++i) { + QVector<QIconDirInfo> subDirs = theme.keyList(); + + // Try to reduce the amount of subDirs by looking in the GTK+ cache in order to save + // a massive amount of file stat (especially if the icon is not there) + auto cache = theme.m_gtkCaches.at(i); + if (cache->isValid()) { + const auto result = cache->lookup(iconNameFallback); + if (cache->isValid()) { + const QVector<QIconDirInfo> subDirsCopy = subDirs; + subDirs.clear(); + subDirs.reserve(result.count()); + for (const char *s : result) { + QString path = QString::fromUtf8(s); + auto it = std::find_if(subDirsCopy.cbegin(), subDirsCopy.cend(), + [&](const QIconDirInfo &info) { + return info.path == path; } ); + if (it != subDirsCopy.cend()) { + subDirs.append(*it); + } + } + } + } + QString contentDir = contentDirs.at(i) + QLatin1Char('/'); for (int j = 0; j < subDirs.size() ; ++j) { const QIconDirInfo &dirInfo = subDirs.at(j); @@ -587,6 +747,11 @@ void QIconLoaderEngine::virtual_hook(int id, void *data) name = m_info.iconName; } break; + case QIconEngine::IsNullHook: + { + *reinterpret_cast<bool*>(data) = m_info.entries.isEmpty(); + } + break; default: QIconEngine::virtual_hook(id, data); } |