• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

kjots

  • sources
  • kde-4.14
  • kdepim
  • kjots
kjotsmodel.cpp
Go to the documentation of this file.
1 /*
2  This file is part of KJots.
3 
4  Copyright (c) 2009 Stephen Kelly <steveire@gmail.com>
5 
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.
10 
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.
15 
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.
20 */
21 
22 #include "kjotsmodel.h"
23 
24 #include <QTextDocument>
25 
26 #include <KIcon>
27 
28 #include <akonadi/changerecorder.h>
29 #include <akonadi/entitydisplayattribute.h>
30 
31 #include <Akonadi/Notes/NoteUtils>
32 
33 #include <kdebug.h>
34 #include <KMime/KMimeMessage>
35 
36 #include <kpimtextedit/textutils.h>
37 
38 #include <grantlee/markupdirector.h>
39 #include <grantlee/texthtmlbuilder.h>
40 #include <grantlee/plaintextmarkupbuilder.h>
41 #include "noteshared/attributes/notelockattribute.h"
42 
43 Q_DECLARE_METATYPE(QTextDocument*)
44 KJotsEntity::KJotsEntity(const QModelIndex &index, QObject *parent)
45  : QObject(parent)
46 {
47  m_index = QPersistentModelIndex(index);
48 }
49 
50 void KJotsEntity::setIndex(const QModelIndex &index)
51 {
52  m_index = QPersistentModelIndex(index);
53 }
54 
55 QString KJotsEntity::title() const
56 {
57  return m_index.data().toString();
58 }
59 
60 QString KJotsEntity::content() const
61 {
62  QTextDocument *document = m_index.data( KJotsModel::DocumentRole ).value<QTextDocument*>();
63  if (!document)
64  return QString();
65 
66  Grantlee::TextHTMLBuilder builder;
67  Grantlee::MarkupDirector director(&builder);
68 
69  director.processDocument(document);
70  QString result = builder.getResult();
71 
72  return result;
73 }
74 
75 QString KJotsEntity::plainContent() const
76 {
77  QTextDocument *document = m_index.data( KJotsModel::DocumentRole ).value<QTextDocument*>();
78  if (!document)
79  return QString();
80 
81  Grantlee::PlainTextMarkupBuilder builder;
82  Grantlee::MarkupDirector director(&builder);
83 
84  director.processDocument(document);
85  QString result = builder.getResult();
86 
87  return result;
88 }
89 
90 qint64 KJotsEntity::entityId() const
91 {
92  Item item = m_index.data(EntityTreeModel::ItemRole).value<Item>();
93  if (!item.isValid())
94  {
95  Collection col = m_index.data(EntityTreeModel::CollectionRole).value<Collection>();
96  if ( !col.isValid() )
97  return -1;
98  return col.id();
99  }
100  return item.id();
101 }
102 
103 bool KJotsEntity::isBook() const
104 {
105  Collection col = m_index.data(EntityTreeModel::CollectionRole).value<Collection>();
106 
107  if (col.isValid())
108  {
109  return col.contentMimeTypes().contains( Akonadi::NoteUtils::noteMimeType() );
110  }
111  return false;
112 }
113 
114 bool KJotsEntity::isPage() const
115 {
116  Item item = m_index.data(EntityTreeModel::ItemRole).value<Item>();
117  if (item.isValid())
118  {
119  return item.hasPayload<KMime::Message::Ptr>();
120  }
121  return false;
122 }
123 
124 QVariantList KJotsEntity::entities() const
125 {
126  QVariantList list;
127  int row = 0;
128  const int column = 0;
129  QModelIndex childIndex = m_index.child(row++, column);
130  while (childIndex.isValid())
131  {
132  QObject *obj = new KJotsEntity(childIndex);
133  list << QVariant::fromValue(obj);
134  childIndex = m_index.child(row++, column);
135  }
136  return list;
137 }
138 
139 QVariantList KJotsEntity::breadcrumbs() const
140 {
141  QVariantList list;
142  int row = 0;
143  const int column = 0;
144  QModelIndex parent = m_index.parent();
145 
146  while ( parent.isValid() )
147  {
148  QObject *obj = new KJotsEntity(parent);
149  list << QVariant::fromValue(obj);
150  parent = parent.parent();
151  }
152  return list;
153 }
154 
155 KJotsModel::KJotsModel( ChangeRecorder *monitor, QObject *parent )
156  : EntityTreeModel( monitor, parent )
157 {
158 
159 }
160 
161 KJotsModel::~KJotsModel()
162 {
163  qDeleteAll( m_documents );
164 }
165 
166 bool KJotsModel::setData( const QModelIndex& index, const QVariant& value, int role )
167 {
168  if ( role == Qt::EditRole )
169  {
170  Item item = index.data( ItemRole ).value<Item>();
171 
172  if ( !item.isValid() )
173  {
174  Collection col = index.data( CollectionRole ).value<Collection>();
175  col.setName( value.toString() );
176  if (col.hasAttribute<EntityDisplayAttribute>())
177  {
178  EntityDisplayAttribute *eda = col.attribute<EntityDisplayAttribute>();
179  eda->setDisplayName(value.toString());
180  }
181  return EntityTreeModel::setData(index, QVariant::fromValue( col ), CollectionRole );
182  }
183  KMime::Message::Ptr m = item.payload<KMime::Message::Ptr>();
184 
185  m->subject( true )->fromUnicodeString( value.toString(), "utf-8" );
186  m->assemble();
187  item.setPayload<KMime::Message::Ptr>( m );
188 
189  if ( item.hasAttribute<EntityDisplayAttribute>() ) {
190  EntityDisplayAttribute *displayAttribute = item.attribute<EntityDisplayAttribute>();
191  displayAttribute->setDisplayName( value.toString() );
192  }
193  return EntityTreeModel::setData(index, QVariant::fromValue<Item>( item ), ItemRole);
194  }
195 
196  if ( role == KJotsModel::DocumentRole )
197  {
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*>();
203 
204  bool isRichText = KPIMTextEdit::TextUtils::containsFormatting( document );
205 
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 );
213  }
214 
215  if ( role == KJotsModel::DocumentCursorPositionRole )
216  {
217  Item item = index.data( ItemRole ).value<Item>();
218  m_cursorPositions.insert( item.id(), value.toInt() );
219  return true;
220  }
221 
222  return EntityTreeModel::setData(index, value, role);
223 }
224 
225 QVariant KJotsModel::data( const QModelIndex &index, int role ) const
226 {
227  if (GrantleeObjectRole == role)
228  {
229  QObject *obj = new KJotsEntity(index);
230  return QVariant::fromValue(obj);
231  }
232 
233 
234  if ( role == KJotsModel::DocumentRole )
235  {
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();
242 
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() );
249 
250  m_documents.insert( itemId, document );
251  return QVariant::fromValue( document );
252  }
253 
254  if ( role == KJotsModel::DocumentCursorPositionRole )
255  {
256  const Item item = index.data( ItemRole ).value<Item>();
257  if (!item.isValid())
258  return 0;
259 
260  if ( m_cursorPositions.contains( item.id() ) )
261  return m_cursorPositions.value( item.id() );
262 
263  return 0;
264  }
265 
266  if ( role == Qt::DecorationRole )
267  {
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") );
275  }
276  }
277  }
278 
279  return EntityTreeModel::data(index, role);
280 }
281 
282 QVariant KJotsModel::entityData( const Akonadi::Item& item, int column, int role) const
283 {
284  if ( ( role == Qt::EditRole || role == Qt::DisplayRole ) && item.hasPayload<KMime::Message::Ptr>() )
285  {
286  KMime::Message::Ptr page = item.payload<KMime::Message::Ptr>();
287  return page->subject()->asUnicodeString();
288  }
289  return EntityTreeModel::entityData( item, column, role );
290 }
291 
QModelIndex
KJotsModel::data
QVariant data(const QModelIndex &index, int role) const
Definition: kjotsmodel.cpp:225
KJotsEntity::isBook
bool isBook() const
QHash::insert
iterator insert(const Key &key, const T &value)
KJotsEntity::title
QString title() const
KJotsEntity
A wrapper QObject making some book and page properties available to Grantlee.
Definition: kjotsmodel.h:39
KJotsEntity::entityId
qint64 entityId() const
KJotsModel::~KJotsModel
virtual ~KJotsModel()
Definition: kjotsmodel.cpp:161
EntityTreeModel
QVariant::value
T value() const
KJotsEntity::entities
QVariantList entities() const
KJotsEntity::breadcrumbs
QVariantList breadcrumbs() const
QPersistentModelIndex::child
QModelIndex child(int row, int column) const
QModelIndex::isValid
bool isValid() const
KJotsModel::setData
virtual bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole)
Definition: kjotsmodel.cpp:166
QVariant::toInt
int toInt(bool *ok) const
QObject
KJotsEntity::setIndex
void setIndex(const QModelIndex &index)
Definition: kjotsmodel.cpp:50
KJotsModel::entityData
virtual QVariant entityData(const Akonadi::Item &item, int column, int role=Qt::DisplayRole) const
Definition: kjotsmodel.cpp:282
QPersistentModelIndex::data
QVariant data(int role) const
QString
QModelIndex::parent
QModelIndex parent() const
KJotsEntity::content
QString content() const
QHash::value
const T value(const Key &key) const
KJotsEntity::plainContent
QString plainContent() const
QVariant::fromValue
QVariant fromValue(const T &value)
KJotsModel::DocumentRole
Definition: kjotsmodel.h:84
QPersistentModelIndex
QModelIndex::data
QVariant data(int role) const
QLatin1String
QTextDocument
QTextDocument::setHtml
void setHtml(const QString &html)
kjotsmodel.h
KJotsModel::GrantleeObjectRole
Definition: kjotsmodel.h:83
QHash::contains
bool contains(const Key &key) const
KJotsModel::DocumentCursorPositionRole
Definition: kjotsmodel.h:85
KJotsEntity::KJotsEntity
KJotsEntity(const QModelIndex &index, QObject *parent=0)
Definition: kjotsmodel.cpp:44
QObject::parent
QObject * parent() const
QVariant::toString
QString toString() const
KJotsEntity::isPage
bool isPage() const
QPersistentModelIndex::parent
QModelIndex parent() const
KJotsModel::KJotsModel
KJotsModel(ChangeRecorder *monitor, QObject *parent=0)
Definition: kjotsmodel.cpp:155
QVariant
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:32:12 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kjots

Skip menu "kjots"
  • Main Page
  • Namespace List
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer
  • pimprint

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal