javauis/amms_qt/module/src/cammsmodule.cpp
changeset 23 98ccebc37403
equal deleted inserted replaced
21:2a9601315dfc 23:98ccebc37403
       
     1 /*
       
     2 * Copyright (c) 2005-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 is a container for MAMMSControlGroup objects.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include "cammsmodule.h"
       
    21 #include "cammscontrolgroup.h"
       
    22 #include <logger.h>
       
    23 
       
    24 
       
    25 // CONSTANTS
       
    26 // Before further testing is done, 4 is sufficient average value to be used here.
       
    27 const TInt KAMMSModuleDefaultGranularity = 4;
       
    28 
       
    29 // ============================ MEMBER FUNCTIONS ===============================
       
    30 
       
    31 // -----------------------------------------------------------------------------
       
    32 // CAMMSModule::NewL
       
    33 // Two-phased constructor.
       
    34 // -----------------------------------------------------------------------------
       
    35 CAMMSModule* CAMMSModule::NewL()
       
    36 {
       
    37     CAMMSModule* self = NewLC();
       
    38     CleanupStack::Pop(self);
       
    39 
       
    40     return self;
       
    41 }
       
    42 
       
    43 // -----------------------------------------------------------------------------
       
    44 // CAMMSModule::NewLC
       
    45 // Two-phased constructor.
       
    46 // -----------------------------------------------------------------------------
       
    47 CAMMSModule* CAMMSModule::NewLC()
       
    48 {
       
    49     CAMMSModule* self = new(ELeave) CAMMSModule;
       
    50 
       
    51     CleanupStack::PushL(self);
       
    52     self->ConstructL();
       
    53     return self;
       
    54 }
       
    55 
       
    56 // Destructor
       
    57 CAMMSModule::~CAMMSModule()
       
    58 {
       
    59     // Check that construction fully succeed.
       
    60     if (iControlGroups)
       
    61     {
       
    62         // control groups are owned and they must be deleted.
       
    63         iControlGroups->ResetAndDestroy();
       
    64         delete iControlGroups;
       
    65     }
       
    66 
       
    67     // Players are not owned.
       
    68     iPlayers.Close();
       
    69 }
       
    70 
       
    71 // -----------------------------------------------------------------------------
       
    72 // CAMMSModule::AddControlGroupL
       
    73 // Add control to iControlGroups array.
       
    74 // (other items were commented in a header).
       
    75 // -----------------------------------------------------------------------------
       
    76 //
       
    77 void CAMMSModule::AddControlGroupAndPopL(CAMMSControlGroup* aGroup)
       
    78 {
       
    79     // Check in debug build that group is not null.
       
    80     __ASSERT_DEBUG(aGroup, User::Invariant());
       
    81 
       
    82     iControlGroups->AppendL(aGroup);
       
    83 
       
    84     // aGroup must be the previous item in the cleanupstack
       
    85     CleanupStack::Pop(aGroup);
       
    86 }
       
    87 
       
    88 // -----------------------------------------------------------------------------
       
    89 // CAMMSModule::AddPlayerL
       
    90 // Adds new player to this module.
       
    91 // (other items were commented in a header).
       
    92 // -----------------------------------------------------------------------------
       
    93 //
       
    94 void CAMMSModule::AddPlayerL(CMMAPlayer* aPlayer)
       
    95 {
       
    96     // Check in debug build that player is not null.
       
    97     __ASSERT_DEBUG(aPlayer, User::Invariant());
       
    98 
       
    99     // Must leave if the player is already in the module
       
   100     if (HasPlayer(aPlayer))
       
   101     {
       
   102         User::Leave(KErrArgument);
       
   103     }
       
   104 
       
   105     // Must leave if player is in PREFETCHED or STARTED state
       
   106     User::LeaveIfError(CheckPlayerState(aPlayer));
       
   107 
       
   108     // Check that none of the players is not in PREFETCHED or STARTED state
       
   109     User::LeaveIfError(CheckAllPlayersState());
       
   110 
       
   111     AddPlayerNoStateCheckL(aPlayer);
       
   112 }
       
   113 
       
   114 // -----------------------------------------------------------------------------
       
   115 // CAMMSModule::RemovePlayer
       
   116 // Removes player from module..
       
   117 // (other items were commented in a header).
       
   118 // -----------------------------------------------------------------------------
       
   119 //
       
   120 TInt CAMMSModule::RemovePlayer(CMMAPlayer* aPlayer)
       
   121 {
       
   122     // call RemovePlayerNoStateCheck only if aPlayer and all players in the
       
   123     // module are in the right state.
       
   124     TInt error = CheckPlayerState(aPlayer);
       
   125     if (error == KErrNone)
       
   126     {
       
   127         error = CheckAllPlayersState();
       
   128         if (error == KErrNone)
       
   129         {
       
   130             error = RemovePlayerNoStateCheck(aPlayer);
       
   131         }
       
   132     }
       
   133     return error;
       
   134 }
       
   135 
       
   136 // -----------------------------------------------------------------------------
       
   137 // CAMMSModule::AddPlayerNoStateCheckL
       
   138 // Adds player without checking its state.
       
   139 // (other items were commented in a header).
       
   140 // -----------------------------------------------------------------------------
       
   141 //
       
   142 void CAMMSModule::AddPlayerNoStateCheckL(CMMAPlayer* aPlayer)
       
   143 {
       
   144     iPlayers.AppendL(aPlayer);
       
   145 
       
   146     // If adding players leaves CAMMSModule::CleanupAddPlayer method will be
       
   147     // called and player removed from module and from groups.
       
   148     CleanupStack::PushL(TCleanupItem(&CAMMSModule::CleanupAddPlayer, this));
       
   149 
       
   150     // Inform all control groups
       
   151     TInt count = iControlGroups->Count();
       
   152     for (TInt i = 0; i < count; i++)
       
   153     {
       
   154         iControlGroups->At(i)->PlayerAddedL(aPlayer);
       
   155     }
       
   156 
       
   157     CleanupStack::Pop(); // CSI: 12 Parameter TCleanupItem cannot be used as a parameter in Pop #
       
   158 }
       
   159 
       
   160 // -----------------------------------------------------------------------------
       
   161 // CAMMSModule::RemovePlayerNoStateCheck
       
   162 // Removes player without checking its state
       
   163 // (other items were commented in a header).
       
   164 // -----------------------------------------------------------------------------
       
   165 //
       
   166 TInt CAMMSModule::RemovePlayerNoStateCheck(CMMAPlayer* aPlayer)
       
   167 {
       
   168     TInt index = iPlayers.Find(aPlayer);
       
   169     if (index != KErrNotFound)
       
   170     {
       
   171         // player was in the array, remove it
       
   172         iPlayers.Remove(index);
       
   173 
       
   174         // inform all groups
       
   175         TInt count = iControlGroups->Count();
       
   176         for (TInt i = 0; i < count; i++)
       
   177         {
       
   178             iControlGroups->At(i)->PlayerRemoved(aPlayer);
       
   179         }
       
   180     }
       
   181 
       
   182     // Find returned KErrNotFound if player was not in the module
       
   183     return index;
       
   184 }
       
   185 
       
   186 // -----------------------------------------------------------------------------
       
   187 // CAMMSModule::HasPlayer
       
   188 // Checks whether the given player is in this module.
       
   189 // (other items were commented in a header).
       
   190 // -----------------------------------------------------------------------------
       
   191 //
       
   192 TBool CAMMSModule::HasPlayer(CMMAPlayer* aPlayer)
       
   193 {
       
   194     LOG1( EJavaAMMS, EInfo, "AMMS:CAMMSModule::HasPlayer %d +", (TInt)aPlayer);
       
   195 
       
   196     TInt index = iPlayers.Find(aPlayer);
       
   197 
       
   198     LOG1( EJavaAMMS, EInfo, "AMMS:CAMMSModule::HasPlayer index=%d -", index);
       
   199 
       
   200     return (index != KErrNotFound);
       
   201 }
       
   202 
       
   203 
       
   204 #ifdef __WINS__
       
   205 
       
   206 // -----------------------------------------------------------------------------
       
   207 // CAMMSModule::PlayerCount
       
   208 // Returns the count of players whose state is between the given limits.
       
   209 // (other items were commented in a header).
       
   210 // -----------------------------------------------------------------------------
       
   211 //
       
   212 TInt CAMMSModule::PlayerCount(TInt aMinState, TInt aMaxState)
       
   213 {
       
   214     LOG2( EJavaAMMS, EInfo, "AMMS:CAMMSModule::PlayerCount %d %d +", aMinState, aMaxState);
       
   215 
       
   216     TInt result = 0;
       
   217 
       
   218     TInt playerCount = iPlayers.Count();
       
   219 
       
   220     LOG1( EJavaAMMS, EInfo, "AMMS:CAMMSModule::HasPlayer, players=%d", playerCount);
       
   221 
       
   222     for (TInt i = 0; i < playerCount; i++)
       
   223     {
       
   224         TInt playerState = iPlayers[ i ]->State();
       
   225 
       
   226         LOG2( EJavaAMMS, EInfo, "AMMS:CAMMSModule::HasPlayer %d, state=%d", i,
       
   227                    playerState);
       
   228 
       
   229         if ((playerState >= aMinState) &&
       
   230                 (playerState <= aMaxState))
       
   231         {
       
   232             result++;
       
   233         }
       
   234     }
       
   235 
       
   236     LOG1( EJavaAMMS, EInfo, "AMMS:CAMMSModule::PlayerCount %d -", result);
       
   237 
       
   238     return result;
       
   239 }
       
   240 
       
   241 #endif  // __WINS__
       
   242 
       
   243 // -----------------------------------------------------------------------------
       
   244 // CAMMSModule::CheckPlayerState
       
   245 // Checks all players state.
       
   246 // (other items were commented in a header).
       
   247 // -----------------------------------------------------------------------------
       
   248 //
       
   249 TInt CAMMSModule::CheckAllPlayersState()
       
   250 {
       
   251     TInt error = KErrNone;
       
   252 
       
   253     TInt playerCount = iPlayers.Count();
       
   254 
       
   255     // Loop until all players are checked or one of the players is in
       
   256     // PREFETCHED or STARTED state
       
   257     for (TInt i = 0; (i < playerCount) &&
       
   258             (error == KErrNone); i++)
       
   259     {
       
   260         error = CheckPlayerState(iPlayers[ i ]);
       
   261     }
       
   262     return error;
       
   263 }
       
   264 
       
   265 // -----------------------------------------------------------------------------
       
   266 // CAMMSModule::CheckPlayerState
       
   267 // Checks player state.
       
   268 // (other items were commented in a header).
       
   269 // -----------------------------------------------------------------------------
       
   270 //
       
   271 TInt CAMMSModule::CheckPlayerState(CMMAPlayer* aPlayer)
       
   272 {
       
   273     // Player may not be in PREFETCHED or STARTED state
       
   274     TInt retVal = KErrNone;
       
   275     TInt playerState = aPlayer->State();
       
   276     if (playerState == CMMAPlayer::EStarted ||
       
   277             playerState == CMMAPlayer::EPrefetched)
       
   278     {
       
   279         retVal = KErrNotReady;
       
   280     }
       
   281     return retVal;
       
   282 }
       
   283 
       
   284 // -----------------------------------------------------------------------------
       
   285 // CAMMSModule::CleanupAddPlayer
       
   286 // Static function to be used with TCleanupItem in AddPlayerNoStateCheckL
       
   287 // method. This method removes last added player from module and control
       
   288 // groups.
       
   289 // (other items were commented in a header).
       
   290 // -----------------------------------------------------------------------------
       
   291 //
       
   292 void CAMMSModule::CleanupAddPlayer(TAny* aModule)
       
   293 {
       
   294     CAMMSModule* module = static_cast< CAMMSModule* >(aModule);
       
   295 
       
   296     // This method is called from AddPlayerNoStateCheckL and there is always
       
   297     // at least one player.
       
   298     __ASSERT_DEBUG(module->iPlayers.Count() > 0, User::Invariant());
       
   299 
       
   300     // Remove last added player from module and from control groups
       
   301     module->RemovePlayerNoStateCheck(
       
   302         module->iPlayers[ module->iPlayers.Count() - 1 ]);
       
   303 }
       
   304 
       
   305 // -----------------------------------------------------------------------------
       
   306 // CAMMSModule::Find
       
   307 // Find control group with specified class name.
       
   308 // (other items were commented in a header).
       
   309 // -----------------------------------------------------------------------------
       
   310 //
       
   311 MAMMSControlGroup* CAMMSModule::Find(const TDesC& aClassName)
       
   312 {
       
   313     MAMMSControlGroup* group = NULL;
       
   314     TInt groupIndex = 0;
       
   315     TInt groupCount = iControlGroups->Count();
       
   316 
       
   317     // Loop until group is found or all group are checked
       
   318     while (!group && (groupIndex < groupCount))
       
   319     {
       
   320         MAMMSControlGroup* tmpGroup = iControlGroups->At(groupIndex);
       
   321 
       
   322         if (tmpGroup->ClassName() == aClassName)
       
   323         {
       
   324             // found the group, set return value which will stop while loop
       
   325             group = tmpGroup;
       
   326         }
       
   327 
       
   328         groupIndex++;
       
   329     }
       
   330 
       
   331     return group;
       
   332 }
       
   333 
       
   334 // -----------------------------------------------------------------------------
       
   335 // CAMMSModule::At
       
   336 // Index must be non-negative and less than the number of objects currently
       
   337 // within the array otherwise the functions raise an E32USER-CBase 21 panic.
       
   338 // (other items were commented in a header).
       
   339 // -----------------------------------------------------------------------------
       
   340 //
       
   341 MAMMSControlGroup* CAMMSModule::At(TInt aIndex)
       
   342 {
       
   343     return iControlGroups->At(aIndex);    // CSI: 1 Array range panic allowed according to function description #
       
   344 }
       
   345 
       
   346 // -----------------------------------------------------------------------------
       
   347 // CAMMSModule::Count
       
   348 // return iControlGroup count.
       
   349 // (other items were commented in a header).
       
   350 // -----------------------------------------------------------------------------
       
   351 //
       
   352 TInt CAMMSModule::Count()
       
   353 {
       
   354     return iControlGroups->Count();
       
   355 }
       
   356 
       
   357 // -----------------------------------------------------------------------------
       
   358 // CAMMSModule::ConstructL
       
   359 // Symbian 2nd phase constructor can leave.
       
   360 // -----------------------------------------------------------------------------
       
   361 void CAMMSModule::ConstructL()
       
   362 {
       
   363     // Create array with default granularity, all derived classes must call
       
   364     // this method.
       
   365     iControlGroups = new(ELeave)CArrayPtrSeg< CAMMSControlGroup >(
       
   366         KAMMSModuleDefaultGranularity);
       
   367 
       
   368 }
       
   369 
       
   370 // -----------------------------------------------------------------------------
       
   371 // CAMMSModule::CAMMSModule
       
   372 // C++ default constructor can NOT contain any code, that
       
   373 // might leave.
       
   374 // -----------------------------------------------------------------------------
       
   375 CAMMSModule::CAMMSModule()
       
   376 {
       
   377 }
       
   378 
       
   379 //  End of File