2 This file is part of KJots.
4 Copyright (c) 2009 Stephen Kelly <steveire@gmail.com>
6 This library is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Library General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or (at your
9 option) any later version.
11 This library is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
14 License for more details.
16 You should have received a copy of the GNU Library General Public License
17 along with this library; see the file COPYING.LIB. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 #include "kjotsmodel.h"
24 #include <QTextDocument>
28 #include <akonadi/changerecorder.h>
29 #include <akonadi/entitydisplayattribute.h>
31 #include "akonadi_next/note.h"
34 #include <KMime/KMimeMessage>
36 #include <kpimtextedit/textutils.h>
38 #include <grantlee/markupdirector.h>
39 #include <grantlee/texthtmlbuilder.h>
40 #include <grantlee/plaintextmarkupbuilder.h>
41 #include "noteshared/attributes/notelockattribute.h"
43 Q_DECLARE_METATYPE(QTextDocument
*)
44 KJotsEntity::KJotsEntity(const QModelIndex
&index
, QObject
*parent
)
47 m_index
= QPersistentModelIndex(index
);
50 void KJotsEntity::setIndex(const QModelIndex
&index
)
52 m_index
= QPersistentModelIndex(index
);
55 QString
KJotsEntity::title() const
57 return m_index
.data().toString();
60 QString
KJotsEntity::content() const
62 QTextDocument
*document
= m_index
.data( KJotsModel::DocumentRole
).value
<QTextDocument
*>();
66 Grantlee::TextHTMLBuilder builder
;
67 Grantlee::MarkupDirector
director(&builder
);
69 director
.processDocument(document
);
70 QString result
= builder
.getResult();
75 QString
KJotsEntity::plainContent() const
77 QTextDocument
*document
= m_index
.data( KJotsModel::DocumentRole
).value
<QTextDocument
*>();
81 Grantlee::PlainTextMarkupBuilder builder
;
82 Grantlee::MarkupDirector
director(&builder
);
84 director
.processDocument(document
);
85 QString result
= builder
.getResult();
90 qint64
KJotsEntity::entityId() const
92 Item item
= m_index
.data(EntityTreeModel::ItemRole
).value
<Item
>();
95 Collection col
= m_index
.data(EntityTreeModel::CollectionRole
).value
<Collection
>();
103 bool KJotsEntity::isBook() const
105 Collection col
= m_index
.data(EntityTreeModel::CollectionRole
).value
<Collection
>();
109 return col
.contentMimeTypes().contains( Akonotes::Note::mimeType() );
114 bool KJotsEntity::isPage() const
116 Item item
= m_index
.data(EntityTreeModel::ItemRole
).value
<Item
>();
119 return item
.hasPayload
<KMime::Message::Ptr
>();
124 QVariantList
KJotsEntity::entities() const
128 const int column
= 0;
129 QModelIndex childIndex
= m_index
.child(row
++, column
);
130 while (childIndex
.isValid())
132 QObject
*obj
= new KJotsEntity(childIndex
);
133 list
<< QVariant::fromValue(obj
);
134 childIndex
= m_index
.child(row
++, column
);
139 QVariantList
KJotsEntity::breadcrumbs() const
143 const int column
= 0;
144 QModelIndex parent
= m_index
.parent();
146 while ( parent
.isValid() )
148 QObject
*obj
= new KJotsEntity(parent
);
149 list
<< QVariant::fromValue(obj
);
150 parent
= parent
.parent();
155 KJotsModel::KJotsModel( ChangeRecorder
*monitor
, QObject
*parent
)
156 : EntityTreeModel( monitor
, parent
)
161 KJotsModel::~KJotsModel()
163 qDeleteAll( m_documents
);
166 bool KJotsModel::setData( const QModelIndex
& index
, const QVariant
& value
, int role
)
168 if ( role
== Qt::EditRole
)
170 Item item
= index
.data( ItemRole
).value
<Item
>();
172 if ( !item
.isValid() )
174 Collection col
= index
.data( CollectionRole
).value
<Collection
>();
175 col
.setName( value
.toString() );
176 if (col
.hasAttribute
<EntityDisplayAttribute
>())
178 EntityDisplayAttribute
*eda
= col
.attribute
<EntityDisplayAttribute
>();
179 eda
->setDisplayName(value
.toString());
181 return EntityTreeModel::setData(index
, QVariant::fromValue( col
), CollectionRole
);
183 KMime::Message::Ptr m
= item
.payload
<KMime::Message::Ptr
>();
185 m
->subject( true )->fromUnicodeString( value
.toString(), "utf-8" );
187 item
.setPayload
<KMime::Message::Ptr
>( m
);
189 if ( item
.hasAttribute
<EntityDisplayAttribute
>() ) {
190 EntityDisplayAttribute
*displayAttribute
= item
.attribute
<EntityDisplayAttribute
>();
191 displayAttribute
->setDisplayName( value
.toString() );
193 return EntityTreeModel::setData(index
, QVariant::fromValue
<Item
>( item
), ItemRole
);
196 if ( role
== KJotsModel::DocumentRole
)
198 Item item
= EntityTreeModel::data( index
, ItemRole
).value
<Item
>();
199 if ( !item
.hasPayload
<KMime::Message::Ptr
>() )
201 KMime::Message::Ptr note
= item
.payload
<KMime::Message::Ptr
>();
202 QTextDocument
*document
= value
.value
<QTextDocument
*>();
204 bool isRichText
= KPIMTextEdit::TextUtils::containsFormatting( document
);
206 note
->contentType()->setMimeType( isRichText
? "text/html" : "text/plain" );
207 note
->contentType()->setCharset("utf-8");
208 note
->contentTransferEncoding(true)->setEncoding(KMime::Headers::CEquPr
);
209 note
->mainBodyPart()->fromUnicodeString( isRichText
? document
->toHtml() : document
->toPlainText() );
211 item
.setPayload
<KMime::Message::Ptr
>( note
);
212 return EntityTreeModel::setData(index
, QVariant::fromValue
<Item
>( item
), ItemRole
);
215 if ( role
== KJotsModel::DocumentCursorPositionRole
)
217 Item item
= index
.data( ItemRole
).value
<Item
>();
218 m_cursorPositions
.insert( item
.id(), value
.toInt() );
222 return EntityTreeModel::setData(index
, value
, role
);
225 QVariant
KJotsModel::data( const QModelIndex
&index
, int role
) const
227 if (GrantleeObjectRole
== role
)
229 QObject
*obj
= new KJotsEntity(index
);
230 return QVariant::fromValue(obj
);
234 if ( role
== KJotsModel::DocumentRole
)
236 const Item item
= index
.data( ItemRole
).value
<Item
>();
237 Entity::Id itemId
= item
.id();
238 if ( m_documents
.contains( itemId
) )
239 return QVariant::fromValue( m_documents
.value( itemId
) );
240 if ( !item
.hasPayload
<KMime::Message::Ptr
>() )
243 KMime::Message::Ptr note
= item
.payload
<KMime::Message::Ptr
>();
244 QTextDocument
*document
= new QTextDocument
;
245 if ( note
->contentType()->isHTMLText() )
246 document
->setHtml( note
->mainBodyPart()->decodedText() );
248 document
->setPlainText( note
->mainBodyPart()->decodedText() );
250 m_documents
.insert( itemId
, document
);
251 return QVariant::fromValue( document
);
254 if ( role
== KJotsModel::DocumentCursorPositionRole
)
256 const Item item
= index
.data( ItemRole
).value
<Item
>();
260 if ( m_cursorPositions
.contains( item
.id() ) )
261 return m_cursorPositions
.value( item
.id() );
266 if ( role
== Qt::DecorationRole
)
268 const Item item
= index
.data( ItemRole
).value
<Item
>();
269 if ( item
.isValid() && item
.hasAttribute
<NoteShared::NoteLockAttribute
>() ) {
270 return KIcon( QLatin1String("emblem-locked") );
272 const Collection col
= index
.data( CollectionRole
).value
<Collection
>();
273 if ( col
.isValid() && col
.hasAttribute
<NoteShared::NoteLockAttribute
>() ) {
274 return KIcon(QLatin1String( "emblem-locked") );
279 return EntityTreeModel::data(index
, role
);
282 QVariant
KJotsModel::entityData( const Akonadi::Item
& item
, int column
, int role
) const
284 if ( ( role
== Qt::EditRole
|| role
== Qt::DisplayRole
) && item
.hasPayload
<KMime::Message::Ptr
>() )
286 KMime::Message::Ptr page
= item
.payload
<KMime::Message::Ptr
>();
287 return page
->subject()->asUnicodeString();
289 return EntityTreeModel::entityData( item
, column
, role
);