phonebookui/phonebookservices/src/cntservicecontactfetchview.cpp
author Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
Wed, 23 Jun 2010 18:02:44 +0300
changeset 46 efe85016a067
parent 40 b46a585f6909
child 50 77bc263e1626
child 59 a642906a277a
permissions -rw-r--r--
Revision: 201023 Kit: 2010125

/*
* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
* All rights reserved.
* This component and the accompanying materials are made available
* under the terms of "Eclipse Public License v1.0"
* which accompanies this distribution, and is available
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
*
* Initial Contributors:
* Nokia Corporation - initial contribution.
*
* Contributors:
*
* Description:
*
*/

#include "cntservicecontactfetchview.h"

#include <cntservicescontact.h>
#include <cntlistmodel.h>
#include "cntservicehandler.h"

#include <hbmenu.h>
#include <hbview.h>
#include <hblistview.h>
#include <hbdocumentloader.h>
#include <hbaction.h>

#include <QCoreApplication>

CntServiceContactFetchView::CntServiceContactFetchView(CntServiceHandler *aServiceHandler):
CntBaseSelectionView(),
mServiceHandler(aServiceHandler)
{
    HbAction* cancel = static_cast<HbAction*>( mDocument->findObject( "cnt:cancel" ) );
    mView->menu()->addAction( cancel );
    
    connect(cancel,  SIGNAL(triggered()), this, SLOT(cancelFetch()) );
    connect( this, SIGNAL(viewClosed()), this, SLOT(aboutToCloseView()) );
    connect( this, SIGNAL(viewOpened(CntAbstractViewManager*, const CntViewParameters)), this, SLOT(aboutToOpenView(CntAbstractViewManager*, const CntViewParameters)) );
}

CntServiceContactFetchView::~CntServiceContactFetchView()
{
}


void CntServiceContactFetchView::cancelFetch()
{
    connect(mServiceHandler, SIGNAL(returnValueDelivered()), qApp, SLOT(quit()));
    CntServicesContactList serviceList;
    mServiceHandler->completeFetch(serviceList);
}

void CntServiceContactFetchView::aboutToCloseView()
{
    CntServicesContactList serviceList;
    QContactManager* mgr = mMgr->contactManager(SYMBIAN_BACKEND);
    QModelIndexList temp = mListView->selectionModel()->selection().indexes();
    for(int i = 0; i < temp.count(); i++ )
    {
        QContact contact = mListModel->contact(temp.at(i));
        CntServicesContact servicesContact;

        //get the name
        servicesContact.mDisplayName = contact.displayLabel();

        //get the phonenumber
        QList<QContactPhoneNumber> phonenumbers = contact.details<QContactPhoneNumber>();
        if(phonenumbers.count() > 0)
        {
            servicesContact.mPhoneNumber = phonenumbers.first().number();
        }
        else
        {
            servicesContact.mPhoneNumber = "";
        }

        //get first email address
        QList<QContactEmailAddress> emailAddresses = contact.details<QContactEmailAddress>();
        if(emailAddresses.count() > 0)
        {
            servicesContact.mEmailAddress = emailAddresses.first().emailAddress();
        }
        //contact id
        servicesContact.mContactId = contact.localId();

        //append it to the list
        serviceList.append(servicesContact);
    }

    connect(mServiceHandler, SIGNAL(returnValueDelivered()), qApp, SLOT(quit()));
    mServiceHandler->completeFetch(serviceList);
}

void CntServiceContactFetchView::aboutToOpenView(CntAbstractViewManager* aMgr, const CntViewParameters aArgs)
{
    mMgr = aMgr;
    
    // Set title of the view.
    QString title = aArgs.value(CntServiceHandler::ETitle).toString();
    mView->setTitle(title);
    
    // Set action filter
    QString filter = aArgs.value(CntServiceHandler::EFilter).toString();
    QString action = aArgs.value(CntServiceHandler::EAction).toString();
    if (action == KCntActionSms)
        {
            QContactActionFilter actionFilter;
            actionFilter.setActionName("message");
            mListModel->setFilter(actionFilter);
        }
        else if (action == KCntActionCall)
        {
            QContactActionFilter actionFilter;
            actionFilter.setActionName("call");
            mListModel->setFilter(actionFilter);
        }
        else if (action == KCntActionEmail)
        {
            QContactActionFilter actionFilter;
            actionFilter.setActionName("email");
            mListModel->setFilter(actionFilter);
        }
        else
        {
            QContactDetailFilter filter;
            filter.setDetailDefinitionName(QContactType::DefinitionName, QContactType::FieldType);
            QString typeContact = QContactType::TypeContact;
            filter.setValue(typeContact);
            mListModel->setFilter(filter);
        }

        // hide my card if it's not set
        if ( mListModel->myCardId() == 0 )
        {
            mListModel->showMyCard( false );
        }
}

// EOF