phonebookui/pbkcommonui/src/cntgroupactionsview.cpp
branchRCL_3
changeset 62 5b6f26637ad3
equal deleted inserted replaced
58:d4f567ce2e7c 62:5b6f26637ad3
       
     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 "cntgroupactionsview.h"
       
    19 #include "cntactionlauncher.h"
       
    20 #include "cntglobal.h"
       
    21 
       
    22 #include <cntactionpopup.h>
       
    23 
       
    24 #include <hblistview.h>
       
    25 #include <hbmenu.h>
       
    26 #include <hbaction.h>
       
    27 #include <hblistview.h>
       
    28 #include <hblistviewitem.h>
       
    29 #include <hbview.h>
       
    30 #include <hbaction.h>
       
    31 #include <hblabel.h>
       
    32 #include <hbicon.h>
       
    33 #include <hbgroupbox.h>
       
    34 #include <cntlistmodel.h>
       
    35 #include <hbframebackground.h>
       
    36 #include <hbnumbergrouping.h>
       
    37 #include <QKeyEvent> 
       
    38 #include <qtcontacts.h>
       
    39 #include <hbmainwindow.h>
       
    40 #include "cntactionlauncher.h"
       
    41 #include <QEvent>
       
    42 #include <QStandardItemModel>
       
    43 
       
    44 const char *CNT_GROUPACTIONSVIEW_XML = ":/xml/contacts_groupactions.docml";
       
    45 
       
    46 CntGroupActionsView::CntGroupActionsView() :
       
    47 mGroupContact(NULL),
       
    48 mModel(NULL),
       
    49 mViewManager(NULL),
       
    50 mListView(NULL),
       
    51 mPopupCount(0),
       
    52 mIsExecutingAction(false)
       
    53 {
       
    54     bool ok = false;
       
    55     mDocumentLoader.load(CNT_GROUPACTIONSVIEW_XML, &ok);
       
    56   
       
    57     if (ok)
       
    58     {
       
    59         mView = static_cast<HbView*>(mDocumentLoader.findWidget(QString("view")));
       
    60     }
       
    61     else
       
    62     {
       
    63         qFatal("Unable to read :/xml/contacts_groupactions.docml");
       
    64     }
       
    65     
       
    66     //back button
       
    67     mSoftkey = new HbAction(Hb::BackNaviAction, mView);
       
    68     connect(mSoftkey, SIGNAL(triggered()), this, SLOT(showPreviousView()));
       
    69     
       
    70     // menu actions
       
    71     mEditGrpDetailAction = static_cast<HbAction*>(mDocumentLoader.findObject("cnt:editgroupdetail"));
       
    72     connect(mEditGrpDetailAction, SIGNAL(triggered()), this, SLOT(editGroup()));
       
    73 }
       
    74 
       
    75 /*!
       
    76 Destructor
       
    77 */
       
    78 CntGroupActionsView::~CntGroupActionsView()
       
    79 {
       
    80     mView->deleteLater();
       
    81     
       
    82     delete mEditGrpDetailAction; 
       
    83     mEditGrpDetailAction = NULL;
       
    84     
       
    85     delete mGroupContact;
       
    86     mGroupContact = NULL;
       
    87     
       
    88     delete mModel;
       
    89     mModel = NULL;
       
    90 }
       
    91 
       
    92 
       
    93 void CntGroupActionsView::editGroup()
       
    94 {
       
    95     CntViewParameters viewParameters;
       
    96     viewParameters.insert(EViewId, groupEditorView);
       
    97     QVariant var;
       
    98     var.setValue(*mGroupContact);
       
    99     viewParameters.insert(ESelectedGroupContact, var);
       
   100     mViewManager->changeView(viewParameters);
       
   101 
       
   102 }
       
   103 
       
   104 
       
   105 /*!
       
   106 Activates a previous view
       
   107 */
       
   108 void CntGroupActionsView::showPreviousView()
       
   109 {
       
   110     CntViewParameters viewParameters;
       
   111     QVariant var;
       
   112     var.setValue(*mGroupContact);
       
   113     viewParameters.insert(ESelectedGroupContact, var);
       
   114     mViewManager->back(viewParameters);
       
   115 }
       
   116 
       
   117 void CntGroupActionsView::deactivate()
       
   118 {
       
   119     
       
   120 }
       
   121 
       
   122 /*
       
   123 Activates a default view and setup name label texts
       
   124 */
       
   125 void CntGroupActionsView::activate( const CntViewParameters aArgs )
       
   126 {
       
   127     if (mView->navigationAction() != mSoftkey)
       
   128         mView->setNavigationAction(mSoftkey);   
       
   129     
       
   130     mGroupContact = new QContact(aArgs.value(ESelectedGroupContact).value<QContact>());
       
   131     mViewManager = &mEngine->viewManager();
       
   132 
       
   133     QContactName contactName = mGroupContact->detail( QContactName::DefinitionName );
       
   134     QString groupName = contactName.value( QContactName::FieldCustomLabel );        
       
   135     if (groupName.isEmpty())
       
   136     {
       
   137         groupName = hbTrId("txt_phob_list_unnamed");
       
   138     }
       
   139     
       
   140     //group box
       
   141     HbGroupBox* groupBox = static_cast<HbGroupBox *>(mDocumentLoader.findWidget(QString("groupBox")));
       
   142     groupBox->setHeading(groupName);
       
   143     
       
   144     // create list & model
       
   145     mListView = static_cast<HbListView*> (mDocumentLoader.findWidget("listView"));
       
   146     mListView->setUniformItemSizes(true);
       
   147     
       
   148     HbFrameBackground frame;
       
   149     frame.setFrameGraphicsName("qtg_fr_list_parent_normal");
       
   150     frame.setFrameType(HbFrameDrawer::NinePieces);
       
   151     mListView->itemPrototypes().first()->setDefaultFrame(frame);
       
   152     
       
   153     mListView->listItemPrototype()->setGraphicsSize(HbListViewItem::LargeIcon);
       
   154     mListView->listItemPrototype()->setStretchingStyle(HbListViewItem::StretchLandscape);
       
   155     
       
   156     
       
   157     mModel = new QStandardItemModel();
       
   158     
       
   159     QContactPhoneNumber confCallNumber = mGroupContact->detail<QContactPhoneNumber>();
       
   160     if(!confCallNumber.number().isEmpty())
       
   161     {
       
   162         /*
       
   163          * Internationalization support, activate the following code 
       
   164          * when support available from Orbit
       
   165          */
       
   166         //populatelist(hbTrId("txt_phob_dblist_conference_call"), HbIcon("qtg_large_call_group"),HbNumberGrouping::formatPhoneNumber(confCallNumber.number()));
       
   167         populatelist(hbTrId("txt_phob_dblist_conference_call"), HbIcon("qtg_large_call_group"),confCallNumber.number(),"call");
       
   168     }
       
   169       
       
   170     populatelist(hbTrId("txt_phob_dblist_send_message"),HbIcon("qtg_large_message_group"),hbTrId("txt_phob_dblist_send_message_val_members"),"message");
       
   171     populatelist(hbTrId("txt_phob_dblist_email"),HbIcon("qtg_large_email_group"),hbTrId("txt_phob_dblist_send_message_val_members"),"email");
       
   172     
       
   173     mListView->setModel(mModel);
       
   174     mListView->setSelectionMode(HbAbstractItemView::NoSelection);
       
   175     
       
   176     connect(mListView, SIGNAL(activated(const QModelIndex&)),
       
   177                 this, SLOT(listItemSelected(const QModelIndex&)));
       
   178     
       
   179     HbMainWindow* window = mView->mainWindow();
       
   180     if (window)
       
   181     {
       
   182         window->installEventFilter(this);
       
   183     }
       
   184 }
       
   185 
       
   186 void CntGroupActionsView::populatelist(QString primaryText,HbIcon icon,QString secondaryText,QString action)
       
   187 {
       
   188     QList<QStandardItem*> items;
       
   189     QStandardItem *labelItem = new QStandardItem();
       
   190     
       
   191     QStringList textList;
       
   192    
       
   193     textList << primaryText << secondaryText;
       
   194     
       
   195     labelItem->setData(textList, Qt::DisplayRole);
       
   196     labelItem->setData(icon, Qt::DecorationRole);
       
   197     labelItem->setData(action, Qt::UserRole+1);
       
   198     
       
   199     items << labelItem ;
       
   200     mModel->appendRow(items);
       
   201 }
       
   202 
       
   203 void CntGroupActionsView::listItemSelected(const QModelIndex &index)
       
   204 {
       
   205     if (index.isValid()) {
       
   206         //reset flags
       
   207         mPopupCount=0;
       
   208         mEmailActionParams.clear();
       
   209         mMessageActionParams.clear();
       
   210         
       
   211         QString action = mModel->item(index.row())->data(Qt::UserRole+1).toString();
       
   212         QContactManager& mgr = mEngine->contactManager( SYMBIAN_BACKEND );
       
   213         
       
   214         //conference call
       
   215         if (action.compare("call", Qt::CaseInsensitive) == 0 ) {
       
   216             CntActionLauncher* other = new CntActionLauncher( mgr, action);
       
   217             connect(other, SIGNAL(actionExecuted(CntActionLauncher*)), this, SLOT(actionExecuted(CntActionLauncher*)));
       
   218             other->execute(*mGroupContact, QContactDetail());
       
   219         }
       
   220         //group email, message
       
   221         else {
       
   222             QContactRelationshipFilter relationshipFilter;
       
   223             relationshipFilter.setRelationshipType(QContactRelationship::HasMember);
       
   224             relationshipFilter.setRelatedContactRole(QContactRelationship::First);
       
   225             relationshipFilter.setRelatedContactId(mGroupContact->id());   
       
   226             QList<QContactLocalId> groupMembers = mgr.contactIds(relationshipFilter);
       
   227             
       
   228             for (int i = 0;i<groupMembers.count();i++) {
       
   229                 QContact contact = mgr.contact(groupMembers.at(i));
       
   230                 QContactDetail preferredDetail = contact.preferredDetail(action);
       
   231                 //use preferred detail if exits
       
   232                 if (!preferredDetail.isEmpty()) {
       
   233                     if(action.compare("message", Qt::CaseInsensitive) == 0) {
       
   234                         QContactPhoneNumber phoneNumber = contact.detail<QContactPhoneNumber>();
       
   235                         mMessageActionParams.insert(phoneNumber.number(),QVariant(contact.displayLabel()));
       
   236                     }
       
   237                     else {
       
   238                         QContactEmailAddress email = contact.detail<QContactEmailAddress>();
       
   239                         mEmailActionParams.append(email.emailAddress());
       
   240                     }
       
   241                 }
       
   242                 else {
       
   243                     CntActionPopup *actionPopup = new CntActionPopup(&contact);
       
   244                     if(actionPopup->showActionPopup(action)) {
       
   245                         //increment actionpopup counter
       
   246                         mPopupCount++;
       
   247                         
       
   248                         connect( actionPopup, SIGNAL(executeContactAction(QContact&, QContactDetail, QString)), this, 
       
   249                                                     SLOT(executeAction(QContact&, QContactDetail, QString)));
       
   250                         connect(actionPopup, SIGNAL(actionPopupCancelPressed()), this, SLOT(actionCancelled()));
       
   251                     }
       
   252                     else {
       
   253                         delete actionPopup;
       
   254                     }
       
   255                 }
       
   256             }
       
   257             //no popup dialog, execute action
       
   258             if (mPopupCount==0) {
       
   259                 QVariantMap map;
       
   260                 QVariant params;
       
   261                 if (action.compare("message", Qt::CaseInsensitive) == 0) {
       
   262                     params.setValue(mMessageActionParams);
       
   263                 }
       
   264                 else if (action.compare("email", Qt::CaseInsensitive) == 0) {
       
   265                     params.setValue(mEmailActionParams);
       
   266                 }
       
   267                 map.insert(action,params);
       
   268                 CntActionLauncher* other = new CntActionLauncher(mgr, action);
       
   269                 connect(other, SIGNAL(actionExecuted(CntActionLauncher*)), this, SLOT(actionExecuted(CntActionLauncher*)));
       
   270                 other->execute(*mGroupContact, QContactDetail(), map);
       
   271             }
       
   272         }
       
   273     }
       
   274 }
       
   275 
       
   276 void CntGroupActionsView::executeAction(QContact& contact, QContactDetail detail, QString action)
       
   277 {
       
   278     Q_UNUSED(contact);
       
   279     
       
   280     if (action.compare("message", Qt::CaseInsensitive) == 0) {
       
   281         QContactPhoneNumber phoneNumber = static_cast<QContactPhoneNumber>(detail);
       
   282         mMessageActionParams.insert(phoneNumber.number(),QVariant(contact.displayLabel()));
       
   283     }
       
   284     else if (action.compare("email", Qt::CaseInsensitive) == 0) {
       
   285         QContactEmailAddress email = static_cast<QContactEmailAddress>(detail);
       
   286         mEmailActionParams.append(email.emailAddress());
       
   287     }
       
   288     
       
   289     //actionpopup executed, decrement counter
       
   290     mPopupCount--;
       
   291     if (mPopupCount==0) {
       
   292         QVariantMap map;
       
   293         QVariant params;
       
   294         if (action.compare("message", Qt::CaseInsensitive) == 0) {
       
   295             params.setValue(mMessageActionParams);
       
   296         }
       
   297         else if (action.compare("email", Qt::CaseInsensitive) == 0) {
       
   298             params.setValue(mEmailActionParams);
       
   299         }
       
   300         map.insert(action,params);
       
   301         
       
   302         CntActionLauncher* other = new CntActionLauncher( mEngine->contactManager(SYMBIAN_BACKEND), action);
       
   303         connect(other, SIGNAL(actionExecuted(CntActionLauncher*)), this, SLOT(actionExecuted(CntActionLauncher*)));
       
   304         other->execute(*mGroupContact, QContactDetail(), map);
       
   305         }
       
   306 }
       
   307 
       
   308 bool CntGroupActionsView::eventFilter(QObject *obj, QEvent *event)
       
   309 {
       
   310     if (event->type() == QEvent::KeyPress && obj == mView->mainWindow())
       
   311     {
       
   312         QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
       
   313         if (keyEvent->key() == Qt::Key_Yes)
       
   314         {
       
   315             return sendKeyPressed();
       
   316         }
       
   317     }
       
   318     return false;
       
   319 }
       
   320 
       
   321 bool CntGroupActionsView::sendKeyPressed()
       
   322 {   
       
   323     QList<QContactPhoneNumber> numberDetails = mGroupContact->details<QContactPhoneNumber>();
       
   324     bool keyConsumed = false;
       
   325         
       
   326     // check if conference call number is present
       
   327     if (!numberDetails.isEmpty()) 
       
   328     { 
       
   329         executeConferenceCallAction( *mGroupContact, mGroupContact->details<QContactPhoneNumber>().first(), "call");
       
   330         
       
   331         keyConsumed = true;
       
   332     }
       
   333     
       
   334     return keyConsumed;
       
   335 }
       
   336 
       
   337 void CntGroupActionsView::executeConferenceCallAction(QContact& aContact, const QContactDetail& aDetail, const QString& aAction)
       
   338 {
       
   339     if (mIsExecutingAction)
       
   340     {
       
   341         return;
       
   342     }
       
   343     else
       
   344     {
       
   345         mIsExecutingAction = true;
       
   346     }
       
   347     
       
   348     CntActionLauncher* other = new CntActionLauncher( mEngine->contactManager(SYMBIAN_BACKEND), aAction);
       
   349     connect(other, SIGNAL(actionExecuted(CntActionLauncher*)), this, SLOT(actionExecuted(CntActionLauncher*)));
       
   350     other->execute(aContact, aDetail);  
       
   351 }
       
   352 
       
   353 void CntGroupActionsView::actionCancelled()
       
   354 {
       
   355     //actionpopup cancelled, decrement counter
       
   356     mPopupCount--;
       
   357 }
       
   358 
       
   359 
       
   360 void CntGroupActionsView::actionExecuted(CntActionLauncher* aAction)
       
   361 {
       
   362     //cleanup
       
   363     aAction->deleteLater();
       
   364     mIsExecutingAction = false;
       
   365 }
       
   366