phonebookui/phonebookservices/src/cntservicecontactfetchview.cpp
changeset 54 47627ab5d7a4
parent 50 77bc263e1626
child 61 d30183af6ca6
--- a/phonebookui/phonebookservices/src/cntservicecontactfetchview.cpp	Fri Jul 23 12:43:48 2010 +0300
+++ b/phonebookui/phonebookservices/src/cntservicecontactfetchview.cpp	Thu Aug 05 14:31:09 2010 +0300
@@ -26,6 +26,7 @@
 #include <hblistview.h>
 #include <hbdocumentloader.h>
 #include <hbaction.h>
+#include "cntactionpopup.h"
 
 #include <QCoreApplication>
 
@@ -35,7 +36,6 @@
     {
     HbAction* cancel = static_cast<HbAction*>( mDocument->findObject( "cnt:cancel" ) );
     mView->menu()->addAction( cancel );
-    
     connect(cancel,  SIGNAL(triggered()), this, SLOT(cancelFetch()) );
     connect( this, SIGNAL(viewClosed()), this, SLOT(closeFetchView()) );
     connect( this, SIGNAL(viewOpened(CntAbstractViewManager*, const CntViewParameters)), this, SLOT(aboutToOpenView(CntAbstractViewManager*, const CntViewParameters)) );
@@ -58,44 +58,171 @@
 
 void CntServiceContactFetchView::closeFetchView()
 {
-    CntServicesContactList serviceList;
     QContactManager* mgr = mMgr->contactManager(SYMBIAN_BACKEND);
     QModelIndexList temp = mListView->selectionModel()->selection().indexes();
+    mIndex = 0;
     for(int i = 0; i < temp.count(); i++ )
     {
         QContact contact = mListModel->contact(temp.at(i));
-        CntServicesContact servicesContact;
-
-        //get the name
-        servicesContact.mDisplayName = contact.displayLabel();
-
-        //get the phonenumber
-        QList<QContactPhoneNumber> phonenumbers = contact.details<QContactPhoneNumber>();
-        if(phonenumbers.count() > 0)
+        if (mAction == KCntActionCall)
+        {
+            serviceCallMessageAction(contact, KCntActionCall);
+        }
+        else if (mAction == KCntActionSms )
         {
-            servicesContact.mPhoneNumber = phonenumbers.first().number();
+            serviceCallMessageAction(contact, "message");
+        }
+        else if (mAction == KCntActionEmail)
+        {
+            serviceEmailAction(contact);
         }
         else
         {
-            servicesContact.mPhoneNumber = "";
+            serviceAllAction(contact);
         }
+    }
+    showPreviousView();
+}
+
+void CntServiceContactFetchView::serviceCallMessageAction(QContact& aContact, QString aAction)
+{
+    CntServicesContact servicesContact;
+    //get the phonenumber
+    QList<QContactPhoneNumber> phonenumbers = aContact.details<QContactPhoneNumber>();
+    
+    Q_ASSERT_X( phonenumbers.count() > 0, "serviceCallMessageAction", "Number count is zero");
+    
+    QContactDetail detail = aContact.preferredDetail(aAction);
+    if (!detail.isEmpty())
+    {
+        //Set preferred detail for services
+        QContactPhoneNumber number = detail;
+        servicesContact.mDisplayName = aContact.displayLabel();
+        servicesContact.mPhoneNumber = number.number();
+        servicesContact.mEmailAddress = "";
+        servicesContact.mContactId = aContact.localId();
+        mServiceList.append(servicesContact);
+    }
+    else 
+    {
+        // show popup for call numbers
+        CntActionPopup *actionPopup = new CntActionPopup(&aContact);
+        bool popup = actionPopup->showActionPopup(aAction);
+        if(popup)
+        {
+            //if call numbers are present wait for signals
+            mIndex++;
+            connect( actionPopup, SIGNAL(executeContactAction(QContact&,QContactDetail, QString)), this, 
+                    SLOT(popupContactAction(QContact&, QContactDetail, QString)));
+            connect( actionPopup, SIGNAL(actionPopupCancelPressed()), this, 
+                                SLOT(actionPopupCancelSlot()));
+        }
+        else 
+        {
+            //else delete popup if no call number present
+            delete actionPopup;
+        }
+    }
+}
 
+void CntServiceContactFetchView::serviceEmailAction(QContact& aContact)
+{
+    CntServicesContact servicesContact;
+    //get the email address
+    QList<QContactEmailAddress> emailAddresses = aContact.details<QContactEmailAddress>();
+    
+    Q_ASSERT_X( emailAddresses.count() > 0, "serviceEmailAction", "email address count is zero");
+    
+    QContactDetail detail = aContact.preferredDetail("email");
+    if (!detail.isEmpty())
+    {
+        //Set preferred detail for services
+        QContactEmailAddress email = detail;
+        servicesContact.mDisplayName = aContact.displayLabel();
+        servicesContact.mPhoneNumber = "";
+        servicesContact.mEmailAddress = email.emailAddress();
+        servicesContact.mContactId = aContact.localId();
+        mServiceList.append(servicesContact);
+        
+    }
+    else 
+    {
+        // show popup for email id's 
+        CntActionPopup *actionPopup = new CntActionPopup(&aContact);
+        bool popup = actionPopup->showActionPopup("email");
+        if(popup)
+        {
+            //if email id's are present wait for signals
+            mIndex++;
+            connect( actionPopup, SIGNAL(executeContactAction(QContact&,QContactDetail, QString)), this, 
+                    SLOT(popupContactAction(QContact&, QContactDetail, QString)));
+            connect( actionPopup, SIGNAL(actionPopupCancelPressed()), this, 
+                                SLOT(actionPopupCancelSlot()));
+        }
+        else 
+        {
+            //else delete popup if no email present
+            delete actionPopup;
+        }
+    }
+}
+
+void CntServiceContactFetchView::serviceAllAction(QContact& aContact)
+{
+    CntServicesContact servicesContact;
+    servicesContact.mDisplayName = aContact.displayLabel();
+    QContactDetail callDetail = aContact.preferredDetail("call");
+    //Check for preferredDetail from call
+    // if not present check for preferredDetail for message
+    // if not present pick first phone number
+    if (!callDetail.isEmpty())
+    {
+        QContactPhoneNumber number = callDetail;
+        servicesContact.mPhoneNumber = number.number();
+    }
+    else
+    {
+        QContactDetail smsDetail = aContact.preferredDetail("message");
+        if (!callDetail.isEmpty())
+        {
+            QContactPhoneNumber number = smsDetail;
+            servicesContact.mPhoneNumber = number.number();
+        }
+        else 
+        {
+            QList<QContactPhoneNumber> phonenumbers = aContact.details<QContactPhoneNumber>();
+            if(phonenumbers.count() > 0)
+            {
+            servicesContact.mPhoneNumber = phonenumbers.first().number();
+            }
+            else
+            {
+            servicesContact.mPhoneNumber = "";
+            }
+        }
+    }
+    
+    QContactDetail emailDetail = aContact.preferredDetail("email");
+    if (!emailDetail.isEmpty())
+    {
+        QContactEmailAddress email = emailDetail;
+        servicesContact.mEmailAddress = email.emailAddress();
+    }
+    else
+    {
         //get first email address
-        QList<QContactEmailAddress> emailAddresses = contact.details<QContactEmailAddress>();
+        QList<QContactEmailAddress> emailAddresses = aContact.details<QContactEmailAddress>();
         if(emailAddresses.count() > 0)
         {
             servicesContact.mEmailAddress = emailAddresses.first().emailAddress();
         }
-        //contact id
-        servicesContact.mContactId = contact.localId();
-
-        //append it to the list
-        serviceList.append(servicesContact);
+        else 
+        {
+            servicesContact.mEmailAddress = "";
+        }
     }
-
-    QVariant variant;
-    variant.setValue(serviceList);
-    mProvider.CompleteServiceAndCloseApp(variant);
+    servicesContact.mContactId = aContact.localId();
+    mServiceList.append(servicesContact);
 }
 
 void CntServiceContactFetchView::aboutToOpenView(CntAbstractViewManager* aMgr, const CntViewParameters aArgs)
@@ -107,26 +234,26 @@
     mView->setTitle(title);
     
     // Set action filter
-    QString action = aArgs.value(ESelectedAction).toString();
+    mAction = aArgs.value(ESelectedAction).toString();
     // ESelectedAction is defined in cntviewparams.h
 
     // Has never been implemented.
     //QString filterStr = aArgs.value(KCntServiceViewParamFilter).toString();
     // KCntServiceViewParamFilter is defined in cntserviceviewparams.h
 
-    if (action == KCntActionSms)
+    if (mAction == KCntActionSms)
         {
             QContactActionFilter actionFilter;
             actionFilter.setActionName("message");
             mListModel->setFilter(actionFilter);
         }
-        else if (action == KCntActionCall)
+        else if (mAction == KCntActionCall)
         {
             QContactActionFilter actionFilter;
             actionFilter.setActionName("call");
             mListModel->setFilter(actionFilter);
         }
-        else if (action == KCntActionEmail)
+        else if (mAction == KCntActionEmail)
         {
             QContactActionFilter actionFilter;
             actionFilter.setActionName("email");
@@ -148,4 +275,59 @@
         }
 }
 
+void CntServiceContactFetchView::popupContactAction(QContact& aContact,QContactDetail contactDetail, QString aAction)
+{
+    if (aAction.compare("call", Qt::CaseInsensitive) == 0 
+           || aAction.compare("message", Qt::CaseInsensitive) == 0 )
+       {       
+           CntServicesContact servicesContact;
+           QContactPhoneNumber number = static_cast<QContactPhoneNumber>(contactDetail);
+           QString name = aContact.displayLabel();
+           servicesContact.mDisplayName = aContact.displayLabel();
+           servicesContact.mPhoneNumber = number.number();
+           servicesContact.mEmailAddress = "";
+           servicesContact.mContactId = aContact.localId();
+           mServiceList.append(servicesContact);
+           mIndex--;
+       }
+       else if (aAction.compare("email", Qt::CaseInsensitive) == 0 )
+       {
+           CntServicesContact servicesContact;
+           QContactEmailAddress email = static_cast<QContactEmailAddress>(contactDetail);
+           servicesContact.mDisplayName = aContact.displayLabel();
+           servicesContact.mPhoneNumber = "";
+           servicesContact.mEmailAddress = email.emailAddress();
+           servicesContact.mContactId = aContact.localId();
+           mServiceList.append(servicesContact);
+           mIndex--;
+       }
+    
+    if (aContact.preferredDetail(aAction).isEmpty() && (aAction == "call" || aAction == "message" || aAction == "email"))
+    {
+        aContact.setPreferredDetail(aAction, contactDetail);
+        //return value will be ignored because we cannot do anything if it fails.
+        mMgr->contactManager(SYMBIAN_BACKEND)->saveContact(&aContact);
+    }
+    
+    showPreviousView();
+}
+
+void CntServiceContactFetchView::actionPopupCancelSlot()
+{
+    mIndex--;
+    showPreviousView();
+}
+
+void CntServiceContactFetchView::showPreviousView()
+{
+    if(mIndex==0)
+    {
+        QVariant variant;
+        variant.setValue(mServiceList);
+        mProvider.CompleteServiceAndCloseApp(variant);
+        
+        CntViewParameters args;
+        mMgr->back( args );
+    }
+}
 // EOF