24 #include <akonadi/agentmanager.h>
25 #include <akonadi/agentinstancecreatejob.h>
26 #include "maildirsettings.h"
28 #include "akonadi_next/note.h"
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>
53 Akonadi::AgentManager *manager = Akonadi::AgentManager::self();
55 Akonadi::AgentInstance::List instances = manager->instances();
57 foreach (
const Akonadi::AgentInstance& instance, instances ) {
72 void LocalResourceCreator::createInstance()
76 Akonadi::AgentInstanceCreateJob *job =
new Akonadi::AgentInstanceCreateJob( notesType );
77 connect( job, SIGNAL(result(KJob*)),
78 this, SLOT(instanceCreated(KJob*)) );
83 void LocalResourceCreator::instanceCreated( KJob *job )
86 kWarning() << job->errorString();
91 Akonadi::AgentInstanceCreateJob *createJob = qobject_cast<Akonadi::AgentInstanceCreateJob*>(job);
92 Akonadi::AgentInstance instance = createJob->instance();
94 instance.setName( i18nc(
"Default name for resource holding notes",
"Local Notes" ) );
96 OrgKdeAkonadiMaildirSettingsInterface *iface =
new OrgKdeAkonadiMaildirSettingsInterface(
97 QLatin1String(
"org.freedesktop.Akonadi.Resource.") + instance.identifier(),
98 QLatin1String(
"/Settings"), QDBusConnection::sessionBus(), this );
101 if (!iface->isValid() ) {
102 kWarning() <<
"Failed to obtain D-Bus interface for remote configuration.";
110 instance.reconfigure();
112 Akonadi::ResourceSynchronizationJob *syncJob =
new Akonadi::ResourceSynchronizationJob(instance,
this);
113 connect( syncJob, SIGNAL(result(KJob*)), SLOT(syncDone(KJob*)));
117 void LocalResourceCreator::syncDone(KJob* job)
119 if ( job->error() ) {
120 kWarning() <<
"Synchronizing the resource failed:" << job->errorString();
125 kWarning() <<
"Instance synchronized";
127 Akonadi::CollectionFetchJob *collectionFetchJob =
new Akonadi::CollectionFetchJob( Akonadi::Collection::root(), Akonadi::CollectionFetchJob::FirstLevel,
this );
128 connect( collectionFetchJob, SIGNAL(result(KJob*)), SLOT(rootFetchFinished(KJob*)) );
131 void LocalResourceCreator::rootFetchFinished(KJob* job)
134 kWarning() << job->errorString();
139 Akonadi::CollectionFetchJob *lastCollectionFetchJob = qobject_cast<Akonadi::CollectionFetchJob*>(job);
140 if (!lastCollectionFetchJob) {
145 Akonadi::Collection::List list = lastCollectionFetchJob->collections();
149 kWarning() <<
"Couldn't find new collection in resource";
154 foreach (
const Akonadi::Collection &col, list)
156 Akonadi::AgentInstance instance = Akonadi::AgentManager::self()->instance(col.resource());
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*)) );
165 Q_ASSERT(!
"Couldn't find new collection");
169 void LocalResourceCreator::topLevelFetchFinished(KJob* job)
172 kWarning() << job->errorString();
177 Akonadi::CollectionFetchJob *lastCollectionFetchJob = qobject_cast<Akonadi::CollectionFetchJob*>(job);
178 if (!lastCollectionFetchJob) {
183 Akonadi::Collection::List list = lastCollectionFetchJob->collections();
191 Akonadi::Collection::Id
id = lastCollectionFetchJob->property(
"FetchedCollection").toLongLong();
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() );
199 Akonadi::EntityDisplayAttribute *eda =
new Akonadi::EntityDisplayAttribute();
200 eda->setIconName( QLatin1String(
"x-office-address-book") );
201 eda->setDisplayName( title );
202 collection.addAttribute(eda);
204 Akonadi::CollectionCreateJob *createJob =
new Akonadi::CollectionCreateJob( collection,
this );
205 connect( createJob, SIGNAL(result(KJob*)),
this, SLOT(createFinished(KJob*)) );
209 void LocalResourceCreator::createFinished(KJob* job)
212 kWarning() << job->errorString();
217 Akonadi::CollectionCreateJob* collectionCreateJob = qobject_cast<Akonadi::CollectionCreateJob*>(job);
218 if (!collectionCreateJob)
225 item.setParentCollection(collectionCreateJob->collection());
226 item.setMimeType( Akonotes::Note::mimeType() );
228 KMime::Message::Ptr note(
new KMime::Message() );
230 QString title = i18nc(
"The default name for new pages.",
"New Page" );
231 QByteArray encoding(
"utf-8" );
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 );
238 note->mainBodyPart()->fromUnicodeString( QLatin1String(
" ") );
242 item.setPayload(note);
243 Akonadi::EntityDisplayAttribute *eda =
new Akonadi::EntityDisplayAttribute();
244 eda->setIconName( QLatin1String(
"text-plain") );
245 item.addAttribute(eda);
247 Akonadi::ItemCreateJob *itemCreateJob =
new Akonadi::ItemCreateJob( item, collectionCreateJob->collection(),
this);
248 connect( itemCreateJob, SIGNAL(result(KJob*)), SLOT(itemCreateFinished(KJob*)) );
251 void LocalResourceCreator::itemCreateFinished(KJob* job)
254 kWarning() << job->errorString();
264 #include "localresourcecreator.moc"
static const QString akonadi_notes_instance_name
LocalResourceCreator(QObject *parent=0)