• 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
  • plasmoid
akonotes_noteapplet.cpp
Go to the documentation of this file.
1 /*
2  Copyright 2009 KDAB and Guillermo A. Amaral B. <gamaral@amaral.com.mx>
3  Copyright (c) 2009 Stephen Kelly <steveire@gmail.com>
4 
5  This library is free software; you can redistribute it and/or modify it
6  under the terms of the GNU Library General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or (at your
8  option) any later version.
9 
10  This library is distributed in the hope that it will be useful, but WITHOUT
11  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13  License for more details.
14 
15  You should have received a copy of the GNU Library General Public License
16  along with this library; see the file COPYING.LIB. If not, write to the
17  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18  02110-1301, USA.
19 */
20 
21 #include "akonotes_noteapplet.h"
22 
23 #include <QtCore/QMetaMethod>
24 #include <QtDBus/QDBusConnection>
25 #include <QtDBus/QDBusInterface>
26 #include <QLabel>
27 #include <QPainter>
28 #include <QGraphicsLinearLayout>
29 #include <QGraphicsSceneResizeEvent>
30 
31 #include <KJob>
32 #include <KLineEdit>
33 #include <KStandardDirs>
34 #include <KTextEdit>
35 
36 #include <Plasma/FrameSvg>
37 #include <Plasma/Label>
38 #include <Plasma/LineEdit>
39 #include <Plasma/TextEdit>
40 
41 #include <KMime/Message>
42 
43 #include <akonadi/agentinstancecreatejob.h>
44 #include <akonadi/agentmanager.h>
45 #include <akonadi/collectionfetchjob.h>
46 #include <akonadi/collectionfetchscope.h>
47 #include <akonadi/item.h>
48 #include <akonadi/itemcreatejob.h>
49 #include <akonadi/itemfetchjob.h>
50 #include <akonadi/itemfetchscope.h>
51 #include <akonadi/itemmonitor.h>
52 #include <akonadi/itemmodifyjob.h>
53 #include <akonadi/monitor.h>
54 #include <akonadi/resourcesynchronizationjob.h>
55 
56 #include <Akonadi/Notes/NoteUtils>
57 
58 using namespace Akonadi;
59 
60 K_EXPORT_PLASMA_APPLET( akonotes_note, AkonotesNoteApplet )
61 
62 AkonotesNoteApplet::AkonotesNoteApplet( QObject* parent, const QVariantList& args )
63  : Applet( parent, args ), m_monitor( new Monitor( this ) )
64 {
65  setAspectRatioMode( Plasma::IgnoreAspectRatio );
66  setBackgroundHints( Plasma::Applet::NoBackground );
67 
68  m_subject = new Plasma::LineEdit( this );
69  m_subject->installEventFilter( this );
70  m_subject->setText( i18n("Subject") );
71  {
72  QFont cf = m_subject->nativeWidget()->font();
73  QPalette cp = m_subject->nativeWidget()->palette();
74 
75  cf.setPointSize( cf.pointSize() - 2 );
76  cp.setColor( QPalette::Active, QPalette::WindowText,
77  QColor( 105, 105, 4 ) );
78  cp.setColor( QPalette::Inactive, QPalette::WindowText,
79  QColor( 185, 185, 84 ) );
80 
81  m_subject->nativeWidget()->setFont( cf );
82  m_subject->nativeWidget()->setPalette( cp );
83  }
84 
85  m_content = new Plasma::TextEdit( this );
86  m_content->setText( i18n("content") );
87  m_content->installEventFilter( this );
88  {
89  QPalette cp = m_content->nativeWidget()->palette();
90 
91  cp.setColor( QPalette::Active, QPalette::Text,
92  QColor( Qt::black ) );
93  cp.setColor( QPalette::Inactive, QPalette::Text,
94  QColor( Qt::black ) );
95 
96  m_content->nativeWidget()->setPalette( cp );
97  }
98 
99  m_theme = new Plasma::FrameSvg( this );
100  m_theme->setImagePath( QLatin1String("widgets/stickynote") );
101  m_theme->setEnabledBorders( Plasma::FrameSvg::AllBorders );
102 
103  m_layout = new QGraphicsLinearLayout;
104  m_layout->setContentsMargins( 9, 9, 9, 9 );
105  m_layout->setOrientation( Qt::Vertical );
106  m_layout->setSpacing( 15 );
107  m_layout->addItem( m_subject );
108  m_layout->addItem( m_content );
109  m_layout->setStretchFactor( m_content, 220 );
110 
111  setLayout( m_layout );
112  resize( 200, 200 );
113 
114  m_monitor->itemFetchScope().fetchFullPayload( true );
115  connect( m_monitor, SIGNAL(itemChanged(Akonadi::Item,QSet<QByteArray>)), SLOT(itemChanged(Akonadi::Item)));
116  connect( m_monitor, SIGNAL(itemRemoved(Akonadi::Item)), SLOT(itemRemoved()) );
117 
118  if ( !args.isEmpty() )
119  {
120  m_item = Akonadi::Item::fromUrl( args.first().toString() );
121  }
122 }
123 
124 void AkonotesNoteApplet::init()
125 {
126  KConfigGroup cg = config();
127 
128  Item::Id itemId = m_item.id();
129 
130  if ( !m_item.isValid() )
131  itemId = cg.readEntry( "itemId", -1 );
132 
133  if ( itemId < 0 )
134  {
135  createInDefaultCollection();
136  } else
137  {
138  Akonadi::ItemFetchJob *job = new Akonadi::ItemFetchJob( Item( itemId ), this );
139  m_monitor->setItemMonitored( Item( itemId ), true );
140  job->fetchScope().fetchFullPayload( true );
141  connect( job, SIGNAL(itemsReceived(Akonadi::Item::List)), SLOT(itemsFetched(Akonadi::Item::List)) );
142  connect( job, SIGNAL(result(KJob*)), SLOT(itemFetchDone(KJob*)) );
143  }
144 }
145 
146 void AkonotesNoteApplet::createInDefaultCollection()
147 {
148  KConfig noteCollectionConfig( QLatin1String("notesrc") );
149  KConfigGroup generalGroup( &noteCollectionConfig, "General" );
150  int unsortedCollectionId = generalGroup.readEntry( "unsortedCollection", -1 );
151 
152  if ( unsortedCollectionId > 1 )
153  {
154  CollectionFetchJob *collectionFetchJob = new CollectionFetchJob( Collection( unsortedCollectionId ), CollectionFetchJob::Base, this );
155  connect( collectionFetchJob, SIGNAL(result(KJob*)), SLOT(collectionFetchDone(KJob*)) );
156  } else {
157  createDefaultConcreteCollection();
158  }
159 }
160 
161 void AkonotesNoteApplet::createDefaultConcreteCollection()
162 {
163  AgentType noteType = AgentManager::self()->type( QLatin1String("akonadi_akonotes_resource") );
164  AgentInstanceCreateJob *noteResourceCreateJob = new AgentInstanceCreateJob( noteType );
165  connect( noteResourceCreateJob, SIGNAL(result(KJob*)), SLOT(defaultCreated(KJob*)) );
166 
167  noteResourceCreateJob->start();
168 }
169 
170 void AkonotesNoteApplet::defaultCreated( KJob *job )
171 {
172  AgentInstanceCreateJob *agentJob = qobject_cast<AgentInstanceCreateJob *>( job );
173 
174  Q_ASSERT( agentJob );
175 
176  AgentInstance instance = agentJob->instance();
177 
178  QDBusInterface iface( QString::fromLatin1("org.freedesktop.Akonadi.Agent.%1").arg( instance.identifier() ),
179  QLatin1String("/Settings"), QString(), QDBusConnection::sessionBus(), this );
180 
181  if ( !iface.isValid() ) {
182  kError() << "Unable to obtain the KConfigXT D-Bus interface of " << instance.identifier();
183  return;
184  }
185  iface.call( QLatin1String("setPath"), KStandardDirs::locateLocal( "data", QLatin1String("unsortednotes/") ) );
186  instance.reconfigure();
187 
188  ResourceSynchronizationJob *syncJob = new ResourceSynchronizationJob( instance );
189  connect( syncJob, SIGNAL(result(KJob*)), SLOT(syncDone(KJob*)) );
190 
191  syncJob->start();
192 }
193 
194 void AkonotesNoteApplet::syncDone( KJob *job )
195 {
196  ResourceSynchronizationJob *resourceSync = qobject_cast<ResourceSynchronizationJob *>( job );
197 
198  Q_ASSERT( resourceSync );
199 
200  AgentInstance agent = resourceSync->resource();
201 
202  CollectionFetchJob *collectionFetchJob = new CollectionFetchJob( Collection::root(), CollectionFetchJob::FirstLevel, this );
203  collectionFetchJob->fetchScope().setResource( agent.identifier() );
204  connect( collectionFetchJob, SIGNAL(result(KJob*)), SLOT(collectionFetchDone(KJob*)) );
205 }
206 
207 void AkonotesNoteApplet::collectionFetchDone( KJob *job )
208 {
209  if ( job->error() )
210  {
211  kWarning() << job->errorString(); // Could be that the collection in the config does not exist.
212  KConfig noteCollectionConfig( QLatin1String("notesrc") );
213  KConfigGroup generalGroup( &noteCollectionConfig, "General" );
214  generalGroup.writeEntry( "unsortedCollection", -1 );
215  static int attempts = 0;
216  if ( attempts == 0 )
217  createDefaultConcreteCollection();
218  return;
219  }
220 
221  CollectionFetchJob *fetchJob = qobject_cast<CollectionFetchJob *>( job );
222 
223  Q_ASSERT( fetchJob );
224 
225  Collection::List collectionList = fetchJob->collections();
226 
227  if ( collectionList.isEmpty() )
228  return; // Can happen if a sync does not create any collections.
229 
230  Q_ASSERT( collectionList.size() == 1 );
231 
232  Collection targetCollection = collectionList.first();
233 
234  Q_ASSERT( targetCollection.isValid() );
235 
236  KConfig noteCollectionConfig( QLatin1String("notesrc") );
237  KConfigGroup generalGroup( &noteCollectionConfig, "General" );
238  generalGroup.writeEntry( "unsortedCollection", targetCollection.id() );
239 
240  Item item;
241  item.setMimeType( Akonadi::NoteUtils::noteMimeType() );
242 
243  KMime::Message::Ptr msg = KMime::Message::Ptr( new KMime::Message() );
244 
245  QString title = i18nc( "The default name for new pages.", "New Page" );
246  QByteArray encoding( "utf-8" );
247 
248  msg->subject( true )->fromUnicodeString( title, encoding );
249  msg->contentType( true )->setMimeType( "text/plain" );
250  msg->contentType()->setCharset("utf-8");
251  msg->contentTransferEncoding(true)->setEncoding(KMime::Headers::CEquPr);
252  msg->date( true )->setDateTime( KDateTime::currentLocalDateTime() );
253  // Need a non-empty body part so that the serializer regards this as a valid message.
254  msg->mainBodyPart()->fromUnicodeString( QLatin1String(" ") );
255 
256  msg->assemble();
257 
258  item.setPayload( msg );
259 
260  ItemCreateJob *itemCreateJob = new ItemCreateJob(item, targetCollection);
261  connect( itemCreateJob, SIGNAL(result(KJob*)), SLOT(itemCreateJobFinished(KJob*)));
262 }
263 
264 
265 void AkonotesNoteApplet::itemFetchDone( KJob *job )
266 {
267  if ( job->error() )
268  {
269  kDebug() << job->errorString();
270  }
271  if ( !m_item.isValid() )
272  {
273  createInDefaultCollection();
274  }
275 }
276 
277 
278 void AkonotesNoteApplet::paintInterface(QPainter* painter, const QStyleOptionGraphicsItem* option, const QRect& contentsRect)
279 {
280  Q_UNUSED(option);
281  Q_UNUSED(contentsRect);
282 
283  painter->setRenderHint(QPainter::SmoothPixmapTransform);
284  painter->setRenderHint(QPainter::Antialiasing);
285 
286  painter->save();
287  m_theme->paintFrame(painter);
288  painter->restore();
289 }
290 
291 void AkonotesNoteApplet::resizeEvent(QGraphicsSceneResizeEvent* event)
292 {
293  Plasma::Applet::resizeEvent(event);
294  m_theme->resizeFrame(event->newSize());
295 }
296 
297 
298 bool AkonotesNoteApplet::eventFilter( QObject* watched, QEvent* event )
299 {
300  if ( ( event->type() == QEvent::FocusOut )
301  && ( m_content->nativeWidget()->document()->isModified() || m_subject->nativeWidget()->isModified() )
302  && ( watched == m_content || watched == m_subject ) )
303  {
304  saveItem();
305  }
306  return QObject::eventFilter(watched,event);
307 }
308 
309 void AkonotesNoteApplet::saveItem()
310 {
311  if ( !m_item.hasPayload<KMime::Message::Ptr>() )
312  return;
313 
314  KMime::Message::Ptr msg = m_item.payload<KMime::Message::Ptr>();
315 
316  QByteArray encoding = "utf-8";
317 
318  msg->subject()->fromUnicodeString( m_subject->text(), encoding );
319  msg->mainBodyPart()->fromUnicodeString( m_content->nativeWidget()->toPlainText() );
320  msg->contentType(true)->setCharset("utf-8");
321  msg->contentTransferEncoding(true)->setEncoding(KMime::Headers::CEquPr);
322  msg->assemble();
323  m_item.setPayload( msg );
324 
325  ItemModifyJob *modifyJob = new ItemModifyJob(m_item, this);
326  connect(modifyJob, SIGNAL(result(KJob*)), SLOT(modifyDone(KJob*)) );
327 
328  m_content->nativeWidget()->document()->setModified( false );
329  m_subject->nativeWidget()->setModified( false );
330 }
331 
332 void AkonotesNoteApplet::modifyDone( KJob *job )
333 {
334  if ( job->error() )
335  {
336  kDebug() << job->errorString();
337  }
338 }
339 
340 
341 void AkonotesNoteApplet::itemsFetched( const Akonadi::Item::List& itemList )
342 {
343  Q_ASSERT( itemList.size() == 1 );
344  Akonadi::Item item = itemList.first();
345 
346  if ( !item.hasPayload<KMime::Message::Ptr>() )
347  {
348  createInDefaultCollection();
349  return;
350  }
351 
352  KConfigGroup cg = config();
353  cg.writeEntry( "itemId", item.id() );
354 
355  itemChanged( item );
356 }
357 
358 void AkonotesNoteApplet::itemCreateJobFinished( KJob *job )
359 {
360  if ( job->error() )
361  {
362  kDebug() << job->errorString();
363  return;
364  }
365 
366  Akonadi::ItemCreateJob *createJob = qobject_cast<Akonadi::ItemCreateJob *>( job );
367 
368  if (!createJob)
369  return;
370 
371  Akonadi::Item item = createJob->item();
372 
373  m_monitor->setItemMonitored( item, true );
374 
375  KConfigGroup cg = config();
376  cg.writeEntry( "itemId", item.id() );
377 
378  itemChanged( item );
379 }
380 
381 void AkonotesNoteApplet::itemChanged( const Akonadi::Item& item )
382 {
383  if ( !item.hasPayload<KMime::Message::Ptr>() )
384  return;
385 
386  KMime::Message::Ptr msg = item.payload<KMime::Message::Ptr>();
387  KMime::Headers::Subject *subject = msg->subject();
388  m_subject->setText( subject->asUnicodeString() );
389  m_content->nativeWidget()->setText( msg->mainBodyPart()->decodedText() );
390  m_item = item;
391 }
392 
393 void AkonotesNoteApplet::itemRemoved()
394 {
395  destroy();
396 }
397 
QEvent
QFont::setPointSize
void setPointSize(int pointSize)
AkonotesNoteApplet::itemsFetched
void itemsFetched(const Akonadi::Item::List &list)
Definition: akonotes_noteapplet.cpp:341
QEvent::type
Type type() const
QGraphicsLayout::setContentsMargins
void setContentsMargins(qreal left, qreal top, qreal right, qreal bottom)
AkonotesNoteApplet::resizeEvent
virtual void resizeEvent(QGraphicsSceneResizeEvent *event)
Definition: akonotes_noteapplet.cpp:291
QGraphicsLinearLayout
QPainter::setRenderHint
void setRenderHint(RenderHint hint, bool on)
QPalette::setColor
void setColor(ColorGroup group, ColorRole role, const QColor &color)
AkonotesNoteApplet::itemRemoved
void itemRemoved()
Definition: akonotes_noteapplet.cpp:393
AkonotesNoteApplet::init
virtual void init()
Definition: akonotes_noteapplet.cpp:124
QByteArray
QFont
QPainter::save
void save()
QDBusConnection::sessionBus
QDBusConnection sessionBus()
AkonotesNoteApplet::paintInterface
virtual void paintInterface(QPainter *painter, const QStyleOptionGraphicsItem *option, const QRect &contentsRect)
Definition: akonotes_noteapplet.cpp:278
akonotes_noteapplet.h
QDBusAbstractInterface::call
QDBusMessage call(const QString &method, const QVariant &arg1, const QVariant &arg2, const QVariant &arg3, const QVariant &arg4, const QVariant &arg5, const QVariant &arg6, const QVariant &arg7, const QVariant &arg8)
QRect
QObject
AkonotesNoteApplet::modifyDone
void modifyDone(KJob *job)
Definition: akonotes_noteapplet.cpp:332
QPainter
AkonotesNoteApplet::eventFilter
virtual bool eventFilter(QObject *watched, QEvent *event)
Definition: akonotes_noteapplet.cpp:298
QObject::eventFilter
virtual bool eventFilter(QObject *watched, QEvent *event)
QSet
QString
QColor
QGraphicsSceneResizeEvent
QDBusInterface
AkonotesNoteApplet::itemChanged
void itemChanged(const Akonadi::Item &item)
Definition: akonotes_noteapplet.cpp:381
QPainter::restore
void restore()
AkonotesNoteApplet::syncDone
void syncDone(KJob *job)
Definition: akonotes_noteapplet.cpp:194
QLatin1String
AkonotesNoteApplet::defaultCreated
void defaultCreated(KJob *job)
Definition: akonotes_noteapplet.cpp:170
QString::fromLatin1
QString fromLatin1(const char *str, int size)
QStyleOptionGraphicsItem
AkonotesNoteApplet::itemCreateJobFinished
void itemCreateJobFinished(KJob *job)
Definition: akonotes_noteapplet.cpp:358
AkonotesNoteApplet::itemFetchDone
void itemFetchDone(KJob *job)
Definition: akonotes_noteapplet.cpp:265
AkonotesNoteApplet
Definition: akonotes_noteapplet.h:44
QGraphicsSceneResizeEvent::newSize
QSizeF newSize() const
AkonotesNoteApplet::collectionFetchDone
void collectionFetchDone(KJob *job)
Definition: akonotes_noteapplet.cpp:207
QPalette
QFont::pointSize
int pointSize() const
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