idlehomescreen/widgetmanager/src/wmwidgetdata.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 * CWmWidgetData implementation.
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include <fbs.h>
       
    22 #include <bautils.h>
       
    23 #include <s32file.h>
       
    24 #include <hscontentinfo.h>
       
    25 #include <widgetregistryclient.h> // widgetreqistry
       
    26 #include <StringLoader.h>
       
    27 #include <eikenv.h>
       
    28 
       
    29 #include "wmwidgetdata.h"
       
    30 #include "wmwidgetdataobserver.h"
       
    31 #include "wmpersistentwidgetorder.h"
       
    32 #include "wmcommon.h"
       
    33 #include <widgetmanagerview.rsg>
       
    34 
       
    35 // ---------------------------------------------------------
       
    36 // CWmWidgetData::NewL
       
    37 // ---------------------------------------------------------
       
    38 //
       
    39 CWmWidgetData* CWmWidgetData::NewL( 
       
    40         CHsContentInfo* aHsContentInfo,
       
    41         RWidgetRegistryClientSession* aRegistryClientSession )
       
    42     {
       
    43     CWmWidgetData* self = CWmWidgetData::NewLC( 
       
    44             aHsContentInfo, aRegistryClientSession );
       
    45     CleanupStack::Pop(); // self;
       
    46     return self;
       
    47     }
       
    48 
       
    49 // ---------------------------------------------------------
       
    50 // CWmWidgetData::NewLC
       
    51 // ---------------------------------------------------------
       
    52 //
       
    53 CWmWidgetData* CWmWidgetData::NewLC( 
       
    54         CHsContentInfo* aHsContentInfo,
       
    55         RWidgetRegistryClientSession* aRegistryClientSession )
       
    56     {
       
    57     CWmWidgetData* self = new ( ELeave ) CWmWidgetData();
       
    58     CleanupStack::PushL(self);
       
    59     self->ConstructL( aHsContentInfo, aRegistryClientSession );
       
    60     return self;
       
    61     }
       
    62 
       
    63 // ---------------------------------------------------------
       
    64 // CWmWidgetData::CWmWidgetData
       
    65 // ---------------------------------------------------------
       
    66 //
       
    67 CWmWidgetData::CWmWidgetData()
       
    68     {
       
    69     iIdle = NULL;
       
    70     iLogoImage = NULL;    
       
    71     iLogoImageMask = NULL;
       
    72     iHsContentInfo = NULL;
       
    73     iWidgetType = CWmWidgetData::EUnknown;
       
    74     iInstallAnimationIndex = KErrNotFound;
       
    75     iPublisherUid = KNullUid;
       
    76     iLogoSize = TSize( 0, 0 );
       
    77     }
       
    78 
       
    79 // ---------------------------------------------------------
       
    80 // CWmWidgetData::ConstructL
       
    81 // ---------------------------------------------------------
       
    82 //
       
    83 void CWmWidgetData::ConstructL( 
       
    84         CHsContentInfo* aHsContentInfo,
       
    85         RWidgetRegistryClientSession* aRegistryClientSession )
       
    86     {
       
    87     InitL( aHsContentInfo, aRegistryClientSession );
       
    88 
       
    89     // start decoding the icon
       
    90     iImageConverter = CWmImageConverter::NewL( this );
       
    91     iIdle = CIdle::NewL( CActive::EPriorityLow );
       
    92     iIdle->Start( TCallBack( HandleAsyncIconString, this ) );
       
    93     }
       
    94 
       
    95 // ---------------------------------------------------------
       
    96 // CWmWidgetData::InitL
       
    97 // ---------------------------------------------------------
       
    98 //
       
    99 void CWmWidgetData::InitL(
       
   100         CHsContentInfo* aHsContentInfo,
       
   101         RWidgetRegistryClientSession* aRegistryClientSession )
       
   102     {
       
   103     // analyse the widget type
       
   104     if ( aHsContentInfo->Type() == KContentTemplate )
       
   105         {
       
   106         iWidgetType = CWmWidgetData::ECps; // wrt widgets included in this gategory
       
   107         }
       
   108     else if ( aHsContentInfo->Type() == KContentWidget )
       
   109         {
       
   110         iWidgetType = CWmWidgetData::ENative;
       
   111         }
       
   112 
       
   113     // take ownership of the content info
       
   114     iHsContentInfo = aHsContentInfo;
       
   115 
       
   116     // get publisher uid from widget registry
       
   117     FetchPublisherUidL( aRegistryClientSession );
       
   118 
       
   119     }
       
   120 
       
   121 // ---------------------------------------------------------
       
   122 // CWmWidgetData::~CWmWidgetData
       
   123 // ---------------------------------------------------------
       
   124 //
       
   125 CWmWidgetData::~CWmWidgetData()
       
   126     {
       
   127     if ( iIdle && iIdle->IsActive() )
       
   128         {
       
   129         iIdle->Cancel();
       
   130         }
       
   131     delete iIdle;
       
   132     SetObserver( NULL );
       
   133     delete iLogoImage;
       
   134     delete iLogoImageMask;
       
   135     delete iImageConverter;
       
   136     delete iHsContentInfo;
       
   137     }
       
   138 
       
   139 // ---------------------------------------------------------
       
   140 // CWmWidgetData::SetObserver
       
   141 // ---------------------------------------------------------
       
   142 //
       
   143 void CWmWidgetData::SetObserver( MWmWidgetDataObserver* aObserver )
       
   144     {
       
   145     iObserver = aObserver;
       
   146     }
       
   147 
       
   148 // ---------------------------------------------------------
       
   149 // CWmWidgetData::SetObserver
       
   150 // ---------------------------------------------------------
       
   151 //
       
   152 void CWmWidgetData::SetPersistentWidgetOrder(
       
   153         const CWmPersistentWidgetOrder* aPersistentWidgetOrder )
       
   154     {
       
   155     iPersistentWidgetOrder = aPersistentWidgetOrder;
       
   156     }
       
   157 
       
   158 // ---------------------------------------------------------
       
   159 // CWmWidgetData::EqualsTo
       
   160 // ---------------------------------------------------------
       
   161 //
       
   162 TBool CWmWidgetData::EqualsTo( CHsContentInfo& aContentInfo )
       
   163     {
       
   164     return (
       
   165         HsContentInfo().Uid() == aContentInfo.Uid() &&
       
   166         HsContentInfo().PublisherId() == aContentInfo.PublisherId() );
       
   167     }
       
   168 
       
   169 // ---------------------------------------------------------
       
   170 // CWmWidgetData::CompareByName
       
   171 // ---------------------------------------------------------
       
   172 //
       
   173 TInt CWmWidgetData::CompareByName( 
       
   174         const CWmWidgetData& aDataOne, const CWmWidgetData& aDataTwo )
       
   175     {
       
   176     // negate the result for ascending alphabetical order
       
   177     TInt result = aDataOne.Name().CompareC( aDataTwo.Name() );
       
   178     return result;
       
   179     }
       
   180 
       
   181 // ---------------------------------------------------------
       
   182 // CWmWidgetData::CompareByPersistentWidgetOrder
       
   183 // ---------------------------------------------------------
       
   184 //
       
   185 TInt CWmWidgetData::CompareByPersistentWidgetOrder( 
       
   186         const CWmWidgetData& aDataOne, const CWmWidgetData& aDataTwo )
       
   187     {
       
   188     TInt result;
       
   189     const CWmPersistentWidgetOrder* order = aDataOne.iPersistentWidgetOrder;
       
   190     if ( order && !order->IsEmpty() )
       
   191         {
       
   192         result = order->IndexOf( aDataOne ) - order->IndexOf( aDataTwo );
       
   193         }
       
   194     else
       
   195         {
       
   196         // fallback: if persistent widget order is not available
       
   197         // or it is empty (this is the case on first start, or if the persistent
       
   198         // file is corrupted or deleted) -> order widgets by name.
       
   199         result = CompareByName( aDataOne, aDataTwo );
       
   200         }
       
   201     return result;
       
   202     }
       
   203 
       
   204 // ---------------------------------------------------------
       
   205 // CWmWidgetData::NotifyCompletion
       
   206 // ---------------------------------------------------------
       
   207 //
       
   208 void CWmWidgetData::NotifyCompletion( TInt aError )
       
   209     {
       
   210     delete iLogoImage;
       
   211     iLogoImage = NULL;
       
   212     delete iLogoImageMask;
       
   213     iLogoImageMask = NULL;
       
   214     if ( KErrNone != aError )
       
   215         {
       
   216         // no image available. Do nothing.
       
   217         }
       
   218     else
       
   219         {
       
   220         iLogoImage = iImageConverter->Bitmap();
       
   221         iLogoImageMask = iImageConverter->Mask();
       
   222         FireDataChanged();
       
   223         }
       
   224     }
       
   225 
       
   226 // ---------------------------------------------------------
       
   227 // CWmWidgetData::HandleIconStringL
       
   228 // ---------------------------------------------------------
       
   229 //
       
   230 void CWmWidgetData::HandleIconStringL( const TDesC& aIconStr )
       
   231     {
       
   232     HBufC* iconStr = NULL;
       
   233     if ( aIconStr.Length() == 0 && 
       
   234         iPublisherUid != KNullUid )
       
   235         {
       
   236         // workaround for wrt widgets icon
       
   237         _LIT( KUidTag, "uid(0x%x)" );
       
   238         const TInt KLength = 32;
       
   239         iconStr = HBufC::NewLC( KLength );
       
   240         iconStr->Des().Format( KUidTag, iPublisherUid.iUid );
       
   241         }
       
   242     else
       
   243         {
       
   244         iconStr = aIconStr.AllocLC();
       
   245         }
       
   246 
       
   247     TSize size( iLogoSize );
       
   248     if ( iLogoImage ) { size = iLogoImage->SizeInPixels(); }
       
   249     if ( size != iLogoSize &&
       
   250         iLogoSize.iWidth > 0 && iLogoSize.iHeight > 0 )
       
   251         {
       
   252         size = iLogoSize;
       
   253         }
       
   254     iImageConverter->HandleIconStringL( 
       
   255             size.iWidth, size.iHeight, *iconStr );
       
   256     
       
   257     CleanupStack::PopAndDestroy( iconStr );    
       
   258     }
       
   259 
       
   260 // ---------------------------------------------------------
       
   261 // CWmWidgetData::FireDataChanged
       
   262 // ---------------------------------------------------------
       
   263 //
       
   264 void CWmWidgetData::FireDataChanged()
       
   265     {
       
   266     if ( iObserver )
       
   267         {
       
   268         iObserver->HandleWidgetDataChanged( this );
       
   269         }
       
   270     }
       
   271 
       
   272 // ----------------------------------------------------
       
   273 // CWmWidgetData::UidFromString
       
   274 // ----------------------------------------------------
       
   275 //
       
   276 TUid CWmWidgetData::UidFromString( const TDesC8& aUidString ) const
       
   277     {
       
   278     TUid uid( TUid::Null() );
       
   279     const TInt KHexPrefixLength = 2;
       
   280     if ( aUidString.Length() > KHexPrefixLength )
       
   281         {
       
   282         TUint id = 0;
       
   283         TLex8 lex( aUidString.Mid( KHexPrefixLength ) );
       
   284         if ( lex.Val( id, EHex ) == KErrNone )
       
   285             {
       
   286             uid.iUid = (TInt32)id;
       
   287             }
       
   288         }
       
   289     return uid;
       
   290     }
       
   291 
       
   292 // ----------------------------------------------------
       
   293 // CWmWidgetData::PublisherUid
       
   294 // ----------------------------------------------------
       
   295 //
       
   296 TUid CWmWidgetData::PublisherUid()
       
   297     {    
       
   298     return iPublisherUid;
       
   299     }
       
   300 
       
   301 // ----------------------------------------------------
       
   302 // CWmWidgetData::FetchPublisherUidL
       
   303 // ----------------------------------------------------
       
   304 //
       
   305 void CWmWidgetData::FetchPublisherUidL( 
       
   306             RWidgetRegistryClientSession* aRegistryClientSession )
       
   307     {
       
   308     if ( iPublisherUid == KNullUid && 
       
   309         PublisherId() != KNullDesC &&
       
   310         iWidgetType != CWmWidgetData::ENative &&
       
   311         aRegistryClientSession )
       
   312         {
       
   313         TInt widgetUid = aRegistryClientSession->GetWidgetUidL( PublisherId() );
       
   314         if ( widgetUid != 0 )
       
   315             {
       
   316             // WRT widget
       
   317             iPublisherUid = TUid::Uid( widgetUid );
       
   318             
       
   319             HBufC* desc = StringLoader::LoadLC( R_QTN_WM_WIDGET_DETAILS_WRT, 
       
   320                     CEikonEnv::Static() );
       
   321             iHsContentInfo->SetDescriptionL( *desc );
       
   322             CleanupStack::PopAndDestroy( desc );
       
   323             }
       
   324         else
       
   325             {
       
   326             iPublisherUid = KNullUid;
       
   327             }
       
   328         }
       
   329     }
       
   330 // ----------------------------------------------------
       
   331 // CWmWidgetData::SetLogoSize
       
   332 // ----------------------------------------------------
       
   333 //
       
   334 void CWmWidgetData::SetLogoSize( const TSize& aSize )
       
   335     {
       
   336     iLogoSize = aSize;
       
   337     if ( iImageConverter )
       
   338         {
       
   339         iImageConverter->SetLogoSize( aSize );
       
   340         }
       
   341     }
       
   342 
       
   343 // ---------------------------------------------------------
       
   344 // CWmWidgetData::HandleAsyncIconString
       
   345 // ---------------------------------------------------------
       
   346 //
       
   347 TInt CWmWidgetData::HandleAsyncIconString( TAny* aPtr )
       
   348     {
       
   349     CWmWidgetData* self = static_cast< CWmWidgetData* >( aPtr );    
       
   350     if ( self->iIdle->IsActive() )
       
   351       { 
       
   352       self->iIdle->Cancel(); 
       
   353       }
       
   354     TRAP_IGNORE( self->HandleIconStringL( 
       
   355             self->HsContentInfo().IconPath() ); );
       
   356     return KErrNone;
       
   357     }
       
   358 
       
   359 // ---------------------------------------------------------
       
   360 // CWmWidgetData::ReCreateLogo
       
   361 // ---------------------------------------------------------
       
   362 //
       
   363 void CWmWidgetData::ReCreateLogo( const TSize& aSize )
       
   364     {
       
   365     iLogoSize = aSize;
       
   366     
       
   367     delete iLogoImage;
       
   368     iLogoImage = NULL;
       
   369     delete iLogoImageMask;
       
   370     iLogoImageMask = NULL;
       
   371 
       
   372     if ( iIdle && !iIdle->IsActive() )
       
   373         {
       
   374         // start decoding the icon
       
   375         iIdle->Start( TCallBack( HandleAsyncIconString, this ) );
       
   376         }
       
   377     }
       
   378 
       
   379 // ---------------------------------------------------------
       
   380 // CWmWidgetData::ReplaceContentInfoL
       
   381 // ---------------------------------------------------------
       
   382 //
       
   383 TBool CWmWidgetData::ReplaceContentInfoL(
       
   384         CHsContentInfo* aHsContentInfo )
       
   385     {
       
   386     TBool sameAppearance = (
       
   387             iHsContentInfo->Name() == aHsContentInfo->Name() &&
       
   388             iHsContentInfo->Description() == aHsContentInfo->Description() &&
       
   389             iHsContentInfo->CanBeAdded() == aHsContentInfo->CanBeAdded() );
       
   390     TBool sameLogo = (
       
   391             iHsContentInfo->IconPath() == aHsContentInfo->IconPath() );
       
   392 
       
   393     // delete the old content info
       
   394     delete iHsContentInfo;
       
   395     iHsContentInfo = NULL;
       
   396 
       
   397     // re-init the object
       
   398     InitL( aHsContentInfo, NULL );
       
   399 
       
   400     if ( !sameAppearance )
       
   401         {
       
   402         // fire change event -> widget redrawn
       
   403         FireDataChanged();
       
   404         }
       
   405     
       
   406     if ( !sameLogo )
       
   407         {
       
   408         // re-convert image
       
   409         // change event will be fired later when bitmap is ready
       
   410         ReCreateLogo( iLogoSize );
       
   411         }
       
   412 
       
   413     return !( sameAppearance && sameLogo );
       
   414     }
       
   415 // End of file
       
   416