DocumentManager: Support expect/unexpect directory changes

Change-Id: I68b69fa00dd6cc189ecfcdb9dfad09c41531883f
Reviewed-by: David Schulz <david.schulz@digia.com>
This commit is contained in:
Orgad Shaneh
2013-06-08 23:39:21 +03:00
committed by Orgad Shaneh
parent a88c52ae0a
commit f6ec8c3342
2 changed files with 57 additions and 2 deletions

View File

@@ -52,6 +52,7 @@
#include <QFile> #include <QFile>
#include <QFileInfo> #include <QFileInfo>
#include <QFileSystemWatcher> #include <QFileSystemWatcher>
#include <QSet>
#include <QSettings> #include <QSettings>
#include <QTimer> #include <QTimer>
#include <QAction> #include <QAction>
@@ -156,6 +157,7 @@ struct DocumentManagerPrivate
QString m_projectsDirectory; QString m_projectsDirectory;
bool m_useProjectsDirectory; bool m_useProjectsDirectory;
QString m_buildDirectory; QString m_buildDirectory;
QSet<QString> m_expectedDirectories;
// When we are callling into a IDocument // When we are callling into a IDocument
// we don't want to receive a changed() // we don't want to receive a changed()
// signal // signal
@@ -547,6 +549,46 @@ void DocumentManager::unexpectFileChange(const QString &fileName)
updateExpectedState(fixedResolvedName); updateExpectedState(fixedResolvedName);
} }
static QString dirWithTrailingSlash(const QString &directory)
{
static const QChar slash(QLatin1Char('/'));
return directory.endsWith(slash) ? directory : directory + slash;
}
/*!
* Any subsequent change to any file inside \a directory is treated as
* an expected file change.
*
* \see DocumentManager::unexpectDirectoryChange(const QString &directory)
*/
void DocumentManager::expectDirectoryChange(const QString &directory)
{
QTC_ASSERT(!directory.isEmpty(), return);
d->m_expectedDirectories.insert(dirWithTrailingSlash(directory));
}
/*!
* Any subsequent change to any file inside \a directory is unexpected again.
*
* \see DocumentManager::expectDirectoryChange(const QString &directory)
*/
void DocumentManager::unexpectDirectoryChange(const QString &directory)
{
QTimer *timer = new QTimer;
timer->setProperty("directory", QString(dirWithTrailingSlash(directory)));
connect(timer, SIGNAL(timeout()), instance(), SLOT(clearExpectedDirectory()));
timer->setSingleShot(true);
timer->start(300);
}
void DocumentManager::clearExpectedDirectory()
{
if (QTimer *timer = qobject_cast<QTimer *>(sender())) {
d->m_expectedDirectories.remove(timer->property("directory").toString());
timer->deleteLater();
}
}
/*! /*!
Tries to save the files listed in \a documents. The \a cancelled argument is set to true Tries to save the files listed in \a documents. The \a cancelled argument is set to true
@@ -903,9 +945,18 @@ void DocumentManager::checkForReload()
continue; continue;
// was the change unexpected? // was the change unexpected?
if ((currentState.modified != expectedState.modified || currentState.permissions != expectedState.permissions) if ((currentState.modified != expectedState.modified
|| currentState.permissions != expectedState.permissions)
&& !expectedFileNames.contains(fileName)) { && !expectedFileNames.contains(fileName)) {
trigger = IDocument::TriggerExternal; bool expectedDir = false;
foreach (const QString &expectedDirectory, d->m_expectedDirectories) {
if (fileName.startsWith(expectedDirectory)) {
expectedDir = true;
break;
}
}
if (!expectedDir)
trigger = IDocument::TriggerExternal;
} }
// find out the type // find out the type

View File

@@ -74,6 +74,9 @@ public:
static void expectFileChange(const QString &fileName); static void expectFileChange(const QString &fileName);
static void unexpectFileChange(const QString &fileName); static void unexpectFileChange(const QString &fileName);
static void expectDirectoryChange(const QString &directory);
static void unexpectDirectoryChange(const QString &directory);
// recent files // recent files
static void addToRecentFiles(const QString &fileName, const Id &editorId = Id()); static void addToRecentFiles(const QString &fileName, const Id &editorId = Id());
Q_SLOT void clearRecentFiles(); Q_SLOT void clearRecentFiles();
@@ -150,6 +153,7 @@ private slots:
void changedFile(const QString &file); void changedFile(const QString &file);
void mainWindowActivated(); void mainWindowActivated();
void syncWithEditor(const QList<Core::IContext *> &context); void syncWithEditor(const QList<Core::IContext *> &context);
void clearExpectedDirectory();
}; };
/*! The FileChangeBlocker blocks all change notifications to all IDocument * that /*! The FileChangeBlocker blocks all change notifications to all IDocument * that