idlehomescreen/widgetmanager/src/wmpersistentwidgetorder.cpp
changeset 1 5315654608de
child 9 f966699dea19
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 * Defines an ordered list of widget id's with persistence capability
       
    16 *
       
    17 */
       
    18 
       
    19 // INCLUDES
       
    20 #include <s32file.h> // RFile
       
    21 #include <s32std.h> // streams
       
    22 #include <driveinfo.h>
       
    23 #include <sysutil.h> 
       
    24 
       
    25 #include "wmwidgetdata.h"
       
    26 #include "wmpersistentwidgetorder.h"
       
    27 
       
    28 // CONSTANTS
       
    29 _LIT( KStoreFileName, "wmlistorder.dat" );
       
    30 
       
    31 
       
    32 // ---------------------------------------------------------
       
    33 // CWmPersistentWidgetOrder::NewL
       
    34 // ---------------------------------------------------------
       
    35 //
       
    36 CWmPersistentWidgetOrder* CWmPersistentWidgetOrder::NewL( RFs& aFs )
       
    37     {
       
    38     CWmPersistentWidgetOrder* self =
       
    39         new (ELeave) CWmPersistentWidgetOrder( aFs );
       
    40     CleanupStack::PushL( self );
       
    41     self->ConstructL();
       
    42     CleanupStack::Pop( self );
       
    43     return self;
       
    44     }
       
    45 
       
    46 // ---------------------------------------------------------
       
    47 // CWmPersistentWidgetOrder::CWmPersistentWidgetOrder
       
    48 // ---------------------------------------------------------
       
    49 //
       
    50 CWmPersistentWidgetOrder::CWmPersistentWidgetOrder( RFs& aFs )
       
    51     : iFs( aFs )
       
    52     {
       
    53     }
       
    54 
       
    55 // ---------------------------------------------------------
       
    56 // CWmPersistentWidgetOrder::ConstructL
       
    57 // ---------------------------------------------------------
       
    58 //
       
    59 void CWmPersistentWidgetOrder::ConstructL()
       
    60     {
       
    61     }
       
    62 
       
    63 // ---------------------------------------------------------
       
    64 // CWmPersistentWidgetOrder::~CWmPersistentWidgetOrder
       
    65 // ---------------------------------------------------------
       
    66 //
       
    67 CWmPersistentWidgetOrder::~CWmPersistentWidgetOrder()
       
    68     {
       
    69     CleanupArray();
       
    70     }
       
    71 
       
    72 // ---------------------------------------------------------
       
    73 // CWmPersistentWidgetOrder::LoadL
       
    74 // ---------------------------------------------------------
       
    75 //
       
    76 void CWmPersistentWidgetOrder::LoadL()
       
    77     {
       
    78     // 1. empty the in-mempory storage
       
    79     CleanupArray();
       
    80     // 2. create stream for reading data from a file
       
    81     TFileName storeFileName;
       
    82     GetStoreFileNameL( storeFileName );
       
    83     CPermanentFileStore* fileStore = NULL;
       
    84     fileStore = CPermanentFileStore::OpenL(
       
    85             iFs, storeFileName, EFileRead );
       
    86     CleanupStack::PushL( fileStore );
       
    87     RStoreReadStream reader;
       
    88     reader.OpenLC( *fileStore, fileStore->Root() );
       
    89     // 3. read all contents from the stream
       
    90     TInt arrayCount = reader.ReadInt32L();
       
    91     while( iTagArray.Count() < arrayCount )
       
    92         {
       
    93         TInt32 uid = reader.ReadInt32L();
       
    94         TInt32 publisherIdLen = reader.ReadInt32L();
       
    95         HBufC16* publisherId = HBufC16::NewLC(publisherIdLen);
       
    96         TPtr16 publisherIdPtr = publisherId->Des();
       
    97         reader.ReadL( publisherIdPtr, publisherIdLen );
       
    98         iTagArray.AppendL( Tag( uid, publisherId ) );
       
    99         CleanupStack::Pop( publisherId );
       
   100         }
       
   101     // 4. cleanup
       
   102     CleanupStack::PopAndDestroy( &reader );
       
   103     CleanupStack::PopAndDestroy( fileStore );
       
   104     }
       
   105 
       
   106 // ---------------------------------------------------------
       
   107 // CWmPersistentWidgetOrder::StoreL
       
   108 // ---------------------------------------------------------
       
   109 //
       
   110 void CWmPersistentWidgetOrder::StoreL( const RWidgetDataValues& aArray )
       
   111     {
       
   112     // 1. create stream for storing the data to a file
       
   113     TFileName storeFileName;
       
   114     GetStoreFileNameL( storeFileName );
       
   115     CPermanentFileStore* fileStore = CPermanentFileStore::ReplaceLC(
       
   116             iFs, storeFileName, EFileWrite );
       
   117     fileStore->SetTypeL( KPermanentFileStoreLayoutUid );
       
   118     RStoreWriteStream writer;
       
   119     TStreamId id = writer.CreateLC( *fileStore );
       
   120     // 2. write all content to the stream
       
   121     writer.WriteInt32L( aArray.Count() );
       
   122     for( TInt i=0; i<aArray.Count(); ++i )
       
   123         {
       
   124         TInt32 uid = aArray[i]->Uid().iUid;
       
   125         const TDesC16& publisherId = aArray[i]->HsContentInfo().PublisherId();
       
   126         writer.WriteInt32L( uid );
       
   127         writer.WriteInt32L( publisherId.Length() );
       
   128         writer.WriteL( publisherId, publisherId.Length() );
       
   129         }
       
   130     // 3. check available space and commit the stream
       
   131     TInt streamsize = writer.Sink()->SizeL();
       
   132     TBool belowCriticalLevel = SysUtil::DiskSpaceBelowCriticalLevelL(
       
   133             &iFs, streamsize, EDriveC );
       
   134     if( !belowCriticalLevel )
       
   135         {
       
   136         writer.CommitL();
       
   137         fileStore->SetRootL(id);
       
   138         fileStore->CommitL();        
       
   139         }
       
   140     // 4. cleanup
       
   141     CleanupStack::PopAndDestroy( &writer );
       
   142     CleanupStack::PopAndDestroy( fileStore );
       
   143     }
       
   144 
       
   145 // ---------------------------------------------------------
       
   146 // CWmPersistentWidgetOrder::CleanupArray
       
   147 // ---------------------------------------------------------
       
   148 //
       
   149 void CWmPersistentWidgetOrder::CleanupArray()
       
   150     {
       
   151     for( TInt i=0; i<iTagArray.Count(); ++i )
       
   152         {
       
   153         delete iTagArray[i].iPublisherId;
       
   154         iTagArray[i].iPublisherId = 0;
       
   155         }
       
   156     iTagArray.Close();
       
   157     }
       
   158 
       
   159 // ---------------------------------------------------------
       
   160 // CWmPersistentWidgetOrder::GetStoreFileNameL
       
   161 // ---------------------------------------------------------
       
   162 //
       
   163 void CWmPersistentWidgetOrder::GetStoreFileNameL(
       
   164         TDes& aPathBuf )
       
   165     {
       
   166     // get default drive for phone memory
       
   167     TInt driveNum = EDriveC;
       
   168     User::LeaveIfError( DriveInfo::GetDefaultDrive(
       
   169         DriveInfo::EDefaultPhoneMemory, driveNum ) );
       
   170     // make sure the directory exists
       
   171     iFs.CreatePrivatePath( driveNum ); // ignore errors
       
   172     // build the store path
       
   173     TChar driveLetter;
       
   174     User::LeaveIfError( iFs.DriveToChar( driveNum, driveLetter ) );
       
   175     aPathBuf.Append( driveLetter );
       
   176     aPathBuf.Append( _L(":") );
       
   177     TFileName privatePath;
       
   178     User::LeaveIfError( iFs.PrivatePath( privatePath ) );
       
   179     aPathBuf.Append( privatePath );
       
   180     aPathBuf.Append( KStoreFileName );
       
   181     }
       
   182 
       
   183 // ---------------------------------------------------------
       
   184 // CWmPersistentWidgetOrder::IndexOf
       
   185 // ---------------------------------------------------------
       
   186 //
       
   187 TInt CWmPersistentWidgetOrder::IndexOf( const CWmWidgetData& aWidgetData ) const
       
   188     {
       
   189     TInt found = KErrNotFound;
       
   190     for( TInt i=0; i<iTagArray.Count() && found<0; ++i )
       
   191         {
       
   192         if ( iTagArray[i].Matches( aWidgetData ) )
       
   193             found = i;
       
   194         }
       
   195     return found;
       
   196     }
       
   197 
       
   198 // ---------------------------------------------------------
       
   199 // CWmPersistentWidgetOrder::IsEmpty
       
   200 // ---------------------------------------------------------
       
   201 //
       
   202 TBool CWmPersistentWidgetOrder::IsEmpty() const
       
   203     {
       
   204     return iTagArray.Count() == 0;
       
   205     }
       
   206 
       
   207 // ---------------------------------------------------------
       
   208 // CWmPersistentWidgetOrder::Tag::Tag
       
   209 // ---------------------------------------------------------
       
   210 //
       
   211 CWmPersistentWidgetOrder::Tag::Tag(
       
   212         TInt32 aUid, HBufC16* aPublisherId )
       
   213     {
       
   214     iUid = aUid;
       
   215     iPublisherId = aPublisherId;
       
   216     }
       
   217 
       
   218 // ---------------------------------------------------------
       
   219 // CWmPersistentWidgetOrder::Tag::Matches
       
   220 // ---------------------------------------------------------
       
   221 //
       
   222 TBool CWmPersistentWidgetOrder::Tag::Matches(
       
   223         const CWmWidgetData& aWidgetData ) const
       
   224     {
       
   225     return ( aWidgetData.Uid().iUid == iUid &&
       
   226             aWidgetData.HsContentInfo().PublisherId() == *iPublisherId );
       
   227     }
       
   228 
       
   229 
       
   230 // end of file