1 /*
2  * static methods on the KTp namespace
3  *
4  * Copyright (C) 2013 David Edmundson <kde@davidedmundson.co.uk>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  */
20 
21 #include "core.h"
22 
23 #ifdef HAVE_KPEOPLE
24 #include <QDBusMessage>
25 #include <QDBusReply>
26 #endif
27 
28 #include <QtGlobal>
29 #include <TelepathyQt/AccountManager>
30 #include <KTp/global-contact-manager.h>
31 #include "contact-factory.h"
32 #include "account-factory_p.h"
33 
34 class CorePrivate
35 {
36 public:
37     CorePrivate();
38     bool m_kPeopleEnabled;
39     Tp::AccountFactoryPtr m_accountFactory;
40     Tp::ConnectionFactoryPtr m_connectionFactory;
41     Tp::ContactFactoryPtr m_contactFactory;
42     Tp::ChannelFactoryPtr m_channelFactory ;
43 
44     Tp::AccountManagerPtr m_accountManager;
45     KTp::GlobalContactManager *m_contactManager;
46 };
47 
CorePrivate()48 CorePrivate::CorePrivate()
49     : m_kPeopleEnabled(false),
50       m_contactManager(nullptr)
51 {
52     //if built with kpeople support, enable it
53     #ifdef HAVE_KPEOPLE
54     m_kPeopleEnabled = true;
55     #endif
56 
57     m_accountFactory = KTp::AccountFactory::create(QDBusConnection::sessionBus(),
58                                                                     Tp::Features() << Tp::Account::FeatureCore
59                                                                                    << Tp::Account::FeatureCapabilities
60                                                                                    << Tp::Account::FeatureProfile);
61 
62     m_connectionFactory = Tp::ConnectionFactory::create(QDBusConnection::sessionBus(),
63                                                                                Tp::Features() << Tp::Connection::FeatureCore
64                                                                                               << Tp::Connection::FeatureConnected
65                                                                                               << Tp::Connection::FeatureSelfContact);
66 
67     m_contactFactory = KTp::ContactFactory::create(Tp::Features()  << Tp::Contact::FeatureAlias
68                                                                                        << Tp::Contact::FeatureSimplePresence
69                                                                                        << Tp::Contact::FeatureCapabilities
70                                                                                        << Tp::Contact::FeatureClientTypes
71                                                                                        << Tp::Contact::FeatureAvatarData);
72 
73     m_channelFactory = Tp::ChannelFactory::create(QDBusConnection::sessionBus());
74 }
75 
Q_GLOBAL_STATIC(CorePrivate,s_instance)76 Q_GLOBAL_STATIC(CorePrivate, s_instance)
77 
78 bool KTp::kpeopleEnabled()
79 {
80     return s_instance->m_kPeopleEnabled;
81 }
82 
accountFactory()83 Tp::AccountFactoryConstPtr KTp::accountFactory()
84 {
85     return s_instance->m_accountFactory;
86 }
87 
connectionFactory()88 Tp::ConnectionFactoryConstPtr KTp::connectionFactory()
89 {
90     return s_instance->m_connectionFactory;
91 }
92 
channelFactory()93 Tp::ChannelFactoryConstPtr KTp::channelFactory()
94 {
95     return s_instance->m_channelFactory;
96 }
97 
contactFactory()98 Tp::ContactFactoryConstPtr KTp::contactFactory()
99 {
100     return s_instance->m_contactFactory;
101 }
102 
accountManager()103 Tp::AccountManagerPtr KTp::accountManager()
104 {
105     if (!s_instance->m_accountManager) {
106         s_instance->m_accountManager = Tp::AccountManager::create(QDBusConnection::sessionBus(),
107                                                    KTp::accountFactory(),
108                                                    KTp::connectionFactory(),
109                                                    KTp::channelFactory(),
110                                                    KTp::contactFactory());
111     }
112     return s_instance->m_accountManager;
113 }
114 
contactManager()115 KTp::GlobalContactManager* KTp::contactManager()
116 {
117     if (!s_instance->m_contactManager) {
118         s_instance->m_contactManager = new KTp::GlobalContactManager(KTp::accountManager(), nullptr);
119     }
120 
121     return s_instance->m_contactManager;
122 }
123