phonebookui/phonebookservices/src/cntservicecontactfetchview.cpp
changeset 59 a642906a277a
parent 46 efe85016a067
child 65 ae724a111993
equal deleted inserted replaced
47:7cbcb2896f0e 59:a642906a277a
    17 
    17 
    18 #include "cntservicecontactfetchview.h"
    18 #include "cntservicecontactfetchview.h"
    19 
    19 
    20 #include <cntservicescontact.h>
    20 #include <cntservicescontact.h>
    21 #include <cntlistmodel.h>
    21 #include <cntlistmodel.h>
    22 #include "cntservicehandler.h"
    22 #include "cntserviceviewparams.h"
    23 
    23 
    24 #include <hbmenu.h>
    24 #include <hbmenu.h>
    25 #include <hbview.h>
    25 #include <hbview.h>
    26 #include <hblistview.h>
    26 #include <hblistview.h>
    27 #include <hbdocumentloader.h>
    27 #include <hbdocumentloader.h>
    28 #include <hbaction.h>
    28 #include <hbaction.h>
       
    29 #include "cntactionpopup.h"
    29 
    30 
    30 #include <QCoreApplication>
    31 #include <QCoreApplication>
    31 
    32 
    32 CntServiceContactFetchView::CntServiceContactFetchView(CntServiceHandler *aServiceHandler):
    33 CntServiceContactFetchView::CntServiceContactFetchView( CntAbstractServiceProvider& aServiceProvider ):
    33 CntBaseSelectionView(),
    34 CntBaseSelectionView(),
    34 mServiceHandler(aServiceHandler)
    35 mProvider( aServiceProvider )
    35 {
    36     {
    36     HbAction* cancel = static_cast<HbAction*>( mDocument->findObject( "cnt:cancel" ) );
    37     HbAction* cancel = static_cast<HbAction*>( mDocument->findObject( "cnt:cancel" ) );
    37     mView->menu()->addAction( cancel );
    38     mView->menu()->addAction( cancel );
    38     
       
    39     connect(cancel,  SIGNAL(triggered()), this, SLOT(cancelFetch()) );
    39     connect(cancel,  SIGNAL(triggered()), this, SLOT(cancelFetch()) );
    40     connect( this, SIGNAL(viewClosed()), this, SLOT(aboutToCloseView()) );
    40     connect( this, SIGNAL(viewClosed()), this, SLOT(closeFetchView()) );
    41     connect( this, SIGNAL(viewOpened(CntAbstractViewManager*, const CntViewParameters)), this, SLOT(aboutToOpenView(CntAbstractViewManager*, const CntViewParameters)) );
    41     connect( this, SIGNAL(viewOpened(CntAbstractViewManager*, const CntViewParameters)), this, SLOT(aboutToOpenView(CntAbstractViewManager*, const CntViewParameters)) );
    42 }
    42 }
    43 
    43 
    44 CntServiceContactFetchView::~CntServiceContactFetchView()
    44 CntServiceContactFetchView::~CntServiceContactFetchView()
    45 {
    45 {
    46 }
    46 }
    47 
    47 
    48 
    48 
    49 void CntServiceContactFetchView::cancelFetch()
    49 void CntServiceContactFetchView::cancelFetch()
    50 {
    50     {
    51     connect(mServiceHandler, SIGNAL(returnValueDelivered()), qApp, SLOT(quit()));
       
    52     CntServicesContactList serviceList;
    51     CntServicesContactList serviceList;
    53     mServiceHandler->completeFetch(serviceList);
    52     QVariant variant;
    54 }
    53     variant.setValue(serviceList);
    55 
    54     mProvider.CompleteServiceAndCloseApp(variant);
    56 void CntServiceContactFetchView::aboutToCloseView()
    55     }
    57 {
    56 
    58     CntServicesContactList serviceList;
    57 
       
    58 
       
    59 void CntServiceContactFetchView::closeFetchView()
       
    60 {
    59     QContactManager* mgr = mMgr->contactManager(SYMBIAN_BACKEND);
    61     QContactManager* mgr = mMgr->contactManager(SYMBIAN_BACKEND);
    60     QModelIndexList temp = mListView->selectionModel()->selection().indexes();
    62     QModelIndexList temp = mListView->selectionModel()->selection().indexes();
       
    63     mIndex = 0;
    61     for(int i = 0; i < temp.count(); i++ )
    64     for(int i = 0; i < temp.count(); i++ )
    62     {
    65     {
    63         QContact contact = mListModel->contact(temp.at(i));
    66         QContact contact = mListModel->contact(temp.at(i));
    64         CntServicesContact servicesContact;
    67         if (mAction == KCntActionCall)
    65 
    68         {
    66         //get the name
    69             serviceCallMessageAction(contact, KCntActionCall);
    67         servicesContact.mDisplayName = contact.displayLabel();
    70         }
    68 
    71         else if (mAction == KCntActionSms )
    69         //get the phonenumber
    72         {
    70         QList<QContactPhoneNumber> phonenumbers = contact.details<QContactPhoneNumber>();
    73             serviceCallMessageAction(contact, "message");
    71         if(phonenumbers.count() > 0)
    74         }
    72         {
    75         else if (mAction == KCntActionEmail)
       
    76         {
       
    77             serviceEmailAction(contact);
       
    78         }
       
    79         else
       
    80         {
       
    81             serviceAllAction(contact);
       
    82         }
       
    83     }
       
    84     showPreviousView();
       
    85 }
       
    86 
       
    87 void CntServiceContactFetchView::serviceCallMessageAction(QContact& aContact, QString aAction)
       
    88 {
       
    89     CntServicesContact servicesContact;
       
    90     //get the phonenumber
       
    91     QList<QContactPhoneNumber> phonenumbers = aContact.details<QContactPhoneNumber>();
       
    92     
       
    93     Q_ASSERT_X( phonenumbers.count() > 0, "serviceCallMessageAction", "Number count is zero");
       
    94     
       
    95     QContactDetail detail = aContact.preferredDetail(aAction);
       
    96     if (!detail.isEmpty())
       
    97     {
       
    98         //Set preferred detail for services
       
    99         QContactPhoneNumber number = detail;
       
   100         servicesContact.mDisplayName = aContact.displayLabel();
       
   101         servicesContact.mPhoneNumber = number.number();
       
   102         servicesContact.mEmailAddress = "";
       
   103         servicesContact.mContactId = aContact.localId();
       
   104         mServiceList.append(servicesContact);
       
   105     }
       
   106     else 
       
   107     {
       
   108         // show popup for call numbers
       
   109         CntActionPopup *actionPopup = new CntActionPopup(&aContact);
       
   110         bool popup = actionPopup->showActionPopup(aAction);
       
   111         if(popup)
       
   112         {
       
   113             //if call numbers are present wait for signals
       
   114             mIndex++;
       
   115             connect( actionPopup, SIGNAL(executeContactAction(QContact&,QContactDetail, QString)), this, 
       
   116                     SLOT(popupContactAction(QContact&, QContactDetail, QString)));
       
   117             connect( actionPopup, SIGNAL(actionPopupCancelPressed()), this, 
       
   118                                 SLOT(actionPopupCancelSlot()));
       
   119         }
       
   120         else 
       
   121         {
       
   122             //else delete popup if no call number present
       
   123             delete actionPopup;
       
   124         }
       
   125     }
       
   126 }
       
   127 
       
   128 void CntServiceContactFetchView::serviceEmailAction(QContact& aContact)
       
   129 {
       
   130     CntServicesContact servicesContact;
       
   131     //get the email address
       
   132     QList<QContactEmailAddress> emailAddresses = aContact.details<QContactEmailAddress>();
       
   133     
       
   134     Q_ASSERT_X( emailAddresses.count() > 0, "serviceEmailAction", "email address count is zero");
       
   135     
       
   136     QContactDetail detail = aContact.preferredDetail("email");
       
   137     if (!detail.isEmpty())
       
   138     {
       
   139         //Set preferred detail for services
       
   140         QContactEmailAddress email = detail;
       
   141         servicesContact.mDisplayName = aContact.displayLabel();
       
   142         servicesContact.mPhoneNumber = "";
       
   143         servicesContact.mEmailAddress = email.emailAddress();
       
   144         servicesContact.mContactId = aContact.localId();
       
   145         mServiceList.append(servicesContact);
       
   146         
       
   147     }
       
   148     else 
       
   149     {
       
   150         // show popup for email id's 
       
   151         CntActionPopup *actionPopup = new CntActionPopup(&aContact);
       
   152         bool popup = actionPopup->showActionPopup("email");
       
   153         if(popup)
       
   154         {
       
   155             //if email id's are present wait for signals
       
   156             mIndex++;
       
   157             connect( actionPopup, SIGNAL(executeContactAction(QContact&,QContactDetail, QString)), this, 
       
   158                     SLOT(popupContactAction(QContact&, QContactDetail, QString)));
       
   159             connect( actionPopup, SIGNAL(actionPopupCancelPressed()), this, 
       
   160                                 SLOT(actionPopupCancelSlot()));
       
   161         }
       
   162         else 
       
   163         {
       
   164             //else delete popup if no email present
       
   165             delete actionPopup;
       
   166         }
       
   167     }
       
   168 }
       
   169 
       
   170 void CntServiceContactFetchView::serviceAllAction(QContact& aContact)
       
   171 {
       
   172     CntServicesContact servicesContact;
       
   173     servicesContact.mDisplayName = aContact.displayLabel();
       
   174     QContactDetail callDetail = aContact.preferredDetail("call");
       
   175     //Check for preferredDetail from call
       
   176     // if not present check for preferredDetail for message
       
   177     // if not present pick first phone number
       
   178     if (!callDetail.isEmpty())
       
   179     {
       
   180         QContactPhoneNumber number = callDetail;
       
   181         servicesContact.mPhoneNumber = number.number();
       
   182     }
       
   183     else
       
   184     {
       
   185         QContactDetail smsDetail = aContact.preferredDetail("message");
       
   186         if (!callDetail.isEmpty())
       
   187         {
       
   188             QContactPhoneNumber number = smsDetail;
       
   189             servicesContact.mPhoneNumber = number.number();
       
   190         }
       
   191         else 
       
   192         {
       
   193             QList<QContactPhoneNumber> phonenumbers = aContact.details<QContactPhoneNumber>();
       
   194             if(phonenumbers.count() > 0)
       
   195             {
    73             servicesContact.mPhoneNumber = phonenumbers.first().number();
   196             servicesContact.mPhoneNumber = phonenumbers.first().number();
    74         }
   197             }
    75         else
   198             else
    76         {
   199             {
    77             servicesContact.mPhoneNumber = "";
   200             servicesContact.mPhoneNumber = "";
    78         }
   201             }
    79 
   202         }
       
   203     }
       
   204     
       
   205     QContactDetail emailDetail = aContact.preferredDetail("email");
       
   206     if (!emailDetail.isEmpty())
       
   207     {
       
   208         QContactEmailAddress email = emailDetail;
       
   209         servicesContact.mEmailAddress = email.emailAddress();
       
   210     }
       
   211     else
       
   212     {
    80         //get first email address
   213         //get first email address
    81         QList<QContactEmailAddress> emailAddresses = contact.details<QContactEmailAddress>();
   214         QList<QContactEmailAddress> emailAddresses = aContact.details<QContactEmailAddress>();
    82         if(emailAddresses.count() > 0)
   215         if(emailAddresses.count() > 0)
    83         {
   216         {
    84             servicesContact.mEmailAddress = emailAddresses.first().emailAddress();
   217             servicesContact.mEmailAddress = emailAddresses.first().emailAddress();
    85         }
   218         }
    86         //contact id
   219         else 
    87         servicesContact.mContactId = contact.localId();
   220         {
    88 
   221             servicesContact.mEmailAddress = "";
    89         //append it to the list
   222         }
    90         serviceList.append(servicesContact);
   223     }
    91     }
   224     servicesContact.mContactId = aContact.localId();
    92 
   225     mServiceList.append(servicesContact);
    93     connect(mServiceHandler, SIGNAL(returnValueDelivered()), qApp, SLOT(quit()));
       
    94     mServiceHandler->completeFetch(serviceList);
       
    95 }
   226 }
    96 
   227 
    97 void CntServiceContactFetchView::aboutToOpenView(CntAbstractViewManager* aMgr, const CntViewParameters aArgs)
   228 void CntServiceContactFetchView::aboutToOpenView(CntAbstractViewManager* aMgr, const CntViewParameters aArgs)
    98 {
   229 {
    99     mMgr = aMgr;
   230     mMgr = aMgr;
   100     
   231     
   101     // Set title of the view.
   232     // Set title of the view.
   102     QString title = aArgs.value(CntServiceHandler::ETitle).toString();
   233     QString title = aArgs.value(KCntServiceViewParamTitle).toString();
   103     mView->setTitle(title);
   234     mView->setTitle(title);
   104     
   235     
   105     // Set action filter
   236     // Set action filter
   106     QString filter = aArgs.value(CntServiceHandler::EFilter).toString();
   237     mAction = aArgs.value(ESelectedAction).toString();
   107     QString action = aArgs.value(CntServiceHandler::EAction).toString();
   238     // ESelectedAction is defined in cntviewparams.h
   108     if (action == KCntActionSms)
   239 
       
   240     // Has never been implemented.
       
   241     //QString filterStr = aArgs.value(KCntServiceViewParamFilter).toString();
       
   242     // KCntServiceViewParamFilter is defined in cntserviceviewparams.h
       
   243 
       
   244     if (mAction == KCntActionSms)
   109         {
   245         {
   110             QContactActionFilter actionFilter;
   246             QContactActionFilter actionFilter;
   111             actionFilter.setActionName("message");
   247             actionFilter.setActionName("message");
   112             mListModel->setFilter(actionFilter);
   248             mListModel->setFilter(actionFilter);
   113         }
   249         }
   114         else if (action == KCntActionCall)
   250         else if (mAction == KCntActionCall)
   115         {
   251         {
   116             QContactActionFilter actionFilter;
   252             QContactActionFilter actionFilter;
   117             actionFilter.setActionName("call");
   253             actionFilter.setActionName("call");
   118             mListModel->setFilter(actionFilter);
   254             mListModel->setFilter(actionFilter);
   119         }
   255         }
   120         else if (action == KCntActionEmail)
   256         else if (mAction == KCntActionEmail)
   121         {
   257         {
   122             QContactActionFilter actionFilter;
   258             QContactActionFilter actionFilter;
   123             actionFilter.setActionName("email");
   259             actionFilter.setActionName("email");
   124             mListModel->setFilter(actionFilter);
   260             mListModel->setFilter(actionFilter);
   125         }
   261         }
   137         {
   273         {
   138             mListModel->showMyCard( false );
   274             mListModel->showMyCard( false );
   139         }
   275         }
   140 }
   276 }
   141 
   277 
       
   278 void CntServiceContactFetchView::popupContactAction(QContact& aContact,QContactDetail contactDetail, QString aAction)
       
   279 {
       
   280     if (aAction.compare("call", Qt::CaseInsensitive) == 0 
       
   281            || aAction.compare("message", Qt::CaseInsensitive) == 0 )
       
   282        {       
       
   283            CntServicesContact servicesContact;
       
   284            QContactPhoneNumber number = static_cast<QContactPhoneNumber>(contactDetail);
       
   285            QString name = aContact.displayLabel();
       
   286            servicesContact.mDisplayName = aContact.displayLabel();
       
   287            servicesContact.mPhoneNumber = number.number();
       
   288            servicesContact.mEmailAddress = "";
       
   289            servicesContact.mContactId = aContact.localId();
       
   290            mServiceList.append(servicesContact);
       
   291            mIndex--;
       
   292        }
       
   293        else if (aAction.compare("email", Qt::CaseInsensitive) == 0 )
       
   294        {
       
   295            CntServicesContact servicesContact;
       
   296            QContactEmailAddress email = static_cast<QContactEmailAddress>(contactDetail);
       
   297            servicesContact.mDisplayName = aContact.displayLabel();
       
   298            servicesContact.mPhoneNumber = "";
       
   299            servicesContact.mEmailAddress = email.emailAddress();
       
   300            servicesContact.mContactId = aContact.localId();
       
   301            mServiceList.append(servicesContact);
       
   302            mIndex--;
       
   303        }
       
   304     
       
   305     if (aContact.preferredDetail(aAction).isEmpty() && (aAction == "call" || aAction == "message" || aAction == "email"))
       
   306     {
       
   307         aContact.setPreferredDetail(aAction, contactDetail);
       
   308         //return value will be ignored because we cannot do anything if it fails.
       
   309         mMgr->contactManager(SYMBIAN_BACKEND)->saveContact(&aContact);
       
   310     }
       
   311     
       
   312     showPreviousView();
       
   313 }
       
   314 
       
   315 void CntServiceContactFetchView::actionPopupCancelSlot()
       
   316 {
       
   317     mIndex--;
       
   318     showPreviousView();
       
   319 }
       
   320 
       
   321 void CntServiceContactFetchView::showPreviousView()
       
   322 {
       
   323     if(mIndex==0)
       
   324     {
       
   325         QVariant variant;
       
   326         variant.setValue(mServiceList);
       
   327         mProvider.CompleteServiceAndCloseApp(variant);
       
   328         
       
   329         CntViewParameters args;
       
   330         mMgr->back( args );
       
   331     }
       
   332 }
   142 // EOF
   333 // EOF