• 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
localresourcecreator.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2010 Klarälvdalens Datakonsult AB,
3  a KDAB Group company, info@kdab.net,
4  author Stephen Kelly <stephen@kdab.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 "localresourcecreator.h"
23 
24 #include <akonadi/agentmanager.h>
25 #include <akonadi/agentinstancecreatejob.h>
26 #include "maildirsettings.h"
27 
28 #include "akonadi_next/note.h"
29 
30 #include <KDebug>
31 #include <KGlobal>
32 #include <KLocale>
33 #include <KRandom>
34 #include <KStandardDirs>
35 #include <akonadi/resourcesynchronizationjob.h>
36 #include <Akonadi/CollectionFetchJob>
37 #include <Akonadi/CollectionCreateJob>
38 #include <Akonadi/ItemCreateJob>
39 #include <akonadi/item.h>
40 #include <KMime/KMimeMessage>
41 #include <Akonadi/EntityDisplayAttribute>
42 
43 static const QString akonadi_notes_instance_name = QLatin1String("akonadi_akonotes_resource");
44 
45 LocalResourceCreator::LocalResourceCreator(QObject* parent)
46  : QObject(parent)
47 {
48 
49 }
50 
51 void LocalResourceCreator::createIfMissing()
52 {
53  Akonadi::AgentManager *manager = Akonadi::AgentManager::self();
54 
55  Akonadi::AgentInstance::List instances = manager->instances();
56  bool found = false;
57  foreach ( const Akonadi::AgentInstance& instance, instances ) {
58  if (instance.type().identifier() == akonadi_notes_instance_name)
59  {
60  found = true;
61  break;
62  }
63  }
64  if (found)
65  {
66  deleteLater();
67  return;
68  }
69  createInstance();
70 }
71 
72 void LocalResourceCreator::createInstance()
73 {
74  Akonadi::AgentType notesType = Akonadi::AgentManager::self()->type( akonadi_notes_instance_name );
75 
76  Akonadi::AgentInstanceCreateJob *job = new Akonadi::AgentInstanceCreateJob( notesType );
77  connect( job, SIGNAL(result(KJob*)),
78  this, SLOT(instanceCreated(KJob*)) );
79 
80  job->start();
81 }
82 
83 void LocalResourceCreator::instanceCreated( KJob *job )
84 {
85  if (job->error()) {
86  kWarning() << job->errorString();
87  deleteLater();
88  return;
89  }
90 
91  Akonadi::AgentInstanceCreateJob *createJob = qobject_cast<Akonadi::AgentInstanceCreateJob*>(job);
92  Akonadi::AgentInstance instance = createJob->instance();
93 
94  instance.setName( i18nc( "Default name for resource holding notes", "Local Notes" ) );
95 
96  OrgKdeAkonadiMaildirSettingsInterface *iface = new OrgKdeAkonadiMaildirSettingsInterface(
97  QLatin1String("org.freedesktop.Akonadi.Resource.") + instance.identifier(),
98  QLatin1String("/Settings"), QDBusConnection::sessionBus(), this );
99 
100  // TODO: Make errors user-visible.
101  if (!iface->isValid() ) {
102  kWarning() << "Failed to obtain D-Bus interface for remote configuration.";
103  delete iface;
104  deleteLater();
105  return;
106  }
107 
108  //QDBusPendingReply<void> response = iface->setPath( KGlobal::dirs()->localxdgdatadir() + "/notes/" + KRandom::randomString( 10 ) );
109 
110  instance.reconfigure();
111 
112  Akonadi::ResourceSynchronizationJob *syncJob = new Akonadi::ResourceSynchronizationJob(instance, this);
113  connect( syncJob, SIGNAL(result(KJob*)), SLOT(syncDone(KJob*)));
114  syncJob->start();
115 }
116 
117 void LocalResourceCreator::syncDone(KJob* job)
118 {
119  if ( job->error() ) {
120  kWarning() << "Synchronizing the resource failed:" << job->errorString();
121  deleteLater();
122  return;
123  }
124 
125  kWarning() << "Instance synchronized";
126 
127  Akonadi::CollectionFetchJob *collectionFetchJob = new Akonadi::CollectionFetchJob( Akonadi::Collection::root(), Akonadi::CollectionFetchJob::FirstLevel, this );
128  connect( collectionFetchJob, SIGNAL(result(KJob*)), SLOT(rootFetchFinished(KJob*)) );
129 }
130 
131 void LocalResourceCreator::rootFetchFinished(KJob* job)
132 {
133  if (job->error()) {
134  kWarning() << job->errorString();
135  deleteLater();
136  return;
137  }
138 
139  Akonadi::CollectionFetchJob *lastCollectionFetchJob = qobject_cast<Akonadi::CollectionFetchJob*>(job);
140  if (!lastCollectionFetchJob) {
141  deleteLater();
142  return;
143  }
144 
145  Akonadi::Collection::List list = lastCollectionFetchJob->collections();
146 
147  if (list.isEmpty())
148  {
149  kWarning() << "Couldn't find new collection in resource";
150  deleteLater();
151  return;
152  }
153 
154  foreach (const Akonadi::Collection &col, list)
155  {
156  Akonadi::AgentInstance instance = Akonadi::AgentManager::self()->instance(col.resource());
157  if (instance.type().identifier() == akonadi_notes_instance_name)
158  {
159  Akonadi::CollectionFetchJob *collectionFetchJob = new Akonadi::CollectionFetchJob( col, Akonadi::CollectionFetchJob::FirstLevel, this );
160  collectionFetchJob->setProperty("FetchedCollection", col.id());
161  connect( collectionFetchJob, SIGNAL(result(KJob*)), SLOT(topLevelFetchFinished(KJob*)) );
162  return;
163  }
164  }
165  Q_ASSERT(!"Couldn't find new collection");
166  deleteLater();
167 }
168 
169 void LocalResourceCreator::topLevelFetchFinished(KJob* job)
170 {
171  if (job->error()) {
172  kWarning() << job->errorString();
173  deleteLater();
174  return;
175  }
176 
177  Akonadi::CollectionFetchJob *lastCollectionFetchJob = qobject_cast<Akonadi::CollectionFetchJob*>(job);
178  if (!lastCollectionFetchJob) {
179  deleteLater();
180  return;
181  }
182 
183  Akonadi::Collection::List list = lastCollectionFetchJob->collections();
184 
185  if (!list.isEmpty())
186  {
187  deleteLater();
188  return;
189  }
190 
191  Akonadi::Collection::Id id = lastCollectionFetchJob->property("FetchedCollection").toLongLong();
192 
193  Akonadi::Collection collection;
194  collection.setParentCollection( Akonadi::Collection(id) );
195  QString title = i18nc( "The default name for new books.", "New Book" );
196  collection.setName( KRandom::randomString( 10 ) );
197  collection.setContentMimeTypes( QStringList() << Akonadi::Collection::mimeType() << Akonotes::Note::mimeType() );
198 
199  Akonadi::EntityDisplayAttribute *eda = new Akonadi::EntityDisplayAttribute();
200  eda->setIconName( QLatin1String("x-office-address-book") );
201  eda->setDisplayName( title );
202  collection.addAttribute(eda);
203 
204  Akonadi::CollectionCreateJob *createJob = new Akonadi::CollectionCreateJob( collection, this );
205  connect( createJob, SIGNAL(result(KJob*)), this, SLOT(createFinished(KJob*)) );
206 
207 }
208 
209 void LocalResourceCreator::createFinished(KJob* job)
210 {
211  if (job->error()) {
212  kWarning() << job->errorString();
213  deleteLater();
214  return;
215  }
216 
217  Akonadi::CollectionCreateJob* collectionCreateJob = qobject_cast<Akonadi::CollectionCreateJob*>(job);
218  if (!collectionCreateJob)
219  {
220  deleteLater();
221  return;
222  }
223 
224  Akonadi::Item item;
225  item.setParentCollection(collectionCreateJob->collection());
226  item.setMimeType( Akonotes::Note::mimeType() );
227 
228  KMime::Message::Ptr note( new KMime::Message() );
229 
230  QString title = i18nc( "The default name for new pages.", "New Page" );
231  QByteArray encoding( "utf-8" );
232 
233  note->subject( true )->fromUnicodeString( title, encoding );
234  note->contentType( true )->setMimeType( "text/plain" );
235  note->date( true )->setDateTime( KDateTime::currentLocalDateTime() );
236  note->from( true )->fromUnicodeString( QLatin1String("Kjots@kde4"), encoding );
237  // Need a non-empty body part so that the serializer regards this as a valid message.
238  note->mainBodyPart()->fromUnicodeString( QLatin1String(" ") );
239 
240  note->assemble();
241 
242  item.setPayload(note);
243  Akonadi::EntityDisplayAttribute *eda = new Akonadi::EntityDisplayAttribute();
244  eda->setIconName( QLatin1String("text-plain") );
245  item.addAttribute(eda);
246 
247  Akonadi::ItemCreateJob *itemCreateJob = new Akonadi::ItemCreateJob( item, collectionCreateJob->collection(), this);
248  connect( itemCreateJob, SIGNAL(result(KJob*)), SLOT(itemCreateFinished(KJob*)) );
249 }
250 
251 void LocalResourceCreator::itemCreateFinished(KJob* job)
252 {
253  if (job->error()) {
254  kWarning() << job->errorString();
255  }
256  deleteLater();
257 }
258 
259 
260 
261 
262 
263 
264 #include "localresourcecreator.moc"
akonadi_notes_instance_name
static const QString akonadi_notes_instance_name
Definition: localresourcecreator.cpp:43
LocalResourceCreator::LocalResourceCreator
LocalResourceCreator(QObject *parent=0)
Definition: localresourcecreator.cpp:45
QObject
localresourcecreator.h
LocalResourceCreator::createIfMissing
void createIfMissing()
Definition: localresourcecreator.cpp:51
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