check if node exists on activated

This commit is contained in:
Le Tan 2021-06-22 21:15:23 +08:00
parent af85dc5aed
commit 36dd070d05
8 changed files with 43 additions and 5 deletions

View File

@ -59,6 +59,13 @@ void BufferMgr::open(Node *p_node, const QSharedPointer<FileOpenParameters> &p_p
return;
}
if (!p_node->checkExists()) {
auto msg = QString("Failed to open node that does not exist (%1)").arg(p_node->fetchAbsolutePath());
qWarning() << msg;
VNoteX::getInst().showStatusMessageShort(msg);
return;
}
auto buffer = findBuffer(p_node);
if (!buffer) {
auto nodePath = p_node->fetchAbsolutePath();
@ -91,7 +98,9 @@ void BufferMgr::open(const QString &p_filePath, const QSharedPointer<FileOpenPar
QFileInfo finfo(p_filePath);
if (!finfo.exists()) {
qWarning() << QString("failed to open file %1 that does not exist").arg(p_filePath);
auto msg = QString("Failed to open file that does not exist (%1)").arg(p_filePath);
qWarning() << msg;
VNoteX::getInst().showStatusMessageShort(msg);
return;
}

View File

@ -403,3 +403,13 @@ void Node::setExists(bool p_exists)
m_flags &= ~Flag::Exists;
}
}
bool Node::checkExists()
{
bool before = exists();
bool after = getConfigMgr()->checkNodeExists(this);
if (before != after) {
emit m_notebook->nodeUpdated(this);
}
return after;
}

View File

@ -89,9 +89,11 @@ namespace vnotex
bool hasContent() const;
// Whether the node exists on disk.
// Whether the node exists on disk (without real check).
bool exists() const;
bool checkExists();
void setExists(bool p_exists);
Node::Flags getFlags() const;

View File

@ -78,6 +78,8 @@ namespace vnotex
virtual QVector<QSharedPointer<ExternalNode>> fetchExternalChildren(Node *p_node) const = 0;
virtual bool checkNodeExists(Node *p_node) = 0;
protected:
// Version of the config processing code.
virtual QString getCodeVersion() const;

View File

@ -989,3 +989,10 @@ bool VXNotebookConfigMgr::isExcludedFromExternalNode(const QString &p_name) cons
}
return false;
}
bool VXNotebookConfigMgr::checkNodeExists(Node *p_node)
{
bool exists = getBackend()->exists(p_node->fetchPath());
p_node->setExists(exists);
return exists;
}

View File

@ -72,6 +72,8 @@ namespace vnotex
QVector<QSharedPointer<ExternalNode>> fetchExternalChildren(Node *p_node) const Q_DECL_OVERRIDE;
bool checkNodeExists(Node *p_node) Q_DECL_OVERRIDE;
private:
// Config of a file child.
struct NodeFileConfig

View File

@ -1906,13 +1906,19 @@ void NotebookNodeExplorer::importToIndex(const QVector<ExternalNode *> &p_nodes)
}
}
bool NotebookNodeExplorer::checkInvalidNode(const Node *p_node) const
bool NotebookNodeExplorer::checkInvalidNode(Node *p_node) const
{
if (!p_node) {
return true;
}
if (!p_node->exists()) {
bool nodeExists = p_node->exists();
if (nodeExists) {
p_node->checkExists();
nodeExists = p_node->exists();
}
if (!nodeExists) {
MessageBoxHelper::notify(MessageBoxHelper::Warning,
tr("Invalid node (%1).").arg(p_node->getName()),
tr("Please check if the node exists on the disk."),

View File

@ -259,7 +259,7 @@ namespace vnotex
// Check whether @p_node is a valid node. Will notify user.
// Return true if it is invalid.
bool checkInvalidNode(const Node *p_node) const;
bool checkInvalidNode(Node *p_node) const;
void expandCurrentNodeAll();