From 2a57613e1caa190f0f0b08237c53171ffea7639f Mon Sep 17 00:00:00 2001
From: Emmanuel Lepage <emmanuel.lepage@savoirfairelinux.com>
Date: Thu, 29 Sep 2011 15:05:51 -0400
Subject: [PATCH] [#7023] Add the ability to load an abstract contact backend
 in the library to resolve more data, polish code

---
 kde/src/SFLPhone.cpp                |  1 +
 kde/src/lib/Call.cpp                | 47 ++++++++++++++++++-----------
 kde/src/lib/Call.h                  |  8 +++--
 kde/src/lib/CallModel.h             | 10 +++---
 kde/src/lib/CallModel.hpp           |  6 ++++
 kde/src/widgets/HistoryDock.cpp     | 21 +++++++++++--
 kde/src/widgets/HistoryTreeItem.cpp | 28 +++++++++++++++--
 7 files changed, 92 insertions(+), 29 deletions(-)

diff --git a/kde/src/SFLPhone.cpp b/kde/src/SFLPhone.cpp
index d179411252..ab696b09bc 100755
--- a/kde/src/SFLPhone.cpp
+++ b/kde/src/SFLPhone.cpp
@@ -65,6 +65,7 @@ TreeWidgetCallModel* SFLPhone::model()
    if (!m_pModel) {
       m_pModel = new TreeWidgetCallModel(TreeWidgetCallModel::ActiveCall);
       m_pModel->initCall();
+      m_pModel->initContact(AkonadiBackend::getInstance());
     }
    return m_pModel;
 }
diff --git a/kde/src/lib/Call.cpp b/kde/src/lib/Call.cpp
index d3c4404b3c..30313bfdf4 100644
--- a/kde/src/lib/Call.cpp
+++ b/kde/src/lib/Call.cpp
@@ -24,6 +24,8 @@
 
 #include "callmanager_interface_singleton.h"
 #include "configurationmanager_interface_singleton.h"
+#include "ContactBackend.h"
+#include "Contact.h"
 
 
 const call_state Call::actionPerformedStateMap [11][5] = 
@@ -94,6 +96,13 @@ const function Call::stateChangedFunctionMap[11][6] =
 
 const char * Call::historyIcons[3] = {ICON_HISTORY_INCOMING, ICON_HISTORY_OUTGOING, ICON_HISTORY_MISSED};
 
+ContactBackend* Call::m_pContactBackend = 0;
+
+void Call::setContactBackend(ContactBackend* be)
+{
+   m_pContactBackend = be;
+}
+
 ///Constructor
 Call::Call(call_state startState, QString callId, QString peerName, QString peerNumber, QString account)
    : conference(false)
@@ -318,13 +327,13 @@ QString Call::getStartTimeStamp() const
 ///Get the number where the call have been transferred
 QString Call::getTransferNumber() const
 {
-   return transferNumber;
+   return m_pTransferNumber;
 }
 
 ///Get the call / peer number
 QString Call::getCallNumber() const
 {
-   return callNumber;
+   return m_pCallNumber;
 }
 
 ///Return the call id
@@ -416,13 +425,13 @@ bool Call::isSecure() const {
 ///Set the transfer number
 void Call::setTransferNumber(QString number)
 {
-   transferNumber = number;
+   m_pTransferNumber = number;
 }
 
 ///Set the call number
 void Call::setCallNumber(QString number)
 {
-   callNumber = number;
+   m_pCallNumber = number;
    emit changed();
 }
 
@@ -544,9 +553,9 @@ void Call::refuse()
 void Call::acceptTransf()
 {
    CallManagerInterface & callManager = CallManagerInterfaceSingleton::getInstance();
-   qDebug() << "Accepting call and transfering it to number : " << transferNumber << ". callId : " << m_pCallId;
+   qDebug() << "Accepting call and transfering it to number : " << m_pTransferNumber << ". callId : " << m_pCallId;
    callManager.accept(m_pCallId);
-   callManager.transfer(m_pCallId, transferNumber);
+   callManager.transfer(m_pCallId, m_pTransferNumber);
 //   m_pHistoryState = TRANSFERED;
 }
 
@@ -595,17 +604,19 @@ void Call::call()
       this->m_pAccount = CallModelConvenience::getCurrentAccountId();
    }
    if(!m_pAccount.isEmpty()) {
-      qDebug() << "Calling " << callNumber << " with account " << m_pAccount << ". callId : " << m_pCallId;
-      callManager.placeCall(m_pAccount, m_pCallId, callNumber);
+      qDebug() << "Calling " << m_pCallNumber << " with account " << m_pAccount << ". callId : " << m_pCallId;
+      callManager.placeCall(m_pAccount, m_pCallId, m_pCallNumber);
       this->m_pAccount = m_pAccount;
-      this->m_pPeerPhoneNumber = callNumber;
-//       Contact * contact = findContactForNumberInKAddressBook(peerPhoneNumber); //TODO port
-//       if(contact) this->m_pPeerName = contact->getNickName();
+      this->m_pPeerPhoneNumber = m_pCallNumber;
+      if (m_pContactBackend) {
+         Contact* contact = m_pContactBackend->getContactByPhone(m_pPeerPhoneNumber);
+         m_pPeerName = contact->getFormattedName();
+      }
       this->m_pStartTime = new QDateTime(QDateTime::currentDateTime());
       this->m_pHistoryState = OUTGOING;
    }
    else {
-      qDebug() << "Trying to call " << transferNumber << " with no account registered . callId : " << m_pCallId;
+      qDebug() << "Trying to call " << m_pTransferNumber << " with no account registered . callId : " << m_pCallId;
       this->m_pHistoryState = NONE;
       throw "No account registered!";
    }
@@ -615,8 +626,8 @@ void Call::call()
 void Call::transfer()
 {
    CallManagerInterface & callManager = CallManagerInterfaceSingleton::getInstance();
-   qDebug() << "Transfering call to number : " << transferNumber << ". callId : " << m_pCallId;
-   callManager.transfer(m_pCallId, transferNumber);
+   qDebug() << "Transfering call to number : " << m_pTransferNumber << ". callId : " << m_pCallId;
+   callManager.transfer(m_pCallId, m_pTransferNumber);
    this->m_pStopTime = new QDateTime(QDateTime::currentDateTime());
 }
 
@@ -694,10 +705,10 @@ void Call::appendText(QString str)
    switch (currentState) {
    case CALL_STATE_TRANSFER    :
    case CALL_STATE_TRANSF_HOLD :
-      editNumber = &transferNumber;
+      editNumber = &m_pTransferNumber;
       break;
    case CALL_STATE_DIALING     :
-      editNumber = &callNumber;
+      editNumber = &m_pCallNumber;
       break;
    default                     :
       qDebug() << "Backspace on call not editable. Doing nothing.";
@@ -717,10 +728,10 @@ void Call::backspaceItemText()
    switch (currentState) {
       case CALL_STATE_TRANSFER        :
       case CALL_STATE_TRANSF_HOLD     :
-         editNumber = &transferNumber;
+         editNumber = &m_pTransferNumber;
          break;
       case CALL_STATE_DIALING         :
-         editNumber = &callNumber;
+         editNumber = &m_pCallNumber;
          break;
       default                         :
          qDebug() << "Backspace on call not editable. Doing nothing.";
diff --git a/kde/src/lib/Call.h b/kde/src/lib/Call.h
index 97caa26250..0ad0ee8bb2 100644
--- a/kde/src/lib/Call.h
+++ b/kde/src/lib/Call.h
@@ -31,6 +31,8 @@
 #include "sflphone_const.h"
 #include "typedefs.h"
 
+class ContactBackend;
+
 
 /** @enum daemon_call_state_t 
   * This enum have all the states a call can take for the daemon.
@@ -134,8 +136,9 @@ private:
    QLabel * labelHistoryTime;
    */
 
-   QString transferNumber;
-   QString callNumber;
+   QString m_pTransferNumber;
+   QString m_pCallNumber;
+   static ContactBackend* m_pContactBackend;
    
    bool conference;
    
@@ -212,6 +215,7 @@ public:
    static Call* buildRingingCall  (const QString & callId                                                                                                       );
    static Call* buildHistoryCall  (const QString & callId, uint startTimeStamp, uint stopTimeStamp, QString account, QString name, QString number, QString type );
    static Call* buildExistingCall (QString callId                                                                                                               );
+   static void  setContactBackend (ContactBackend* be                                                                                                           );
 
    //Static getters
    static history_state getHistoryStateFromType            ( QString type                                    );
diff --git a/kde/src/lib/CallModel.h b/kde/src/lib/CallModel.h
index 6853da3978..cdd9c681ae 100644
--- a/kde/src/lib/CallModel.h
+++ b/kde/src/lib/CallModel.h
@@ -36,6 +36,7 @@
 #include "sflphone_const.h"
 #include "unistd.h"
 #include "typedefs.h"
+#include "ContactBackend.h"
 
 typedef QHash<QString, Call*> CallHash;
 typedef QList<Call*>          CallList;
@@ -90,10 +91,11 @@ class LIB_EXPORT CallModel : public CallModelBase {
       };
 
       //Constructors, initializer and destructors
-      CallModel                ( ModelType type );
-      virtual ~CallModel       (                ) {}
-      virtual bool initCall    (                );
-      virtual bool initHistory (                );
+      CallModel                ( ModelType type     );
+      virtual ~CallModel       (                    ) {}
+      virtual bool initCall    (                    );
+      virtual bool initHistory (                    );
+      virtual void initContact ( ContactBackend* be );
 
       //Call related
       virtual Call*  addCall         ( Call* call                , Call* parent =0    );
diff --git a/kde/src/lib/CallModel.hpp b/kde/src/lib/CallModel.hpp
index b81efc9834..54c74c2b21 100644
--- a/kde/src/lib/CallModel.hpp
+++ b/kde/src/lib/CallModel.hpp
@@ -65,6 +65,12 @@ template<typename CallWidget, typename Index> bool CallModel<CallWidget,Index>::
    return true;
 }
 
+///Set how the call can find more informations about the call it receive
+template<typename CallWidget, typename Index> void CallModel<CallWidget,Index>::initContact ( ContactBackend* be )
+{
+   Call::setContactBackend(be);
+}
+
 ///Fill the history list
 ///@warning This solution wont scale to multiple call or history model implementation. Some static addCall + foreach for each call would be needed if this case ever become unavoidable
 template<typename CallWidget, typename Index> bool CallModel<CallWidget,Index>::initHistory()
diff --git a/kde/src/widgets/HistoryDock.cpp b/kde/src/widgets/HistoryDock.cpp
index 8ac63e8022..b9abcc193e 100644
--- a/kde/src/widgets/HistoryDock.cpp
+++ b/kde/src/widgets/HistoryDock.cpp
@@ -17,6 +17,7 @@
 #include "AkonadiBackend.h"
 #include "lib/sflphone_const.h"
 
+///Qt lack official functional sorting algo, so this hack around it
 class QNumericTreeWidgetItem : public QTreeWidgetItem {
    public:
       QNumericTreeWidgetItem(QTreeWidget* parent):QTreeWidgetItem(parent),widget(0),weight(-1){}
@@ -36,6 +37,7 @@ class QNumericTreeWidgetItem : public QTreeWidgetItem {
       }
 };
 
+///Event filter allowing to write text on the Tree widget to filter it.
 bool KeyPressEater::eventFilter(QObject *obj, QEvent *event)
 {
    if (event->type() == QEvent::KeyPress) {
@@ -47,6 +49,7 @@ bool KeyPressEater::eventFilter(QObject *obj, QEvent *event)
    }
 }
 
+///Constructor
 HistoryDock::HistoryDock(QWidget* parent) : QDockWidget(parent)
 {
    setObjectName("historyDock");
@@ -121,10 +124,12 @@ HistoryDock::HistoryDock(QWidget* parent) : QDockWidget(parent)
    connect(AkonadiBackend::getInstance(), SIGNAL(collectionChanged()),      this, SLOT(updateContactInfo()         ));
 }
 
+///Destructor
 HistoryDock::~HistoryDock()
 {
 }
 
+///Return the identity of the call caller
 QString HistoryDock::getIdentity(HistoryTreeItem* item)
 {
    if (item->getName().trimmed().isEmpty())
@@ -133,6 +138,7 @@ QString HistoryDock::getIdentity(HistoryTreeItem* item)
       return item->getName();
 }
 
+///Update informations
 void HistoryDock::updateContactInfo()
 {
    foreach(HistoryTreeItem* hitem, m_pHistory) {
@@ -140,6 +146,7 @@ void HistoryDock::updateContactInfo()
    }
 }
 
+///Reload the history list
 void HistoryDock::reload()
 {
    m_pItemView->clear();
@@ -209,6 +216,7 @@ void HistoryDock::reload()
    m_pItemView->sortItems(0,Qt::AscendingOrder);
 }
 
+///Enable the ability to set a date range like 1 month to limit history
 void HistoryDock::enableDateRange(bool enable)
 {
    m_pFromL->setVisible(enable);
@@ -220,6 +228,7 @@ void HistoryDock::enableDateRange(bool enable)
    ConfigurationSkeleton::setDisplayDataRange(enable);
 }
 
+///Filter the history
 void HistoryDock::filter(QString text)
 {
    foreach(HistoryTreeItem* item, m_pHistory) {
@@ -229,6 +238,7 @@ void HistoryDock::filter(QString text)
    m_pItemView->expandAll();
 }
 
+///When the data range is linked, change the opposite value when editing the first
 void HistoryDock::updateLinkedDate(KDateWidget* item, QDate& prevDate, QDate& newDate)
 {
    if (m_pLinkPB->isChecked()) {
@@ -251,20 +261,23 @@ void HistoryDock::updateLinkedDate(KDateWidget* item, QDate& prevDate, QDate& ne
    prevDate = newDate;
 }
 
+///The signals have to be disabled to prevent an ifinite loop
 void HistoryDock::updateLinkedFromDate(QDate date)
 {
-   disconnect(m_pToDW  ,  SIGNAL(changed(QDate)),       this, SLOT(updateLinkedToDate(QDate)));
+   disconnect (m_pToDW  ,  SIGNAL(changed(QDate)),       this, SLOT(updateLinkedToDate(QDate)));
    updateLinkedDate(m_pToDW,m_pCurrentFromDate,date);
-   connect(m_pToDW  ,  SIGNAL(changed(QDate)),       this, SLOT(updateLinkedToDate(QDate)));
+   connect    (m_pToDW  ,  SIGNAL(changed(QDate)),       this, SLOT(updateLinkedToDate(QDate)));
 }
 
+///The signals have to be disabled to prevent an ifinite loop
 void HistoryDock::updateLinkedToDate(QDate date)
 {
    disconnect(m_pFromDW  ,  SIGNAL(changed(QDate)),       this, SLOT(updateLinkedFromDate(QDate)));
    updateLinkedDate(m_pFromDW,m_pCurrentToDate,date);
-   connect(m_pFromDW  ,  SIGNAL(changed(QDate)),       this, SLOT(updateLinkedFromDate(QDate)));
+   connect   (m_pFromDW  ,  SIGNAL(changed(QDate)),       this, SLOT(updateLinkedFromDate(QDate)));
 }
 
+///Generate serializerd version of the content 
 QMimeData* HistoryTree::mimeData( const QList<QTreeWidgetItem *> items) const
 {
    qDebug() << "An history call is being dragged";
@@ -287,6 +300,7 @@ QMimeData* HistoryTree::mimeData( const QList<QTreeWidgetItem *> items) const
    return mimeData;
 }
 
+///Handle what happen when serialized data is dropped
 bool HistoryTree::dropMimeData(QTreeWidgetItem *parent, int index, const QMimeData *data, Qt::DropAction action)
 {
    Q_UNUSED(index)
@@ -300,6 +314,7 @@ bool HistoryTree::dropMimeData(QTreeWidgetItem *parent, int index, const QMimeDa
    return false;
 }
 
+///Handle keyboard input and redirect them to the filterbox
 void HistoryDock::keyPressEvent(QKeyEvent* event) {
    int key = event->key();
    if(key == Qt::Key_Escape)
diff --git a/kde/src/widgets/HistoryTreeItem.cpp b/kde/src/widgets/HistoryTreeItem.cpp
index 169ee5c053..f0ca222f54 100644
--- a/kde/src/widgets/HistoryTreeItem.cpp
+++ b/kde/src/widgets/HistoryTreeItem.cpp
@@ -37,6 +37,7 @@
 
 const char * HistoryTreeItem::callStateIcons[12] = {ICON_INCOMING, ICON_RINGING, ICON_CURRENT, ICON_DIALING, ICON_HOLD, ICON_FAILURE, ICON_BUSY, ICON_TRANSFER, ICON_TRANSF_HOLD, "", "", ICON_CONFERENCE};
 
+///Constructor
 HistoryTreeItem::HistoryTreeItem(QWidget *parent ,QString phone)
    : QWidget(parent), itemCall(0),m_pMenu(0), init(false)
 {
@@ -56,6 +57,7 @@ HistoryTreeItem::HistoryTreeItem(QWidget *parent ,QString phone)
    m_pAddToContact->setShortcut ( Qt::CTRL + Qt::Key_E         );
    m_pAddToContact->setText     ( "Add Number to Contact"      );
    m_pAddToContact->setIcon     ( KIcon("list-resource-add")   );
+   m_pAddToContact->setDisabled ( true                         );
    
    m_pAddContact->setShortcut   ( Qt::CTRL + Qt::Key_E         );
    m_pAddContact->setText       ( "Add Contact"                );
@@ -64,10 +66,12 @@ HistoryTreeItem::HistoryTreeItem(QWidget *parent ,QString phone)
    m_pCopy->setShortcut         ( Qt::CTRL + Qt::Key_C         );
    m_pCopy->setText             ( "Copy"                       );
    m_pCopy->setIcon             ( KIcon("edit-copy")           );
+   m_pCopy->setDisabled         ( true                         );
    
    m_pEmail->setShortcut        ( Qt::CTRL + Qt::Key_M         );
    m_pEmail->setText            ( "Send Email"                 );
    m_pEmail->setIcon            ( KIcon("mail-message-new")    );
+   m_pEmail->setDisabled        ( true                         );
 
    m_pBookmark->setShortcut     ( Qt::CTRL + Qt::Key_D         );
    m_pBookmark->setText         ( "Bookmark"                   );
@@ -108,16 +112,19 @@ HistoryTreeItem::HistoryTreeItem(QWidget *parent ,QString phone)
    }
 }
 
+///Destructor
 HistoryTreeItem::~HistoryTreeItem()
 {
    
 }
 
+///Return the call item
 Call* HistoryTreeItem::call() const
 {
    return itemCall;
 }
 
+///Set the call to be handled by this item
 void HistoryTreeItem::setCall(Call *call)
 {
    itemCall = call;
@@ -143,6 +150,7 @@ void HistoryTreeItem::setCall(Call *call)
    m_pPhoneNumber = itemCall->getPeerPhoneNumber();
 }
 
+///Can a contact be associed with this call?
 bool HistoryTreeItem::getContactInfo(QString phoneNumber)
 {
    Contact* contact = AkonadiBackend::getInstance()->getContactByPhone(phoneNumber);
@@ -159,6 +167,7 @@ bool HistoryTreeItem::getContactInfo(QString phoneNumber)
    return true;
 }
 
+///The item have to be updated
 void HistoryTreeItem::updated()
 {
    if (!getContactInfo(itemCall->getPeerPhoneNumber())) {
@@ -185,37 +194,43 @@ void HistoryTreeItem::updated()
    
 }
 
+///Return the time stamp
 uint HistoryTreeItem::getTimeStamp()
 {
    return m_pTimeStamp;
 }
 
+///Return the duration
 uint HistoryTreeItem::getDuration()
 {
    return m_pDuration;
 }
 
+///Return the caller name
 QString HistoryTreeItem::getName()
 {
    return m_pName;
 }
 
+///Return the caller peer number
 QString HistoryTreeItem::getPhoneNumber()
 {
    return m_pPhoneNumber;
 }
 
-
+///Get the index item assiciated with this widget
 QTreeWidgetItem* HistoryTreeItem::getItem()
 {
    return m_pItem;
 }
 
+///Set the index associed with this widget
 void HistoryTreeItem::setItem(QTreeWidgetItem* item)
 {
    m_pItem = item;
 }
 
+///Show the context menu
 void HistoryTreeItem::showContext(const QPoint& pos)
 {
    if (!m_pMenu) {
@@ -230,12 +245,14 @@ void HistoryTreeItem::showContext(const QPoint& pos)
    m_pMenu->exec(mapToGlobal(pos));
 }
 
-
+///Send an email
 void HistoryTreeItem::sendEmail()
 {
+   //TODO
    qDebug() << "Sending email";
 }
 
+///Call the caller again
 void HistoryTreeItem::callAgain()
 {
    if (itemCall) {
@@ -244,11 +261,14 @@ void HistoryTreeItem::callAgain()
    SFLPhone::model()->addDialingCall(m_pName, SFLPhone::app()->model()->getCurrentAccountId())->setCallNumber(m_pPhoneNumber);
 }
 
+///Copy the call
 void HistoryTreeItem::copy()
 {
+   //TODO
    qDebug() << "Copying contact";
 }
 
+///Create a contact from those informations
 void HistoryTreeItem::addContact()
 {
    qDebug() << "Adding contact";
@@ -258,10 +278,14 @@ void HistoryTreeItem::addContact()
    AkonadiBackend::getInstance()->addNewContact(aContact);
 }
 
+///Add this call number to an existing contact
 void HistoryTreeItem::addToContact()
 {
+   //TODO
    qDebug() << "Adding to contact";
 }
+
+///Bookmark this contact
 void HistoryTreeItem::bookmark()
 {
    qDebug() << "bookmark";
-- 
GitLab