~ubuntu-docviewer-dev/ubuntu-docviewer-app/lo-viewer

« back to all changes in this revision

Viewing changes to src/plugin/file-qml-plugin/fswatcher.cpp

Update framework to 'ubuntu-sdk-14.10'. Fixes: https://bugs.launchpad.net/bugs/1401718.

Approved by Nekhelesh Ramananthan, Ubuntu Phone Apps Jenkins Bot.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 *  Copyright (C) 2014, 2015
3
 
 *                  Stefano Verzegnassi <stefano92.100@gmail.com>
4
 
 *
5
 
 *  This 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, either version 3 of the License.
8
 
 *
9
 
 *  This 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 "fswatcher.h"
19
 
#include <QDirIterator>
20
 
 
21
 
#include <QFile>
22
 
#include <QDir>
23
 
 
24
 
#include <QDebug>
25
 
 
26
 
FSWatcher::FSWatcher(QObject *parent)
27
 
    : QFileSystemWatcher(parent)
28
 
{
29
 
    connect(this, SIGNAL(fileChanged(QString)), this, SLOT(q_fileChanged(QString)));
30
 
    connect(this, SIGNAL(directoryChanged(QString)), this, SLOT(q_dirChanged(QString)));
31
 
}
32
 
 
33
 
void FSWatcher::clear()
34
 
{
35
 
    if (!this->directories().isEmpty())
36
 
        this->removePaths(this->directories());
37
 
 
38
 
    if (!this->files().isEmpty())
39
 
        this->removePaths(this->files());
40
 
}
41
 
 
42
 
void FSWatcher::addDirectory(const QString &path)
43
 
{
44
 
    if (!this->directories().contains(path))
45
 
    {
46
 
        this->addPath(path);
47
 
        Q_EMIT directoryAdded(path);
48
 
 
49
 
        parseDirectoryContent(path);
50
 
    }
51
 
}
52
 
 
53
 
void FSWatcher::q_fileChanged(const QString &path)
54
 
{
55
 
    // Check if file has been removed
56
 
    if (!QFile::exists(path))
57
 
    {
58
 
        Q_EMIT fileRemoved(path);
59
 
        return;
60
 
    }
61
 
 
62
 
    /* We got a signal for a file already watched, but it's not been deleted.
63
 
       That means that it has been modified. */
64
 
    Q_EMIT fileModified(path);
65
 
}
66
 
 
67
 
void FSWatcher::q_dirChanged(const QString &path)
68
 
{
69
 
    QDir dir(path);
70
 
 
71
 
    /* Check if file has been removed.
72
 
       When this happens, files in that folder are automatically removed
73
 
       from watcher. We need to manually notify their removal.
74
 
       */
75
 
    if (!dir.exists())
76
 
    {
77
 
        Q_FOREACH (const QString& file, m_cachedFilesList)
78
 
        {
79
 
            if (!this->files().contains(file))
80
 
            {
81
 
                Q_EMIT fileRemoved(file);
82
 
              //  qDebug() << "file removed" << file;
83
 
            }
84
 
        }
85
 
 
86
 
        m_cachedFilesList = this->files();
87
 
 
88
 
        Q_EMIT directoryRemoved(path);
89
 
        return;
90
 
    }
91
 
 
92
 
    // The content of the directory has changed (a file has been added or removed)
93
 
    // Let's check it out!
94
 
    parseDirectoryContent(path);
95
 
}
96
 
 
97
 
void FSWatcher::parseDirectoryContent(QString path)
98
 
{
99
 
    QString filePath;
100
 
    QDirIterator dir(path, QDir::Files | QDir::NoDotAndDotDot | QDir::Readable,
101
 
                     QDirIterator::Subdirectories);
102
 
 
103
 
    while (dir.hasNext())
104
 
    {
105
 
        dir.next();
106
 
        filePath = dir.filePath();
107
 
 
108
 
        // Do not check for files already watched.
109
 
        if (!this->files().contains(filePath))
110
 
        {
111
 
            // Add the file to the tracker
112
 
            this->addPath(filePath);
113
 
            Q_EMIT fileAdded(filePath);
114
 
        }
115
 
    }
116
 
 
117
 
    m_cachedFilesList = this->files();
118
 
}