phonebookui/pbkcommonui/src/cntcontactcardview_p.cpp
changeset 47 7cbcb2896f0e
parent 46 efe85016a067
child 50 77bc263e1626
child 59 a642906a277a
--- a/phonebookui/pbkcommonui/src/cntcontactcardview_p.cpp	Wed Jun 23 18:02:44 2010 +0300
+++ b/phonebookui/pbkcommonui/src/cntcontactcardview_p.cpp	Tue Jul 06 14:05:47 2010 +0300
@@ -62,6 +62,7 @@
 #include "cntfavourite.h"
 #include "cntactionlauncher.h"
 #include "cntpresencelistener.h"
+#include "cntactionpopup.h"
 
 #define CNT_MAPTILE_PROGRESS_TIMER  100 //100 msec
 #define CNT_UNKNOWN_MAPTILE_STATUS  -1
@@ -124,9 +125,12 @@
     connect(mProgressTimer, SIGNAL(timeout()),this, SLOT(updateSpinningIndicator())); 
 
     mMaptile = new CntMapTileService;
-    //Connect for maptile status evenet
-    bool ret = QObject::connect( mMaptile, SIGNAL(maptileFetchingStatusUpdate(int, 
+    if( mMaptile->isLocationFeatureEnabled() )
+    {
+        //Connect for maptile status evenet
+        QObject::connect( mMaptile, SIGNAL(maptileFetchingStatusUpdate(int, 
             int,int)),this,SLOT(mapTileStatusReceived(int,int,int)));
+    }
 }
 
 /*!
@@ -170,6 +174,16 @@
     
     delete mProgressTimer;
     mProgressTimer = NULL;
+    
+    //delete maptile label memory
+    for ( int index = 0; index < mMaptileLabelList.count(); index++ )
+    {
+        if( mMaptileLabelList[index] )
+        {
+           delete mMaptileLabelList[index];
+           mMaptileLabelList[index] = NULL ;
+        }
+    }
 }
 
 /*!
@@ -183,15 +197,7 @@
     QContact contact = contactManager()->contact(mContact->localId());
     if ( contact != *mContact )
     {
-        QList<QContactAvatar> details = mContact->details<QContactAvatar>();
-        for (int i = 0; i < details.count(); i++)
-        {
-            if (!details.at(i).imageUrl().isEmpty())
-            {
-                contactManager()->saveContact(mContact);
-                break;
-            }
-        }
+        contactManager()->saveContact(mContact);
     }
        
     mViewManager->back( mArgs );
@@ -262,7 +268,8 @@
     }
     
     // data
-    mDataContainer = new CntContactCardDataContainer(mContact, NULL, myCard, mMaptile );
+    mDataContainer = new CntContactCardDataContainer( 
+            mContact, NULL, myCard, mMaptile, mView->mainWindow()->orientation() );
 
     // scroll area + container widget
     mScrollArea = static_cast<HbScrollArea*>(document()->findWidget(QString("scrollArea")));
@@ -315,21 +322,22 @@
         {
             //map support (image only)
             if (pos >= CntContactCardDataItem::EAddress && pos <= CntContactCardDataItem::EAddressWork && !dataItem->icon().isNull())
-            {         
-                HbLabel* iconLabel = new HbLabel(mView);
-                iconLabel->setIcon(dataItem->icon());
-                
-                int width = dataItem->icon().width();
-                int height = dataItem->icon().height();    
+            {        
+                int addressType = CntMapTileService::AddressPreference;
                 
-                //HbLabel setPreferredSize is not working properly,
-                //so added minimum , maximum size to fix the issue
-                iconLabel->setPreferredSize(QSizeF(width,height));
-                iconLabel->setMinimumSize(QSizeF(width, height));
-                iconLabel->setMaximumSize(QSizeF(width, height));
-                iconLabel->setSizePolicy(QSizePolicy(QSizePolicy::Fixed,
-                                    QSizePolicy::Fixed));            
-                mContainerLayout->addItem(iconLabel);
+                if( pos == CntContactCardDataItem::EAddressHome  )
+                {
+                    addressType  = CntMapTileService::AddressHome;
+                }
+                else if( pos == CntContactCardDataItem::EAddressWork )
+                {
+                    addressType  = CntMapTileService::AddressWork;
+                }
+                  
+                HbLabel* maptileLabel = loadMaptileLabel( addressType );
+                setMaptileLabel( maptileLabel, dataItem->icon() );
+                mContainerLayout->addItem(  maptileLabel );
+                mMaptileLabelList.insert( addressType, maptileLabel );
             } 
             //other details
             else
@@ -456,53 +464,45 @@
             {
                  if( mAddressList[index]->maptileStatus == CntMapTileService::MapTileFetchingCompleted )
                  {
-                     //Empty icon. Clear the search stop icon
-                     HbIcon icon;
-                     mAddressList[index]->mDetailItem->setSecondaryIconItem( icon );
-                         
+ 
+                     //Read the maptile path and update the image
                      QString imagePath;
-                                     
-                     //Read the maptile path and update the image
-                     mMaptile->getMapTileImage( contactId, sourceAddressType, imagePath );
+                     mMaptile->getMapTileImage( 
+                                contactId, sourceAddressType, imagePath, mView->mainWindow()->orientation() );
+					 
                      if( !imagePath.isEmpty() )
                      {
+                         //Empty icon. Clear the inprogress  icon
+                         HbIcon emptyIcon;
+                         mAddressList[index]->mDetailItem->setSecondaryIconItem( emptyIcon );
+                         
                          HbIcon icon( imagePath );
                          
-                         HbLabel* iconLabel = new HbLabel(mView);
-                         iconLabel->setIcon(icon);
-                         
-                         int width = icon.width();
-                         int height = icon.height();    
-                         
-                         //HbLabel setPreferredSize is not working properly,
-                         //so added minimum , maximum size to fix the issue
-                         iconLabel->setPreferredSize(QSizeF(width,height));
-                         iconLabel->setMinimumSize(QSizeF(width, height));
-                         iconLabel->setMaximumSize(QSizeF(width, height));
-                         iconLabel->setSizePolicy(QSizePolicy(QSizePolicy::Fixed,
-                                             QSizePolicy::Fixed));        
+                         HbLabel* maptileLabel = loadMaptileLabel( sourceAddressType );
+                         setMaptileLabel( maptileLabel, icon );
+                         mMaptileLabelList.insert( sourceAddressType, maptileLabel );
                         
                          //find the index of the item and insert maptile in the next index 
                          for( int itemIndex = 0 ; itemIndex < mContainerLayout->count(); itemIndex++ )
                          {
                              if( mContainerLayout->itemAt(itemIndex) == mAddressList[index]->mDetailItem )
                              {
-                                 mContainerLayout->insertItem( itemIndex+1, iconLabel );
+                                 mContainerLayout->insertItem( itemIndex+1, maptileLabel );
                                  break;
                              }
                          }
                          
                      }
+                     else
+                     {
+                         //Maptile image not available. Show the search stop icon
+                         setMaptileSearchStopIcon( index );
+                     }
                  }
                  else
                  {
                      //Maptile fetching failed. Show the search stop icon
-                     QString iconName(CNT_MAPTILE_SEARCH_STOP_ICON);
-                     HbIcon icon(iconName);
-                     mAddressList[index]->mDetailItem->setSecondaryIconItem( icon );
-                     bool visible = mAddressList[index]->mDetailItem->isVisible();
-                     mAddressList[index]->mDetailItem->update();
- 
+                     setMaptileSearchStopIcon( index );
                  }
                  
                  delete mAddressList[index];
@@ -518,6 +518,20 @@
 }
 
 /*
+* Sets the search stop icon to secondary icon item
+*/
+void CntContactCardViewPrivate::setMaptileSearchStopIcon( int index )
+{
+    if( index < mAddressList.count() )
+    {
+        QString iconName(CNT_MAPTILE_SEARCH_STOP_ICON);
+        HbIcon icon(iconName);
+        mAddressList[index]->mDetailItem->setSecondaryIconItem( icon );
+        mAddressList[index]->mDetailItem->update();  
+    }
+}
+
+/*
 * Slot to receive the maptile status information
 */
 void CntContactCardViewPrivate::mapTileStatusReceived( int contactid, int addressType, int status)
@@ -535,6 +549,101 @@
     updateSpinningIndicator();
 }
 
+/*
+* Updates correct maptile image when screen orientation changes.
+*/
+void CntContactCardViewPrivate::updateMaptileImage()
+{
+    //If there is no maptile displayed, return immediately
+    if( mMaptileLabelList.count() > 0 )
+    {
+        QContactLocalId contactId = mContact->id().localId();
+        
+        QList<QContactAddress> addressDetails = mContact->details<QContactAddress>();
+        
+        //address
+        QString contextHome(QContactAddress::ContextHome.operator QString());
+        QString contextWork(QContactAddress::ContextWork.operator QString());
+        CntMapTileService::ContactAddressType sourceAddressType 
+                                             = CntMapTileService::AddressPreference;
+        
+        QString imagePath;
+        
+        for ( int i = 0; i < addressDetails.count(); i++ )
+        {
+            if ( !addressDetails[i].contexts().isEmpty() && 
+                   addressDetails[i].contexts().at(0) == contextHome )
+            {
+                sourceAddressType = CntMapTileService::AddressHome;
+            }
+            else if ( !addressDetails[i].contexts().isEmpty() && 
+                         addressDetails[i].contexts().at(0) == contextWork )
+            {
+                sourceAddressType = CntMapTileService::AddressWork;
+            }
+            
+            int status = mMaptile->getMapTileImage( 
+                                             contactId, 
+                                             sourceAddressType, 
+                                             imagePath, 
+                                             mView->mainWindow()->orientation() );
+            if( !imagePath.isEmpty() )
+            {
+                HbIcon icon( imagePath );
+                HbLabel* label = mMaptileLabelList.value( sourceAddressType );
+                if( label )
+                {
+                    setMaptileLabel( label, icon );
+                }
+            }
+        }
+    }
+}
+
+
+/*
+* Asscoiate the maptile label widget with maptile image
+*/
+void CntContactCardViewPrivate::setMaptileLabel( HbLabel*& mapLabel, const HbIcon& icon )
+{
+    mapLabel->clear();
+    mapLabel->setIcon( icon );
+    
+    int width = icon.width();
+    int height = icon.height();    
+                    
+    //HbLabel setPreferredSize is not working properly,
+    //so added minimum , maximum size to fix the issue
+    mapLabel->setPreferredSize(QSizeF(width,height));
+    mapLabel->setMinimumSize(QSizeF(width, height));
+    mapLabel->setMaximumSize(QSizeF(width, height));
+    mapLabel->setSizePolicy(QSizePolicy( QSizePolicy::Fixed,
+                        QSizePolicy::Fixed));            
+}
+
+/*
+* Load the maptile label based on the address type
+*/
+HbLabel* CntContactCardViewPrivate::loadMaptileLabel( int addressType )
+{
+    HbLabel* maptileLabel = NULL ;
+    
+    if( addressType == CntMapTileService::AddressPreference )
+    {
+        maptileLabel = static_cast<HbLabel*>(document()->findWidget(QString("maptilePreferenceWidget")));
+    }
+    else if( addressType == CntMapTileService::AddressHome  )
+    {
+        maptileLabel  = static_cast<HbLabel*>(document()->findWidget(QString("maptileHomeWidget")));
+    }
+    else if( addressType == CntMapTileService::AddressWork )
+    {
+        maptileLabel  = static_cast<HbLabel*>(document()->findWidget(QString("maptileWorkWidget")));
+    }
+                            
+    return maptileLabel;
+}
+
 void CntContactCardViewPrivate::thumbnailReady(const QPixmap& pixmap, void *data, int id, int error)
 {
     CNT_ENTRY
@@ -543,10 +652,9 @@
     Q_UNUSED(id);
     if (!error)
     {
-        QIcon qicon(pixmap);
-        HbIcon icon(qicon);
+        HbIcon icon(pixmap);
         mHeadingItem->setIcon(icon);
-        mVCardIcon = new HbIcon(qicon);
+        mVCardIcon = new HbIcon(pixmap);
         mImageLabel->clear();
         mImageLabel->setIcon(icon);
     }
@@ -616,7 +724,7 @@
     QString name = contactManager()->synthesizedDisplayLabel(*mContact);
     
     HbMessageBox::question(HbParameterLengthLimiter(hbTrId("txt_phob_info_delete_1")).arg(name), this, SLOT(handleDeleteContact(HbAction*)),
-            hbTrId("txt_phob_button_delete"), hbTrId("txt_common_button_cancel"));
+            hbTrId("txt_common_button_delete"), hbTrId("txt_common_button_cancel"));
 }
 
 /*!
@@ -629,8 +737,7 @@
     if (note && action == note->actions().first())
     {
         contactManager()->removeContact(mContact->localId());
-        mArgs.insert(EViewId, namesView);
-        mViewManager->changeView( mArgs );
+        mViewManager->back( mArgs );
     }
 }
 
@@ -714,6 +821,8 @@
         // reading "landscape" section
         document()->load(CNT_CONTACTCARDVIEW_XML, "landscape");
     }
+	//Update the maptile image
+	updateMaptileImage();
 }
 
 /*!
@@ -726,10 +835,10 @@
     QString action = mDataContainer->dataItem(index)->action();
     // Check if action is internal
     QList<QContactActionDescriptor> actionDescriptors = QContactAction::actionDescriptors(action, "symbian", 1);
-    if(0 < actionDescriptors.count())
+    if (0 < actionDescriptors.count())
     {
         // These actions are considered internal(vendor=symbian and version=1)
-        executeAction(*mContact, mDataContainer->dataItem(index)->detail(), action);
+        executeAction(*mContact, mDataContainer->dataItem(index)->detail(), action, item);
     }
     else
     {
@@ -741,11 +850,36 @@
 /*!
 Execute the call / message / email action
 */
-void CntContactCardViewPrivate::executeAction(QContact& aContact, QContactDetail aDetail, QString aAction)
+void CntContactCardViewPrivate::executeAction(QContact& aContact, const QContactDetail& aDetail, const QString& aAction, CntContactCardDetailItem* aItem)
 {
-    CntActionLauncher* other = new CntActionLauncher( aAction );
+    CntActionLauncher* other = new CntActionLauncher(*contactManager(), aAction);
     connect(other, SIGNAL(actionExecuted(CntActionLauncher*)), this, SLOT(actionExecuted(CntActionLauncher*)));
-    other->execute(aContact, aDetail);
+    if (aItem && aContact.preferredDetail(aAction).isEmpty())
+    {
+        setPreferredAction(aAction, aDetail, aItem);
+    }
+    other->execute(aContact, aDetail);  
+}
+
+
+/*!
+Execute the call / message / email action
+*/
+void CntContactCardViewPrivate::executeAction(QContact& aContact, const QContactDetail& aDetail, const QString& aAction)
+{ 
+    //TODO: need refactoring
+    CntContactCardDetailItem* detailItem = NULL;
+    for (int index = 0; index < mDataContainer->itemCount(); index++)
+    {
+        QContactDetail detail = mDataContainer->dataItem(index)->detail();
+        QString action = mDataContainer->dataItem(index)->action();
+        if (detail == aDetail && action == aAction)
+        {
+            detailItem = static_cast<CntContactCardDetailItem*>(mContainerLayout->itemAt(index));
+        }
+    }
+    executeAction(aContact, aDetail, aAction, detailItem);
+    mAcceptSendKey=true;
 }
 
 /*!
@@ -753,8 +887,8 @@
 */
 void CntContactCardViewPrivate::executeDynamicAction(QContact& aContact, QContactDetail aDetail, QContactActionDescriptor aActionDescriptor)
 {
-    CntActionLauncher* other = new CntActionLauncher( );
-    connect(other, SIGNAL(actionExecuted(CntAction*)), this, SLOT(actionExecuted(CntAction*)));
+    CntActionLauncher* other = new CntActionLauncher(*contactManager());
+    connect(other, SIGNAL(actionExecuted(CntActionLauncher*)), this, SLOT(actionExecuted(CntActionLauncher*)));
     other->execute(aContact, aDetail, aActionDescriptor);
 }
 
@@ -764,15 +898,6 @@
 }
 
 /*!
-Set selected detail as preferred for selected action
-*/
-void CntContactCardViewPrivate::setPreferredAction(const QString &aAction, const QContactDetail &aDetail)
-{
-    mContact->setPreferredDetail(aAction, aDetail);
-    contactManager()->saveContact(mContact);
-}
-
-/*!
 Item specific menu
 */
 void CntContactCardViewPrivate::onLongPressed(const QPointF &aCoords)
@@ -893,13 +1018,13 @@
     
     if ( name == "communicationAction" )
     {
-        executeAction( *mContact, detail, action );
+        executeAction(*mContact, detail, action, item);
     }
 
     if ( name == "videoCommunicationAction" )
     {
         //service name latter on should come from service table.
-        executeAction( *mContact, detail, "videocall" );
+        executeAction(*mContact, detail, "videocall", item);
     }
     if ( name == "dynamicAction" )
     {             
@@ -908,22 +1033,34 @@
     
     if ( name == "preferredAction" )
     {
-        setPreferredAction(action, detail);
-                    
-        if (mPreferredItems.contains(action))
-        {
-            CntContactCardDetailItem *oldItem = mPreferredItems.value(action);
-            mDataContainer->dataItem(oldItem->index())->setSecondaryIcon(HbIcon());
-            oldItem->setDetails(mDataContainer->dataItem(oldItem->index()));
-        }
-            
-        mDataContainer->dataItem(item->index())->setSecondaryIcon(HbIcon("qtg_mono_favourites"));
-        item->setDetails(mDataContainer->dataItem(item->index()));
-        
-        mPreferredItems.insert(action, item);
+        setPreferredAction(action, detail, item);
     }
 }
 
+/*!
+Set selected detail as preferred for selected action in detail item
+*/
+void CntContactCardViewPrivate::setPreferredAction(const QString &aAction, const QContactDetail &aDetail, CntContactCardDetailItem *aDetailItem)
+{
+    if (aAction == "call" || aAction == "message" || aAction == "email")
+    {
+        mContact->setPreferredDetail(aAction, aDetail);
+        contactManager()->saveContact(mContact);
+        if (mPreferredItems.contains(aAction))
+        {
+           CntContactCardDetailItem *oldItem = mPreferredItems.value(aAction);
+           mDataContainer->dataItem(oldItem->index())->setSecondaryIcon(HbIcon());
+           oldItem->setDetails(mDataContainer->dataItem(oldItem->index()));
+        }
+        
+        mDataContainer->dataItem(aDetailItem->index())->setSecondaryIcon(HbIcon("qtg_mono_favourites"));
+        aDetailItem->setDetails(mDataContainer->dataItem(aDetailItem->index()));
+        
+        mPreferredItems.insert(aAction, aDetailItem);
+    }
+}
+
+
 void CntContactCardViewPrivate::handleSendBusinessCard( HbAction* aAction )
 {
     Q_UNUSED(aAction);
@@ -1047,7 +1184,10 @@
                     imageUtility.removeImage(filePath);
                 }
             }
-            mHeadingItem->setIcon(HbIcon("qtg_large_avatar"));
+            mAvatar->setImageUrl(QUrl());
+            mImageLabel->clear();
+            mImageLabel->setIcon(HbIcon("qtg_large_add_contact_picture"));
+            mHeadingItem->setIcon(HbIcon("qtg_large_add_contact_picture"));
             contactManager()->saveContact(mContact);
         }
     }
@@ -1099,98 +1239,50 @@
 
 void CntContactCardViewPrivate::sendKeyPressed()
 {   
-    QContactDetail preferredDetail = mContact->preferredDetail("call");
-    if (!preferredDetail.isEmpty())
+    int count = 0;
+    for (int index = 0; index < mDataContainer->itemCount(); index++)
     {
-        executeAction(*mContact, preferredDetail, "call"); 
-    }
-    else 
-    {   
-        if(!mSendKeyListModel)
-        {
-            mSendKeyListModel = new QStandardItemModel();
-        }
-        mSendKeyListModel->clear();
-        for (int index = 0; index < mDataContainer->itemCount(); index++)
-        {
-            CntContactCardDataItem* dataItem = mDataContainer->dataItem(index);
-            if (dataItem->action().compare("call", Qt::CaseInsensitive) == 0)
-            {
-                QStandardItem *labelItem = new QStandardItem();
-                QStringList textList;
-                textList << dataItem->titleText() << dataItem->valueText();
-                labelItem->setData(textList, Qt::DisplayRole);
-                labelItem->setData(dataItem->icon(), Qt::DecorationRole);
-                labelItem->setData(index, Qt::UserRole);
-                mSendKeyListModel->appendRow(labelItem);
-            }
-        }
-  
-        if (mSendKeyListModel->rowCount() == 0)
-        {
-            XQServiceRequest snd("com.nokia.services.logsservices.starter", "start(int,bool)", false);
-            snd << 0; // all calls
-            snd << true; // show dialpad
-            snd.send();
-        }
-        else if (mSendKeyListModel->rowCount() ==1 )
+        CntContactCardDataItem* dataItem = mDataContainer->dataItem(index);
+        if (dataItem->action().compare("call", Qt::CaseInsensitive) == 0)
         {
-            QContactDetail detail = mDataContainer->dataItem(0)->detail();
-            executeAction( *mContact, detail, "call" ); 
+            count++;
         }
-        else if (mSendKeyListModel->rowCount() >= 2 && mAcceptSendKey)
+    }
+    if (!count)
+    {
+        XQServiceRequest snd("com.nokia.services.logsservices.starter", "start(int,bool)", false);
+        snd << 0; // all calls
+        snd << true; // show dialpad
+        snd.send();
+    }
+    else
+    {
+        QContactDetail preferredDetail = mContact->preferredDetail("call");
+        if (!preferredDetail.isEmpty())
         {
+            executeAction(*mContact, preferredDetail, "call", NULL); 
+        }
+        else if (count == 1 )
+        {
+           mContact->setPreferredDetail("call", mContact->details<QContactPhoneNumber>().first());
+           executeAction( *mContact, mContact->details<QContactPhoneNumber>().first(), "call", NULL); 
+        }
+        else if(count >= 2 && mAcceptSendKey)
+        {   
             mAcceptSendKey = false;
-            
-            // Instantiate a dialog        
-            mSendKeyPopup = new HbSelectionDialog();
-            mSendKeyPopup->clearActions(); 
-            mSendKeyPopup->setAttribute(Qt::WA_DeleteOnClose, true);
-            mSendKeyPopup->setDismissPolicy(HbPopup::NoDismiss);
-            mSendKeyPopup->setTimeout(HbPopup::NoTimeout);
-            mSendKeyPopup->setModal(true);
+            CntActionPopup *actionPopup = new CntActionPopup(mContact);
+            actionPopup->showActionPopup("call");
+            connect( actionPopup, SIGNAL(executeContactAction(QContact&, QContactDetail, QString)), this, 
+                    SLOT(executeAction(QContact&, QContactDetail, QString)));   
+            connect( actionPopup, SIGNAL(actionPopupCancelPressed()), this, 
+                                SLOT(sendKeyCancelSlot()));   
             
-            HbListView* listView = new HbListView(mSendKeyPopup);
-            listView->setModel(mSendKeyListModel);
-            listView->setScrollingStyle(HbScrollArea::PanWithFollowOn);
-            listView->setFrictionEnabled(true);
-            
-            connect(listView, SIGNAL(activated (const QModelIndex&)), this,
-                SLOT(launchSendKeyAction(const QModelIndex&)));
-            
-            QString contactName = mContact->displayLabel();
-        
-            HbLabel *headingText = new HbLabel(mSendKeyPopup);
-            headingText->setPlainText(contactName);
-            
-            mSendKeyPopup->setHeadingWidget(headingText);
-            mSendKeyPopup->setContentWidget(listView);
-            
-            HbAction *cancelAction = new HbAction(hbTrId("txt_phob_button_cancel"), mSendKeyPopup);
-            mSendKeyPopup->addAction(cancelAction);
-            
-            // Launch dialog asyncronously
-            mSendKeyPopup->open(this, SLOT(sendKeyDialogSlot(HbAction*)));
         }
     }
 }
 
-void CntContactCardViewPrivate::sendKeyDialogSlot(HbAction* action)
-{
-    Q_UNUSED(action);
-    mAcceptSendKey = true;
-}
-
-void CntContactCardViewPrivate::launchSendKeyAction(const QModelIndex &index)
+void CntContactCardViewPrivate::sendKeyCancelSlot()
 {
     mAcceptSendKey = true;
-    mSendKeyPopup->close();
-    if (index.isValid())
-    {
-        int row = index.row();
-        int selectedDetail = mSendKeyListModel->item(index.row())->data(Qt::UserRole).toInt();
-        executeAction(*mContact, mDataContainer->dataItem(selectedDetail)->detail(), "call");
-    }
 }
-
 // end of file