messagingapp/msgappfw/client/src/ccsnotificationhandler.cpp
branchRCL_3
changeset 57 ebe688cedc25
equal deleted inserted replaced
54:fa1df4b99609 57:ebe688cedc25
       
     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 EPartialDeleteConversationListEvent:
       
   131             iRequestHandler->HandlePartialDeleteConversationList(iNotificationBuffer);
       
   132             break;
       
   133             
       
   134         case EAddConversationEvent:
       
   135             iRequestHandler->HandleAddConversation(iNotificationBuffer);
       
   136             break;
       
   137 
       
   138         case EDeleteConversationEvent:
       
   139             iRequestHandler->HandleDeleteConversation(iNotificationBuffer);
       
   140             break;
       
   141 
       
   142         case EModifyConversationEvent:
       
   143             iRequestHandler->HandleModifyConversation(iNotificationBuffer);
       
   144             break;
       
   145 
       
   146         case ECachingStartedEvent:
       
   147             iRequestHandler->HandleCachingStarted(iNotificationBuffer);
       
   148             break;
       
   149 
       
   150         case ECachingCompletedEvent:
       
   151             iRequestHandler->HandleCachingCompleted(iNotificationBuffer);
       
   152             break;
       
   153 
       
   154         case ECachingErrorEvent:
       
   155             iRequestHandler->HandleCachingError(iNotificationBuffer);
       
   156             break;
       
   157             
       
   158         case ERefreshConversationListEvent:
       
   159             iRequestHandler->HandleRefreshConversationList(iNotificationBuffer);
       
   160             break;
       
   161                 
       
   162         case ERefreshConversationEvent:
       
   163             iRequestHandler->HandleRefreshConversation(iNotificationBuffer);
       
   164             break;
       
   165         }
       
   166     
       
   167     iLastReqID = currentReqID;
       
   168     RequestChangeEventL(); // re-subscribe
       
   169     }
       
   170 
       
   171 // -----------------------------------------------------------------------------
       
   172 // CCsNotificationHandler::DoCancel()
       
   173 // Cancels any outstanding operation.
       
   174 // -----------------------------------------------------------------------------
       
   175 void CCsNotificationHandler::DoCancel()
       
   176     {
       
   177     // Do nothing
       
   178     }
       
   179 
       
   180 // -----------------------------------------------------------------------------
       
   181 // CCsNotificationHandler::RequestChangeEventL
       
   182 // Request server for any change events
       
   183 // -----------------------------------------------------------------------------
       
   184 void CCsNotificationHandler::RequestChangeEventL()
       
   185     {
       
   186     // Create a buffer to store the search results.
       
   187     if ( iNotificationBuffer )
       
   188         {
       
   189         delete iNotificationBuffer;
       
   190         iNotificationBuffer = NULL;
       
   191         }
       
   192     iNotificationBuffer = HBufC8::NewL(KBufferMaxLen);
       
   193     
       
   194     if ( iNextReqIDBuffer )
       
   195         {
       
   196         delete iNextReqIDBuffer;
       
   197         iNextReqIDBuffer = NULL;
       
   198         }
       
   199     iNextReqIDBuffer = HBufC8::NewL(4);
       
   200     
       
   201     iSession.RequestChangeEventL(iLastReqID, 
       
   202             iNextReqIDBuffer->Des(), 
       
   203             iNotificationBuffer->Des(), 
       
   204             iStatus );   
       
   205   
       
   206     SetActive();
       
   207     }
       
   208 
       
   209 // -----------------------------------------------------------------------------
       
   210 // CCsNotificationHandler::RemoveChangeEventL
       
   211 // Remove for any change events
       
   212 // -----------------------------------------------------------------------------
       
   213 void CCsNotificationHandler::RemoveChangeEventL()
       
   214     {
       
   215     iSession.RemoveChangeEventL();
       
   216     }
       
   217 
       
   218 // -----------------------------------------------------------------------------
       
   219 // CCsNotificationHandler::RequestConversationListChangeEventL
       
   220 // -----------------------------------------------------------------------------
       
   221 void CCsNotificationHandler::RequestConversationListChangeEventL()
       
   222     {
       
   223     iSession.SetConversationListChangeObserverL();  
       
   224     }
       
   225 
       
   226 // -----------------------------------------------------------------------------
       
   227 // CCsNotificationHandler::RemoveConversationListChangeEventL
       
   228 // -----------------------------------------------------------------------------
       
   229 void CCsNotificationHandler::RemoveConversationListChangeEventL()
       
   230     {
       
   231     iSession.ResetConversationListChangeObserverL();  
       
   232     }
       
   233 
       
   234 // -----------------------------------------------------------------------------
       
   235 // CCsNotificationHandler::RequestConversationChangeEventL
       
   236 // -----------------------------------------------------------------------------
       
   237 void CCsNotificationHandler::RequestConversationChangeEventL
       
   238 (CCsClientConversation* aClientConversation)
       
   239     {
       
   240    // Set ConversationChangeObserver for aClientConversation in server
       
   241    // Tmp buffer
       
   242    CBufFlat* dataBuf = CBufFlat::NewL(KBufferMaxLen);
       
   243    CleanupStack::PushL(dataBuf);
       
   244 
       
   245    // Stream over the temp buffer
       
   246    RBufWriteStream dataStream(*dataBuf);
       
   247    dataStream.PushL();
       
   248 
       
   249    // Write the Client Conversation in the stream
       
   250    aClientConversation->ExternalizeL(dataStream);
       
   251    dataStream.CommitL();
       
   252    
       
   253    //------------------Input Buffer--------------------------
       
   254 
       
   255    // Create a HBufC8 for IPC
       
   256    iRequestBuffer = HBufC8::NewL(dataBuf->Size());
       
   257    TPtr8 dataPtr(iRequestBuffer->Des());
       
   258    dataBuf->Read(0, dataPtr, dataBuf->Size());
       
   259 
       
   260    CleanupStack::PopAndDestroy(2, dataBuf);
       
   261    
       
   262    // Send the request
       
   263    iSession.SetConversationChangeObserverL(iRequestBuffer->Des());
       
   264    
       
   265    // Cleanup
       
   266    delete iRequestBuffer;
       
   267    iRequestBuffer = NULL;
       
   268    }
       
   269     
       
   270 // -----------------------------------------------------------------------------
       
   271 // CCsNotificationHandler::RemoveConversationChangeEventL
       
   272 // -----------------------------------------------------------------------------
       
   273 void CCsNotificationHandler::RemoveConversationChangeEventL
       
   274 (CCsClientConversation* aClientConversation)
       
   275     {
       
   276     // Set ConversationChangeObserver for aClientConversation in server
       
   277     // Tmp buffer
       
   278     CBufFlat* dataBuf = CBufFlat::NewL(KBufferMaxLen);
       
   279     CleanupStack::PushL(dataBuf);
       
   280     
       
   281     // Stream over the temp buffer
       
   282     RBufWriteStream dataStream(*dataBuf);
       
   283     dataStream.PushL();
       
   284     
       
   285     // Write the Client Conversation in the stream
       
   286     aClientConversation->ExternalizeL(dataStream);
       
   287     dataStream.CommitL();
       
   288     
       
   289     //------------------Input Buffer--------------------------
       
   290     
       
   291     // Create a HBufC8 for IPC
       
   292     iRequestBuffer = HBufC8::NewL(dataBuf->Size());
       
   293     TPtr8 dataPtr(iRequestBuffer->Des());
       
   294     dataBuf->Read(0, dataPtr, dataBuf->Size());
       
   295     
       
   296     CleanupStack::PopAndDestroy(2, dataBuf);
       
   297     
       
   298     // Send the request
       
   299     iSession.ResetConversationChangeObserverL(iRequestBuffer->Des());
       
   300     
       
   301     // Cleanup
       
   302     delete iRequestBuffer;
       
   303     iRequestBuffer = NULL;
       
   304     }
       
   305   
       
   306 // EOF