SVN_SILENT made messages (.desktop file)
[kdepim.git] / kjots / kjotsmodel.cpp
blobea5f5672b332aa6298e74a60020a5bc6ce5a931f
1 /*
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
19 02110-1301, USA.
22 #include "kjotsmodel.h"
24 #include <QTextDocument>
26 #include <KIcon>
28 #include <akonadi/changerecorder.h>
29 #include <akonadi/entitydisplayattribute.h>
31 #include "akonadi_next/note.h"
33 #include <kdebug.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)
45 : 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*>();
63 if (!document)
64 return QString();
66 Grantlee::TextHTMLBuilder builder;
67 Grantlee::MarkupDirector director(&builder);
69 director.processDocument(document);
70 QString result = builder.getResult();
72 return result;
75 QString KJotsEntity::plainContent() const
77 QTextDocument *document = m_index.data( KJotsModel::DocumentRole ).value<QTextDocument*>();
78 if (!document)
79 return QString();
81 Grantlee::PlainTextMarkupBuilder builder;
82 Grantlee::MarkupDirector director(&builder);
84 director.processDocument(document);
85 QString result = builder.getResult();
87 return result;
90 qint64 KJotsEntity::entityId() const
92 Item item = m_index.data(EntityTreeModel::ItemRole).value<Item>();
93 if (!item.isValid())
95 Collection col = m_index.data(EntityTreeModel::CollectionRole).value<Collection>();
96 if ( !col.isValid() )
97 return -1;
98 return col.id();
100 return item.id();
103 bool KJotsEntity::isBook() const
105 Collection col = m_index.data(EntityTreeModel::CollectionRole).value<Collection>();
107 if (col.isValid())
109 return col.contentMimeTypes().contains( Akonotes::Note::mimeType() );
111 return false;
114 bool KJotsEntity::isPage() const
116 Item item = m_index.data(EntityTreeModel::ItemRole).value<Item>();
117 if (item.isValid())
119 return item.hasPayload<KMime::Message::Ptr>();
121 return false;
124 QVariantList KJotsEntity::entities() const
126 QVariantList list;
127 int row = 0;
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);
136 return list;
139 QVariantList KJotsEntity::breadcrumbs() const
141 QVariantList list;
142 int row = 0;
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();
152 return list;
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" );
186 m->assemble();
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>() )
200 return false;
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() );
210 note->assemble();
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() );
219 return true;
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>() )
241 return QVariant();
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() );
247 else
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>();
257 if (!item.isValid())
258 return 0;
260 if ( m_cursorPositions.contains( item.id() ) )
261 return m_cursorPositions.value( item.id() );
263 return 0;
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") );
271 } else {
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 );