Lomiri
Loading...
Searching...
No Matches
iconcachewatcher.cpp
1/*
2 * Copyright (C) 2025 UBports Foundation
3 * Author(s): Riccardo Riccio <riccardo.riccio@ubports.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; version 3.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "iconcachewatcher.h"
19
20#include <QDebug>
21#include <QDir>
22#include <QFile>
23
24IconCacheWatcher::IconCacheWatcher(QObject *parent)
25 : QObject(parent),
26 m_watcher(new QFileSystemWatcher(this))
27{
28 // List of base directories
29 QStringList baseDirs = {
30 "/usr/share",
31 "/usr/local/share",
32 QDir::homePath() + "/.local/share"
33 };
34
35 QStringList cacheFiles;
36 QStringList foldersToWatch;
37 for (const QString &base : baseDirs) {
38 // icons
39 QString iconsDir = base + "/icons";
40 QDir icons(iconsDir);
41 if (icons.exists()) {
42 foldersToWatch << iconsDir;
43 QFileInfoList subdirs = icons.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot);
44 for (const QFileInfo &subdir : subdirs) {
45 QString cachePath = subdir.absoluteFilePath() + "/icon-theme.cache";
46 if (QFile::exists(cachePath)) {
47 cacheFiles << cachePath;
48 }
49 }
50 }
51 // pixmaps
52 QString pixmapsDir = base + "/pixmaps";
53 QDir pixmaps(pixmapsDir);
54 if (pixmaps.exists()) {
55 foldersToWatch << pixmapsDir;
56 }
57 // meta/gui
58 QString metaGuiDir = base + "/meta/gui";
59 QDir metaGui(metaGuiDir);
60 if (metaGui.exists()) {
61 foldersToWatch << metaGuiDir;
62 }
63 }
64
65 if (!cacheFiles.isEmpty()) {
66 m_watcher->addPaths(cacheFiles);
67 connect(m_watcher, &QFileSystemWatcher::fileChanged,
68 this, &IconCacheWatcher::onCacheFileChanged);
69 }
70
71 if (!foldersToWatch.isEmpty()) {
72 m_watcher->addPaths(foldersToWatch);
73 connect(m_watcher, &QFileSystemWatcher::directoryChanged,
74 this, &IconCacheWatcher::onIconDirectoryChanged);
75 }
76
77 if (foldersToWatch.isEmpty() && cacheFiles.isEmpty()) {
78 qWarning() << "No icon-theme.cache files found in standard icon directories.";
79 } else {
80 qDebug() << "IconCacheWatcher initialized with" << foldersToWatch.size() << "directories and"
81 << cacheFiles.size() << "cache files.";
82 }
83}
84
85void IconCacheWatcher::onCacheFileChanged(const QString &path)
86{
87 Q_EMIT iconCacheChanged();
88 // Re-add the path in case the file was recreated
89 if (QFile::exists(path)) {
90 m_watcher->addPath(path);
91 }
92}
93
94void IconCacheWatcher::onIconDirectoryChanged(const QString &path)
95{
96 // Defer update if dpkg-new files are present (icon is still being installed)
97 QDir dir(path);
98 QStringList dpkgNewFiles = dir.entryList(QStringList("*.dpkg-new"), QDir::Files);
99 if (!dpkgNewFiles.isEmpty())
100 return;
101
102 Q_EMIT iconCacheChanged();
103}