ccservices/cmsservices/cmsengine/Client/src/cmscontactnotifier.cpp
changeset 0 e686773b3f54
equal deleted inserted replaced
-1:000000000000 0:e686773b3f54
       
     1 /*
       
     2 * Copyright (c) 2008 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 
       
    19 #include <e32std.h>
       
    20 #include "cmscontact.h"
       
    21 #include "cmssession.h"
       
    22 #include "cmscontactfield.h"
       
    23 #include "cmscontactfielditem.h"
       
    24 #include "cmsnotificationhandlerapi.h"
       
    25 #include "cmscontactnotifier.h"
       
    26 #include "cmscommondefines.h"
       
    27 
       
    28 
       
    29 // ================= MEMBER FUNCTIONS =======================
       
    30 //
       
    31 
       
    32 // ----------------------------------------------------------
       
    33 // CCmsContactNotifier::CCmsContactNotifier
       
    34 // ----------------------------------------------------------
       
    35 //
       
    36 CCmsContactNotifier::CCmsContactNotifier( 
       
    37     RCmsContact& aContact, 
       
    38     MCmsNotificationHandlerAPI& aObserver,
       
    39     CCmsContactFieldItem::TCmsContactNotification aType )
       
    40     : CActive( CActive::EPriorityStandard), 
       
    41     iContact( aContact ),
       
    42     iStreamDesc( ( TText* )"", 0, 0 ), 
       
    43     iObserver( aObserver ),
       
    44     iPresenceStarted( EFalse ),
       
    45     iPhonebookStarted( EFalse ),
       
    46     iDestroyedPtr( NULL ),
       
    47     iType( aType ),
       
    48     iContactField( NULL )
       
    49     {
       
    50     }
       
    51 
       
    52 // ----------------------------------------------------------
       
    53 // CCmsContactNotifier::~CCmsContactNotifier
       
    54 // ----------------------------------------------------------
       
    55 //
       
    56 CCmsContactNotifier::~CCmsContactNotifier()
       
    57     {        
       
    58     Cancel();
       
    59     delete iStreamBuffer;
       
    60     delete iContactField;     
       
    61     if ( iDestroyedPtr )
       
    62         {
       
    63         // We are called inside callback
       
    64         *iDestroyedPtr = ETrue;
       
    65         iDestroyedPtr = NULL;
       
    66         }
       
    67     }
       
    68     
       
    69 // ----------------------------------------------------------
       
    70 // CCmsContactNotifier::NewL
       
    71 // ----------------------------------------------------------
       
    72 //
       
    73 CCmsContactNotifier* CCmsContactNotifier::NewL(
       
    74     RCmsContact& aContact, 
       
    75     MCmsNotificationHandlerAPI& aObs,
       
    76     CCmsContactFieldItem::TCmsContactNotification aType )
       
    77     {
       
    78     CCmsContactNotifier* self = new (ELeave) CCmsContactNotifier( aContact, aObs, aType );
       
    79     CleanupStack::PushL( self );
       
    80     self->ConstructL(   );
       
    81     CleanupStack::Pop( self );
       
    82     return self;
       
    83     }
       
    84     
       
    85 // ---------------------------------------------------------------------------
       
    86 // CCmsContactNotifier::ConstructL
       
    87 // ---------------------------------------------------------------------------
       
    88 //
       
    89 void CCmsContactNotifier::ConstructL( )
       
    90     {
       
    91     CActiveScheduler::Add(this);        
       
    92     }
       
    93     
       
    94 // ---------------------------------------------------------------------------
       
    95 // CCmsContactNotifier::StartOrderL
       
    96 // ---------------------------------------------------------------------------
       
    97 //
       
    98 void CCmsContactNotifier::StartOrderL( TBool aAskMore )
       
    99     {
       
   100     #ifdef _DEBUG
       
   101         RCmsSession::WriteToLog( _L8( "CCmsContactNotifier::StartOrderL() - Ask more: %d" ), aAskMore );
       
   102     #endif 
       
   103 
       
   104     if ( IsActive() )
       
   105     	{
       
   106     	User::Leave( KErrInUse );
       
   107     	}
       
   108     else
       
   109         {        
       
   110         }
       
   111     
       
   112     if ( iType & CCmsContactFieldItem::ECmsPresenceAllNotification )
       
   113         {
       
   114         #ifdef _DEBUG
       
   115         RCmsSession::WriteToLog( _L8( "  Presence notification ordered type=%d" ), iType );
       
   116         #endif
       
   117         StartPresenceNotifyL( aAskMore );             
       
   118         } 
       
   119     else if ( iType & CCmsContactFieldItem::ECmsPhonebookNotification )
       
   120         {
       
   121         #ifdef _DEBUG
       
   122         RCmsSession::WriteToLog( _L8( "  Phonebook notification ordered" ) );
       
   123         #endif
       
   124         StartPhoneboookNotifyL( aAskMore );            
       
   125         }
       
   126     else
       
   127         {
       
   128         User::Leave( KErrNotSupported );
       
   129         }                      	    
       
   130     }
       
   131 
       
   132 // ---------------------------------------------------------------------------
       
   133 // CCmsContactNotifier::StartPhoneboookNotifyL
       
   134 //
       
   135 // ---------------------------------------------------------------------------
       
   136 //
       
   137 void CCmsContactNotifier::StartPhoneboookNotifyL( TBool aAskMore )
       
   138     {
       
   139     #ifdef _DEBUG
       
   140         RCmsSession::WriteToLog( _L8( "CCmsContactNotifier::StartPhoneboookNotifyL()" ) );
       
   141     #endif
       
   142     if( !iPhonebookStarted )
       
   143         {
       
   144         delete iStreamBuffer; 
       
   145         iStreamBuffer = NULL;
       
   146         iStreamBuffer = HBufC::NewL( 5 );
       
   147         iStreamDesc.Set( iStreamBuffer->Des() );
       
   148         TIpcArgs arguments( iType, &iStreamDesc );
       
   149         if( !aAskMore )
       
   150             {
       
   151             iContact.SendMessage( ECmsOrderNotification, arguments, iStatus );         
       
   152             }
       
   153         else
       
   154             {
       
   155             iContact.SendMessage( ECmsOrderNextNotification, arguments, iStatus );      
       
   156             }
       
   157         iPhonebookStarted = ETrue;
       
   158         SetActive();
       
   159         #ifdef _DEBUG
       
   160             RCmsSession::WriteToLog( _L8( "  Notification started" ) );
       
   161         #endif
       
   162         }
       
   163     else
       
   164         {
       
   165         #ifdef _DEBUG
       
   166             RCmsSession::WriteToLog( _L8( "  Notification already started => ignore" ) );
       
   167         #endif
       
   168         }
       
   169     }
       
   170 
       
   171 // ---------------------------------------------------------------------------
       
   172 // CCmsContactNotifier::StartPresenceNotifyL
       
   173 //
       
   174 // ---------------------------------------------------------------------------
       
   175 //
       
   176 void CCmsContactNotifier::StartPresenceNotifyL( TBool aAskMore )
       
   177     {
       
   178     #ifdef _DEBUG
       
   179         RCmsSession::WriteToLog( _L8( "CCmsContactNotifier::StartPresenceNotifyL()" ) );
       
   180     #endif
       
   181     delete iContactField;
       
   182     iContactField = NULL;    
       
   183     iContactField = CCmsContactField::NewL( iContact, iStatus, 
       
   184                     CCmsContactFieldItem::ECmsPresenceData ); 
       
   185     TIpcArgs arguments( iType, iContactField->StreamDesc8() );
       
   186     if( !aAskMore )
       
   187         {
       
   188         iContact.SendMessage( ECmsOrderNotification, arguments, iContactField->Activate() );         
       
   189         }
       
   190     else
       
   191         {
       
   192         iContact.SendMessage( ECmsOrderNextNotification, arguments, iContactField->Activate() );         
       
   193         }  
       
   194     SetActive();    
       
   195     }
       
   196         
       
   197 // ----------------------------------------------------------
       
   198 // CCmsContactNotifier::RunL
       
   199 // ----------------------------------------------------------
       
   200 //
       
   201 void CCmsContactNotifier::RunL()
       
   202     {
       
   203     #ifdef _DEBUG
       
   204         RCmsSession::WriteToLog( _L8( "CCmsContactNotifier::RunL() - Status: %d" ), iStatus.Int() );
       
   205     #endif 
       
   206     TInt pbEvent = KErrGeneral;
       
   207     TInt status = iStatus.Int();
       
   208     if( status )
       
   209         {
       
   210         RunError( status );
       
   211         }
       
   212     else
       
   213         {
       
   214         // Set the member to point to stack variable
       
   215         TBool destroyed( EFalse );
       
   216         iDestroyedPtr = &destroyed;
       
   217         if ( CCmsContactFieldItem::ECmsPresenceAllNotification & Type() )
       
   218             {
       
   219             TRAP_IGNORE( iObserver.HandlePresenceNotificationL( iContactField ) );
       
   220             }
       
   221         else if( CCmsContactFieldItem::ECmsPhonebookNotification == Type() )
       
   222             {
       
   223             TLex lex( iStreamDesc );
       
   224             TInt event = KErrNotFound;
       
   225             lex.Val( event );
       
   226             iPhonebookStarted = EFalse;            
       
   227             TRAP_IGNORE( iObserver.HandlePhonebookNotificationL( ( TCmsPhonebookEvent  )event ) );
       
   228             pbEvent = event;
       
   229             }
       
   230         else
       
   231             {
       
   232             RunError( KErrNotSupported );
       
   233             }
       
   234         if ( !destroyed )
       
   235             {
       
   236             iDestroyedPtr = NULL;
       
   237             }
       
   238         CheckRestartL( destroyed, ( TCmsPhonebookEvent  )pbEvent );
       
   239         }
       
   240     }
       
   241 
       
   242 // ----------------------------------------------------------
       
   243 // CCmsContactNotifier::CheckRestartL
       
   244 //
       
   245 // ----------------------------------------------------------
       
   246 //
       
   247 void CCmsContactNotifier::CheckRestartL( TBool aDestroyed, TCmsPhonebookEvent aEvent )
       
   248     {
       
   249     #ifdef _DEBUG
       
   250         RCmsSession::WriteToLog( _L8( "CCmsContactNotifier::CheckRestartL()" ) );
       
   251     #endif
       
   252     //If the object has been deleted inside the callback or
       
   253     //if the contact handle has been deleted in the server,
       
   254     //notifications must not be re-ordered
       
   255     if( !aDestroyed && ECmsContactDeleted != aEvent )
       
   256         {
       
   257         // Ownership is transferred
       
   258         iContactField = NULL;                 
       
   259         StartOrderL( ETrue );
       
   260         #ifdef _DEBUG
       
   261             RCmsSession::WriteToLog( _L8( "  Notification renewed" ) );
       
   262         #endif
       
   263         }         
       
   264     } 
       
   265    
       
   266 // ----------------------------------------------------------
       
   267 // CCmsContactNotifier::RunError
       
   268 // ----------------------------------------------------------
       
   269 //
       
   270 TInt CCmsContactNotifier::RunError( TInt aError )
       
   271     {
       
   272     #ifdef _DEBUG
       
   273         RCmsSession::WriteToLog( _L8( "CCmsContactNotifier::RunError() - Error: %d" ), aError );
       
   274     #endif       
       
   275     // Save this since we ask to delete ourself and members cannot be used anymore.
       
   276     MCmsNotificationHandlerAPI& observer = iObserver; 
       
   277     CCmsContactFieldItem::TCmsContactNotification myType = Type();    
       
   278     iContact.DeleteNotifier( myType );
       
   279     observer.CmsNotificationTerminatedL( aError, myType );
       
   280     return KErrNone;          
       
   281     }          
       
   282 
       
   283 // ----------------------------------------------------------
       
   284 // CCmsContactNotifier::DoCancel
       
   285 // ----------------------------------------------------------
       
   286 //
       
   287 void CCmsContactNotifier::DoCancel()
       
   288     {
       
   289     #ifdef _DEBUG
       
   290         RCmsSession::WriteToLog( _L8( "CCmsContactNotifier::DoCancel()" ) );
       
   291     #endif
       
   292     TIpcArgs arguments( iType );
       
   293     iContact.SendMessage( ECmsCancelNotification, arguments );
       
   294     if ( CCmsContactFieldItem::ECmsPresenceAllNotification & Type() )
       
   295         {
       
   296         iContactField->Cancel();
       
   297         }
       
   298     }
       
   299 
       
   300 // ----------------------------------------------------------
       
   301 //
       
   302 CCmsContactFieldItem::TCmsContactNotification CCmsContactNotifier::Type()
       
   303     {
       
   304     return iType;
       
   305     }
       
   306         
       
   307         
       
   308