~ubuntu-branches/ubuntu/vivid/sflphone/vivid

« back to all changes in this revision

Viewing changes to kde/src/klib/dataengine/sflphoneengine.cpp

  • Committer: Package Import Robot
  • Author(s): Mark Purcell
  • Date: 2013-06-30 11:40:56 UTC
  • mfrom: (4.1.18 saucy-proposed)
  • Revision ID: package-import@ubuntu.com-20130630114056-0np50jkyqo6vnmii
Tags: 1.2.3-2
* changeset_r92d62cfc54732bbbcfff2b1d36c096b120b981a5.diff 
  - fixes automatic endian detection 
* Update Vcs: fixes vcs-field-not-canonical

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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> *
 
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 General Public License      *
 
17
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.  *
 
18
 ***************************************************************************/
 
19
 
 
20
#include "sflphoneengine.h"
 
21
 
 
22
//KDE
 
23
#include <Plasma/DataContainer>
 
24
#include <Plasma/Service>
 
25
 
 
26
//SFLPhone
 
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"
 
42
 
 
43
//Static
 
44
CallModel<>* SFLPhoneEngine::m_pModel = nullptr;
 
45
 
 
46
 
 
47
/*****************************************************************************
 
48
 *                                                                           *
 
49
 *                               Constructor                                 *
 
50
 *                                                                           *
 
51
 ****************************************************************************/
 
52
 
 
53
///Constructor
 
54
SFLPhoneEngine::SFLPhoneEngine(QObject* parent, const QVariantList& args)
 
55
    : Plasma::DataEngine(parent, args)
 
56
{
 
57
   Q_UNUSED(args)
 
58
   if (not m_pModel) {
 
59
      m_pModel = new CallModel<>();
 
60
      m_pModel->initCall();
 
61
      //m_pModel->initHistory();
 
62
   }
 
63
 
 
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())            );
 
69
   /*                                                                                                                       */
 
70
}
 
71
 
 
72
 
 
73
/*****************************************************************************
 
74
 *                                                                           *
 
75
 *                           Dateengine internal                             *
 
76
 *                                                                           *
 
77
 ****************************************************************************/
 
78
 
 
79
///Fill a source only when it is called for the first time, then do it asyncroniously
 
80
bool SFLPhoneEngine::sourceRequestEvent(const QString &name)
 
81
{
 
82
   /*                SOURCE                        CALLBACK         */
 
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); }
 
91
   /*                                                               */
 
92
   
 
93
   return true;//updateSourceEvent(name);
 
94
}
 
95
 
 
96
///Not used
 
97
bool SFLPhoneEngine::updateSourceEvent(const QString &name)
 
98
{
 
99
   Q_UNUSED(name)
 
100
   return true;
 
101
}
 
102
 
 
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";
 
107
   return toReturn;
 
108
}
 
109
 
 
110
///Return the service used for RPC
 
111
Plasma::Service* SFLPhoneEngine::serviceForSource(const QString &source)
 
112
{
 
113
    if (source != "calls")
 
114
      return 0;
 
115
 
 
116
    SFLPhoneService* service = new SFLPhoneService(this);
 
117
    service->setParent(this);
 
118
    return service;
 
119
}
 
120
 
 
121
/*****************************************************************************
 
122
 *                                                                           *
 
123
 *                                  Getters                                  *
 
124
 *                                                                           *
 
125
 ****************************************************************************/
 
126
 
 
127
///Return the model
 
128
CallModel<>* SFLPhoneEngine::getModel()
 
129
{
 
130
   return m_pModel;
 
131
}
 
132
 
 
133
 
 
134
/*****************************************************************************
 
135
 *                                                                           *
 
136
 *                                Callbacks                                  *
 
137
 *                                                                           *
 
138
 ****************************************************************************/
 
139
 
 
140
///Load/Update history
 
141
void SFLPhoneEngine::updateHistory()
 
142
{
 
143
   CallList list = HistoryModel::getHistory().values();
 
144
   setHistoryCategory(list,HistorySortingMode::Date);
 
145
 
 
146
   foreach (Call* oldCall, list) {
 
147
      HashStringString current;
 
148
      /*             KEY                   VALUE                                                               */
 
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         ()                                               ;
 
154
      /*                                                                                                       */
 
155
      if (oldCall->property("section").isValid())
 
156
         current[ "section" ] = oldCall->property("section");
 
157
      setData("history", oldCall->getCallId() , current);
 
158
   }
 
159
}
 
160
 
 
161
///Load/Update calllist
 
162
void SFLPhoneEngine::updateCallList()
 
163
{
 
164
   //As of KDE 4.8, an empty source are ignored, adding an invisible entry
 
165
   QStringList keys;
 
166
   keys << "peerName" << "peerNumber" << "stateName" << "state" << "id";
 
167
   QHash<QString,QVariant> fake;
 
168
   foreach (const QString& key, keys) {
 
169
      fake[key] = "";
 
170
   }
 
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;
 
176
         /*               KEY                     VALUE              */
 
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          ( );
 
182
         /*                                                          */
 
183
         setData("calls", call->getCallId(), current);
 
184
      }
 
185
   }
 
186
}
 
187
 
 
188
///Load/Update bookmark list
 
189
void SFLPhoneEngine::updateBookmarkList()
 
190
{
 
191
   removeAllData("bookmark");
 
192
   int i=0;
 
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);
 
197
      /*           KEY                          VALUE                */
 
198
      /**/pop["peerName"     ] = (cont)?cont->getFormattedName():cl[i];
 
199
      /**/pop["peerNumber"   ] = cl[i]                                ;
 
200
      /**/pop["section"      ] = "Popular"                            ;
 
201
      /**/pop["listPriority" ] = 1000                                 ;
 
202
      /**/pop["id"           ] = i                                    ;
 
203
      /*                                                             */
 
204
      
 
205
      setData("bookmark", QString::number(i), pop);
 
206
   }
 
207
 
 
208
   //TODO Wont work for now
 
209
   foreach (const QString& nb, ConfigurationSkeleton::bookmarkList()) {
 
210
      i++;
 
211
      QHash<QString,QVariant> pop;
 
212
      /*             KEY          VALUE */
 
213
      /**/pop["peerName"     ] = "TODO"  ;
 
214
      /**/pop["peerNumber"   ] = nb      ;
 
215
      /**/pop["section"      ] = '1'     ;
 
216
      /**/pop["listPriority" ] = 0       ;
 
217
      /**/pop["id"           ] = i       ;
 
218
      /*                                */
 
219
      
 
220
      setData("bookmark", QString::number(i), pop);
 
221
   }
 
222
}
 
223
 
 
224
///Load/Update conference list (TODO)
 
225
void SFLPhoneEngine::updateConferenceList()
 
226
{
 
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()]);
 
232
      }
 
233
   }*/
 
234
}
 
235
 
 
236
///Update contact collection
 
237
void SFLPhoneEngine::updateCollection()
 
238
{
 
239
   
 
240
   typedef QHash<QString,QVariant> SerializedContact;
 
241
   ContactList list = AkonadiBackend::getInstance()->update();
 
242
   
 
243
   if (!list.size())
 
244
      return;
 
245
   
 
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;
 
250
      }
 
251
   }
 
252
   
 
253
   removeAllData("contacts");
 
254
   int i=0;
 
255
   foreach (const SerializedContact& cont, m_hContacts) {
 
256
//       cont["section"] = "test";
 
257
      setData("contacts", QString::number(i), QVariant(cont));
 
258
      i++;
 
259
   }
 
260
   
 
261
   updateBookmarkList();
 
262
}
 
263
 
 
264
///Dummy implementation of the contact list (TOREMOVE)
 
265
void SFLPhoneEngine::updateContacts()
 
266
{
 
267
   //As of KDE 4.8, an empty source is ignored, adding an invisible entry
 
268
   QStringList keys;
 
269
   keys << "nickName" << "firstName"      << "secondName" << "formattedName" << "organization" <<
 
270
            "Uid"     << "preferredEmail" << "type"       << "group"         << "department";
 
271
   
 
272
   QHash<QString,QVariant> fake;
 
273
   foreach(const QString& key,keys) {
 
274
      fake[key].clear();
 
275
   }
 
276
   setData("contacts", "fake",fake );
 
277
}
 
278
 
 
279
///Update other information
 
280
void SFLPhoneEngine::updateInfo()
 
281
{
 
282
   setData("info", I18N_NOOP("Current_account"), AccountList::getCurrentAccount()->getAccountId());
 
283
}
 
284
 
 
285
///Load/Update account list
 
286
void SFLPhoneEngine::updateAccounts()
 
287
{
 
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);
 
295
      }
 
296
   }
 
297
}
 
298
 
 
299
 
 
300
/*****************************************************************************
 
301
 *                                                                           *
 
302
 *                                 Mutators                                  *
 
303
 *                                                                           *
 
304
 ****************************************************************************/
 
305
 
 
306
///Generate a number
 
307
void SFLPhoneEngine::generateNumberList(QString name)
 
308
{
 
309
   QString contactUid = name.right(name.size()-7);
 
310
   qDebug() << "LOOKING FOR " << contactUid;
 
311
   Contact* cont = AkonadiBackend::getInstance()->getContactByUid(contactUid);
 
312
   if (cont) {
 
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);
 
318
      }
 
319
   }
 
320
   else {
 
321
      kDebug() << "Contact not found";
 
322
   }
 
323
}
 
324
 
 
325
/*****************************************************************************
 
326
 *                                                                           *
 
327
 *                                   Slots                                   *
 
328
 *                                                                           *
 
329
 ****************************************************************************/
 
330
 
 
331
///When call state change
 
332
void SFLPhoneEngine::callStateChangedSignal(Call* call)
 
333
{
 
334
   Q_UNUSED(call)
 
335
   updateCallList();
 
336
}
 
337
 
 
338
///When incoming call
 
339
void SFLPhoneEngine::incomingCallSignal(Call* call)
 
340
{
 
341
   Q_UNUSED(call)
 
342
   updateCallList();
 
343
}
 
344
 
 
345
///When incoming messge
 
346
void SFLPhoneEngine::incomingMessageSignal(const QString& accountId, const QString& message)
 
347
{
 
348
   Q_UNUSED(accountId)
 
349
   Q_UNUSED(message)
 
350
   //TODO
 
351
}
 
352
 
 
353
///When voicemail notify
 
354
void SFLPhoneEngine::voiceMailNotifySignal(const QString& accountId, int count)
 
355
{
 
356
   Q_UNUSED(accountId)
 
357
   Q_UNUSED(count)
 
358
   //TODO
 
359
}
 
360
 
 
361
K_EXPORT_PLASMA_DATAENGINE(sflphone, SFLPhoneEngine)