phonebookui/cntcommonui/widgets/cntactionpopup_p.cpp
changeset 72 6abfb1094884
child 81 640d30f4fb64
equal deleted inserted replaced
67:59984e68247d 72:6abfb1094884
       
     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 #include "cntactionpopup_p.h"
       
    18 #include <qcontact.h>
       
    19 #include <qcontactdetail.h>
       
    20 #include <qcontactphonenumber.h>
       
    21 #include <hbaction.h>
       
    22 #include <QDebug>
       
    23 #include "cntstringmapper.h"
       
    24 #include <hblistview.h>
       
    25 #include <hblistviewitem.h>
       
    26 #include <QStandardItemModel>
       
    27 #include <hblabel.h>
       
    28 //#include <QtAlgorithms.h>
       
    29 
       
    30 
       
    31 
       
    32 CntActionPopupPrivate::CntActionPopupPrivate( QContact* aContact, QGraphicsItem *parent ) :
       
    33 HbSelectionDialog(parent),
       
    34 mContact( NULL ),
       
    35 mListModel(NULL),
       
    36 mListView(NULL),
       
    37 mCancelAction(NULL)
       
    38 {
       
    39     mContact = new QContact( *aContact );
       
    40     mListView = new HbListView(this);
       
    41     mListModel = new QStandardItemModel(this);
       
    42     setAttribute(Qt::WA_DeleteOnClose, true);
       
    43     mDataItemList.clear();
       
    44 }
       
    45 
       
    46 CntActionPopupPrivate::~CntActionPopupPrivate()
       
    47 {
       
    48     delete mContact;
       
    49     mContact = NULL;
       
    50 }
       
    51     
       
    52 void CntActionPopupPrivate::buildCallActionPopup()
       
    53 {
       
    54     // Create call action
       
    55     QList<QContactPhoneNumber> details = mContact->details<QContactPhoneNumber>();
       
    56     CntStringMapper map;
       
    57     for (int index = 0; index < details.count(); index++)
       
    58     {
       
    59        if(supportsDetail("call", details[index]))
       
    60        {
       
    61            QString context = details[index].contexts().isEmpty() ? QString() : details[index].contexts().first();
       
    62            QString subtype = details[index].subTypes().isEmpty() ? details[index].definitionName() : details[index].subTypes().first();
       
    63            
       
    64            int position = getPosition(subtype, context);
       
    65            
       
    66            QString titleText = map.getContactCardListLocString(subtype , context);
       
    67            QString valueText = details[index].number();
       
    68            
       
    69            QStandardItem *labelItem = new QStandardItem();
       
    70            QStringList textList;
       
    71            textList << titleText << valueText;
       
    72            labelItem->setData(textList, Qt::DisplayRole);
       
    73            labelItem->setData(HbIcon(map.getContactCardIconString(subtype, context)), Qt::DecorationRole);
       
    74            labelItem->setData(index, Qt::UserRole);
       
    75            labelItem->setData("call", Qt::UserRole+1);
       
    76            labelItem->setData(position, Qt::UserRole+2);
       
    77            mDataItemList.append(labelItem);
       
    78        }
       
    79     }
       
    80 }
       
    81 
       
    82 void CntActionPopupPrivate::buildEmailActionPopup()
       
    83 {
       
    84     // Create email action
       
    85     QList<QContactEmailAddress> details = mContact->details<QContactEmailAddress>();
       
    86     CntStringMapper map;
       
    87     for (int index = 0; index < details.count(); index++)
       
    88     {
       
    89        if(supportsDetail("email", details[index]))
       
    90        {
       
    91            QString context = details[index].contexts().isEmpty() ? QString() : details[index].contexts().first();
       
    92            int position = getPosition(details[index].definitionName(), context);
       
    93            
       
    94            QString titleText = map.getContactCardListLocString(details[index].definitionName() , context);
       
    95            QString valueText = details[index].emailAddress();
       
    96            
       
    97            QStandardItem *labelItem = new QStandardItem();
       
    98            QStringList textList;
       
    99            textList << titleText << valueText;
       
   100            labelItem->setData(textList, Qt::DisplayRole);
       
   101            labelItem->setData(HbIcon(map.getContactEditorIconString(details[index].definitionName(), context)), Qt::DecorationRole);
       
   102            labelItem->setData(index, Qt::UserRole);
       
   103            labelItem->setData("email", Qt::UserRole+1);
       
   104            labelItem->setData(position, Qt::UserRole+2);
       
   105            mDataItemList.append(labelItem);
       
   106        }
       
   107     }
       
   108 }
       
   109 
       
   110 void CntActionPopupPrivate::buildMessageActionPopup()
       
   111 {
       
   112     // Create message action
       
   113     QList<QContactPhoneNumber> details = mContact->details<QContactPhoneNumber>();
       
   114     CntStringMapper map;
       
   115     for (int index = 0; index < details.count(); index++)
       
   116     {
       
   117         if(supportsDetail("message", details[index]))
       
   118         {
       
   119             // Todo : Use loc string for this dialog
       
   120             QString context = details[index].contexts().isEmpty() ? QString() : details[index].contexts().first();
       
   121             QString subtype = details[index].subTypes().isEmpty() ? details[index].definitionName() : details[index].subTypes().first();
       
   122             
       
   123             int position = getPosition(subtype, context);
       
   124         
       
   125             QString icon;
       
   126             if (details[index].contexts().isEmpty())
       
   127             {
       
   128               icon = "qtg_large_message";
       
   129             }
       
   130             else if (details[index].contexts().first() == QContactDetail::ContextHome)
       
   131             {
       
   132               icon = "qtg_large_message_home";
       
   133             }
       
   134             else if (details[index].contexts().first() == QContactDetail::ContextWork)
       
   135             {
       
   136               icon = "qtg_large_message_work";
       
   137             }
       
   138             else
       
   139             {
       
   140               icon = "qtg_large_message";
       
   141             }
       
   142             
       
   143             QString titleText( hbTrId("txt_phob_dblist_send_message"));
       
   144             QString valueText = details[index].number();
       
   145             
       
   146             QStandardItem *labelItem = new QStandardItem();
       
   147             QStringList textList;
       
   148             textList << titleText << valueText;
       
   149             labelItem->setData(textList, Qt::DisplayRole);
       
   150             labelItem->setData(HbIcon(icon), Qt::DecorationRole);
       
   151             labelItem->setData(index, Qt::UserRole);
       
   152             labelItem->setData("message", Qt::UserRole+1);
       
   153             labelItem->setData(position, Qt::UserRole+2);
       
   154             mDataItemList.append(labelItem);
       
   155         }
       
   156     }
       
   157 }
       
   158 
       
   159 bool CntActionPopupPrivate::showActionPopup(QString action)
       
   160 {
       
   161     if (action.compare("call", Qt::CaseInsensitive) == 0 )
       
   162     {
       
   163         buildCallActionPopup();
       
   164     }
       
   165     else if( action.compare("message", Qt::CaseInsensitive) == 0 )
       
   166     {       
       
   167         buildMessageActionPopup();
       
   168     }
       
   169     else if (action.compare("email", Qt::CaseInsensitive) == 0 )
       
   170     {
       
   171         buildEmailActionPopup();
       
   172     }
       
   173     
       
   174     if(mDataItemList.count())
       
   175     {
       
   176         showPopup();
       
   177         return true;
       
   178     }
       
   179     return false; 
       
   180     
       
   181 }
       
   182 
       
   183 void CntActionPopupPrivate::showPopup()
       
   184 {
       
   185     for (int i= 0 ;i< mDataItemList.count() ;i++) 
       
   186         {
       
   187         mListModel->appendRow(mDataItemList.at(i));
       
   188         }
       
   189     mListModel->setSortRole(Qt::UserRole+2);
       
   190     mListModel->sort(0, Qt::AscendingOrder);
       
   191 
       
   192     clearActions(); 
       
   193     mListView->setModel(mListModel);
       
   194     mListView->setScrollingStyle(HbScrollArea::PanWithFollowOn);
       
   195     mListView->setFrictionEnabled(true);
       
   196     mListView->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Maximum );
       
   197     
       
   198     QString contactName = mContact->displayLabel();
       
   199     if (contactName.isEmpty())
       
   200     {
       
   201         contactName = hbTrId("txt_phob_list_unnamed");
       
   202     }
       
   203     HbLabel *label = new HbLabel();
       
   204     label->setPlainText(contactName);
       
   205     
       
   206     setHeadingWidget(label);
       
   207     setContentWidget(mListView);
       
   208     
       
   209     mCancelAction = new HbAction(hbTrId("txt_common_button_cancel"), this);
       
   210     addAction(mCancelAction);
       
   211     setTimeout(HbDialog::NoTimeout);
       
   212     setDismissPolicy(HbDialog::NoDismiss);
       
   213     setModal(true);
       
   214     
       
   215     connect(mListView, SIGNAL(activated(const QModelIndex&)), this, SLOT(listItemSelected(QModelIndex)));
       
   216     connect(mListView, SIGNAL(longPressed(HbAbstractViewItem*,const QPointF&)), 
       
   217             this, SLOT(onLongPressed(HbAbstractViewItem*,const QPointF&)) );
       
   218       
       
   219     
       
   220     //launch dialog asyncronously
       
   221     open(this, SLOT(handleUserResponse(HbAction*)));
       
   222 
       
   223 }
       
   224 
       
   225 void CntActionPopupPrivate::handleUserResponse(HbAction* action)
       
   226 {
       
   227     if (action == mCancelAction )
       
   228     {
       
   229     emit actionPopupCancelPressed();
       
   230     }
       
   231     mCancelAction = 0;
       
   232     
       
   233 }
       
   234 
       
   235 void CntActionPopupPrivate::onLongPressed(HbAbstractViewItem *item, const QPointF &coords)
       
   236 {
       
   237     Q_UNUSED(coords);
       
   238     QModelIndex index = item->modelIndex();
       
   239     listItemSelected(index);
       
   240 }
       
   241 
       
   242 void CntActionPopupPrivate::listItemSelected( QModelIndex index )
       
   243 {   
       
   244     if (index.isValid())
       
   245     {
       
   246         int selectedDetail = mListModel->item(index.row())->data(Qt::UserRole).toInt();
       
   247         QString actionName = mListModel->item(index.row())->data(Qt::UserRole+1).toString();   
       
   248         if (actionName.compare("call", Qt::CaseInsensitive) == 0 
       
   249             || actionName.compare("message", Qt::CaseInsensitive) == 0 )
       
   250         {       
       
   251             QList<QContactPhoneNumber> details = mContact->details<QContactPhoneNumber>();
       
   252             emit executeContactAction( *mContact, details[selectedDetail], actionName);
       
   253         }
       
   254         else if (actionName.compare("email", Qt::CaseInsensitive) == 0 )
       
   255         {
       
   256             QList<QContactEmailAddress> details = mContact->details<QContactEmailAddress>();
       
   257             emit executeContactAction( *mContact, details[selectedDetail], actionName);
       
   258         }
       
   259     }
       
   260     close();
       
   261 }
       
   262 
       
   263 /*!
       
   264 Returns position of specific item
       
   265 */
       
   266 int CntActionPopupPrivate::getPosition(const QString& aId, const QString& aContext)
       
   267 {
       
   268     int position = CntActionPopupPrivate::ENotSupported;
       
   269     if (aId == QContactPhoneNumber::SubTypeAssistant && aContext.isEmpty() )
       
   270     {
       
   271         position = CntActionPopupPrivate::ECallAssistant;
       
   272     }
       
   273     else if (aId == QContactPhoneNumber::SubTypeCar && aContext.isEmpty() )
       
   274     {
       
   275         position = CntActionPopupPrivate::ECallCar;
       
   276     }
       
   277     else if (aId == QContactPhoneNumber::SubTypeMobile && aContext.isEmpty() )
       
   278     {
       
   279         position = CntActionPopupPrivate::ECallMobile;
       
   280     }
       
   281     else if (aId == QContactPhoneNumber::SubTypeMobile && aContext == QContactDetail::ContextHome )
       
   282     {
       
   283         position = CntActionPopupPrivate::ECallMobileHome;
       
   284     }
       
   285     else if (aId == QContactPhoneNumber::SubTypeMobile && aContext == QContactDetail::ContextWork )
       
   286     {
       
   287         position = CntActionPopupPrivate::ECallMobileWork;
       
   288     }
       
   289     else if (aId == QContactPhoneNumber::SubTypeLandline && aContext.isEmpty() )
       
   290     {
       
   291         position = CntActionPopupPrivate::ECallPhone;
       
   292     }
       
   293     else if (aId == QContactPhoneNumber::SubTypeLandline && aContext == QContactDetail::ContextHome )
       
   294     {
       
   295         position = CntActionPopupPrivate::ECallPhoneHome;    
       
   296     }
       
   297     else if (aId == QContactPhoneNumber::SubTypeLandline && aContext == QContactDetail::ContextWork )
       
   298     {
       
   299         position = CntActionPopupPrivate::ECallPhoneWork;
       
   300     }
       
   301     else if (aId == QContactPhoneNumber::SubTypeFax && aContext.isEmpty() )
       
   302     {
       
   303         position = CntActionPopupPrivate::ECallFax;
       
   304     }
       
   305     else if (aId == QContactPhoneNumber::SubTypeFax && aContext == QContactDetail::ContextHome )
       
   306     {
       
   307         position = CntActionPopupPrivate::ECallFaxHome;
       
   308     }
       
   309     else if (aId == QContactPhoneNumber::SubTypeFax && aContext == QContactDetail::ContextWork )
       
   310     {
       
   311         position = CntActionPopupPrivate::ECallFaxWork;
       
   312     }
       
   313     else if (aId == QContactPhoneNumber::SubTypePager && aContext.isEmpty() )
       
   314     {
       
   315         position = CntActionPopupPrivate::ECallPager;
       
   316     }
       
   317     else if (aId == QContactEmailAddress::DefinitionName && aContext.isEmpty())
       
   318     {
       
   319             position = CntActionPopupPrivate::EEmail;
       
   320     }
       
   321     else if (aId == QContactEmailAddress::DefinitionName && aContext == QContactDetail::ContextHome)
       
   322     {
       
   323             position = CntActionPopupPrivate::EEmailHome;
       
   324     }
       
   325     else if (aId == QContactEmailAddress::DefinitionName && aContext == QContactDetail::ContextWork)
       
   326     {
       
   327             position = CntActionPopupPrivate::EEmailWork;
       
   328     }
       
   329     else 
       
   330     {
       
   331         position = CntActionPopupPrivate::ENotSupported;
       
   332     }
       
   333     
       
   334     return position;
       
   335 }
       
   336 
       
   337 /*!
       
   338 Returns true if contactDetails contains spesific action.
       
   339 */
       
   340 bool CntActionPopupPrivate::supportsDetail(const QString &actionName, const QContactDetail &contactDetail)
       
   341 {    
       
   342     QList<QContactActionDescriptor> actionDescriptors = QContactAction::actionDescriptors(actionName, "symbian");
       
   343     if (actionDescriptors.isEmpty())
       
   344     {
       
   345         return false;
       
   346     }
       
   347     
       
   348     QContactAction* contactAction = QContactAction::action(actionDescriptors.first());
       
   349     bool isSupportDetail = contactAction->isDetailSupported(contactDetail);
       
   350     
       
   351     delete contactAction;
       
   352     
       
   353     return isSupportDetail;
       
   354 }
       
   355 
       
   356 // End of File