Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge branch 'jb43023' into 'master'
[nemo-filemanager] Create QFileSystemWatcher for FileWatcher on demand. Contributes to JB#43023

See merge request mer-core/nemo-qml-plugin-filemanager!14
  • Loading branch information
rainemak committed Sep 25, 2018
2 parents 51d7e55 + bd47cad commit 123ced4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 15 deletions.
23 changes: 10 additions & 13 deletions src/plugin/filewatcher.cpp
Expand Up @@ -39,10 +39,8 @@
FileWatcher::FileWatcher(QObject *parent)
: QObject(parent)
, m_exists(false)
, m_file(new QFile(this))
, m_watcher(new QFileSystemWatcher(this))
, m_watcher(nullptr)
{
connect(m_watcher, SIGNAL(directoryChanged(QString)), this, SLOT(testFileExists()));
}

FileWatcher::~FileWatcher()
Expand All @@ -51,7 +49,7 @@ FileWatcher::~FileWatcher()

void FileWatcher::testFileExists()
{
bool exists = m_file->exists();
bool exists = m_file.exists();
if (m_exists != exists) {
m_exists = exists;
emit existsChanged();
Expand All @@ -65,22 +63,21 @@ bool FileWatcher::exists() const

QString FileWatcher::fileName() const
{
return m_file->fileName();
return m_file.fileName();
}

void FileWatcher::setFileName(const QString &fileName)
{
if (m_file->fileName() != fileName) {
if (!m_file->fileName().isEmpty()) {
m_watcher->removePath(m_file->fileName());
}
m_file->setFileName(fileName);
if (m_file.fileName() != fileName) {
delete m_watcher;
m_watcher = nullptr;
m_file.setFileName(fileName);
if (!fileName.isEmpty()) {
QFileInfo fileInfo(fileName);
QString absolutePath = fileInfo.absolutePath();
if (!m_watcher->addPath(absolutePath)) {
qmlInfo(this) << "FileWatcher: watching folder " << absolutePath << " failed";
}
m_watcher = new QFileSystemWatcher(this);
m_watcher->addPath(absolutePath);
connect(m_watcher, SIGNAL(directoryChanged(QString)), this, SLOT(testFileExists()));
}
testFileExists();
emit fileNameChanged();
Expand Down
4 changes: 2 additions & 2 deletions src/plugin/filewatcher.h
Expand Up @@ -35,9 +35,9 @@

#include <QObject>
#include <QString>
#include <QFile>

class QFileSystemWatcher;
class QFile;

class FileWatcher : public QObject
{
Expand Down Expand Up @@ -65,7 +65,7 @@ protected slots:

private:
bool m_exists;
QFile *m_file;
QFile m_file;
QFileSystemWatcher *m_watcher;
};

Expand Down

0 comments on commit 123ced4

Please sign in to comment.