widgetmanager/src/wmpersistentwidgetorder.cpp
changeset 0 f72a12da539e
equal deleted inserted replaced
-1:000000000000 0:f72a12da539e
       
     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. empty the in-mempory storage
       
   113     CleanupArray();
       
   114     // 2. serialize the widget array tags to a local array
       
   115     for( TInt i=0; i<aArray.Count(); ++i )
       
   116         {
       
   117         TInt32 uid = aArray[i]->Uid().iUid;
       
   118         HBufC16* publisherId = aArray[i]->HsContentInfo().PublisherId().AllocLC();
       
   119         iTagArray.AppendL( Tag( uid, publisherId ) );
       
   120         CleanupStack::Pop( publisherId );
       
   121         }
       
   122     // 3. create stream for storing the data to a file
       
   123     TFileName storeFileName;
       
   124     GetStoreFileNameL( storeFileName );
       
   125     CPermanentFileStore* fileStore = CPermanentFileStore::ReplaceLC(
       
   126             iFs, storeFileName, EFileWrite );
       
   127     fileStore->SetTypeL(KPermanentFileStoreLayoutUid);
       
   128     RStoreWriteStream writer;
       
   129     TStreamId id = writer.CreateLC( *fileStore );
       
   130     // 4. write all content to the stream
       
   131     writer.WriteInt32L( iTagArray.Count() );
       
   132     for( TInt i=0; i<iTagArray.Count(); ++i )
       
   133         {
       
   134         writer.WriteInt32L( iTagArray[i].iUid );
       
   135         writer.WriteInt32L( iTagArray[i].iPublisherId->Length() );
       
   136         writer.WriteL( *iTagArray[i].iPublisherId,
       
   137                 iTagArray[i].iPublisherId->Length() );
       
   138         }
       
   139     // 5. check available space and commit the stream
       
   140     TInt streamsize = writer.Sink()->SizeL();
       
   141     TBool  noSpace = SysUtil::DiskSpaceBelowCriticalLevelL( &iFs, streamsize,  
       
   142                                                                 EDriveC ); 
       
   143     if( noSpace )
       
   144         {
       
   145         //do nothing
       
   146         }
       
   147     else // there is enough free space
       
   148         {
       
   149         writer.CommitL();
       
   150         fileStore->SetRootL(id);
       
   151         fileStore->CommitL();        
       
   152         }
       
   153 
       
   154     // 6. cleanup
       
   155     CleanupStack::PopAndDestroy( &writer );
       
   156     CleanupStack::PopAndDestroy( fileStore );
       
   157     }
       
   158 
       
   159 // ---------------------------------------------------------
       
   160 // CWmPersistentWidgetOrder::CleanupArray
       
   161 // ---------------------------------------------------------
       
   162 //
       
   163 void CWmPersistentWidgetOrder::CleanupArray()
       
   164     {
       
   165     for( TInt i=0; i<iTagArray.Count(); ++i )
       
   166         {
       
   167         delete iTagArray[i].iPublisherId;
       
   168         iTagArray[i].iPublisherId = 0;
       
   169         }
       
   170     iTagArray.Close();
       
   171     }
       
   172 
       
   173 // ---------------------------------------------------------
       
   174 // CWmPersistentWidgetOrder::GetStoreFileNameL
       
   175 // ---------------------------------------------------------
       
   176 //
       
   177 void CWmPersistentWidgetOrder::GetStoreFileNameL(
       
   178         TDes& aPathBuf )
       
   179     {
       
   180     // get default drive for phone memory
       
   181     TInt driveNum = EDriveC;
       
   182     User::LeaveIfError( DriveInfo::GetDefaultDrive(
       
   183         DriveInfo::EDefaultPhoneMemory, driveNum ) );
       
   184     // make sure the directory exists
       
   185     iFs.CreatePrivatePath( driveNum ); // ignore errors
       
   186     // build the store path
       
   187     TChar driveLetter;
       
   188     User::LeaveIfError( iFs.DriveToChar( driveNum, driveLetter ) );
       
   189     aPathBuf.Append( driveLetter );
       
   190     aPathBuf.Append( _L(":") );
       
   191     TFileName privatePath;
       
   192     User::LeaveIfError( iFs.PrivatePath( privatePath ) );
       
   193     aPathBuf.Append( privatePath );
       
   194     aPathBuf.Append( KStoreFileName );
       
   195     }
       
   196 
       
   197 // ---------------------------------------------------------
       
   198 // CWmPersistentWidgetOrder::IndexOf
       
   199 // ---------------------------------------------------------
       
   200 //
       
   201 TInt CWmPersistentWidgetOrder::IndexOf( const CWmWidgetData& aWidgetData ) const
       
   202     {
       
   203     TInt found = KErrNotFound;
       
   204     for( TInt i=0; i<iTagArray.Count() && found<0; ++i )
       
   205         {
       
   206         if ( iTagArray[i].Matches( aWidgetData ) )
       
   207             found = i;
       
   208         }
       
   209     return found;
       
   210     }
       
   211 
       
   212 // ---------------------------------------------------------
       
   213 // CWmPersistentWidgetOrder::IsEmpty
       
   214 // ---------------------------------------------------------
       
   215 //
       
   216 TBool CWmPersistentWidgetOrder::IsEmpty() const
       
   217     {
       
   218     return iTagArray.Count() == 0;
       
   219     }
       
   220 
       
   221 // ---------------------------------------------------------
       
   222 // CWmPersistentWidgetOrder::Tag::Tag
       
   223 // ---------------------------------------------------------
       
   224 //
       
   225 CWmPersistentWidgetOrder::Tag::Tag(
       
   226         TInt32 aUid, HBufC16* aPublisherId )
       
   227     {
       
   228     iUid = aUid;
       
   229     iPublisherId = aPublisherId;
       
   230     }
       
   231 
       
   232 // ---------------------------------------------------------
       
   233 // CWmPersistentWidgetOrder::Tag::Matches
       
   234 // ---------------------------------------------------------
       
   235 //
       
   236 TBool CWmPersistentWidgetOrder::Tag::Matches(
       
   237         const CWmWidgetData& aWidgetData ) const
       
   238     {
       
   239     return ( aWidgetData.Uid().iUid == iUid &&
       
   240             aWidgetData.HsContentInfo().PublisherId() == *iPublisherId );
       
   241     }
       
   242 
       
   243 
       
   244 // end of file