Removed bug when renaming icon file in nested folder while application is running

Signed-off-by: QazCetelic <qaz.cetelic@protonmail.com>
This commit is contained in:
QazCetelic 2024-11-17 20:35:24 +01:00
parent f641f3acda
commit abbebff400

View File

@ -147,22 +147,23 @@ QStringList IconList::getIconFilePaths() const
return iconFiles; return iconFiles;
} }
QString formatName(const QDir& iconsDir, const QFileInfo& file) QString formatName(const QDir& iconsDir, const QFileInfo& iconFile)
{ {
if (file.dir() == iconsDir) if (iconFile.dir() == iconsDir)
return file.baseName(); return iconFile.baseName();
constexpr auto delimiter = " » "; constexpr auto delimiter = " » ";
QString relativePathWithoutExtension = iconsDir.relativeFilePath(file.dir().path()) + QDir::separator() + file.baseName(); QString relativePathWithoutExtension = iconsDir.relativeFilePath(iconFile.dir().path()) + QDir::separator() + iconFile.baseName();
return relativePathWithoutExtension.replace(QDir::separator(), delimiter); return relativePathWithoutExtension.replace(QDir::separator(), delimiter);
} }
QSet<QString> toStringSet(const QList<QString>& list) // Split into a separate function because the preprocessing impedes readability /// Split into a separate function because the preprocessing impedes readability
QSet<QString> toStringSet(const QList<QString>& list)
{ {
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0) #if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
QSet<QString> set(list.begin(), list.end()); QSet<QString> set(list.begin(), list.end());
#else #else
auto set = list.toSet(); QSet<QString> set = list.toSet();
#endif #endif
return set; return set;
} }
@ -171,6 +172,7 @@ void IconList::directoryChanged(const QString& path)
{ {
QDir newDir(path); QDir newDir(path);
if (m_dir.absolutePath() != newDir.absolutePath()) { if (m_dir.absolutePath() != newDir.absolutePath()) {
if (!path.startsWith(m_dir.absolutePath()))
m_dir.setPath(path); m_dir.setPath(path);
m_dir.refresh(); m_dir.refresh();
if (is_watching) if (is_watching)
@ -192,9 +194,9 @@ void IconList::directoryChanged(const QString& path)
QSet<QString> toRemove = currentSet - newSet; QSet<QString> toRemove = currentSet - newSet;
QSet<QString> toAdd = newSet - currentSet; QSet<QString> toAdd = newSet - currentSet;
for (const auto& remove : toRemove) { for (const QString& removedPath : toRemove) {
qDebug() << "Removing " << remove; qDebug() << "Removing icon " << removedPath;
QFileInfo removedFile(remove); QFileInfo removedFile(removedPath);
QString key = m_dir.relativeFilePath(removedFile.absoluteFilePath()); QString key = m_dir.relativeFilePath(removedFile.absoluteFilePath());
int idx = getIconIndex(key); int idx = getIconIndex(key);
@ -209,19 +211,19 @@ void IconList::directoryChanged(const QString& path)
} else { } else {
dataChanged(index(idx), index(idx)); dataChanged(index(idx), index(idx));
} }
m_watcher->removePath(remove); m_watcher->removePath(removedPath);
emit iconUpdated(key); emit iconUpdated(key);
} }
for (const auto& add : toAdd) { for (const QString& addedPath : toAdd) {
qDebug() << "Adding " << add; qDebug() << "Adding icon " << addedPath;
QFileInfo addfile(add); QFileInfo addfile(addedPath);
QString key = m_dir.relativeFilePath(addfile.absoluteFilePath()); QString key = m_dir.relativeFilePath(addfile.absoluteFilePath());
QString name = formatName(m_dir, addfile); QString name = formatName(m_dir, addfile);
if (addIcon(key, name, addfile.filePath(), IconType::FileBased)) { if (addIcon(key, name, addfile.filePath(), IconType::FileBased)) {
m_watcher->addPath(add); m_watcher->addPath(addedPath);
emit iconUpdated(key); emit iconUpdated(key);
} }
} }
@ -231,7 +233,7 @@ void IconList::directoryChanged(const QString& path)
void IconList::fileChanged(const QString& path) void IconList::fileChanged(const QString& path)
{ {
qDebug() << "Checking " << path; qDebug() << "Checking icon " << path;
QFileInfo checkfile(path); QFileInfo checkfile(path);
if (!checkfile.exists()) if (!checkfile.exists())
return; return;