messagingapp/msgappfw/client/src/ccsnotificationhandler.cpp
changeset 23 238255e8b033
child 37 518b245aa84c
equal deleted inserted replaced
5:4697dfb2d7ad 23:238255e8b033
       
     1 /*
       
     2 * Copyright (c) 2007 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:  This class handles asynchronous notifications (e.g. new messages) 
       
    15 *               from server to client.
       
    16 *
       
    17 */
       
    18 
       
    19 // SYSTEM INCLUDES
       
    20 #include <ccsnotificationhandler.h>
       
    21 #include <ccsrequesthandler.h>
       
    22 #include <rcssession.h>
       
    23 #include <ccsclientconversation.h>
       
    24 
       
    25 // USER INCLUDES
       
    26 #include "ccsdebug.h"
       
    27 
       
    28 // -----------------------------------------------------------------------------
       
    29 // CCsNotificationHandler::NewL()
       
    30 // Two-phased constructor.
       
    31 // -----------------------------------------------------------------------------
       
    32 CCsNotificationHandler* CCsNotificationHandler::
       
    33 NewL(CCSRequestHandler* aRequestHandler)
       
    34     {
       
    35     PRINT ( _L("Enter CCsNotificationHandler::NewL") );
       
    36 
       
    37     CCsNotificationHandler* self = new ( ELeave ) CCsNotificationHandler();
       
    38     CleanupStack::PushL( self );
       
    39     self->ConstructL(aRequestHandler);
       
    40     CleanupStack::Pop();
       
    41     
       
    42     PRINT ( _L("End CCsNotificationHandler::NewL") );
       
    43 
       
    44     return( self ) ;
       
    45     }
       
    46 
       
    47 // -----------------------------------------------------------------------------
       
    48 // CCsNotificationHandler::CCsNotificationHandler()
       
    49 // C++ default constructor can NOT contain any code, that might leave.
       
    50 // -----------------------------------------------------------------------------
       
    51 CCsNotificationHandler::CCsNotificationHandler()
       
    52 : CActive( EPriorityStandard )
       
    53     {
       
    54     CActiveScheduler::Add( this );
       
    55     }
       
    56 
       
    57 // -----------------------------------------------------------------------------
       
    58 // CCsNotificationHandler::~CCsNotificationHandler()
       
    59 // Destructor.
       
    60 // -----------------------------------------------------------------------------
       
    61 CCsNotificationHandler::~CCsNotificationHandler()
       
    62     {
       
    63     // Unsubscribe
       
    64     TRAP_IGNORE(RemoveChangeEventL());
       
    65     
       
    66     Cancel();
       
    67     
       
    68     iSession.Close();
       
    69     
       
    70     if ( iNotificationBuffer )
       
    71         {
       
    72         delete iNotificationBuffer;
       
    73         iNotificationBuffer = NULL;
       
    74         }
       
    75     
       
    76     if ( iNextReqIDBuffer )
       
    77         {
       
    78         delete iNextReqIDBuffer;
       
    79         iNextReqIDBuffer = NULL;
       
    80         }
       
    81     }
       
    82 
       
    83 // -----------------------------------------------------------------------------
       
    84 // CCsNotificationHandler::ConstructL()
       
    85 // Symbian 2nd phase constructor can leave.
       
    86 // -----------------------------------------------------------------------------
       
    87 void CCsNotificationHandler::ConstructL(CCSRequestHandler* aRequestHandler)
       
    88     {
       
    89     iRequestHandler = aRequestHandler;
       
    90     iLastReqID = -1; 
       
    91     iNextReqIDBuffer = NULL;
       
    92     iNotificationBuffer = NULL;
       
    93     
       
    94     User::LeaveIfError( iSession.Connect() );
       
    95   
       
    96     // Subscribe
       
    97     RequestChangeEventL();
       
    98     }
       
    99 
       
   100 // -----------------------------------------------------------------------------
       
   101 // CCsNotificationHandler::RunL()
       
   102 // Invoked to handle responses from the server.
       
   103 // -----------------------------------------------------------------------------
       
   104 void CCsNotificationHandler::RunL()
       
   105     {
       
   106     TInt currentReqID = 0;
       
   107     if ( iNextReqIDBuffer )
       
   108        {
       
   109        TLex8 lex(iNextReqIDBuffer->Des());
       
   110        lex.Val(currentReqID);
       
   111        
       
   112        delete iNextReqIDBuffer;
       
   113        iNextReqIDBuffer = NULL;
       
   114        }
       
   115        
       
   116     switch ( iStatus.Int() ) 
       
   117         {
       
   118         case EAddConversationListEvent:
       
   119             iRequestHandler->HandleAddConversationList(iNotificationBuffer);
       
   120             break;
       
   121 
       
   122         case EDeleteConversationListEvent:
       
   123             iRequestHandler->HandleDeleteConversationList(iNotificationBuffer);
       
   124             break;
       
   125 
       
   126         case EModifyConversationListEvent:
       
   127             iRequestHandler->HandleModifyConversationList(iNotificationBuffer);
       
   128             break;
       
   129 
       
   130         case EAddConversationEvent:
       
   131             iRequestHandler->HandleAddConversation(iNotificationBuffer);
       
   132             break;
       
   133 
       
   134         case EDeleteConversationEvent:
       
   135             iRequestHandler->HandleDeleteConversation(iNotificationBuffer);
       
   136             break;
       
   137 
       
   138         case EModifyConversationEvent:
       
   139             iRequestHandler->HandleModifyConversation(iNotificationBuffer);
       
   140             break;
       
   141 
       
   142         case ECachingStartedEvent:
       
   143             iRequestHandler->HandleCachingStarted(iNotificationBuffer);
       
   144             break;
       
   145 
       
   146         case ECachingCompletedEvent:
       
   147             iRequestHandler->HandleCachingCompleted(iNotificationBuffer);
       
   148             break;
       
   149 
       
   150         case ECachingErrorEvent:
       
   151             iRequestHandler->HandleCachingError(iNotificationBuffer);
       
   152             break;
       
   153             
       
   154         case ERefreshConversationListEvent:
       
   155             iRequestHandler->HandleRefreshConversationList(iNotificationBuffer);
       
   156             break;
       
   157                 
       
   158         case ERefreshConversationEvent:
       
   159             iRequestHandler->HandleRefreshConversation(iNotificationBuffer);
       
   160             break;
       
   161         }
       
   162     
       
   163     iLastReqID = currentReqID;
       
   164     RequestChangeEventL(); // re-subscribe
       
   165     }
       
   166 
       
   167 // -----------------------------------------------------------------------------
       
   168 // CCsNotificationHandler::DoCancel()
       
   169 // Cancels any outstanding operation.
       
   170 // -----------------------------------------------------------------------------
       
   171 void CCsNotificationHandler::DoCancel()
       
   172     {
       
   173     // Do nothing
       
   174     }
       
   175 
       
   176 // -----------------------------------------------------------------------------
       
   177 // CCsNotificationHandler::RequestChangeEventL
       
   178 // Request server for any change events
       
   179 // -----------------------------------------------------------------------------
       
   180 void CCsNotificationHandler::RequestChangeEventL()
       
   181     {
       
   182     // Create a buffer to store the search results.
       
   183     if ( iNotificationBuffer )
       
   184         {
       
   185         delete iNotificationBuffer;
       
   186         iNotificationBuffer = NULL;
       
   187         }
       
   188     iNotificationBuffer = HBufC8::NewL(KBufferMaxLen);
       
   189     
       
   190     if ( iNextReqIDBuffer )
       
   191         {
       
   192         delete iNextReqIDBuffer;
       
   193         iNextReqIDBuffer = NULL;
       
   194         }
       
   195     iNextReqIDBuffer = HBufC8::NewL(4);
       
   196     
       
   197     iSession.RequestChangeEventL(iLastReqID, 
       
   198             iNextReqIDBuffer->Des(), 
       
   199             iNotificationBuffer->Des(), 
       
   200             iStatus );   
       
   201   
       
   202     SetActive();
       
   203     }
       
   204 
       
   205 // -----------------------------------------------------------------------------
       
   206 // CCsNotificationHandler::RemoveChangeEventL
       
   207 // Remove for any change events
       
   208 // -----------------------------------------------------------------------------
       
   209 void CCsNotificationHandler::RemoveChangeEventL()
       
   210     {
       
   211     iSession.RemoveChangeEventL();
       
   212     }
       
   213 
       
   214 // -----------------------------------------------------------------------------
       
   215 // CCsNotificationHandler::RequestConversationListChangeEventL
       
   216 // -----------------------------------------------------------------------------
       
   217 void CCsNotificationHandler::RequestConversationListChangeEventL()
       
   218     {
       
   219     iSession.SetConversationListChangeObserverL();  
       
   220     }
       
   221 
       
   222 // -----------------------------------------------------------------------------
       
   223 // CCsNotificationHandler::RemoveConversationListChangeEventL
       
   224 // -----------------------------------------------------------------------------
       
   225 void CCsNotificationHandler::RemoveConversationListChangeEventL()
       
   226     {
       
   227     iSession.ResetConversationListChangeObserverL();  
       
   228     }
       
   229 
       
   230 // -----------------------------------------------------------------------------
       
   231 // CCsNotificationHandler::RequestConversationChangeEventL
       
   232 // -----------------------------------------------------------------------------
       
   233 void CCsNotificationHandler::RequestConversationChangeEventL
       
   234 (CCsClientConversation* aClientConversation)
       
   235     {
       
   236    // Set ConversationChangeObserver for aClientConversation in server
       
   237    // Tmp buffer
       
   238    CBufFlat* dataBuf = CBufFlat::NewL(KBufferMaxLen);
       
   239    CleanupStack::PushL(dataBuf);
       
   240 
       
   241    // Stream over the temp buffer
       
   242    RBufWriteStream dataStream(*dataBuf);
       
   243    dataStream.PushL();
       
   244 
       
   245    // Write the Client Conversation in the stream
       
   246    aClientConversation->ExternalizeL(dataStream);
       
   247    dataStream.CommitL();
       
   248    
       
   249    //------------------Input Buffer--------------------------
       
   250 
       
   251    // Create a HBufC8 for IPC
       
   252    iRequestBuffer = HBufC8::NewL(dataBuf->Size());
       
   253    TPtr8 dataPtr(iRequestBuffer->Des());
       
   254    dataBuf->Read(0, dataPtr, dataBuf->Size());
       
   255 
       
   256    CleanupStack::PopAndDestroy(2, dataBuf);
       
   257    
       
   258    // Send the request
       
   259    iSession.SetConversationChangeObserverL(iRequestBuffer->Des());
       
   260    
       
   261    // Cleanup
       
   262    delete iRequestBuffer;
       
   263    iRequestBuffer = NULL;
       
   264    }
       
   265     
       
   266 // -----------------------------------------------------------------------------
       
   267 // CCsNotificationHandler::RemoveConversationChangeEventL
       
   268 // -----------------------------------------------------------------------------
       
   269 void CCsNotificationHandler::RemoveConversationChangeEventL
       
   270 (CCsClientConversation* aClientConversation)
       
   271     {
       
   272     // Set ConversationChangeObserver for aClientConversation in server
       
   273     // Tmp buffer
       
   274     CBufFlat* dataBuf = CBufFlat::NewL(KBufferMaxLen);
       
   275     CleanupStack::PushL(dataBuf);
       
   276     
       
   277     // Stream over the temp buffer
       
   278     RBufWriteStream dataStream(*dataBuf);
       
   279     dataStream.PushL();
       
   280     
       
   281     // Write the Client Conversation in the stream
       
   282     aClientConversation->ExternalizeL(dataStream);
       
   283     dataStream.CommitL();
       
   284     
       
   285     //------------------Input Buffer--------------------------
       
   286     
       
   287     // Create a HBufC8 for IPC
       
   288     iRequestBuffer = HBufC8::NewL(dataBuf->Size());
       
   289     TPtr8 dataPtr(iRequestBuffer->Des());
       
   290     dataBuf->Read(0, dataPtr, dataBuf->Size());
       
   291     
       
   292     CleanupStack::PopAndDestroy(2, dataBuf);
       
   293     
       
   294     // Send the request
       
   295     iSession.ResetConversationChangeObserverL(iRequestBuffer->Des());
       
   296     
       
   297     // Cleanup
       
   298     delete iRequestBuffer;
       
   299     iRequestBuffer = NULL;
       
   300     }
       
   301   
       
   302 // EOF