1/*
2 Copyright (c) 2011 Stephen Kelly <steveire@gmail.com>
3
4 This library is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Library General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or (at your
7 option) any later version.
8
9 This library is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12 License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to the
16 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 02110-1301, USA.
18*/
19
20#include "changenotificationdependenciesfactory_p.h"
21#include "dbusconnectionpool.h"
22#include "notificationsource_p.h"
23#include "notificationsourceinterface.h"
24#include "notificationmanagerinterface.h"
25#include "changemediator_p.h"
26#include "servermanager.h"
27
28#include <KComponentData>
29#include <KGlobal>
30#include <KRandom>
31#include <qdbusextratypes.h>
32
33using namespace Akonadi;
34
35NotificationSource *ChangeNotificationDependenciesFactory::createNotificationSource(QObject *parent)
36{
37 if (!Akonadi::ServerManager::self()->isRunning()) {
38 return 0;
39 }
40
41 org::freedesktop::Akonadi::NotificationManager *manager =
42 new org::freedesktop::Akonadi::NotificationManager(
43 ServerManager::serviceName(Akonadi::ServerManager::Server),
44 QLatin1String("/notifications"),
45 DBusConnectionPool::threadConnection());
46
47 if (!manager) {
48 // :TODO: error handling
49 return 0;
50 }
51
52 const QString name =
53 QString::fromLatin1("%1_%2_%3").arg(
54 KGlobal::mainComponent().componentName(),
55 QString::number(QCoreApplication::applicationPid()),
56 KRandom::randomString(6));
57 QDBusObjectPath p = manager->subscribeV2(name, true);
58 const bool validError = manager->lastError().isValid();
59 if (validError) {
60 kWarning() << manager->lastError().name() << manager->lastError().message();
61 // :TODO: What to do?
62 delete manager;
63 return 0;
64 }
65 delete manager;
66 org::freedesktop::Akonadi::NotificationSource *notificationSource =
67 new org::freedesktop::Akonadi::NotificationSource(
68 ServerManager::serviceName(Akonadi::ServerManager::Server),
69 p.path(),
70 DBusConnectionPool::threadConnection(), parent);
71
72 if (!notificationSource) {
73 // :TODO: error handling
74 return 0;
75 }
76 return new NotificationSource(notificationSource);
77}
78
79QObject *ChangeNotificationDependenciesFactory::createChangeMediator(QObject *parent)
80{
81 Q_UNUSED(parent);
82 return ChangeMediator::instance();
83}
84
85CollectionCache *ChangeNotificationDependenciesFactory::createCollectionCache(int maxCapacity, Session *session)
86{
87 return new CollectionCache(maxCapacity, session);
88}
89
90ItemCache *ChangeNotificationDependenciesFactory::createItemCache(int maxCapacity, Session *session)
91{
92 return new ItemCache(maxCapacity, session);
93}
94
95ItemListCache *ChangeNotificationDependenciesFactory::createItemListCache(int maxCapacity, Session *session)
96{
97 return new ItemListCache(maxCapacity, session);
98}
99
100TagListCache *ChangeNotificationDependenciesFactory::createTagListCache(int maxCapacity, Session *session)
101{
102 return new TagListCache(maxCapacity, session);
103}
104