File indexing completed on 2024-12-22 04:13:05

0001 /*
0002  *  SPDX-FileCopyrightText: 2022 Alvin Wong <alvin@alvinhc.com>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #ifndef KIS_RECENT_FILE_ICON_CACHE_H
0008 #define KIS_RECENT_FILE_ICON_CACHE_H
0009 
0010 #include <QFuture>
0011 #include <QIcon>
0012 #include <QMap>
0013 #include <QThreadPool>
0014 #include <QUrl>
0015 
0016 class KisRecentFileIconCache : public QObject
0017 {
0018     Q_OBJECT
0019 
0020     struct CacheItem;
0021 
0022     QMap<QUrl, CacheItem> m_iconCacheMap;
0023     QThreadPool m_iconFetchThreadPool;
0024 
0025 public:
0026     /**
0027      * DO NOT USE! Use `instance()` instead. This constructor is public only
0028      * because it is needed by Q_GLOBAL_STATIC.
0029      */
0030     KisRecentFileIconCache();
0031     ~KisRecentFileIconCache();
0032 
0033 public:
0034     static KisRecentFileIconCache *instance();
0035 
0036     /**
0037      * Get a cached icon or queue fetching of the icon.
0038      *
0039      * If the icon is cached and available, the cached icon is returned.
0040      * Otherwise, a null default-constructed `QIcon` will be returned, and
0041      * the fetching of the icon *may* be queued in a background thread.
0042      */
0043     QIcon getOrQueueFileIcon(const QUrl &url);
0044 
0045     /**
0046      * Invalidate (remove) a cached file icon. If the file icon is still in
0047      * the process of being loaded, its result will be discarded.
0048      */
0049     void invalidateFileIcon(const QUrl &url);
0050 
0051     /**
0052      * Invalidate a cached file icon and trigger a reload of it.
0053      */
0054     void reloadFileIcon(const QUrl &url);
0055 
0056 private Q_SLOTS:
0057     void cleanupOnQuit();
0058     void iconFetched();
0059     void futureCanceled();
0060 
0061 Q_SIGNALS:
0062     void fileIconChanged(const QUrl &url, const QIcon &icon);
0063 };
0064 
0065 #endif /* KIS_RECENT_FILE_ICON_CACHE_H */