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

« back to all changes in this revision

Viewing changes to kde/src/lib/historymodel.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) 2012-2013 by Savoir-Faire Linux                          *
 
3
 *   Author : Emmanuel Lepage Vallee <emmanuel.lepage@savoirfairelinux.com> *
 
4
 *                                                                          *
 
5
 *   This library is free software; you can redistribute it and/or          *
 
6
 *   modify it under the terms of the GNU Lesser General Public             *
 
7
 *   License as published by the Free Software Foundation; either           *
 
8
 *   version 2.1 of the License, or (at your option) any later version.     *
 
9
 *                                                                          *
 
10
 *   This library is distributed in the hope that it will be useful,        *
 
11
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of         *
 
12
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU      *
 
13
 *   Lesser General Public License for more details.                        *
 
14
 *                                                                          *
 
15
 *   You should have received a copy of the GNU General Public License      *
 
16
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.  *
 
17
 ***************************************************************************/
 
18
#include "historymodel.h"
 
19
#include "callmanager_interface_singleton.h"
 
20
#include "configurationmanager_interface_singleton.h"
 
21
#include "call.h"
 
22
 
 
23
 
 
24
/*****************************************************************************
 
25
 *                                                                           *
 
26
 *                             Private classes                               *
 
27
 *                                                                           *
 
28
 ****************************************************************************/
 
29
 
 
30
///SortableCallSource: helper class to make sorting possible
 
31
class SortableCallSource {
 
32
public:
 
33
   SortableCallSource(Call* call=0) : count(0),callInfo(call) {}
 
34
   uint count;
 
35
   Call* callInfo;
 
36
   bool operator<(SortableCallSource other) {
 
37
      return (other.count > count);
 
38
   }
 
39
};
 
40
 
 
41
inline bool operator< (const SortableCallSource & s1, const SortableCallSource & s2)
 
42
{
 
43
    return  s1.count < s2.count;
 
44
}
 
45
 
 
46
HistoryModel* HistoryModel::m_spInstance    = nullptr;
 
47
CallMap       HistoryModel::m_sHistoryCalls          ;
 
48
 
 
49
 
 
50
/*****************************************************************************
 
51
 *                                                                           *
 
52
 *                                 Constructor                               *
 
53
 *                                                                           *
 
54
 ****************************************************************************/
 
55
 
 
56
///Constructor
 
57
HistoryModel::HistoryModel():m_HistoryInit(false)
 
58
{
 
59
   ConfigurationManagerInterface& configurationManager = ConfigurationManagerInterfaceSingleton::getInstance();
 
60
   const QVector< QMap<QString, QString> > history = configurationManager.getHistory();
 
61
   foreach (const MapStringString& hc, history) {
 
62
      Call* pastCall = Call::buildHistoryCall(
 
63
               hc[ CALLID_KEY          ]         ,
 
64
               hc[ TIMESTAMP_START_KEY ].toUInt(),
 
65
               hc[ TIMESTAMP_STOP_KEY  ].toUInt(),
 
66
               hc[ ACCOUNT_ID_KEY      ]         ,
 
67
               hc[ DISPLAY_NAME_KEY    ]         ,
 
68
               hc[ PEER_NUMBER_KEY     ]         ,
 
69
               hc[ STATE_KEY           ]
 
70
      );
 
71
      if (pastCall->getPeerName().isEmpty()) {
 
72
         pastCall->setPeerName("Unknown");
 
73
      }
 
74
      pastCall->setRecordingPath(hc[ RECORDING_PATH_KEY ]);
 
75
      addPriv(pastCall);
 
76
   }
 
77
   m_HistoryInit = true;
 
78
} //initHistory
 
79
 
 
80
///Destructor
 
81
HistoryModel::~HistoryModel()
 
82
{
 
83
   m_spInstance = nullptr;
 
84
}
 
85
 
 
86
///Singleton
 
87
HistoryModel* HistoryModel::self()
 
88
{
 
89
   if (!m_spInstance)
 
90
      m_spInstance = new HistoryModel();
 
91
   return m_spInstance;
 
92
}
 
93
 
 
94
 
 
95
/*****************************************************************************
 
96
 *                                                                           *
 
97
 *                           History related code                            *
 
98
 *                                                                           *
 
99
 ****************************************************************************/
 
100
 
 
101
///Add to history
 
102
void HistoryModel::add(Call* call)
 
103
{
 
104
   self()->addPriv(call);
 
105
}
 
106
 
 
107
///Add to history
 
108
void HistoryModel::addPriv(Call* call)
 
109
{
 
110
   if (call) {
 
111
      m_sHistoryCalls[call->getStartTimeStamp()] = call;
 
112
   }
 
113
   emit newHistoryCall(call);
 
114
   emit historyChanged();
 
115
}
 
116
 
 
117
///Return the history list
 
118
const CallMap& HistoryModel::getHistory()
 
119
{
 
120
   self();
 
121
   return m_sHistoryCalls;
 
122
}
 
123
 
 
124
///Return a list of all previous calls
 
125
const QStringList HistoryModel::getHistoryCallId()
 
126
{
 
127
   self();
 
128
   QStringList toReturn;
 
129
   foreach(Call* call, m_sHistoryCalls) {
 
130
      toReturn << call->getCallId();
 
131
   }
 
132
   return toReturn;
 
133
}
 
134
 
 
135
///Sort all history call by popularity and return the result (most popular first)
 
136
const QStringList HistoryModel::getNumbersByPopularity()
 
137
{
 
138
   self();
 
139
   QHash<QString,SortableCallSource*> hc;
 
140
   foreach (Call* call, getHistory()) {
 
141
      if (!hc[call->getPeerPhoneNumber()]) {
 
142
         hc[call->getPeerPhoneNumber()] = new SortableCallSource(call);
 
143
      }
 
144
      hc[call->getPeerPhoneNumber()]->count++;
 
145
   }
 
146
   QList<SortableCallSource> userList;
 
147
   foreach (SortableCallSource* i,hc) {
 
148
      userList << *i;
 
149
   }
 
150
   qSort(userList);
 
151
   QStringList cl;
 
152
   for (int i=userList.size()-1;i >=0 ;i--) {
 
153
      cl << userList[i].callInfo->getPeerPhoneNumber();
 
154
   }
 
155
   foreach (SortableCallSource* i,hc) {
 
156
      delete i;
 
157
   }
 
158
 
 
159
   return cl;
 
160
} //getNumbersByPopularity