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

kjots

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

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