24 #include <Akonadi/Notes/NoteUtils>
27 #include <KLocalizedString>
29 #include <Akonadi/CollectionFetchJob>
30 #include <Akonadi/AgentInstance>
31 #include <Akonadi/AgentManager>
32 #include <Akonadi/CollectionCreateJob>
33 #include <Akonadi/ItemCreateJob>
34 #include <akonadi/item.h>
35 #include <KMime/KMimeMessage>
36 #include <Akonadi/EntityDisplayAttribute>
46 Akonadi::CollectionFetchJob *collectionFetchJob =
new Akonadi::CollectionFetchJob( Akonadi::Collection::root(), Akonadi::CollectionFetchJob::FirstLevel,
this );
47 connect( collectionFetchJob, SIGNAL(result(KJob*)), SLOT(rootFetchFinished(KJob*)) );
50 void LocalResourceCreator::rootFetchFinished(KJob* job)
53 kWarning() << job->errorString();
58 Akonadi::CollectionFetchJob *lastCollectionFetchJob = qobject_cast<Akonadi::CollectionFetchJob*>(job);
59 if (!lastCollectionFetchJob) {
64 Akonadi::Collection::List list = lastCollectionFetchJob->collections();
68 kWarning() <<
"Couldn't find new collection in resource";
73 foreach (
const Akonadi::Collection &col, list)
75 Akonadi::AgentInstance instance = Akonadi::AgentManager::self()->instance(col.resource());
76 if (instance.type().identifier() == akonadiNotesInstanceName())
78 Akonadi::CollectionFetchJob *collectionFetchJob =
new Akonadi::CollectionFetchJob( col, Akonadi::CollectionFetchJob::FirstLevel,
this );
79 collectionFetchJob->setProperty(
"FetchedCollection", col.id());
80 connect( collectionFetchJob, SIGNAL(result(KJob*)), SLOT(topLevelFetchFinished(KJob*)) );
84 Q_ASSERT(!
"Couldn't find new collection");
88 void LocalResourceCreator::topLevelFetchFinished(KJob* job)
91 kWarning() << job->errorString();
96 Akonadi::CollectionFetchJob *lastCollectionFetchJob = qobject_cast<Akonadi::CollectionFetchJob*>(job);
97 if (!lastCollectionFetchJob) {
102 Akonadi::Collection::List list = lastCollectionFetchJob->collections();
110 Akonadi::Collection::Id
id = lastCollectionFetchJob->property(
"FetchedCollection").toLongLong();
112 Akonadi::Collection collection;
113 collection.setParentCollection( Akonadi::Collection(
id) );
114 QString title = i18nc(
"The default name for new books.",
"New Book" );
115 collection.setName( KRandom::randomString( 10 ) );
116 collection.setContentMimeTypes(
QStringList() << Akonadi::Collection::mimeType() << Akonadi::NoteUtils::noteMimeType() );
118 Akonadi::EntityDisplayAttribute *eda =
new Akonadi::EntityDisplayAttribute();
120 eda->setDisplayName( title );
121 collection.addAttribute(eda);
123 Akonadi::CollectionCreateJob *createJob =
new Akonadi::CollectionCreateJob( collection,
this );
124 connect( createJob, SIGNAL(result(KJob*)),
this, SLOT(createFinished(KJob*)) );
128 void LocalResourceCreator::createFinished(KJob* job)
131 kWarning() << job->errorString();
136 Akonadi::CollectionCreateJob* collectionCreateJob = qobject_cast<Akonadi::CollectionCreateJob*>(job);
137 if (!collectionCreateJob)
144 item.setParentCollection(collectionCreateJob->collection());
145 item.setMimeType( Akonadi::NoteUtils::noteMimeType() );
147 KMime::Message::Ptr note(
new KMime::Message() );
149 QString title = i18nc(
"The default name for new pages.",
"New Page" );
152 note->subject(
true )->fromUnicodeString( title, encoding );
153 note->contentType(
true )->setMimeType(
"text/plain" );
154 note->date(
true )->setDateTime( KDateTime::currentLocalDateTime() );
155 note->from(
true )->fromUnicodeString(
QLatin1String(
"Kjots@kde4"), encoding );
157 note->mainBodyPart()->fromUnicodeString(
QLatin1String(
" ") );
161 item.setPayload(note);
162 Akonadi::EntityDisplayAttribute *eda =
new Akonadi::EntityDisplayAttribute();
164 item.addAttribute(eda);
166 Akonadi::ItemCreateJob *itemCreateJob =
new Akonadi::ItemCreateJob( item, collectionCreateJob->collection(),
this);
167 connect( itemCreateJob, SIGNAL(result(KJob*)), SLOT(itemCreateFinished(KJob*)) );
170 void LocalResourceCreator::itemCreateFinished(KJob* job)
173 kWarning() << job->errorString();
Creates a notes resource, a book and a page if one does not already exist.
LocalResourceCreator(QObject *parent=0)
virtual void finishCreateResource()