uiservicetab/vimpstengine/src/cvimpstenginecvlistener.cpp
author Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
Tue, 02 Feb 2010 10:12:18 +0200
changeset 0 5e5d6b214f4f
child 11 bef90b82da71
permissions -rw-r--r--
Revision: 201003 Kit: 201005

/*
* Copyright (c) 2008-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:  active object class for interacting with conversation view
*
*/

// INCLUDE FILES

#include "cvimpstenginecvlistener.h"
#include "cvimpststoragemanagerfactory.h"
#include "mvimpststorageserviceview.h"
#include "mvimpststoragecontact.h"
#include "cvimpstengineservicetablefetcher.h"
#include "vimpstdebugtrace.h"

//system includes
#include <e32base.h>
#include <MVPbkContactLink.h>

// ================= MEMBER FUNCTIONS =======================

// --------------------------------------------------------------------------
// CVIMPSTEngineCVListener::CVIMPSTEngineCVListener
// --------------------------------------------------------------------------
//
CVIMPSTEngineCVListener::CVIMPSTEngineCVListener(
    CVIMPSTEngineServiceTableFetcher& aServiceTableFetcher ): 
    CActive( CActive::EPriorityStandard ),
    iServiceTableFetcher( aServiceTableFetcher )
    {
    CActiveScheduler::Add( this );
    }


// --------------------------------------------------------------------------
// CVIMPSTEngineCVListener::ConstructL
// --------------------------------------------------------------------------
//
void CVIMPSTEngineCVListener::ConstructL()
    {
    TRACE( T_LIT("CVIMPSTEngineCVListener::ConstructL() start"));
    // subscribing for the property published by conversation view.
    // attach the properties.
    User::LeaveIfError( iProperty.Attach(KConvViewUID,KXspIdServiceIDKey  ) );
    //start listening property published by CV (to get Contact link and Display name.).
    iProperty.Subscribe( iStatus );
    SetActive();
    TRACE( T_LIT("CVIMPSTEngineCVListener::ConstructL() end"));
    }

// --------------------------------------------------------------------------
// CVIMPSTEngineCVListener::NewL
// --------------------------------------------------------------------------
//
CVIMPSTEngineCVListener* CVIMPSTEngineCVListener::NewL(
        CVIMPSTEngineServiceTableFetcher& aServiceTableFetcher )
    {
    CVIMPSTEngineCVListener* self = new(ELeave) CVIMPSTEngineCVListener( aServiceTableFetcher );
    CleanupStack::PushL(self);
    self->ConstructL();
    CleanupStack::Pop(self);
    return self;
    }
// ---------------------------------------------------------
// CVIMPSTEngineCVListener::~CVIMPSTEngineCVListener()
// C++ Destructor 
// ---------------------------------------------------------
//
CVIMPSTEngineCVListener::~CVIMPSTEngineCVListener()
    {
    Cancel();
    iProperty.Close();
    }
// ---------------------------------------------------------
// CVIMPSTEngineCVListener::RunL()
// ---------------------------------------------------------
//
void CVIMPSTEngineCVListener::RunL()
    {
    TRACE( T_LIT("CVIMPSTEngineCVListener::RunL() start"));
    // resubscribe before processing new value to prevent missing updates
    //TBuf <KMaxSerIdXspIdLen> serId_usrId;
    HBufC16* serId_usrId = HBufC16::NewLC(KMaxSerIdXspIdLen);
    TPtr serId_usrIdPtr(serId_usrId->Des());
    
    iProperty.Get(KConvViewUID,KXspIdServiceIDKey,serId_usrIdPtr);
    // parse the service id and userid form the buf and get the 
    // contactlink and first name and publish it.
    TInt posOfDelimiter  = serId_usrId->Find(KDelimiter);
    if(KErrNotFound != posOfDelimiter )
        {
        TInt serviceId = KErrNotFound;
        TBuf<KMaxServiceIdLength> servIdBuf;
        TPtrC ptr(serId_usrId->Left(posOfDelimiter));
        servIdBuf = ptr;
        TLex16 lex(servIdBuf);
        lex.Val(serviceId);
       
        // Check if this service id belongs to us. If there is customized 
        // service with own service tab, we MUST not handle this callback.
        RArray<TUint32> supportedServices;
        CleanupClosePushL( supportedServices );
        iServiceTableFetcher.GetMasterServiceIdsL( supportedServices );
        TInt ourService = supportedServices.Find( serviceId );
        if( KErrNotFound != ourService )
            {
            MVIMPSTStorageServiceView* storage =  CVIMPSTStorageManagerFactory::ContactListInterfaceL((TUint32)serviceId);
            __ASSERT_ALWAYS( storage, User::Leave( KErrGeneral ));
            TPtrC useridPtr = serId_usrId->Right(serId_usrId->Length() - (posOfDelimiter+ 1) );
            MVIMPSTStorageContact* contact = storage->FindContactByUserId(useridPtr);
            if(contact)
                {
                TBuf8<KMaxPackedContactLinkLength> link;
                link = contact->ContactLink()->PackLC()->Des();
     
                CleanupStack::PopAndDestroy(); //pop and destroy the hbufc8*
                               
                TInt length = KMaxServiceIdLength + useridPtr.Length() + link.Length()
                               + contact->Name().Length() + (KDelimiter().Length() * 3);
                TPtrC fullname = contact->Name();
                HBufC* dispName = NULL;
                posOfDelimiter = fullname.Find(KDelimiter);
                TInt displayIndex = fullname.Length() - (posOfDelimiter+ 1);
                if(KErrNotFound != posOfDelimiter && (displayIndex > 0))
                    {
                    dispName = fullname.Right( displayIndex  ).AllocLC() ;
                    }
                else
                    {
                    dispName= fullname.AllocLC();
                    }
                HBufC16* contactLink = HBufC16::NewLC(link.Length());
                TPtr16 ptr = contactLink->Des();
                ptr.Copy(link);
                HBufC* userDetails = HBufC::NewLC(length);
                TPtr userDetailsPtr = userDetails->Des();
         
                userDetailsPtr.Zero();
                userDetailsPtr.AppendNum(serviceId);
                userDetailsPtr.Append(KDelimiter());
                userDetailsPtr.Append(useridPtr);
                userDetailsPtr.Append(KDelimiter());
                userDetailsPtr.Append(*dispName);
                userDetailsPtr.Append(KDelimiter());
                userDetailsPtr.Append( *contactLink );
                
                TRACE( T_LIT("CVIMPSTEngineCVListener::RunL publishing = %S"),&(*userDetails));        
                
                iProperty.Set(KConvViewUID,KContactLinkDisplayNameKey,*userDetails);
                CleanupStack::PopAndDestroy(3);//userDetails,contactLink,dispName
              
                }
            // contact not found
            else
                {
                TRACE( T_LIT("CVIMPSTEngineCVListener::RunL publishing null display name"));        
                iProperty.Set(KConvViewUID,KContactLinkDisplayNameKey,KNullDesC());
                }
            }
        CleanupStack::PopAndDestroy( &supportedServices );
        }
    CleanupStack::PopAndDestroy( serId_usrId );
    iProperty.Subscribe( iStatus );
    SetActive();
    TRACE( T_LIT("CVIMPSTEngineCVListener::RunL() end"));
    }

// ---------------------------------------------------------
// CVIMPSTEngineCVListener::RunL()
// ---------------------------------------------------------
//
void CVIMPSTEngineCVListener::DoCancel()
    {    
    TRACE( T_LIT("CVIMPSTEngineCVListener::DoCancel() start"));
    iProperty.Cancel();
    }
  		     
// ---------------------------------------------------------
// CIMCVEngineStorageListener::StopListening()
// ---------------------------------------------------------
//
TInt CVIMPSTEngineCVListener::RunError( TInt aError )
    {
    TRACE( T_LIT("CVIMPSTEngineCVListener::RunError() start"));
    if ( KErrCancel != aError )
        {
        iProperty.Subscribe( iStatus );
        SetActive();
        }
    return KErrNone;
    }
//  End of File