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

« back to all changes in this revision

Viewing changes to kde/src/lib/accountlist.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
//Parent
 
21
#include "accountlist.h"
 
22
 
 
23
//SFLPhone
 
24
#include "sflphone_const.h"
 
25
 
 
26
//SFLPhone library
 
27
#include "configurationmanager_interface_singleton.h"
 
28
#include "callmanager_interface_singleton.h"
 
29
 
 
30
AccountList* AccountList::m_spAccountList   = nullptr;
 
31
QString      AccountList::m_sPriorAccountId = ""     ;
 
32
 
 
33
QVariant AccountListNoCheckProxyModel::data(const QModelIndex& index,int role ) const
 
34
{
 
35
   if (role == Qt::CheckStateRole) {
 
36
      return QVariant();
 
37
   }
 
38
   return AccountList::getInstance()->data(index,role);
 
39
}
 
40
bool AccountListNoCheckProxyModel::setData( const QModelIndex& index, const QVariant &value, int role)
 
41
{
 
42
   return AccountList::getInstance()->setData(index,value,role);
 
43
}
 
44
Qt::ItemFlags AccountListNoCheckProxyModel::flags (const QModelIndex& index) const
 
45
{
 
46
   return AccountList::getInstance()->flags(index);
 
47
}
 
48
int AccountListNoCheckProxyModel::rowCount(const QModelIndex& parent ) const
 
49
{
 
50
   return AccountList::getInstance()->rowCount(parent);
 
51
}
 
52
 
 
53
///Constructors
 
54
AccountList::AccountList(QStringList & _accountIds) : m_pColorVisitor(nullptr),m_pDefaultAccount(nullptr)
 
55
{
 
56
   m_pAccounts = new QVector<Account*>();
 
57
   for (int i = 0; i < _accountIds.size(); ++i) {
 
58
      Account* a = Account::buildExistingAccountFromId(_accountIds[i]);
 
59
      (*m_pAccounts) += a;
 
60
      emit dataChanged(index(size()-1,0),index(size()-1,0));
 
61
      connect(a,SIGNAL(changed(Account*)),this,SLOT(accountChanged(Account*)));
 
62
   }
 
63
   CallManagerInterface&          callManager          = CallManagerInterfaceSingleton::getInstance();
 
64
   ConfigurationManagerInterface& configurationManager = ConfigurationManagerInterfaceSingleton::getInstance();
 
65
 
 
66
   connect(&callManager         , SIGNAL(registrationStateChanged(QString,QString,int)) ,this,SLOT(accountChanged(QString,QString,int)));
 
67
   connect(&configurationManager, SIGNAL(accountsChanged())                             ,this,SLOT(updateAccounts())                   );
 
68
}
 
69
 
 
70
///Constructors
 
71
///@param fill Whether to fill the list with accounts from configurationManager or not.
 
72
AccountList::AccountList(bool fill) : m_pColorVisitor(nullptr),m_pDefaultAccount(nullptr)
 
73
{
 
74
   m_pAccounts = new QVector<Account *>();
 
75
   if(fill)
 
76
      updateAccounts();
 
77
   CallManagerInterface& callManager = CallManagerInterfaceSingleton::getInstance();
 
78
   ConfigurationManagerInterface& configurationManager = ConfigurationManagerInterfaceSingleton::getInstance();
 
79
 
 
80
   connect(&callManager         , SIGNAL(registrationStateChanged(QString,QString,int)),this,SLOT(accountChanged(QString,QString,int)));
 
81
   connect(&configurationManager, SIGNAL(accountsChanged())                            ,this,SLOT(updateAccounts())                   );
 
82
}
 
83
 
 
84
///Destructor
 
85
AccountList::~AccountList()
 
86
{
 
87
   foreach(Account* a,*m_pAccounts) {
 
88
      delete a;
 
89
   }
 
90
   delete m_pAccounts;
 
91
}
 
92
 
 
93
///Singleton
 
94
AccountList* AccountList::getInstance()
 
95
{
 
96
   if (not m_spAccountList) {
 
97
      m_spAccountList = new AccountList(true);
 
98
   }
 
99
   return m_spAccountList;
 
100
}
 
101
 
 
102
///Static destructor
 
103
void AccountList::destroy()
 
104
{
 
105
   if (m_spAccountList)
 
106
      delete m_spAccountList;
 
107
   m_spAccountList = nullptr;
 
108
}
 
109
 
 
110
///Account status changed
 
111
void AccountList::accountChanged(const QString& account,const QString& state, int code)
 
112
{
 
113
   Q_UNUSED(code)
 
114
   qDebug() << "Account status changed";
 
115
   Account* a = getAccountById(account);
 
116
   if (!a) {
 
117
      ConfigurationManagerInterface& configurationManager = ConfigurationManagerInterfaceSingleton::getInstance();
 
118
      QStringList accountIds = configurationManager.getAccountList().value();
 
119
      for (int i = 0; i < accountIds.size(); ++i) {
 
120
         if (!getAccountById(accountIds[i])) {
 
121
            Account* a = Account::buildExistingAccountFromId(accountIds[i]);
 
122
            m_pAccounts->insert(i, a);
 
123
            connect(a,SIGNAL(changed(Account*)),this,SLOT(accountChanged(Account*)));
 
124
            emit dataChanged(index(i,0),index(size()-1));
 
125
         }
 
126
      }
 
127
      foreach (Account* a, *m_pAccounts) {
 
128
//          if (!dynamic_cast<Account*>(a)) { //If an account is being created while updating it may crash or remove it
 
129
            int idx =accountIds.indexOf(a->getAccountId());
 
130
            if (idx == -1 && (a->currentState() == READY || a->currentState() == REMOVED)) {
 
131
               m_pAccounts->remove(idx);
 
132
               emit dataChanged(index(idx - 1, 0), index(m_pAccounts->size()-1, 0));
 
133
            }
 
134
            a = getAccountById(account);
 
135
//          }
 
136
      }
 
137
   }
 
138
   if (a)
 
139
      emit accountStateChanged(a,a->getStateName(state));
 
140
   else
 
141
      qDebug() << "Account not found";
 
142
}
 
143
 
 
144
///Tell the model something changed
 
145
void AccountList::accountChanged(Account* a)
 
146
{
 
147
   int idx = (*m_pAccounts).indexOf(a);
 
148
   if (idx != -1) {
 
149
      emit dataChanged(index(idx, 0), index(idx, 0));
 
150
   }
 
151
}
 
152
 
 
153
 
 
154
/*****************************************************************************
 
155
 *                                                                           *
 
156
 *                                  Mutator                                  *
 
157
 *                                                                           *
 
158
 ****************************************************************************/
 
159
 
 
160
///Update accounts
 
161
void AccountList::update()
 
162
{
 
163
   ConfigurationManagerInterface & configurationManager = ConfigurationManagerInterfaceSingleton::getInstance();
 
164
   Account* current;
 
165
   for (int i = 0; i < m_pAccounts->size(); i++) {
 
166
      current = (*m_pAccounts)[i];
 
167
      if (!(*m_pAccounts)[i]->isNew() && (current->currentState() != NEW || current->currentState() != MODIFIED || current->currentState() != OUTDATED))
 
168
         removeAccount(current);
 
169
   }
 
170
   //ask for the list of accounts ids to the configurationManager
 
171
   QStringList accountIds = configurationManager.getAccountList().value();
 
172
   for (int i = 0; i < accountIds.size(); ++i) {
 
173
      Account* a = Account::buildExistingAccountFromId(accountIds[i]);
 
174
      m_pAccounts->insert(i, a);
 
175
      emit dataChanged(index(i,0),index(size()-1,0));
 
176
      connect(a,SIGNAL(changed(Account*)),this,SLOT(accountChanged(Account*)));
 
177
   }
 
178
} //update
 
179
 
 
180
///Update accounts
 
181
void AccountList::updateAccounts()
 
182
{
 
183
   qDebug() << "updateAccounts";
 
184
   ConfigurationManagerInterface& configurationManager = ConfigurationManagerInterfaceSingleton::getInstance();
 
185
   QStringList accountIds = configurationManager.getAccountList().value();
 
186
   //m_pAccounts->clear();
 
187
   for (int i = 0; i < accountIds.size(); ++i) {
 
188
      Account* acc = getAccountById(accountIds[i]);
 
189
      if (!acc) {
 
190
         qDebug() << "updateAccounts " << accountIds[i];
 
191
         Account* a = Account::buildExistingAccountFromId(accountIds[i]);
 
192
         (*m_pAccounts) += a;
 
193
         connect(a,SIGNAL(changed(Account*)),this,SLOT(accountChanged(Account*)));
 
194
         emit dataChanged(index(size()-1,0),index(size()-1,0));
 
195
      }
 
196
      else {
 
197
         acc->performAction(RELOAD);
 
198
      }
 
199
   }
 
200
   emit accountListUpdated();
 
201
} //updateAccounts
 
202
 
 
203
///Save accounts details and reload it
 
204
void AccountList::save()
 
205
{
 
206
   ConfigurationManagerInterface& configurationManager = ConfigurationManagerInterfaceSingleton::getInstance();
 
207
   QStringList accountIds= QStringList(configurationManager.getAccountList().value());
 
208
 
 
209
   //create or update each account from accountList
 
210
   for (int i = 0; i < size(); i++) {
 
211
      Account* current = (*this)[i];
 
212
      QString currentId;
 
213
      //current->save();
 
214
      current->performAction(AccountEditAction::SAVE);
 
215
      currentId = QString(current->getAccountId());
 
216
   }
 
217
 
 
218
   //remove accounts that are in the configurationManager but not in the client
 
219
   for (int i = 0; i < accountIds.size(); i++) {
 
220
      if(!getAccountById(accountIds[i])) {
 
221
         configurationManager.removeAccount(accountIds[i]);
 
222
      }
 
223
   }
 
224
 
 
225
   configurationManager.setAccountsOrder(getOrderedList());
 
226
}
 
227
 
 
228
///Move account up
 
229
bool AccountList::accountUp( int index )
 
230
{
 
231
   if(index > 0 && index <= rowCount()) {
 
232
      Account* account = getAccountAt(index);
 
233
      m_pAccounts->remove(index);
 
234
      m_pAccounts->insert(index - 1, account);
 
235
      emit dataChanged(this->index(index - 1, 0, QModelIndex()), this->index(index, 0, QModelIndex()));
 
236
      return true;
 
237
   }
 
238
   return false;
 
239
}
 
240
 
 
241
///Move account down
 
242
bool AccountList::accountDown( int index )
 
243
{
 
244
   if(index >= 0 && index < rowCount()) {
 
245
      Account* account = getAccountAt(index);
 
246
      m_pAccounts->remove(index);
 
247
      m_pAccounts->insert(index + 1, account);
 
248
      emit dataChanged(this->index(index, 0, QModelIndex()), this->index(index + 1, 0, QModelIndex()));
 
249
      return true;
 
250
   }
 
251
   return false;
 
252
}
 
253
 
 
254
///Try to register all enabled accounts
 
255
void AccountList::registerAllAccounts()
 
256
{
 
257
   ConfigurationManagerInterface& configurationManager = ConfigurationManagerInterfaceSingleton::getInstance();
 
258
   configurationManager.registerAllAccounts();
 
259
}
 
260
 
 
261
 
 
262
/*****************************************************************************
 
263
 *                                                                           *
 
264
 *                                  Getters                                  *
 
265
 *                                                                           *
 
266
 ****************************************************************************/
 
267
 
 
268
///Get all accounts
 
269
const QVector<Account*>& AccountList::getAccounts()
 
270
{
 
271
   return *m_pAccounts;
 
272
}
 
273
 
 
274
///Get a single account
 
275
const Account* AccountList::getAccountAt (int i) const
 
276
{
 
277
   return (*m_pAccounts)[i];
 
278
}
 
279
 
 
280
///Get a single account
 
281
Account* AccountList::getAccountAt (int i)
 
282
{
 
283
   return (*m_pAccounts)[i];
 
284
}
 
285
 
 
286
///Get a serialized string of all accounts
 
287
QString AccountList::getOrderedList() const
 
288
{
 
289
   QString order;
 
290
   for( int i = 0 ; i < size() ; i++) {
 
291
      order += getAccountAt(i)->getAccountId() + '/';
 
292
   }
 
293
   return order;
 
294
}
 
295
 
 
296
///Get account using its ID
 
297
Account* AccountList::getAccountById(const QString & id) const
 
298
{
 
299
   if(id.isEmpty())
 
300
          return nullptr;
 
301
   for (int i = 0; i < m_pAccounts->size(); ++i) {
 
302
      if (!(*m_pAccounts)[i]->isNew() && (*m_pAccounts)[i]->getAccountId() == id)
 
303
         return (*m_pAccounts)[i];
 
304
   }
 
305
   return nullptr;
 
306
}
 
307
 
 
308
///Get account with a specific state
 
309
QVector<Account*> AccountList::getAccountsByState(const QString& state)
 
310
{
 
311
   QVector<Account *> v;
 
312
   for (int i = 0; i < m_pAccounts->size(); ++i) {
 
313
      if ((*m_pAccounts)[i]->getAccountRegistrationStatus() == state)
 
314
         v += (*m_pAccounts)[i];
 
315
   }
 
316
   return v;
 
317
}
 
318
 
 
319
///Get a list of all registerred account
 
320
QVector<Account*> AccountList::registeredAccounts() const
 
321
{
 
322
   qDebug() << "registeredAccounts";
 
323
   QVector<Account*> registeredAccounts;
 
324
   Account* current;
 
325
   for (int i = 0; i < m_pAccounts->count(); ++i) {
 
326
      current = (*m_pAccounts)[i];
 
327
      if(current->getAccountRegistrationStatus() == ACCOUNT_STATE_REGISTERED) {
 
328
         qDebug() << current->getAlias() << " : " << current;
 
329
         registeredAccounts.append(current);
 
330
      }
 
331
   }
 
332
   return registeredAccounts;
 
333
}
 
334
 
 
335
///Get the first registerred account (default account)
 
336
Account* AccountList::firstRegisteredAccount() const
 
337
{
 
338
   Account* current;
 
339
   for (int i = 0; i < m_pAccounts->count(); ++i) {
 
340
      current = (*m_pAccounts)[i];
 
341
      if(current && current->getAccountRegistrationStatus() == ACCOUNT_STATE_REGISTERED && current->isAccountEnabled())
 
342
         return current;
 
343
      else if (current && (current->getAccountRegistrationStatus() == ACCOUNT_STATE_READY) && m_pAccounts->count() == 1)
 
344
         return current;
 
345
      else if (current && !(current->getAccountRegistrationStatus() == ACCOUNT_STATE_READY)) {
 
346
         qDebug() << "Account " << ((current)?current->getAccountId():"") << " is not registered ("
 
347
         << ((current)?current->getAccountRegistrationStatus():"") << ") State:"
 
348
         << ((current)?current->getAccountRegistrationStatus():"");
 
349
      }
 
350
   }
 
351
   return nullptr;
 
352
}
 
353
 
 
354
///Get the account size
 
355
int AccountList::size() const
 
356
{
 
357
   return m_pAccounts->size();
 
358
}
 
359
 
 
360
///Return the current account
 
361
Account* AccountList::getCurrentAccount()
 
362
{
 
363
   Account* priorAccount = getInstance()->getAccountById(m_sPriorAccountId);
 
364
   if(priorAccount && priorAccount->getAccountDetail(ACCOUNT_REGISTRATION_STATUS) == ACCOUNT_STATE_REGISTERED && priorAccount->isAccountEnabled() ) {
 
365
      return priorAccount;
 
366
   }
 
367
   else {
 
368
      Account* a = getInstance()->firstRegisteredAccount();
 
369
      if (a)
 
370
         return getInstance()->firstRegisteredAccount();
 
371
      else
 
372
         return getInstance()->getAccountById("IP2IP");
 
373
   }
 
374
} //getCurrentAccount
 
375
 
 
376
///Return the previously used account ID
 
377
QString AccountList::getPriorAccoundId()
 
378
{
 
379
   return m_sPriorAccountId;
 
380
}
 
381
 
 
382
///Get data from the model
 
383
QVariant AccountList::data ( const QModelIndex& index, int role) const
 
384
{
 
385
   if (!index.isValid() || index.row() < 0 || index.row() >= rowCount())
 
386
      return QVariant();
 
387
 
 
388
   const Account * account = (*m_pAccounts)[index.row()];
 
389
   if(index.column() == 0 && (role == Qt::DisplayRole || role == Qt::EditRole))
 
390
      return QVariant(account->getAlias());
 
391
   else if(index.column() == 0 && role == Qt::CheckStateRole) {
 
392
      return QVariant(account->isEnabled() ? Qt::Checked : Qt::Unchecked);
 
393
   }
 
394
   else if (role == Qt::BackgroundRole) {
 
395
      if (m_pColorVisitor)
 
396
         return m_pColorVisitor->getColor(account);
 
397
      else
 
398
         return QVariant(account->getStateColor());
 
399
   }
 
400
   else if(index.column() == 0 && role == Qt::DecorationRole && m_pColorVisitor) {
 
401
      return m_pColorVisitor->getIcon(account);
 
402
   }
 
403
   return QVariant();
 
404
} //data
 
405
 
 
406
///Flags for "index"
 
407
Qt::ItemFlags AccountList::flags(const QModelIndex & index) const
 
408
{
 
409
   if (index.column() == 0)
 
410
      return QAbstractItemModel::flags(index) | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable;
 
411
   return QAbstractItemModel::flags(index);
 
412
}
 
413
 
 
414
///Number of account
 
415
int AccountList::rowCount(const QModelIndex & /*parent*/) const
 
416
{
 
417
   return m_pAccounts->size();
 
418
}
 
419
 
 
420
Account* AccountList::getAccountByModelIndex(QModelIndex item) const {
 
421
   return (*m_pAccounts)[item.row()];
 
422
}
 
423
 
 
424
///Return the default account (used for contact lookup)
 
425
Account* AccountList::getDefaultAccount()
 
426
{
 
427
   return m_pDefaultAccount;
 
428
}
 
429
 
 
430
///Generate an unique suffix to prevent multiple account from sharing alias
 
431
QString AccountList::getSimilarAliasIndex(QString alias)
 
432
{
 
433
   int count = 0;
 
434
   foreach (Account* a, getInstance()->getAccounts()) {
 
435
      if (a->getAccountAlias().left(alias.size()) == alias)
 
436
         count++;
 
437
   }
 
438
   bool found = true;
 
439
   do {
 
440
      found = false;
 
441
      foreach (Account* a, getInstance()->getAccounts()) {
 
442
         if (a->getAccountAlias() == alias+QString(" (%1)").arg(count)) {
 
443
            count++;
 
444
            found = false;
 
445
            break;
 
446
         }
 
447
      }
 
448
   } while(found);
 
449
   if (count)
 
450
      return QString(" (%1)").arg(count);
 
451
   return QString();
 
452
}
 
453
 
 
454
 
 
455
/*****************************************************************************
 
456
 *                                                                           *
 
457
 *                                  Setters                                  *
 
458
 *                                                                           *
 
459
 ****************************************************************************/
 
460
 
 
461
///Add an account
 
462
Account* AccountList::addAccount(const QString& alias)
 
463
{
 
464
   Account* a = Account::buildNewAccountFromAlias(alias);
 
465
   connect(a,SIGNAL(changed(Account*)),this,SLOT(accountChanged(Account*)));
 
466
   (*m_pAccounts) += a;
 
467
   
 
468
   emit dataChanged(index(m_pAccounts->size()-1,0), index(m_pAccounts->size()-1,0));
 
469
   return a;
 
470
}
 
471
 
 
472
///Remove an account
 
473
void AccountList::removeAccount(Account* account)
 
474
{
 
475
   if (not account) return;
 
476
   qDebug() << "Removing" << m_pAccounts;
 
477
   int aindex = m_pAccounts->indexOf(account);
 
478
   m_pAccounts->remove(aindex);
 
479
   emit dataChanged(index(aindex,0), index(m_pAccounts->size()-1,0));
 
480
}
 
481
 
 
482
void AccountList::removeAccount( QModelIndex index )
 
483
{
 
484
   removeAccount(getAccountByModelIndex(index));
 
485
}
 
486
 
 
487
///Set the previous account used
 
488
void AccountList::setPriorAccount(Account* account) {
 
489
   bool changed = (account && m_sPriorAccountId != account->getAccountId()) || (!account && !m_sPriorAccountId.isEmpty());
 
490
   m_sPriorAccountId = account?account->getAccountId() : QString();
 
491
   if (changed)
 
492
      emit priorAccountChanged(getCurrentAccount());
 
493
}
 
494
 
 
495
///Set model data
 
496
bool AccountList::setData(const QModelIndex& index, const QVariant &value, int role)
 
497
{
 
498
   if (index.isValid() && index.column() == 0 && role == Qt::CheckStateRole) {
 
499
      bool prevEnabled = (*m_pAccounts)[index.row()]->isEnabled();
 
500
      (*m_pAccounts)[index.row()]->setEnabled(value.toBool());
 
501
      emit dataChanged(index, index);
 
502
      if (prevEnabled != value.toBool())
 
503
         emit accountEnabledChanged((*m_pAccounts)[index.row()]);
 
504
      emit dataChanged(index, index);
 
505
      return true;
 
506
   }
 
507
   else if ( role == Qt::EditRole ) {
 
508
      bool changed = value.toString() != data(index,Qt::EditRole);
 
509
      if (changed) {
 
510
         (*m_pAccounts)[index.row()]->setAccountAlias(value.toString());
 
511
         emit dataChanged(index, index);
 
512
      }
 
513
   }
 
514
   return false;
 
515
}
 
516
 
 
517
///Set QAbstractItemModel BackgroundRole visitor
 
518
void AccountList::setColorVisitor(AccountListColorVisitor* visitor)
 
519
{
 
520
   m_pColorVisitor = visitor;
 
521
}
 
522
 
 
523
///Set the default account (used for contact lookup)
 
524
void AccountList::setDefaultAccount(Account* a)
 
525
{
 
526
   if (a != m_pDefaultAccount)
 
527
      emit defaultAccountChanged(a);
 
528
   m_pDefaultAccount = a;
 
529
}
 
530
 
 
531
 
 
532
/*****************************************************************************
 
533
 *                                                                           *
 
534
 *                                 Operator                                  *
 
535
 *                                                                           *
 
536
 ****************************************************************************/
 
537
 
 
538
///Get the account from its index
 
539
const Account* AccountList::operator[] (int i) const
 
540
{
 
541
   return (*m_pAccounts)[i];
 
542
}
 
543
 
 
544
///Get the account from its index
 
545
Account* AccountList::operator[] (int i)
 
546
{
 
547
   return (*m_pAccounts)[i];
 
548
}