uiacceltk/hitchcock/Client/src/alfbrusharray.cpp
changeset 0 15bf7259bb7c
equal deleted inserted replaced
-1:000000000000 0:15bf7259bb7c
       
     1 /*
       
     2 * Copyright (c) 2006 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:   Array for brushes
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #include "alf/alfbrusharray.h"
       
    21 #include "alf/alfvisual.h"
       
    22 #include "alf/alfenv.h"
       
    23 #include "alfclient.h"
       
    24 #include "alf/alfbrush.h"
       
    25 #include "alflogger.h"
       
    26 #include "alf/alfgencomponent.h"
       
    27 #include "alf/alfconstants.h"
       
    28 
       
    29 #include <uiacceltk/HuiUtil.h>
       
    30 
       
    31 /**
       
    32  * This watcher is a helper class to detect misusages of the brush arrays.
       
    33  *
       
    34  * A classic example is that the user creates a brush which is deleted before
       
    35  * it is removed from all the arrays where it is added. 
       
    36  *
       
    37  * This idle-loop is started when the system notices when a brush is deleted which
       
    38  * is still in this array. This is only acceptable if the owning visual is deleted
       
    39  * in the same scheduler loop. See the CAlfBrushArrayUsageWatcher::RunL
       
    40  * for the result if the array is not deleted right away.
       
    41  */ 
       
    42 NONSHARABLE_CLASS(CAlfBrushArrayUsageWatcher) : public CActive
       
    43     {
       
    44 public:
       
    45     CAlfBrushArrayUsageWatcher();
       
    46     ~CAlfBrushArrayUsageWatcher();
       
    47 protected:
       
    48     void RunL();
       
    49     void DoCancel(){};
       
    50     };
       
    51 
       
    52 // Constructor, which initiates the idle-loop
       
    53 CAlfBrushArrayUsageWatcher::CAlfBrushArrayUsageWatcher() : CActive( EPriorityIdle )
       
    54     {
       
    55     CActiveScheduler::Add( this );
       
    56     
       
    57     SetActive();
       
    58     iStatus = KRequestPending;
       
    59     TRequestStatus* status = &iStatus;
       
    60     User::RequestComplete( status, KErrNone );
       
    61     }
       
    62 
       
    63 // destructor which cancels the loop    
       
    64 CAlfBrushArrayUsageWatcher::~CAlfBrushArrayUsageWatcher()
       
    65     {
       
    66     Cancel();
       
    67     }
       
    68 
       
    69 // RunL is called when the idle-loop is executed.
       
    70 void CAlfBrushArrayUsageWatcher::RunL()
       
    71     {
       
    72     // In normal usage, this code snippet should never ne run.
       
    73     // This is called, when a user deletes a brush (directly or through a brush array)
       
    74     // which is still left in this brush array. 
       
    75     
       
    76     __ALFLOGSTRING( "CAlfBrushArrayUsageWatcher::RunL Incorrect brush deletion!" )
       
    77 #ifdef _DEBUG
       
    78     USER_INVARIANT();
       
    79 #endif
       
    80     User::Leave( KErrGeneral ); 
       
    81     }
       
    82 
       
    83 
       
    84 // Private data structure.
       
    85 struct CAlfBrushArray::TPrivateData
       
    86     {
       
    87     CAlfVisual* iOwner;
       
    88     RPointerArray<CAlfBrush> iBrushes;
       
    89     RPointerArray<CAlfBrush> iOwnedBrushes;
       
    90     CAlfBrushArrayUsageWatcher* iWatcher;
       
    91     };
       
    92     
       
    93 // ======== MEMBER FUNCTIONS ========
       
    94 
       
    95 // ---------------------------------------------------------------------------
       
    96 // Constructor.
       
    97 // ---------------------------------------------------------------------------
       
    98 //
       
    99 CAlfBrushArray::CAlfBrushArray()
       
   100     {
       
   101     }
       
   102 
       
   103 
       
   104 // ---------------------------------------------------------------------------
       
   105 // 2nd phase constructor
       
   106 // ---------------------------------------------------------------------------
       
   107 //
       
   108 void CAlfBrushArray::ConstructL(CAlfVisual& aOwner)
       
   109     {
       
   110     iData = new (ELeave) TPrivateData;
       
   111     
       
   112     iData->iOwner = &aOwner;
       
   113     iData->iOwnedBrushes.Reset();
       
   114     iData->iBrushes.Reset();
       
   115     iData->iWatcher = NULL;
       
   116     }
       
   117 
       
   118 
       
   119 // ---------------------------------------------------------------------------
       
   120 // Two-phased constructor.
       
   121 // ---------------------------------------------------------------------------
       
   122 //
       
   123 CAlfBrushArray* CAlfBrushArray::NewL(CAlfVisual& aOwner)
       
   124     {
       
   125     CAlfBrushArray* self = new( ELeave ) CAlfBrushArray;
       
   126     CleanupStack::PushL( self );
       
   127     self->ConstructL(aOwner);
       
   128     CleanupStack::Pop( self );
       
   129     return self;
       
   130     }
       
   131 
       
   132 
       
   133 // ---------------------------------------------------------------------------
       
   134 // Destructor.
       
   135 // ---------------------------------------------------------------------------
       
   136 //
       
   137 CAlfBrushArray::~CAlfBrushArray()
       
   138     {
       
   139     if ( iData )
       
   140         {
       
   141         Reset();
       
   142         }
       
   143     delete iData;
       
   144     iData = NULL;
       
   145     }
       
   146 
       
   147 // ---------------------------------------------------------------------------
       
   148 // Resets the array.
       
   149 // ---------------------------------------------------------------------------
       
   150 //    
       
   151 EXPORT_C void CAlfBrushArray::Reset()
       
   152     {
       
   153     for ( TInt i = iData->iBrushes.Count()-1 ; i>= 0; i-- )
       
   154         {
       
   155         iData->iBrushes[i]->RemoveContainingArray( *this );
       
   156         }
       
   157 
       
   158     iData->iOwnedBrushes.ResetAndDestroy();
       
   159     iData->iBrushes.Reset();
       
   160     
       
   161     delete iData->iWatcher;
       
   162     iData->iWatcher= NULL;
       
   163     
       
   164     // Update the server side.
       
   165     TBuf8<1> dummy;   
       
   166     TInt err = iData->iOwner->Comms()->DoSynchronousCmd(EAlfVisualBrushArrayReset, KNullDesC8(), dummy); 
       
   167 
       
   168     if ( err )
       
   169         {
       
   170         __ALFLOGSTRING1( "CAlfBrushArray::Reset panic error %d", err )
       
   171         USER_INVARIANT();
       
   172         }
       
   173 
       
   174     }
       
   175 
       
   176 // ---------------------------------------------------------------------------
       
   177 // Add brush onto the array.
       
   178 // ---------------------------------------------------------------------------
       
   179 //
       
   180 EXPORT_C void CAlfBrushArray::AppendL(CAlfBrush* aBrush, TAlfOwnership aOwnership)
       
   181     {
       
   182     // Add to iBrushes array.
       
   183     TInt err = iData->iBrushes.Append( aBrush );
       
   184     if ( err != KErrNone )
       
   185         {
       
   186         // on failure, log'n'leave
       
   187         __ALFLOGSTRING1( "CAlfBrushArray::AppendL leave error %d", err )
       
   188         User::Leave( err );
       
   189         }
       
   190         
       
   191     err = aBrush->AppendContainingArray( *this );
       
   192     if ( err != KErrNone )
       
   193         {
       
   194         // on failure remove from iBrushes, log'n'leave
       
   195         iData->iBrushes.Remove( iData->iBrushes.Count() - 1 );
       
   196         iData->iBrushes.Compress();
       
   197         __ALFLOGSTRING1( "CAlfBrushArray::AppendL leave error %d", err )
       
   198         User::Leave( err );
       
   199         }
       
   200 
       
   201     // Update the server side.
       
   202     TInt2 int2(aBrush->Identifier(), aOwnership); 
       
   203     TPckgC<TInt2> buf(int2);
       
   204     TBuf8<1> dum;
       
   205 
       
   206     err = iData->iOwner->Comms()->DoSynchronousCmd(EAlfVisualBrushArrayAppend, buf, dum ); 
       
   207     if ( err != KErrNone )
       
   208         {
       
   209         // On error, remove it from the iBrushes and log'n'leave
       
   210         RemoveBrush(aBrush, iData->iBrushes.Count() - 1);        
       
   211         
       
   212         __ALFLOGSTRING1( "CAlfBrushArray::AppendL leave error %d", err )
       
   213         User::Leave(err);
       
   214         }
       
   215     // append ownership only on success (otherwise double deletion takes place on leave)
       
   216     if ( aOwnership == EAlfHasOwnership )
       
   217         {
       
   218         err = iData->iOwnedBrushes.Append( aBrush );
       
   219         if ( err != KErrNone )
       
   220             {
       
   221             // on failure remove brush (inform server as well) and log'n'leave
       
   222             Remove( iData->iBrushes.Count() - 1);
       
   223             __ALFLOGSTRING1( "CAlfBrushArray::AppendL leave error %d", err )
       
   224             User::Leave( err );
       
   225             }
       
   226         }
       
   227     }
       
   228 
       
   229 // ---------------------------------------------------------------------------
       
   230 // Insert
       
   231 // ---------------------------------------------------------------------------
       
   232 //    
       
   233 EXPORT_C void CAlfBrushArray::InsertL(TInt aPos, CAlfBrush* aBrush, TAlfOwnership aOwnership)
       
   234     {
       
   235     // Add to iBrushes array.
       
   236     TInt err = iData->iBrushes.Insert( aBrush, aPos );
       
   237     if ( err != KErrNone )
       
   238         {
       
   239         // on failure, log'n'leave
       
   240         __ALFLOGSTRING1( "CAlfBrushArray::InsertL leave error %d", err )
       
   241         User::Leave( err );
       
   242         }
       
   243         
       
   244     err = aBrush->AppendContainingArray( *this );
       
   245     if ( err != KErrNone )
       
   246         {
       
   247         // on failure remove from iBrushes, log'n'leave
       
   248         iData->iBrushes.Remove( aPos );
       
   249         iData->iBrushes.Compress();
       
   250         __ALFLOGSTRING1( "CAlfBrushArray::InsertL leave error %d", err )
       
   251         User::Leave( err );
       
   252         }
       
   253         
       
   254     // Update the server side.
       
   255     TInt3 int3(aBrush->Identifier(), aOwnership, aPos); 
       
   256     TPckgC<TInt3> buf(int3);
       
   257     TBuf8<1> dum;
       
   258 
       
   259     err = iData->iOwner->Comms()->DoSynchronousCmd(EAlfVisualBrushArrayInsert, buf, dum ); 
       
   260     if ( err != KErrNone )
       
   261         {
       
   262         // On error, remove it from the iBrushes and log'n'leave 
       
   263         RemoveBrush(aBrush, aPos);
       
   264         
       
   265         __ALFLOGSTRING1( "CAlfBrushArray::InsertL leave error %d", err )
       
   266         User::Leave(err);
       
   267         }
       
   268     
       
   269     // append ownership only on success (otherwise double deletion takes place on leave)    
       
   270     if ( aOwnership == EAlfHasOwnership )
       
   271         {
       
   272         err = iData->iOwnedBrushes.Append( aBrush );
       
   273         if ( err != KErrNone )
       
   274             {
       
   275             // on failure remove brush (inform server as well) and log'n'leave
       
   276             Remove( aPos );
       
   277             __ALFLOGSTRING1( "CAlfBrushArray::InsertL leave error %d", err )
       
   278             User::Leave( err );
       
   279             }
       
   280         }
       
   281     }
       
   282   
       
   283 // ---------------------------------------------------------------------------
       
   284 // Remove
       
   285 // ---------------------------------------------------------------------------
       
   286 //  
       
   287 EXPORT_C void CAlfBrushArray::Remove(TInt aPos)
       
   288     {
       
   289     // Update the server side.
       
   290     TPckgC<TInt> buf(aPos);
       
   291     TBuf8<1> dum;
       
   292 
       
   293     TInt err = iData->iOwner->Comms()->DoSynchronousCmd(EAlfVisualBrushArrayRemove, buf, dum ); 
       
   294     if ( err )
       
   295         {
       
   296         __ALFLOGSTRING1( "CAlfBrushArray::Remove panic error %d", err )
       
   297         USER_INVARIANT();
       
   298         }
       
   299     
       
   300     RemoveBrush(iData->iBrushes[aPos], aPos);
       
   301     }
       
   302   
       
   303 // ---------------------------------------------------------------------------
       
   304 // Brush count
       
   305 // ---------------------------------------------------------------------------
       
   306 //  
       
   307 EXPORT_C TInt CAlfBrushArray::Count() const
       
   308     {
       
   309     if ( iData )
       
   310         {
       
   311         return iData->iBrushes.Count();
       
   312         }
       
   313     return 0;
       
   314     }
       
   315   
       
   316 // ---------------------------------------------------------------------------
       
   317 // []
       
   318 // ---------------------------------------------------------------------------
       
   319 // 
       
   320 EXPORT_C CAlfBrush& CAlfBrushArray::operator [] (TInt aPos)
       
   321     {
       
   322     return At(aPos);
       
   323     }
       
   324 
       
   325 // ---------------------------------------------------------------------------
       
   326 // At
       
   327 // ---------------------------------------------------------------------------
       
   328 //
       
   329 EXPORT_C CAlfBrush& CAlfBrushArray::At(TInt aPos)
       
   330     {
       
   331     return *iData->iBrushes[aPos];
       
   332     }
       
   333 
       
   334 
       
   335 // ---------------------------------------------------------------------------
       
   336 // Remove brush and owned brushes if they exist
       
   337 // ---------------------------------------------------------------------------
       
   338 //  
       
   339 void CAlfBrushArray::RemoveBrush(CAlfBrush* aBrush, TInt aPosIndex)
       
   340     {
       
   341     aBrush->RemoveContainingArray( *this );
       
   342             
       
   343     const TInt ownedIndex = iData->iOwnedBrushes.Find( aBrush );
       
   344     if ( ownedIndex != KErrNotFound )
       
   345         {
       
   346         iData->iOwnedBrushes.Remove( ownedIndex );
       
   347         iData->iOwnedBrushes.Compress();
       
   348         delete aBrush;
       
   349         }
       
   350     
       
   351     if ( aPosIndex != KErrNotFound )
       
   352         {
       
   353         iData->iBrushes.Remove( aPosIndex );
       
   354         iData->iBrushes.Compress();
       
   355         }
       
   356     }
       
   357 
       
   358 // ---------------------------------------------------------------------------
       
   359 // Brush is been deleted. Remove it from the array (if exists - might be several times).
       
   360 // ---------------------------------------------------------------------------
       
   361 // 
       
   362 void CAlfBrushArray::HandleBrushDestroyed( CAlfBrush& aBrush )
       
   363     {
       
   364     for ( TInt index = Count()-1 ; index>=0 ; index-- )
       
   365         {
       
   366         if ( &At(index) == &aBrush )
       
   367             {
       
   368             // Update the server side.
       
   369             TPckgC<TInt> buf(index);
       
   370             TBuf8<1> dum;
       
   371             TInt err = iData->iOwner->Comms()->DoSynchronousCmd(EAlfVisualBrushArrayRemove, buf, dum ); 
       
   372             if ( err )
       
   373                 {
       
   374                 __ALFLOGSTRING1( "CAlfBrushArray::HandleBrushDestroyed panic error %d", err )
       
   375                 USER_INVARIANT();
       
   376                 }
       
   377             
       
   378             // Check the owned brushes - should never found!
       
   379             const TInt ownedIndex = iData->iOwnedBrushes.Find( &aBrush );
       
   380             __ASSERT_ALWAYS( ownedIndex == KErrNotFound, USER_INVARIANT() );
       
   381             
       
   382             // Remove from iBrushes array
       
   383             iData->iBrushes.Remove( index );
       
   384             iData->iBrushes.Compress();
       
   385             
       
   386             // create watcher
       
   387             if ( !iData->iWatcher )
       
   388                 {
       
   389                 // do not mind the OOM
       
   390                 iData->iWatcher = new CAlfBrushArrayUsageWatcher;
       
   391                 }
       
   392             }
       
   393         }
       
   394     }
       
   395 
       
   396