ximpfw/presence/srcpresencedatamodel/presenceinfo/presenceinfofieldcollectionimp.cpp
changeset 0 e6b17d312c8b
equal deleted inserted replaced
-1:000000000000 0:e6b17d312c8b
       
     1 /*
       
     2 * Copyright (c) 2006 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:  MPresenceInfoFieldValueText API object implementation.
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 #include "presenceinfofield.h"
       
    20 #include "presenceinfofieldimp.h"
       
    21 #include "presenceinfofieldcollectionimp.h"
       
    22 #include "presenceapidataobjfactory.h"
       
    23 #include "ximprbufhelpers.h"
       
    24 #include "ximpobjecthelpers.h"
       
    25 #include "presencetypehelpers.h"
       
    26 
       
    27 // ============================ MEMBER FUNCTIONS =============================
       
    28 
       
    29 // ---------------------------------------------------------------------------
       
    30 // CPresenceInfoFieldCollectionImp::NewLC()
       
    31 // ---------------------------------------------------------------------------
       
    32 //
       
    33 EXPORT_C CPresenceInfoFieldCollectionImp* CPresenceInfoFieldCollectionImp::NewLC()
       
    34     {
       
    35     CPresenceInfoFieldCollectionImp* self = new ( ELeave ) CPresenceInfoFieldCollectionImp();
       
    36     CleanupStack::PushL( self );
       
    37     self->ConstructL();
       
    38     return self;
       
    39     }
       
    40 
       
    41 // ---------------------------------------------------------------------------
       
    42 // CPresenceInfoFieldCollectionImp::~CPresenceInfoFieldCollectionImp()
       
    43 // ---------------------------------------------------------------------------
       
    44 //
       
    45 CPresenceInfoFieldCollectionImp::~CPresenceInfoFieldCollectionImp()
       
    46     {
       
    47     iFields.Close();
       
    48     }
       
    49 
       
    50 // ---------------------------------------------------------------------------
       
    51 // CPresenceInfoFieldCollectionImp::CPresenceInfoFieldCollectionImp()
       
    52 // ---------------------------------------------------------------------------
       
    53 //
       
    54 CPresenceInfoFieldCollectionImp::CPresenceInfoFieldCollectionImp()
       
    55     {
       
    56     // nothing
       
    57     }
       
    58 
       
    59 // ---------------------------------------------------------------------------
       
    60 // CPresenceInfoFieldCollectionImp::ConstructL()
       
    61 // ---------------------------------------------------------------------------
       
    62 //
       
    63 void CPresenceInfoFieldCollectionImp::ConstructL()
       
    64     {
       
    65     }
       
    66 
       
    67 // ---------------------------------------------------------------------------
       
    68 // CPresenceInfoFieldCollectionImp::NewFromStreamLC()
       
    69 // ---------------------------------------------------------------------------
       
    70 //
       
    71 XIMPIMP_IMPLEMENT_DATAOBJ_NEWFROMSTREAM( CPresenceInfoFieldCollectionImp )
       
    72 
       
    73 // ---------------------------------------------------------------------------
       
    74 // Implement supported interface access.
       
    75 // ---------------------------------------------------------------------------
       
    76 //
       
    77 XIMPIMP_IF_BASE_GET_INTERFACE_BEGIN( CPresenceInfoFieldCollectionImp, MPresenceInfoFieldCollection )
       
    78 XIMPIMP_IF_BASE_GET_INTERFACE_END()
       
    79 XIMPIMP_IF_BASE_GET_CONST_INTERFACE_BEGIN( CPresenceInfoFieldCollectionImp, MPresenceInfoFieldCollection )
       
    80 XIMPIMP_IF_BASE_GET_INTERFACE_END()
       
    81 
       
    82 
       
    83 // ---------------------------------------------------------------------------
       
    84 // CPresenceInfoFieldCollectionImp::ExternalizeL()
       
    85 // ---------------------------------------------------------------------------
       
    86 //
       
    87 void CPresenceInfoFieldCollectionImp::ExternalizeL( RWriteStream& aStream ) const
       
    88     {
       
    89     CPresenceApiDataObjFactory::ExternalizeL< CPresenceInfoFieldImp >
       
    90         ( aStream, iFields );
       
    91     }
       
    92 
       
    93 // ---------------------------------------------------------------------------
       
    94 // CPresenceInfoFieldCollectionImp::InternalizeL()
       
    95 // ---------------------------------------------------------------------------
       
    96 //
       
    97 EXPORT_C void CPresenceInfoFieldCollectionImp::InternalizeL( RReadStream& aStream )
       
    98     {
       
    99     iFields.ResetAndDestroy();
       
   100     CPresenceApiDataObjFactory::InternalizeL< CPresenceInfoFieldImp >
       
   101         ( aStream, iFields );
       
   102     }
       
   103 
       
   104 // ---------------------------------------------------------------------------
       
   105 // CPresenceInfoFieldCollectionImp::EqualsContent()
       
   106 // ---------------------------------------------------------------------------
       
   107 //
       
   108 TBool CPresenceInfoFieldCollectionImp::EqualsContent( 
       
   109         const CXIMPApiDataObjBase& aOtherInstance ) const
       
   110     {
       
   111     const CPresenceInfoFieldCollectionImp* tmp =
       
   112         TXIMPGetImpClassOrPanic< const CPresenceInfoFieldCollectionImp >::From( 
       
   113                 aOtherInstance.Base() );
       
   114 
       
   115     TBool same = ETrue;
       
   116     TBool x;
       
   117 
       
   118     x = TXIMPEqualsContent< CPresenceInfoFieldImp >::Array( iFields, tmp->iFields );
       
   119     same &= x;
       
   120 
       
   121     return same;
       
   122     }
       
   123 
       
   124 // ---------------------------------------------------------------------------
       
   125 // CPresenceInfoFieldCollectionImp::FieldCount()
       
   126 // ---------------------------------------------------------------------------
       
   127 //
       
   128 TInt CPresenceInfoFieldCollectionImp::FieldCount() const
       
   129     {
       
   130     return iFields.Count();
       
   131     }
       
   132 
       
   133 // ---------------------------------------------------------------------------
       
   134 // CPresenceInfoFieldCollectionImp::FieldAt()
       
   135 // ---------------------------------------------------------------------------
       
   136 //
       
   137 const MPresenceInfoField& CPresenceInfoFieldCollectionImp::FieldAt(
       
   138         TInt aIndex ) const
       
   139     {
       
   140     CPresenceInfoFieldImp* obj = iFields[ aIndex ];
       
   141     MPresenceInfoField* infoField = ( MPresenceInfoField* ) 
       
   142         obj->Base().GetInterface( MPresenceInfoField::KInterfaceId, 
       
   143                 MXIMPBase::EPanicIfUnknown );
       
   144     return *infoField;
       
   145     }
       
   146 
       
   147 // ---------------------------------------------------------------------------
       
   148 // CPresenceInfoFieldCollectionImp::LookupFieldByFieldType()
       
   149 // ---------------------------------------------------------------------------
       
   150 //
       
   151 void CPresenceInfoFieldCollectionImp::LookupFieldByFieldType(
       
   152             MPresenceInfoField*& aPresenceInfoField,
       
   153             const TDesC8& aMatch ) const
       
   154     {
       
   155     TInt start = FindIndexOfObj( aPresenceInfoField );
       
   156     start = start == KErrNotFound ? 0 : start;
       
   157 
       
   158     aPresenceInfoField = NULL;
       
   159 
       
   160     // now find the object with name
       
   161     for ( TInt i = start; i < iFields.Count(); i++ )
       
   162         {
       
   163         CPresenceInfoFieldImp* field = iFields[ i ];
       
   164 
       
   165         if ( KErrNotFound != field->FieldType().MatchF( aMatch ) )
       
   166             {
       
   167             // got a match with correct name
       
   168             aPresenceInfoField = &(*field);
       
   169             return;
       
   170             }
       
   171         }
       
   172 
       
   173     return;
       
   174     }
       
   175     
       
   176 // ---------------------------------------------------------------------------
       
   177 // CPresenceInfoFieldCollectionImp::DeleteFieldByFieldType()
       
   178 // ---------------------------------------------------------------------------
       
   179 //
       
   180 TInt CPresenceInfoFieldCollectionImp::DeleteFieldByFieldType(
       
   181             const TDesC8& aMatch )
       
   182     {
       
   183     TInt deleted(KErrNotFound);
       
   184     // now find the object with name
       
   185     for ( TInt i = 0; i < iFields.Count(); i++ )
       
   186         {
       
   187         CPresenceInfoFieldImp* field = iFields[ i ];
       
   188 
       
   189         if ( KErrNotFound != field->FieldType().MatchF( aMatch ) )
       
   190             {
       
   191             // got a match with correct name
       
   192             iFields.Remove( i );
       
   193             delete field;
       
   194             deleted = KErrNone;
       
   195             }
       
   196         }
       
   197 
       
   198     return deleted;
       
   199     }    
       
   200 
       
   201 // ---------------------------------------------------------------------------
       
   202 // CPresenceInfoFieldCollectionImp::AddOrReplaceFieldL()
       
   203 // ---------------------------------------------------------------------------
       
   204 //
       
   205 void CPresenceInfoFieldCollectionImp::AddOrReplaceFieldL(
       
   206             MPresenceInfoField* aPresenceInfoField )
       
   207     {
       
   208     User::LeaveIfError( aPresenceInfoField == NULL ? KErrArgument : KErrNone );
       
   209 
       
   210     TInt pos = FindIndexOfObj( aPresenceInfoField );
       
   211     CPresenceInfoFieldImp* obj = TXIMPGetImpClassOrPanic< CPresenceInfoFieldImp >::From( *aPresenceInfoField );
       
   212 
       
   213     if ( KErrNotFound == pos )
       
   214         {
       
   215         // not found, so just append
       
   216         iFields.AppendL( obj );
       
   217         }
       
   218     else
       
   219         {
       
   220         // found it, so replace the one at "pos"
       
   221         CPresenceInfoFieldImp* old = iFields[ pos ];
       
   222         iFields.Remove( pos );
       
   223         iFields.Insert( obj, pos );
       
   224         delete old;
       
   225         old = NULL;
       
   226         }
       
   227     }
       
   228     
       
   229     
       
   230 
       
   231 // ---------------------------------------------------------------------------
       
   232 // CPresenceInfoFieldCollectionImp::FindIndexOfObj()
       
   233 // ---------------------------------------------------------------------------
       
   234 //
       
   235 TInt CPresenceInfoFieldCollectionImp::FindIndexOfObj( MPresenceInfoField*& aObject ) const
       
   236     {
       
   237     TInt start = KErrNotFound;
       
   238 
       
   239     // naive lookup, least recently used (from beginning to the end).
       
   240     // find starting position
       
   241     if ( aObject != NULL )
       
   242         {
       
   243         TBool found = EFalse;
       
   244         for ( TInt i = 0; ( i < iFields.Count()-1 ) && ( ! found ); i++ )
       
   245             {
       
   246             CPresenceInfoFieldImp* gotObj = TXIMPGetImpClassOrPanic< CPresenceInfoFieldImp >::From( *aObject );
       
   247             CPresenceInfoFieldImp* obj = iFields[ i ];
       
   248             if ( &(gotObj->Base()) == &(obj->Base()) )
       
   249                 {
       
   250                 found = ETrue;
       
   251                 start = i;
       
   252                 }
       
   253             }
       
   254         }
       
   255 
       
   256     return start;
       
   257     }
       
   258 
       
   259 // ---------------------------------------------------------------------------
       
   260 // CPresenceInfoFieldCollectionImp::RemoveFieldL
       
   261 // ---------------------------------------------------------------------------
       
   262 //
       
   263 void CPresenceInfoFieldCollectionImp::RemoveField( TInt aIndex )
       
   264     {
       
   265     CPresenceInfoFieldImp* field = iFields[ aIndex ];
       
   266     iFields.Remove( aIndex );
       
   267     delete field;
       
   268     }
       
   269 
       
   270 // End of file