cbs/CbsServer/ServerSrc/CCbsDbImpTopicCollection.cpp
changeset 46 2fa1fa551b0b
parent 42 35488577e233
child 48 78df25012fda
equal deleted inserted replaced
42:35488577e233 46:2fa1fa551b0b
     1 /*
       
     2 * Copyright (c) 2003 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 module contains the implementation of CCbsDbImpTopicCollection class 
       
    15 *                member functions.
       
    16 *    
       
    17 *                Topic collection stored in the database is represented by this class.
       
    18 *
       
    19 */
       
    20 
       
    21 
       
    22 
       
    23 // INCLUDE FILES
       
    24 
       
    25 #include <e32svr.h>
       
    26 #include "CbsCommon.h"
       
    27 #include "CbsServerPanic.h"
       
    28 #include "CCbsDbImpTopicCollection.h"
       
    29 #include "CCbsDbImp.H"
       
    30 #include "CbsDbConstants.h"
       
    31 #include "CbsLogger.h"
       
    32 
       
    33 // CONSTANTS
       
    34 
       
    35 // These values specify a range of accepted topic number values (inclusive).
       
    36 const TInt KMinTopicNumber = 1;     // 000 is not accepted.
       
    37 const TInt KMaxTopicNumber = 999;
       
    38 // Granularity for the iIdentities array
       
    39 const TInt KIdentitiesGranularity = 10;
       
    40 
       
    41 
       
    42 // ==================== LOCAL FUNCTIONS ====================
       
    43 
       
    44 // -----------------------------------------------------------------------------
       
    45 // Print
       
    46 // Prints out debug info about topics
       
    47 // Returns: None
       
    48 // -----------------------------------------------------------------------------
       
    49 //
       
    50 #ifdef _DEBUG
       
    51 void Print( const TDesC& aText, const TInt& aValue )
       
    52     {
       
    53     TBuf<100> stuff;
       
    54     TBuf<10> value;
       
    55     value.Num( aValue );
       
    56     stuff = aText;
       
    57     stuff.Append(value);
       
    58     RDebug::Print(stuff);
       
    59     }
       
    60 
       
    61 // -----------------------------------------------------------------------------
       
    62 // PrintTopicCollectionContentL
       
    63 // Prints the contentent of the Topic Collection
       
    64 // Returns: None
       
    65 // -----------------------------------------------------------------------------
       
    66 //
       
    67 void PrintTopicCollectionContentL( const CFileStore* aStore ) 
       
    68     {
       
    69     RDebug::Print(_L("Current topic collection content: "));
       
    70     TStreamId rootStreamId = aStore->Root();
       
    71     if ( rootStreamId == 0 ) 
       
    72         {
       
    73         RDebug::Print(_L("Store contains no root stream."));
       
    74         return;
       
    75         }
       
    76     RStoreReadStream rootStream;
       
    77     rootStream.OpenLC( *aStore, rootStreamId ); // on CS
       
    78     TInt identityCount( rootStream.ReadUint16L() );
       
    79 
       
    80     for ( TInt index( 0 ); index < identityCount; index++ )
       
    81         {
       
    82         TCbsDbTopicIdentity id;
       
    83         id.iName.Zero();
       
    84         TInt length( rootStream.ReadInt16L() );
       
    85         for ( TInt index2( 0 ); index2 < length; index2++ )
       
    86             {
       
    87             TInt character( rootStream.ReadInt16L() );
       
    88             id.iName.Append( TChar(character) );
       
    89             }
       
    90         id.iName.SetLength( length );
       
    91     
       
    92         id.iNumber = rootStream.ReadUint16L();
       
    93 
       
    94         TBuf<255> buf = _L("Identity name: ");
       
    95         buf.Append( id.iName );
       
    96         buf.Append( _L(", number: ") );
       
    97         buf.AppendNum( static_cast<TInt>(id.iNumber) );
       
    98         RDebug::Print( buf );
       
    99         }
       
   100    
       
   101     CleanupStack::PopAndDestroy();    // root stream
       
   102     }
       
   103 #endif // _DEBUG
       
   104     
       
   105 
       
   106 // ================= MEMBER FUNCTIONS =======================
       
   107 
       
   108 // -----------------------------------------------------------------------------
       
   109 // CCbsDbImpTopicCollection::CCbsDbImpTopicCollection
       
   110 // C++ default constructor can NOT contain any code, that
       
   111 // might leave.
       
   112 // -----------------------------------------------------------------------------
       
   113 //
       
   114 CCbsDbImpTopicCollection::CCbsDbImpTopicCollection()
       
   115     { 
       
   116     }
       
   117 
       
   118 // -----------------------------------------------------------------------------
       
   119 // CCbsDbImpTopicCollection::ConstructL
       
   120 // Symbian 2nd phase constructor can leave.
       
   121 // -----------------------------------------------------------------------------
       
   122 //
       
   123 void CCbsDbImpTopicCollection::ConstructL( )
       
   124     {
       
   125     // Create an array for observers.
       
   126     iObservers = new ( ELeave ) CArrayFixFlat<MCbsDbTopicCollectionObserver*>
       
   127         ( KCbsDbObserverArraySize );
       
   128 
       
   129     iIdentities = new ( ELeave ) CArrayFixFlat<TCbsDbTopicIdentity>
       
   130         ( KIdentitiesGranularity );
       
   131     }
       
   132 
       
   133 // -----------------------------------------------------------------------------
       
   134 // CCbsDbImpTopicCollection::NewL
       
   135 // Two-phased constructor.
       
   136 // -----------------------------------------------------------------------------
       
   137 //
       
   138 CCbsDbImpTopicCollection* CCbsDbImpTopicCollection::NewL()
       
   139     {
       
   140     // Normal two phase construction
       
   141     CCbsDbImpTopicCollection* self = 
       
   142             new ( ELeave ) CCbsDbImpTopicCollection();
       
   143     CleanupStack::PushL( self );
       
   144     self->ConstructL();
       
   145     CleanupStack::Pop();
       
   146     return self;
       
   147     }
       
   148 
       
   149 // Destructor
       
   150 CCbsDbImpTopicCollection::~CCbsDbImpTopicCollection()
       
   151     {
       
   152     CBSLOGSTRING("CBSSERVER: >>> CCbsDbImpTopicCollection::~CCbsDbImpTopicCollection()");
       
   153     // Deallocate the observer array
       
   154     delete iObservers;
       
   155     delete iIdentities;
       
   156     CBSLOGSTRING("CBSSERVER: <<< CCbsDbImpTopicCollection::~CCbsDbImpTopicCollection()");
       
   157     }
       
   158 
       
   159 // -----------------------------------------------------------------------------
       
   160 // CCbsDbImpTopicCollection::GetTopicIdentityCount
       
   161 // Returns the number of topic identities stored in the database.
       
   162 // (other items were commented in a header).
       
   163 // -----------------------------------------------------------------------------
       
   164 //
       
   165 void CCbsDbImpTopicCollection::GetTopicIdentityCount( 
       
   166     TInt& aCount ) const
       
   167     {
       
   168     aCount = iIdentities->Count();
       
   169     }
       
   170 
       
   171 // -----------------------------------------------------------------------------
       
   172 // CCbsDbImpTopicCollection::GetTopicIdentityL
       
   173 // Returns a topic identity matching the given topic index parameter.
       
   174 // (other items were commented in a header).
       
   175 // -----------------------------------------------------------------------------
       
   176 //
       
   177 void CCbsDbImpTopicCollection::GetTopicIdentityL( 
       
   178     TInt& aIndex, 
       
   179     TCbsDbTopicIdentity& aIdentity ) const
       
   180     {
       
   181     if ( aIndex >= iIdentities->Count() || aIndex < 0 )
       
   182         {
       
   183         User::Leave( KErrNotFound );
       
   184         }
       
   185     
       
   186     aIdentity = iIdentities->At( aIndex );
       
   187     }
       
   188 
       
   189 // -----------------------------------------------------------------------------
       
   190 // CCbsDbImpTopicCollection::AddObserverL
       
   191 // Adds an observer. The observer will be informed of changes 
       
   192 // in the topic collection.
       
   193 // (other items were commented in a header).
       
   194 // -----------------------------------------------------------------------------
       
   195 //
       
   196 void CCbsDbImpTopicCollection::AddObserverL( 
       
   197     MCbsDbTopicCollectionObserver* aObserver )
       
   198     {
       
   199     __ASSERT_DEBUG( aObserver!=0, CbsServerPanic(ECbsObserverNull) );
       
   200     iObservers->AppendL( aObserver );
       
   201     }
       
   202 
       
   203 // -----------------------------------------------------------------------------
       
   204 // CCbsDbImpTopicCollection::RemoveObserverL
       
   205 // Removes an observer.
       
   206 // (other items were commented in a header).
       
   207 // -----------------------------------------------------------------------------
       
   208 //
       
   209 void CCbsDbImpTopicCollection::RemoveObserver( 
       
   210     const MCbsDbTopicCollectionObserver* aObserver )
       
   211     {
       
   212     __ASSERT_DEBUG( aObserver != 0, CbsServerPanic( ECbsObserverNull ) );
       
   213     TInt amountOfObservers( iObservers->Count() );
       
   214     for ( TInt index( 0 ); index < amountOfObservers; index++ )
       
   215         {
       
   216         if ( aObserver == iObservers->At( index ) )
       
   217             {
       
   218             iObservers->Delete( index );
       
   219             return;
       
   220             }
       
   221         }
       
   222 #ifdef _DEBUG
       
   223     CbsServerPanic( ECbsObserverNotFound );
       
   224 #endif
       
   225     }
       
   226 
       
   227 // -----------------------------------------------------------------------------
       
   228 // CCbsDbImpTopicCollection::ClearL
       
   229 // Clears the topic collection.
       
   230 // (other items were commented in a header).
       
   231 // -----------------------------------------------------------------------------
       
   232 //
       
   233 void CCbsDbImpTopicCollection::Clear()
       
   234     {
       
   235     iIdentities->Reset();
       
   236     }
       
   237 
       
   238 // -----------------------------------------------------------------------------
       
   239 // CCbsDbImpTopicCollection::Apply
       
   240 // Writes topic identity cache (array iIdentities) into a store.
       
   241 // (other items were commented in a header).
       
   242 // -----------------------------------------------------------------------------
       
   243 //
       
   244 void CCbsDbImpTopicCollection::Apply() 
       
   245     {
       
   246     NotifyObservers();
       
   247     }
       
   248 
       
   249 // -----------------------------------------------------------------------------
       
   250 // CCbsDbImpTopicCollection::AddTopicIdentityL
       
   251 // Adds a topic identity to the topic collection. This method 
       
   252 // is called by the index message handling routine after resolving a single 
       
   253 // topic number and name out of an index message.
       
   254 // Topic identities are maintained in ascending topic number order.
       
   255 // (other items were commented in a header).
       
   256 // -----------------------------------------------------------------------------
       
   257 //
       
   258 void CCbsDbImpTopicCollection::AddTopicIdentityL( 
       
   259     const TCbsDbTopicIdentity& aIdentity )
       
   260     {
       
   261     if ( aIdentity.iNumber < KMinTopicNumber || 
       
   262          aIdentity.iNumber > KMaxTopicNumber )
       
   263         {
       
   264         User::Leave( KErrArgument );
       
   265         }
       
   266 
       
   267 	// Insert using binary search. KErrAlreadyExists is returned, but
       
   268 	// all other error codes are passed to Leave.
       
   269 	TKeyArrayFix key( _FOFF( TCbsDbTopicIdentity, iNumber ), ECmpTUint16 );
       
   270 	TRAPD( errorCode, iIdentities->InsertIsqL( aIdentity, key ) );
       
   271 	if ( errorCode != KErrAlreadyExists )
       
   272 		{
       
   273 		User::LeaveIfError( errorCode );
       
   274 		}
       
   275     }    
       
   276 
       
   277 // -----------------------------------------------------------------------------
       
   278 // CCbsDbImpTopicCollection::NotifyObservers
       
   279 // Notifies all registred observers about a topic collection contents change.
       
   280 // (other items were commented in a header).
       
   281 // -----------------------------------------------------------------------------
       
   282 //
       
   283 void CCbsDbImpTopicCollection::NotifyObservers()
       
   284     {
       
   285     // Notify each observer.
       
   286     TInt count( iObservers->Count() );
       
   287     for ( TInt index( 0 ); index < count; index++ )
       
   288         {
       
   289         iObservers->At( index )->TopicCollectionContentsChangedInd();
       
   290         }
       
   291     }
       
   292 
       
   293 // ========================== OTHER EXPORTED FUNCTIONS =========================
       
   294 
       
   295 //  End of File