#include "contactsdlg.h"
#include <QHBoxLayout>
#include <QApplication>
#include <QAction>
#include <QTimer>
#include <QPushButton>
ContactsDialog::ContactsDialog(QWidget *parent) :
QDialog(parent)
{
QHBoxLayout* l = new QHBoxLayout;
m_listWidget = new QListWidget(this);
l->addWidget(m_listWidget);
QVBoxLayout* lv = new QVBoxLayout;
QPushButton* backBtn = new QPushButton("Back",this);
QObject::connect(backBtn, SIGNAL(pressed()), this, SLOT(close()));
backBtn->setFixedWidth(100);
lv->addWidget(backBtn);
lv->setAlignment(backBtn,Qt::AlignTop);
QPushButton* okBtn = new QPushButton("Ok",this);
QObject::connect(okBtn, SIGNAL(pressed()), this, SLOT(selectContact()));
okBtn->setFixedWidth(100);
lv->addWidget(okBtn);
lv->setAlignment(okBtn,Qt::AlignBottom);
l->addLayout(lv);
setLayout(l);
#ifdef Q_OS_SYMBIAN
QWidgetList widgets = QApplication::allWidgets();
QWidget* w = 0;
foreach (w,widgets)
{
w->setContextMenuPolicy(Qt::NoContextMenu);
}
#endif
createContactManager();
searchContact();
showFullScreen();
}
ContactsDialog::~ContactsDialog()
{
delete m_contactManager;
}
void ContactsDialog::createContactManager()
{
#if defined Q_WS_MAEMO_5
m_contactManager = new QContactManager("maemo5");
#elif defined Q_OS_SYMBIAN
m_contactManager = new QContactManager("symbian");
#endif
if (!m_contactManager) {
m_contactManager = new QContactManager();
}
}
void ContactsDialog::searchContact()
{
m_listWidget->clear();
QContactSortOrder sort;
sort.setDirection(Qt::AscendingOrder);
sort.setDetailDefinitionName(QContactName::DefinitionName, QContactName::FieldLastName);
QList<QContactLocalId> contactIds = m_contactManager->contactIds(sort);
QContact currContact;
foreach (const QContactLocalId& id, contactIds)
{
QListWidgetItem *currItem = new QListWidgetItem;
currContact = m_contactManager->contact(id);
QContactDisplayLabel dl = currContact.detail(QContactDisplayLabel::DefinitionName);
currItem->setData(Qt::DisplayRole, dl.label());
currItem->setData(Qt::UserRole, currContact.localId());
m_listWidget->addItem(currItem);
}
if (m_listWidget->count()>0) {
m_listWidget->setCurrentRow(0);
}
}
void ContactsDialog::selectContact()
{
QList<QListWidgetItem*> items = m_listWidget->selectedItems();
if (!items.isEmpty()) {
itemClicked(items.first());
}
}
void ContactsDialog::itemClicked(QListWidgetItem *item)
{
QVariant data = item->data(Qt::UserRole);
QContactLocalId id = data.toInt();
QContact contact = m_contactManager->contact(id);
QContactPhoneNumber cpn = contact.detail<QContactPhoneNumber> ();
if (!cpn.isEmpty()) {
emit contactSelected(cpn.number());
close();
}
}