widgetmanager/src/wmwidgetloaderao.cpp
changeset 1 5315654608de
parent 0 f72a12da539e
child 2 08c6ee43b396
equal deleted inserted replaced
0:f72a12da539e 1:5315654608de
     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 * Active object to load widgets into list
       
    16 *
       
    17 */
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include "wmcommon.h"
       
    21 #include "wmplugin.h"
       
    22 #include "wmresourceloader.h"
       
    23 #include "wmpersistentwidgetorder.h"
       
    24 #include "wmlistbox.h"
       
    25 #include "wmwidgetloaderao.h"
       
    26 
       
    27 #include <hscontentcontroller.h> // content control api
       
    28 #include <hscontentinfoarray.h> // content control api
       
    29 #include <WidgetRegistryClient.h> // widget reqistry
       
    30 
       
    31 
       
    32 // ---------------------------------------------------------------------------
       
    33 // CWmWidgetLoaderAo::NewL
       
    34 // ---------------------------------------------------------------------------
       
    35 //
       
    36 CWmWidgetLoaderAo* CWmWidgetLoaderAo::NewL(
       
    37         CWmPlugin& aWmPlugin,
       
    38         CWmListBox& aTargetList )
       
    39     {
       
    40     CWmWidgetLoaderAo* self = new (ELeave) CWmWidgetLoaderAo(
       
    41             aWmPlugin, aTargetList );
       
    42     CleanupStack::PushL( self );
       
    43     self->ConstructL();
       
    44     CleanupStack::Pop( self );
       
    45     return self;
       
    46     }
       
    47 
       
    48 // ---------------------------------------------------------------------------
       
    49 // CWmWidgetLoaderAo::NewL
       
    50 // ---------------------------------------------------------------------------
       
    51 //
       
    52 CWmWidgetLoaderAo::CWmWidgetLoaderAo(
       
    53         CWmPlugin& aWmPlugin,
       
    54         CWmListBox& aTargetList )
       
    55     : CAsyncOneShot( EPriorityLow )
       
    56     , iWmPlugin( aWmPlugin )
       
    57     , iWidgetsList( aTargetList )
       
    58     {
       
    59     iWidgetRegistry = NULL;
       
    60     iWidgetOrder = NULL;
       
    61     }
       
    62 
       
    63 // ---------------------------------------------------------------------------
       
    64 // CWmWidgetLoaderAo::ConstructL
       
    65 // ---------------------------------------------------------------------------
       
    66 //
       
    67 void CWmWidgetLoaderAo::ConstructL()
       
    68     {
       
    69     }
       
    70 
       
    71 // ---------------------------------------------------------------------------
       
    72 // CWmWidgetLoaderAo::~CWmWidgetLoaderAo
       
    73 // ---------------------------------------------------------------------------
       
    74 //
       
    75 CWmWidgetLoaderAo::~CWmWidgetLoaderAo()
       
    76     {
       
    77     // cancel ongoing operation
       
    78     Cancel();
       
    79 
       
    80     // cleanup run data
       
    81     Cleanup();
       
    82     
       
    83     }
       
    84 
       
    85 // ---------------------------------------------------------------------------
       
    86 // CWmWidgetLoaderAo::StartLoading
       
    87 // ---------------------------------------------------------------------------
       
    88 //
       
    89 void CWmWidgetLoaderAo::StartLoading()
       
    90     {
       
    91     if ( IsActive() )
       
    92         {
       
    93         // cancel ongoing process
       
    94         Cancel();
       
    95         }
       
    96     Call();
       
    97     }
       
    98 
       
    99 // ---------------------------------------------------------------------------
       
   100 // CWmWidgetLoaderAo::RunL
       
   101 // ---------------------------------------------------------------------------
       
   102 //
       
   103 void CWmWidgetLoaderAo::RunL()
       
   104     {
       
   105     DoLoadWidgetsL();
       
   106     Cleanup();
       
   107     }
       
   108 
       
   109 // ---------------------------------------------------------------------------
       
   110 // CWmWidgetLoaderAo::RunError
       
   111 // ---------------------------------------------------------------------------
       
   112 //
       
   113 TInt CWmWidgetLoaderAo::RunError( TInt /*aError*/ )
       
   114     {
       
   115     Cleanup();
       
   116     return KErrNone;
       
   117     }
       
   118 
       
   119 // ---------------------------------------------------------
       
   120 // CWmWidgetLoaderAo::DoLoadWidgetsL
       
   121 // ---------------------------------------------------------
       
   122 //
       
   123 void CWmWidgetLoaderAo::DoLoadWidgetsL()
       
   124     {
       
   125     // 1. mark all the existing widgets initially non-valid
       
   126     for( TInt i=0; i<iWidgetsList.WidgetDataCount(); ++i )
       
   127         iWidgetsList.WidgetData(i).SetValid( EFalse );
       
   128 
       
   129     // 2. load the widgets array
       
   130     MHsContentController& controller = iWmPlugin.ContentController();    
       
   131     CHsContentInfoArray* contentInfoArray = CHsContentInfoArray::NewL();
       
   132     CleanupStack::PushL( contentInfoArray );
       
   133     controller.WidgetListL( *contentInfoArray );
       
   134     
       
   135     // 3. load the widget order
       
   136     iWidgetOrder = CWmPersistentWidgetOrder::NewL( iWmPlugin.FileServer() );
       
   137     TRAPD( loadError, iWidgetOrder->LoadL() );
       
   138 
       
   139     // 4. loop through the content array and compare it against the existing
       
   140     // widget data.
       
   141     TInt widgetsAdded = 0;
       
   142     for ( TInt i=0; i < contentInfoArray->Array().Count(); i++ )
       
   143         {
       
   144         CHsContentInfo* contentInfo = contentInfoArray->Array()[i];
       
   145 
       
   146         // check if this widget exists.
       
   147         // if it does, keep the existing one
       
   148         // if it does not, add it
       
   149         CWmWidgetData* existingData = FindWidgetData( *contentInfo );
       
   150         if ( existingData )
       
   151             {
       
   152             // keep the existing one
       
   153             existingData->SetValid( ETrue );
       
   154             }
       
   155         else
       
   156             {
       
   157             // add a new one
       
   158             AddWidgetDataL( *contentInfo );
       
   159             ++widgetsAdded;
       
   160             }
       
   161         }
       
   162 
       
   163     // if widgets were added, extend the listbox
       
   164     if ( widgetsAdded )
       
   165         iWidgetsList.HandleItemAdditionL();
       
   166     
       
   167     // 5: finally, remove all UI widgets that during the loading process were
       
   168     // NOT marked as VALID. those widgets do not exist anymore.
       
   169     TInt widgetsRemoved = 0;
       
   170     for( TInt i=0; i<iWidgetsList.WidgetDataCount(); i++ )
       
   171         {
       
   172         if( !iWidgetsList.WidgetData(i).IsValid() )
       
   173             {
       
   174             iWidgetsList.RemoveWidgetData( i );
       
   175             ++widgetsRemoved;
       
   176             }
       
   177         }
       
   178 
       
   179     // 6: cleanup
       
   180     CleanupStack::PopAndDestroy( contentInfoArray );
       
   181 
       
   182     // 7. redraw list
       
   183     if ( widgetsAdded > 0 || widgetsRemoved > 0 )
       
   184         {
       
   185         iWidgetsList.DrawDeferred();
       
   186         }
       
   187 
       
   188     // 8. store list order if necessary
       
   189     if ( loadError != KErrNone || widgetsAdded > 0 || widgetsRemoved > 0 )
       
   190         {
       
   191         iWidgetOrder->StoreL( iWidgetsList.WidgetDataArray() );
       
   192         }
       
   193     
       
   194     }
       
   195 
       
   196 // ---------------------------------------------------------
       
   197 // CWmWidgetLoaderAo::FindWidgetDataL
       
   198 // ---------------------------------------------------------
       
   199 //
       
   200 CWmWidgetData* CWmWidgetLoaderAo::FindWidgetData(
       
   201         CHsContentInfo& aContentInfo )
       
   202     {
       
   203     CWmWidgetData* data = NULL;
       
   204     for( TInt i=0; i<iWidgetsList.WidgetDataCount() && !data; ++i )
       
   205         {
       
   206         if ( iWidgetsList.WidgetData(i).EqualsTo( aContentInfo ) )
       
   207             {
       
   208             data = &iWidgetsList.WidgetData(i);
       
   209             }
       
   210         }
       
   211     return data;
       
   212     }
       
   213 
       
   214 // ---------------------------------------------------------
       
   215 // CWmWidgetLoaderAo::AddWidgetDataL
       
   216 // ---------------------------------------------------------
       
   217 //
       
   218 void CWmWidgetLoaderAo::AddWidgetDataL(
       
   219         CHsContentInfo& aContentInfo )
       
   220     {
       
   221     if ( !iWidgetRegistry )
       
   222         {
       
   223         iWidgetRegistry = new (ELeave) RWidgetRegistryClientSession();
       
   224         User::LeaveIfError( iWidgetRegistry->Connect() );
       
   225         }
       
   226     
       
   227     CWmWidgetData* widgetData = CWmWidgetData::NewLC( 
       
   228             aContentInfo, iWidgetRegistry );
       
   229     widgetData->SetPersistentWidgetOrder( iWidgetOrder );
       
   230     widgetData->SetValid( ETrue );
       
   231     iWidgetsList.AddWidgetDataL( widgetData );
       
   232     CleanupStack::Pop( widgetData );
       
   233     }
       
   234 
       
   235 // ---------------------------------------------------------------------------
       
   236 // CWmWidgetLoaderAo::Cleanup
       
   237 // ---------------------------------------------------------------------------
       
   238 //
       
   239 void CWmWidgetLoaderAo::Cleanup()
       
   240     {
       
   241     // disconnect widget registry
       
   242     if ( iWidgetRegistry )
       
   243         {
       
   244         iWidgetRegistry->Disconnect();
       
   245         iWidgetRegistry->Close();
       
   246         delete iWidgetRegistry;
       
   247         iWidgetRegistry = NULL;
       
   248         }
       
   249     
       
   250     // delete widget order
       
   251     delete iWidgetOrder;
       
   252     iWidgetOrder = NULL;
       
   253     }
       
   254 
       
   255 // end of file
       
   256