• Skip to content
  • Skip to link menu
KDE API Documentation - emails.cpp Source File (KPeople)
  • KDE Home
  • Contact Us
 

KPeople

  • frameworks
  • kpeople
  • src
  • widgets
  • plugins
emails.cpp
1 /*
2  Copyright 2014 Nilesh Suthar <nileshsuthar@live.in>
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Lesser General Public
6  License as published by the Free Software Foundation; either
7  version 2.1 of the License, or (at your option) any later version.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Lesser General Public License for more details.
13 
14  You should have received a copy of the GNU Lesser General Public
15  License along with this library; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 
19 #include "emails.h"
20 #include "plugins/emaillistmodel.h"
21 #include "plugins/emaillistviewdelegate.h"
22 
23 #include <QDebug>
24 #include <QLabel>
25 #include <QListView>
26 #include <QVBoxLayout>
27 
28 #include <KMimeType>
29 #include <KMime/Message>
30 #include <QDesktopServices>
31 #include <KLocalizedString>
32 
33 #include <baloo/query.h>
34 #include <baloo/resultiterator.h>
35 
36 #include <Akonadi/Item>
37 #include <Akonadi/ItemFetchJob>
38 #include <Akonadi/ItemFetchScope>
39 
40 using namespace Akonadi;
41 
42 Emails::Emails(QObject *parent): AbstractFieldWidgetFactory(parent)
43 {
44 }
45 
46 QWidget *Emails::createDetailsWidget(const AbstractContact::Ptr &person, const AbstractContact::List &contacts, QWidget *parent) const
47 {
48  Q_UNUSED(contacts);
49  QWidget *widget = new QWidget(parent);
50 
51  QVBoxLayout *layout = new QVBoxLayout(widget);
52  layout->setContentsMargins(0, 0, 0, 0);
53 
54  //Fetches emails from Baloo using person preferredEmail
55  Baloo::Query query;
56  query.setSearchString(person.preferredEmail());
57  query.setType("Email");
58  Baloo::ResultIterator rt = query.exec();
59  QList<Akonadi::Item > itemList;
60 
61  while (rt.next()) {
62  //Complete msg is not return so fetches the complete mail from akonadi
63  itemList << Item::fromUrl(rt.url());
64  }
65 
66  if (!itemList.empty()) {
67  ItemFetchJob *itemFetchJob = new ItemFetchJob(itemList);
68  itemFetchJob->fetchScope().fetchFullPayload();
69  connect(itemFetchJob, SIGNAL(finished(KJob*)), SLOT(jobFinished(KJob*)));
70 
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);
79 
80  connect(listview, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(onEmailDoubleClicked(QModelIndex)));
81  } else {
82  layout->addWidget(new QLabel(i18n("No Emails")));
83  }
84  widget->setLayout(layout);
85  return widget;
86 }
87 
88 void Emails::onEmailDoubleClicked(const QModelIndex &clicked)
89 {
90  QUrl url = m_emailsModel->getItemUrl(clicked.row());
91  QDesktopServices::openUrl(url);
92 }
93 
94 void Emails::jobFinished(KJob *job)
95 {
96  if (job->error()) {
97  qDebug() << "Error:" << job->errorString();
98  return;
99  }
100 
101  Akonadi::ItemFetchJob *fetchJob = qobject_cast<Akonadi::ItemFetchJob *>(job);
102  const Akonadi::Item::List items = fetchJob->items();
103  KMime::Message msg;
104 
105  foreach (const Akonadi::Item &item, items) {
106  msg.setContent(item.payloadData());
107  msg.setFrozen(true);
108  msg.parse();
109  KMime::Headers::Subject *subject = msg.subject();
110  KMime::Headers::Date *date = msg.date();
111  KMime::Content *textContent = msg.textContent();
112 
113  EmailItem mail;
114  mail.subject = subject->asUnicodeString();
115  mail.date = date->dateTime();
116  mail.body = textContent->body();
117  mail.url = item.url();
118 
119  m_emailsModel->addEmail(mail);
120  }
121 }
122 
123 QString Emails::label() const
124 {
125  return i18n("Mails");
126 }
127 
128 int Emails::sortWeight() const
129 {
130  return 0;
131 }
132 
133 #include "emails.moc"
Akonadi
This file is part of the KDE documentation.
Documentation copyright © 1996-2015 The KDE developers.
Generated on Fri Feb 13 2015 15:16:39 by doxygen 1.8.9.1 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KPeople

Skip menu "KPeople"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • File List

Class Picker

Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal