Commit 300047ef authored by Christoph Cullmann's avatar Christoph Cullmann 🐮
Browse files

simplify file name handling

m_fileName is now constant
that allows to avoid some checks
it will make it easier to determine if we were a auto-generated
or real project, too
parent aeae1f30
......@@ -25,58 +25,50 @@
#include <QPlainTextDocumentLayout>
#include <utility>
KateProject::KateProject(QThreadPool &threadPool, KateProjectPlugin *plugin)
: m_notesDocument(nullptr)
, m_untrackedDocumentsRoot(nullptr)
, m_threadPool(threadPool)
KateProject::KateProject(QThreadPool &threadPool, KateProjectPlugin *plugin, const QString &fileName)
: m_threadPool(threadPool)
, m_plugin(plugin)
, m_fileName(QFileInfo(fileName).canonicalFilePath())
, m_baseDir(QFileInfo(fileName).canonicalPath())
{
// if canonicalFilePath already returned empty string, no need to try to load this
if (m_fileName.isEmpty()) {
return;
}
// ensure we get notified for project file changes
connect(&m_plugin->fileWatcher(), &QFileSystemWatcher::fileChanged, this, &KateProject::slotFileChanged);
m_plugin->fileWatcher().addPath(m_fileName);
// try to load the project map from our file, will start worker thread, too
reload();
}
KateProject::~KateProject()
KateProject::KateProject(QThreadPool &threadPool, KateProjectPlugin *plugin, const QVariantMap &globalProject, const QString &directory)
: m_threadPool(threadPool)
, m_plugin(plugin)
, m_baseDir(directory)
, m_globalProject(globalProject)
{
saveNotesDocument();
// stop watching if we have some real project file
if (!m_fileName.isEmpty()) {
m_plugin->fileWatcher().removePath(m_fileName);
}
// try to load the project map, will start worker thread, too
load(globalProject);
}
bool KateProject::loadFromFile(const QString &fileName)
KateProject::~KateProject()
{
saveNotesDocument();
// stop watching if we have some real project file
if (!m_fileName.isEmpty()) {
m_plugin->fileWatcher().removePath(m_fileName);
}
/**
* set new filename and base directory
*/
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
*/
return reload();
}
bool KateProject::reload(bool force)
{
const QVariantMap map = readProjectFile();
if (map.isEmpty()) {
m_fileLastModified = QDateTime();
} else {
m_fileLastModified = QFileInfo(m_fileName).lastModified();
if (!map.isEmpty()) {
m_globalProject = map;
}
......@@ -180,19 +172,6 @@ QVariantMap KateProject::readProjectFile() const
return project.toVariant().toMap();
}
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_globalProject = globalProject;
return load(globalProject);
}
bool KateProject::load(const QVariantMap &globalProject, bool force)
{
/**
......
......@@ -10,8 +10,9 @@
#include "kateprojectindex.h"
#include "kateprojectitem.h"
#include <KTextEditor/ModificationInterface>
#include <QDateTime>
#include <QHash>
#include <QSharedPointer>
#include <QTextDocument>
......@@ -42,9 +43,25 @@ class KateProject : public QObject
public:
/**
* construct empty project
* Construct project by reading from given file.
* Success can be checked later by using isValid().
*
* @param threadPool thread pool to be used by worker threads
* @param plugin our plugin instance, for config & file system watcher
* @param fileName fileName to load the project from
*/
KateProject(QThreadPool &threadPool, KateProjectPlugin *plugin, const QString &fileName);
/**
* Construct project from given data for given base directory
* Success can be checked later by using isValid().
*
* @param threadPool thread pool to be used by worker threads
* @param plugin our plugin instance, for config & file system watcher
* @param globalProject globalProject object content
* @param directory project base directory
*/
KateProject(QThreadPool &threadPool, KateProjectPlugin *plugin);
KateProject(QThreadPool &threadPool, KateProjectPlugin *plugin, const QVariantMap &globalProject, const QString &directory);
/**
* deconstruct project
......@@ -52,13 +69,13 @@ public:
~KateProject() override;
/**
* Load a project from project file
* Only works once, afterwards use reload().
* @param fileName name of project file
* @return success
* Is this project valid?
* @return project valid? we are valid, if we have some name set
*/
bool loadFromFile(const QString &fileName);
bool loadFromData(const QVariantMap &globalProject, const QString &directory);
bool isValid() const
{
return !name().isEmpty();
}
/**
* Try to reload a project.
......@@ -87,15 +104,6 @@ public:
return m_baseDir;
}
/**
* Return the time when the project file has been modified last.
* @return QFileInfo::lastModified()
*/
QDateTime fileLastModified() const
{
return m_fileLastModified;
}
/**
* Accessor to project map containing the whole project info.
* @return project info
......@@ -111,7 +119,6 @@ public:
*/
QString name() const
{
// MSVC doesn't support QStringLiteral here
return m_projectMap[QStringLiteral("name")].toString();
}
......@@ -270,14 +277,19 @@ private:
private:
/**
* Last modification time of the project file
* thread pool used for project worker
*/
QThreadPool &m_threadPool;
/**
* Project plugin (configuration)
*/
QDateTime m_fileLastModified;
KateProjectPlugin * const m_plugin;
/**
* project file name
* project file name, will stay constant
*/
QString m_fileName;
const QString m_fileName;
/**
* base directory of the project
......@@ -312,7 +324,7 @@ private:
/**
* notes buffer for project local notes
*/
QTextDocument *m_notesDocument;
QTextDocument *m_notesDocument = nullptr;
/**
* Set of existing documents for this project.
......@@ -322,22 +334,12 @@ private:
/**
* Parent item for existing documents that are not in the project tree
*/
QStandardItem *m_untrackedDocumentsRoot;
/**
* thread pool used for project worker
*/
QThreadPool &m_threadPool;
QStandardItem *m_untrackedDocumentsRoot = nullptr;
/**
* project configuration (read from file or injected)
*/
QVariantMap m_globalProject;
/**
* Project plugin (configuration)
*/
KateProjectPlugin *m_plugin;
};
#endif
......@@ -145,8 +145,8 @@ KTextEditor::ConfigPage *KateProjectPlugin::configPage(int number, QWidget *pare
KateProject *KateProjectPlugin::createProjectForFileName(const QString &fileName)
{
KateProject *project = new KateProject(m_threadPool, this);
if (!project->loadFromFile(fileName)) {
KateProject *project = new KateProject(m_threadPool, this, fileName);
if (!project->isValid()) {
delete project;
return nullptr;
}
......@@ -346,8 +346,7 @@ KateProject *KateProjectPlugin::createProjectForRepository(const QString &type,
cnf[QStringLiteral("name")] = dir.dirName();
cnf[QStringLiteral("files")] = (QVariantList() << files);
KateProject *project = new KateProject(m_threadPool, this);
project->loadFromData(cnf, dir.canonicalPath());
KateProject *project = new KateProject(m_threadPool, this, cnf, dir.canonicalPath());
m_projects.append(project);
......@@ -362,8 +361,7 @@ KateProject *KateProjectPlugin::createProjectForDirectory(const QDir &dir)
cnf[QStringLiteral("name")] = dir.dirName();
cnf[QStringLiteral("files")] = (QVariantList() << files);
KateProject *project = new KateProject(m_threadPool, this);
project->loadFromData(cnf, dir.canonicalPath());
KateProject *project = new KateProject(m_threadPool, this, cnf, dir.canonicalPath());
m_projects.append(project);
......
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