20 #include "plugins/emaillistmodel.h"
21 #include "plugins/emaillistviewdelegate.h"
26 #include <QVBoxLayout>
29 #include <KMime/Message>
30 #include <QDesktopServices>
31 #include <KLocalizedString>
33 #include <baloo/query.h>
34 #include <baloo/resultiterator.h>
36 #include <Akonadi/Item>
37 #include <Akonadi/ItemFetchJob>
38 #include <Akonadi/ItemFetchScope>
42 Emails::Emails(QObject *parent): AbstractFieldWidgetFactory(parent)
46 QWidget *Emails::createDetailsWidget(
const AbstractContact::Ptr &person,
const AbstractContact::List &contacts, QWidget *parent)
const
49 QWidget *widget =
new QWidget(parent);
51 QVBoxLayout *layout =
new QVBoxLayout(widget);
52 layout->setContentsMargins(0, 0, 0, 0);
56 query.setSearchString(person.preferredEmail());
57 query.setType(
"Email");
58 Baloo::ResultIterator rt = query.exec();
59 QList<Akonadi::Item > itemList;
63 itemList << Item::fromUrl(rt.url());
66 if (!itemList.empty()) {
67 ItemFetchJob *itemFetchJob =
new ItemFetchJob(itemList);
68 itemFetchJob->fetchScope().fetchFullPayload();
69 connect(itemFetchJob, SIGNAL(finished(KJob*)), SLOT(jobFinished(KJob*)));
71 QListView *listview =
new QListView(widget);
72 const_cast<Emails *
>(
this)->m_emailsModel =
new EmailListModel(listview);
73 listview->setModel(m_emailsModel);
74 listview->setItemDelegate(
new EmailListViewDelegate());
75 listview->setSelectionMode(QAbstractItemView::SingleSelection);
76 listview->setSelectionBehavior(QAbstractItemView::SelectRows);
77 listview->setEditTriggers(QAbstractItemView::NoEditTriggers);
78 layout->addWidget(listview);
80 connect(listview, SIGNAL(doubleClicked(QModelIndex)),
this, SLOT(onEmailDoubleClicked(QModelIndex)));
82 layout->addWidget(
new QLabel(i18n(
"No Emails")));
84 widget->setLayout(layout);
88 void Emails::onEmailDoubleClicked(
const QModelIndex &clicked)
90 QUrl url = m_emailsModel->getItemUrl(clicked.row());
91 QDesktopServices::openUrl(url);
94 void Emails::jobFinished(KJob *job)
97 qDebug() <<
"Error:" << job->errorString();
101 Akonadi::ItemFetchJob *fetchJob = qobject_cast<Akonadi::ItemFetchJob *>(job);
102 const Akonadi::Item::List items = fetchJob->items();
105 foreach (
const Akonadi::Item &item, items) {
106 msg.setContent(item.payloadData());
109 KMime::Headers::Subject *subject = msg.subject();
110 KMime::Headers::Date *date = msg.date();
111 KMime::Content *textContent = msg.textContent();
114 mail.subject = subject->asUnicodeString();
115 mail.date = date->dateTime();
116 mail.body = textContent->body();
117 mail.url = item.url();
119 m_emailsModel->addEmail(mail);
123 QString Emails::label()
const
125 return i18n(
"Mails");
128 int Emails::sortWeight()
const
133 #include "emails.moc"