phonebookui/Phonebook2/CommonUI/src/CPbk2ViewGraph.cpp
changeset 0 e686773b3f54
equal deleted inserted replaced
-1:000000000000 0:e686773b3f54
       
     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:  Phonebook 2 view nodes and graphs.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include <CPbk2ViewGraph.h>
       
    21 
       
    22 // System includes
       
    23 #include <barsc.h>      // RResourceFile
       
    24 #include <barsread.h>   // TResourceReader
       
    25 
       
    26 /// Unnamed namespace for local definitions
       
    27 namespace {
       
    28 
       
    29 /**
       
    30  * Comparison operator for TPbk2ViewTransition objects.
       
    31  *
       
    32  * @param aLeft     Left hand side to compare.
       
    33  * @param aRight    Right hand side to compare.
       
    34  * @return  ETrue if the objects are equal, EFalse otherwise.
       
    35  */
       
    36 inline TBool operator==
       
    37         ( const TPbk2ViewTransition& aLeft,
       
    38           const TPbk2ViewTransition& aRight )
       
    39     {
       
    40     return ( aLeft.iEvent == aRight.iEvent && aLeft.iNode == aRight.iNode );
       
    41     }
       
    42 
       
    43 /**
       
    44  * Compares tab group orderings.
       
    45  *
       
    46  * @param aLhs  Left hand side (LHS).
       
    47  * @param aRhs  Right hand side (RHS).
       
    48  * @return  Positive integer if LHS is greater than RHS.
       
    49  *          Zero if LHS is equal to RHS.
       
    50  *          Negative integer if RHS is greater than LHS.
       
    51  */
       
    52 TInt TabGroupOrdering
       
    53         ( const CPbk2ViewNode& aLhs, const CPbk2ViewNode& aRhs )
       
    54     {
       
    55     return ( aLhs.TabGroupOrdering() - aRhs.TabGroupOrdering() );
       
    56     }
       
    57 
       
    58 #ifdef _DEBUG
       
    59 
       
    60 enum TPaniCode
       
    61     {
       
    62     EPanic_NodeAlreadyExists = 1,
       
    63     EPanic_FindViewsInTabGroupL_OOB
       
    64     };
       
    65 
       
    66 void Panic(TPaniCode aReason)
       
    67     {
       
    68     _LIT( KPanicText, "CPbk2ViewGraph" );
       
    69     User::Panic( KPanicText, aReason );
       
    70     }
       
    71 
       
    72 #endif // _DEBUG
       
    73 
       
    74 } /// namespace
       
    75 
       
    76 
       
    77 
       
    78 // --------------------------------------------------------------------------
       
    79 // CPbk2ViewNode::NewL
       
    80 // --------------------------------------------------------------------------
       
    81 //
       
    82 EXPORT_C CPbk2ViewNode* CPbk2ViewNode::NewL(
       
    83         TUid aViewId, 
       
    84         TUid aDefaultPrevViewId, 
       
    85         TBool aExitNode,
       
    86         TPbk2TabGroupId aTabGroupId,
       
    87         TPbk2TabGroupOrdering aTabGroupOrdering,
       
    88         TInt aTabResourceId,
       
    89         TResourceReader& aTransitionReader)
       
    90     {
       
    91     CPbk2ViewNode* self = new(ELeave) CPbk2ViewNode(aViewId, 
       
    92         aDefaultPrevViewId, aExitNode, aTabGroupId, aTabGroupOrdering,
       
    93         aTabResourceId);
       
    94     CleanupStack::PushL(self);
       
    95     self->ReadTransitionsL(aTransitionReader);
       
    96     CleanupStack::Pop(self);
       
    97     return self;
       
    98     }
       
    99 
       
   100 // --------------------------------------------------------------------------
       
   101 // CPbk2ViewNode::CPbk2ViewNode
       
   102 // --------------------------------------------------------------------------
       
   103 //
       
   104 CPbk2ViewNode::CPbk2ViewNode() :
       
   105     iTransitions( 1 )  // Reallocation granularity of 1
       
   106     {
       
   107     }
       
   108 
       
   109 // --------------------------------------------------------------------------
       
   110 // CPbk2ViewNode::CPbk2ViewNode
       
   111 // --------------------------------------------------------------------------
       
   112 //
       
   113 CPbk2ViewNode::CPbk2ViewNode(
       
   114         TUid aViewId, 
       
   115         TUid aDefaultPrevViewId, 
       
   116         TBool aExitNode,
       
   117         TPbk2TabGroupId aTabGroupId,
       
   118         TPbk2TabGroupOrdering aTabGroupOrdering,
       
   119         TInt aTabResourceId) :
       
   120     iViewId ( aViewId ), 
       
   121     iDefaultPrevViewId ( aDefaultPrevViewId ), 
       
   122     iExitNode ( aExitNode ),
       
   123     iTransitions( 1 ),  // Reallocation granularity of 1    
       
   124     iTabGroupId ( aTabGroupId ),
       
   125     iTabGroupOrdering ( aTabGroupOrdering ),
       
   126     iTabResourceId ( aTabResourceId )
       
   127     {
       
   128     }
       
   129 
       
   130 // --------------------------------------------------------------------------
       
   131 // CPbk2ViewNode::~CPbk2ViewNode
       
   132 // --------------------------------------------------------------------------
       
   133 //
       
   134 CPbk2ViewNode::~CPbk2ViewNode()
       
   135     {
       
   136     }
       
   137 
       
   138 // --------------------------------------------------------------------------
       
   139 // CPbk2ViewNode::FindTransition
       
   140 // --------------------------------------------------------------------------
       
   141 //
       
   142 EXPORT_C TBool CPbk2ViewNode::FindTransition
       
   143         ( const TPbk2ViewTransition& aTransition ) const
       
   144     {
       
   145     for (TInt i=0; i < iTransitions.Count(); ++i)
       
   146         {
       
   147         if (iTransitions[i] == aTransition)
       
   148             {
       
   149             return ETrue;
       
   150             }
       
   151         }
       
   152     return EFalse;
       
   153     }
       
   154 
       
   155 // --------------------------------------------------------------------------
       
   156 // CPbk2ViewNode::FindTransition
       
   157 // --------------------------------------------------------------------------
       
   158 //
       
   159 TPbk2ViewTransition* CPbk2ViewNode::FindTransition
       
   160         ( TPbk2ViewTransitionEvent aEvent )
       
   161     {
       
   162     TPbk2ViewTransition* transition = NULL;
       
   163 
       
   164     for ( TInt i=0; i < iTransitions.Count(); ++i )
       
   165         {
       
   166         if ( iTransitions[i].iEvent == aEvent )
       
   167             {
       
   168             transition = &iTransitions[i];
       
   169             break;
       
   170             }
       
   171         }
       
   172     return transition;
       
   173     }
       
   174 
       
   175 // --------------------------------------------------------------------------
       
   176 // CPbk2ViewNode::ReadTransitionsL
       
   177 // --------------------------------------------------------------------------
       
   178 //
       
   179 void CPbk2ViewNode::ReadTransitionsL( TResourceReader& aResReader )
       
   180     {
       
   181     // read view transitions
       
   182     TInt transCount = aResReader.ReadInt16(); // transitions[]
       
   183     iTransitions.SetReserveL(transCount);
       
   184     while (transCount-- > 0)
       
   185         {
       
   186         // read PBK2_VIEW_TRANSITION resource struct
       
   187         aResReader.ReadInt8();  // read version number
       
   188         TPbk2ViewTransition trans;
       
   189         trans.iEvent = TPbk2ViewTransitionEvent(aResReader.ReadInt32());
       
   190         trans.iTargetViewId.iUid = aResReader.ReadInt32();
       
   191         trans.iNode = NULL;
       
   192         iTransitions.AppendL(trans);
       
   193         }
       
   194     }
       
   195 
       
   196 // --------------------------------------------------------------------------
       
   197 // CPbk2ViewGraph::~CPbk2ViewGraph
       
   198 // --------------------------------------------------------------------------
       
   199 //
       
   200 CPbk2ViewGraph::~CPbk2ViewGraph()
       
   201     {
       
   202     if (iNodes)
       
   203         {
       
   204         iNodes->ResetAndDestroy();
       
   205         }
       
   206     delete iNodes;
       
   207     }
       
   208 
       
   209 // --------------------------------------------------------------------------
       
   210 // CPbk2ViewGraph::NewL
       
   211 // --------------------------------------------------------------------------
       
   212 //
       
   213 EXPORT_C CPbk2ViewGraph* CPbk2ViewGraph::NewL( TResourceReader& aResReader )
       
   214     {
       
   215     CPbk2ViewGraph* self = new(ELeave) CPbk2ViewGraph;
       
   216     CleanupStack::PushL(self);
       
   217     self->ConstructL(aResReader);
       
   218     CleanupStack::Pop(self);  //self
       
   219     return self;
       
   220     }
       
   221 
       
   222 // --------------------------------------------------------------------------
       
   223 // CPbk2ViewGraph::ConstructL
       
   224 // --------------------------------------------------------------------------
       
   225 //
       
   226 void CPbk2ViewGraph::ConstructL( TResourceReader& aResReader )
       
   227     {
       
   228     iNodes = new ( ELeave ) CPbk2ViewNodeArray( 1 );
       
   229     AppendNodesFromResourceL( aResReader );
       
   230     // unravel the view id's to pointers to view graph nodes
       
   231     LinkNodesWithTransitions();
       
   232     }
       
   233 
       
   234 // --------------------------------------------------------------------------
       
   235 // CPbk2ViewGraph::FindNodeWithViewId
       
   236 // --------------------------------------------------------------------------
       
   237 //
       
   238 EXPORT_C CPbk2ViewNode* CPbk2ViewGraph::FindNodeWithViewId
       
   239         ( TUid aViewId ) const
       
   240     {
       
   241     if (aViewId != KNullUid)
       
   242         {
       
   243         TInt count = iNodes->Count();
       
   244         while (count-- > 0)
       
   245             {
       
   246             CPbk2ViewNode* node = iNodes->At(count);
       
   247             if (node->iViewId == aViewId)
       
   248                 {
       
   249                 return node;
       
   250                 }
       
   251             }
       
   252         }
       
   253     return NULL;
       
   254     }
       
   255 
       
   256 // --------------------------------------------------------------------------
       
   257 // CPbk2ViewGraph::ModifyViewGraphL
       
   258 // --------------------------------------------------------------------------
       
   259 //
       
   260 EXPORT_C void CPbk2ViewGraph::ModifyViewGraphL( 
       
   261         TResourceReader& aResReader )
       
   262     {
       
   263     AppendNodesFromResourceL( aResReader );
       
   264     // unravel the view id's to pointers to view graph nodes
       
   265     LinkNodesWithTransitions();
       
   266     }
       
   267 
       
   268 // --------------------------------------------------------------------------
       
   269 // CPbk2ViewGraph::AddViewNode
       
   270 // --------------------------------------------------------------------------
       
   271 //
       
   272 EXPORT_C void CPbk2ViewGraph::AddViewNodeL(
       
   273         CPbk2ViewNode* aNode)
       
   274     {
       
   275     __ASSERT_DEBUG(!FindNodeWithViewId(aNode->iViewId),
       
   276                    Panic(EPanic_NodeAlreadyExists));
       
   277 
       
   278     iNodes->AppendL(aNode);
       
   279     // unravel the view id's to pointers to view graph nodes
       
   280     LinkNodesWithTransitions();
       
   281     }
       
   282 
       
   283 // --------------------------------------------------------------------------
       
   284 // CPbk2ViewGraph::FindViewsInTabGroupL
       
   285 // returns the nodes in the tab group in the tab group sort order
       
   286 // --------------------------------------------------------------------------
       
   287 //
       
   288 EXPORT_C RPointerArray<CPbk2ViewNode> CPbk2ViewGraph::FindViewsInTabGroupL( 
       
   289         TPbk2TabGroupId aTabGroupId ) const
       
   290     {
       
   291     RPointerArray<CPbk2ViewNode> result;
       
   292     CleanupClosePushL(result);
       
   293 
       
   294     TLinearOrder<CPbk2ViewNode> ordering(TabGroupOrdering);
       
   295     const TInt count = Count();
       
   296     
       
   297     __ASSERT_DEBUG( iNodes->Count() >= count, 
       
   298         Panic( EPanic_FindViewsInTabGroupL_OOB ) );
       
   299     
       
   300     // loop through all the view graph nodes    
       
   301     for (TInt i = 0; i < count; ++i)
       
   302         {
       
   303         // if the tab group id is the one were looking for
       
   304         // insert it into the result array
       
   305         if (iNodes->At(i)->iTabGroupId == aTabGroupId)
       
   306             {
       
   307             result.InsertInOrder(iNodes->At(i), ordering);
       
   308             }
       
   309         }
       
   310     CleanupStack::Pop(); // result
       
   311     return result;
       
   312     }
       
   313 
       
   314 // --------------------------------------------------------------------------
       
   315 // CPbk2ViewGraph::AppendNodesFromResourceL
       
   316 // Appends the nodes from resource to iNodes.
       
   317 // --------------------------------------------------------------------------
       
   318 //
       
   319 void CPbk2ViewGraph::AppendNodesFromResourceL( TResourceReader& aResReader )
       
   320     {
       
   321     // reads an array of PBK2_VIEW_NODE resource structures
       
   322     TInt count = aResReader.ReadInt16();
       
   323     iNodes->SetReserveL(iNodes->Count() + count);
       
   324     while (count-- > 0)
       
   325         {
       
   326         // create view node for each element in resource structure
       
   327         CPbk2ViewNode* node = new(ELeave) CPbk2ViewNode;
       
   328         CleanupStack::PushL(node);
       
   329 
       
   330         // read PBK2_VIEW_NODE resource struct
       
   331         aResReader.ReadInt8();  // read version number
       
   332         node->iViewId.iUid = aResReader.ReadInt32();
       
   333         node->iDefaultPrevViewId.iUid = aResReader.ReadInt32();
       
   334         node->iDefaultPreviousNode = NULL;
       
   335         node->iPreviousNode = NULL;
       
   336         node->iExitNode = aResReader.ReadInt8();
       
   337 
       
   338         // read view transitions
       
   339         node->ReadTransitionsL(aResReader);
       
   340 
       
   341         // read tab group id's and resource id's
       
   342         node->iTabGroupId = TPbk2TabGroupId(aResReader.ReadInt32());
       
   343         node->iTabGroupOrdering = TPbk2TabGroupOrdering( 
       
   344                 aResReader.ReadInt32() );
       
   345         // reads the link id of a PBK2_VIEW_NODE_TAB structure
       
   346         node->iTabResourceId = aResReader.ReadInt32();
       
   347 
       
   348         __ASSERT_DEBUG(!FindNodeWithViewId(node->iViewId),
       
   349                        Panic(EPanic_NodeAlreadyExists));
       
   350         iNodes->AppendL(node);
       
   351         CleanupStack::Pop();
       
   352         }
       
   353     }
       
   354 
       
   355 // --------------------------------------------------------------------------
       
   356 // CPbk2ViewGraph::LinkNodesWithTransitions
       
   357 // --------------------------------------------------------------------------
       
   358 //
       
   359 void CPbk2ViewGraph::LinkNodesWithTransitions()
       
   360     {
       
   361     // loop through all the view graph nodes
       
   362     const TInt count( iNodes->Count() );
       
   363     for (TInt i = 0; i < count; ++i)
       
   364         {
       
   365         CPbk2ViewNode* node = iNodes->At(i);
       
   366         // Find the nodes previous views node from the graph
       
   367         // and set this nodes previous node pointer
       
   368         node->iDefaultPreviousNode = FindNodeWithViewId
       
   369             ( node->iDefaultPrevViewId );
       
   370 
       
   371         // loop through the transitions of this node
       
   372         TInt transCount = node->iTransitions.Count();
       
   373         while (transCount-- > 0)
       
   374             {
       
   375             TPbk2ViewTransition& trans = node->iTransitions.At(transCount);
       
   376             switch (trans.iEvent)
       
   377                 {
       
   378                 // ignore left and right transitions
       
   379                 case EPbk2ViewTransitionLeft:
       
   380                     {
       
   381                     break;
       
   382                     }
       
   383                 case EPbk2ViewTransitionRight:
       
   384                     {
       
   385                     break;
       
   386                     }
       
   387                 default:
       
   388                     {
       
   389                     // find the transitions node pointer and set it to the
       
   390                     // transition node
       
   391                     trans.iNode = FindNodeWithViewId(trans.iTargetViewId);
       
   392                     break;
       
   393                     }
       
   394                 }
       
   395             }
       
   396         }
       
   397     }
       
   398 
       
   399 //  End of File