1/*
2 * Copyright (C) 2007, 2008, 2009, 2010 Ivan Cukic <ivan.cukic(at)kde.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser/Library General Public License version 2,
6 * or (at your option) any later version, as published by the Free
7 * Software Foundation
8 *
9 * This program 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
12 * GNU Lesser/Library General Public License for more details
13 *
14 * You should have received a copy of the GNU Lesser/Library General Public
15 * License along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19
20#include "MessagesKmail.h"
21
22#include <QDBusConnection>
23#include <QDBusInterface>
24#include <QDBusConnectionInterface>
25
26#include <KIcon>
27#include <KRun>
28#include <KToolInvocation>
29#include <KStandardDirs>
30
31#include "Logger.h"
32
33#include "config-lancelot-datamodels.h"
34
35#ifdef LANCELOT_THE_COMPILER_DOESNT_NEED_TO_PROCESS_THIS
36// just in case messages:
37I18N_NOOP("Unread messages");
38I18N_NOOP("Unable to find Kontact");
39I18N_NOOP("Start Akonadi server");
40I18N_NOOP("Akonadi server is not running");
41#endif
42
43#ifndef LANCELOT_DATAMODELS_HAS_PIMLIBS
44
45#warning "Pimlibs are not present"
46
47 #define DummyModelClassName MessagesKmail
48 #define DummyModelInit \
49 setSelfTitle(i18n("Unread messages")); \
50 setSelfIcon(KIcon("kmail")); \
51 if (!addService("kontact") && !addService("kmail")) { \
52 add(i18n("Unable to find Kontact"), "", \
53 KIcon("application-x-executable"), \
54 QVariant("http://kontact.kde.org")); \
55 }
56
57 #include "DummyModel_p.cpp"
58
59 namespace Lancelot {
60 namespace Models {
61
62 class MessagesKmail::Private {
63 void fetchEmailCollectionsDone(KJob * job)
64 {
65 Q_UNUSED(job);
66 }
67 };
68
69 void MessagesKmail::updateLater()
70 {
71 }
72
73 void MessagesKmail::update()
74 {
75 }
76
77 QString MessagesKmail::selfShortTitle() const
78 {
79 return QString();
80 }
81 }
82 }
83
84 #undef DummyModelClassName
85 #undef DummyModelInit
86
87#else
88
89// We have kdepimlibs
90
91#include <KJob>
92#include <Akonadi/Collection>
93#include <Akonadi/CollectionFetchJob>
94#include <Akonadi/CollectionStatistics>
95#include <Akonadi/CollectionStatisticsJob>
96#include <Akonadi/EntityDisplayAttribute>
97#include "MessagesKmail_p.h"
98
99namespace Lancelot {
100namespace Models {
101
102MessagesKmail::Private::Private(MessagesKmail * parent)
103 : monitor(NULL), unread(0), q(parent)
104{
105}
106
107void MessagesKmail::Private::fetchEmailCollectionsDone(KJob * job)
108{
109 q->clear();
110 unread = 0;
111
112 q->setEmitInhibited(true);
113
114 if ( job->error() ) {
115 kDebug() << "Job Error:" << job->errorString();
116
117 } else {
118 Akonadi::CollectionFetchJob * cjob =
119 static_cast < Akonadi::CollectionFetchJob * > ( job );
120
121 foreach (const Akonadi::Collection & collection, cjob->collections()) {
122 if (collection.contentMimeTypes().contains("message/rfc822")) {
123 int unreadCount = collection.statistics().unreadCount();
124
125 if (unreadCount) {
126 q->add(
127 i18nc("Directory name (number of unread messages)",
128 "%1 (%2)",
129 collection.name(),
130 unreadCount),
131 QString::null,
132 entityIcon(collection),
133 collection.url()
134 );
135 unread += unreadCount;
136 }
137 }
138 }
139 }
140
141 if (q->size() == 0) {
142 q->add(i18n("No unread mail"), "", KIcon("mail-folder-inbox"), QVariant());
143 }
144
145 if (unread) {
146 q->setSelfTitle(i18nc("Unread messages (number of unread messages)",
147 "Unread messages (%1)", QString::number(unread)));
148 } else {
149 q->setSelfTitle(i18n("Unread messages"));
150 }
151
152 q->setEmitInhibited(false);
153 q->updated();
154}
155
156KIcon MessagesKmail::Private::entityIcon(const Akonadi::Collection & collection) const
157{
158 Akonadi::EntityDisplayAttribute * displayAttribute =
159 static_cast < Akonadi::EntityDisplayAttribute * > (
160 collection.attribute < Akonadi::EntityDisplayAttribute > ()
161 );
162
163 if (displayAttribute) {
164 return displayAttribute->icon();
165 }
166
167 return KIcon("mail-folder-inbox");
168}
169
170MessagesKmail::MessagesKmail()
171 : d(new Private(this))
172{
173 setSelfTitle(i18n("Unread messages"));
174 setSelfIcon(KIcon("kmail"));
175
176 d->monitor = new Akonadi::Monitor();
177 d->monitor->setCollectionMonitored(Akonadi::Collection::root());
178 d->monitor->fetchCollection(true);
179
180 connect(d->monitor, SIGNAL(collectionAdded(Akonadi::Collection,Akonadi::Collection)),
181 this, SLOT(updateLater()));
182 connect(d->monitor, SIGNAL(collectionRemoved(Akonadi::Collection)),
183 this, SLOT(updateLater()));
184 connect(d->monitor, SIGNAL(collectionChanged(Akonadi::Collection)),
185 this, SLOT(updateLater()));
186 connect(d->monitor, SIGNAL(collectionStatisticsChanged(Akonadi::Collection::Id,Akonadi::CollectionStatistics)),
187 this, SLOT(updateLater()));
188
189 connect(Akonadi::ServerManager::self(), SIGNAL(stateChanged(Akonadi::ServerManager::State)),
190 this, SLOT(updateLater()));
191
192 load();
193}
194
195MessagesKmail::~MessagesKmail()
196{
197 delete d->monitor;
198 delete d;
199}
200
201void MessagesKmail::updateLater()
202{
203 QTimer::singleShot(200, this, SLOT(update()));
204}
205
206void MessagesKmail::update()
207{
208 load();
209}
210
211void MessagesKmail::activate(int index)
212{
213 Q_UNUSED(index);
214
215 // TODO: This could be a bit prettier
216 if (index == 0 && d->unread == 0 && !Akonadi::ServerManager::isRunning()) {
217 Akonadi::ServerManager::start();
218 return;
219 }
220
221 if (QDBusConnection::sessionBus().interface()->isServiceRegistered("org.kde.kmail")) {
222 QDBusInterface kmailInterface("org.kde.kmail", "/KMail", "org.kde.kmail.kmail");
223 kmailInterface.call("openReader");
224 } else {
225 KToolInvocation::startServiceByDesktopName("kmail");
226 }
227}
228
229QString MessagesKmail::selfShortTitle() const
230{
231 if (d->unread) {
232 return QString::number(d->unread);
233 } else {
234 return QString();
235 }
236}
237
238void MessagesKmail::load()
239{
240 kDebug();
241
242 if (!Akonadi::ServerManager::isRunning()) {
243 clear();
244 d->unread = 0;
245 add(i18n("Start Akonadi server"), i18n("Akonadi server is not running"), KIcon("akonadi"), QVariant("start-akonadi"));
246 return;
247
248 }
249
250 Akonadi::Collection emailCollection(Akonadi::Collection::root());
251 emailCollection.setContentMimeTypes(QStringList() << "message/rfc822");
252
253 Akonadi::CollectionFetchScope scope;
254 scope.setIncludeStatistics(true);
255 scope.setContentMimeTypes(QStringList() << "message/rfc822");
256 scope.setAncestorRetrieval(Akonadi::CollectionFetchScope::All);
257
258 Akonadi::CollectionFetchJob * fetch = new Akonadi::CollectionFetchJob(
259 emailCollection, Akonadi::CollectionFetchJob::Recursive);
260 fetch->setFetchScope(scope);
261
262 connect(fetch, SIGNAL(result(KJob*)),
263 d, SLOT(fetchEmailCollectionsDone(KJob*)));
264}
265
266} // namespace Models
267} // namespace Lancelot
268
269#endif
270
271