idlehomescreen/xmluirendering/renderingplugins/xnclockfactory/src/xnclockcontrol.cpp
changeset 0 f72a12da539e
child 15 ff572dfe6d86
equal deleted inserted replaced
-1:000000000000 0:f72a12da539e
       
     1 /*
       
     2 * Copyright (c) 2006-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:  Implementation for xuikon clock.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // SYSTEM INCLUDE FILES
       
    20 #include <e32std.h>         // for RChangeNotifier
       
    21 
       
    22 // USER INCLUDE FILES
       
    23 #include "xncontroladapter.h"
       
    24 #include "xnclockadapter.h"
       
    25 #include "xnclockface.h"
       
    26 
       
    27 #include "xnclockcontrol.h"
       
    28 
       
    29 
       
    30 // LOCAL CONSTANTS AND MACROS
       
    31 static const TInt KIntervalTime( 60000000 ); // in microseconds
       
    32 
       
    33 
       
    34 // MODULE DATA STRUCTURES
       
    35 class CXnClockChangeHandler : public CActive
       
    36     {
       
    37     public: // Constructor and destructor
       
    38         static CXnClockChangeHandler* NewL(
       
    39             CXnClockControl& aClient )
       
    40             {
       
    41             CXnClockChangeHandler* self =
       
    42                 new (ELeave) CXnClockChangeHandler( aClient );
       
    43             CleanupStack::PushL( self );
       
    44             self->ConstructL();
       
    45             CleanupStack::Pop( self );
       
    46             return self;
       
    47             }
       
    48 
       
    49         virtual ~CXnClockChangeHandler()
       
    50             {
       
    51             Cancel();
       
    52             iChangeNotifier.Close();
       
    53             }
       
    54 
       
    55     private: // From CActive
       
    56         void DoCancel()
       
    57             {
       
    58             iChangeNotifier.LogonCancel();
       
    59             // Return value is ignored.
       
    60             }
       
    61 
       
    62         void RunL()
       
    63             {
       
    64             if( iStatus.Int() & ( EChangesLocale | EChangesSystemTime ) )
       
    65                 {
       
    66                 iClient.TimeOrLocaleChanged();
       
    67                 }
       
    68 
       
    69             User::LeaveIfError( iChangeNotifier.Logon( iStatus ) );
       
    70             SetActive();
       
    71             }
       
    72 
       
    73     private: // Private constructors
       
    74         void ConstructL()
       
    75             {
       
    76             User::LeaveIfError( iChangeNotifier.Create() );
       
    77             User::LeaveIfError( iChangeNotifier.Logon( iStatus ) );
       
    78             SetActive();
       
    79             }
       
    80 
       
    81         CXnClockChangeHandler( CXnClockControl& aClient )
       
    82             : CActive( EPriorityStandard ), iClient( aClient )
       
    83             {
       
    84             CActiveScheduler::Add( this );
       
    85             }
       
    86 
       
    87     private: // Data
       
    88         RChangeNotifier  iChangeNotifier;
       
    89         CXnClockControl& iClient;
       
    90     };
       
    91 
       
    92 // ============================ MEMBER FUNCTIONS ===============================
       
    93 
       
    94 // -----------------------------------------------------------------------------
       
    95 // CXnClockControl::CXnClockControl
       
    96 // C++ default constructor can NOT contain any code, that
       
    97 // might leave.
       
    98 // -----------------------------------------------------------------------------
       
    99 //
       
   100 CXnClockControl::CXnClockControl( CXnClockAdapter* aAdapter,
       
   101                                   const TBool aFormatFromLocale,
       
   102                                   const TClockFormat aFormat )
       
   103     : iAdapter( aAdapter ), 
       
   104       iClockFormat( aFormat ),
       
   105       iFormatFromLocale ( aFormatFromLocale )      
       
   106     {
       
   107     }
       
   108 
       
   109 // -----------------------------------------------------------------------------
       
   110 // CXnClockControl::ConstructL
       
   111 // Symbian 2nd phase constructor can leave.
       
   112 // -----------------------------------------------------------------------------
       
   113 //
       
   114 void CXnClockControl::ConstructL()
       
   115     {           
       
   116     iTimer = CPeriodic::NewL( CActive::EPriorityHigh );
       
   117             
       
   118     SetFormatL( iFormatFromLocale, iClockFormat );
       
   119 
       
   120     iHandler = CXnClockChangeHandler::NewL( *this );
       
   121     }
       
   122 
       
   123 // -----------------------------------------------------------------------------
       
   124 // CXnClockControl::NewL
       
   125 // Two-phased constructor.
       
   126 // -----------------------------------------------------------------------------
       
   127 //
       
   128 CXnClockControl* CXnClockControl::NewL( CXnClockAdapter* aAdapter,
       
   129                                         const TBool aFormatFromLocale,
       
   130                                         const TClockFormat aFormat )
       
   131     {
       
   132     CXnClockControl* self =
       
   133         new (ELeave) CXnClockControl( aAdapter, aFormatFromLocale, aFormat );
       
   134         
       
   135     CleanupStack::PushL( self );    
       
   136     self->ConstructL();
       
   137     
       
   138     CleanupStack::Pop( self );
       
   139     
       
   140     return self;
       
   141     }
       
   142 
       
   143 // -----------------------------------------------------------------------------
       
   144 // Destructor
       
   145 // -----------------------------------------------------------------------------
       
   146 //
       
   147 CXnClockControl::~CXnClockControl()
       
   148     {
       
   149     delete iHandler;
       
   150     delete iTimer;
       
   151     delete iFace;
       
   152     }
       
   153 
       
   154 // -----------------------------------------------------------------------------
       
   155 // CXnClockControl::UpdateDisplay
       
   156 // (other items were commented in a header).
       
   157 // -----------------------------------------------------------------------------
       
   158 //
       
   159 void CXnClockControl::UpdateDisplay()
       
   160     {
       
   161     if( !iFace )
       
   162         {
       
   163         return;
       
   164         }
       
   165             
       
   166     iAdapter->UpdateDisplay();
       
   167     }
       
   168 
       
   169 // -----------------------------------------------------------------------------
       
   170 // CXnClockControl::TimeOrLocaleChanged
       
   171 // (other items were commented in a header).
       
   172 // -----------------------------------------------------------------------------
       
   173 //
       
   174 void CXnClockControl::TimeOrLocaleChanged()
       
   175     {
       
   176     TLocale locale;
       
   177 
       
   178     if( iFormatFromLocale && ( locale.ClockFormat() != iClockFormat ) )
       
   179         {
       
   180         TRAP_IGNORE( SetFormatL( iFormatFromLocale, locale.ClockFormat() ) );        
       
   181         }
       
   182         
       
   183     UpdateDisplay();        
       
   184     }
       
   185 
       
   186 // -----------------------------------------------------------------------------
       
   187 // CXnClockControl::SetFormatL
       
   188 // (other items were commented in a header).
       
   189 // -----------------------------------------------------------------------------
       
   190 //
       
   191 void CXnClockControl::SetFormatL( const TBool aFormatFromLocale, 
       
   192                                   const TClockFormat aFormat )
       
   193     {
       
   194     TClockFormat format( aFormat );
       
   195     
       
   196     if( aFormatFromLocale )
       
   197         {
       
   198         format = TLocale().ClockFormat();                
       
   199         }
       
   200         
       
   201     iFormatFromLocale = aFormatFromLocale;
       
   202         
       
   203     if( format == iClockFormat && iFace )
       
   204         {
       
   205         // Already correct face
       
   206         return;    
       
   207         }      
       
   208             
       
   209     iClockFormat = format;
       
   210     
       
   211     delete iFace;
       
   212     iFace = NULL;
       
   213 
       
   214     if( format == EClockAnalog )
       
   215         {
       
   216         iFace = CXnClockFaceAnalog::NewL();
       
   217         }
       
   218     else if( format == EClockDigital )
       
   219         {
       
   220         iFace = CXnClockFaceDigital::NewL();
       
   221         }                
       
   222     }
       
   223 
       
   224 // -----------------------------------------------------------------------------
       
   225 // CXnClockControl::Format
       
   226 // (other items were commented in a header).
       
   227 // -----------------------------------------------------------------------------
       
   228 //       
       
   229 TClockFormat CXnClockControl::Format() const
       
   230     {
       
   231     return iClockFormat;
       
   232     }
       
   233 
       
   234 // -----------------------------------------------------------------------------
       
   235 // CXnClockControl::Draw
       
   236 // (other items were commented in a header).
       
   237 // -----------------------------------------------------------------------------
       
   238 //       
       
   239 void CXnClockControl::Draw( CWindowGc& aGc, const TRect& aRect ) const
       
   240     {
       
   241     if( iFace && !aRect.IsEmpty() )
       
   242         {
       
   243         TTime homeTime;
       
   244         homeTime.HomeTime();
       
   245 
       
   246         TRAP_IGNORE( iFace->DrawL( *iAdapter, aGc, aRect, homeTime ) );
       
   247         
       
   248         // Ensure timer is active
       
   249         const_cast< CXnClockControl* >( this )->StartTimer();
       
   250         }
       
   251     }
       
   252 
       
   253 // -----------------------------------------------------------------------------
       
   254 // CXnClockControl::TimerCallback
       
   255 // (other items were commented in a header).
       
   256 // -----------------------------------------------------------------------------
       
   257 //
       
   258 TInt CXnClockControl::TimerCallback( TAny* aThis )
       
   259     {
       
   260     CXnClockControl* self = static_cast< CXnClockControl* >( aThis );
       
   261 
       
   262     // Update the clock display
       
   263     self->UpdateDisplay();
       
   264 
       
   265     // Adjust the timer delay if necessary
       
   266     TTime time;
       
   267     time.HomeTime();
       
   268     TDateTime dateTime( time.DateTime() );
       
   269 
       
   270     if( dateTime.Second() > 0 )
       
   271         {
       
   272         self->iTimer->Cancel();
       
   273         self->iTimer->After( KIntervalTime -
       
   274             1000000 * dateTime.Second() - dateTime.MicroSecond() );
       
   275         }
       
   276 
       
   277     return KErrNone;
       
   278     }
       
   279     
       
   280 // -----------------------------------------------------------------------------
       
   281 // CXnClockControl::StartTimer
       
   282 // (other items were commented in a header).
       
   283 // -----------------------------------------------------------------------------
       
   284 //
       
   285 void CXnClockControl::StartTimer()
       
   286     {
       
   287     if ( iTimer && !iTimer->IsActive() )
       
   288         {
       
   289         TTime time;
       
   290         time.HomeTime();
       
   291         TDateTime dateTime( time.DateTime() );
       
   292         TCallBack callBack( TimerCallback, this );
       
   293 
       
   294         iTimer->Start(
       
   295             TTimeIntervalMicroSeconds32(
       
   296                 KIntervalTime - 1000000 * dateTime.Second() - dateTime.MicroSecond() ),
       
   297             TTimeIntervalMicroSeconds32( KIntervalTime ),
       
   298             callBack );
       
   299         }
       
   300     }
       
   301 
       
   302 // -----------------------------------------------------------------------------
       
   303 // CXnClockControl::StopTimer
       
   304 // (other items were commented in a header).
       
   305 // -----------------------------------------------------------------------------
       
   306 //
       
   307 void CXnClockControl::StopTimer()
       
   308     {
       
   309     if ( iTimer && iTimer->IsActive() )
       
   310         {
       
   311         iTimer->Cancel();
       
   312         }
       
   313     }
       
   314 
       
   315 //  End of File