phonebookui/pbkcommonui/src/cntgroupactionsview.cpp
changeset 59 a642906a277a
parent 40 b46a585f6909
child 65 ae724a111993
--- a/phonebookui/pbkcommonui/src/cntgroupactionsview.cpp	Tue Jul 06 14:05:47 2010 +0300
+++ b/phonebookui/pbkcommonui/src/cntgroupactionsview.cpp	Wed Aug 18 09:39:00 2010 +0300
@@ -16,6 +16,10 @@
 */
 
 #include "cntgroupactionsview.h"
+#include "cntactionlauncher.h"
+#include "cntglobal.h"
+
+#include <cntactionpopup.h>
 
 #include <hblistview.h>
 #include <hbmenu.h>
@@ -30,7 +34,12 @@
 #include <cntlistmodel.h>
 #include <hbframebackground.h>
 #include <hbnumbergrouping.h>
+#include <QKeyEvent> 
+#include <qtcontacts.h>
+#include <hbmainwindow.h>
+#include "cntactionlauncher.h"
 
+#include <QEvent>
 #include <QStandardItemModel>
 
 const char *CNT_GROUPACTIONSVIEW_XML = ":/xml/contacts_groupactions.docml";
@@ -39,7 +48,9 @@
 mGroupContact(NULL),
 mModel(NULL),
 mViewManager(NULL),
-mListView(NULL)
+mListView(NULL),
+mPopupCount(0),
+mIsExecutingAction(false)
 {
     bool ok = false;
     mDocumentLoader.load(CNT_GROUPACTIONSVIEW_XML, &ok);
@@ -120,9 +131,12 @@
     mGroupContact = new QContact(aArgs.value(ESelectedGroupContact).value<QContact>());
     mViewManager = aMgr;
 
-    QContactName groupContactName = mGroupContact->detail( QContactName::DefinitionName );
-    QString groupName(groupContactName.value( QContactName::FieldCustomLabel ));
-    
+    QContactName contactName = mGroupContact->detail( QContactName::DefinitionName );
+    QString groupName = contactName.value( QContactName::FieldCustomLabel );        
+    if (groupName.isEmpty())
+    {
+        groupName = hbTrId("txt_phob_list_unnamed");
+    }
     
     //group box
     HbGroupBox* groupBox = static_cast<HbGroupBox *>(mDocumentLoader.findWidget(QString("groupBox")));
@@ -145,24 +159,32 @@
     
     QContactPhoneNumber confCallNumber = mGroupContact->detail<QContactPhoneNumber>();
     if(!confCallNumber.number().isEmpty())
-        {
+    {
         /*
          * Internationalization support, activate the following code 
          * when support available from Orbit
          */
         //populatelist(hbTrId("txt_phob_dblist_conference_call"), HbIcon("qtg_large_call_group"),HbNumberGrouping::formatPhoneNumber(confCallNumber.number()));
-        populatelist(hbTrId("txt_phob_dblist_conference_call"), HbIcon("qtg_large_call_group"),confCallNumber.number());
-        }
+        populatelist(hbTrId("txt_phob_dblist_conference_call"), HbIcon("qtg_large_call_group"),confCallNumber.number(),"call");
+    }
       
-    populatelist(hbTrId("txt_phob_dblist_send_message"),HbIcon("qtg_large_message_group"),hbTrId("txt_phob_dblist_send_message_val_members"));
-    populatelist(hbTrId("txt_phob_dblist_email"),HbIcon("qtg_large_email_group"),hbTrId("txt_phob_dblist_send_message_val_members"));
+    populatelist(hbTrId("txt_phob_dblist_send_message"),HbIcon("qtg_large_message_group"),hbTrId("txt_phob_dblist_send_message_val_members"),"message");
+    populatelist(hbTrId("txt_phob_dblist_email"),HbIcon("qtg_large_email_group"),hbTrId("txt_phob_dblist_send_message_val_members"),"email");
     
     mListView->setModel(mModel);
     mListView->setSelectionMode(HbAbstractItemView::NoSelection);
     
+    connect(mListView, SIGNAL(activated(const QModelIndex&)),
+                this, SLOT(listItemSelected(const QModelIndex&)));
+    
+    HbMainWindow* window = mView->mainWindow();
+    if (window)
+    {
+        window->installEventFilter(this);
+    }
 }
 
-void CntGroupActionsView:: populatelist(QString primaryText,HbIcon icon,QString secondaryText)
+void CntGroupActionsView::populatelist(QString primaryText,HbIcon icon,QString secondaryText,QString action)
 {
     QList<QStandardItem*> items;
     QStandardItem *labelItem = new QStandardItem();
@@ -173,7 +195,172 @@
     
     labelItem->setData(textList, Qt::DisplayRole);
     labelItem->setData(icon, Qt::DecorationRole);
+    labelItem->setData(action, Qt::UserRole+1);
     
     items << labelItem ;
     mModel->appendRow(items);
 }
+
+void CntGroupActionsView::listItemSelected(const QModelIndex &index)
+{
+    if (index.isValid()) {
+        //reset flags
+        mPopupCount=0;
+        mEmailActionParams.clear();
+        mMessageActionParams.clear();
+        
+        QString action = mModel->item(index.row())->data(Qt::UserRole+1).toString();
+        
+        //conference call
+        if (action.compare("call", Qt::CaseInsensitive) == 0 ) {
+            CntActionLauncher* other = new CntActionLauncher(*mViewManager->contactManager(SYMBIAN_BACKEND), action);
+            connect(other, SIGNAL(actionExecuted(CntActionLauncher*)), this, SLOT(actionExecuted(CntActionLauncher*)));
+            other->execute(*mGroupContact, QContactDetail());
+        }
+        //group email, message
+        else {
+            QContactRelationshipFilter relationshipFilter;
+            relationshipFilter.setRelationshipType(QContactRelationship::HasMember);
+            relationshipFilter.setRelatedContactRole(QContactRelationship::First);
+            relationshipFilter.setRelatedContactId(mGroupContact->id());   
+            QList<QContactLocalId> groupMembers = mViewManager->contactManager(SYMBIAN_BACKEND)->contactIds(relationshipFilter);
+            
+            for (int i = 0;i<groupMembers.count();i++) {
+                QContact contact = mViewManager->contactManager(SYMBIAN_BACKEND)->contact(groupMembers.at(i));
+                QContactDetail preferredDetail = contact.preferredDetail(action);
+                //use preferred detail if exits
+                if (!preferredDetail.isEmpty()) {
+                    if(action.compare("message", Qt::CaseInsensitive) == 0) {
+                        QContactPhoneNumber phoneNumber = contact.detail<QContactPhoneNumber>();
+                        mMessageActionParams.insert(phoneNumber.number(),QVariant(contact.displayLabel()));
+                    }
+                    else {
+                        QContactEmailAddress email = contact.detail<QContactEmailAddress>();
+                        mEmailActionParams.append(email.emailAddress());
+                    }
+                }
+                else {
+                    CntActionPopup *actionPopup = new CntActionPopup(&contact);
+                    if(actionPopup->showActionPopup(action)) {
+                        //increment actionpopup counter
+                        mPopupCount++;
+                        
+                        connect( actionPopup, SIGNAL(executeContactAction(QContact&, QContactDetail, QString)), this, 
+                                                    SLOT(executeAction(QContact&, QContactDetail, QString)));
+                        connect(actionPopup, SIGNAL(actionPopupCancelPressed()), this, SLOT(actionCancelled()));
+                    }
+                    else {
+                        delete actionPopup;
+                    }
+                }
+            }
+            //no popup dialog, execute action
+            if (mPopupCount==0) {
+                QVariantMap map;
+                QVariant params;
+                if (action.compare("message", Qt::CaseInsensitive) == 0) {
+                    params.setValue(mMessageActionParams);
+                }
+                else if (action.compare("email", Qt::CaseInsensitive) == 0) {
+                    params.setValue(mEmailActionParams);
+                }
+                map.insert(action,params);
+                CntActionLauncher* other = new CntActionLauncher(*mViewManager->contactManager(SYMBIAN_BACKEND), action);
+                connect(other, SIGNAL(actionExecuted(CntActionLauncher*)), this, SLOT(actionExecuted(CntActionLauncher*)));
+                other->execute(*mGroupContact, QContactDetail(), map);
+            }
+        }
+    }
+}
+
+void CntGroupActionsView::executeAction(QContact& contact, QContactDetail detail, QString action)
+{
+    Q_UNUSED(contact);
+    
+    if (action.compare("message", Qt::CaseInsensitive) == 0) {
+        QContactPhoneNumber phoneNumber = static_cast<QContactPhoneNumber>(detail);
+        mMessageActionParams.insert(phoneNumber.number(),QVariant(contact.displayLabel()));
+    }
+    else if (action.compare("email", Qt::CaseInsensitive) == 0) {
+        QContactEmailAddress email = static_cast<QContactEmailAddress>(detail);
+        mEmailActionParams.append(email.emailAddress());
+    }
+    
+    //actionpopup executed, decrement counter
+    mPopupCount--;
+    if (mPopupCount==0) {
+        QVariantMap map;
+        QVariant params;
+        if (action.compare("message", Qt::CaseInsensitive) == 0) {
+            params.setValue(mMessageActionParams);
+        }
+        else if (action.compare("email", Qt::CaseInsensitive) == 0) {
+            params.setValue(mEmailActionParams);
+        }
+        map.insert(action,params);
+        
+        CntActionLauncher* other = new CntActionLauncher(*mViewManager->contactManager(SYMBIAN_BACKEND), action);
+        connect(other, SIGNAL(actionExecuted(CntActionLauncher*)), this, SLOT(actionExecuted(CntActionLauncher*)));
+        other->execute(*mGroupContact, QContactDetail(), map);
+        }
+}
+
+bool CntGroupActionsView::eventFilter(QObject *obj, QEvent *event)
+{
+    if (event->type() == QEvent::KeyPress && obj == mView->mainWindow())
+    {
+        QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
+        if (keyEvent->key() == Qt::Key_Yes)
+        {
+            return sendKeyPressed();
+        }
+    }
+    return false;
+}
+
+bool CntGroupActionsView::sendKeyPressed()
+{   
+    QList<QContactPhoneNumber> numberDetails = mGroupContact->details<QContactPhoneNumber>();
+    bool keyConsumed = false;
+        
+    // check if conference call number is present
+    if (!numberDetails.isEmpty()) 
+    { 
+        executeConferenceCallAction( *mGroupContact, mGroupContact->details<QContactPhoneNumber>().first(), "call");
+        
+        keyConsumed = true;
+    }
+    
+    return keyConsumed;
+}
+
+void CntGroupActionsView::executeConferenceCallAction(QContact& aContact, const QContactDetail& aDetail, const QString& aAction)
+{
+    if (mIsExecutingAction)
+    {
+        return;
+    }
+    else
+    {
+        mIsExecutingAction = true;
+    }
+    
+    CntActionLauncher* other = new CntActionLauncher(*mViewManager->contactManager(SYMBIAN_BACKEND), aAction);
+    connect(other, SIGNAL(actionExecuted(CntActionLauncher*)), this, SLOT(actionExecuted(CntActionLauncher*)));
+    other->execute(aContact, aDetail);  
+}
+
+void CntGroupActionsView::actionCancelled()
+{
+    //actionpopup cancelled, decrement counter
+    mPopupCount--;
+}
+
+
+void CntGroupActionsView::actionExecuted(CntActionLauncher* aAction)
+{
+    //cleanup
+    aAction->deleteLater();
+    mIsExecutingAction = false;
+}
+