Skip to content

Commit

Permalink
[filemanager] Add FileWatcher. Contributes to JB#41873
Browse files Browse the repository at this point in the history
  • Loading branch information
rainemak committed Jun 21, 2018
1 parent a0bf9e8 commit 9f7df85
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 0 deletions.
93 changes: 93 additions & 0 deletions src/plugin/filewatcher.cpp
@@ -0,0 +1,93 @@
/*
* Copyright (C) 2018 Jolla Ltd.
* Contact: Joona Petrell <joona.petrell@jollamobile.com>
*
* You may use this file under the terms of the BSD license as follows:
*
* "Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Jolla Ltd. nor the names of its contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
*/

#include "filewatcher.h"
#include <QFile>
#include <QFileInfo>
#include <QFileSystemWatcher>
#include <QQmlInfo>

FileWatcher::FileWatcher(QObject *parent)
: QObject(parent)
, m_exists(false)
, m_file(new QFile(this))
, m_watcher(new QFileSystemWatcher(this))
{
connect(m_watcher, SIGNAL(directoryChanged(QString)), this, SLOT(testFileExists()));
}

FileWatcher::~FileWatcher()
{
}

void FileWatcher::testFileExists()
{
bool exists = m_file->exists();
if (m_exists != exists) {
m_exists = exists;
emit existsChanged();
}
}

bool FileWatcher::exists() const
{
return m_exists;
}

QString FileWatcher::fileName() const
{
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 (!fileName.isEmpty()) {
QFileInfo fileInfo(fileName);
QString absolutePath = fileInfo.absolutePath();
if (!m_watcher->addPath(absolutePath)) {
qmlInfo(this) << "FileWatcher: watching folder " << absolutePath << " failed";
}
}
testFileExists();
emit fileNameChanged();
}
}

bool FileWatcher::testFileExists(const QString &fileName) const
{
return QFile::exists(fileName);
}
72 changes: 72 additions & 0 deletions src/plugin/filewatcher.h
@@ -0,0 +1,72 @@
/*
* Copyright (C) 2018 Jolla Ltd.
* Contact: Joona Petrell <joona.petrell@jollamobile.com>
*
* You may use this file under the terms of the BSD license as follows:
*
* "Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Jolla Ltd. nor the names of its contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
*/

#ifndef FILEWATCHER_H
#define FILEWATCHER_H

#include <QObject>
#include <QString>

class QFileSystemWatcher;
class QFile;

class FileWatcher : public QObject
{
Q_OBJECT
Q_PROPERTY(QString fileName READ fileName WRITE setFileName NOTIFY fileNameChanged)
Q_PROPERTY(bool exists READ exists NOTIFY existsChanged)

public:
explicit FileWatcher(QObject *parent = 0);
~FileWatcher();

bool exists() const;

QString fileName() const;
void setFileName(const QString &fileName);

Q_INVOKABLE bool testFileExists(const QString &fileName) const;

signals:
void existsChanged();
void fileNameChanged();

protected slots:
void testFileExists();

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

#endif // FILEWATCHER_H
2 changes: 2 additions & 0 deletions src/plugin/plugin.cpp
Expand Up @@ -39,6 +39,7 @@
#include "archivemodel.h"
#include "fileengine.h"
#include "filemodel.h"
#include "filewatcher.h"

static QObject *engine_api_factory(QQmlEngine *, QJSEngine *)
{
Expand All @@ -63,6 +64,7 @@ class Q_DECL_EXPORT NemoFileManagerPlugin : public QQmlExtensionPlugin
Q_ASSERT(uri == QLatin1String("Nemo.FileManager"));
qmlRegisterType<FileModel>(uri, 1, 0, "FileModel");
qmlRegisterType<Sailfish::ArchiveModel>("Nemo.FileManager", 1, 0, "ArchiveModel");
qmlRegisterType<FileWatcher>(uri, 1, 0, "FileWatcher");
qmlRegisterSingletonType<FileEngine>(uri, 1, 0, "FileEngine", engine_api_factory);

qRegisterMetaType<FileEngine::Error>("FileEngine::Error");
Expand Down
2 changes: 2 additions & 0 deletions src/plugin/plugin.pro
Expand Up @@ -35,6 +35,7 @@ SOURCES += archiveinfo.cpp \
filemodel.cpp \
fileoperations.cpp \
fileoperationsproxy.cpp \
filewatcher.cpp \
fileworker.cpp \
plugin.cpp \
statfileinfo.cpp
Expand All @@ -46,6 +47,7 @@ HEADERS += archiveinfo.h \
filemodel.h \
fileoperations.h \
fileoperationsproxy.h \
filewatcher.h \
fileworker.h \
statfileinfo.h

Expand Down

0 comments on commit 9f7df85

Please sign in to comment.