1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
|
/*
Copyright (c) 2007 Volker Krause <vkrause@kde.org>
Copyright (c) 2009 Sebastian Kügler <sebas@kde.org>
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
#include "akonadiengine.h"
#include <Akonadi/CollectionFetchJob>
#include <Akonadi/ItemFetchJob>
#include <Akonadi/ItemFetchScope>
#include <Akonadi/Monitor>
#include <akonadi/kmime/messageparts.h>
#include <kabc/addressee.h>
#include <kabc/phonenumber.h>
#include <kabc/picture.h>
#include <kabc/key.h>
using namespace Akonadi;
AkonadiEngine::AkonadiEngine(QObject* parent, const QVariantList& args)
: Plasma::DataEngine(parent),
m_emailMonitor(0),
m_contactMonitor(0)
{
Q_UNUSED(args);
setMaxSourceCount( 512 ); // Guard against loading thousands of emails
}
void AkonadiEngine::initEmailMonitor()
{
m_emailMonitor = new Monitor( this );
m_emailMonitor->setMimeTypeMonitored("message/rfc822");
//m_emailMonitor->setCollectionMonitored(Collection::root(), false);
m_emailMonitor->itemFetchScope().fetchFullPayload( true );
connect(m_emailMonitor, SIGNAL(itemAdded(Akonadi::Item,Akonadi::Collection)),
SLOT(emailItemAdded(Akonadi::Item)) );
connect(m_emailMonitor, SIGNAL(itemChanged(Akonadi::Item,QSet<QByteArray>)),
SLOT(emailItemAdded(Akonadi::Item)) );
// remove the monitor on a source that's not used
connect(this, SIGNAL(sourceRemoved(QString)), SLOT(stopMonitor(QString)));
}
void AkonadiEngine::initContactMonitor()
{
m_contactMonitor = new Monitor( this );
m_contactMonitor->setMimeTypeMonitored("text/directory");
m_contactMonitor->setCollectionMonitored(Collection::root(), false);
m_contactMonitor->itemFetchScope().fetchFullPayload();
connect(m_contactMonitor, SIGNAL(itemAdded(Akonadi::Item,Akonadi::Collection)),
SLOT(contactItemAdded(Akonadi::Item)) );
connect(m_contactMonitor, SIGNAL(itemChanged(Akonadi::Item,QSet<QByteArray>)),
SLOT(contactItemAdded(Akonadi::Item)) );
// remove the monitor on a source that's not used
connect(this, SIGNAL(sourceRemoved(QString)), SLOT(stopMonitor(QString)));
}
AkonadiEngine::~AkonadiEngine()
{
}
QStringList AkonadiEngine::sources() const
{
return QStringList() << "EmailCollections" << "ContactCollections";
}
void AkonadiEngine::fetchEmailCollectionsDone(KJob* job)
{
// called when the job fetching email collections from Akonadi emits result()
if ( job->error() ) {
qDebug() << "Job Error:" << job->errorString();
} else {
CollectionFetchJob* cjob = static_cast<CollectionFetchJob*>( job );
int i = 0;
foreach( const Collection &collection, cjob->collections() ) {
if (collection.contentMimeTypes().contains("message/rfc822")) {
//qDebug() << "EmailCollection setting data:" << collection.name() << collection.url() << collection.contentMimeTypes();
i++;
setData("EmailCollections", QString("EmailCollection-%1").arg(collection.id()), collection.name());
}
}
qDebug() << i << "Email collections are in now";
scheduleSourcesUpdated();
}
}
void AkonadiEngine::fetchContactCollectionsDone(KJob* job)
{
// called when the job fetching contact collections from Akonadi emits result()
if ( job->error() ) {
qDebug() << "Job Error:" << job->errorString();
} else {
CollectionFetchJob* cjob = static_cast<CollectionFetchJob*>( job );
int i = 0;
foreach( const Collection &collection, cjob->collections() ) {
if (collection.contentMimeTypes().contains("text/directory")) {
//qDebug() << "ContactCollection setting data:" << collection.name() << collection.url() << collection.contentMimeTypes();
i++;
setData("ContactCollections", QString("ContactCollection-%1").arg(collection.id()), collection.name());
}
}
qDebug() << i << "Contact collections are in now";
scheduleSourcesUpdated();
}
}
bool AkonadiEngine::sourceRequestEvent(const QString &name)
{
qDebug() << "Source requested:" << name << sources();
if (name == "EmailCollections") {
Collection emailCollection(Collection::root());
emailCollection.setContentMimeTypes(QStringList() << "message/rfc822");
CollectionFetchJob *fetch = new CollectionFetchJob( emailCollection, CollectionFetchJob::Recursive);
connect( fetch, SIGNAL(result(KJob*)), SLOT(fetchEmailCollectionsDone(KJob*)) );
// For async data fetching, it's mandatory to set the data source to empty before returning true
setData(name, DataEngine::Data());
return true;
} else if (name.startsWith(QString("EmailCollection-"))) {
qlonglong id = name.split('-')[1].toLongLong();
ItemFetchJob* fetch = new ItemFetchJob( Collection( id ), this );
if (!m_emailMonitor) {
initEmailMonitor();
}
m_emailMonitor->setCollectionMonitored(Collection( id ), true);
fetch->fetchScope().fetchPayloadPart( MessagePart::Envelope );
connect( fetch, SIGNAL(result(KJob*)), SLOT(fetchEmailCollectionDone(KJob*)) );
connect( fetch, SIGNAL(itemsReceived(Akonadi::Item::List)), SLOT(emailItemsReceived(Akonadi::Item::List)) );
m_jobCollections[fetch] = name;
setData(name, DataEngine::Data());
return true;
} else if (name.startsWith(QString("Email-"))) {
qlonglong id = name.split('-')[1].toLongLong();
ItemFetchJob* fetch = new ItemFetchJob( Item( id ), this );
if (!m_emailMonitor) {
initEmailMonitor();
}
m_emailMonitor->setItemMonitored(Item( id ), true);
fetch->fetchScope().fetchFullPayload( true );
connect( fetch, SIGNAL(result(KJob*)), SLOT(fetchEmailCollectionDone(KJob*)) );
connect( fetch, SIGNAL(itemsReceived(Akonadi::Item::List)), SLOT(emailItemsReceived(Akonadi::Item::List)) );
m_jobCollections[fetch] = name;
setData(name, DataEngine::Data());
return true;
} else if (name == "ContactCollections") {
Collection contactCollection(Collection::root());
contactCollection.setContentMimeTypes(QStringList() << "text/directory");
CollectionFetchJob* fetch = new CollectionFetchJob( contactCollection, CollectionFetchJob::Recursive);
connect( fetch, SIGNAL(result(KJob*)), SLOT(fetchContactCollectionsDone(KJob*)) );
setData(name, DataEngine::Data());
return true;
} else if (name.startsWith(QString("ContactCollection-"))) {
qlonglong id = name.split('-')[1].toLongLong();
ItemFetchJob *fetch = new ItemFetchJob( Collection( id ), this );
if (!m_contactMonitor) {
initContactMonitor();
}
m_contactMonitor->setCollectionMonitored(Collection( id ), true); // FIXME: should be contacts monitor
fetch->fetchScope().fetchFullPayload();
connect( fetch, SIGNAL(result(KJob*)), SLOT(fetchContactCollectionDone(KJob*)) );
setData(name, DataEngine::Data());
return true;
} else if (name.startsWith(QString("Contact-"))) {
qDebug() << "Fetching contact" << name;
qlonglong id = name.split('-')[1].toLongLong();
ItemFetchJob *fetch = new ItemFetchJob( Item( id ), this );
if (!m_contactMonitor) {
initContactMonitor();
}
m_contactMonitor->setItemMonitored(Item( id ), true); // FIXME: should be contacts monitor
fetch->fetchScope().fetchFullPayload();
connect( fetch, SIGNAL(result(KJob*)), SLOT(fetchContactCollectionDone(KJob*)) );
setData(name, DataEngine::Data());
return true;
// We don't understand the request.
qDebug() << "Don't know what to do with:" << name;
return false;
}
void AkonadiEngine::stopMonitor(const QString &name)
{
if (name.startsWith(QString("EmailCollection-"))) {
// Stop monitoring this one
qlonglong id = name.split('-')[1].toLongLong();
m_emailMonitor->setCollectionMonitored( Collection( id ), false);
qDebug() << "Removed monitor from:" << name << id;
}
}
void AkonadiEngine::emailItemsReceived(const Akonadi::Item::List &items)
{
foreach (const Akonadi::Item &item, items) {
emailItemAdded(item);
}
}
void AkonadiEngine::emailItemAdded(const Akonadi::Item &item, const QString &collection)
{
if ( !item.hasPayload<MessagePtr>() ) {
return;
}
MessagePtr msg = item.payload<MessagePtr>();
if (msg) {
QString source = QString::number( item.id() );
source = "Email-" + source;
//qDebug() << "new source adding:" << source << item.url() << msg->subject()->asUnicodeString();
setData( source, "Id", item.id() );
setData( source, "Collection", collection);
setData( source, "Url", item.url().url() );
setData( source, "Subject", msg->subject()->asUnicodeString() );
setData( source, "From", msg->from()->asUnicodeString() );
setData( source, "DateTime", msg->date()->dateTime().date() );
setData( source, "To", msg->to()->asUnicodeString() );
setData( source, "Cc", msg->cc()->asUnicodeString() );
setData( source, "Bcc", msg->bcc()->asUnicodeString() );
setData( source, "Body", QString(msg->mainBodyPart()->body()));
// Flags
//qDebug() << item.flags();
setData( source, "Flag-New", !item.hasFlag("\\Seen") );
setData( source, "Flag-Task", item.hasFlag("\\Task") ); // not in Akonadi!
setData( source, "Flag-Important", item.hasFlag("important") );
setData( source, "Flag-Attachment", item.hasFlag("has_attachment") );
setData( source, "Flag-Spam", item.hasFlag("spam") );
setData( source, "Flag-Draft", item.hasFlag("\\Draft") );
setData( source, "Flag-Answered", item.hasFlag("\\Answered") );
setData( source, "Flag-Deleted", item.hasFlag("\\Deleted") );
setData( source, "Flag-Flagged", item.hasFlag("\\Flagged") );
if (!collection.isEmpty()) {
setData( collection, source, msg->subject()->asUnicodeString());
}
printMessage(msg);
scheduleSourcesUpdated();
}
}
void AkonadiEngine::printMessage(const MessagePtr msg)
{
return;
qDebug() << "sub" << msg->subject()->asUnicodeString();
return;
qDebug() << "=============== New Item" << msg->from()->asUnicodeString() << msg->subject()->asUnicodeString();
qDebug() << "sub" << msg->subject()->asUnicodeString();
qDebug() << "from" << msg->from()->asUnicodeString();
qDebug() << "date" << msg->date()->dateTime().date();
qDebug() << "to" << msg->to()->asUnicodeString();
qDebug() << "cc" << msg->cc()->asUnicodeString();
qDebug() << "bcc" << msg->bcc()->asUnicodeString();
qDebug() << "body" << msg->mainBodyPart()->body();
}
void AkonadiEngine::fetchEmailCollectionDone(KJob* job)
{
if ( job->error() ) {
qDebug() << "Job Error:" << job->errorString();
return;
}
const QString col = m_jobCollections[job];
Item::List items = static_cast<ItemFetchJob*>( job )->items();
foreach ( const Item &item, items ) {
emailItemAdded(item, col);
}
m_jobCollections.remove(job);
scheduleSourcesUpdated();
}
void AkonadiEngine::fetchContactCollectionDone(KJob* job)
{
if ( job->error() ) {
return;
}
Item::List items = static_cast<ItemFetchJob*>( job )->items();
foreach ( const Item &item, items ) {
contactItemAdded( item );
}
}
void AkonadiEngine::printContact(const QString &source, const KABC::Addressee &a)
{
qDebug() << "-----------------------------------";
qDebug() << source;
qDebug() << "name" << a.name();
qDebug() << "formattedName" << a.formattedName();
qDebug() << "nameLabel" << a.nameLabel();
qDebug() << "given" << a.givenName();
qDebug() << "familyName" << a.familyName();
qDebug() << "realName" << a.realName();
qDebug() << "organization" << a.organization();
qDebug() << "department" << a.department();
qDebug() << "role" << a.role();
qDebug() << "emails" << a.emails();
qDebug() << "fullEmail" << a.fullEmail();
qDebug() << "photoUrl" << a.photo().url();
qDebug() << "note" << a.note();
QStringList phoneNumbers;
foreach (const KABC::PhoneNumber &pn, a.phoneNumbers()) {
const QString key = QString("Phone-%1").arg(pn.typeLabel());
qDebug() << key << a.phoneNumber(pn.type()).number();
phoneNumbers << a.phoneNumber(pn.type()).number();
}
qDebug() << "phoneNumbers" << phoneNumbers;
qDebug() << "additionalName" << a.additionalName();
}
void AkonadiEngine::contactItemAdded( const Akonadi::Item &item )
{
if (item.hasPayload<KABC::Addressee>()) {
//qDebug() << item.id() << "item has payload ...";
KABC::Addressee a = item.payload<KABC::Addressee>();
if (!a.isEmpty()) {
const QString source = QString("Contact-%1").arg(item.id());
setData(source, "Id", item.id());
setData(source, "Url", item.url().url());
// Name and related
setData(source, "Name", a.formattedName());
setData(source, "GivenName", a.givenName());
setData(source, "FamilyName", a.familyName());
setData(source, "NickName", a.nickName());
setData(source, "RealName", a.realName());
setData(source, "AdditionalName", a.additionalName());
// Organization and related
setData(source, "Organization", a.organization());
setData(source, "Department", a.department());
setData(source, "Role", a.role());
// EMail and related
setData(source, "Emails", a.emails());
setData(source, "FullEmail", a.fullEmail());
// Phone and related
QStringList phoneNumbers;
foreach (const KABC::PhoneNumber &pn, a.phoneNumbers()) {
const QString key = QString("Phone-%1").arg(pn.typeLabel());
setData(source, key, a.phoneNumber(pn.type()).number());
phoneNumbers << a.phoneNumber(pn.type()).number();
}
setData(source, "PhoneNumbers", phoneNumbers);
// Personal
setData(source, "Birthday", a.birthday());
setData(source, "Photo", a.photo().data());
setData(source, "PhotoUrl", a.photo().url());
setData(source, "Latitude", a.geo().latitude());
setData(source, "Longitude", a.geo().longitude());
// addresses
// categories
// note,
setData(source, "Note", a.note());
// prefix
// ...
//printContact(source, a);
scheduleSourcesUpdated();
}
}
}
|