messagingapp/msgui/conversationview/src/msgcontactcardwidget.cpp
branchRCL_3
changeset 57 ebe688cedc25
equal deleted inserted replaced
54:fa1df4b99609 57:ebe688cedc25
       
     1 /*
       
     2  * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3  * All rights reserved.
       
     4  * This component and the accompanying materials are made available
       
     5  * under the terms of "Eclipse Public License v1.0"
       
     6  * which accompanies this distribution, and is available
       
     7  * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8  *
       
     9  * Initial Contributors:
       
    10  * Nokia Corporation - initial contribution.
       
    11  *
       
    12  * Contributors:
       
    13  *
       
    14  * Description:
       
    15  *
       
    16  */
       
    17 
       
    18 #include "msgcontactcardwidget.h"
       
    19 
       
    20 // SYSTEM INCLUDES
       
    21 #include <HbStyle>
       
    22 #include <HbIconItem>
       
    23 #include <HbTextItem>
       
    24 #include <HbTapGesture>
       
    25 #include <HbInstantFeedback>
       
    26 #include <HbFrameItem>
       
    27 #include <qcontactphonenumber.h>
       
    28 #include <qcontactavatar.h>
       
    29 #include <xqaiwrequest.h>
       
    30 #include <xqappmgr.h>
       
    31 #include <xqservicerequest.h>
       
    32 #include <cntservicescontact.h>
       
    33 #include <QGraphicsSceneMouseEvent>
       
    34 #include <HbMenu>
       
    35 #include <thumbnailmanager_qt.h>
       
    36 #include <QTimer>
       
    37 
       
    38 #include <ccsdefs.h>
       
    39 
       
    40 // USER INCLUDES
       
    41 #include "conversationsenginedefines.h"
       
    42 #include "conversationsengine.h"
       
    43 #include "convergedmessage.h"
       
    44 #include "msgcontacthandler.h"
       
    45 
       
    46 // LOCAL CONSTANTS
       
    47 const QString DEFAULT_AVATAR_ICON("qtg_large_avatar");
       
    48 const QString BT_ICON("qtg_large_bluetooth");
       
    49 const QString BG_FRAME_NORMAL("qtg_fr_groupbox_normal");
       
    50 const QString BG_FRAME_PRESSED("qtg_fr_groupbox_pressed");
       
    51 
       
    52 // LOCALIZATION CONSTANTS
       
    53 #define LOC_RECEIVED_FILES hbTrId("txt_messaging_title_received_files")
       
    54 #define LOC_MENU_CONTACT_INFO hbTrId("txt_messaging_menu_contact_info")
       
    55 #define LOC_COMMON_MENU_CALL hbTrId("txt_common_menu_call_verb")
       
    56 #define LOC_SAVETO_CONTACTS hbTrId("txt_messaging_menu_save_to_contacts")
       
    57 
       
    58 // LOCAL FUNCTIONS
       
    59 
       
    60 //---------------------------------------------------------------
       
    61 // MsgContactCardWidget::MsgContactCardWidget
       
    62 // @see header
       
    63 //---------------------------------------------------------------
       
    64 MsgContactCardWidget::MsgContactCardWidget(QGraphicsItem *parent) :
       
    65     HbWidget(parent), mAvatarIconItem(NULL), mPresenceIconItem(NULL),
       
    66         mAddressTextItem(NULL), mThumbnailManager(NULL)
       
    67 {
       
    68     init();
       
    69 }
       
    70 
       
    71 //---------------------------------------------------------------
       
    72 // MsgContactCardWidget::~MsgContactCardWidget
       
    73 // @see header
       
    74 //---------------------------------------------------------------
       
    75 MsgContactCardWidget::~MsgContactCardWidget()
       
    76 {
       
    77 }
       
    78 
       
    79 //---------------------------------------------------------------
       
    80 // MsgContactCardWidget::init
       
    81 // @see header
       
    82 //---------------------------------------------------------------
       
    83 void MsgContactCardWidget::init()
       
    84 {
       
    85     this->setProperty("state", "normal");
       
    86     this->grabGesture(Qt::TapGesture);
       
    87 
       
    88     HbFrameItem *frameItem = new HbFrameItem(BG_FRAME_NORMAL, HbFrameDrawer::NinePieces, this);
       
    89     this->setBackgroundItem(frameItem);
       
    90 
       
    91     mAvatarIconItem = new HbIconItem(this);
       
    92     HbStyle::setItemName(mAvatarIconItem, "avatar");
       
    93 
       
    94     mPresenceIconItem = new HbIconItem(this);
       
    95     HbStyle::setItemName(mPresenceIconItem, "presence");
       
    96 
       
    97     mAddressTextItem = new HbTextItem(this);
       
    98     HbStyle::setItemName(mAddressTextItem, "addressText");
       
    99 
       
   100     mThumbnailManager = new ThumbnailManager(this);
       
   101     mThumbnailManager->setMode(ThumbnailManager::Default);
       
   102     mThumbnailManager->setQualityPreference(ThumbnailManager::OptimizeForQuality);
       
   103     mThumbnailManager->setThumbnailSize(ThumbnailManager::ThumbnailLarge);
       
   104 
       
   105     connect(mThumbnailManager, SIGNAL(thumbnailReady(QPixmap, void*, int, int)), this,
       
   106         SLOT(thumbnailReady(QPixmap, void*, int, int)));
       
   107 }
       
   108 
       
   109 //---------------------------------------------------------------
       
   110 // MsgContactCardWidget::setAvatar
       
   111 // @see header
       
   112 //---------------------------------------------------------------
       
   113 void MsgContactCardWidget::setAvatar(const HbIcon &avatar)
       
   114 {
       
   115     mAvatarIconItem->setIcon(avatar);
       
   116 }
       
   117 
       
   118 //---------------------------------------------------------------
       
   119 // MsgContactCardWidget::setPresenceIcon
       
   120 // @see header
       
   121 //---------------------------------------------------------------
       
   122 void MsgContactCardWidget::setPresenceIcon(const HbIcon &presenceIcon)
       
   123 {
       
   124     mPresenceIconItem->setIcon(presenceIcon);
       
   125 }
       
   126 
       
   127 //---------------------------------------------------------------
       
   128 // MsgContactCardWidget::setAddress
       
   129 // @see header
       
   130 //---------------------------------------------------------------
       
   131 void MsgContactCardWidget::setAddress(const QString &address)
       
   132 {
       
   133     mAddress = address;
       
   134     mAddressTextItem->setText(address);
       
   135 }
       
   136 
       
   137 //---------------------------------------------------------------
       
   138 // MsgContactCardWidget::address
       
   139 // @see header
       
   140 //---------------------------------------------------------------
       
   141 ConvergedMessageAddressList MsgContactCardWidget::address()
       
   142 {
       
   143     ConvergedMessageAddressList addresses;
       
   144     QStandardItemModel* msgModel = ConversationsEngine::instance()->getConversationsModel();
       
   145     const int rowCnt = msgModel->rowCount();
       
   146     QModelIndex index = msgModel->index(rowCnt-1, 0);
       
   147     QString addr = index.data(ConversationAddress).toString();
       
   148     ConvergedMessageAddress* address = new ConvergedMessageAddress(addr);
       
   149     // resolve contact
       
   150     QString displayname;
       
   151     int count;
       
   152     int retval = MsgContactHandler::resolveContactDisplayName(
       
   153             addr, displayname, count);
       
   154     if(retval != -1)
       
   155     {
       
   156         address->setAlias(displayname);
       
   157     }
       
   158     addresses.append(address);
       
   159     return addresses;
       
   160 }
       
   161 
       
   162 //---------------------------------------------------------------
       
   163 // MsgContactCardWidget::updateContents
       
   164 // @see header file
       
   165 //---------------------------------------------------------------
       
   166 void MsgContactCardWidget::updateContents()
       
   167 {
       
   168     // Get the Model Index
       
   169     QModelIndex index = ConversationsEngine::instance()->getConversationsModel()->index(0, 0);
       
   170 
       
   171     // Index is not valid, return.
       
   172     if (!index.isValid()) {
       
   173         return;
       
   174     }
       
   175 
       
   176     qint64 convId = ConversationsEngine::instance()->getCurrentConversationId();
       
   177 
       
   178     if (KBluetoothMsgsConversationId == convId) {
       
   179         setAddress(LOC_RECEIVED_FILES);
       
   180         setAvatar(HbIcon(BT_ICON));
       
   181     }
       
   182     else {
       
   183         //Set the Contact Name/Number
       
   184         qint64 convId;
       
   185         QString displayName;
       
   186         QString contactAddress;
       
   187         convId = ConversationsEngine::instance()->getCurrentConversationId();
       
   188         ConversationsEngine::instance()->getContactDetails(convId, displayName, contactAddress);
       
   189 
       
   190         mContactNumber = contactAddress;
       
   191         QString contactName;
       
   192         if (displayName.isEmpty()) {
       
   193             contactName.append(contactAddress);
       
   194         }
       
   195         else {
       
   196             contactName.append(displayName);
       
   197         }
       
   198 
       
   199         setAddress(contactName);
       
   200 
       
   201         // Set Avatar
       
   202         QList<QContact> contactList = MsgContactHandler::findContactList(mContactNumber);
       
   203 
       
   204         if (!contactList.isEmpty()) {
       
   205             QList<QContactAvatar> avatarDetails = contactList.at(0).details<QContactAvatar> ();
       
   206 
       
   207             if (!avatarDetails.isEmpty()) {
       
   208                 mThumbnailManager->getThumbnail(avatarDetails.at(0).imageUrl().toString());
       
   209             }
       
   210         }
       
   211 
       
   212         // Set default avatar till actual is set.
       
   213         setAvatar(HbIcon(DEFAULT_AVATAR_ICON));
       
   214     }
       
   215 }
       
   216 
       
   217 //---------------------------------------------------------------
       
   218 // MsgContactCardWidget::clearContent
       
   219 // @see header file
       
   220 //---------------------------------------------------------------
       
   221 void MsgContactCardWidget::clearContent()
       
   222 {
       
   223     setAddress("");
       
   224     setAvatar(HbIcon());
       
   225 }
       
   226 
       
   227 //---------------------------------------------------------------
       
   228 // MsgContactCardWidget::gestureEvent
       
   229 // @see header file
       
   230 //---------------------------------------------------------------
       
   231 void MsgContactCardWidget::gestureEvent(QGestureEvent *event)
       
   232 {
       
   233     HbTapGesture *tapGesture = qobject_cast<HbTapGesture*> (event->gesture(Qt::TapGesture));
       
   234     if (tapGesture) {
       
   235         switch (tapGesture->state()) {
       
   236         case Qt::GestureStarted:
       
   237         {
       
   238             // Trigger haptic feedback.
       
   239             HbInstantFeedback::play(HbFeedback::Basic);
       
   240             setPressed(true);
       
   241             break;
       
   242         }
       
   243         case Qt::GestureUpdated:
       
   244         {
       
   245             if (HbTapGesture::TapAndHold == tapGesture->tapStyleHint()) {
       
   246                 // Handle longtap.
       
   247                 setPressed(false);
       
   248                 handleLongTap(tapGesture->scenePosition());
       
   249             }
       
   250             break;
       
   251         }
       
   252         case Qt::GestureFinished:
       
   253         {
       
   254             HbInstantFeedback::play(HbFeedback::Basic);
       
   255             if (HbTapGesture::Tap == tapGesture->tapStyleHint()) {
       
   256                 // Handle short tap.
       
   257                 setPressed(false);
       
   258                 handleShortTap(tapGesture->scenePosition());
       
   259             }
       
   260             break;
       
   261         }
       
   262         case Qt::GestureCanceled:
       
   263         {
       
   264             HbInstantFeedback::play(HbFeedback::Basic);
       
   265             setPressed(false);
       
   266             break;
       
   267         }
       
   268         }
       
   269     }
       
   270     else {
       
   271         HbWidget::gestureEvent(event);
       
   272     }
       
   273 }
       
   274 
       
   275 //---------------------------------------------------------------
       
   276 // MsgContactCardWidget::setPressed
       
   277 // @see header file
       
   278 //---------------------------------------------------------------
       
   279 void MsgContactCardWidget::setPressed(bool pressed)
       
   280 {
       
   281     HbFrameItem *frameItem = static_cast<HbFrameItem *> (this->backgroundItem());
       
   282     if (pressed) {
       
   283         this->setProperty("state", "pressed");
       
   284         frameItem->frameDrawer().setFrameGraphicsName(BG_FRAME_PRESSED);
       
   285     }
       
   286     else {
       
   287         this->setProperty("state", "normal");
       
   288         frameItem->frameDrawer().setFrameGraphicsName(BG_FRAME_NORMAL);
       
   289     }
       
   290 }
       
   291 
       
   292 //---------------------------------------------------------------
       
   293 // MsgContactCardWidget::handleLongTap
       
   294 // @see header file
       
   295 //---------------------------------------------------------------
       
   296 void MsgContactCardWidget::handleLongTap(const QPointF &position)
       
   297 {
       
   298     if (KBluetoothMsgsConversationId != ConversationsEngine::instance()->getCurrentConversationId()) {
       
   299         HbMenu* contextMenu = new HbMenu();
       
   300         contextMenu->setDismissPolicy(HbPopup::TapAnywhere);
       
   301         contextMenu->setAttribute(Qt::WA_DeleteOnClose, true);
       
   302         contextMenu->setPreferredPos(position);   
       
   303 
       
   304         //If contact doesn't exist in phonebook then add another menu item "Save to Contacts"
       
   305         int contactId = resolveContactId(mContactNumber);
       
   306         if (contactId < 0) {
       
   307             contextMenu->addAction(LOC_SAVETO_CONTACTS, this, SLOT(addToContacts()));
       
   308         }
       
   309         else{
       
   310             contextMenu->addAction(LOC_MENU_CONTACT_INFO, this, SLOT(openContactInfo()));
       
   311         }
       
   312         contextMenu->addAction(LOC_COMMON_MENU_CALL, this, SLOT(call()));
       
   313 
       
   314         contextMenu->show();
       
   315     }
       
   316 }
       
   317 
       
   318 //---------------------------------------------------------------
       
   319 // MsgContactCardWidget::handleShortTap
       
   320 // @see header
       
   321 //---------------------------------------------------------------
       
   322 void MsgContactCardWidget::handleShortTap(const QPointF &position)
       
   323 {
       
   324     this->ungrabGesture(Qt::TapGesture);
       
   325     
       
   326     int contactId = resolveContactId(mContactNumber);
       
   327     if(contactId > 0)
       
   328     {
       
   329         //resolved contact open contact card.
       
   330         openContactInfo();
       
   331     }
       
   332     else
       
   333     {
       
   334         //unresolved contact show longpress options menu.
       
   335         handleLongTap(position);
       
   336     }
       
   337     
       
   338     //fire timer to regrab gesture after some delay.
       
   339     QTimer::singleShot(300,this,SLOT(regrabGesture()));    
       
   340 }
       
   341 
       
   342 //---------------------------------------------------------------
       
   343 // MsgContactCardWidget::openContactInfo
       
   344 // @see header
       
   345 //---------------------------------------------------------------
       
   346 void MsgContactCardWidget::openContactInfo()
       
   347 {
       
   348     QString operation;
       
   349     QList<QVariant> args;
       
   350     if (KBluetoothMsgsConversationId != ConversationsEngine::instance()->getCurrentConversationId()) {
       
   351         int contactId = resolveContactId(mContactNumber);
       
   352         if (contactId > 0) {
       
   353             //open existing contact card
       
   354             operation = QString("open(int)");
       
   355             args << contactId;
       
   356         }
       
   357         else {
       
   358             //populate data and open unknown contact template
       
   359             operation = QString("editCreateNew(QString,QString)");
       
   360             QString type = QContactPhoneNumber::DefinitionName;
       
   361 
       
   362             args << type;
       
   363             args << mAddress;
       
   364         }
       
   365         //service stuff.
       
   366         QString serviceName("com.nokia.services.phonebookservices");
       
   367 
       
   368         XQAiwRequest* request;
       
   369         XQApplicationManager appManager;
       
   370         request = appManager.create(serviceName, "Fetch", operation, true); // embedded
       
   371         if (request == NULL) {
       
   372             return;
       
   373         }
       
   374 
       
   375         // Result handlers
       
   376         connect(request, SIGNAL(requestOk(const QVariant&)), this, SLOT(handleOk(const QVariant&)));
       
   377         connect(request, SIGNAL(requestError(const QVariant&)), this,
       
   378             SLOT(handleError(const QVariant&)));
       
   379 
       
   380         //disbale subscritption for the CV events
       
   381         ConversationsEngine::instance()->disableRegisterationForCVEvents();
       
   382                 
       
   383         request->setArguments(args);
       
   384         request->send();
       
   385         delete request;
       
   386     }
       
   387 }
       
   388 
       
   389 //---------------------------------------------------------------
       
   390 // MsgContactCardWidget::resolveContactId
       
   391 // @see header
       
   392 //---------------------------------------------------------------
       
   393 int MsgContactCardWidget::resolveContactId(const QString& value)
       
   394 {
       
   395     QString displayLabel;
       
   396 
       
   397     int count;
       
   398     return MsgContactHandler::resolveContactDisplayName(value, displayLabel, count);
       
   399 }
       
   400 
       
   401 //---------------------------------------------------------------
       
   402 // MsgContactCardWidget::call
       
   403 // @see header
       
   404 //---------------------------------------------------------------
       
   405 void MsgContactCardWidget::call()
       
   406 {
       
   407     //Launch dialer service     
       
   408     QString serviceName("com.nokia.symbian.ICallDial");
       
   409     QString operation("dial(QString)");
       
   410 
       
   411     XQServiceRequest* serviceRequest = new XQServiceRequest(serviceName, operation, false);
       
   412 
       
   413     connect(serviceRequest, SIGNAL(requestCompleted(QVariant)), this,
       
   414         SLOT(onServiceRequestCompleted()));
       
   415 
       
   416     connect(serviceRequest, SIGNAL(requestError(int)), this, SLOT(onServiceRequestCompleted()));
       
   417 
       
   418     *serviceRequest << mContactNumber;
       
   419     serviceRequest->send();
       
   420 }
       
   421 
       
   422 //---------------------------------------------------------------
       
   423 // MsgContactCardWidget::addToContacts
       
   424 // @see header
       
   425 //---------------------------------------------------------------
       
   426 void MsgContactCardWidget::addToContacts()
       
   427 {
       
   428     openContactInfo();
       
   429 }
       
   430 
       
   431 //---------------------------------------------------------------
       
   432 // MsgContactCardWidget::handleOk
       
   433 // @see header
       
   434 //---------------------------------------------------------------
       
   435 void MsgContactCardWidget::handleOk(const QVariant& result)
       
   436 {
       
   437     Q_UNUSED(result)
       
   438 
       
   439     QList<QContact> matchingContacts = MsgContactHandler::findContactList(mContactNumber);
       
   440 
       
   441     if (!matchingContacts.isEmpty()) {
       
   442         setAddress(matchingContacts.at(0).displayLabel());
       
   443 
       
   444         QList<QContactAvatar> avatarDetails = matchingContacts.at(0).details<QContactAvatar> ();
       
   445 
       
   446         if (!avatarDetails.isEmpty()) {
       
   447             mThumbnailManager->getThumbnail(avatarDetails.at(0).imageUrl().toString());
       
   448         }
       
   449         else {
       
   450             //Set default avatar since avtar is deleted        
       
   451             setAvatar(HbIcon(DEFAULT_AVATAR_ICON));
       
   452         }        
       
   453 	}
       
   454     else  {
       
   455         //case for deletion from contact card widget
       
   456         //set the default number as address
       
   457         setAddress(mContactNumber);
       
   458         //Set default avatar since contact is deleted
       
   459         setAvatar(HbIcon(DEFAULT_AVATAR_ICON));
       
   460     }
       
   461 	
       
   462 	// Get the new conversation id.
       
   463     qint64 convId = ConversationsEngine::instance()->getConversationIdFromAddress(
       
   464                 mContactNumber);
       
   465     emit conversationIdChanged(convId);
       
   466             
       
   467     ConversationsEngine::instance(
       
   468                     )->emitOpenConversationViewIdUpdate(convId);
       
   469     
       
   470 }
       
   471 
       
   472 //---------------------------------------------------------------
       
   473 // MsgContactCardWidget::handleError
       
   474 // @see header
       
   475 //---------------------------------------------------------------
       
   476 void MsgContactCardWidget::handleError(int errorCode, const QString& errorMessage)
       
   477 {
       
   478     Q_UNUSED(errorMessage)
       
   479     Q_UNUSED(errorCode)    
       
   480     
       
   481     //unblock the cv events in case of contacts save error
       
   482     ConversationsEngine::instance(
       
   483                         )->emitOpenConversationViewIdUpdate(
       
   484                                 ConversationsEngine::instance(
       
   485                                         )->getCurrentConversationId(
       
   486                                                 ));
       
   487 }
       
   488 
       
   489 //---------------------------------------------------------------
       
   490 // MsgContactCardWidget::onServiceRequestCompleted
       
   491 // @see header
       
   492 //---------------------------------------------------------------
       
   493 void MsgContactCardWidget::onServiceRequestCompleted()
       
   494 {
       
   495     //service request is now complete. delete it.
       
   496     XQServiceRequest* request = qobject_cast<XQServiceRequest*> (sender());
       
   497     if (request) {
       
   498         delete request;
       
   499     }
       
   500 }
       
   501 
       
   502 //---------------------------------------------------------------
       
   503 // MsgContactCardWidget::thumbnailReady
       
   504 // @see header
       
   505 //---------------------------------------------------------------
       
   506 void MsgContactCardWidget::thumbnailReady(const QPixmap& pixmap, void *data, int id, int error)
       
   507 {
       
   508     Q_UNUSED(data)
       
   509     Q_UNUSED(id)
       
   510 
       
   511     if (!error) {
       
   512         QIcon icon(pixmap);
       
   513         setAvatar(HbIcon(icon));
       
   514     }
       
   515     else {
       
   516         setAvatar(HbIcon(DEFAULT_AVATAR_ICON));
       
   517     }
       
   518 }
       
   519 
       
   520 //---------------------------------------------------------------
       
   521 // MsgContactCardWidget::regrabGesture
       
   522 // @see header file
       
   523 //---------------------------------------------------------------
       
   524 void MsgContactCardWidget::regrabGesture()
       
   525 {
       
   526     this->grabGesture(Qt::TapGesture);
       
   527 }
       
   528 // EOF