Prechádzať zdrojové kódy

check if node exists on activated

Le Tan 4 rokov pred
rodič
commit
36dd070d05

+ 10 - 1
src/core/buffermgr.cpp

@@ -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;
     }
 

+ 10 - 0
src/core/notebook/node.cpp

@@ -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;
+}

+ 3 - 1
src/core/notebook/node.h

@@ -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;

+ 2 - 0
src/core/notebookconfigmgr/inotebookconfigmgr.h

@@ -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;

+ 7 - 0
src/core/notebookconfigmgr/vxnotebookconfigmgr.cpp

@@ -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;
+}

+ 2 - 0
src/core/notebookconfigmgr/vxnotebookconfigmgr.h

@@ -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

+ 8 - 2
src/widgets/notebooknodeexplorer.cpp

@@ -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."),

+ 1 - 1
src/widgets/notebooknodeexplorer.h

@@ -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();