Commit aeae1f30 authored by Christoph Cullmann's avatar Christoph Cullmann 🐮
Browse files

handle project file watching inside KateProject

parent 8573b124
......@@ -31,27 +31,38 @@ KateProject::KateProject(QThreadPool &threadPool, KateProjectPlugin *plugin)
, m_threadPool(threadPool)
, m_plugin(plugin)
{
// ensure we get notified for project file changes
connect(&m_plugin->fileWatcher(), &QFileSystemWatcher::fileChanged, this, &KateProject::slotFileChanged);
}
KateProject::~KateProject()
{
saveNotesDocument();
// stop watching if we have some real project file
if (!m_fileName.isEmpty()) {
m_plugin->fileWatcher().removePath(m_fileName);
}
}
bool KateProject::loadFromFile(const QString &fileName)
{
/**
* bail out if already fileName set!
*/
// stop watching if we have some real project file
if (!m_fileName.isEmpty()) {
return false;
m_plugin->fileWatcher().removePath(m_fileName);
}
/**
* set new filename and base directory
*/
m_fileName = fileName;
m_baseDir = QFileInfo(m_fileName).canonicalPath();
const QFileInfo fi(fileName);
m_fileName = fi.canonicalFilePath();
m_baseDir = fi.canonicalPath();
// start watching if we have some real project file
if (!m_fileName.isEmpty()) {
m_plugin->fileWatcher().addPath(m_fileName);
}
/**
* trigger reload
......@@ -61,8 +72,7 @@ bool KateProject::loadFromFile(const QString &fileName)
bool KateProject::reload(bool force)
{
QVariantMap map = readProjectFile();
const QVariantMap map = readProjectFile();
if (map.isEmpty()) {
m_fileLastModified = QDateTime();
} else {
......@@ -101,8 +111,11 @@ void KateProject::removeFile(const QString &file)
*/
QJsonDocument KateProject::readJSONFile(const QString &fileName)
{
QFile file(fileName);
if (fileName.isEmpty()) {
return QJsonDocument();
}
QFile file(fileName);
if (!file.exists() || !file.open(QFile::ReadOnly)) {
return QJsonDocument();
}
......@@ -123,8 +136,8 @@ QJsonDocument KateProject::readJSONFile(const QString &fileName)
QVariantMap KateProject::readProjectFile() const
{
QJsonDocument project(readJSONFile(m_fileName));
// bail out on error
QJsonDocument project(readJSONFile(m_fileName));
if (project.isNull()) {
return QVariantMap();
}
......@@ -169,8 +182,13 @@ QVariantMap KateProject::readProjectFile() const
bool KateProject::loadFromData(const QVariantMap &globalProject, const QString &directory)
{
// stop watching if we have some real project file
if (!m_fileName.isEmpty()) {
m_plugin->fileWatcher().removePath(m_fileName);
}
m_fileName.clear();
m_baseDir = directory;
m_fileName = QDir(directory).filePath(QStringLiteral(".kateproject"));
m_globalProject = globalProject;
return load(globalProject);
}
......@@ -491,3 +509,10 @@ void KateProject::unregisterUntrackedItem(const KateProjectItem *item)
m_untrackedDocumentsRoot = nullptr;
}
}
void KateProject::slotFileChanged(const QString &file)
{
if (file == m_fileName) {
reload();
}
}
......@@ -70,6 +70,7 @@ public:
/**
* Accessor to file name.
* Non-empty if really a project from some disk file, else empty e.g. for auto generated projects from VCS.
* @return file name
*/
const QString &fileName() const
......@@ -231,6 +232,12 @@ private Q_SLOTS:
void slotModifiedOnDisk(KTextEditor::Document *document, bool isModified, KTextEditor::ModificationInterface::ModifiedOnDiskReason reason);
/**
* did some project file change?
* @param file name of file that did change
*/
void slotFileChanged(const QString &file);
Q_SIGNALS:
/**
* Emitted on project map changes.
......
......@@ -67,7 +67,6 @@ KateProjectPlugin::KateProjectPlugin(QObject *parent, const QList<QVariant> &)
qRegisterMetaType<KateProjectSharedProjectIndex>("KateProjectSharedProjectIndex");
connect(KTextEditor::Editor::instance()->application(), &KTextEditor::Application::documentCreated, this, &KateProjectPlugin::slotDocumentCreated);
connect(&m_fileWatcher, &QFileSystemWatcher::directoryChanged, this, &KateProjectPlugin::slotDirectoryChanged);
// read configuration prior to cwd project setup below
readConfig();
......@@ -121,7 +120,6 @@ KateProjectPlugin::~KateProjectPlugin()
unregisterVariables();
for (KateProject *project : qAsConst(m_projects)) {
m_fileWatcher.removePath(QFileInfo(project->fileName()).canonicalPath());
delete project;
}
m_projects.clear();
......@@ -154,7 +152,6 @@ KateProject *KateProjectPlugin::createProjectForFileName(const QString &fileName
}
m_projects.append(project);
m_fileWatcher.addPath(QFileInfo(fileName).canonicalPath());
Q_EMIT projectCreated(project);
return project;
}
......@@ -249,7 +246,6 @@ bool KateProjectPlugin::closeProject(KateProject *project)
Q_EMIT pluginViewProjectClosing(project);
if (m_projects.removeOne(project)) {
m_fileWatcher.removePath(QFileInfo(project->fileName()).canonicalPath());
delete project;
return true;
}
......@@ -306,20 +302,6 @@ void KateProjectPlugin::slotDocumentUrlChanged(KTextEditor::Document *document)
}
}
void KateProjectPlugin::slotDirectoryChanged(const QString &path)
{
QString fileName = QDir(path).filePath(ProjectFileName);
for (KateProject *project : qAsConst(m_projects)) {
if (project->fileName() == fileName) {
QDateTime lastModified = QFileInfo(fileName).lastModified();
if (project->fileLastModified().isNull() || (lastModified > project->fileLastModified())) {
project->reload();
}
break;
}
}
}
KateProject *KateProjectPlugin::detectGit(const QDir &dir)
{
// allow .git as dir and file (file for git worktree stuff, https://git-scm.com/docs/git-worktree)
......
......@@ -128,6 +128,15 @@ public:
void setDoubleClickAction(ClickAction cb);
ClickAction doubleClickAcion();
/**
* filesystem watcher to keep track of all project files
* and auto-reload
*/
QFileSystemWatcher &fileWatcher()
{
return m_fileWatcher;
}
Q_SIGNALS:
/**
......@@ -172,12 +181,6 @@ public Q_SLOTS:
*/
void slotDocumentUrlChanged(KTextEditor::Document *document);
/**
* did some project file change?
* @param path name of directory that did change
*/
void slotDirectoryChanged(const QString &path);
private:
KateProject *createProjectForRepository(const QString &type, const QDir &dir);
KateProject *createProjectForDirectory(const QDir &dir);
......
Supports Markdown
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