~ubuntu-filemanager-dev/ubuntu-filemanager-app/trunk

« back to all changes in this revision

Viewing changes to src/plugin/folderlistmodel/externalfswatcher.cpp

  • Committer: Tarmac
  • Author(s): carlos-mazieri
  • Date: 2014-05-16 15:27:05 UTC
  • mfrom: (168.3.3 app-devel-pre2)
  • Revision ID: tarmac-20140516152705-7vfzc1unhe9g03ml
this is the second part of the https://code.launchpad.net/~carlos-mazieri/ubuntu-filemanager-app/app-devel/+merge/216409.

Approved by Arto Jalkanen, Ubuntu Phone Apps Jenkins Bot.

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
#if DEBUG_EXT_FS_WATCHER
29
29
# define DEBUG_FSWATCHER()    \
30
30
    qDebug() << "[extFsWatcher]" << QDateTime::currentDateTime().toString("hh:mm:ss.zzz") \
31
 
             << Q_FUNC_INFO << "m_setPath:" << m_setPath \
 
31
             << Q_FUNC_INFO << "m_setPath:" << m_setPaths \
32
32
             << "m_changedPath:" << m_changedPath        \
33
33
             << "m_waitingEmit:" << m_waitingEmitCounter
34
34
#else
40
40
    QFileSystemWatcher(parent)
41
41
  , m_waitingEmitCounter(0)
42
42
  , m_msWaitTime(DEFAULT_NOTICATION_PERIOD)
 
43
  , m_lastChangedIndex(-1)
43
44
{
44
45
    connect(this,   SIGNAL(directoryChanged(QString)),
45
46
            this,   SLOT(slotDirChanged(QString)));
48
49
 
49
50
void ExternalFSWatcher::setCurrentPath(const QString &curPath)
50
51
{
51
 
    if (!curPath.isEmpty())
 
52
    if (!curPath.isEmpty() && (m_setPaths.count() != 1 || m_setPaths.at(0) != curPath))
52
53
    {
53
 
        if (m_setPath != curPath)
54
 
        {
55
 
            if (!m_setPath.isEmpty())
56
 
            {
57
 
                removePath(m_setPath);
58
 
            }
59
 
            m_setPath = curPath;
60
 
            addPath(m_setPath);
61
 
        }        
 
54
        clearPaths();
 
55
        m_setPaths.clear();
 
56
        m_setPaths.append(curPath);
 
57
        QFileSystemWatcher::addPath(curPath);
62
58
    }
63
59
    DEBUG_FSWATCHER();
64
60
}
65
61
 
66
62
 
 
63
void ExternalFSWatcher::setCurrentPaths(const QStringList &paths)
 
64
{
 
65
    QStringList myPaths(paths);
 
66
    ::qSort(myPaths);
 
67
    clearPaths();
 
68
    m_setPaths = myPaths;
 
69
    QFileSystemWatcher::addPaths(paths);
 
70
}
 
71
 
 
72
 
 
73
void ExternalFSWatcher::clearPaths()
 
74
{
 
75
    QStringList existentPaths = QFileSystemWatcher::directories();
 
76
    if (existentPaths.count() > 0)
 
77
    {
 
78
        QFileSystemWatcher::removePaths(existentPaths);
 
79
    }
 
80
}
 
81
 
 
82
 
 
83
/*!
 
84
 * \brief ExternalFSWatcher::slotDirChanged() schedules a Disk change to be notified
 
85
 *
 
86
 *  Once path that belongs to \a m_setPaths is modified in the Disk it becomes the \a m_changedPath and
 
87
 *  its change is scheculed to notified later. This path is taken out from QFileSystemWatcher to avoid
 
88
 *  lots of continuous notifications from QFileSystemWatcher when having hevy disk io.
 
89
 *
 
90
 * \param dir directory changed in the File System
 
91
 */
67
92
void ExternalFSWatcher::slotDirChanged(const QString &dir)
68
93
{
69
94
    DEBUG_FSWATCHER();
70
 
    if (    (m_setPath == dir)
71
 
         && ( m_waitingEmitCounter == 0 || m_setPath != m_changedPath )
72
 
       )
 
95
    int index = m_setPaths.indexOf(dir);
 
96
    if (index != -1  && (m_waitingEmitCounter == 0 || dir != m_changedPath))
73
97
    {
74
 
        removePath(m_setPath);
 
98
        m_lastChangedIndex = index;
 
99
        //changed path is taken from the QFileSystemWatcher and it becomes the current changed
 
100
        //in this case there will not be slotDirChanged() for this path until slotFireChanges()
 
101
        //restores the path in the QFileSystemWatcher
 
102
        removePath(m_setPaths.at(m_lastChangedIndex));
75
103
        ++m_waitingEmitCounter;
76
 
        m_changedPath = m_setPath;
 
104
        m_changedPath = dir;
77
105
        QTimer::singleShot(m_msWaitTime, this, SLOT(slotFireChanges()));       
78
106
    }
79
107
}
81
109
 
82
110
/*!
83
111
 * \brief ExternalFSWatcher::slotFireChanges() emits \ref pathModified() only when it is sure
84
 
 *  that the  current path was changed.
85
 
 *
86
 
 *  A change for the current path (the last current) MUST be notified at least once.
 
112
 *  that the LAST current path was changed.
 
113
 *
 
114
 *  The notification will be sent out only for the LAST modified path (if more than one) from the \a m_setPaths
 
115
 *
 
116
 *  \sa \ref ExternalFSWatcher class
87
117
 */
88
118
void ExternalFSWatcher::slotFireChanges()
89
119
{
90
 
   if (--m_waitingEmitCounter == 0)
91
 
   {
92
 
       addPath(m_setPath);
93
 
       if (m_setPath == m_changedPath)
94
 
       {
95
 
           emit pathModified();
 
120
   if (   --m_waitingEmitCounter == 0
 
121
       && m_lastChangedIndex != -1
 
122
       && m_lastChangedIndex < m_setPaths.count() )
 
123
   {            
 
124
       if (m_setPaths.at(m_lastChangedIndex) == m_changedPath)
 
125
       {          
 
126
           emit pathModified(m_changedPath);
96
127
#if DEBUG_EXT_FS_WATCHER
97
128
       DEBUG_FSWATCHER() << "emit pathModified()";
98
129
#endif
99
130
       }
 
131
       //restore the original list in QFileSystemWatcher
 
132
       clearPaths();
 
133
       QFileSystemWatcher::addPaths(m_setPaths);
100
134
   }  
101
135
}
102
136