imservices/ossprotocoladaptation/src/presencepublishing/cpresencenotificationhandler.cpp
changeset 46 860cd8a5168c
parent 35 085f765766a0
equal deleted inserted replaced
35:085f765766a0 46:860cd8a5168c
     1 /*
       
     2 * Copyright (c) 2007-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:  Implementation for handling presence notification
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 #include <ximpprotocolconnectionhost.h>
       
    20 #include "cpresencenotificationhandler.h"
       
    21 #include "mossprotocolconnectionmanager.h"
       
    22 #include "ossprotocolpluginlogger.h"
       
    23 #include "waittimer.h"
       
    24 #include "cossprotocolconnectionmanager.h"
       
    25 
       
    26 #include <e32des16.h>
       
    27 #include <string.h>
       
    28 #include <sys/types.h>
       
    29 #include <stdio.h>
       
    30 #include <stdlib.h>
       
    31 #include "msgliterals.h"
       
    32 
       
    33 #include "ossprotocoladaptationuids.h"
       
    34 #include "ossprotocoladaptutils.h"
       
    35 
       
    36 #include<avabilitytext.h>
       
    37 
       
    38 
       
    39 // ======== MEMBER FUNCTIONS ========
       
    40 
       
    41 // ---------------------------------------------------------------------------
       
    42 // CPresenceNotificationHandler::CPresenceNotificationHandler
       
    43 // ---------------------------------------------------------------------------
       
    44 //
       
    45 CPresenceNotificationHandler::CPresenceNotificationHandler (
       
    46         MOSSProtocolConnectionManager& aConnMan ) :
       
    47         CActive ( EPriorityNormal ),
       
    48         iConnMan ( aConnMan )
       
    49     {
       
    50     LOGGER ( TXT ( "CPresenceNotificationHandler::CPresenceNotificationHandler Start" ) );
       
    51     CActiveScheduler::Add ( this );
       
    52     LOGGER ( TXT ( "CPresenceNotificationHandler::CPresenceNotificationHandler End" ) );
       
    53     }
       
    54 
       
    55 
       
    56 // ---------------------------------------------------------------------------
       
    57 // CPresenceNotificationHandler::ConstructL
       
    58 // ---------------------------------------------------------------------------
       
    59 //
       
    60 void CPresenceNotificationHandler::ConstructL()
       
    61     {
       
    62     LOGGER ( TXT ( "CPresenceNotificationHandler::ConstructL Start-End" ) );
       
    63     iPresenceCacheWriter = MPresenceCacheWriter2::CreateWriterL();
       
    64 
       
    65     }
       
    66 
       
    67 
       
    68 // ---------------------------------------------------------------------------
       
    69 // CPresenceNotificationHandler::NewL
       
    70 // ---------------------------------------------------------------------------
       
    71 //
       
    72 CPresenceNotificationHandler* CPresenceNotificationHandler::NewL (
       
    73         MOSSProtocolConnectionManager& aConnMan )
       
    74     {
       
    75     LOGGER ( TXT ( "CPresenceNotificationHandler::NewL Start" ) );
       
    76 
       
    77     CPresenceNotificationHandler* self = new ( ELeave ) CPresenceNotificationHandler ( aConnMan );
       
    78     CleanupStack::PushL ( self );
       
    79     self->ConstructL();
       
    80     CleanupStack::Pop ( self );
       
    81 
       
    82     LOGGER ( TXT ( "CPresenceNotificationHandler::NewL End" ) );
       
    83     return self;
       
    84     }
       
    85 
       
    86 
       
    87 // ---------------------------------------------------------------------------
       
    88 // CPresenceNotificationHandler::NewLC
       
    89 // ---------------------------------------------------------------------------
       
    90 //
       
    91 CPresenceNotificationHandler* CPresenceNotificationHandler::NewLC (
       
    92         MOSSProtocolConnectionManager& aConnMan )
       
    93     {
       
    94     LOGGER ( TXT ( "CPresenceNotificationHandler::NewLC Start" ) );
       
    95 
       
    96     CPresenceNotificationHandler* self = CPresenceNotificationHandler::NewL ( aConnMan );
       
    97     CleanupStack::PushL ( self );
       
    98 
       
    99     LOGGER ( TXT ( "CPresenceNotificationHandler::NewLC End" ) );
       
   100     return self;
       
   101     }
       
   102 
       
   103 // ---------------------------------------------------------------------------
       
   104 // CPresenceNotificationHandler::~CPresenceNotificationHandler
       
   105 // ---------------------------------------------------------------------------
       
   106 //
       
   107 CPresenceNotificationHandler::~CPresenceNotificationHandler()
       
   108     {
       
   109     LOGGER ( TXT ( "::~CPresenceNotificationHandler Start" ) );
       
   110 
       
   111     Cancel();
       
   112     //
       
   113     if ( iPresenceCacheWriter )
       
   114         {
       
   115         iPresenceCacheWriter->DeleteService( iConnMan.ServiceName() );
       
   116         delete iPresenceCacheWriter ;
       
   117         }
       
   118     LOGGER ( TXT ( "::~CPresenceNotificationHandler End" ) );
       
   119     }
       
   120 
       
   121 // ---------------------------------------------------------------------------
       
   122 // CPresenceNotificationHandler::DoCancel()
       
   123 // ---------------------------------------------------------------------------
       
   124 //
       
   125 void CPresenceNotificationHandler::DoCancel()
       
   126     {
       
   127     LOGGER ( TXT ( "CPresenceNotificationHandler::DoCancel Start" ) );
       
   128 
       
   129     iConnMan.DataHandler().CancelListeningPres();
       
   130 
       
   131     LOGGER ( TXT ( "CPresenceNotificationHandler::DoCancel End" ) );
       
   132     }
       
   133 
       
   134 // ---------------------------------------------------------------------------
       
   135 // CPresenceNotificationHandler::RunL()
       
   136 // ---------------------------------------------------------------------------
       
   137 //
       
   138 void CPresenceNotificationHandler::RunL()
       
   139     {
       
   140     LOGGER ( TXT ( "CPresenceNotificationHandler::RunL Start" ) );
       
   141 
       
   142     // something is coming from the xmpp server
       
   143     User::LeaveIfError ( iStatus.Int() );
       
   144     ProcessIncomingDataL();
       
   145     StartListeningL();
       
   146 
       
   147     LOGGER ( TXT ( "CPresenceNotificationHandler::RunL End" ) );
       
   148     }
       
   149 
       
   150 // ---------------------------------------------------------------------------
       
   151 // CPresenceNotificationHandler::ProcessIncomingDataL()
       
   152 // ---------------------------------------------------------------------------
       
   153 /* This Function is used to convert the received 
       
   154  *  presence notification from Isolation Server Message queue 
       
   155  *  into the ximpfw data types. and storing the presence information
       
   156  *  in to the presence cache ( available in ximpfw ) 
       
   157  *  client has to read the presence information from the 
       
   158  *  presence cache. when the change in status notification is received
       
   159  *  while updating presence cache, client will receive the notification if subscribed.
       
   160  */
       
   161 void CPresenceNotificationHandler::ProcessIncomingDataL()
       
   162     {	
       
   163     LOGGER ( TXT ( "CPresenceNotificationHandler::ProcessIncomingDataL Start" ) );
       
   164 
       
   165     char* pResponse = NULL, *pOrigResponse = NULL;  
       
   166     pOrigResponse = pResponse = iConnMan.DataHandler().ResponseL ( 2 );// 2 is to differentiate presence notification from server initiated request
       
   167     CleanupStack::PushL(pOrigResponse) ; 	
       
   168     MPresenceBuddyInfo2* buddyPresInfo = MPresenceBuddyInfo2::NewLC();
       
   169 
       
   170     // pResponse contians "<MsgHdr>ContactId/0AvailabilityStatus/0StatusMessage".
       
   171     pResponse += sizeof ( message_hdr_resp ); // Skipping Message Header
       
   172 
       
   173     ///////// Geting Contact-id from message queue
       
   174     char* contactid = pResponse; // contactid
       
   175     HBufC16 *contactName = OssProtocolAdapUtils::ConvertCharToTUint16LC( contactid );   
       
   176     HBufC* name = HBufC::NewLC( contactName->Length() + iConnMan.ServiceName().Length() + KColon().Length() );
       
   177     TPtr namePtr( name->Des() );
       
   178     namePtr.Zero();
       
   179     namePtr.Append(iConnMan.ServiceName() ); // prepend service name
       
   180     namePtr.Append(KColon);
       
   181     namePtr.Append(contactName->Des() );
       
   182     buddyPresInfo->SetIdentityL(namePtr);
       
   183     LOGGER ( TXT ( "CPresenceNotificationHandler::ProcessIncomingDataL  :ContactName:%S " ), contactName );	
       
   184     CleanupStack::PopAndDestroy();//name
       
   185     CleanupStack::PopAndDestroy();//contactName
       
   186 
       
   187     ///////// End Getting Contact-id 
       
   188 
       
   189     int toklength = strlen(pResponse) + 1 ; // finding length of contactid + '/0' 
       
   190     pResponse += toklength;	  // skipping the contactid
       
   191 
       
   192     ///////// Getting availibility text
       
   193     char* availability = pResponse;	  // availability text
       
   194     HBufC16 *message = OssProtocolAdapUtils::ConvertCharToTUint16LC( availability );	    
       
   195     switch (*(pResponse))
       
   196         {
       
   197         case 'd': //compares the first char of "dnd"
       
   198             {
       
   199             buddyPresInfo->SetAvailabilityL( MPresenceBuddyInfo2::EBusy,KDndState); 
       
   200             break;
       
   201 	        }
       
   202         case 'b':
       
   203             {
       
   204             buddyPresInfo->SetAvailabilityL( MPresenceBuddyInfo2::EBusy,*message); 
       
   205             break;
       
   206             }
       
   207         case 'o':  //compares the first char of "offline"
       
   208             {
       
   209             buddyPresInfo->SetAvailabilityL( MPresenceBuddyInfo2::ENotAvailable,*message);            
       
   210             break;
       
   211             }
       
   212         case 'a':  ////compares the first char of "away" and "available"
       
   213             {
       
   214             if ('v' == *(pResponse + 1))
       
   215                 {
       
   216                 buddyPresInfo->SetAvailabilityL( MPresenceBuddyInfo2::EAvailable,*message);                 
       
   217                 } 
       
   218             else 
       
   219                 {
       
   220                 // EAway  - is not available in  presence cache
       
   221                 buddyPresInfo->SetAvailabilityL( MPresenceBuddyInfo2::EBusy,*message);              	              
       
   222                 }
       
   223             break;
       
   224             }
       
   225         case 'x':  
       
   226             {
       
   227             buddyPresInfo->SetAvailabilityL( MPresenceBuddyInfo2::EBusy,KAwayState ); 
       
   228             break;
       
   229             }
       
   230         case 'h':  
       
   231             {
       
   232             buddyPresInfo->SetAvailabilityL( MPresenceBuddyInfo2::EUnknownAvailability,KInvisibleState ); 
       
   233             break;
       
   234             }
       
   235         default:
       
   236             buddyPresInfo->SetAvailabilityL( MPresenceBuddyInfo2::EUnknownAvailability,*message);
       
   237             break;
       
   238         }
       
   239     CleanupStack::PopAndDestroy( message );
       
   240     ///////// End getting availability message
       
   241 
       
   242     toklength = strlen(pResponse) + 1; // finding length of availability text + '/0'
       
   243     pResponse += toklength;		// skipping availability text 
       
   244 
       
   245     ///////// Getting status message if exists
       
   246     if(toklength >1)
       
   247         {
       
   248         char* statustext = pResponse;		
       
   249         HBufC16 *statusmessage = OssProtocolAdapUtils::ConvertCharToTUint16LC( statustext );
       
   250         buddyPresInfo->SetStatusMessageL(*statusmessage);
       
   251         CleanupStack::PopAndDestroy( statusmessage );
       
   252         }	
       
   253     ///////// End Getting status message
       
   254 
       
   255     // Writing into Presence Cache
       
   256     TInt cacheerror = iPresenceCacheWriter->WritePresenceL(buddyPresInfo);
       
   257     User::LeaveIfError ( cacheerror);
       
   258     // End
       
   259     CleanupStack::PopAndDestroy ( 1 ); //buddyPresInfo	
       
   260     CleanupStack::PopAndDestroy(pOrigResponse) ;//pOrigResponse
       
   261     LOGGER ( TXT ( "CPresenceNotificationHandler::ProcessIncomingDataL End" ) );
       
   262     }
       
   263 
       
   264 
       
   265 // ---------------------------------------------------------------------------
       
   266 // CPresenceNotificationHandler::RunError
       
   267 // ---------------------------------------------------------------------------
       
   268 //
       
   269 TInt CPresenceNotificationHandler::RunError ( TInt  aError )
       
   270     {
       
   271     LOGGER ( TXT ( "CPresenceNotificationHandler::RunError Start %d" ), aError );
       
   272 
       
   273     LOGGER ( TXT ( "CPresenceNotificationHandler::RunError End" ) );
       
   274     return KErrNone;
       
   275     }
       
   276 
       
   277 // ---------------------------------------------------------------------------
       
   278 // CPresenceNotificationHandler::StartListeningL
       
   279 // ---------------------------------------------------------------------------
       
   280 //
       
   281 void CPresenceNotificationHandler::StartListeningL()
       
   282     {
       
   283     LOGGER ( TXT ( "CPresenceNotificationHandler::StartListeningL Start" ) );
       
   284 
       
   285     if ( ! ( this->IsActive() ) )
       
   286         {
       
   287         iConnMan.DataHandler().ListenIncomingPresence ( iStatus );
       
   288         SetActive();
       
   289         }
       
   290     LOGGER ( TXT ( "CPresenceNotificationHandler::StartListeningL End" ) );
       
   291     }
       
   292 
       
   293 // End of file