1
/****************************************************************************
2
* Copyright (C) 2009-2013 by Savoir-Faire Linux *
3
* Author : Jérémy Quentin <jeremy.quentin@savoirfairelinux.com> *
4
* Emmanuel Lepage Vallee <emmanuel.lepage@savoirfairelinux.com> *
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. *
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. *
16
* You should have received a copy of the GNU General Public License *
17
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
18
***************************************************************************/
20
#include "sflphoneengine.h"
23
#include <Plasma/DataContainer>
24
#include <Plasma/Service>
27
#include "../../lib/call.h"
28
#include "../../lib/account.h"
29
#include "../../lib/accountlist.h"
30
#include "../../lib/contact.h"
31
#include "../../lib/dbus/metatypes.h"
32
#include "../../lib/instance_interface_singleton.h"
33
#include "../../lib/configurationmanager_interface_singleton.h"
34
#include "../../lib/callmanager_interface_singleton.h"
35
#include "../../lib/sflphone_const.h"
36
#include "../../klib/akonadibackend.h"
37
#include "../../klib/helperfunctions.h"
38
#include "../../klib/configurationskeleton.h"
39
#include "../../lib/callmodel.h"
40
#include "../../lib/historymodel.h"
41
#include "sflphoneservice.h"
44
CallModel<>* SFLPhoneEngine::m_pModel = nullptr;
47
/*****************************************************************************
51
****************************************************************************/
54
SFLPhoneEngine::SFLPhoneEngine(QObject* parent, const QVariantList& args)
55
: Plasma::DataEngine(parent, args)
59
m_pModel = new CallModel<>();
61
//m_pModel->initHistory();
64
/* SOURCE SIGNAL DESTINATION SLOT */
65
/**/connect(m_pModel , SIGNAL(callStateChanged(Call*)) , this , SLOT(callStateChangedSignal(Call*)) );
66
/**/connect(m_pModel , SIGNAL(callAdded(Call*)) , this , SLOT(callStateChangedSignal(Call*)) );
67
/**/connect(m_pModel , SIGNAL(callStateChanged(Call*)) , this , SLOT(callStateChangedSignal(Call*)) );
68
/**/connect(AkonadiBackend::getInstance(), SIGNAL(collectionChanged()) , this , SLOT(updateCollection()) );
73
/*****************************************************************************
75
* Dateengine internal *
77
****************************************************************************/
79
///Fill a source only when it is called for the first time, then do it asyncroniously
80
bool SFLPhoneEngine::sourceRequestEvent(const QString &name)
83
if ( name == "history" ) { updateHistory(); }
84
else if ( name == "calls" ) { updateCallList(); }
85
else if ( name == "conferences" ) { updateConferenceList(); }
86
else if ( name == "info" ) { updateInfo(); }
87
else if ( name == "accounts" ) { updateAccounts(); }
88
else if ( name == "contacts" ) { updateContacts(); }
89
else if ( name == "bookmark" ) { updateBookmarkList(); }
90
else if ( name.left(7) == "Number:" ) { generateNumberList(name); }
93
return true;//updateSourceEvent(name);
97
bool SFLPhoneEngine::updateSourceEvent(const QString &name)
103
///List all default valid sources, more can be requested dynamically
104
QStringList SFLPhoneEngine::sources() const {
105
QStringList toReturn;
106
toReturn << "calls" << "history" << "conferences" << "info" << "accounts" << "contacts" << "bookmark";
110
///Return the service used for RPC
111
Plasma::Service* SFLPhoneEngine::serviceForSource(const QString &source)
113
if (source != "calls")
116
SFLPhoneService* service = new SFLPhoneService(this);
117
service->setParent(this);
121
/*****************************************************************************
125
****************************************************************************/
128
CallModel<>* SFLPhoneEngine::getModel()
134
/*****************************************************************************
138
****************************************************************************/
140
///Load/Update history
141
void SFLPhoneEngine::updateHistory()
143
CallList list = HistoryModel::getHistory().values();
144
setHistoryCategory(list,HistorySortingMode::Date);
146
foreach (Call* oldCall, list) {
147
HashStringString current;
149
/**/current[ "peerName" ] = oldCall->getPeerName () ;
150
/**/current[ "peerNumber" ] = oldCall->getPeerPhoneNumber() ;
151
/**/current[ "length" ] = oldCall->getStopTimeStamp ().toInt() - oldCall->getStartTimeStamp().toInt();
152
/**/current[ "date" ] = oldCall->getStopTimeStamp () ;
153
/**/current[ "id" ] = oldCall->getCallId () ;
155
if (oldCall->property("section").isValid())
156
current[ "section" ] = oldCall->property("section");
157
setData("history", oldCall->getCallId() , current);
161
///Load/Update calllist
162
void SFLPhoneEngine::updateCallList()
164
//As of KDE 4.8, an empty source are ignored, adding an invisible entry
166
keys << "peerName" << "peerNumber" << "stateName" << "state" << "id";
167
QHash<QString,QVariant> fake;
168
foreach (const QString& key, keys) {
171
setData("calls", "fake",fake );
172
removeAllData("calls");
173
foreach (Call* call, m_pModel->getCalls()) {
174
if ((!m_pModel->isConference(call)) && (call->getState() != CALL_STATE_OVER)) {
175
HashStringString current;
177
/**/current[ "peerName" ] = call->getPeerName ( );
178
/**/current[ "peerNumber" ] = call->getPeerPhoneNumber ( );
179
/**/current[ "stateName" ] = call->toHumanStateName ( );
180
/**/current[ "state" ] = call->getState ( );
181
/**/current[ "id" ] = call->getCallId ( );
183
setData("calls", call->getCallId(), current);
188
///Load/Update bookmark list
189
void SFLPhoneEngine::updateBookmarkList()
191
removeAllData("bookmark");
193
QStringList cl = HistoryModel::getNumbersByPopularity();
194
for (;i < ((cl.size() < 10)?cl.size():10);i++) {
195
QHash<QString,QVariant> pop;
196
Contact* cont = AkonadiBackend::getInstance()->getContactByPhone(cl[i],true);
198
/**/pop["peerName" ] = (cont)?cont->getFormattedName():cl[i];
199
/**/pop["peerNumber" ] = cl[i] ;
200
/**/pop["section" ] = "Popular" ;
201
/**/pop["listPriority" ] = 1000 ;
205
setData("bookmark", QString::number(i), pop);
208
//TODO Wont work for now
209
foreach (const QString& nb, ConfigurationSkeleton::bookmarkList()) {
211
QHash<QString,QVariant> pop;
213
/**/pop["peerName" ] = "TODO" ;
214
/**/pop["peerNumber" ] = nb ;
215
/**/pop["section" ] = '1' ;
216
/**/pop["listPriority" ] = 0 ;
220
setData("bookmark", QString::number(i), pop);
224
///Load/Update conference list (TODO)
225
void SFLPhoneEngine::updateConferenceList()
227
/*foreach (Call* call, m_pModel->getCalls()) {
228
if (m_pModel->isConference(call)) {
229
CallManagerInterface& callManager = CallManagerInterfaceSingleton::getInstance();
230
currentConferences[call->getConfId()] = callManager.getParticipantList(call->getConfId());
231
setData("conferences", call->getConfId(), currentConferences[call->getConfId()]);
236
///Update contact collection
237
void SFLPhoneEngine::updateCollection()
240
typedef QHash<QString,QVariant> SerializedContact;
241
ContactList list = AkonadiBackend::getInstance()->update();
246
const ContactHash hash = HelperFunctions::toHash(list);
247
foreach (const SerializedContact& cont, hash) {
248
if (!m_hContacts[hash.key(cont)].size()) {
249
m_hContacts[hash.key(cont)] = cont;
253
removeAllData("contacts");
255
foreach (const SerializedContact& cont, m_hContacts) {
256
// cont["section"] = "test";
257
setData("contacts", QString::number(i), QVariant(cont));
261
updateBookmarkList();
264
///Dummy implementation of the contact list (TOREMOVE)
265
void SFLPhoneEngine::updateContacts()
267
//As of KDE 4.8, an empty source is ignored, adding an invisible entry
269
keys << "nickName" << "firstName" << "secondName" << "formattedName" << "organization" <<
270
"Uid" << "preferredEmail" << "type" << "group" << "department";
272
QHash<QString,QVariant> fake;
273
foreach(const QString& key,keys) {
276
setData("contacts", "fake",fake );
279
///Update other information
280
void SFLPhoneEngine::updateInfo()
282
setData("info", I18N_NOOP("Current_account"), AccountList::getCurrentAccount()->getAccountId());
285
///Load/Update account list
286
void SFLPhoneEngine::updateAccounts()
288
const QVector<Account*>& list = AccountList::getInstance()->getAccounts();
289
foreach(Account* a,list) {
290
if (dynamic_cast<Account*>(a)) {
291
QHash<QString,QVariant> acc;
292
acc[ "id" ] = a->getAccountId() ;
293
acc[ "alias"] = a->getAccountDetail(ACCOUNT_ALIAS);
294
setData("accounts", QString::number(rand()) , acc);
300
/*****************************************************************************
304
****************************************************************************/
307
void SFLPhoneEngine::generateNumberList(QString name)
309
QString contactUid = name.right(name.size()-7);
310
qDebug() << "LOOKING FOR " << contactUid;
311
Contact* cont = AkonadiBackend::getInstance()->getContactByUid(contactUid);
313
foreach(Contact::PhoneNumber* num,cont->getPhoneNumbers()) {
314
QHash<QString,QVariant> hash;
315
hash[ "number" ] = num->getNumber() ;
316
hash[ "type" ] = num->getType() ;
317
setData(name, QString::number(rand()) , hash);
321
kDebug() << "Contact not found";
325
/*****************************************************************************
329
****************************************************************************/
331
///When call state change
332
void SFLPhoneEngine::callStateChangedSignal(Call* call)
338
///When incoming call
339
void SFLPhoneEngine::incomingCallSignal(Call* call)
345
///When incoming messge
346
void SFLPhoneEngine::incomingMessageSignal(const QString& accountId, const QString& message)
353
///When voicemail notify
354
void SFLPhoneEngine::voiceMailNotifySignal(const QString& accountId, int count)
361
K_EXPORT_PLASMA_DATAENGINE(sflphone, SFLPhoneEngine)