phonebookui/pbkcommonui/src/cntcontactcardview_p.cpp
changeset 31 2a11b5b00470
parent 27 de1630741fbe
child 37 fd64c38c277d
equal deleted inserted replaced
27:de1630741fbe 31:2a11b5b00470
     8 *
     8 *
     9 * Initial Contributors:
     9 * Initial Contributors:
    10 * Nokia Corporation - initial contribution.
    10 * Nokia Corporation - initial contribution.
    11 *
    11 *
    12 * Contributors:
    12 * Contributors:
    13 *
       
    14 * Description:
    13 * Description:
    15 *
    14 *
    16 */
    15 */
    17 
    16 
    18 #include "cntcontactcardview_p.h"
    17 #include "cntcontactcardview_p.h"
    28 #include <hbtoolbar.h>
    27 #include <hbtoolbar.h>
    29 #include <hbgroupbox.h>
    28 #include <hbgroupbox.h>
    30 #include <hbaction.h>
    29 #include <hbaction.h>
    31 #include <hbmessagebox.h>
    30 #include <hbmessagebox.h>
    32 #include <hbicon.h>
    31 #include <hbicon.h>
       
    32 #include <hbparameterlengthlimiter.h>
       
    33 #include <hbframeitem.h>
       
    34 #include <hbframedrawer.h>
    33 #include <shareui.h>
    35 #include <shareui.h>
    34 #include <thumbnailmanager_qt.h>
    36 #include <thumbnailmanager_qt.h>
    35 #include <cntmaptileservice.h>  //For maptile processing
    37 #include <cntmaptileservice.h>  //For maptile processing
    36 #include <qversitcontactexporter.h>
    38 #include <qversitcontactexporter.h>
    37 #include <qversitwriter.h>
    39 #include <qversitwriter.h>
    38 #include <xqservicerequest.h>
    40 #include <xqservicerequest.h>
    39 #include "cntcontactcarddatacontainer.h"
    41 #include "cntcontactcarddatacontainer.h"
    40 #include "cntcontactcarddetailitem.h"
    42 #include "cntcontactcarddetailitem.h"
    41 #include "cntcontactcardheadingitem.h"
    43 #include "cntcontactcardheadingitem.h"
    42 #include "cntcontactcarddataitem.h"
    44 #include "cntcontactcarddataitem.h"
       
    45 #include "cntcontactcardcontextmenu.h"
    43 #include "cntmainwindow.h"
    46 #include "cntmainwindow.h"
    44 #include "cntstringmapper.h"
    47 #include "cntstringmapper.h"
    45 #include "cntdocumentloader.h"
    48 #include "cntdocumentloader.h"
    46 #include "cntimagelabel.h"
    49 #include "cntimagelabel.h"
       
    50 #include "cntimageutility.h"
    47 
    51 
    48 const char *CNT_CONTACTCARDVIEW_XML = ":/xml/contacts_contactcard.docml";
    52 const char *CNT_CONTACTCARDVIEW_XML = ":/xml/contacts_contactcard.docml";
    49 
    53 
    50 /*!
    54 /*!
    51 Constructor, initialize member variables.
    55 Constructor, initialize member variables.
    63     mThumbnailManager(NULL),
    67     mThumbnailManager(NULL),
    64     mGroupContact(NULL),
    68     mGroupContact(NULL),
    65     mAvatar(NULL),
    69     mAvatar(NULL),
    66     mIsGroupMember(false),
    70     mIsGroupMember(false),
    67     mIsHandlingMenu(false),
    71     mIsHandlingMenu(false),
       
    72     mIsPreviousImageEditorView(false),
    68     mFavoriteGroupId(-1),
    73     mFavoriteGroupId(-1),
    69     mLoader(NULL),
    74     mLoader(NULL),
    70     mContactAction(NULL),
    75     mContactAction(NULL),
    71     mBackKey(NULL),
    76     mBackKey(NULL),
    72     mImageLabel(NULL),
    77     mImageLabel(NULL),
   124 */
   129 */
   125 void CntContactCardViewPrivate::showPreviousView()
   130 void CntContactCardViewPrivate::showPreviousView()
   126 {
   131 {
   127     emit backPressed();
   132     emit backPressed();
   128     
   133     
       
   134     //save an avatar
       
   135     if (mIsPreviousImageEditorView)
       
   136     {
       
   137         QList<QContactAvatar> details = mContact->details<QContactAvatar>();
       
   138         for (int i = 0; i < details.count(); i++)
       
   139         {
       
   140             if (!details.at(i).imageUrl().isEmpty())
       
   141             {
       
   142                 contactManager()->saveContact(mContact);
       
   143                 break;
       
   144             }
       
   145         }
       
   146     }
       
   147        
   129     CntViewParameters viewParameters;
   148     CntViewParameters viewParameters;
   130     if (mIsGroupMember)
   149     if (mIsGroupMember)
   131     {
   150     {
   132         QVariant var;
   151         QVariant var;
   133         var.setValue(*mGroupContact);
   152         var.setValue(*mGroupContact);
   146     mViewManager = aMgr;
   165     mViewManager = aMgr;
   147     
   166     
   148     HbMainWindow* window = mView->mainWindow();
   167     HbMainWindow* window = mView->mainWindow();
   149     connect(window, SIGNAL(orientationChanged(Qt::Orientation)), this, SLOT(setOrientation(Qt::Orientation)));
   168     connect(window, SIGNAL(orientationChanged(Qt::Orientation)), this, SLOT(setOrientation(Qt::Orientation)));
   150     setOrientation(window->orientation());
   169     setOrientation(window->orientation());
       
   170         
       
   171     if (aArgs.value(ECustomParam).toInt() == imageEditorView)
       
   172     {    
       
   173         mIsPreviousImageEditorView = true;   
       
   174     }
   151     
   175     
   152     QContact contact = aArgs.value(ESelectedContact).value<QContact>();
   176     QContact contact = aArgs.value(ESelectedContact).value<QContact>();
   153     mContact = new QContact(contact);
   177     mContact = new QContact(contact);
   154 
   178 
   155     //my card
   179     //my card
   172     // add heading widget to the content
   196     // add heading widget to the content
   173     QGraphicsWidget *c = document()->findWidget(QString("content"));
   197     QGraphicsWidget *c = document()->findWidget(QString("content"));
   174     QGraphicsLinearLayout* l = static_cast<QGraphicsLinearLayout*>(c->layout());
   198     QGraphicsLinearLayout* l = static_cast<QGraphicsLinearLayout*>(c->layout());
   175 
   199 
   176     mHeadingItem = static_cast<CntContactCardHeadingItem*>(document()->findWidget(QString("cnt_contactcard_heading")));
   200     mHeadingItem = static_cast<CntContactCardHeadingItem*>(document()->findWidget(QString("cnt_contactcard_heading")));
   177     mHeadingItem->setDetails(mContact, mContact->localId() == contactManager()->selfContactId());
   201     mHeadingItem->setDetails(mContact);
   178     mHeadingItem->setSecondaryIcon(isFavoriteGroupContact());
   202     mHeadingItem->setSecondaryIcon(isFavoriteGroupContact());
   179     
   203     
   180     connect(mHeadingItem, SIGNAL(passLongPressed(const QPointF&)), this, SLOT(drawMenu(const QPointF&)));
   204     connect(mHeadingItem, SIGNAL(passLongPressed(const QPointF&)), this, SLOT(drawMenu(const QPointF&)));
   181 
   205 
   182     mImageLabel = static_cast<CntImageLabel*>(document()->findWidget("cnt_contactcard_image"));
   206     mImageLabel = static_cast<CntImageLabel*>(document()->findWidget("cnt_contactcard_image"));
   185     // avatar
   209     // avatar
   186     QList<QContactAvatar> details = mContact->details<QContactAvatar>();
   210     QList<QContactAvatar> details = mContact->details<QContactAvatar>();
   187     for (int i = 0;i < details.count();i++)
   211     for (int i = 0;i < details.count();i++)
   188     {
   212     {
   189         if (details.at(i).imageUrl().isValid())
   213         if (details.at(i).imageUrl().isValid())
   190             {
   214         {
   191             mAvatar = new QContactAvatar(details.at(i));
   215             mAvatar = new QContactAvatar(details.at(i));
   192             mThumbnailManager->getThumbnail(mAvatar->imageUrl().toString());
   216             mThumbnailManager->getThumbnail(mAvatar->imageUrl().toString());
   193             break;
   217             break;
   194             }
   218         }
   195     }
   219     }
   196     
   220     
   197     // data
   221     // data
   198     mDataContainer = new CntContactCardDataContainer(mContact);
   222     mDataContainer = new CntContactCardDataContainer(mContact);
   199 
   223 
   233         }
   257         }
   234 
   258 
   235         // separator
   259         // separator
   236         else if (index == mDataContainer->separatorIndex())
   260         else if (index == mDataContainer->separatorIndex())
   237         {      
   261         {      
   238             HbGroupBox* details = new HbGroupBox(mView);
   262             HbFrameItem* frameItem = new HbFrameItem(QString("qtg_fr_list_separator"), HbFrameDrawer::NinePieces);
   239             details->setHeading(mDataContainer->dataItem(index)->titleText());
   263             HbLabel* label = static_cast<HbLabel*>(document()->findWidget(QString("separator")));
   240             mContainerLayout->addItem(details);
   264             label->setPlainText(mDataContainer->dataItem(index)->titleText());
       
   265             label->setBackgroundItem(frameItem);
       
   266             mContainerLayout->addItem(label);
   241         }
   267         }
   242 
   268 
   243         // details
   269         // details
   244         else
   270         else
   245         {
   271         {
   246             CntContactCardDataItem* dataItem = mDataContainer->dataItem(index);
   272             CntContactCardDataItem* dataItem = mDataContainer->dataItem(index);
   247             if (!dataItem->icon().isNull())
   273             if (!dataItem->icon().isNull())
   248             { 
   274             { 
   249                 HbLabel* iconLabel = new HbLabel(mView);
   275                 HbLabel* iconLabel = new HbLabel(mView);
   250                 iconLabel->setIcon(dataItem->icon());
   276                 iconLabel->setIcon(dataItem->icon());
   251                 iconLabel->setPreferredSize(dataItem->icon().width(), dataItem->icon().height());
   277                 
       
   278                 int width = dataItem->icon().width();
       
   279                 int height = dataItem->icon().height();    
       
   280                 
       
   281                 //HbLabel setPreferredSize is not working properly,
       
   282                 //so added minimum , maximum size to fix the issue
       
   283                 iconLabel->setPreferredSize(QSizeF(width,height));
       
   284                 iconLabel->setMinimumSize(QSizeF(width, height));
       
   285                 iconLabel->setMaximumSize(QSizeF(width, height));
       
   286                 iconLabel->setSizePolicy(QSizePolicy(QSizePolicy::Fixed,
       
   287                                     QSizePolicy::Fixed));            
   252                 mContainerLayout->addItem(iconLabel);
   288                 mContainerLayout->addItem(iconLabel);
   253             } 
   289             } 
   254             else
   290             else
   255             {    
   291             {    
   256                 CntContactCardDetailItem* item = new CntContactCardDetailItem(index, mContainerWidget, false);
   292                 CntContactCardDetailItem* item = new CntContactCardDetailItem(index, mContainerWidget, false);
   274         int count = mContactsList.count();
   310         int count = mContactsList.count();
   275         if (count)
   311         if (count)
   276         {
   312         {
   277             for (int i = 0 ; i < count ; i++)
   313             for (int i = 0 ; i < count ; i++)
   278             {
   314             {
   279                 if (mContactsList.at(i) == mContact->localId()  )
   315                 if (mContactsList.at(i) == mContact->localId())
   280                 {
   316                 {
   281                 setAsFavorite = true;
   317                 setAsFavorite = true;
   282                 }
   318                 }
   283             }
   319             }
   284         }
   320         }
   285     }
   321     }
   286     
   322     
   287     if (setAsFavorite)
   323     if (setAsFavorite)
   288     {
   324     {
   289         mView->menu()->removeAction(qobject_cast<HbAction *>(document()->findObject("cnt:setasfavorite")));
   325         qobject_cast<HbAction *>(document()->findObject("cnt:setasfavorite"))->setVisible(false);
   290     }
   326     }
   291     else
   327     else
   292     {
   328     {
   293         mView->menu()->removeAction(qobject_cast<HbAction *>(document()->findObject("cnt:removefromfavorite")));
   329         qobject_cast<HbAction *>(document()->findObject("cnt:removefromfavorite"))->setVisible(false);
   294     }
   330     }
   295         
   331         
   296     // Menu items
   332     // Menu items
   297     connect(qobject_cast<HbAction *>(document()->findObject("cnt:sendbusinesscard")), SIGNAL(triggered()),
   333     connect(qobject_cast<HbAction *>(document()->findObject("cnt:sendbusinesscard")), SIGNAL(triggered()),
   298                 this, SLOT (sendBusinessCard()));
   334                 this, SLOT (sendBusinessCard()));
   388     relationship.setFirst(favoriteGroup.id());
   424     relationship.setFirst(favoriteGroup.id());
   389     relationship.setSecond(mContact->id());
   425     relationship.setSecond(mContact->id());
   390     // save relationship
   426     // save relationship
   391     contactManager()->saveRelationship(&relationship);
   427     contactManager()->saveRelationship(&relationship);
   392     
   428     
   393     mView->menu()->removeAction(qobject_cast<HbAction *>(document()->findObject("cnt:setasfavorite")));
   429     qobject_cast<HbAction *>(document()->findObject("cnt:setasfavorite"))->setVisible(false);
   394     mView->menu()->addAction(qobject_cast<HbAction *>(document()->findObject("cnt:removefromfavorite")));
   430     qobject_cast<HbAction *>(document()->findObject("cnt:removefromfavorite"))->setVisible(true);
   395     mHeadingItem->setSecondaryIcon(true);  
   431     mHeadingItem->setSecondaryIcon(true);  
   396 }
   432 }
   397 
   433 
   398 void CntContactCardViewPrivate::removeFromFavorite()
   434 void CntContactCardViewPrivate::removeFromFavorite()
   399     {
   435 {
   400     QContact favoriteGroup = contactManager()->contact(mFavoriteGroupId);
   436     QContact favoriteGroup = contactManager()->contact(mFavoriteGroupId);
   401     QContactRelationship relationship;
   437     QContactRelationship relationship;
   402     relationship.setRelationshipType(QContactRelationship::HasMember);
   438     relationship.setRelationshipType(QContactRelationship::HasMember);
   403     relationship.setFirst(favoriteGroup.id());
   439     relationship.setFirst(favoriteGroup.id());
   404     relationship.setSecond(mContact->id());
   440     relationship.setSecond(mContact->id());
   405     contactManager()->removeRelationship(relationship);
   441     contactManager()->removeRelationship(relationship);
   406    
   442 
   407     mView->menu()->removeAction(qobject_cast<HbAction *>(document()->findObject("cnt:removefromfavorite")));
   443     qobject_cast<HbAction *>(document()->findObject("cnt:removefromfavorite"))->setVisible(false);
   408     mView->menu()->addAction(qobject_cast<HbAction *>(document()->findObject("cnt:setasfavorite")));
   444     qobject_cast<HbAction *>(document()->findObject("cnt:setasfavorite"))->setVisible(true);
   409     mHeadingItem->setSecondaryIcon(false); 
   445     mHeadingItem->setSecondaryIcon(false); 
   410     }
   446 }
   411 
   447 
   412 /*!
   448 /*!
   413 Delete contact
   449 Delete contact
   414 */
   450 */
   415 void CntContactCardViewPrivate::deleteContact()
   451 void CntContactCardViewPrivate::deleteContact()
   416 {    
   452 {    
   417     QString name = contactManager()->synthesizedDisplayLabel(*mContact);
   453     QString name = contactManager()->synthesizedDisplayLabel(*mContact);
   418 
   454     
   419     HbMessageBox *note = new HbMessageBox(hbTrId("txt_phob_info_delete_1").arg(name), HbMessageBox::MessageTypeQuestion);
   455     HbMessageBox::question(HbParameterLengthLimiter(hbTrId("txt_phob_info_delete_1")).arg(name), this, SLOT(handleDeleteContact(HbAction*)),
   420     note->setPrimaryAction(new HbAction(hbTrId("txt_phob_button_delete"), note));
   456             hbTrId("txt_phob_button_delete"), hbTrId("txt_common_button_cancel"));
   421     note->setSecondaryAction(new HbAction(hbTrId("txt_common_button_cancel"), note));
   457 }
   422     HbAction *selected = note->exec();
   458 
   423     if (selected == note->primaryAction())
   459 /*!
       
   460 Handle action for deleting a contact
       
   461 */
       
   462 void CntContactCardViewPrivate::handleDeleteContact(HbAction *action)
       
   463 {
       
   464     HbMessageBox *note = static_cast<HbMessageBox*>(sender());
       
   465     
       
   466     if (note && action == note->actions().first())
   424     {
   467     {
   425         contactManager()->removeContact(mContact->localId());
   468         contactManager()->removeContact(mContact->localId());
   426         CntViewParameters viewParameters;
   469         CntViewParameters viewParameters;
   427         viewParameters.insert(EViewId, namesView);
   470         viewParameters.insert(EViewId, namesView);
   428         QVariant var;
       
   429         var.setValue(*mContact);
       
   430         viewParameters.insert(ESelectedContact, var);
       
   431         viewParameters.insert(ESelectedAction, "delete");
       
   432         mViewManager->changeView(viewParameters);
   471         mViewManager->changeView(viewParameters);
   433     }
   472     }
   434     delete note;   
       
   435 }
   473 }
   436 
   474 
   437 /*!
   475 /*!
   438 Launch history view 
   476 Launch history view 
   439 */
   477 */
   458 /*!
   496 /*!
   459 Send the business card / my card 
   497 Send the business card / my card 
   460 */
   498 */
   461 void CntContactCardViewPrivate::sendBusinessCard()
   499 void CntContactCardViewPrivate::sendBusinessCard()
   462 {
   500 {
       
   501     // Check if the contact has an image.
       
   502     QList<QContactAvatar> avatars = mContact->details<QContactAvatar>();
       
   503     bool imageExists( false );
       
   504     foreach(QContactAvatar a, mContact->details<QContactAvatar>())
       
   505     {
       
   506         if (!a.imageUrl().isEmpty())
       
   507         {
       
   508             imageExists = true;
       
   509             HbMessageBox *note = new HbMessageBox(
       
   510                     hbTrId("txt_phob_info_add_contact_card_image_to_business_c"),
       
   511                     HbMessageBox::MessageTypeQuestion);
       
   512             note->setIcon(*mVCardIcon);
       
   513             
       
   514             HbAction* ok = new HbAction(hbTrId("txt_common_button_ok"), note);
       
   515             HbAction* cancel = new HbAction(hbTrId("txt_common_button_cancel"), note);
       
   516             
       
   517             ok->setObjectName( "ok" );
       
   518             cancel->setObjectName( "cancel" );
       
   519              
       
   520             note->addAction( ok );
       
   521             note->addAction( cancel );
       
   522             
       
   523             note->setModal( true );
       
   524             note->setAttribute(Qt::WA_DeleteOnClose, true );
       
   525             note->open( this, SLOT(handleSendBusinessCard(HbAction*)));
       
   526             break;
       
   527         }
       
   528     }
       
   529     
       
   530     if ( !imageExists )
       
   531     {
       
   532         handleSendBusinessCard( NULL ); // no image
       
   533     }
       
   534 }
       
   535 
       
   536 /*!
       
   537 Set orientation of the view
       
   538 */
       
   539 void CntContactCardViewPrivate::setOrientation(Qt::Orientation orientation)
       
   540 {
       
   541     if (orientation == Qt::Vertical) 
       
   542     {
       
   543         // reading "portrait" section
       
   544         document()->load(CNT_CONTACTCARDVIEW_XML, "portrait");
       
   545     } 
       
   546     else 
       
   547     {
       
   548         // reading "landscape" section
       
   549         document()->load(CNT_CONTACTCARDVIEW_XML, "landscape");
       
   550     }
       
   551 }
       
   552 
       
   553 /*!
       
   554 Called after user clicked on the listview.
       
   555 */
       
   556 void CntContactCardViewPrivate::onItemActivated()
       
   557 {
       
   558     CntContactCardDetailItem *item = qobject_cast<CntContactCardDetailItem*>(sender());
       
   559     int index = item->index();
       
   560     launchAction(*mContact, mDataContainer->dataItem(index)->detail(), mDataContainer->dataItem(index)->action());
       
   561 }
       
   562 
       
   563 /*!
       
   564 Launch the call / message / email action
       
   565 */
       
   566 void CntContactCardViewPrivate::launchAction(QContact contact, QContactDetail detail, QString action)
       
   567 {
       
   568     // detail might be empty -> in that case engine uses the preferred detail for the selected action
       
   569     QList<QContactActionDescriptor> actionDescriptors = QContactAction::actionDescriptors(action, "symbian");
       
   570     if (actionDescriptors.isEmpty())
       
   571     {
       
   572         return;
       
   573     }
       
   574     mContactAction = QContactAction::action(actionDescriptors.first());
       
   575     connect(mContactAction, SIGNAL(stateChanged(QContactAction::State)),
       
   576                 this, SLOT(progress(QContactAction::State)));
       
   577     mContactAction->invokeAction(contact, detail);
       
   578 }
       
   579 
       
   580 void CntContactCardViewPrivate::progress(QContactAction::State status)
       
   581 {
       
   582     switch(status)
       
   583     {
       
   584     case QContactAction::FinishedState:
       
   585     case QContactAction::FinishedWithErrorState:
       
   586         mContactAction->deleteLater();
       
   587         mContactAction = 0;
       
   588         break;
       
   589     default:
       
   590         break;
       
   591     }
       
   592 }
       
   593 
       
   594 /*!
       
   595 Set selected detail as preferred for selected action
       
   596 */
       
   597 void CntContactCardViewPrivate::setPreferredAction(const QString &aAction, const QContactDetail &aDetail)
       
   598 {
       
   599     mContact->setPreferredDetail(aAction, aDetail);
       
   600     contactManager()->saveContact(mContact);
       
   601 }
       
   602 
       
   603 /*!
       
   604 Item specific menu
       
   605 */
       
   606 void CntContactCardViewPrivate::onLongPressed(const QPointF &aCoords)
       
   607 {
       
   608     CntContactCardDetailItem *item = qobject_cast<CntContactCardDetailItem*>(sender());
       
   609     int index = item->index();
       
   610     
       
   611     CntContactCardContextMenu *menu = new CntContactCardContextMenu( item );
       
   612     HbAction *communicationAction = 0;
       
   613     HbAction *videoCommunicationAction = 0;
       
   614     HbAction *preferredAction = 0;
       
   615     CntStringMapper stringMapper;
       
   616     
       
   617     QString action = mDataContainer->dataItem(index)->action();
       
   618     QContactDetail detail = mDataContainer->dataItem(index)->detail();
       
   619     
       
   620     if (action.compare("call", Qt::CaseInsensitive) == 0)
       
   621     {       
       
   622         QContactDetail detail = mDataContainer->dataItem(index)->detail();
       
   623         if (detail.definitionName() == QContactPhoneNumber::DefinitionName)
       
   624         {
       
   625             QContactPhoneNumber number = static_cast<QContactPhoneNumber>(detail);
       
   626             QString context = number.contexts().isEmpty() ? QString() : number.contexts().first();
       
   627             QString subtype = number.subTypes().isEmpty() ? number.definitionName() : number.subTypes().first();
       
   628 
       
   629             communicationAction = menu->addAction(stringMapper.getItemSpecificMenuLocString(subtype, context));
       
   630    
       
   631             // TODO : uncomment next line when videotelephony is released
       
   632             //videoCommunicationAction = menu->addAction(QString("VideoCall"));
       
   633         }        
       
   634     }
       
   635     else if (action.compare("message", Qt::CaseInsensitive) == 0)
       
   636     {
       
   637         communicationAction = menu->addAction(hbTrId("txt_phob_menu_send_message"));
       
   638     }
       
   639     else if (action.compare("email", Qt::CaseInsensitive) == 0)
       
   640     {
       
   641         if (!detail.contexts().isEmpty())
       
   642         {
       
   643             communicationAction = menu->addAction(stringMapper.getItemSpecificMenuLocString(detail.definitionName(), detail.contexts().first()));
       
   644         }
       
   645         else
       
   646         {
       
   647             communicationAction = menu->addAction(stringMapper.getItemSpecificMenuLocString(detail.definitionName(), QString()));
       
   648         }        
       
   649     }
       
   650     
       
   651     if (action.compare("call", Qt::CaseInsensitive) == 0)
       
   652     {
       
   653         preferredAction = menu->addAction(hbTrId("txt_phob_menu_set_as_default_number"));
       
   654     }
       
   655     else if (action.compare("message", Qt::CaseInsensitive) == 0)
       
   656     {
       
   657         preferredAction = menu->addAction(hbTrId("txt_phob_menu_set_as_default_number"));
       
   658     }
       
   659     else if (action.compare("email", Qt::CaseInsensitive) == 0)
       
   660     {
       
   661         preferredAction = menu->addAction(hbTrId("txt_phob_menu_set_as_default_email"));
       
   662     }
       
   663       
       
   664     if (mContact->isPreferredDetail(action, detail))
       
   665     {
       
   666         preferredAction->setEnabled(false);
       
   667     }
       
   668    
       
   669     if ( communicationAction )
       
   670     {
       
   671         communicationAction->setObjectName( "communicationAction" );
       
   672     }
       
   673     
       
   674     if ( preferredAction )
       
   675     {
       
   676         preferredAction->setObjectName( "preferredAction" );
       
   677         menu->insertSeparator(preferredAction);
       
   678     }
       
   679    
       
   680     menu->setPreferredPos( aCoords );
       
   681     menu->setAttribute( Qt::WA_DeleteOnClose, true );
       
   682     menu->open( this, SLOT(handleMenuAction(HbAction*)) );
       
   683 }
       
   684 
       
   685 void CntContactCardViewPrivate::handleMenuAction(HbAction* aAction)
       
   686 {
       
   687     CntContactCardContextMenu* menu = static_cast<CntContactCardContextMenu*>(sender());
       
   688     CntContactCardDetailItem *item = menu->item();
       
   689     int index = item->index();
       
   690        
       
   691     QString action = mDataContainer->dataItem(index)->action();
       
   692     QContactDetail detail = mDataContainer->dataItem(index)->detail();
       
   693         
       
   694     QString name = aAction->objectName();
       
   695     
       
   696     if ( name == "communicationAction" )
       
   697     {
       
   698         launchAction( *mContact, detail, action );
       
   699     }
       
   700     
       
   701     if ( name == "preferredAction" )
       
   702     {
       
   703         setPreferredAction(action, detail);
       
   704                     
       
   705         if (mPreferredItems.contains(action))
       
   706         {
       
   707             CntContactCardDetailItem *oldItem = mPreferredItems.value(action);
       
   708             mDataContainer->dataItem(oldItem->index())->setSecondaryIcon(HbIcon());
       
   709             oldItem->setDetails(mDataContainer->dataItem(oldItem->index()));
       
   710         }
       
   711             
       
   712         mDataContainer->dataItem(item->index())->setSecondaryIcon(HbIcon("qtg_mono_favourites"));
       
   713         item->setDetails(mDataContainer->dataItem(item->index()));
       
   714         
       
   715         mPreferredItems.insert(action, item);
       
   716     }
       
   717 }
       
   718 
       
   719 void CntContactCardViewPrivate::handleSendBusinessCard( HbAction* aAction )
       
   720 {
       
   721     QList<QContact> list;
       
   722     if ( aAction && aAction->objectName() == "cancel" )
       
   723     {
       
   724         QContact tmpContact( *mContact );
       
   725         foreach ( QContactAvatar a, tmpContact.details<QContactAvatar>() )
       
   726         {
       
   727             tmpContact.removeDetail( &a );
       
   728         }
       
   729         list.append( tmpContact );
       
   730     }
       
   731     else
       
   732     {
       
   733         list.append( *mContact );
       
   734     }
       
   735     
   463     QString tempDir = QDir::tempPath().append("/tempcntvcard");
   736     QString tempDir = QDir::tempPath().append("/tempcntvcard");
   464     
       
   465     QDir dir(tempDir);
   737     QDir dir(tempDir);
   466     
   738            
   467     // Temporary directory to store the vCard file
   739     // Temporary directory to store the vCard file
   468     if (!dir.exists()) 
   740     if (!dir.exists()) 
   469     {
   741     {
   470         // Create a temp directory
   742         // Create a temp directory
   471         QDir::temp().mkdir("tempcntvcard");
   743         QDir::temp().mkdir("tempcntvcard");
   477         foreach(QString s, l) 
   749         foreach(QString s, l) 
   478         {
   750         {
   479             if (dir.exists(s))
   751             if (dir.exists(s))
   480             {
   752             {
   481                 dir.remove(s);
   753                 dir.remove(s);
   482             }
   754             }    
   483         }
   755         }   
   484     }
   756     }
   485     
   757            
   486     QString vCardName = QString(mContact->displayLabel().append(".vcf"));
   758     QString vCardName = QString(mContact->displayLabel().append(".vcf"));
   487     QString vCardPath = dir.absolutePath().append(QDir::separator());
   759     QString vCardPath = dir.absolutePath().append(QDir::separator());
   488     vCardPath.append(vCardName);
   760     vCardPath.append(vCardName);
   489     vCardPath = QDir::toNativeSeparators(vCardPath);
   761     vCardPath = QDir::toNativeSeparators(vCardPath);
   490     
   762         
   491     QString service("com.nokia.services.hbserviceprovider.conversationview");
   763     QVersitContactExporter exporter;
   492     QString type("send(QVariant)");
   764     // The vCard version needs to be 2.1 due to backward compatiblity when sending 
   493     
   765     if (!exporter.exportContacts(list, QVersitDocument::VCard21Type))
   494     // Create the vCard and send it to messaging service
   766     {
   495     if (createVCard(vCardPath)) 
   767             
   496     {
   768         QList<QVersitDocument> docs = exporter.documents();
   497         ShareUi s;
   769         QFile f(vCardPath);
   498         QStringList l;
   770         if ( f.open(QIODevice::WriteOnly) ) 
   499         l << vCardPath;
   771         {
   500         s.send(l,false);
   772             // Start creating the vCard
   501     }
   773             QVersitWriter writer;
   502 }
   774             writer.setDevice(&f);
   503 
   775         
   504 /*!
   776             bool ret = writer.startWriting(docs);
   505 Set orientation of the view
   777             ret = writer.waitForFinished();
   506 */
   778         
   507 void CntContactCardViewPrivate::setOrientation(Qt::Orientation orientation)
   779             // Create the vCard and send it to messaging service
   508 {
   780             ShareUi s;
   509     if (orientation == Qt::Vertical) 
   781             QStringList l;
   510     {
   782             l << vCardPath;
   511         // reading "portrait" section
   783             s.send(l,false);
   512         document()->load(CNT_CONTACTCARDVIEW_XML, "portrait");
   784         }
   513     } 
   785     }
   514     else 
       
   515     {
       
   516         // reading "landscape" section
       
   517         document()->load(CNT_CONTACTCARDVIEW_XML, "landscape");
       
   518     }
       
   519 }
       
   520 
       
   521 /*!
       
   522 Called after user clicked on the listview.
       
   523 */
       
   524 void CntContactCardViewPrivate::onItemActivated()
       
   525 {
       
   526     CntContactCardDetailItem *item = qobject_cast<CntContactCardDetailItem*>(sender());
       
   527     int index = item->index();
       
   528     launchAction(*mContact, mDataContainer->dataItem(index)->detail(), mDataContainer->dataItem(index)->action());
       
   529 }
       
   530 
       
   531 /*!
       
   532 Launch the call / message / email action
       
   533 */
       
   534 void CntContactCardViewPrivate::launchAction(QContact contact, QContactDetail detail, QString action)
       
   535 {
       
   536     // detail might be empty -> in that case engine uses the preferred detail for the selected action
       
   537     QList<QContactActionDescriptor> callActionDescriptors = QContactAction::actionDescriptors(action, "symbian");
       
   538     mContactAction = QContactAction::action(callActionDescriptors.at(0));
       
   539     connect(mContactAction, SIGNAL(stateChanged(QContactAction::State)),
       
   540                 this, SLOT(progress(QContactAction::State)));
       
   541     mContactAction->invokeAction(contact, detail);
       
   542 }
       
   543 
       
   544 void CntContactCardViewPrivate::progress(QContactAction::State status)
       
   545 {
       
   546     switch(status)
       
   547     {
       
   548     case QContactAction::FinishedState:
       
   549     case QContactAction::FinishedWithErrorState:
       
   550         mContactAction->deleteLater();
       
   551         mContactAction = 0;
       
   552         break;
       
   553     default:
       
   554         break;
       
   555     }
       
   556 }
       
   557 
       
   558 /*!
       
   559 Set selected detail as preferred for selected action
       
   560 */
       
   561 void CntContactCardViewPrivate::setPreferredAction(const QString &aAction, const QContactDetail &aDetail)
       
   562 {
       
   563     mContact->setPreferredDetail(aAction, aDetail);
       
   564     contactManager()->saveContact(mContact);
       
   565     emit preferredUpdated();
       
   566 }
       
   567 
       
   568 /*!
       
   569 Item specific menu
       
   570 */
       
   571 void CntContactCardViewPrivate::onLongPressed(const QPointF &aCoords)
       
   572 {
       
   573     CntContactCardDetailItem *item = qobject_cast<CntContactCardDetailItem*>(sender());
       
   574     int index = item->index();
       
   575     
       
   576     HbMenu *menu = new HbMenu();
       
   577     HbAction *communicationAction = 0;
       
   578     HbAction *preferredAction = 0;
       
   579     CntStringMapper stringMapper;
       
   580     
       
   581     QString action = mDataContainer->dataItem(index)->action();
       
   582     QContactDetail detail = mDataContainer->dataItem(index)->detail();
       
   583     
       
   584     if (action.compare("call", Qt::CaseInsensitive) == 0)
       
   585     {       
       
   586         QContactDetail detail = mDataContainer->dataItem(index)->detail();
       
   587         if (!detail.contexts().isEmpty())
       
   588         {
       
   589             communicationAction = menu->addAction(stringMapper.getContactCardMenuLocString(detail.definitionName(), detail.contexts().first()));
       
   590         }
       
   591         else
       
   592         {
       
   593             communicationAction = menu->addAction(stringMapper.getContactCardMenuLocString(detail.definitionName(), QString()));
       
   594         }          
       
   595     }
       
   596     else if (action.compare("message", Qt::CaseInsensitive) == 0)
       
   597     {
       
   598         communicationAction = menu->addAction(hbTrId("txt_phob_menu_send_message"));
       
   599     }
       
   600     else if (action.compare("email", Qt::CaseInsensitive) == 0)
       
   601     {
       
   602         if (!detail.contexts().isEmpty())
       
   603         {
       
   604             communicationAction = menu->addAction(stringMapper.getContactCardMenuLocString(detail.definitionName(), detail.contexts().first()));
       
   605         }
       
   606         else
       
   607         {
       
   608             communicationAction = menu->addAction(stringMapper.getContactCardMenuLocString(detail.definitionName(), QString()));
       
   609         }        
       
   610     }
       
   611     
       
   612     if (action.compare("call", Qt::CaseInsensitive) == 0)
       
   613     {
       
   614         preferredAction = menu->addAction(hbTrId("txt_phob_menu_set_as_default_number"));
       
   615     }
       
   616     else if (action.compare("email", Qt::CaseInsensitive) == 0)
       
   617     {
       
   618         preferredAction = menu->addAction(hbTrId("txt_phob_menu_set_as_default_email"));
       
   619     }
       
   620       
       
   621     if (mContact->isPreferredDetail(action, detail))
       
   622     {
       
   623         preferredAction->setEnabled(false);
       
   624     }
       
   625     
       
   626     if (preferredAction)
       
   627     {
       
   628         menu->insertSeparator(preferredAction);
       
   629     }
       
   630 
       
   631     HbAction *selectedAction = menu->exec(aCoords);
       
   632 
       
   633     if (selectedAction)
       
   634     {
       
   635         if (selectedAction == communicationAction)
       
   636         {
       
   637             launchAction(*mContact, detail, action);
       
   638         }
       
   639         else if (selectedAction == preferredAction)
       
   640         {
       
   641             setPreferredAction(action, detail);
       
   642             //item->setUnderLine(true);
       
   643       
       
   644             if (mPreferredItems.contains(action))
       
   645             {
       
   646                 //mPreferredItems.value(action)->setUnderLine(false);
       
   647             }
       
   648             mPreferredItems.insert(action, item);     
       
   649         }
       
   650     }
       
   651     menu->deleteLater();
       
   652 }
   786 }
   653 
   787 
   654 /*!
   788 /*!
   655 Event filter for green key
   789 Event filter for green key
   656 */
   790 */
   701 */
   835 */
   702 void CntContactCardViewPrivate::doRemoveImage()
   836 void CntContactCardViewPrivate::doRemoveImage()
   703 {
   837 {
   704     if (mAvatar) 
   838     if (mAvatar) 
   705     {
   839     {
       
   840         CntImageUtility imageUtility;
       
   841         QString filePath=mAvatar->imageUrl().toString();
       
   842         
   706         bool success = mContact->removeDetail(mAvatar);
   843         bool success = mContact->removeDetail(mAvatar);
   707         if (success) 
   844         if (success) 
   708         { 
   845         { 
       
   846             if (!filePath.isEmpty())
       
   847             {
       
   848                 // Check if image removable.
       
   849                 CntImageUtility imageUtility;
       
   850                 if(imageUtility.isImageRemovable(filePath))
       
   851                 {
       
   852                     imageUtility.removeImage(filePath);
       
   853                 }
       
   854             }
   709             mHeadingItem->setIcon(HbIcon("qtg_large_avatar"));
   855             mHeadingItem->setIcon(HbIcon("qtg_large_avatar"));
   710             contactManager()->saveContact(mContact);
   856             contactManager()->saveContact(mContact);
   711         }
   857         }
   712     }
   858     }
   713 }
   859 }
   722     // To avoid re-drawing the menu and causing a crash due to 
   868     // To avoid re-drawing the menu and causing a crash due to 
   723     // multiple emitted signals, set state that we are handling the signal
   869     // multiple emitted signals, set state that we are handling the signal
   724     mIsHandlingMenu = true;
   870     mIsHandlingMenu = true;
   725     
   871     
   726     HbMenu *menu = new HbMenu();
   872     HbMenu *menu = new HbMenu();
   727     HbAction *changeImageAction = menu->addAction(hbTrId("txt_phob_menu_change_picture"));
   873     menu->addAction(hbTrId("txt_phob_menu_change_picture"), this, SLOT(doChangeImage()) );
   728     HbAction *removeAction = menu->addAction(hbTrId("txt_phob_menu_remove_image"));
   874     menu->addAction(hbTrId("txt_phob_menu_remove_image"), this, SLOT(doRemoveImage()) );
   729     
   875  
   730     HbAction *selectedAction = menu->exec(aCoords);
   876     menu->setAttribute( Qt::WA_DeleteOnClose );
   731 
   877     menu->setPreferredPos( aCoords );
   732     if (selectedAction) 
   878     menu->open();
   733     {
   879     }
   734         if (selectedAction == changeImageAction) 
       
   735         {
       
   736             doChangeImage();
       
   737         }
       
   738         else if (selectedAction == removeAction) 
       
   739         {
       
   740             doRemoveImage();
       
   741         }
       
   742     }
       
   743 
       
   744     mIsHandlingMenu = false;  
       
   745     menu->deleteLater();
       
   746 }
       
   747 
   880 
   748 /*!
   881 /*!
   749 Check if the favourite group created
   882 Check if the favourite group created
   750 */
   883 */
   751 bool CntContactCardViewPrivate::isFavoriteGroupCreated()
   884 bool CntContactCardViewPrivate::isFavoriteGroupCreated()
   805     }
   938     }
   806     return favoriteGroupContact;
   939     return favoriteGroupContact;
   807 }
   940 }
   808 
   941 
   809 /*!
   942 /*!
   810 Creates the v-card
       
   811 */
       
   812 bool CntContactCardViewPrivate::createVCard(QString& vCardPath)
       
   813 {
       
   814     QList<QContact> list;
       
   815     QContact tempContact(*mContact);
       
   816     bool createVCard( false );
       
   817     
       
   818     // Check if the contact has an image.
       
   819     QList<QContactAvatar> avatars = tempContact.details<QContactAvatar>();
       
   820     
       
   821     foreach(QContactAvatar a, avatars)
       
   822     {
       
   823         if (!a.imageUrl().isEmpty())
       
   824         {
       
   825             // If true and query the user if they want to add it to
       
   826             // the business card
       
   827         
       
   828             // TODO: Missing translation
       
   829             HbMessageBox *note = new HbMessageBox(hbTrId("txt_phob_info_add_contact_card_image_to_business_c"),
       
   830                     HbMessageBox::MessageTypeQuestion);
       
   831             note->setIcon(*mVCardIcon);
       
   832             
       
   833             note->setPrimaryAction(new HbAction(hbTrId("txt_common_button_ok"), note));
       
   834             note->setSecondaryAction(new HbAction(hbTrId("txt_common_button_cancel"), note));
       
   835             HbAction *selected = note->exec();
       
   836             if (selected == note->secondaryAction())
       
   837             {
       
   838                 // Remove the avatar detail from the temp contact
       
   839                 createVCard = tempContact.removeDetail(&a);
       
   840             } 
       
   841             else if (selected == note->primaryAction()) 
       
   842             {
       
   843                 createVCard = true;
       
   844             }
       
   845             delete note;
       
   846             break;
       
   847         }
       
   848         else
       
   849         {
       
   850             // Contact does not have an image
       
   851             createVCard = true;
       
   852         }
       
   853     } 
       
   854     
       
   855     // False = User clicked outside the popup, dismissing it
       
   856     //       = User prompted the removal of the avatar but it failed
       
   857     if (!createVCard)
       
   858     {
       
   859         return false;
       
   860     }
       
   861     
       
   862     list.append(tempContact);
       
   863     QVersitContactExporter exporter;
       
   864     // The vCard version needs to be 2.1 due to backward compatiblity when sending 
       
   865     if (!exporter.exportContacts(list, QVersitDocument::VCard21Type))
       
   866         return false;
       
   867     
       
   868     QList<QVersitDocument> docs = exporter.documents();
       
   869     
       
   870     QFile f(vCardPath);
       
   871     if (!f.open(QIODevice::WriteOnly)) 
       
   872     {
       
   873         return false;
       
   874     }
       
   875     
       
   876     // Start creating the vCard
       
   877     QVersitWriter writer;
       
   878     writer.setDevice(&f);
       
   879     
       
   880     bool ret = writer.startWriting(docs);
       
   881     ret = writer.waitForFinished();
       
   882     
       
   883     return ret;
       
   884 }
       
   885 
       
   886 /*!
       
   887 Return the pointer to the document loader
   943 Return the pointer to the document loader
   888 */
   944 */
   889 CntDocumentLoader* CntContactCardViewPrivate::document()
   945 CntDocumentLoader* CntContactCardViewPrivate::document()
   890 {
   946 {
   891     if (!mLoader) 
   947     if (!mLoader)