~ubuntu-branches/ubuntu/wily/sflphone/wily

« back to all changes in this revision

Viewing changes to kde/src/klib/sortabledockcommon.hpp

  • Committer: Package Import Robot
  • Author(s): Mark Purcell
  • Date: 2014-01-28 18:23:36 UTC
  • mfrom: (1.1.11)
  • mto: This revision was merged to the branch mainline in revision 24.
  • Revision ID: package-import@ubuntu.com-20140128182336-3xenud1kbnwmf3mz
* New upstream release 
  - Fixes "New Upstream Release" (Closes: #735846)
  - Fixes "Ringtone does not stop" (Closes: #727164)
  - Fixes "[sflphone-kde] crash on startup" (Closes: #718178)
  - Fixes "sflphone GUI crashes when call is hung up" (Closes: #736583)
* Build-Depends: ensure GnuTLS 2.6
  - libucommon-dev (>= 6.0.7-1.1), libccrtp-dev (>= 2.0.6-3)
  - Fixes "FTBFS Build-Depends libgnutls{26,28}-dev" (Closes: #722040)
* Fix "boost 1.49 is going away" unversioned Build-Depends: (Closes: #736746)
* Add Build-Depends: libsndfile-dev, nepomuk-core-dev

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
 
//Qt
21
 
#include <QtCore/QDateTime>
22
 
#include <QtCore/QStringList>
23
 
#include <QtCore/QString>
24
 
 
25
 
//KDE
26
 
#include <KLocale>
27
 
 
28
 
//SFLPhone
29
 
#include "../lib/call.h"
30
 
#include "../lib/contact.h"
31
 
#include "../lib/callmodel.h"
32
 
#include "../lib/historymodel.h"
33
 
#include "akonadibackend.h"
34
 
#include "helperfunctions.h"
35
 
#include "configurationskeleton.h"
36
 
 
37
 
//Define
38
 
#define CALLMODEL_TEMPLATE template<typename CallWidget, typename Index>
39
 
#define SORTABLE_T SortableDockCommon<CallWidget,Index>
40
 
 
41
 
CALLMODEL_TEMPLATE QStringList         SORTABLE_T::m_slHistoryConst = QStringList();
42
 
CALLMODEL_TEMPLATE StaticEventHandler* SORTABLE_T::m_spEvHandler = new StaticEventHandler(0,&(SORTABLE_T::m_slHistoryConst));
43
 
 
44
 
///Constructor
45
 
CALLMODEL_TEMPLATE SORTABLE_T::SortableDockCommon()
46
 
{
47
 
 
48
 
}
49
 
 
50
 
 
51
 
/*****************************************************************************
52
 
 *                                                                           *
53
 
 *                                  Helpers                                  *
54
 
 *                                                                           *
55
 
 ****************************************************************************/
56
 
 
57
 
///Convert call end time stamp to human readable relative date
58
 
CALLMODEL_TEMPLATE QString SORTABLE_T::timeToHistoryCategory(QDate date)
59
 
{
60
 
   if (m_slHistoryConst.size() < 10)
61
 
      m_spEvHandler->update();
62
 
      
63
 
   //m_spEvHandler->update();
64
 
   if (QDate::currentDate()  == date || QDate::currentDate()  < date) //The future case would be a bug, but it have to be handled anyway or it will appear in "very long time ago"
65
 
      return i18n(m_slHistoryConst[HistoryConst::Today].toAscii());
66
 
 
67
 
   //Check for last week
68
 
   for (int i=1;i<7;i++) {
69
 
      if (QDate::currentDate().addDays(-i)  == date)
70
 
         return i18n(m_slHistoryConst[i].toAscii()); //Yesterday to Six_days_ago
71
 
   }
72
 
 
73
 
   //Check for last month
74
 
   for (int i=1;i<4;i++) {
75
 
      if (QDate::currentDate().addDays(-(i*7))  >= date && QDate::currentDate().addDays(-(i*7) -7)  < date)
76
 
         return i18n(m_slHistoryConst[i+Last_week-1].toAscii()); //Last_week to Three_weeks_ago
77
 
   }
78
 
 
79
 
   //Check for last year
80
 
   for (int i=1;i<12;i++) {
81
 
      if (QDate::currentDate().addMonths(-i)  >= date && QDate::currentDate().addMonths((-i) - 1)  < date)
82
 
         return i18n(m_slHistoryConst[i+Last_month-1].toAscii()); //Last_month to Twelve_months ago
83
 
   }
84
 
 
85
 
   if (QDate::currentDate().addYears(-1)  >= date && QDate::currentDate().addYears(-2)  < date)
86
 
      return i18n(m_slHistoryConst[Last_year].toAscii());
87
 
 
88
 
   //Every other senario
89
 
   return i18n(m_slHistoryConst[Very_long_time_ago].toAscii());
90
 
}
91
 
 
92
 
///Return the list of contact from history (in order, most recently used first)
93
 
CALLMODEL_TEMPLATE QHash<Contact*, QDateTime> SORTABLE_T::getContactListByTime(/*ContactList list*/)
94
 
{
95
 
   const CallMap& history= HistoryModel::getHistory();
96
 
   QHash<Contact*, QDateTime> toReturn;
97
 
   QSet<QString> alreadyUsed;
98
 
   QMapIterator<QString, Call*> i(history);
99
 
   i.toBack();
100
 
   while (i.hasPrevious()) { //Iterate from the end up
101
 
      i.previous();
102
 
      (alreadyUsed.find(i.value()->getPeerPhoneNumber()) == alreadyUsed.constEnd()); //Don't ask, leave it there Elv13(2012)
103
 
      if (alreadyUsed.find(i.value()->getPeerPhoneNumber()) == alreadyUsed.constEnd()) {
104
 
         Contact* contact = i.value()->getContact();
105
 
         if (contact && toReturn.find(contact) == toReturn.end()) {
106
 
            toReturn[contact] = QDateTime::fromTime_t(i.value()->getStartTimeStamp().toUInt());
107
 
         }
108
 
         alreadyUsed << i.value()->getPeerPhoneNumber();
109
 
      }
110
 
   }
111
 
   return toReturn;
112
 
} //getContactListByTime
113
 
 
114
 
///Set category
115
 
CALLMODEL_TEMPLATE void SORTABLE_T::setHistoryCategory(QList<Call*>& calls,HistorySortingMode mode)
116
 
{
117
 
   QHash<QString,uint> popularityCount;
118
 
   QMap<QString, QList<Call*> > byDate;
119
 
   switch (mode) {
120
 
      case HistorySortingMode::Date:
121
 
         foreach (const QString& cat, m_slHistoryConst) {
122
 
            byDate[i18n(cat.toAscii())] = QList<Call*>();
123
 
         }
124
 
         break;
125
 
      case HistorySortingMode::Popularity:
126
 
         foreach (Call* call, calls) {
127
 
            popularityCount[getIdentity(call)]++;
128
 
         }
129
 
         break;
130
 
      default:
131
 
         break;
132
 
   }
133
 
   foreach (Call* call, calls) {
134
 
      QString category;
135
 
      switch (mode) {
136
 
         case HistorySortingMode::Date:
137
 
         {
138
 
            category = timeToHistoryCategory(QDateTime::fromTime_t(call->getStartTimeStamp().toUInt()).date());
139
 
            byDate[category] <<call;
140
 
         }
141
 
            break;
142
 
         case HistorySortingMode::Name2:
143
 
            category = getIdentity(call);
144
 
            break;
145
 
         case HistorySortingMode::Popularity:
146
 
            {
147
 
               QString identity = getIdentity(call);
148
 
               category = identity+"("+QString::number(popularityCount[identity])+")";
149
 
            }
150
 
            break;
151
 
         case HistorySortingMode::Length:
152
 
            category = i18n("TODO");
153
 
            break;
154
 
         default:
155
 
            break;
156
 
      }
157
 
      call->setProperty("section",category);
158
 
   }
159
 
   switch (mode) {
160
 
      case HistorySortingMode::Date:
161
 
         calls.clear();
162
 
         foreach (const QString& cat, m_slHistoryConst) {
163
 
            foreach (Call* call, byDate[i18n(cat.toAscii())]) {
164
 
               calls << call;
165
 
            }
166
 
         }
167
 
         break;
168
 
      default:
169
 
         break;
170
 
   }
171
 
} //setHistoryCategory
172
 
 
173
 
///Set contact category
174
 
CALLMODEL_TEMPLATE void SORTABLE_T::setContactCategory(QList<Contact*> contacts,ContactSortingMode mode)
175
 
{
176
 
   QHash<Contact*, QDateTime> recentlyUsed;
177
 
   switch (mode) {
178
 
      case ContactSortingMode::Recently_used:
179
 
         recentlyUsed = getContactListByTime();
180
 
         foreach (const QString& cat, m_slHistoryConst) {
181
 
            //m_pContactView->addCategory(cat);
182
 
         }
183
 
         break;
184
 
      default:
185
 
         break;
186
 
   }
187
 
   foreach (Contact* cont, contacts) {
188
 
      if (cont->getPhoneNumbers().count() && usableNumberCount(cont)) {
189
 
         QString category;
190
 
         switch (mode) {
191
 
            case ContactSortingMode::Name:
192
 
               category = QString(cont->getFormattedName().trimmed()[0]);
193
 
               break;
194
 
            case ContactSortingMode::Organisation:
195
 
               category = (cont->getOrganization().isEmpty())?i18nc("Unknown category","Unknown"):cont->getOrganization();
196
 
               break;
197
 
            case ContactSortingMode::Recently_used:
198
 
               if (recentlyUsed.find(cont) != recentlyUsed.end())
199
 
                  category = timeToHistoryCategory(recentlyUsed[cont].date());
200
 
               else
201
 
                  category = i18n(m_slHistoryConst[Never].toAscii());
202
 
               break;
203
 
            case ContactSortingMode::Group:
204
 
               category = i18n("TODO");
205
 
               break;
206
 
            case ContactSortingMode::Department:
207
 
               category = (cont->getDepartment().isEmpty())?i18nc("Unknown category","Unknown"):cont->getDepartment();;
208
 
               break;
209
 
            default:
210
 
               break;
211
 
         }
212
 
      }
213
 
   }
214
 
} //setContactCategory
215
 
 
216
 
///Return the identity of the call caller, try to return something useful
217
 
CALLMODEL_TEMPLATE QString SORTABLE_T::getIdentity(Call* item)
218
 
{
219
 
   Contact* contact = item->getContact();
220
 
   if (contact)
221
 
      return contact->getFormattedName();
222
 
   else if (!item->getPeerName().isEmpty())
223
 
      return item->getPeerName();
224
 
   else
225
 
      return item->getPeerPhoneNumber();
226
 
}
227
 
 
228
 
///How many different number are present for 'cont'
229
 
CALLMODEL_TEMPLATE int SORTABLE_T::usableNumberCount(Contact* cont)
230
 
{
231
 
   uint result =0;
232
 
   QStringList list = ConfigurationSkeleton::phoneTypeList();
233
 
   foreach (Contact::PhoneNumber* pn,cont->getPhoneNumbers()) {
234
 
      result += (list.indexOf(pn->getType()) != -1) || (pn->getType().isEmpty()); //Always allow empty because of LDAP, vcard
235
 
   }
236
 
   return result;
237
 
}