diff --git a/src/akonadi_next/notecreatorandselector.cpp b/src/akonadi_next/notecreatorandselector.cpp --- a/src/akonadi_next/notecreatorandselector.cpp +++ b/src/akonadi_next/notecreatorandselector.cpp @@ -84,27 +84,16 @@ Item newItem; newItem.setMimeType(Akonadi::NoteUtils::noteMimeType()); - KMime::Message::Ptr newPage = KMime::Message::Ptr(new KMime::Message()); - - QString title = i18nc("The default name for new pages.", "New Page"); - QByteArray encoding("utf-8"); - - newPage->subject(true)->fromUnicodeString(title, encoding); - newPage->contentType(true)->setMimeType("text/plain"); - newPage->contentType()->setCharset("utf-8"); - newPage->contentTransferEncoding(true)->setEncoding(KMime::Headers::CEquPr); - newPage->date(true)->setDateTime(QDateTime::currentDateTime()); - newPage->from(true)->fromUnicodeString(QString::fromLatin1("Kjots@kde4"), encoding); + Akonadi::NoteUtils::NoteMessageWrapper note(KMime::Message::Ptr(new KMime::Message)); + note.setFrom(QStringLiteral("KJots@KDE5")); + note.setTitle(i18nc("The default name for new pages.", "New Page")); + note.setCreationDate(QDateTime::currentDateTime()); + note.setLastModifiedDate(QDateTime::currentDateTime()); // Need a non-empty body part so that the serializer regards this as a valid message. - newPage->mainBodyPart()->fromUnicodeString(QString::fromLatin1(" ")); - - newPage->assemble(); - - newItem.setPayload(newPage); + note.setText(QStringLiteral(" ")); - Akonadi::EntityDisplayAttribute *eda = new Akonadi::EntityDisplayAttribute(); - eda->setIconName(QStringLiteral("text-plain")); - newItem.addAttribute(eda); + newItem.setPayload(note.message()); + newItem.attribute(Akonadi::Item::AddIfMissing)->setIconName(QStringLiteral("text-plain")); Akonadi::ItemCreateJob *job = new Akonadi::ItemCreateJob(newItem, Collection(m_containerCollectionId), this); connect(job, &Akonadi::ItemCreateJob::result, this, &NoteCreatorAndSelector::noteCreationFinished); diff --git a/src/kjotsmodel.h b/src/kjotsmodel.h --- a/src/kjotsmodel.h +++ b/src/kjotsmodel.h @@ -83,8 +83,7 @@ enum KJotsRoles { GrantleeObjectRole = EntityTreeModel::UserRole, - DocumentRole, - DocumentCursorPositionRole + DocumentRole }; // We don't reimplement the Collection overload. @@ -99,8 +98,6 @@ private: QHash m_colors; mutable QHash m_documents; - QHash m_cursorPositions; - }; #endif diff --git a/src/kjotsmodel.cpp b/src/kjotsmodel.cpp --- a/src/kjotsmodel.cpp +++ b/src/kjotsmodel.cpp @@ -179,42 +179,35 @@ } return EntityTreeModel::setData(index, QVariant::fromValue(col), CollectionRole); } - KMime::Message::Ptr m = item.payload(); - - m->subject(true)->fromUnicodeString(value.toString(), "utf-8"); - m->assemble(); - item.setPayload(m); + NoteUtils::NoteMessageWrapper note(item.payload()); + note.setTitle(value.toString()); + note.setLastModifiedDate(QDateTime::currentDateTime()); + item.setPayload(note.message()); if (item.hasAttribute()) { EntityDisplayAttribute *displayAttribute = item.attribute(); displayAttribute->setDisplayName(value.toString()); } - return EntityTreeModel::setData(index, QVariant::fromValue(item), ItemRole); + return EntityTreeModel::setData(index, QVariant::fromValue(item), ItemRole); } if (role == KJotsModel::DocumentRole) { Item item = EntityTreeModel::data(index, ItemRole).value(); if (!item.hasPayload()) { return false; } - KMime::Message::Ptr note = item.payload(); QTextDocument *document = value.value(); + NoteUtils::NoteMessageWrapper note(item.payload()); bool isRichText = KPIMTextEdit::TextUtils::containsFormatting(document); - - note->contentType()->setMimeType(isRichText ? "text/html" : "text/plain"); - note->contentType()->setCharset("utf-8"); - note->contentTransferEncoding(true)->setEncoding(KMime::Headers::CEquPr); - note->mainBodyPart()->fromUnicodeString(isRichText ? document->toHtml() : document->toPlainText()); - note->assemble(); - item.setPayload(note); - return EntityTreeModel::setData(index, QVariant::fromValue(item), ItemRole); - } - - if (role == KJotsModel::DocumentCursorPositionRole) { - Item item = index.data(ItemRole).value(); - m_cursorPositions.insert(item.id(), value.toInt()); - return true; + if (isRichText) { + note.setText( document->toHtml(), Qt::RichText ); + } else { + note.setText( document->toPlainText(), Qt::PlainText ); + } + note.setLastModifiedDate(QDateTime::currentDateTime()); + item.setPayload(note.message()); + return EntityTreeModel::setData(index, QVariant::fromValue(item), ItemRole); } return EntityTreeModel::setData(index, value, role); @@ -237,34 +230,22 @@ return QVariant(); } - KMime::Message::Ptr note = item.payload(); - QTextDocument *document = new QTextDocument; - const QString decodedText = note->mainBodyPart()->decodedText(); - if (note->contentType()->isHTMLText() - || decodedText.startsWith(QLatin1String(""))) { - document->setHtml(decodedText); + NoteUtils::NoteMessageWrapper note(item.payload()); + const QString doc = note.text(); + auto document = new QTextDocument(); + if (note.textFormat() == Qt::RichText + || doc.startsWith(u"")) + { + document->setHtml(doc); } else { - document->setPlainText(decodedText); + document->setPlainText(doc); } m_documents.insert(itemId, document); return QVariant::fromValue(document); } - if (role == KJotsModel::DocumentCursorPositionRole) { - const Item item = index.data(ItemRole).value(); - if (!item.isValid()) { - return 0; - } - - if (m_cursorPositions.contains(item.id())) { - return m_cursorPositions.value(item.id()); - } - - return 0; - } - if (role == Qt::DecorationRole) { const Item item = index.data(ItemRole).value(); if (item.isValid() && item.hasAttribute()) { @@ -283,8 +264,8 @@ QVariant KJotsModel::entityData(const Akonadi::Item &item, int column, int role) const { if ((role == Qt::EditRole || role == Qt::DisplayRole) && item.hasPayload()) { - KMime::Message::Ptr page = item.payload(); - return page->subject()->asUnicodeString(); + NoteUtils::NoteMessageWrapper note(item.payload()); + return note.title(); } return EntityTreeModel::entityData(item, column, role); } diff --git a/src/localresourcecreator.h b/src/localresourcecreator.h --- a/src/localresourcecreator.h +++ b/src/localresourcecreator.h @@ -44,7 +44,6 @@ void rootFetchFinished(KJob *job); void topLevelFetchFinished(KJob *job); void createFinished(KJob *job); - void itemCreateFinished(KJob *job); }; diff --git a/src/localresourcecreator.cpp b/src/localresourcecreator.cpp --- a/src/localresourcecreator.cpp +++ b/src/localresourcecreator.cpp @@ -141,34 +141,23 @@ item.setParentCollection(collectionCreateJob->collection()); item.setMimeType(Akonadi::NoteUtils::noteMimeType()); - KMime::Message::Ptr note(new KMime::Message()); - - QString title = i18nc("The default name for new pages.", "New Page"); - QByteArray encoding("utf-8"); - - note->subject(true)->fromUnicodeString(title, encoding); - note->contentType(true)->setMimeType("text/plain"); - note->date(true)->setDateTime(QDateTime::currentDateTime()); - note->from(true)->fromUnicodeString(QLatin1String("Kjots@kde4"), encoding); + Akonadi::NoteUtils::NoteMessageWrapper note(KMime::Message::Ptr(new KMime::Message)); + note.setFrom(QStringLiteral("KJots@KDE5")); + note.setTitle(i18nc("The default name for new pages.", "New Page")); + note.setCreationDate(QDateTime::currentDateTime()); + note.setLastModifiedDate(QDateTime::currentDateTime()); // Need a non-empty body part so that the serializer regards this as a valid message. - note->mainBodyPart()->fromUnicodeString(QLatin1String(" ")); - - note->assemble(); - - item.setPayload(note); - Akonadi::EntityDisplayAttribute *eda = new Akonadi::EntityDisplayAttribute(); - eda->setIconName(QLatin1String("text-plain")); - item.addAttribute(eda); - - Akonadi::ItemCreateJob *itemCreateJob = new Akonadi::ItemCreateJob(item, collectionCreateJob->collection(), this); - connect(itemCreateJob, &Akonadi::ItemCreateJob::result, this, &LocalResourceCreator::itemCreateFinished); -} - -void LocalResourceCreator::itemCreateFinished(KJob *job) -{ - if (job->error()) { - qWarning() << job->errorString(); - } - deleteLater(); + note.setText(QStringLiteral(" ")); + + item.setPayload(note.message()); + item.attribute(Akonadi::Item::AddIfMissing)->setIconName(QStringLiteral("text-plain")); + + auto itemJob = new Akonadi::ItemCreateJob(item, collectionCreateJob->collection(), this); + connect(itemJob, &Akonadi::ItemCreateJob::result, this, [this, itemJob](KJob*){ + if (itemJob->error()) { + qWarning() << "Failed to create a note:" << itemJob->errorString(); + } + deleteLater(); + }); }