examples/ForumNokia/TZLocalizer/src/TZLocalizerEngine.cpp

00001 /*
00002  * Copyright © 2008 Nokia Corporation.
00003  */
00004 
00005 #include <e32base.h>
00006 #include <t32wld.h>
00007 #include <badesca.h>
00008 
00009 #include "TZLocalizerEngine.h"
00010 const TInt KBufSize=30;
00011 
00012 _LIT( KTab, "\t");
00013 _LIT( KEmpty, " ");
00014 _LIT(KDateTimeString, "%*E%*D%X%*N%*Y %1 %2 '%3 %H%:1%T%:2%S");
00015 
00016 // ---------------------------------------------------------
00017 //  CTZLocalizerEngine::NewL()
00018 //  
00019 // ---------------------------------------------------------
00020 CTZLocalizerEngine* CTZLocalizerEngine::NewL()
00021     {
00022     CTZLocalizerEngine* self = CTZLocalizerEngine::NewLC();
00023     CleanupStack::Pop( self );
00024     return self;
00025     }
00026 
00027 // ---------------------------------------------------------
00028 //  CTZLocalizerEngine::NewLC()
00029 //  
00030 // ---------------------------------------------------------
00031 CTZLocalizerEngine* CTZLocalizerEngine::NewLC()
00032     {
00033     CTZLocalizerEngine* self = new( ELeave ) CTZLocalizerEngine();
00034     CleanupStack::PushL( self );
00035 
00036     self->ConstructL();
00037     return self;
00038     }
00039 
00040 // ---------------------------------------------------------
00041 //  CTZLocalizerEngine::~CTZLocalizerEngine()
00042 //  closes Time Zone server connection
00043 // ---------------------------------------------------------
00044 CTZLocalizerEngine::~CTZLocalizerEngine()
00045     {
00046     }
00047 
00048 // ---------------------------------------------------------
00049 //  CTZLocalizerEngine::CTZLocalizerEngine()
00050 //  
00051 // ---------------------------------------------------------
00052 CTZLocalizerEngine::CTZLocalizerEngine()
00053     {
00054     }
00055 
00056 // ---------------------------------------------------------
00057 //  CTZLocalizerEngine::ConstructL()
00058 //  Connect to time zone server. 
00059 // ---------------------------------------------------------
00060 void CTZLocalizerEngine::ConstructL()
00061     {
00062     }
00063 
00064 // ---------------------------------------------------------
00065 // TPtrC CTZLocalizerEngine::GetCityName(CTzLocalizedCity *aLocalizedCity)
00066 // Get current city name.
00067 // ---------------------------------------------------------
00068 TPtrC CTZLocalizerEngine::GetCityName(CTzLocalizedCity *aLocalizedCity)
00069 {
00070     return aLocalizedCity->Name();
00071 }
00072 
00073 // ---------------------------------------------------------
00074 // CTZLocalizerEngine::GetCityTimeZoneId(CTzLocalizedCity *aLocalizedCity)
00075 // Get current city time zone id.
00076 // ---------------------------------------------------------
00077 TUint16 CTZLocalizerEngine::GetCityTimeZoneId(CTzLocalizedCity *aLocalizedCity)
00078 {
00079     return aLocalizedCity->TimeZoneId();
00080 }
00081 
00082 // ---------------------------------------------------------
00083 // CTZLocalizerEngine::GetCityGroupId(CTzLocalizedCity *aLocalizedCity)
00084 // Get current city group id.
00085 // ---------------------------------------------------------
00086 TUint8 CTZLocalizerEngine::GetCityGroupId(CTzLocalizedCity *aLocalizedCity)
00087 {
00088     return aLocalizedCity->GroupId();
00089 }
00090 
00091 // ---------------------------------------------------------
00092 // CTZLocalizerEngine::GetCityLocalTime()
00093 // Convert universal time to local time.
00094 // ---------------------------------------------------------
00095 HBufC* CTZLocalizerEngine::GetCityLocalTimeL(CTzLocalizedCity *aLocalizedCity)
00096     {   
00097     RTz tzServer;
00098     User::LeaveIfError(tzServer.Connect());
00099     CleanupClosePushL(tzServer);
00100     
00101     TBuf<KBufSize> dateTimeString;
00102     TTime time;
00103     time.HomeTime();
00104     time.FormatL(dateTimeString, KDateTimeString);
00105     // this is a 2 stage process - first convert the date/time to UTC.
00106     // No need to specify the time zone because 
00107     // this function uses the current system time zone.
00108     TInt results=tzServer.ConvertToUniversalTime(time);
00109     
00110     //get the current time zone id
00111     CTzId* timezoneId = CTzId::NewL(GetCityTimeZoneId(aLocalizedCity));
00112     CleanupStack::PushL(timezoneId);
00113     // convert UTC time to local time
00114     results=tzServer.ConvertToLocalTime(time,*timezoneId);
00115     
00116     TBuf<KBufSize> timeZoneString;
00117     timeZoneString.Copy(timezoneId->TimeZoneNameID());
00118     time.FormatL(dateTimeString, KDateTimeString);
00119         
00120     CleanupStack::PopAndDestroy(timezoneId);
00121     
00122     HBufC* temp = HBufC::NewL(dateTimeString.Size());
00123     (*temp)= dateTimeString;
00124     
00125     CleanupStack::PopAndDestroy(1); // causes tzServer.Close() to be called
00126 
00127     return temp;        
00128     }
00129 
00130 // ---------------------------------------------------------
00131 // CTZLocalizerEngine::FindCityL()
00132 // Find the specific city.
00133 // return localizedcity
00134 // ---------------------------------------------------------
00135 CTzLocalizedCity* CTZLocalizerEngine::FindCityL(const TDesC& aCityName )
00136     {
00137     CTzLocalizer* loc = CTzLocalizer::NewL(); 
00138     CleanupStack::PushL(loc); 
00139 
00140     CTzLocalizedCity* localizedCity = loc->FindCityByNameL(aCityName);
00141     
00142     CleanupStack::PopAndDestroy(loc); 
00143 
00144     return localizedCity;   
00145     }
00146     
00147 // ---------------------------------------------------------
00148 //  CTZLocalizerEngine::GetAllTimeZonesL()
00149 //  Fetch the valid time zone ID's
00150 // ---------------------------------------------------------
00151 CDesC16ArrayFlat* CTZLocalizerEngine::GetAllTimeZonesL()
00152     {
00153     CTzLocalizer* loc = CTzLocalizer::NewL(); 
00154     CleanupStack::PushL(loc); 
00155 
00156     CTzLocalizedCityArray* locCityArray = 
00157         loc->GetCitiesL(loc->ETzUTCAscending);
00158     CleanupStack::PushL(locCityArray); 
00159     
00160     CDesC16ArrayFlat* timeZones = 
00161         new (ELeave)CDesC16ArrayFlat( locCityArray->Count() );
00162         
00163     TBuf<KBufSize> temp;
00164     TInt result;
00165     TInt k = 0;
00166     for(TInt i=0; i<locCityArray->Count();i++)
00167         {
00168         result = 0;
00169         //get the time zone id
00170         temp.Num((locCityArray->At(i)).TimeZoneId());
00171         result = timeZones->Find(temp, k);
00172         //add if not allready exist
00173         if(result!=0)
00174             {
00175             timeZones->AppendL(temp);
00176             }           
00177         }
00178 
00179     timeZones->Sort();
00180 
00181     CleanupStack::PopAndDestroy(2); //loc and locCityArray  
00182     return timeZones;
00183     }    
00184     
00185 // ---------------------------------------------------------
00186 // CTZLocalizerEngine::GetAllGroupID()
00187 // Fetch the valid city group ID's
00188 // ---------------------------------------------------------
00189 CDesC16ArrayFlat* CTZLocalizerEngine::GetAllGroupIDL()
00190     {
00191     CTzLocalizer* loc = CTzLocalizer::NewL(); 
00192     CleanupStack::PushL(loc); 
00193 
00194     CTzLocalizedCityArray* locCityArray = 
00195         loc->GetCitiesL(loc->ETzUTCAscending);
00196     CleanupStack::PushL(locCityArray); 
00197     
00198     CDesC16ArrayFlat* groupIDs = 
00199         new (ELeave)CDesC16ArrayFlat( locCityArray->Count() );
00200         
00201     TBuf<KBufSize> temp;
00202     TInt result;
00203     TInt k = 0;
00204     for(TInt i=0; i<locCityArray->Count();i++)
00205         {
00206         result = 0;
00207         //get the group id
00208         temp.Num((locCityArray->At(i)).GroupId());
00209         result = groupIDs->Find(temp, k);
00210         //add if not allready exist
00211         if(result!=0)
00212             {
00213             groupIDs->AppendL(temp);    
00214             }       
00215         }
00216         
00217     groupIDs->Sort();
00218 
00219     CleanupStack::PopAndDestroy(2); //loc and locCityArray  
00220     return groupIDs;
00221     }      
00222 
00223 // ---------------------------------------------------------
00224 // CDesC16ArrayFlat* CTZLocalizerEngine::GetAllCitiesL()
00225 // Fetch the localized cities
00226 // ---------------------------------------------------------
00227 CDesC16ArrayFlat* CTZLocalizerEngine::GetAllCitiesL()
00228     {
00229     CTzLocalizer* loc = CTzLocalizer::NewL(); 
00230     CleanupStack::PushL(loc); 
00231 
00232     CTzLocalizedCityArray* locCityArray = 
00233         loc->GetCitiesL(loc->ETzAlphaNameAscending);
00234     CleanupStack::PushL(locCityArray); 
00235     
00236     CDesC16ArrayFlat* locCity = 
00237         new (ELeave)CDesC16ArrayFlat( locCityArray->Count() );
00238 
00239     for(TInt i=0; i<locCityArray->Count();i++)
00240         {
00241         locCity->AppendL((locCityArray->At(i)).Name()); 
00242         }   
00243 
00244     CleanupStack::PopAndDestroy(2); //loc and locCityArray
00245 
00246     return locCity;
00247     }
00248 
00249 
00250 // ---------------------------------------------------------
00251 //  CTZLocalizerEngine::AddCityL()
00252 //  Add the city in database.
00253 //  return localized city
00254 // ---------------------------------------------------------    
00255 CTzLocalizedCity* CTZLocalizerEngine::AddCityL(TInt aTimeZoneId, 
00256     const TDesC &aCityName, TInt aGroupId)
00257 {
00258     CTzLocalizer* loc = CTzLocalizer::NewL(); 
00259     CleanupStack::PushL(loc);
00260     CTzLocalizedCity* localizedCity;
00261     TRAPD(error,
00262         localizedCity = loc->AddCityL(aTimeZoneId, aCityName, aGroupId));
00263     
00264     if(error!=KErrNone)
00265         {
00266         localizedCity = NULL;
00267         }
00268     CleanupStack::PopAndDestroy(loc); 
00269     
00270     return localizedCity;
00271 }
00272 
00273 
00274 // ---------------------------------------------------------
00275 // CTZLocalizerEngine::FindCitiesInGroupL()
00276 // Find the cities with same group id.
00277 // ---------------------------------------------------------
00278 CDesC16ArrayFlat* CTZLocalizerEngine::FindCitiesInGroupL( TInt aGroupID )
00279     {   
00280     CTzLocalizer* loc = CTzLocalizer::NewL(); 
00281     CleanupStack::PushL(loc); 
00282     
00283     CTzLocalizedCityArray* locCityArray
00284         = loc->GetCitiesInGroupL(aGroupID, loc->ETzAlphaNameAscending);
00285     CleanupStack::PushL(locCityArray); 
00286     
00287     CDesC16ArrayFlat* locCity = 
00288         new (ELeave)CDesC16ArrayFlat( locCityArray->Count() );
00289 
00290     TBuf<KBufSize> temp;
00291 
00292     for(TInt i=0; i<locCityArray->Count();i++)
00293         {
00294         temp.Copy( KTab );
00295         temp.Append((locCityArray->At(i)).Name());
00296         temp.Append( KTab );
00297         temp.Append( KEmpty );
00298         locCity->AppendL(temp);
00299         }   
00300 
00301     CleanupStack::PopAndDestroy(2); //loc and locCityArray
00302 
00303     return locCity;
00304     }
00305 
00306 // ---------------------------------------------------------
00307 // CTZLocalizerEngine::RemoveCityL()
00308 // Removes a city from the user defined city collection 
00309 // ---------------------------------------------------------
00310 void CTZLocalizerEngine::RemoveCityL(const TDesC& aCityName )
00311     {
00312     CTzLocalizer* loc = CTzLocalizer::NewL(); 
00313     CleanupStack::PushL(loc); 
00314     CTzLocalizedCity* localizedCity = FindCityL(aCityName);
00315     //Remove a city from the user defined collection
00316     loc->RemoveCityL(localizedCity);
00317     CleanupStack::PopAndDestroy(loc);
00318     }
00319 
00320 //EOF

Generated by  doxygen 1.6.2