1
/***************************************************************************
2
* Copyright (C) 2009-2012 by Savoir-Faire Linux *
3
* Author : Emmanuel Lepage Valle <emmanuel.lepage@savoirfairelinux.com >*
5
* This program is free software; you can redistribute it and/or modify *
6
* it under the terms of the GNU General Public License as published by *
7
* the Free Software Foundation; either version 3 of the License, or *
8
* (at your option) any later version. *
10
* This program 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 *
13
* GNU General Public License for more details. *
15
* You should have received a copy of the GNU General Public License *
16
* along with this program; if not, write to the *
17
* Free Software Foundation, Inc., *
18
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19
**************************************************************************/
22
#include "ContactDock.h"
25
#include <QtCore/QDateTime>
26
#include <QtGui/QVBoxLayout>
27
#include <QtGui/QListWidget>
28
#include <QtGui/QTreeWidget>
29
#include <QtGui/QHeaderView>
30
#include <QtGui/QCheckBox>
31
#include <QtGui/QSplitter>
32
#include <QtGui/QLabel>
33
#include <QtGui/QComboBox>
38
#include <KLocalizedString>
42
#include "AkonadiBackend.h"
43
#include "ContactItemWidget.h"
45
#include "conf/ConfigurationSkeleton.h"
49
#include "lib/Contact.h"
51
///@class QNumericTreeWidgetItem_hist TreeWidget using different sorting criterias
52
class QNumericTreeWidgetItem_hist : public QTreeWidgetItem {
54
QNumericTreeWidgetItem_hist(QTreeWidget* parent):QTreeWidgetItem(parent),widget(0),weight(-1){}
55
QNumericTreeWidgetItem_hist(QTreeWidgetItem* parent):QTreeWidgetItem(parent),widget(0),weight(-1){}
56
ContactItemWidget* widget;
60
bool operator<(const QTreeWidgetItem & other) const {
61
int column = treeWidget()->sortColumn();
62
//if (dynamic_cast<QNumericTreeWidgetItem_hist*>((QTreeWidgetItem*)&other)) {
63
//if (widget !=0 && dynamic_cast<QNumericTreeWidgetItem_hist*>((QTreeWidgetItem*)&other)->widget != 0)
64
// return widget->getTimeStamp() < dynamic_cast<QNumericTreeWidgetItem_hist*>((QTreeWidgetItem*)&other)->widget->getTimeStamp();
65
//else if (weight > 0 && dynamic_cast<QNumericTreeWidgetItem_hist*>((QTreeWidgetItem*)&other)->weight > 0)
66
// return weight > dynamic_cast<QNumericTreeWidgetItem_hist*>((QTreeWidgetItem*)&other)->weight;
68
return text(column) < other.text(column);
72
///Forward keypresses to the filter line edit
73
bool KeyPressEaterC::eventFilter(QObject *obj, QEvent *event)
75
if (event->type() == QEvent::KeyPress) {
76
m_pDock->keyPressEvent((QKeyEvent*)event);
79
// standard event processing
80
return QObject::eventFilter(obj, event);
85
ContactDock::ContactDock(QWidget* parent) : QDockWidget(parent)
87
setObjectName("contactDock");
88
m_pFilterLE = new KLineEdit ( );
89
m_pSplitter = new QSplitter ( Qt::Vertical,this );
90
m_pSortByCBB = new QComboBox ( this );
91
m_pContactView = new ContactTree ( this );
92
m_pCallView = new QListWidget ( this );
93
m_pShowHistoCK = new QCheckBox ( this );
97
sortType << "Name" << "Organisation" << "Phone number type" << "Rencently used" << "Group";
98
m_pSortByCBB->addItems(sortType);
99
m_pSortByCBB->setDisabled(true);
101
QWidget* mainWidget = new QWidget(this);
102
setWidget(mainWidget);
104
m_pContactView->headerItem()->setText(0,i18n("Contacts"));
105
m_pContactView->header()->setClickable(true);
106
m_pContactView->header()->setSortIndicatorShown(true);
107
m_pContactView->setAcceptDrops(true);
108
m_pContactView->setDragEnabled(true);
109
KeyPressEaterC *keyPressEater = new KeyPressEaterC(this);
110
m_pContactView->installEventFilter(keyPressEater);
112
m_pContactView->setAlternatingRowColors(true);
114
m_pFilterLE->setPlaceholderText(i18n("Filter"));
115
m_pFilterLE->setClearButtonShown(true);
117
m_pShowHistoCK->setChecked(ConfigurationSkeleton::displayContactCallHistory());
118
m_pShowHistoCK->setText(i18n("Display history"));
120
setHistoryVisible(ConfigurationSkeleton::displayContactCallHistory());
122
QVBoxLayout* mainLayout = new QVBoxLayout(mainWidget);
124
mainLayout->addWidget ( m_pSortByCBB );
125
mainLayout->addWidget ( m_pShowHistoCK );
126
mainLayout->addWidget ( m_pSplitter );
127
m_pSplitter->addWidget ( m_pContactView );
128
m_pSplitter->addWidget ( m_pCallView );
129
mainLayout->addWidget ( m_pFilterLE );
131
m_pSplitter->setChildrenCollapsible(true);
132
m_pSplitter->setStretchFactor(0,7);
134
connect (AkonadiBackend::getInstance(),SIGNAL(collectionChanged()), this, SLOT(reloadContact() ));
135
connect (m_pContactView, SIGNAL(currentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*)),this, SLOT(loadContactHistory(QTreeWidgetItem*) ));
136
connect (m_pFilterLE, SIGNAL(textChanged(QString)), this, SLOT(filter(QString) ));
137
connect (m_pShowHistoCK, SIGNAL(toggled(bool)), this, SLOT(setHistoryVisible(bool) ));
138
setWindowTitle(i18n("Contact"));
142
ContactDock::~ContactDock()
148
/*****************************************************************************
152
****************************************************************************/
154
///Reload the contact
155
void ContactDock::reloadContact()
157
ContactList list = AkonadiBackend::getInstance()->update();
158
foreach (Contact* cont, list) {
159
ContactItemWidget* aContact = new ContactItemWidget(m_pContactView);
160
QNumericTreeWidgetItem_hist* item = new QNumericTreeWidgetItem_hist(m_pContactView);
161
item->widget = aContact;
162
aContact->setItem(item);
163
aContact->setContact(cont);
165
PhoneNumbers numbers = aContact->getContact()->getPhoneNumbers();
166
kDebug() << "Phone count" << numbers.count();
167
if (numbers.count() > 1) {
168
foreach (Contact::PhoneNumber* number, numbers) {
169
QNumericTreeWidgetItem_hist* item2 = new QNumericTreeWidgetItem_hist(item);
170
QLabel* numberL = new QLabel("<b>"+number->getType()+":</b>"+number->getNumber(),this);
171
item2->number = number->getNumber();
172
m_pContactView->setItemWidget(item2,0,numberL);
175
else if (numbers.count() == 1) {
176
item->number = numbers[0]->getNumber();
179
m_pContactView->addTopLevelItem(item);
180
m_pContactView->setItemWidget(item,0,aContact);
181
m_Contacts << aContact;
185
///Query the call history for all items related to this contact
186
void ContactDock::loadContactHistory(QTreeWidgetItem* item)
188
if (m_pShowHistoCK->isChecked()) {
189
m_pCallView->clear();
190
if (dynamic_cast<QNumericTreeWidgetItem_hist*>(item) != NULL) {
191
QNumericTreeWidgetItem_hist* realItem = dynamic_cast<QNumericTreeWidgetItem_hist*>(item);
192
foreach (Call* call, SFLPhone::app()->model()->getHistory()) {
193
if (realItem->widget != 0) {
194
foreach (Contact::PhoneNumber* number, realItem->widget->getContact()->getPhoneNumbers()) {
195
if (number->getNumber() == call->getPeerPhoneNumber()) {
196
m_pCallView->addItem(QDateTime::fromTime_t(call->getStartTimeStamp().toUInt()).toString());
206
void ContactDock::filter(const QString& text)
208
foreach(ContactItemWidget* item, m_Contacts) {
209
bool foundNumber = false;
210
foreach (Contact::PhoneNumber* number, item->getContact()->getPhoneNumbers()) {
211
foundNumber |= number->getNumber().toLower().indexOf(text) != -1;
213
bool visible = (item->getContact()->getFormattedName().toLower().indexOf(text) != -1)
214
|| (item->getContact()->getOrganization().toLower().indexOf(text) != -1)
215
|| (item->getContact()->getPreferredEmail().toLower().indexOf(text) != -1)
217
item->getItem()->setHidden(!visible);
219
m_pContactView->expandAll();
223
/*****************************************************************************
227
****************************************************************************/
229
///Serialize informations to be used for drag and drop
230
QMimeData* ContactTree::mimeData( const QList<QTreeWidgetItem *> items) const
232
kDebug() << "An history call is being dragged";
233
if (items.size() < 1) {
237
QMimeData *mimeData = new QMimeData();
240
if (dynamic_cast<QNumericTreeWidgetItem_hist*>(items[0])) {
241
QNumericTreeWidgetItem_hist* item = dynamic_cast<QNumericTreeWidgetItem_hist*>(items[0]);
242
if (item->widget != 0) {
243
mimeData->setData(MIME_CONTACT, item->widget->getContact()->getUid().toUtf8());
245
else if (!item->number.isEmpty()) {
246
mimeData->setData(MIME_PHONENUMBER, item->number.toUtf8());
250
kDebug() << "the item is not a call";
255
///Handle data being dropped on the widget
256
bool ContactTree::dropMimeData(QTreeWidgetItem *parent, int index, const QMimeData *data, Qt::DropAction action)
262
QByteArray encodedData = data->data(MIME_CALLID);
264
kDebug() << "In history import"<< QString(encodedData);
270
/*****************************************************************************
274
****************************************************************************/
276
///Show or hide the history list
277
void ContactDock::setHistoryVisible(bool visible)
279
kDebug() << "Toggling history visibility";
280
m_pCallView->setVisible(visible);
281
ConfigurationSkeleton::setDisplayContactCallHistory(visible);
285
/*****************************************************************************
287
* Keyboard handling *
289
****************************************************************************/
291
///Handle keypresses ont the dock
292
void ContactDock::keyPressEvent(QKeyEvent* event) {
293
int key = event->key();
294
if(key == Qt::Key_Escape)
295
m_pFilterLE->setText(QString());
296
else if(key == Qt::Key_Return || key == Qt::Key_Enter) {}
297
else if((key == Qt::Key_Backspace) && (m_pFilterLE->text().size()))
298
m_pFilterLE->setText(m_pFilterLE->text().left( m_pFilterLE->text().size()-1 ));
299
else if (!event->text().isEmpty() && !(key == Qt::Key_Backspace))
300
m_pFilterLE->setText(m_pFilterLE->text()+event->text());
b'\\ No newline at end of file'