Browse Source

MdTab: handle non-exist file

Le Tan 7 years ago
parent
commit
29c1c346a4
6 changed files with 37 additions and 11 deletions
  1. 1 1
      src/utils/vmetawordmanager.cpp
  2. 15 3
      src/vedittab.cpp
  3. 0 1
      src/vedittab.h
  4. 10 3
      src/vfile.cpp
  5. 1 2
      src/vfile.h
  6. 10 1
      src/vmdtab.cpp

+ 1 - 1
src/utils/vmetawordmanager.cpp

@@ -302,7 +302,7 @@ void VMetaWordManager::init()
                 });
 
     // %w%
-    addMetaWord(MetaWordType::Dynamic,
+    addMetaWord(MetaWordType::FunctionBased,
                 "w",
                 tr("the week number (`1` to `53`)"),
                 [](const VMetaWord *p_metaWord) {

+ 15 - 3
src/vedittab.cpp

@@ -187,9 +187,21 @@ void VEditTab::checkFileChangeOutside()
 
 void VEditTab::reloadFromDisk()
 {
-    m_file->reload();
-    m_fileDiverged = false;
-    m_checkFileChange = true;
+    bool ret = m_file->reload();
+    if (!ret) {
+        VUtils::showMessage(QMessageBox::Warning,
+                            tr("Warning"),
+                            tr("Fail to reload note <span style=\"%1\">%2</span>.")
+                              .arg(g_config->c_dataTextStyle).arg(m_file->getName()),
+                            tr("Please check if file %1 exists.").arg(m_file->fetchPath()),
+                            QMessageBox::Ok,
+                            QMessageBox::Ok,
+                            this);
+    }
+
+    m_fileDiverged = !ret;
+    m_checkFileChange = ret;
+
     reload();
 }
 

+ 0 - 1
src/vedittab.h

@@ -211,5 +211,4 @@ private slots:
     // Called when app focus changed.
     void handleFocusChanged(QWidget *p_old, QWidget *p_now);
 };
-
 #endif // VEDITTAB_H

+ 10 - 3
src/vfile.cpp

@@ -126,14 +126,21 @@ bool VFile::isChangedOutside(bool &p_missing) const
     return lm.toSecsSinceEpoch() != m_lastModified.toSecsSinceEpoch();
 }
 
-void VFile::reload()
+bool VFile::reload()
 {
-    Q_ASSERT(m_opened);
+    if (!m_opened) {
+        return false;
+    }
 
     QString filePath = fetchPath();
-    Q_ASSERT(QFileInfo::exists(filePath));
+    if (!QFileInfo::exists(filePath)) {
+        m_content.clear();
+        return false;
+    }
+
     m_content = VUtils::readFileFromDisk(filePath);
     m_lastModified = QFileInfo(filePath).lastModified();
+    return true;
 }
 
 QString VFile::backupFileOfPreviousSession() const

+ 1 - 2
src/vfile.h

@@ -33,7 +33,7 @@ public:
     virtual bool save();
 
     // Reload content from disk.
-    virtual void reload();
+    virtual bool reload();
 
     const QString &getName() const;
 
@@ -163,7 +163,6 @@ inline FileType VFile::getType() const
 
 inline const QString &VFile::getContent() const
 {
-    Q_ASSERT(m_opened);
     return m_content;
 }
 

+ 10 - 1
src/vmdtab.cpp

@@ -45,7 +45,16 @@ VMdTab::VMdTab(VFile *p_file, VEditArea *p_editArea,
 {
     V_ASSERT(m_file->getDocType() == DocType::Markdown);
 
-    m_file->open();
+    if (!m_file->open()) {
+        VUtils::showMessage(QMessageBox::Warning,
+                            tr("Warning"),
+                            tr("Fail to open note <span style=\"%1\">%2</span>.")
+                              .arg(g_config->c_dataTextStyle).arg(m_file->getName()),
+                            tr("Please check if file %1 exists.").arg(m_file->fetchPath()),
+                            QMessageBox::Ok,
+                            QMessageBox::Ok,
+                            this);
+    }
 
     HeadingSequenceType headingSequenceType = g_config->getHeadingSequenceType();
     if (headingSequenceType == HeadingSequenceType::Enabled) {