Commit e2a06db3 authored by dt's avatar dt

Make FileWatcher more robust, fix a few bugs in that code.

Also cache a lastModified time, so that we don't emit a signal if it
didn't change.
parent 77959c8b
......@@ -304,7 +304,6 @@ bool MainWindow::init(QString *errorMessage)
OutputPanePlaceHolder *oph = new OutputPanePlaceHolder(m_outputMode);
oph->setCloseable(false);
outputModeWidget->layout()->addWidget(oph);
oph->setVisible(true);
outputModeWidget->layout()->addWidget(new Core::FindToolBarPlaceHolder(m_outputMode));
outputModeWidget->setFocusProxy(oph);
......
......@@ -55,8 +55,10 @@ FileWatcher::FileWatcher(QObject *parent) :
FileWatcher::~FileWatcher()
{
foreach (const QString &file, m_files)
QStringList keys = m_files.keys();
foreach(const QString &file, keys)
removeFile(file);
if (--m_objectCount == 0) {
delete m_watcher;
m_watcher = 0;
......@@ -65,29 +67,41 @@ FileWatcher::~FileWatcher()
void FileWatcher::slotFileChanged(const QString &file)
{
if (m_files.contains(file))
emit fileChanged(file);
}
QStringList FileWatcher::files()
{
return m_files;
if (m_files.contains(file)) {
QDateTime lastModified = QFileInfo(file).lastModified();
if (lastModified != m_files.value(file)) {
m_files[file] = lastModified;
qDebug() << "slotFileChanged" << file << lastModified;
emit fileChanged(file);
} else {
qDebug() << "WTF:" << file<<lastModified.toString();
}
}
}
void FileWatcher::addFile(const QString &file)
{
if (m_files.contains(file))
return;
m_files += file;
if (m_fileCount[file] == 0)
const int count = ++m_fileCount[file];
Q_ASSERT(count > 0);
m_files.insert(file, QDateTime());
if (count == 1)
m_watcher->addPath(file);
m_fileCount[file] += 1;
}
void FileWatcher::removeFile(const QString &file)
{
m_files.removeOne(file);
m_fileCount[file] -= 1;
if (m_fileCount[file] == 0)
m_watcher->removePath(file);
if (!m_files.contains(file))
return;
const int count = --m_fileCount[file];
Q_ASSERT(count >= 0);
m_files.remove(file);
if (!count) {
m_watcher->removePath(file);
m_fileCount.remove(file);
}
}
......@@ -36,6 +36,8 @@
#include <QtCore/QHash>
#include <QtCore/QObject>
#include <QtCore/QStringList>
#include <QtCore/QDateTime>
#include <QtCore/QMap>
QT_BEGIN_NAMESPACE
class QTimer;
......@@ -52,7 +54,6 @@ public:
explicit FileWatcher(QObject *parent = 0);
virtual ~FileWatcher();
QStringList files();
void addFile(const QString &file);
void removeFile(const QString &file);
signals:
......@@ -66,7 +67,7 @@ private:
static int m_objectCount;
static QHash<QString, int> m_fileCount;
static QFileSystemWatcher *m_watcher;
QStringList m_files;
QMap<QString, QDateTime> m_files;
};
} // namespace ProjectExplorer
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment