23 #include <QtCore/QMetaMethod>
24 #include <QtDBus/QDBusConnection>
25 #include <QtDBus/QDBusInterface>
28 #include <QGraphicsLinearLayout>
29 #include <QGraphicsSceneResizeEvent>
33 #include <KStandardDirs>
36 #include <Plasma/FrameSvg>
37 #include <Plasma/Label>
38 #include <Plasma/LineEdit>
39 #include <Plasma/TextEdit>
41 #include <KMime/Message>
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>
56 #include <Akonadi/Notes/NoteUtils>
58 using namespace Akonadi;
63 : Applet( parent, args ), m_monitor( new Monitor( this ) )
65 setAspectRatioMode( Plasma::IgnoreAspectRatio );
66 setBackgroundHints( Plasma::Applet::NoBackground );
68 m_subject =
new Plasma::LineEdit(
this );
69 m_subject->installEventFilter(
this );
70 m_subject->setText( i18n(
"Subject") );
72 QFont cf = m_subject->nativeWidget()->font();
73 QPalette cp = m_subject->nativeWidget()->palette();
76 cp.
setColor( QPalette::Active, QPalette::WindowText,
78 cp.
setColor( QPalette::Inactive, QPalette::WindowText,
81 m_subject->nativeWidget()->setFont( cf );
82 m_subject->nativeWidget()->setPalette( cp );
85 m_content =
new Plasma::TextEdit(
this );
86 m_content->setText( i18n(
"content") );
87 m_content->installEventFilter(
this );
89 QPalette cp = m_content->nativeWidget()->palette();
91 cp.
setColor( QPalette::Active, QPalette::Text,
93 cp.
setColor( QPalette::Inactive, QPalette::Text,
96 m_content->nativeWidget()->setPalette( cp );
99 m_theme =
new Plasma::FrameSvg(
this );
100 m_theme->setImagePath(
QLatin1String(
"widgets/stickynote") );
101 m_theme->setEnabledBorders( Plasma::FrameSvg::AllBorders );
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 );
111 setLayout( m_layout );
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()) );
118 if ( !args.isEmpty() )
120 m_item = Akonadi::Item::fromUrl( args.first().toString() );
126 KConfigGroup cg = config();
128 Item::Id itemId = m_item.id();
130 if ( !m_item.isValid() )
131 itemId = cg.readEntry(
"itemId", -1 );
135 createInDefaultCollection();
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*)) );
146 void AkonotesNoteApplet::createInDefaultCollection()
149 KConfigGroup generalGroup( ¬eCollectionConfig,
"General" );
150 int unsortedCollectionId = generalGroup.readEntry(
"unsortedCollection", -1 );
152 if ( unsortedCollectionId > 1 )
154 CollectionFetchJob *collectionFetchJob =
new CollectionFetchJob( Collection( unsortedCollectionId ), CollectionFetchJob::Base,
this );
155 connect( collectionFetchJob, SIGNAL(result(KJob*)), SLOT(collectionFetchDone(KJob*)) );
157 createDefaultConcreteCollection();
161 void AkonotesNoteApplet::createDefaultConcreteCollection()
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*)) );
167 noteResourceCreateJob->start();
172 AgentInstanceCreateJob *agentJob = qobject_cast<AgentInstanceCreateJob *>( job );
174 Q_ASSERT( agentJob );
176 AgentInstance instance = agentJob->instance();
181 if ( !iface.isValid() ) {
182 kError() <<
"Unable to obtain the KConfigXT D-Bus interface of " << instance.identifier();
186 instance.reconfigure();
188 ResourceSynchronizationJob *syncJob =
new ResourceSynchronizationJob( instance );
189 connect( syncJob, SIGNAL(result(KJob*)), SLOT(syncDone(KJob*)) );
196 ResourceSynchronizationJob *resourceSync = qobject_cast<ResourceSynchronizationJob *>( job );
198 Q_ASSERT( resourceSync );
200 AgentInstance agent = resourceSync->resource();
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*)) );
211 kWarning() << job->errorString();
213 KConfigGroup generalGroup( ¬eCollectionConfig,
"General" );
214 generalGroup.writeEntry(
"unsortedCollection", -1 );
215 static int attempts = 0;
217 createDefaultConcreteCollection();
221 CollectionFetchJob *fetchJob = qobject_cast<CollectionFetchJob *>( job );
223 Q_ASSERT( fetchJob );
225 Collection::List collectionList = fetchJob->collections();
227 if ( collectionList.isEmpty() )
230 Q_ASSERT( collectionList.size() == 1 );
232 Collection targetCollection = collectionList.first();
234 Q_ASSERT( targetCollection.isValid() );
237 KConfigGroup generalGroup( ¬eCollectionConfig,
"General" );
238 generalGroup.writeEntry(
"unsortedCollection", targetCollection.id() );
241 item.setMimeType( Akonadi::NoteUtils::noteMimeType() );
243 KMime::Message::Ptr msg = KMime::Message::Ptr(
new KMime::Message() );
245 QString title = i18nc(
"The default name for new pages.",
"New Page" );
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() );
254 msg->mainBodyPart()->fromUnicodeString(
QLatin1String(
" ") );
258 item.setPayload( msg );
260 ItemCreateJob *itemCreateJob =
new ItemCreateJob(item, targetCollection);
261 connect( itemCreateJob, SIGNAL(result(KJob*)), SLOT(itemCreateJobFinished(KJob*)));
269 kDebug() << job->errorString();
271 if ( !m_item.isValid() )
273 createInDefaultCollection();
281 Q_UNUSED(contentsRect);
287 m_theme->paintFrame(painter);
293 Plasma::Applet::resizeEvent(event);
294 m_theme->resizeFrame(event->
newSize());
300 if ( ( event->
type() == QEvent::FocusOut )
301 && ( m_content->nativeWidget()->document()->isModified() || m_subject->nativeWidget()->isModified() )
302 && ( watched == m_content || watched == m_subject ) )
309 void AkonotesNoteApplet::saveItem()
311 if ( !m_item.hasPayload<KMime::Message::Ptr>() )
314 KMime::Message::Ptr msg = m_item.payload<KMime::Message::Ptr>();
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);
323 m_item.setPayload( msg );
325 ItemModifyJob *modifyJob =
new ItemModifyJob(m_item,
this);
326 connect(modifyJob, SIGNAL(result(KJob*)), SLOT(modifyDone(KJob*)) );
328 m_content->nativeWidget()->document()->setModified(
false );
329 m_subject->nativeWidget()->setModified(
false );
336 kDebug() << job->errorString();
343 Q_ASSERT( itemList.size() == 1 );
344 Akonadi::Item item = itemList.first();
346 if ( !item.hasPayload<KMime::Message::Ptr>() )
348 createInDefaultCollection();
352 KConfigGroup cg = config();
353 cg.writeEntry(
"itemId", item.id() );
362 kDebug() << job->errorString();
366 Akonadi::ItemCreateJob *createJob = qobject_cast<Akonadi::ItemCreateJob *>( job );
371 Akonadi::Item item = createJob->item();
373 m_monitor->setItemMonitored( item,
true );
375 KConfigGroup cg = config();
376 cg.writeEntry(
"itemId", item.id() );
383 if ( !item.hasPayload<KMime::Message::Ptr>() )
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() );
void setPointSize(int pointSize)
void itemsFetched(const Akonadi::Item::List &list)
void setContentsMargins(qreal left, qreal top, qreal right, qreal bottom)
virtual void resizeEvent(QGraphicsSceneResizeEvent *event)
void setRenderHint(RenderHint hint, bool on)
void setColor(ColorGroup group, ColorRole role, const QColor &color)
QDBusConnection sessionBus()
virtual void paintInterface(QPainter *painter, const QStyleOptionGraphicsItem *option, const QRect &contentsRect)
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)
void modifyDone(KJob *job)
virtual bool eventFilter(QObject *watched, QEvent *event)
virtual bool eventFilter(QObject *watched, QEvent *event)
void itemChanged(const Akonadi::Item &item)
void defaultCreated(KJob *job)
QString fromLatin1(const char *str, int size)
void itemCreateJobFinished(KJob *job)
void itemFetchDone(KJob *job)
void collectionFetchDone(KJob *job)