25 #include <QTextDocument>
29 #include <akonadi/changerecorder.h>
30 #include <akonadi/entitydisplayattribute.h>
32 #include "akonadi_next/note.h"
35 #include <KMime/KMimeMessage>
37 #include <kpimtextedit/textutils.h>
39 #include <grantlee/markupdirector.h>
40 #include <grantlee/texthtmlbuilder.h>
41 #include <grantlee/plaintextmarkupbuilder.h>
44 Q_DECLARE_METATYPE(QTextDocument*)
48 m_index = QPersistentModelIndex(index);
53 m_index = QPersistentModelIndex(index);
58 return m_index.data().toString();
67 Grantlee::TextHTMLBuilder builder;
68 Grantlee::MarkupDirector director(&builder);
70 director.processDocument(document);
71 QString result = builder.getResult();
82 Grantlee::PlainTextMarkupBuilder builder;
83 Grantlee::MarkupDirector director(&builder);
85 director.processDocument(document);
86 QString result = builder.getResult();
93 Item item = m_index.data(EntityTreeModel::ItemRole).value<Item>();
96 Collection col = m_index.data(EntityTreeModel::CollectionRole).value<Collection>();
106 Collection col = m_index.data(EntityTreeModel::CollectionRole).value<Collection>();
110 return col.contentMimeTypes().contains( Akonotes::Note::mimeType() );
117 Item item = m_index.data(EntityTreeModel::ItemRole).value<Item>();
120 return item.hasPayload<KMime::Message::Ptr>();
129 const int column = 0;
130 QModelIndex childIndex = m_index.child(row++, column);
131 while (childIndex.isValid())
134 list << QVariant::fromValue(obj);
135 childIndex = m_index.child(row++, column);
144 const int column = 0;
145 QModelIndex parent = m_index.parent();
147 while ( parent.isValid() )
150 list << QVariant::fromValue(obj);
151 parent = parent.parent();
164 qDeleteAll( m_documents );
169 if ( role == Qt::EditRole )
171 Item item = index.data( ItemRole ).value<Item>();
173 if ( !item.isValid() )
175 Collection col = index.data( CollectionRole ).value<Collection>();
176 col.setName( value.toString() );
177 if (col.hasAttribute<EntityDisplayAttribute>())
179 EntityDisplayAttribute *eda = col.attribute<EntityDisplayAttribute>();
180 eda->setDisplayName(value.toString());
182 return EntityTreeModel::setData(index, QVariant::fromValue( col ), CollectionRole );
184 KMime::Message::Ptr m = item.payload<KMime::Message::Ptr>();
186 m->subject(
true )->fromUnicodeString( value.toString(),
"utf-8" );
188 item.setPayload<KMime::Message::Ptr>( m );
190 if ( item.hasAttribute<EntityDisplayAttribute>() ) {
191 EntityDisplayAttribute *displayAttribute = item.attribute<EntityDisplayAttribute>();
192 displayAttribute->setDisplayName( value.toString() );
194 return EntityTreeModel::setData(index, QVariant::fromValue<Item>( item ), ItemRole);
199 Item item = EntityTreeModel::data( index, ItemRole ).value<Item>();
200 if ( !item.hasPayload<KMime::Message::Ptr>() )
202 KMime::Message::Ptr note = item.payload<KMime::Message::Ptr>();
203 QTextDocument *document = value.value<QTextDocument*>();
205 bool isRichText = KPIMTextEdit::TextUtils::containsFormatting( document );
207 note->contentType()->setMimeType( isRichText ?
"text/html" :
"text/plain" );
208 note->contentType()->setCharset(
"utf-8");
209 note->contentTransferEncoding(
true)->setEncoding(KMime::Headers::CEquPr);
210 note->mainBodyPart()->fromUnicodeString( isRichText ? document->toHtml() : document->toPlainText() );
212 item.setPayload<KMime::Message::Ptr>( note );
213 return EntityTreeModel::setData(index, QVariant::fromValue<Item>( item ), ItemRole );
218 Item item = index.data( ItemRole ).value<Item>();
219 m_cursorPositions.insert( item.id(), value.toInt() );
223 return EntityTreeModel::setData(index, value, role);
231 return QVariant::fromValue(obj);
237 const Item item = index.data( ItemRole ).value<Item>();
238 Entity::Id itemId = item.id();
239 if ( m_documents.contains( itemId ) )
240 return QVariant::fromValue( m_documents.value( itemId ) );
241 if ( !item.hasPayload<KMime::Message::Ptr>() )
244 KMime::Message::Ptr note = item.payload<KMime::Message::Ptr>();
245 QTextDocument *document =
new QTextDocument;
246 if ( note->contentType()->isHTMLText() )
247 document->setHtml( note->mainBodyPart()->decodedText() );
249 document->setPlainText( note->mainBodyPart()->decodedText() );
251 m_documents.insert( itemId, document );
252 return QVariant::fromValue( document );
257 const Item item = index.data( ItemRole ).value<Item>();
261 if ( m_cursorPositions.contains( item.id() ) )
262 return m_cursorPositions.value( item.id() );
267 if ( role == Qt::DecorationRole )
269 const Item item = index.data( ItemRole ).value<Item>();
271 return KIcon( QLatin1String(
"emblem-locked") );
273 const Collection col = index.data( CollectionRole ).value<Collection>();
275 return KIcon(QLatin1String(
"emblem-locked") );
280 return EntityTreeModel::data(index, role);
285 if ( ( role == Qt::EditRole || role == Qt::DisplayRole ) && item.hasPayload<KMime::Message::Ptr>() )
287 KMime::Message::Ptr page = item.payload<KMime::Message::Ptr>();
288 return page->subject()->asUnicodeString();
290 return EntityTreeModel::entityData( item, column, role );
293 #include "kjotsmodel.moc"
QVariant data(const QModelIndex &index, int role) const
A wrapper QObject making some book and page properties available to Grantlee.
QVariantList entities() const
QVariantList breadcrumbs() const
virtual bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole)
void setIndex(const QModelIndex &index)
virtual QVariant entityData(const Akonadi::Item &item, int column, int role=Qt::DisplayRole) const
QString plainContent() const
KJotsEntity(const QModelIndex &index, QObject *parent=0)
KJotsModel(ChangeRecorder *monitor, QObject *parent=0)