diff --git a/src/KJotsMain.h b/src/KJotsMain.h --- a/src/KJotsMain.h +++ b/src/KJotsMain.h @@ -36,9 +36,6 @@ public: explicit KJotsMain(QWidget *parent = nullptr); -public Q_SLOTS: - void onQuit(); - protected: bool queryClose() override; diff --git a/src/KJotsMain.cpp b/src/KJotsMain.cpp --- a/src/KJotsMain.cpp +++ b/src/KJotsMain.cpp @@ -44,7 +44,7 @@ : KXmlGuiWindow(parent) , component(new KJotsWidget(this, this)) { - KStandardAction::quit(this, &KJotsMain::onQuit, actionCollection()); + KStandardAction::quit(this, &KJotsMain::close, actionCollection()); setCentralWidget(component); @@ -56,11 +56,3 @@ { return component->queryClose(); } - -void KJotsMain::onQuit() -{ - component->queryClose(); - deleteLater(); - qApp->quit(); -} - diff --git a/src/kjotsmodel.h b/src/kjotsmodel.h --- a/src/kjotsmodel.h +++ b/src/kjotsmodel.h @@ -95,6 +95,10 @@ bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override; static QModelIndex modelIndexForUrl(const QAbstractItemModel *model, const QUrl &url); + /** + * Returns an Item for @p index, and sets its content by @p document + */ + static Item updateItem(const QModelIndex &index, QTextDocument *document); /** * A helper function which returns a full "path" to the @p item (e.g. "Resource / Notebook / Note") * using @p sep as a separator. If multiple items are selected, returns "Multiple selection" diff --git a/src/kjotsmodel.cpp b/src/kjotsmodel.cpp --- a/src/kjotsmodel.cpp +++ b/src/kjotsmodel.cpp @@ -190,21 +190,7 @@ } if (role == KJotsModel::DocumentRole) { - Item item = EntityTreeModel::data(index, ItemRole).value(); - if (!item.hasPayload()) { - return false; - } - auto *document = value.value(); - - NoteUtils::NoteMessageWrapper note(item.payload()); - bool isRichText = KPIMTextEdit::TextUtils::containsFormatting(document); - if (isRichText) { - note.setText( document->toHtml(), Qt::RichText ); - } else { - note.setText( document->toPlainText(), Qt::PlainText ); - } - note.setLastModifiedDate(QDateTime::currentDateTime()); - item.setPayload(note.message()); + Item item = updateItem(index, value.value()); return EntityTreeModel::setData(index, QVariant::fromValue(item), ItemRole); } @@ -286,6 +272,24 @@ return {}; } +Item KJotsModel::updateItem(const QModelIndex &index, QTextDocument *document) +{ + Item item = index.data(ItemRole).value(); + if (!item.hasPayload()) { + return Item(); + } + NoteUtils::NoteMessageWrapper note(item.payload()); + bool isRichText = KPIMTextEdit::TextUtils::containsFormatting(document); + if (isRichText) { + note.setText( document->toHtml(), Qt::RichText ); + } else { + note.setText( document->toPlainText(), Qt::PlainText ); + } + note.setLastModifiedDate(QDateTime::currentDateTime()); + item.setPayload(note.message()); + return item; +} + QString KJotsModel::itemPath(const QModelIndex &index, const QString &sep) { QString caption; diff --git a/src/kjotswidget.cpp b/src/kjotswidget.cpp --- a/src/kjotswidget.cpp +++ b/src/kjotswidget.cpp @@ -52,6 +52,7 @@ #include #include #include +#include #include #include #include @@ -1226,11 +1227,42 @@ bool KJotsWidget::queryClose() { + // Saving the current note + // We cannot use async interface (i.e. ETM) here + // because we need to abort the close if something went wrong + if ((selProxy->rowCount() == 1) && (editor->document()->isModified())) { + QModelIndex idx = selProxy->mapToSource(selProxy->index(0, 0, QModelIndex())); + auto job = new ItemModifyJob(KJotsModel::updateItem(idx, editor->document())); + if (!job->exec()) { + int res = KMessageBox::warningContinueCancelDetailed(this, + i18n("Unable to save the note.\n" + "You can save your note to a local file using the \"File - Export\" menu,\n" + "otherwise you will lose your changes!\n" + "Do you want to close anyways?"), + i18n("Close Document"), + KStandardGuiItem::quit(), + KStandardGuiItem::cancel(), + QString(), + KMessageBox::Notify, + i18n("Error message:\n" + "%1", job->errorString())); + if (res == KMessageBox::Cancel) { + return false; + } + } else { + // Saved successfully. + // However, KJotsEdit will still catch focusOutEvent and try saving using async interface + // (application will quit soon, so it doesn't really make much sense doing it) + // Marking the document as saved explicitly it order to avoid it + editor->document()->setModified(false); + } + + } + KJotsSettings::setSplitterSizes(m_splitter->sizes()); KJotsSettings::self()->save(); m_orderProxy->saveOrder(); - // TODO: we better wait for a result here! - editor->savePage(); + return true; }