phonebookui/cntcommonui/collections/cntgroupactionsview.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 
       
    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_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     connect( mListView, SIGNAL(longPressed(HbAbstractViewItem*,const QPointF&)), this, SLOT(onLongPressed(HbAbstractViewItem*,const QPointF&)) );
       
   180     
       
   181     HbMainWindow* window = mView->mainWindow();
       
   182     if (window)
       
   183     {
       
   184         window->installEventFilter(this);
       
   185     }
       
   186 }
       
   187 
       
   188 void CntGroupActionsView::populatelist(QString primaryText,HbIcon icon,QString secondaryText,QString action)
       
   189 {
       
   190     QList<QStandardItem*> items;
       
   191     QStandardItem *labelItem = new QStandardItem();
       
   192     
       
   193     QStringList textList;
       
   194    
       
   195     textList << primaryText << secondaryText;
       
   196     
       
   197     labelItem->setData(textList, Qt::DisplayRole);
       
   198     labelItem->setData(icon, Qt::DecorationRole);
       
   199     labelItem->setData(action, Qt::UserRole+1);
       
   200     
       
   201     items << labelItem ;
       
   202     mModel->appendRow(items);
       
   203 }
       
   204 
       
   205 void CntGroupActionsView::onLongPressed(HbAbstractViewItem *item, const QPointF &coords)
       
   206 {
       
   207     Q_UNUSED(coords);
       
   208     QModelIndex index = item->modelIndex();
       
   209     listItemSelected(index);
       
   210 }
       
   211 
       
   212 void CntGroupActionsView::listItemSelected(const QModelIndex &index)
       
   213 {
       
   214     if (index.isValid()) {
       
   215         //reset flags
       
   216         mPopupCount=0;
       
   217         mEmailActionParams.clear();
       
   218         mMessageActionParams.clear();
       
   219         
       
   220         QString action = mModel->item(index.row())->data(Qt::UserRole+1).toString();
       
   221         QContactManager& mgr = mEngine->contactManager( SYMBIAN_BACKEND );
       
   222         
       
   223         //conference call
       
   224         if (action.compare("call", Qt::CaseInsensitive) == 0 ) {
       
   225             CntActionLauncher* other = new CntActionLauncher( mgr, action);
       
   226             connect(other, SIGNAL(actionExecuted(CntActionLauncher*)), this, SLOT(actionExecuted(CntActionLauncher*)));
       
   227             other->execute(*mGroupContact, QContactDetail());
       
   228         }
       
   229         //group email, message
       
   230         else {
       
   231             QContactRelationshipFilter relationshipFilter;
       
   232             relationshipFilter.setRelationshipType(QContactRelationship::HasMember);
       
   233             relationshipFilter.setRelatedContactRole(QContactRelationship::First);
       
   234             relationshipFilter.setRelatedContactId(mGroupContact->id());   
       
   235             QList<QContactLocalId> groupMembers = mgr.contactIds(relationshipFilter);
       
   236             
       
   237             for (int i = 0;i<groupMembers.count();i++) {
       
   238                 QContact contact = mgr.contact(groupMembers.at(i));
       
   239                 QContactDetail preferredDetail = contact.preferredDetail(action);
       
   240                 //use preferred detail if exits
       
   241                 if (!preferredDetail.isEmpty()) {
       
   242                     if(action.compare("message", Qt::CaseInsensitive) == 0) {
       
   243                         QContactPhoneNumber phoneNumber = contact.detail<QContactPhoneNumber>();
       
   244                         mMessageActionParams.insert(phoneNumber.number(),QVariant(contact.displayLabel()));
       
   245                     }
       
   246                     else {
       
   247                         QContactEmailAddress email = contact.detail<QContactEmailAddress>();
       
   248                         mEmailActionParams.append(email.emailAddress());
       
   249                     }
       
   250                 }
       
   251                 else {
       
   252                     CntActionPopup *actionPopup = new CntActionPopup(&contact);
       
   253                     if(actionPopup->showActionPopup(action)) {
       
   254                         //increment actionpopup counter
       
   255                         mPopupCount++;
       
   256                         
       
   257                         connect( actionPopup, SIGNAL(executeContactAction(QContact&, QContactDetail, QString)), this, 
       
   258                                                     SLOT(executeAction(QContact&, QContactDetail, QString)));
       
   259                         connect(actionPopup, SIGNAL(actionPopupCancelPressed()), this, SLOT(actionCancelled()));
       
   260                     }
       
   261                     else {
       
   262                         delete actionPopup;
       
   263                     }
       
   264                 }
       
   265             }
       
   266             //no popup dialog, execute action
       
   267             if (mPopupCount==0) {
       
   268                 QVariantMap map;
       
   269                 QVariant params;
       
   270                 if (action.compare("message", Qt::CaseInsensitive) == 0) {
       
   271                     params.setValue(mMessageActionParams);
       
   272                 }
       
   273                 else if (action.compare("email", Qt::CaseInsensitive) == 0) {
       
   274                     params.setValue(mEmailActionParams);
       
   275                 }
       
   276                 map.insert(action,params);
       
   277                 CntActionLauncher* other = new CntActionLauncher(mgr, action);
       
   278                 connect(other, SIGNAL(actionExecuted(CntActionLauncher*)), this, SLOT(actionExecuted(CntActionLauncher*)));
       
   279                 other->execute(*mGroupContact, QContactDetail(), map);
       
   280             }
       
   281         }
       
   282     }
       
   283 }
       
   284 
       
   285 void CntGroupActionsView::executeAction(QContact& contact, QContactDetail detail, QString action)
       
   286 {
       
   287     Q_UNUSED(contact);
       
   288     
       
   289     if (action.compare("message", Qt::CaseInsensitive) == 0)
       
   290     {
       
   291         QContactPhoneNumber phoneNumber = static_cast<QContactPhoneNumber>(detail);
       
   292         mMessageActionParams.insert(phoneNumber.number(),QVariant(contact.displayLabel()));
       
   293     }
       
   294     else if (action.compare("email", Qt::CaseInsensitive) == 0)
       
   295     {
       
   296         QContactEmailAddress email = static_cast<QContactEmailAddress>(detail);
       
   297         mEmailActionParams.append(email.emailAddress());
       
   298     }
       
   299     
       
   300     if (contact.preferredDetail(action).isEmpty())
       
   301     {
       
   302         contact.setPreferredDetail(action, detail);
       
   303        //return value will be ignored because we cannot do anything if it fails.
       
   304        mEngine->contactManager(SYMBIAN_BACKEND).saveContact(&contact);
       
   305     }
       
   306     
       
   307     //actionpopup executed, decrement counter
       
   308     mPopupCount--;
       
   309     if (mPopupCount==0)
       
   310     {
       
   311         QVariantMap map;
       
   312         QVariant params;
       
   313         if (action.compare("message", Qt::CaseInsensitive) == 0)
       
   314         {
       
   315             params.setValue(mMessageActionParams);
       
   316         }
       
   317         else if (action.compare("email", Qt::CaseInsensitive) == 0)
       
   318         {
       
   319             params.setValue(mEmailActionParams);
       
   320         }
       
   321         map.insert(action,params);
       
   322 
       
   323         CntActionLauncher* other = new CntActionLauncher( mEngine->contactManager(SYMBIAN_BACKEND), action);
       
   324         connect(other, SIGNAL(actionExecuted(CntActionLauncher*)), this, SLOT(actionExecuted(CntActionLauncher*)));
       
   325         other->execute(*mGroupContact, QContactDetail(), map);
       
   326     }
       
   327 }
       
   328 
       
   329 bool CntGroupActionsView::eventFilter(QObject *obj, QEvent *event)
       
   330 {
       
   331     if (event->type() == QEvent::KeyPress && obj == mView->mainWindow())
       
   332     {
       
   333         QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
       
   334         if (keyEvent->key() == Qt::Key_Yes)
       
   335         {
       
   336             return sendKeyPressed();
       
   337         }
       
   338     }
       
   339     return false;
       
   340 }
       
   341 
       
   342 bool CntGroupActionsView::sendKeyPressed()
       
   343 {   
       
   344     QList<QContactPhoneNumber> numberDetails = mGroupContact->details<QContactPhoneNumber>();
       
   345     bool keyConsumed = false;
       
   346         
       
   347     // check if conference call number is present
       
   348     if (!numberDetails.isEmpty()) 
       
   349     { 
       
   350         executeConferenceCallAction( *mGroupContact, mGroupContact->details<QContactPhoneNumber>().first(), "call");
       
   351         
       
   352         keyConsumed = true;
       
   353     }
       
   354     
       
   355     return keyConsumed;
       
   356 }
       
   357 
       
   358 void CntGroupActionsView::executeConferenceCallAction(QContact& aContact, const QContactDetail& aDetail, const QString& aAction)
       
   359 {
       
   360     if (mIsExecutingAction)
       
   361     {
       
   362         return;
       
   363     }
       
   364     else
       
   365     {
       
   366         mIsExecutingAction = true;
       
   367     }
       
   368     
       
   369     CntActionLauncher* other = new CntActionLauncher( mEngine->contactManager(SYMBIAN_BACKEND), aAction);
       
   370     connect(other, SIGNAL(actionExecuted(CntActionLauncher*)), this, SLOT(actionExecuted(CntActionLauncher*)));
       
   371     other->execute(aContact, aDetail);  
       
   372 }
       
   373 
       
   374 void CntGroupActionsView::actionCancelled()
       
   375 {
       
   376     //actionpopup cancelled, decrement counter
       
   377     mPopupCount--;
       
   378     
       
   379     if (mPopupCount==0)
       
   380     {
       
   381         QVariantMap map;
       
   382         QVariant params;
       
   383         QString action;
       
   384         if (mMessageActionParams.count())
       
   385         {
       
   386             params.setValue(mMessageActionParams);
       
   387             action = "message";
       
   388         }
       
   389         else if (mEmailActionParams.count())
       
   390         {
       
   391             params.setValue(mEmailActionParams);
       
   392             action = "email";
       
   393         }
       
   394         else
       
   395         {
       
   396             return;
       
   397         }
       
   398         map.insert(action,params);
       
   399 
       
   400         CntActionLauncher* other = new CntActionLauncher( mEngine->contactManager(SYMBIAN_BACKEND), action);
       
   401         connect(other, SIGNAL(actionExecuted(CntActionLauncher*)), this, SLOT(actionExecuted(CntActionLauncher*)));
       
   402         other->execute(*mGroupContact, QContactDetail(), map);
       
   403     }
       
   404 }
       
   405 
       
   406 
       
   407 void CntGroupActionsView::actionExecuted(CntActionLauncher* aAction)
       
   408 {
       
   409     //cleanup
       
   410     aAction->deleteLater();
       
   411     mIsExecutingAction = false;
       
   412 }
       
   413