clock2/clockui/uilayer/src/clockdocument.cpp
branchRCL_3
changeset 30 bd7edf625bdd
equal deleted inserted replaced
29:12af337248b1 30:bd7edf625bdd
       
     1 /*
       
     2 * Copyright (c) 2008 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:   This is the source file for the CClockDocument class.
       
    15 *
       
    16 */
       
    17 
       
    18 // System includes
       
    19 #include <s32file.h>
       
    20 #include <f32file.h>
       
    21 #include <tzlocalizationdatatypes.h>
       
    22 #include <tzlocalizer.h>
       
    23 
       
    24 // User includes
       
    25 #include "clockdocument.h"
       
    26 #include "clockappui.h"
       
    27 #include "clkuialarmmodel.h"
       
    28 #include "clkcommon.h"
       
    29 
       
    30 // Constants
       
    31 const TInt KWorldClockListGranularity( 15 );
       
    32 const TInt KZeroValue( 0 );
       
    33 
       
    34 // Literals
       
    35 _LIT( KFileName, "clock.dat" );
       
    36 
       
    37 // ---------------------------------------------------------
       
    38 // CClockDocument::NewL
       
    39 // rest of the details are commented in the header
       
    40 // ---------------------------------------------------------
       
    41 //
       
    42 CClockDocument* CClockDocument::NewL( CEikApplication& aApp )
       
    43     {
       
    44     CClockDocument* selfObj = new ( ELeave ) CClockDocument( aApp );
       
    45     CleanupStack::PushL( selfObj );
       
    46     
       
    47     selfObj->ConstructL();
       
    48     
       
    49     CleanupStack::Pop( selfObj );
       
    50     return selfObj;
       
    51     }
       
    52 
       
    53 // ---------------------------------------------------------
       
    54 // CClockDocument::~CClockDocument
       
    55 // rest of the details are commented in the header
       
    56 // ---------------------------------------------------------
       
    57 //
       
    58 CClockDocument::~CClockDocument()
       
    59     {
       
    60     if( iAlarmModel )
       
    61         {
       
    62         delete iAlarmModel;
       
    63         iAlarmModel = NULL;
       
    64         }
       
    65     if( iTzLocalizer )
       
    66     	{
       
    67     	delete iTzLocalizer;
       
    68     	iTzLocalizer = NULL;
       
    69     	}
       
    70     if( iWorldClockList )
       
    71         {
       
    72         iWorldClockList->Reset();
       
    73         delete iWorldClockList;
       
    74         iWorldClockList = NULL;
       
    75         }
       
    76     }
       
    77 
       
    78 // ---------------------------------------------------------
       
    79 // CClockDocument::ReadDataL
       
    80 // rest of the details are commented in the header
       
    81 // ---------------------------------------------------------
       
    82 //
       
    83 void CClockDocument::ReadDataL()
       
    84     {
       
    85     // The stream used to store data.
       
    86     RFileReadStream fileRStream;
       
    87     TFileName fileName;
       
    88 
       
    89     CleanupClosePushL( fileRStream );
       
    90 
       
    91     // The eikon environment.
       
    92     CEikonEnv* eikonEnv = CEikonEnv::Static();
       
    93     eikonEnv->FsSession().PrivatePath( fileName );
       
    94     
       
    95     TParse fileParse;
       
    96     fileParse.Set( KFileName, &fileName, NULL ); 
       
    97 
       
    98     // Open the file for reading.
       
    99     User::LeaveIfError( fileRStream.Open( Process()->FsSession(), fileParse.FullName(), EFileShareReadersOnly ) );
       
   100 
       
   101     TUint highInt( fileRStream.ReadUint32L() );
       
   102     TUint lowInt( fileRStream.ReadUint32L() );
       
   103     
       
   104     // Restore the previous alarm time.    
       
   105     iPrevAlarmTime = MAKE_TINT64( highInt, lowInt );
       
   106     
       
   107     // Read the cities selected by user previously.
       
   108     TUint cityCount( FALSE );
       
   109     TRAP_IGNORE( cityCount = fileRStream.ReadUint32L() );
       
   110 
       
   111     TCityInfo cityInfo;
       
   112     RTz tzHandle;
       
   113     // Connect to the timezone handle.
       
   114     User::LeaveIfError( tzHandle.Connect() );
       
   115     CleanupClosePushL( tzHandle );
       
   116 
       
   117     for( TUint index( FALSE ); index < cityCount; index++ ) 
       
   118     	{
       
   119     	cityInfo.iImagePath.Delete( KZeroValue, cityInfo.iImagePath.Length() );
       
   120     	ReadCityL( fileRStream, cityInfo, tzHandle );
       
   121     	iWorldClockList->AppendL( cityInfo, sizeof( cityInfo ) );    
       
   122     	}
       
   123     
       
   124     // Cleanup.
       
   125     CleanupStack::PopAndDestroy( &tzHandle );
       
   126     CleanupStack::PopAndDestroy( &fileRStream );
       
   127     }
       
   128 
       
   129 // ---------------------------------------------------------
       
   130 // CClockDocument::StoreDataL
       
   131 // rest of the details are commented in the header
       
   132 // ---------------------------------------------------------
       
   133 //
       
   134 void CClockDocument::StoreDataL()
       
   135     {
       
   136     // The stream used to store data.
       
   137     RFileWriteStream fileWStream;
       
   138     TFileName fileName;
       
   139 
       
   140     CleanupClosePushL( fileWStream );
       
   141 
       
   142     // The eikon environment.
       
   143     CEikonEnv* eikonEnv = CEikonEnv::Static();
       
   144 
       
   145     eikonEnv->FsSession().PrivatePath( fileName );
       
   146     eikonEnv->FsSession().MkDirAll( fileName );
       
   147 
       
   148     TParse fileParse;
       
   149     fileParse.Set( KFileName, &fileName, NULL ); 
       
   150 
       
   151     // Replace the file if it already exists.
       
   152     User::LeaveIfError( fileWStream.Replace( Process()->FsSession(),fileParse.FullName(), EFileShareExclusive ) );
       
   153 
       
   154     // Store the previous alarm time.
       
   155     fileWStream.WriteUint32L( I64HIGH( iPrevAlarmTime ) );
       
   156     fileWStream.WriteUint32L( I64LOW( iPrevAlarmTime ) );
       
   157 
       
   158     // Complete the write operation.
       
   159     fileWStream.CommitL();
       
   160     
       
   161     TInt index( FALSE );
       
   162     // Check if the home city is added in world clock
       
   163     if( iHomeCityItemAdded )
       
   164         {
       
   165         // Do not store the home city data.
       
   166         // Start storing data from index 1
       
   167         index = TRUE;
       
   168         // Store the count of cities in the list without home city.
       
   169         fileWStream.WriteUint32L( iWorldClockList->Count()- 1 );
       
   170         }
       
   171     else
       
   172         {
       
   173         // Store all the cities selected in world clock view.
       
   174         // Store the count of cities in the list.
       
   175         fileWStream.WriteUint32L( iWorldClockList->Count() );
       
   176         }
       
   177 		
       
   178     // Now store the cities selected in world clock view.    
       
   179     for( ; index < iWorldClockList->Count(); index++ )
       
   180     	{
       
   181     	fileWStream.WriteInt16L( ( *iWorldClockList )[ index ].iCityGroupId );
       
   182     	fileWStream.WriteInt16L( ( *iWorldClockList )[ index ].iCityOffsetInGroup );
       
   183 
       
   184     	TInt imagePathLength = ( *iWorldClockList )[ index ].iImagePath.Length();
       
   185     	fileWStream.WriteInt16L( imagePathLength  );
       
   186     	if( KZeroValue != imagePathLength )
       
   187             {
       
   188             fileWStream.WriteL( ( *iWorldClockList )[ index ].iImagePath, imagePathLength );
       
   189             }
       
   190     	}
       
   191     // Complete the write operation. 
       
   192     fileWStream.CommitL();
       
   193     
       
   194     // Cleanup.
       
   195     CleanupStack::PopAndDestroy( &fileWStream );
       
   196     }
       
   197 
       
   198 // ---------------------------------------------------------
       
   199 // CClockDocument::AlarmModel
       
   200 // rest of the details are commented in the header
       
   201 // ---------------------------------------------------------
       
   202 //
       
   203 CClkUiAlarmModel* CClockDocument::AlarmModel()
       
   204     {
       
   205     return iAlarmModel;
       
   206     }
       
   207 
       
   208 /*// ---------------------------------------------------------
       
   209 // CClockDocument::GetPrevAlarmTime
       
   210 // rest of the details are commented in the header
       
   211 // ---------------------------------------------------------
       
   212 //
       
   213 TTime CClockDocument::GetPrevAlarmTime()
       
   214     {
       
   215     return TTime( iPrevAlarmTime );
       
   216     }
       
   217 
       
   218 // ---------------------------------------------------------
       
   219 // CClockDocument::SetPrevAlarmTime
       
   220 // rest of the details are commented in the header
       
   221 // ---------------------------------------------------------
       
   222 //
       
   223 void CClockDocument::SetPrevAlarmTime( TTime aPrevAlarmTime )
       
   224     {
       
   225     iPrevAlarmTime = aPrevAlarmTime.Int64();
       
   226     }*/
       
   227 
       
   228 // ---------------------------------------------------------
       
   229 // CClockDocument::GetWorldClockList
       
   230 // rest of the details are commented in the header
       
   231 // ---------------------------------------------------------
       
   232 //
       
   233 CArrayPakFlat< TCityInfo >* CClockDocument::GetWorldClockList()
       
   234     {
       
   235     return iWorldClockList;
       
   236     }
       
   237 
       
   238 // ---------------------------------------------------------
       
   239 // CClockDocument::GetHomeCity
       
   240 // rest of the details are commented in the header
       
   241 // ---------------------------------------------------------
       
   242 //
       
   243 const TBuf< KHomeCityLength > CClockDocument::GetHomeCity()
       
   244     {
       
   245     return iHomeCity;
       
   246     }
       
   247 
       
   248 // ---------------------------------------------------------
       
   249 // CClockDocument::SetHomeCityL
       
   250 // rest of the details are commented in the header
       
   251 // ---------------------------------------------------------
       
   252 //
       
   253 void CClockDocument::SetHomeCityL( TCityInfo& aCityInfo )
       
   254     {
       
   255     // First save the home city.
       
   256     iHomeCity.Copy( aCityInfo.iCity );
       
   257     
       
   258     // Now use the localizer to update the home city as a frequently used zone.
       
   259     iTzLocalizer->SetTimeZoneL( aCityInfo.iTimeZoneId );
       
   260     CTzLocalizedCity* localizedCity = iTzLocalizer->FindCityByNameL( aCityInfo.iCity, aCityInfo.iTimeZoneId );
       
   261     if( localizedCity )
       
   262         {
       
   263     	CleanupStack::PushL( localizedCity );
       
   264     	iTzLocalizer->SetFrequentlyUsedZoneL( *localizedCity, CTzLocalizedTimeZone::ECurrentZone );
       
   265     	// Cleanup.
       
   266     	CleanupStack::PopAndDestroy( localizedCity );
       
   267         }
       
   268     }
       
   269 
       
   270 // ---------------------------------------------------------
       
   271 // CClockDocument::UpdateHomeCityL
       
   272 // rest of the details are commented in the header
       
   273 // ---------------------------------------------------------
       
   274 //
       
   275 void CClockDocument::UpdateHomeCityL()
       
   276     {
       
   277     // Connect to the timezone server.
       
   278     RTz tzHandle;
       
   279     User::LeaveIfError( tzHandle.Connect() );
       
   280     CleanupClosePushL( tzHandle );
       
   281     
       
   282     // Get the current timezone ID.
       
   283     CTzId* timeZoneId;
       
   284     timeZoneId = tzHandle.GetTimeZoneIdL();
       
   285     CleanupStack::PushL( timeZoneId);
       
   286 
       
   287     CTzLocalizedTimeZone* localizedTimeZone( NULL );
       
   288     CTzLocalizer* localizer( NULL );
       
   289         
       
   290     TRAPD( errVal, localizer = CTzLocalizer::NewL() );
       
   291     if( localizer && ( !errVal ) )
       
   292         {
       
   293         CleanupStack::PushL( localizer );
       
   294         // Get the currently set localized timezone.
       
   295         TRAPD( errVal, localizedTimeZone = localizer->GetLocalizedTimeZoneL( timeZoneId->TimeZoneNumericID() ) );
       
   296         CleanupStack::PushL( localizedTimeZone );
       
   297         
       
   298         if( localizedTimeZone && ( !errVal ) )
       
   299             {
       
   300             // This returns the previously stored value of homecity.
       
   301             CTzLocalizedCity* localizedCity = localizer->GetFrequentlyUsedZoneCityL(CTzLocalizedTimeZone::ECurrentZone);
       
   302             CleanupStack::PushL(localizedCity);
       
   303             
       
   304             // Update the local copy of homecity.
       
   305             iHomeCity.Copy(localizedCity->Name());
       
   306             
       
   307             // Cleanup.
       
   308             CleanupStack::PopAndDestroy(localizedCity); 
       
   309             }
       
   310         
       
   311         // Cleanup.
       
   312         CleanupStack::PopAndDestroy( localizedTimeZone );   
       
   313         CleanupStack::PopAndDestroy(localizer);
       
   314         }
       
   315     
       
   316     // Cleanup.
       
   317     CleanupStack::PopAndDestroy( timeZoneId );
       
   318     CleanupStack::PopAndDestroy( &tzHandle );
       
   319     }
       
   320 
       
   321 // CClockDocument::CClockDocument
       
   322 // rest of the details are commented in the header
       
   323 // ---------------------------------------------------------
       
   324 //
       
   325 CClockDocument::CClockDocument( CEikApplication& aApp ) : CAknDocument( aApp )
       
   326     {
       
   327     // No implementation yet.
       
   328     }
       
   329 
       
   330 // ---------------------------------------------------------
       
   331 // CClockDocument::ConstructL
       
   332 // rest of the details are commented in the header
       
   333 // ---------------------------------------------------------
       
   334 //
       
   335 void CClockDocument::ConstructL()
       
   336     {
       
   337     // Construct the localizer.
       
   338     iTzLocalizer = CTzLocalizer::NewL();
       
   339     // Construct the alarm model.
       
   340     iAlarmModel = CClkUiAlarmModel::NewL( NULL, KAlarmListenerPriority );
       
   341     // Construct the world clock cities list.
       
   342     iWorldClockList = new( ELeave ) CArrayPakFlat< TCityInfo >( KWorldClockListGranularity );
       
   343     
       
   344     // Try to open the document file and read the data.
       
   345     TRAPD( readError, ReadDataL() );
       
   346     
       
   347     if( KErrNone != readError )
       
   348         {
       
   349         TTime homeTime;
       
   350         homeTime.HomeTime();
       
   351         TDateTime homeDateTime = homeTime.DateTime();
       
   352         // Set the hour minute and second.
       
   353         homeDateTime.SetHour( 9 );
       
   354         homeDateTime.SetMinute( 0 );
       
   355         homeDateTime.SetSecond( 0 );
       
   356         homeTime = homeDateTime;
       
   357         
       
   358         // Update the previous alarm time.
       
   359         iPrevAlarmTime = homeTime.Int64();
       
   360         
       
   361         // Reset the worldclocklist.
       
   362         iWorldClockList->Reset();
       
   363         }
       
   364     
       
   365     // Store the date read above.
       
   366     TRAP_IGNORE( StoreDataL() );
       
   367     // Update the homecity data.
       
   368     UpdateHomeCityL();
       
   369     }
       
   370 
       
   371 // ---------------------------------------------------------
       
   372 // CClockDocument::ReadCityL
       
   373 // rest of the details are commented in the header
       
   374 // ---------------------------------------------------------
       
   375 //
       
   376 void CClockDocument::ReadCityL( RFileReadStream& aStream, TCityInfo& aCityInfo, RTz& aTzHandle )
       
   377 	{
       
   378 	// We store the offset and the city group id. So we just read them.
       
   379 	TInt cityGroupId( aStream.ReadInt16L() );
       
   380 	TInt cityOffset( aStream.ReadInt16L() );
       
   381 	TInt imagePathLength ( aStream.ReadInt16L() );
       
   382 	
       
   383 	// These will hold the read zones and the respective UTC offsets.
       
   384 	RArray<TInt> timeZones;
       
   385 	RArray<TInt> utcOffsets;
       
   386 		
       
   387 	CTzLocalizedCityGroup* cityGroup( NULL );
       
   388 	CTzLocalizedCityArray* cityArray( NULL );
       
   389 	// Construct the cityArray and the cityGroup.
       
   390 	cityArray = iTzLocalizer->GetCitiesInGroupL( cityGroupId, CTzLocalizer::ETzUnsorted );
       
   391 	CleanupStack::PushL( cityArray );
       
   392 	cityGroup = iTzLocalizer->GetCityGroupL( cityGroupId );
       
   393 	CleanupStack::PushL( cityGroup );
       
   394 
       
   395 	if( ( FALSE <= cityOffset ) && ( cityOffset < cityArray->Count() ) )
       
   396 		{
       
   397 		// First get the localized city and get the offset for the same.
       
   398 		CTzLocalizedCity& localizedCity = cityArray->At( cityOffset );
       
   399 		timeZones.Append( localizedCity.TimeZoneId() );
       
   400 		aTzHandle.GetOffsetsForTimeZoneIdsL( timeZones, utcOffsets );    
       
   401 
       
   402 		// Now with the data obtained, update the city information to be returned.
       
   403 		aCityInfo.iCity.Copy( localizedCity.Name() );
       
   404 		aCityInfo.iCountry.Copy(cityGroup->Name());
       
   405 		aCityInfo.iCityGroupId = localizedCity.GroupId();
       
   406 		aCityInfo.iUtcOffset = utcOffsets[ FALSE ];
       
   407 		aCityInfo.iCityOffsetInGroup = cityOffset;
       
   408 		aCityInfo.iTimeZoneId = localizedCity.TimeZoneId();
       
   409 		}
       
   410 	if( KZeroValue != imagePathLength )
       
   411 		{
       
   412 		aStream.ReadL( aCityInfo.iImagePath, imagePathLength );	
       
   413 		}
       
   414 		
       
   415 	// Cleanup.
       
   416 	CleanupStack::PopAndDestroy( cityGroup ); 
       
   417 	CleanupStack::PopAndDestroy( cityArray ); 
       
   418 	timeZones.Close();
       
   419 	utcOffsets.Close();
       
   420 	}
       
   421 
       
   422 // ---------------------------------------------------------
       
   423 // CClockDocument::CreateAppUiL
       
   424 // rest of the details are commented in the header
       
   425 // ---------------------------------------------------------
       
   426 //
       
   427 CEikAppUi* CClockDocument::CreateAppUiL()
       
   428     {
       
   429     return new( ELeave ) CClockAppUi;
       
   430     }
       
   431 
       
   432 // End of file