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
***************************************************************************/
21
#include <QtCore/QDateTime>
22
#include <QtCore/QStringList>
23
#include <QtCore/QString>
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"
38
#define CALLMODEL_TEMPLATE template<typename CallWidget, typename Index>
39
#define SORTABLE_T SortableDockCommon<CallWidget,Index>
41
CALLMODEL_TEMPLATE QStringList SORTABLE_T::m_slHistoryConst = QStringList();
42
CALLMODEL_TEMPLATE StaticEventHandler* SORTABLE_T::m_spEvHandler = new StaticEventHandler(0,&(SORTABLE_T::m_slHistoryConst));
45
CALLMODEL_TEMPLATE SORTABLE_T::SortableDockCommon()
51
/*****************************************************************************
55
****************************************************************************/
57
///Convert call end time stamp to human readable relative date
58
CALLMODEL_TEMPLATE QString SORTABLE_T::timeToHistoryCategory(QDate date)
60
if (m_slHistoryConst.size() < 10)
61
m_spEvHandler->update();
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());
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
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
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
85
if (QDate::currentDate().addYears(-1) >= date && QDate::currentDate().addYears(-2) < date)
86
return i18n(m_slHistoryConst[Last_year].toAscii());
89
return i18n(m_slHistoryConst[Very_long_time_ago].toAscii());
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*/)
95
const CallMap& history= HistoryModel::getHistory();
96
QHash<Contact*, QDateTime> toReturn;
97
QSet<QString> alreadyUsed;
98
QMapIterator<QString, Call*> i(history);
100
while (i.hasPrevious()) { //Iterate from the end up
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());
108
alreadyUsed << i.value()->getPeerPhoneNumber();
112
} //getContactListByTime
115
CALLMODEL_TEMPLATE void SORTABLE_T::setHistoryCategory(QList<Call*>& calls,HistorySortingMode mode)
117
QHash<QString,uint> popularityCount;
118
QMap<QString, QList<Call*> > byDate;
120
case HistorySortingMode::Date:
121
foreach (const QString& cat, m_slHistoryConst) {
122
byDate[i18n(cat.toAscii())] = QList<Call*>();
125
case HistorySortingMode::Popularity:
126
foreach (Call* call, calls) {
127
popularityCount[getIdentity(call)]++;
133
foreach (Call* call, calls) {
136
case HistorySortingMode::Date:
138
category = timeToHistoryCategory(QDateTime::fromTime_t(call->getStartTimeStamp().toUInt()).date());
139
byDate[category] <<call;
142
case HistorySortingMode::Name2:
143
category = getIdentity(call);
145
case HistorySortingMode::Popularity:
147
QString identity = getIdentity(call);
148
category = identity+"("+QString::number(popularityCount[identity])+")";
151
case HistorySortingMode::Length:
152
category = i18n("TODO");
157
call->setProperty("section",category);
160
case HistorySortingMode::Date:
162
foreach (const QString& cat, m_slHistoryConst) {
163
foreach (Call* call, byDate[i18n(cat.toAscii())]) {
171
} //setHistoryCategory
173
///Set contact category
174
CALLMODEL_TEMPLATE void SORTABLE_T::setContactCategory(QList<Contact*> contacts,ContactSortingMode mode)
176
QHash<Contact*, QDateTime> recentlyUsed;
178
case ContactSortingMode::Recently_used:
179
recentlyUsed = getContactListByTime();
180
foreach (const QString& cat, m_slHistoryConst) {
181
//m_pContactView->addCategory(cat);
187
foreach (Contact* cont, contacts) {
188
if (cont->getPhoneNumbers().count() && usableNumberCount(cont)) {
191
case ContactSortingMode::Name:
192
category = QString(cont->getFormattedName().trimmed()[0]);
194
case ContactSortingMode::Organisation:
195
category = (cont->getOrganization().isEmpty())?i18nc("Unknown category","Unknown"):cont->getOrganization();
197
case ContactSortingMode::Recently_used:
198
if (recentlyUsed.find(cont) != recentlyUsed.end())
199
category = timeToHistoryCategory(recentlyUsed[cont].date());
201
category = i18n(m_slHistoryConst[Never].toAscii());
203
case ContactSortingMode::Group:
204
category = i18n("TODO");
206
case ContactSortingMode::Department:
207
category = (cont->getDepartment().isEmpty())?i18nc("Unknown category","Unknown"):cont->getDepartment();;
214
} //setContactCategory
216
///Return the identity of the call caller, try to return something useful
217
CALLMODEL_TEMPLATE QString SORTABLE_T::getIdentity(Call* item)
219
Contact* contact = item->getContact();
221
return contact->getFormattedName();
222
else if (!item->getPeerName().isEmpty())
223
return item->getPeerName();
225
return item->getPeerPhoneNumber();
228
///How many different number are present for 'cont'
229
CALLMODEL_TEMPLATE int SORTABLE_T::usableNumberCount(Contact* cont)
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