Skip to content

Commit

Permalink
tmxrasterizer: Don't watch tileset images for changes
Browse files Browse the repository at this point in the history
The TilesetManager no longer watches by default, it now relies on the
Tiled Preferences to enable this. This makes sure, that other tools,
like the tmxrasterizer, for which it makes no sense to watch the file,
will not watch the files (and won't trigger SELinux pop-ups on
Fedora...).
  • Loading branch information
bjorn committed Feb 14, 2024
1 parent 0e3dfd6 commit aa5619c
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 14 deletions.
31 changes: 28 additions & 3 deletions src/libtiled/filesystemwatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,23 @@ FileSystemWatcher::FileSystemWatcher(QObject *parent) :
this, &FileSystemWatcher::pathsChangedTimeout);
}

void FileSystemWatcher::setEnabled(bool enabled)
{
if (mEnabled == enabled)
return;

mEnabled = enabled;

if (enabled) {
const auto files = mWatchCount.keys();
if (!files.isEmpty())
mWatcher->addPaths(files);
} else {
clearInternal();
mChangedPathsTimer.stop();
}
}

void FileSystemWatcher::addPaths(const QStringList &paths)
{
QStringList pathsToAdd;
Expand All @@ -63,7 +80,9 @@ void FileSystemWatcher::addPaths(const QStringList &paths)

QMap<QString, int>::iterator entry = mWatchCount.find(path);
if (entry == mWatchCount.end()) {
pathsToAdd.append(path);
if (mEnabled)
pathsToAdd.append(path);

mWatchCount.insert(path, 1);
} else {
// Path is already being watched, increment watch count
Expand Down Expand Up @@ -93,15 +112,17 @@ void FileSystemWatcher::removePaths(const QStringList &paths)

if (entry.value() == 0) {
mWatchCount.erase(entry);
pathsToRemove.append(path);

if (mEnabled)
pathsToRemove.append(path);
}
}

if (!pathsToRemove.isEmpty())
mWatcher->removePaths(pathsToRemove);
}

void FileSystemWatcher::clear()
void FileSystemWatcher::clearInternal()
{
const QStringList files = mWatcher->files();
if (!files.isEmpty())
Expand All @@ -110,7 +131,11 @@ void FileSystemWatcher::clear()
const QStringList directories = mWatcher->directories();
if (!directories.isEmpty())
mWatcher->removePaths(directories);
}

void FileSystemWatcher::clear()
{
clearInternal();
mWatchCount.clear();
}

Expand Down
10 changes: 10 additions & 0 deletions src/libtiled/filesystemwatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ class TILEDSHARED_EXPORT FileSystemWatcher : public QObject
public:
explicit FileSystemWatcher(QObject *parent = nullptr);

void setEnabled(bool enabled);
bool isEnabled() const;

void addPath(const QString &path);
void addPaths(const QStringList &paths);
void removePath(const QString &path);
Expand All @@ -79,14 +82,21 @@ class TILEDSHARED_EXPORT FileSystemWatcher : public QObject
void onFileChanged(const QString &path);
void onDirectoryChanged(const QString &path);
void pathsChangedTimeout();
void clearInternal();

QFileSystemWatcher *mWatcher;
QMap<QString, int> mWatchCount;

QSet<QString> mChangedPaths;
QTimer mChangedPathsTimer;
bool mEnabled = true;
};

inline bool FileSystemWatcher::isEnabled() const
{
return mEnabled;
}

inline void FileSystemWatcher::addPath(const QString &path)
{
addPaths(QStringList(path));
Expand Down
16 changes: 9 additions & 7 deletions src/libtiled/tilesetmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ TilesetManager *TilesetManager::mInstance;
*/
TilesetManager::TilesetManager():
mWatcher(new FileSystemWatcher(this)),
mAnimationDriver(new TileAnimationDriver(this)),
mReloadTilesetsOnChange(false)
mAnimationDriver(new TileAnimationDriver(this))
{
mWatcher->setEnabled(false);

connect(mWatcher, &FileSystemWatcher::pathsChanged,
this, &TilesetManager::filesChanged);

Expand Down Expand Up @@ -169,8 +170,12 @@ void TilesetManager::reloadImages(Tileset *tileset)
*/
void TilesetManager::setReloadTilesetsOnChange(bool enabled)
{
mReloadTilesetsOnChange = enabled;
// TODO: Clear the file system watcher when disabled
mWatcher->setEnabled(enabled);
}

bool TilesetManager::reloadTilesetsOnChange() const
{
return mWatcher->isEnabled();
}

/**
Expand Down Expand Up @@ -206,9 +211,6 @@ void TilesetManager::tilesetImageSourceChanged(const Tileset &tileset,

void TilesetManager::filesChanged(const QStringList &fileNames)
{
if (!mReloadTilesetsOnChange)
return;

for (const QString &fileName : fileNames)
ImageCache::remove(fileName);

Expand Down
4 changes: 0 additions & 4 deletions src/libtiled/tilesetmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,8 @@ class TILEDSHARED_EXPORT TilesetManager : public QObject
QList<Tileset*> mTilesets;
FileSystemWatcher *mWatcher;
TileAnimationDriver *mAnimationDriver;
bool mReloadTilesetsOnChange;

static TilesetManager *mInstance;
};

inline bool TilesetManager::reloadTilesetsOnChange() const
{ return mReloadTilesetsOnChange; }

} // namespace Tiled

0 comments on commit aa5619c

Please sign in to comment.