MdTab: handle non-exist file

This commit is contained in:
Le Tan 2018-10-22 12:08:34 +08:00
parent 8d42ffea0f
commit 29c1c346a4
6 changed files with 37 additions and 11 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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