19 #include "akonadidatasource.h"
21 #include <Akonadi/Item>
22 #include <Akonadi/ItemFetchJob>
23 #include <Akonadi/ItemFetchScope>
24 #include <Akonadi/Collection>
25 #include <Akonadi/CollectionFetchJob>
26 #include <Akonadi/CollectionFetchScope>
27 #include <Akonadi/ServerManager>
29 #include <KPluginFactory>
30 #include <KPluginLoader>
41 ~AkonadiAllContacts();
42 virtual QMap<QString, AbstractContact::Ptr> contacts();
44 void onCollectionsFetched(KJob *job);
45 void onItemsFetched(KJob *job);
46 void onItemAdded(
const Akonadi::Item &item);
47 void onItemChanged(
const Akonadi::Item &item);
48 void onItemRemoved(
const Akonadi::Item &item);
49 void onServerStateChanged(Akonadi::ServerManager::State);
51 Akonadi::Monitor *m_monitor;
52 QMap<QString, AbstractContact::Ptr> m_contacts;
53 int m_activeFetchJobsCount;
57 AkonadiAllContacts::AkonadiAllContacts():
58 m_monitor(new
Akonadi::Monitor(this)),
59 m_activeFetchJobsCount(0),
62 connect(Akonadi::ServerManager::self(), SIGNAL(stateChanged(Akonadi::ServerManager::State)), SLOT(onServerStateChanged(Akonadi::ServerManager::State)));
63 onServerStateChanged(Akonadi::ServerManager::state());
65 connect(m_monitor, SIGNAL(itemAdded(Akonadi::Item,Akonadi::Collection)), SLOT(onItemAdded(Akonadi::Item)));
66 connect(m_monitor, SIGNAL(itemChanged(Akonadi::Item,QSet<QByteArray>)), SLOT(onItemChanged(Akonadi::Item)));
67 connect(m_monitor, SIGNAL(itemRemoved(Akonadi::Item)), SLOT(onItemRemoved(Akonadi::Item)));
69 m_monitor->setMimeTypeMonitored(
"text/directory");
70 m_monitor->itemFetchScope().fetchFullPayload();
71 m_monitor->itemFetchScope().setFetchModificationTime(
false);
72 #ifdef HAVE_KDEPIM_ATLEAST_412
73 m_monitor->itemFetchScope().setFetchRemoteIdentification(
false);
76 CollectionFetchJob *fetchJob =
new CollectionFetchJob(Collection::root(), CollectionFetchJob::Recursive,
this);
77 fetchJob->fetchScope().setContentMimeTypes(QStringList() <<
"text/directory");
78 connect(fetchJob, SIGNAL(finished(KJob*)), SLOT(onCollectionsFetched(KJob*)));
81 AkonadiAllContacts::~AkonadiAllContacts()
85 QMap<QString, AbstractContact::Ptr> AkonadiAllContacts::contacts()
90 QString AkonadiDataSource::sourcePluginId()
const
95 void AkonadiAllContacts::onItemAdded(
const Item &item)
97 if (!item.hasPayload<AbstractContact::Ptr>()) {
100 const QString
id = item.url().prettyUrl();
101 const AbstractContact::Ptr contact = item.payload<AbstractContact::Ptr>();
102 m_contacts[id] = contact;
103 Q_EMIT contactAdded(item.url().prettyUrl(), contact);
106 void AkonadiAllContacts::onItemChanged(
const Item &item)
108 if (!item.hasPayload<AbstractContact::Ptr>()) {
111 const QString
id = item.url().prettyUrl();
112 const AbstractContact::Ptr contact = item.payload<AbstractContact::Ptr>();
113 m_contacts[id] = contact;
114 Q_EMIT contactChanged(item.url().prettyUrl(), contact);
117 void AkonadiAllContacts::onItemRemoved(
const Item &item)
119 if (!item.hasPayload<AbstractContact::Ptr>()) {
122 const QString
id = item.url().prettyUrl();
123 m_contacts.remove(
id);
124 Q_EMIT contactRemoved(
id);
128 void AkonadiAllContacts::onItemsFetched(KJob *job)
131 kWarning() << job->errorString();
134 ItemFetchJob *itemFetchJob = qobject_cast<ItemFetchJob *>(job);
135 foreach (
const Item &item, itemFetchJob->items()) {
140 if (--m_activeFetchJobsCount == 0 && !isInitialFetchComplete()) {
141 emitInitialFetchComplete(
true);
145 void AkonadiAllContacts::onCollectionsFetched(KJob *job)
148 kWarning() << job->errorString();
149 emitInitialFetchComplete(
false);
151 CollectionFetchJob *fetchJob = qobject_cast<CollectionFetchJob *>(job);
152 QList<Collection> contactCollections;
153 foreach (
const Collection &collection, fetchJob->collections()) {
156 if (collection.isVirtual()) {
159 if (collection.contentMimeTypes().contains(AbstractContact::Ptr::mimeType())) {
160 ItemFetchJob *itemFetchJob =
new ItemFetchJob(collection);
161 itemFetchJob->fetchScope().fetchFullPayload();
162 connect(itemFetchJob, SIGNAL(finished(KJob*)), SLOT(onItemsFetched(KJob*)));
163 ++m_activeFetchJobsCount;
166 if (m_activeFetchJobsCount == 0) {
167 emitInitialFetchComplete(
true);
170 if (m_activeFetchJobsCount == 0 && !isInitialFetchComplete()) {
171 emitInitialFetchComplete(
true);
176 void AkonadiAllContacts::onServerStateChanged(ServerManager::State state)
179 if (state == Akonadi::ServerManager::Broken && !isInitialFetchComplete()) {
180 emitInitialFetchComplete(
false);
181 qWarning() <<
"Akonadi failed to load, some metacontact features may not be available";
182 qWarning() <<
"For more information please load akonadi_console" ;
190 AkonadiContact(Akonadi::Monitor *monitor,
const QString &contactUri);
193 void onContactFetched(KJob *);
194 void onContactChanged(
const Akonadi::Item &);
196 Akonadi::Monitor *m_monitor;
197 Akonadi::Item m_item;
200 AkonadiContact::AkonadiContact(Akonadi::Monitor *monitor,
const QString &contactUri):
201 ContactMonitor(contactUri),
208 m_item = Item::fromUrl(QUrl(contactUri));
209 ItemFetchJob *itemFetchJob =
new ItemFetchJob(m_item);
210 itemFetchJob->fetchScope().fetchFullPayload();
211 connect(itemFetchJob, SIGNAL(finished(KJob*)), SLOT(onContactFetched(KJob*)));
214 m_monitor->setItemMonitored(m_item,
true);
215 connect(m_monitor, SIGNAL(itemChanged(Akonadi::Item,QSet<QByteArray>)), SLOT(onContactChanged(Akonadi::Item)));
218 AkonadiContact::~AkonadiContact()
220 m_monitor->setItemMonitored(m_item,
false);
223 void AkonadiContact::onContactFetched(KJob *job)
225 ItemFetchJob *fetchJob = qobject_cast<ItemFetchJob *>(job);
226 if (fetchJob->items().count() && fetchJob->items().first().hasPayload<AbstractContact::Ptr>()) {
227 setContact(fetchJob->items().first().payload<AbstractContact::Ptr>());
231 void AkonadiContact::onContactChanged(
const Item &item)
233 if (item != m_item) {
236 if (!item.hasPayload<AbstractContact::Ptr>()) {
239 setContact(item.payload<AbstractContact::Ptr>());
242 AkonadiDataSource::AkonadiDataSource(QObject *parent,
const QVariantList &args):
243 BasePersonsDataSource(parent),
244 m_monitor(new
Akonadi::Monitor(this))
247 m_monitor->itemFetchScope().fetchFullPayload();
248 m_monitor->itemFetchScope().setFetchModificationTime(
false);
249 #ifdef HAVE_KDEPIM_ATLEAST_412
250 m_monitor->itemFetchScope().setFetchRemoteIdentification(
false);
254 AkonadiDataSource::~AkonadiDataSource()
261 return new AkonadiAllContacts();
266 return new AkonadiContact(m_monitor, contactUri);
269 K_PLUGIN_FACTORY(AkonadiDataSourceFactory, registerPlugin<AkonadiDataSource>();)
270 K_EXPORT_PLUGIN(AkonadiDataSourceFactory(
"akonadi_kpeople_plugin"))
272 #include "akonadidatasource.moc"