menucontentsrv/engsrc/menueng.cpp
changeset 0 79c6a41cd166
child 23 ace62b58f4b2
equal deleted inserted replaced
-1:000000000000 0:79c6a41cd166
       
     1 /*
       
     2 * Copyright (c) 2009 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:
       
    15 *
       
    16 */
       
    17 
       
    18 // INCLUDE FILES
       
    19 
       
    20 #include <xcfwtree.h>
       
    21 #include <xcfwengine.h>
       
    22 #include <pathinfo.h>
       
    23 #include <f32file.h>
       
    24 #include <sysutil.h>
       
    25 #include "menueng.h"
       
    26 #include "menuengobjectfactoryproxy.h"
       
    27 #include "menuengobject.h"
       
    28 #include "menuengobserver.h"
       
    29 #include "menuengoperation.h"
       
    30 #include "menuengfilter.h"
       
    31 #include "menuengvisitor.h"
       
    32 #include "menuengitemlister.h"
       
    33 #include "menuengdeletionchecker.h"
       
    34 #include "menuengidmanagerinit.h"
       
    35 #include "menuengidsetter.h"
       
    36 #include "menuengidmanager.h"
       
    37 #include "menuengidcollector.h"
       
    38 #include "menuengflags.h"
       
    39 #include "mcsmenuitem.h" // for flags
       
    40 
       
    41 // CONSTANTS
       
    42 
       
    43 LOCAL_D const TInt KDriveAndColon = 2; //drive letter and colon, e.g. "c:"
       
    44 _LIT( KMenuTypeIdSeed, "menu:id_seed" );
       
    45 _LIT( KMenuAttrIdSeed, "seed" );
       
    46 _LIT( KMenuContentDirName, "content\\" );
       
    47 _LIT( KMenuContentExtension, ".xml" );
       
    48 _LIT( KMenuTempExtension, ".$$$" );
       
    49 
       
    50 // ================= LOCAL FUNCTIONS =======================
       
    51 
       
    52 /**
       
    53 * Insert string into other string, checking vaailable space.
       
    54 * Leaves with KErrOverflow if aSoure cannot be inserted to aTarget.
       
    55 * @param aTarget Target descriptor.
       
    56 * @param aPos Insertion point.
       
    57 * @param aSource Source descriptor.
       
    58 */
       
    59 LOCAL_C void InsertL( TDes& aTarget, TInt aPos, const TDesC& aSource )
       
    60     {
       
    61     if ( aTarget.MaxLength() < aTarget.Length() + aSource.Length() )
       
    62         {
       
    63         User::Leave( KErrOverflow );
       
    64         }
       
    65     aTarget.Insert( aPos, aSource );
       
    66     }
       
    67 
       
    68 /**
       
    69 * Append string to the end of other string, checking available space.
       
    70 * Leaves with KErrOverflow if aSource cannot be appended to aTarget.
       
    71 * @param aTarget Target descriptor.
       
    72 * @param aSource Source descriptor.
       
    73 */
       
    74 LOCAL_C void AppendL( TDes& aTarget, const TDesC& aSource )
       
    75     {
       
    76     if ( aTarget.MaxLength() < aTarget.Length() + aSource.Length() )
       
    77         {
       
    78         User::Leave( KErrOverflow );
       
    79         }
       
    80     aTarget.Append( aSource );
       
    81     }
       
    82 
       
    83 // ================= MEMBER FUNCTIONS =======================
       
    84 
       
    85 // ---------------------------------------------------------
       
    86 // CMenuEng::Object
       
    87 // ---------------------------------------------------------
       
    88 //
       
    89 CMenuEngObject& CMenuEng::Object( MXCFWNode& aNode ) const
       
    90     {
       
    91     return (CMenuEngObject&)(*aNode.Data());
       
    92     }
       
    93 
       
    94 // ---------------------------------------------------------
       
    95 // CMenuEng::~CMenuEng
       
    96 // ---------------------------------------------------------
       
    97 //
       
    98 EXPORT_C CMenuEng::~CMenuEng()
       
    99     {
       
   100     Cancel();
       
   101     CancelAllOperations();
       
   102     iOperations.Close();
       
   103     iNotifyQueue.Close();
       
   104     delete iEngine;
       
   105     delete iTree;
       
   106     delete iObjectFactory;
       
   107     delete iIdManager;
       
   108     iFs.Close();
       
   109     iName.Close();
       
   110     iTempFileName.Close();
       
   111     iRamFileName.Close();
       
   112     delete iActiveWait;
       
   113     }
       
   114 
       
   115 // ---------------------------------------------------------
       
   116 // CMenuEng::NewL
       
   117 // ---------------------------------------------------------
       
   118 //
       
   119 EXPORT_C CMenuEng* CMenuEng::NewL
       
   120 ( const TDesC& aName, MMenuEngObserver& aObserver )
       
   121     {
       
   122     CMenuEng* eng = new (ELeave) CMenuEng( aObserver );
       
   123     CleanupStack::PushL( eng );
       
   124     eng->ConstructL( aName );
       
   125     CleanupStack::Pop( eng );
       
   126     return eng;
       
   127     }
       
   128 
       
   129 // ---------------------------------------------------------
       
   130 // CMenuEng::CMenuEng
       
   131 // ---------------------------------------------------------
       
   132 //
       
   133 CMenuEng::CMenuEng( MMenuEngObserver& aObserver )
       
   134 : CActive( CActive::EPriorityStandard ), iObserver( aObserver )
       
   135     {
       
   136     CActiveScheduler::Add( this );
       
   137     iLegacyFormat = EFalse;
       
   138     }
       
   139 
       
   140 // ---------------------------------------------------------
       
   141 // CMenuEng::ConstructL
       
   142 // ---------------------------------------------------------
       
   143 //
       
   144 void CMenuEng::ConstructL( const TDesC& aName )
       
   145     {
       
   146     ValidateNameL( aName );
       
   147     iName.CreateL( aName );
       
   148     User::LeaveIfError( iFs.Connect() );
       
   149     // Keep temp file and the RAM file name as members -
       
   150     // we use these all the time.
       
   151     TFileName fname;
       
   152     GetFileNameL( fname, ETempFile );
       
   153     iTempFileName.CreateL( fname );
       
   154     GetFileNameL( fname, ERamFile );
       
   155     iRamFileName.CreateL( fname );
       
   156     iObjectFactory = CMenuEngObjectFactoryProxy::NewL( *this );
       
   157     iEngine = CXCFWEngine::NewL( this );
       
   158     iEngine->RegisterObjectFactoryL( iObjectFactory );
       
   159     SelfComplete( KErrNone ); 	// Start processing asynchronously.
       
   160     ActiveWaitForFileLoadL();	//we need to wait until parsing is complete
       
   161     }
       
   162 
       
   163 // ---------------------------------------------------------
       
   164 // CMenuEng::QueueOperationL
       
   165 // ---------------------------------------------------------
       
   166 //
       
   167 EXPORT_C void CMenuEng::QueueOperationL( MMenuEngOperation& aOperation )
       
   168     {
       
   169     __ASSERT_DEBUG( KErrNotFound == iOperations.Find( &aOperation ), \
       
   170                     User::Invariant() );
       
   171     iOperations.AppendL( &aOperation );
       
   172     // If idle, kick back into life.
       
   173     if ( EReady == iState )
       
   174         {
       
   175         iState = EExecuteOp;
       
   176         SelfComplete( KErrNone );
       
   177         }
       
   178     }
       
   179 
       
   180 // ---------------------------------------------------------
       
   181 // CMenuEng::DequeueOperation
       
   182 // ---------------------------------------------------------
       
   183 //
       
   184 EXPORT_C void CMenuEng::DequeueOperation( MMenuEngOperation& aOperation )
       
   185     {
       
   186     if ( iCurrentOperation == &aOperation )
       
   187         {
       
   188         iCurrentOperation = NULL;
       
   189         }
       
   190     TInt i = iOperations.Find( &aOperation );
       
   191     if ( KErrNotFound != i )
       
   192         {
       
   193         iOperations.Remove( i );
       
   194         }
       
   195     }
       
   196 
       
   197 // ---------------------------------------------------------
       
   198 // CMenuEng::TraverseFolderL
       
   199 // ---------------------------------------------------------
       
   200 //
       
   201 EXPORT_C void CMenuEng::TraverseFolderL
       
   202 ( TInt aFolder, MMenuEngVisitor& aVisitor ) const
       
   203     {
       
   204     if ( !iTree )
       
   205         {
       
   206         User::Leave( KErrNotReady );
       
   207         }
       
   208     TraverseNodeL( FolderNodeL( aFolder ), aVisitor );
       
   209     }
       
   210 
       
   211 // ---------------------------------------------------------
       
   212 // CMenuEng::NewObjectL
       
   213 // ---------------------------------------------------------
       
   214 //
       
   215 EXPORT_C CMenuEngObject* CMenuEng::NewObjectL( const TDesC& aType )
       
   216     {
       
   217     if ( !iIdManager )
       
   218         {
       
   219         User::Leave( KErrNotReady );
       
   220         }
       
   221     CMenuEngObject* object = CMenuEngObject::NewL( *this, aType );
       
   222     CleanupStack::PushL( object );
       
   223     TInt id;
       
   224     iIdManager->AllocL( id );
       
   225     object->SetId( id );
       
   226     CleanupStack::Pop( object );
       
   227     return object;
       
   228     }
       
   229 
       
   230 // ---------------------------------------------------------
       
   231 // CMenuEng::RootFolderL
       
   232 // ---------------------------------------------------------
       
   233 //
       
   234 EXPORT_C void CMenuEng::RootFolderL( TInt& aId ) const
       
   235     {
       
   236     if ( !iTree )
       
   237         {
       
   238         User::Leave( KErrNotReady );
       
   239         }
       
   240     aId = iRoot;
       
   241     }
       
   242 
       
   243 // ---------------------------------------------------------
       
   244 // CMenuEng::ParentFolderL
       
   245 // ---------------------------------------------------------
       
   246 //
       
   247 EXPORT_C void CMenuEng::ParentFolderL( TInt aId, TInt& aParentId ) const
       
   248     {
       
   249     if ( !iTree )
       
   250         {
       
   251         User::Leave( KErrNotReady );
       
   252         }
       
   253     TInt parentId = 0;
       
   254     if ( aId != iRoot )
       
   255         {
       
   256         // We may have nodes above the root, but do not allow access to those.
       
   257         // Say 0 to the root's parent.
       
   258         MXCFWNode* parentNode = NodeL( aId ).Parent();
       
   259         if ( parentNode )
       
   260             {
       
   261             parentId = Object( *parentNode ).Id();
       
   262             }
       
   263         }
       
   264     aParentId = parentId;
       
   265     }
       
   266 
       
   267 // ---------------------------------------------------------
       
   268 // CMenuEng::GetItemL
       
   269 // ---------------------------------------------------------
       
   270 //
       
   271 EXPORT_C void CMenuEng::GetItemL( TInt aId, TMenuItem& aItem ) const
       
   272     {
       
   273     if ( !iTree )
       
   274         {
       
   275         User::Leave( KErrNotReady );
       
   276         }
       
   277     const CMenuEngObject& object = ObjectL( aId );
       
   278     aItem.SetId( object.Id() );
       
   279     aItem.SetFlags( object.Flags() );
       
   280     aItem.SetType( object.Type() );
       
   281     TInt parent;
       
   282     ParentFolderL( aId, parent );
       
   283     aItem.SetParent( parent );
       
   284     }
       
   285 
       
   286 // ---------------------------------------------------------
       
   287 // CMenuEng::GetItemsL
       
   288 // ---------------------------------------------------------
       
   289 //
       
   290 EXPORT_C void CMenuEng::GetItemsL(
       
   291         RArray<TMenuItem>& aItemArray,
       
   292         TInt aFolder,
       
   293         const MMenuEngFilter* aFilter,
       
   294         TBool aRecursive ) const
       
   295     {
       
   296     if ( !iTree )
       
   297         {
       
   298         User::Leave( KErrNotReady );
       
   299         }
       
   300     TMenuEngItemLister lister
       
   301         ( *this, aItemArray, aFolder, aFilter, aRecursive );
       
   302     TraverseFolderL( aFolder, lister );
       
   303     }
       
   304 
       
   305 // ---------------------------------------------------------
       
   306 // CMenuEng::ObjectL
       
   307 // ---------------------------------------------------------
       
   308 //
       
   309 EXPORT_C const CMenuEngObject& CMenuEng::ObjectL( TInt aId ) const
       
   310     {
       
   311     if ( !iTree )
       
   312         {
       
   313         User::Leave( KErrNotReady );
       
   314         }
       
   315     return Object( NodeL( aId ) );
       
   316     }
       
   317 
       
   318 // ---------------------------------------------------------
       
   319 // CMenuEng::GetLegacyFormat
       
   320 // ---------------------------------------------------------
       
   321 //
       
   322 EXPORT_C TBool CMenuEng::GetOnceLegacyFormat()
       
   323     {
       
   324     TBool format = iLegacyFormat;
       
   325     iLegacyFormat = EFalse;
       
   326     return format;
       
   327     }
       
   328 
       
   329 // ---------------------------------------------------------
       
   330 // CMenuEng::RemoveL
       
   331 // ---------------------------------------------------------
       
   332 //
       
   333 EXPORT_C void CMenuEng::RemoveL( TInt aId )
       
   334     {
       
   335 	if ( SysUtil::DiskSpaceBelowCriticalLevelL( &iFs, 0, EDriveC) )
       
   336 		{
       
   337 		User::Leave( KErrDiskFull );
       
   338 		}
       
   339     if ( iState != EExecuteOp )
       
   340         {
       
   341         User::Leave( KErrLocked );
       
   342         }
       
   343     // Check if aId exists.
       
   344     MXCFWNode& node = NodeL( aId );
       
   345     // Can't delete the root.
       
   346     if ( aId == iRoot )
       
   347         {
       
   348         User::Leave( KErrAccessDenied );
       
   349         }
       
   350     // Can't delete read-only items, or folders containing any read-only items.
       
   351     TMenuEngDeletionChecker checker;
       
   352     TraverseNodeL( node, checker );
       
   353     // Get the IDs of items to be deleted.
       
   354     RArray<TInt> deletedIds;
       
   355     CleanupClosePushL( deletedIds );
       
   356     TMenuEngIdCollector idCollector( deletedIds );
       
   357     TraverseNodeL( node, idCollector );
       
   358     // Add notification event.
       
   359     MXCFWNode* parent = node.Parent();
       
   360     __ASSERT_DEBUG( parent, User::Invariant() );
       
   361     AppendNotifyL( Object( *parent ).Id(),
       
   362                    RMenuNotifier::EItemsAddedRemoved );
       
   363     // Delete node.
       
   364     iTree->RemoveNodeL( &node );
       
   365     iChanged = ETrue;
       
   366     // Deletion successful, remove the IDs.
       
   367     for ( TInt i = 0; i < deletedIds.Count(); i++ )
       
   368         {
       
   369         iIdManager->Remove( deletedIds[i] );
       
   370         }
       
   371     CleanupStack::PopAndDestroy( &deletedIds );
       
   372     }
       
   373 
       
   374 // ---------------------------------------------------------
       
   375 // CMenuEng::MoveToFolderL
       
   376 // ---------------------------------------------------------
       
   377 //
       
   378 EXPORT_C void CMenuEng::MoveToFolderL
       
   379 ( RArray<TInt>& aItems, TInt aFolder, TInt aMoveBefore )
       
   380     {
       
   381 	if ( SysUtil::DiskSpaceBelowCriticalLevelL( &iFs, 0, EDriveC) )
       
   382 		{
       
   383 		User::Leave( KErrDiskFull );
       
   384 		}
       
   385     if ( iState != EExecuteOp )
       
   386         {
       
   387         User::Leave( KErrLocked );
       
   388         }
       
   389     TInt i;
       
   390     TInt id;
       
   391     MXCFWNode* fromFolder = NULL;
       
   392     // Check that toFolder exists.
       
   393     MXCFWNode& toFolder = FolderNodeL( aFolder );
       
   394     CMenuEngObject& toFolderObj = Object( toFolder );
       
   395     // Check that we can move items into target toFolder.
       
   396     if ( toFolderObj.Flags() & TMenuItem::ELockMoveInto )
       
   397         {
       
   398         User::Leave( KErrAccessDenied );
       
   399         }
       
   400     // Check that all items exist and they are not locked against moving.
       
   401     // Also check that and they are in the same toFolder.
       
   402     if ( aItems.Count() )
       
   403         {
       
   404         fromFolder = NodeL( aItems[0] ).Parent();
       
   405         for ( i = 0; i< aItems.Count(); i++ )
       
   406             {
       
   407             id = aItems[i];
       
   408             if ( id == iRoot )
       
   409                 {
       
   410                 User::Leave( KErrAccessDenied ); // Can't move the root.
       
   411                 }
       
   412             MXCFWNode& node = NodeL( id );
       
   413             if ( Object( node ).Flags() & TMenuItem::ELockItem )
       
   414                 {
       
   415                 User::Leave( KErrAccessDenied ); // lock_item
       
   416                 }
       
   417             if ( node.Parent() != fromFolder )
       
   418                 {
       
   419                 User::Leave( KErrArgument ); // Items from different folders.
       
   420                 }
       
   421             }
       
   422         // Add notification events. Presume that there will be no recursion.
       
   423         // If there is recursion, then we make an unnecessary notify, that is
       
   424         // better than missing one.
       
   425         __ASSERT_DEBUG( fromFolder, User::Invariant() );
       
   426         AppendNotifyL( Object( *fromFolder ).Id(),
       
   427                        RMenuNotifier::EItemsAddedRemoved );
       
   428         AppendNotifyL( toFolderObj.Id(),
       
   429                        RMenuNotifier::EItemsAddedRemoved );
       
   430         // Do move the items.
       
   431         // Recursion check is done by the XML tree (->KErrArgument).
       
   432         MXCFWNode* moveBefore = Child( toFolder, aMoveBefore );
       
   433         for ( i = 0; i < aItems.Count(); i++ )
       
   434             {
       
   435             iTree->MoveNodeL( &NodeL( aItems[i] ), &toFolder, moveBefore );
       
   436             }
       
   437         iChanged = ETrue;
       
   438         }
       
   439     }
       
   440 
       
   441 // ---------------------------------------------------------
       
   442 // CMenuEng::ReorderL
       
   443 // ---------------------------------------------------------
       
   444 //
       
   445 EXPORT_C void CMenuEng::ReorderL( TInt aId, TInt aMoveBefore )
       
   446     {
       
   447 	if ( SysUtil::DiskSpaceBelowCriticalLevelL( &iFs, 0, EDriveC) )
       
   448 		{
       
   449 		User::Leave( KErrDiskFull );
       
   450 		}
       
   451     if ( iState != EExecuteOp )
       
   452         {
       
   453         User::Leave( KErrLocked );
       
   454         }
       
   455     // Can't reorder the root.
       
   456     if ( aId == iRoot )
       
   457         {
       
   458         User::Leave( KErrArgument );
       
   459         }
       
   460     // Check if aId exists.
       
   461     MXCFWNode& node = NodeL( aId );
       
   462     // Check if item can be reordered.
       
   463     if ( Object( node ).Flags() & TMenuItem::ELockItem )
       
   464         {
       
   465         User::Leave( KErrAccessDenied ); // lock_item
       
   466         }
       
   467     MXCFWNode* parent = node.Parent();
       
   468     __ASSERT_DEBUG( parent, User::Invariant() );
       
   469     if ( parent )
       
   470         {
       
   471         // Add notification event.
       
   472         AppendNotifyL( Object( *parent ).Id(),
       
   473                        RMenuNotifier::EItemsReordered );
       
   474         // Move it.
       
   475         MXCFWNode* moveBefore = Child( *parent, aMoveBefore );
       
   476         iTree->MoveNodeL( &node, parent, moveBefore );
       
   477         iChanged = ETrue;
       
   478         }
       
   479     }
       
   480 
       
   481 // ---------------------------------------------------------
       
   482 // CMenuEng::AddL
       
   483 // ---------------------------------------------------------
       
   484 //
       
   485 EXPORT_C void CMenuEng::AddL
       
   486 ( CMenuEngObject& aObject, TInt aFolder, TInt aInsertBefore )
       
   487     {
       
   488 	if ( SysUtil::DiskSpaceBelowCriticalLevelL( &iFs, 0, EDriveC) )
       
   489 		{
       
   490 		User::Leave( KErrDiskFull );
       
   491 		}
       
   492     if ( &aObject.Engine() != this )
       
   493         {
       
   494         // ID integrity requires that objects are bound to engines.
       
   495         User::Leave( KErrArgument );
       
   496         }
       
   497     if ( iState != EExecuteOp )
       
   498         {
       
   499         User::Leave( KErrLocked );
       
   500         }
       
   501     if ( KMenuTypeData() == aObject.Type() )
       
   502         {
       
   503         // Internal type, disallow.
       
   504         User::Leave( KErrArgument );
       
   505         }
       
   506     // Check if aFolder exists.
       
   507     MXCFWNode& folder = FolderNodeL( aFolder );
       
   508     // Check if folder is locked.
       
   509     if ( Object( folder ).Flags() & TMenuItem::ELockMoveInto )
       
   510         {
       
   511         User::Leave( KErrAccessDenied ); // lock_moveinto
       
   512         }
       
   513     // Add notification event.
       
   514     AppendNotifyL( aFolder, RMenuNotifier::EItemsAddedRemoved );
       
   515     // Add the node.
       
   516     MXCFWNode* insertBefore = Child( folder, aInsertBefore );
       
   517     if ( insertBefore )
       
   518         {
       
   519         iTree->AddNodeL( &aObject, &folder, insertBefore );
       
   520         // No leaving after this point - ownership of aObject taken by the
       
   521         // tree. (Well, almost -- CXCFWTree is buggy, but hopefully it will
       
   522         // be fixed.)
       
   523         }
       
   524     else
       
   525         {
       
   526         // CXCFWTree API design flaw:
       
   527         // AddNodeL( &aObject, &folder, NULL ) leaves with KErrArgument!
       
   528         iTree->AddNodeL( &aObject, &folder );
       
   529         // No leaving after this point - ownership of aObject taken by the
       
   530         // tree. (Well, almost -- CXCFWTree is buggy, but hopefully it will
       
   531         // be fixed.)
       
   532         }
       
   533     iChanged = ETrue;
       
   534     }
       
   535 
       
   536 // ---------------------------------------------------------
       
   537 // CMenuEng::ModifiableObjectL
       
   538 // ---------------------------------------------------------
       
   539 //
       
   540 EXPORT_C CMenuEngObject& CMenuEng::ModifiableObjectL( TInt aId , TInt aEvent)
       
   541     {
       
   542     if ( !iTree )
       
   543         {
       
   544         User::Leave( KErrNotReady );
       
   545         }
       
   546     if ( iState != EExecuteOp )
       
   547         {
       
   548         User::Leave( KErrLocked );
       
   549         }
       
   550     MXCFWNode& node = NodeL( aId );
       
   551     CMenuEngObject& object = Object( node );
       
   552     // Add notification event.
       
   553     MXCFWNode* parent = node.Parent();
       
   554     __ASSERT_DEBUG( parent, User::Invariant() );
       
   555     if( aEvent != RMenuNotifier::EItemsNone )
       
   556     	{
       
   557     	AppendNotifyL( Object( *parent ).Id(), aEvent );
       
   558     	}
       
   559     iChanged = ETrue; // Might not be true, but how could we know?
       
   560     // Note1: We must presume that this object will actually be changed
       
   561     // because we haven't any information what is being done to it!
       
   562     // If the object had a back-pointer to the engine, this could
       
   563     // be solved, but that would be a BAD THING to do.
       
   564     // Note2: other possible solution is to drop this method and invent
       
   565     // something else to allow item data update.
       
   566     return object;
       
   567     }
       
   568 
       
   569 
       
   570 	
       
   571 // ---------------------------------------------------------
       
   572 // CMenuEng::AppendNotifyL
       
   573 // ---------------------------------------------------------
       
   574 //
       
   575 EXPORT_C void CMenuEng::AppendNotifyL( TInt aFolder, TInt aEvents )
       
   576     {
       
   577     for ( TInt i = 0; i < iNotifyQueue.Count(); i++ )
       
   578         {
       
   579         if ( iNotifyQueue[i].iFolder == aFolder )
       
   580             {
       
   581             iNotifyQueue[i].iEvents |= aEvents;
       
   582             return;
       
   583             }
       
   584         }
       
   585     iNotifyQueue.AppendL( TMenuEngNotify( aFolder, aEvents ) );
       
   586     }
       
   587 
       
   588 // ---------------------------------------------------------
       
   589 // CMenuEng::NodeL
       
   590 // ---------------------------------------------------------
       
   591 //
       
   592 EXPORT_C TBool CMenuEng::Exist( TInt aId ) const
       
   593     {
       
   594     MXCFWNode* node = NULL;
       
   595     const RNodeArray& nodes = iTree->Nodes();
       
   596     for ( TInt i = 0; i < nodes.Count(); i++ )
       
   597         {
       
   598         node = nodes[i];
       
   599         if ( aId == Object( *node ).Id() )
       
   600             {
       
   601             return ETrue;
       
   602             }
       
   603         }
       
   604     return EFalse;
       
   605     }
       
   606 // ---------------------------------------------------------
       
   607 // CMenuEng::RunL
       
   608 // ---------------------------------------------------------
       
   609 //
       
   610 void CMenuEng::RunL()
       
   611     {
       
   612     switch( iState )
       
   613         {
       
   614         case ENull:
       
   615             {
       
   616             // Self-completion in ConstructL(). Load RAM tree.
       
   617             __ASSERT_DEBUG( !iStatus.Int(), User::Invariant() );
       
   618             iState = ELoadRamFile;
       
   619             LoadRamFileL();
       
   620             break;
       
   621             }
       
   622 
       
   623         case ELoadRamFile:
       
   624             {
       
   625             if ( iStatus.Int() )
       
   626                 {
       
   627                 // Error loading RAM tree -> Try ROM tree.
       
   628                 iObserver.EngineTreeReloaded();
       
   629                 iState = ELoadRomFile;
       
   630                 LoadRomFileL();
       
   631                 }
       
   632             else
       
   633                 {
       
   634                 // Tree loaded OK. Start processing operations.
       
   635                 iState = EExecuteOp;
       
   636                 ExecuteOperationL();
       
   637                 }
       
   638             break;
       
   639             }
       
   640 
       
   641         case ELoadRomFile:
       
   642             {
       
   643             // Error loading ROM tree is fatal. Nothing we can do.
       
   644             User::LeaveIfError( iStatus.Int() );
       
   645             iState = ESaveFile;
       
   646             SaveTempFileL();
       
   647             break;
       
   648             }
       
   649 
       
   650         case ESaveFile:
       
   651             {
       
   652             // Error saving tree is fatal. Nothing we can do.
       
   653             User::LeaveIfError( iStatus.Int() );
       
   654             // Tree saved to temp file OK. Replace content file with temp file.
       
   655             ReplaceRamFileL();
       
   656             // Saving completed succesfully.
       
   657             iChanged = EFalse;
       
   658             // If there is a current operation, this is the final result.
       
   659             CompleteCurrentOperation( iStatus.Int() );
       
   660             // Start next operation.
       
   661             iState = EExecuteOp;
       
   662             ExecuteOperationL();
       
   663             break;
       
   664             }
       
   665 
       
   666         case EExecuteOp:
       
   667             {
       
   668             // Start next operation.
       
   669             __ASSERT_DEBUG( !iStatus.Int(), User::Invariant() );
       
   670             ExecuteOperationL();
       
   671             break;
       
   672             }
       
   673 
       
   674         case EReady:
       
   675         case EDead:
       
   676         default:
       
   677             {
       
   678             User::Invariant();
       
   679             }
       
   680         }
       
   681     }
       
   682 
       
   683 // ---------------------------------------------------------
       
   684 // CMenuEng::DoCancel
       
   685 // ---------------------------------------------------------
       
   686 //
       
   687 void CMenuEng::DoCancel()
       
   688     {
       
   689     // We don't have real requests, only self-completion; which is already
       
   690     // completed.
       
   691     }
       
   692 
       
   693 // ---------------------------------------------------------
       
   694 // CMenuEng::RunError
       
   695 // ---------------------------------------------------------
       
   696 //
       
   697 TInt CMenuEng::RunError( TInt aError )
       
   698     {
       
   699     switch( iState )
       
   700         {
       
   701         case ENull:
       
   702             {
       
   703             User::Invariant(); // Self-completion cannot fail.
       
   704             break;
       
   705             }
       
   706 
       
   707         case ELoadRamFile:
       
   708             {
       
   709             // Error loading RAM tree -> keep going (try ROM tree).
       
   710             SelfComplete( aError );
       
   711             break;
       
   712             }
       
   713 
       
   714         case ELoadRomFile:
       
   715             {
       
   716             // Error loading ROM tree is fatal. Nothing we can do.
       
   717             // stop nested active scheduler loop if error in parsing
       
   718             if ( iActiveWait->IsStarted() )
       
   719                 {
       
   720                 iActiveWait->AsyncStop();
       
   721                 }  
       
   722                 
       
   723             iState = EDead;
       
   724             iObserver.EngineError( aError );
       
   725             // Can't delete the tree now, XCFW Engine keeps a pointer to it
       
   726             // and still uses it even after reporting the error. :(
       
   727          
       
   728             break;
       
   729             }
       
   730 
       
   731         case ESaveFile:
       
   732             {
       
   733             // File is too big and cannot be saved, delete it
       
   734             // and try reading from rom
       
   735             if (iStatus.Int() == KErrNoMemory)
       
   736             	{
       
   737             	iState = ELoadRamFile;
       
   738             	SelfComplete( aError );
       
   739             	}
       
   740             else {
       
   741 				// Error saving tree is fatal. Nothing we can do.
       
   742 				// If there is a current operation, this is the final result.
       
   743 				CompleteCurrentOperation( aError );
       
   744 				iState = EDead;
       
   745 				iObserver.EngineError( aError );
       
   746             }
       
   747             // Can't delete the tree now, XCFW Engine keeps a pointer to it
       
   748             // and still uses it even after reporting the error. :(
       
   749             break;
       
   750             }
       
   751 
       
   752         case EExecuteOp:
       
   753             {
       
   754             // Current operation failed.
       
   755             CompleteCurrentOperation( aError );
       
   756             // Continue with next operation.
       
   757             SelfComplete( KErrNone );
       
   758             break;
       
   759             }
       
   760 
       
   761         case EReady:
       
   762         case EDead:
       
   763         default:
       
   764             {
       
   765             User::Invariant();
       
   766             }
       
   767         }
       
   768     return KErrNone;
       
   769     }
       
   770 
       
   771 // ---------------------------------------------------------
       
   772 // CMenuEng::HandleEngineEventL
       
   773 // ---------------------------------------------------------
       
   774 //
       
   775 void CMenuEng::HandleEngineEventL( TXCFWEngineEvent aEvent )
       
   776     {
       
   777     switch ( aEvent )
       
   778         {
       
   779         case EEvtParsingComplete:
       
   780             {
       
   781             __ASSERT_DEBUG( iTree, User::Invariant() );
       
   782 
       
   783             // parsing operation complete
       
   784             // we can stop nested active scheduler loop
       
   785             if ( iActiveWait->IsStarted() )
       
   786                 {
       
   787                 iActiveWait->AsyncStop();
       
   788                 }         
       
   789             
       
   790             TBool legacyFormat = iObjectFactory->IsLegacyFormat();                        
       
   791             // Reset object factory in all cases.
       
   792             iObjectFactory->Reset();
       
   793             // Tree is up (maybe unsaved yet).
       
   794             // Check the structure and get the root folder node.
       
   795             MXCFWNode& rootNode = CheckTreeL( *iTree );
       
   796             // Init ID manager, check and fix ID-s.
       
   797             InitIdManagerL( rootNode );
       
   798             // Now we have ID for all (including the root).
       
   799             iRoot = Object( rootNode ).Id();
       
   800             // This is the earliest point to do an ID sanity check.
       
   801             __ASSERT_DEBUG( DebugSanityCheck( *iTree ), User::Invariant() );
       
   802             // Send a "wildcard" notification so everybody reloads.
       
   803             iObserver.EngineEvents( 0, RMenuNotifier::EItemsAddedRemoved );
       
   804             iLegacyFormat = legacyFormat;
       
   805             SelfComplete( KErrNone ); // Go to RunL().
       
   806             break;
       
   807             }
       
   808 
       
   809         case EEvtSavingComplete:
       
   810             {
       
   811             //iLegacyFormat = EFalse;
       
   812             SelfComplete( KErrNone ); // Go to RunL().
       
   813             break;
       
   814             }
       
   815 
       
   816         case EEvtParsingCanceled:
       
   817         case EEvtSavingCanceled:
       
   818             {
       
   819             // Reset object factory in all cases.
       
   820             iObjectFactory->Reset();
       
   821             SelfComplete( KErrCancel ); // Go to RunL().
       
   822             break;
       
   823             }
       
   824 
       
   825         case EEvtParsingStarted:
       
   826         case EEvtSavingStarted:
       
   827         case EEvtNull:
       
   828         default:
       
   829             {
       
   830             break;
       
   831             }
       
   832         }
       
   833     }
       
   834 
       
   835 // ---------------------------------------------------------
       
   836 // CMenuEng::HandleEngineErrorL
       
   837 // ---------------------------------------------------------
       
   838 //
       
   839 void CMenuEng::HandleEngineErrorL( TInt aErrorCode )
       
   840     {
       
   841     // Reset object factory in all cases.
       
   842     iObjectFactory->Reset();
       
   843 
       
   844     SelfComplete( aErrorCode ); // Go to RunL().
       
   845     }
       
   846 
       
   847 // ---------------------------------------------------------
       
   848 // CMenuEng::CheckTreeL
       
   849 // ---------------------------------------------------------
       
   850 //
       
   851 MXCFWNode& CMenuEng::CheckTreeL( MXCFWTree& aTree ) const
       
   852     {
       
   853     MXCFWNode* rootFolder = NULL;
       
   854     MXCFWNode* treeRoot = aTree.Root();
       
   855     if ( treeRoot )
       
   856         {
       
   857         // The tree has nodes.
       
   858         if ( KMenuTypeData() == Object( *treeRoot ).Type() )
       
   859             {
       
   860             // Root node is "menu:data"
       
   861             RNodeArray nodes;
       
   862             CleanupClosePushL( nodes );
       
   863             aTree.GetNodesOfTypeL( KMenuTypeFolder(), nodes,
       
   864                                     treeRoot, EFalse );
       
   865             if ( 1 == nodes.Count() )
       
   866                 {
       
   867                 // Exactly 1 "menu:folder" in the root.
       
   868                 // This is the root folder.
       
   869                 rootFolder = nodes[0];
       
   870                 }
       
   871             CleanupStack::PopAndDestroy( &nodes );
       
   872             }
       
   873         }
       
   874     if ( !rootFolder )
       
   875         {
       
   876         User::Leave( KErrCorrupt );
       
   877         }
       
   878     return *rootFolder;
       
   879     }
       
   880 
       
   881 // ---------------------------------------------------------
       
   882 // CMenuEng::DebugSanityCheck
       
   883 // ---------------------------------------------------------
       
   884 //
       
   885 TBool CMenuEng::DebugSanityCheck( MXCFWTree& aTree ) const
       
   886     {
       
   887     const RNodeArray& nodes = aTree.Nodes();
       
   888     for ( TInt i = 0; i < nodes.Count(); i++ )
       
   889         {
       
   890         if ( !DebugSanityCheck( *nodes[i] ) )
       
   891             {
       
   892             return EFalse;
       
   893             }
       
   894         }
       
   895     return ETrue;
       
   896     }
       
   897 
       
   898 // ---------------------------------------------------------
       
   899 // CMenuEng::ActiveWaitForLoadFileL
       
   900 // ---------------------------------------------------------
       
   901 //
       
   902 void CMenuEng::ActiveWaitForFileLoadL()
       
   903     {
       
   904     iActiveWait = new( ELeave ) CActiveSchedulerWait();
       
   905     if ( !iActiveWait->IsStarted() )
       
   906         {
       
   907         iActiveWait->Start();
       
   908         }
       
   909     //now we check if file was properly loaded
       
   910     TInt root(0);
       
   911     RootFolderL( root );
       
   912     if( !root && ( iState != EDead ) )
       
   913     	{
       
   914     	//there was problem with Ram file(probably file was empty), lets load Rom file
       
   915     	iState = ELoadRamFile;
       
   916         if ( !iActiveWait->IsStarted() )
       
   917             {
       
   918             iActiveWait->Start();
       
   919             }
       
   920     	}
       
   921     }
       
   922 
       
   923 // ---------------------------------------------------------
       
   924 // CMenuEng::DebugSanityCheck
       
   925 // ---------------------------------------------------------
       
   926 //
       
   927 TBool CMenuEng::DebugSanityCheck( MXCFWNode& aNode ) const
       
   928     {
       
   929     __ASSERT_DEBUG( iRoot, User::Invariant() );
       
   930     // Sanity check, for node location and ID.
       
   931     // - Nodes under the root folder (==items) should have non-0 ID.
       
   932     // - Nodes not under the root folder (==other stuff like id_seed) should
       
   933     //   always have 0 id.
       
   934     // This is crucial for the ID space integrity. XCFW node lookup uses
       
   935     // a flat list of nodes; the only way we can differentiate items from
       
   936     // non-items is the ID.
       
   937     MXCFWNode* node;
       
   938     for ( node = &aNode; node; node = node->Parent() )
       
   939         {
       
   940         if ( Object( *node ).Id() == iRoot )
       
   941             {
       
   942             // The node is, or descendant of, the root folder. Must have an ID.
       
   943             return 0 != Object( aNode ).Id();
       
   944             }
       
   945         }
       
   946     // Node is not under the root folder. Must not have an ID.
       
   947     return 0 == Object( aNode ).Id();
       
   948     }
       
   949 
       
   950 // ---------------------------------------------------------
       
   951 // CMenuEng::NodeL
       
   952 // ---------------------------------------------------------
       
   953 //
       
   954 MXCFWNode& CMenuEng::NodeL( TInt aId ) const
       
   955     {
       
   956     __ASSERT_DEBUG( iTree, User::Invariant() );
       
   957     if ( !aId )
       
   958         {
       
   959         // We can actually have 0 id node (outside the item tree), but
       
   960         // those are not items.
       
   961         User::Leave( KErrNotFound );
       
   962         }
       
   963     MXCFWNode* node = NULL;
       
   964     const RNodeArray& nodes = iTree->Nodes();
       
   965     for ( TInt i = 0; i < nodes.Count(); i++ )
       
   966         {
       
   967         node = nodes[i];
       
   968         if ( aId == Object( *node ).Id() )
       
   969             {
       
   970             __ASSERT_DEBUG( DebugSanityCheck( *node ), User::Invariant() );
       
   971             return *node;
       
   972             }
       
   973         }
       
   974     User::Leave( KErrNotFound );
       
   975     /* NOTREACHED */
       
   976     return *node;
       
   977     }
       
   978 
       
   979 // ---------------------------------------------------------
       
   980 // CMenuEng::FolderNodeL
       
   981 // ---------------------------------------------------------
       
   982 //
       
   983 MXCFWNode& CMenuEng::FolderNodeL( TInt aId ) const
       
   984     {
       
   985     __ASSERT_DEBUG( iTree, User::Invariant() );
       
   986     MXCFWNode& node = NodeL( aId );
       
   987     if ( KMenuTypeFolder() == Object( node ).Type() )
       
   988         {
       
   989         return node;
       
   990         }
       
   991     User::Leave( KErrNotFound );
       
   992     /* NOTREACHED */
       
   993     return node;
       
   994     }
       
   995 
       
   996 // ---------------------------------------------------------
       
   997 // CMenuEng::Child
       
   998 // ---------------------------------------------------------
       
   999 //
       
  1000 
       
  1001 MXCFWNode* CMenuEng::Child( MXCFWNode& aParent, TInt aId )
       
  1002     {
       
  1003     MXCFWNode* node;
       
  1004     for ( node = aParent.FirstChild(); node; node = node->NextSibling() )
       
  1005         {
       
  1006         if ( Object( *node ).Id() == aId )
       
  1007             {
       
  1008             break;
       
  1009             }
       
  1010         }
       
  1011     return node;
       
  1012     }
       
  1013 
       
  1014 // ---------------------------------------------------------
       
  1015 // CMenuEng::CancelAllOperations
       
  1016 // ---------------------------------------------------------
       
  1017 //
       
  1018 void CMenuEng::CancelAllOperations()
       
  1019     {
       
  1020     // For safety, we remove pointers before completion.
       
  1021     // (OperationCompleted might want to remove operation too, avoid
       
  1022     // conflict.)
       
  1023     MMenuEngOperation* operation;
       
  1024     if ( iCurrentOperation )
       
  1025         {
       
  1026         operation = iCurrentOperation;
       
  1027         iCurrentOperation = NULL;
       
  1028         operation->CompletedMenuEngOperation( KErrCancel );
       
  1029         }
       
  1030     while ( iOperations.Count() )
       
  1031         {
       
  1032         operation = iOperations[0];
       
  1033         iOperations.Remove( 0 );
       
  1034         operation->CompletedMenuEngOperation( KErrCancel );
       
  1035         }
       
  1036     }
       
  1037 
       
  1038 // ---------------------------------------------------------
       
  1039 // CMenuEng::ValidateNameL
       
  1040 // ---------------------------------------------------------
       
  1041 //
       
  1042 void CMenuEng::ValidateNameL( const TDesC& aName )
       
  1043     {
       
  1044     // We only accept alphanumeric characters and underscore as content
       
  1045     // file name. This is stricter than the (current) file system
       
  1046     // restriction, for future-proofing reasons.
       
  1047     if ( !aName.Length() )
       
  1048         {
       
  1049         User::Leave( KErrArgument );
       
  1050         }
       
  1051     for ( TInt i = 0; i < aName.Length(); i++ )
       
  1052         {
       
  1053         TChar c =  aName[i];
       
  1054         if ( !c.IsAlphaDigit() && '_' != c )
       
  1055             {
       
  1056             User::Leave( KErrArgument );
       
  1057             }
       
  1058         }
       
  1059     }
       
  1060 
       
  1061 // ---------------------------------------------------------
       
  1062 // CMenuEng::GetFileNameL
       
  1063 // ---------------------------------------------------------
       
  1064 //
       
  1065 void CMenuEng::GetFileNameL( TFileName& aFname, TFile aSelector )
       
  1066     {
       
  1067     User::LeaveIfError( iFs.PrivatePath( aFname ) );
       
  1068     if ( ERomFile == aSelector )
       
  1069         {
       
  1070         InsertL(
       
  1071             aFname,
       
  1072             0,
       
  1073             PathInfo::RomRootPath().Left( KDriveAndColon ) );
       
  1074         }
       
  1075     else
       
  1076         {
       
  1077         InsertL(
       
  1078             aFname,
       
  1079             0,
       
  1080             PathInfo::PhoneMemoryRootPath().Left( KDriveAndColon ) );
       
  1081         }
       
  1082     AppendL( aFname, KMenuContentDirName );
       
  1083     AppendL( aFname, iName );
       
  1084     AppendL( aFname, ETempFile == aSelector ?
       
  1085         KMenuTempExtension : KMenuContentExtension );
       
  1086     }
       
  1087 
       
  1088 // ---------------------------------------------------------
       
  1089 // CMenuEng::TraverseNodeL
       
  1090 // ---------------------------------------------------------
       
  1091 //
       
  1092 TBool CMenuEng::TraverseNodeL
       
  1093 ( MXCFWNode& aNode, MMenuEngVisitor& aVisitor ) const
       
  1094     {
       
  1095     CMenuEngObject& object = Object( aNode );
       
  1096     if ( KMenuTypeFolder() == object.Type() )
       
  1097         {
       
  1098         if ( aVisitor.VisitEnterL( object ) )
       
  1099             {
       
  1100             // Recursion is used - menu data tree is not very deep.
       
  1101             MXCFWNode* node = aNode.FirstChild();
       
  1102             while( node && TraverseNodeL( *node, aVisitor ) )
       
  1103                 {
       
  1104                 node = node->NextSibling();
       
  1105                 }
       
  1106             }
       
  1107         // countChildren( object );
       
  1108         return aVisitor.VisitLeaveL( object );
       
  1109         }
       
  1110     else
       
  1111         {
       
  1112         return aVisitor.VisitL( object );
       
  1113         }
       
  1114     }
       
  1115 
       
  1116 // ---------------------------------------------------------
       
  1117 // CMenuEng::InitIdManagerL
       
  1118 // ---------------------------------------------------------
       
  1119 //
       
  1120 void CMenuEng::InitIdManagerL( MXCFWNode& aRootNode )
       
  1121     {
       
  1122     __ASSERT_DEBUG( !iIdManager, User::Invariant() );
       
  1123     __ASSERT_DEBUG( iTree, User::Invariant() );
       
  1124     iIdManager = new (ELeave) CMenuEngIdManager();
       
  1125     TInt idSeed;
       
  1126     GetIdSeedL( idSeed );
       
  1127     iIdManager->SetSeed( idSeed );
       
  1128     // Read ID-s to ID manager.
       
  1129     TMenuEngIdManagerInit idManagerInit( *iIdManager );
       
  1130     TraverseNodeL( aRootNode, idManagerInit );
       
  1131     // Make sure all nodes have ID.
       
  1132     TMenuEngIdSetter idSetter( *iIdManager );
       
  1133     TraverseNodeL( aRootNode, idSetter ); 
       
  1134     }
       
  1135 
       
  1136 // ---------------------------------------------------------
       
  1137 // CMenuEng::GetIdSeedL
       
  1138 // ---------------------------------------------------------
       
  1139 //
       
  1140 void CMenuEng::GetIdSeedL( TInt& aIdSeed )
       
  1141     {
       
  1142     TInt seed = 0;
       
  1143     __ASSERT_DEBUG( iTree, User::Invariant() );
       
  1144     MXCFWNode* root = iTree->Root();
       
  1145     if ( root )
       
  1146         {
       
  1147         RNodeArray nodes;
       
  1148         CleanupClosePushL( nodes );
       
  1149         iTree->GetNodesOfTypeL
       
  1150             ( KMenuTypeIdSeed(), nodes, root, EFalse );
       
  1151         if ( nodes.Count() )
       
  1152             {
       
  1153             const CMenuEngObject& object = Object( *nodes[0] );
       
  1154             TPtrC val;
       
  1155             TBool dummy;
       
  1156             if ( object.FindAttribute( KMenuAttrIdSeed(), val, dummy ) )
       
  1157                 {
       
  1158                 seed = MenuEngId::AsInt( val );
       
  1159                 }
       
  1160             }
       
  1161         CleanupStack::PopAndDestroy( &nodes );
       
  1162         }
       
  1163     aIdSeed = seed;
       
  1164     }
       
  1165 
       
  1166 // ---------------------------------------------------------
       
  1167 // CMenuEng::SetIdSeedL
       
  1168 // ---------------------------------------------------------
       
  1169 //
       
  1170 void CMenuEng::SetIdSeedL( TInt aSeed )
       
  1171     {
       
  1172     __ASSERT_DEBUG( iTree, User::Invariant() );
       
  1173     MXCFWNode* root = iTree->Root();
       
  1174     if ( root )
       
  1175         {
       
  1176         CMenuEngObject* object = NULL;
       
  1177         RNodeArray nodes;
       
  1178         CleanupClosePushL( nodes );
       
  1179         iTree->GetNodesOfTypeL
       
  1180             ( KMenuTypeIdSeed(), nodes, root, EFalse );
       
  1181         if ( nodes.Count() )
       
  1182             {
       
  1183             object = &Object( *nodes[0] );
       
  1184             }
       
  1185         else
       
  1186             {
       
  1187             // Outside of the item tree -> no ID set.
       
  1188             object = CMenuEngObject::NewL( *this, KMenuTypeIdSeed() );
       
  1189             CleanupStack::PushL( object );
       
  1190             iTree->AddNodeL( object, root );
       
  1191             CleanupStack::Pop( object );
       
  1192             }
       
  1193         __ASSERT_DEBUG( object, User::Invariant() );
       
  1194         TBuf<KMenuMaxAttrValueLen> buf;
       
  1195         MenuEngId::AsString( aSeed, buf );
       
  1196         object->SetAttributeL( KMenuAttrIdSeed(), buf, EFalse );
       
  1197         CleanupStack::PopAndDestroy( &nodes );
       
  1198         }
       
  1199     }
       
  1200 
       
  1201 // ---------------------------------------------------------
       
  1202 // CMenuEng::SelfComplete
       
  1203 // ---------------------------------------------------------
       
  1204 //
       
  1205 void CMenuEng::SelfComplete( TInt aError )
       
  1206     {
       
  1207     TRequestStatus* ownStatus = &iStatus;
       
  1208     *ownStatus = KRequestPending;
       
  1209     SetActive();
       
  1210     User::RequestComplete( ownStatus, aError );
       
  1211     }
       
  1212 
       
  1213 // ---------------------------------------------------------
       
  1214 // CMenuEng::LoadRamFileL
       
  1215 // ---------------------------------------------------------
       
  1216 //
       
  1217 void CMenuEng::LoadRamFileL()
       
  1218     {
       
  1219     __ASSERT_DEBUG( ELoadRamFile == iState, User::Invariant() );
       
  1220     __ASSERT_DEBUG( !iTree, User::Invariant() );
       
  1221     iTree = CXCFWTree::NewL();
       
  1222     // Legacy xml format supported only if the xml is from rom:
       
  1223     iObjectFactory->SupportLegacyFormat( EFalse ); 
       
  1224     iEngine->LoadL( *iTree, iRamFileName );
       
  1225     }
       
  1226 
       
  1227 // ---------------------------------------------------------
       
  1228 // CMenuEng::LoadRomFileL
       
  1229 // ---------------------------------------------------------
       
  1230 //
       
  1231 void CMenuEng::LoadRomFileL()
       
  1232     {
       
  1233     __ASSERT_DEBUG( ELoadRomFile == iState, User::Invariant() );
       
  1234     // ROM file name is not kept as member - it is used only once.
       
  1235     TFileName fname;
       
  1236     GetFileNameL( fname, ERomFile );
       
  1237 	delete iIdManager; iIdManager = NULL;
       
  1238     delete iTree; iTree = NULL;
       
  1239     iTree = CXCFWTree::NewL();
       
  1240     // Legacy xml format supported only if the xml is from rom:
       
  1241     iObjectFactory->SupportLegacyFormat( ETrue ); 
       
  1242     iEngine->LoadL( *iTree, fname );
       
  1243     }
       
  1244 
       
  1245 // ---------------------------------------------------------
       
  1246 // CMenuEng::SaveTempFileL
       
  1247 // ---------------------------------------------------------
       
  1248 //
       
  1249 void CMenuEng::SaveTempFileL()
       
  1250     {
       
  1251     __ASSERT_DEBUG( ESaveFile == iState, User::Invariant() );
       
  1252     __ASSERT_DEBUG( iTree, User::Invariant() );
       
  1253     __ASSERT_DEBUG( iIdManager, User::Invariant() );
       
  1254     // Write back ID seed.
       
  1255     SetIdSeedL( iIdManager->Seed() );
       
  1256     // Save to temp file then replace content file with temp file.
       
  1257     // This avoids having incomplete content file, if something goes wrong.
       
  1258     __ASSERT_DEBUG( DebugSanityCheck( *iTree ), User::Invariant() );
       
  1259     TRAPD( err, iEngine->SaveL( *iTree, iTempFileName ) );
       
  1260     if( err==KErrDiskFull )
       
  1261         {
       
  1262         iDiskWasFullAndRamFileWasNotCreated = ETrue;
       
  1263         SelfComplete( KErrNone );
       
  1264         }
       
  1265     else
       
  1266         {
       
  1267         iDiskWasFullAndRamFileWasNotCreated = EFalse;
       
  1268         User::LeaveIfError( err );
       
  1269         }
       
  1270 
       
  1271     }
       
  1272 
       
  1273 // ---------------------------------------------------------
       
  1274 // CMenuEng::ReplaceRamFileL
       
  1275 // ---------------------------------------------------------
       
  1276 //
       
  1277 void CMenuEng::ReplaceRamFileL()
       
  1278     {
       
  1279     __ASSERT_DEBUG( ESaveFile == iState, User::Invariant() );
       
  1280     // RFs::Replace() copies the file data ->
       
  1281     // Delete() + Rename() is used instead.
       
  1282     iFs.Delete( iRamFileName );
       
  1283     TInt err = iFs.Rename( iTempFileName, iRamFileName );
       
  1284     if( err==KErrDiskFull )
       
  1285         {
       
  1286         iDiskWasFullAndRamFileWasNotCreated = ETrue;
       
  1287         }
       
  1288     else
       
  1289         {
       
  1290         iDiskWasFullAndRamFileWasNotCreated = EFalse;
       
  1291         User::LeaveIfError( err );
       
  1292         }
       
  1293     }
       
  1294 
       
  1295 // ---------------------------------------------------------
       
  1296 // CMenuEng::CompleteCurrentOperation
       
  1297 // ---------------------------------------------------------
       
  1298 //
       
  1299 void CMenuEng::CompleteCurrentOperation( TInt aError )
       
  1300     {
       
  1301     // Operation completion reported two ways:
       
  1302     // - "No change" operations report completion immediately;
       
  1303     // - "Change" operations trigger saving and wait the result.
       
  1304     __ASSERT_DEBUG( EExecuteOp == iState || ESaveFile == iState, \
       
  1305         User::Invariant() );
       
  1306     if ( iCurrentOperation )
       
  1307         {
       
  1308         iCurrentOperation->CompletedMenuEngOperation( aError );
       
  1309         iCurrentOperation = NULL;
       
  1310         }
       
  1311     // Flush notifications in the success case.
       
  1312     // (Otherwise, the changes did not happen.)
       
  1313     if ( !aError )
       
  1314         {
       
  1315         for ( TInt i = 0; i < iNotifyQueue.Count(); i++ )
       
  1316             {
       
  1317             const TMenuEngNotify& notify = iNotifyQueue[i];
       
  1318             iObserver.EngineEvents( notify.iFolder, notify.iEvents );
       
  1319             }
       
  1320         }
       
  1321     iNotifyQueue.Reset();
       
  1322     }
       
  1323 
       
  1324 // ---------------------------------------------------------
       
  1325 // CMenuEng::ExecuteOperationL
       
  1326 // ---------------------------------------------------------
       
  1327 //
       
  1328 void CMenuEng::ExecuteOperationL()
       
  1329     {
       
  1330     __ASSERT_DEBUG( EExecuteOp == iState, User::Invariant() );
       
  1331     __ASSERT_DEBUG( !iCurrentOperation, User::Invariant() );
       
  1332     if ( iOperations.Count() )
       
  1333         {
       
  1334         iCurrentOperation = iOperations[0];
       
  1335         iOperations.Remove( 0 );
       
  1336         iCurrentOperation->RunMenuEngOperationL();
       
  1337         SaveChangesL();
       
  1338         }
       
  1339     else
       
  1340         {
       
  1341         // No pending operations, we stop here.
       
  1342         // Next QueueOperationL will kick us back to operation.
       
  1343         iState = EReady;
       
  1344         }
       
  1345     }
       
  1346 
       
  1347 // ---------------------------------------------------------
       
  1348 // CMenuEng::SaveChangesL
       
  1349 // ---------------------------------------------------------
       
  1350 //
       
  1351 void CMenuEng::SaveChangesL()
       
  1352     {
       
  1353     // This method is part of ExecuteOperationL.
       
  1354     // (Post-operation processing.)
       
  1355     __ASSERT_DEBUG( EExecuteOp == iState, User::Invariant() );
       
  1356     if ( iChanged || iDiskWasFullAndRamFileWasNotCreated )
       
  1357         {
       
  1358         // Tree changed, async save.
       
  1359         iState = ESaveFile;
       
  1360         SaveTempFileL();
       
  1361         }
       
  1362     else
       
  1363         {
       
  1364         // Tree not changed, we are done.
       
  1365         CompleteCurrentOperation( KErrNone );
       
  1366         if ( iOperations.Count() )
       
  1367             {
       
  1368             // Go for the next operation (async).
       
  1369             iState = EExecuteOp; // Same as current value, code clarity.
       
  1370             SelfComplete( KErrNone );
       
  1371             }
       
  1372         else
       
  1373             {
       
  1374             // No pending operations, we stop here.
       
  1375             // Next QueueOperationL will kick us back to operation.
       
  1376             iState = EReady;
       
  1377             }
       
  1378         }
       
  1379     }
       
  1380 
       
  1381 //  End of File