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