examples/ForumNokia/TZLocalizer/src/TZLocalizerEngine.cpp

00001 /*
00002  * Copyright (c) 2008-2010 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
00003  *    
00004  * Redistribution and use in source and binary forms, with or without
00005  * modification, are permitted provided that the following conditions are met:
00006  *    
00007  *  * Redistributions of source code must retain the above copyright notice, this
00008  *    list of conditions and the following disclaimer.
00009  *  * Redistributions in binary form must reproduce the above copyright notice,
00010  *    this list of conditions and the following disclaimer in the documentation
00011  *    and/or other materials provided with the distribution.
00012  *  * Neither the name of Nokia Corporation nor the names of its contributors
00013  *    may be used to endorse or promote products derived from this software
00014  *    without specific prior written permission.
00015  *    
00016  *    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017  *    AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018  *    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019  *    DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00020  *    FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00021  *    DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00022  *    SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00023  *    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00024  *    OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00025  *    OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026  *    
00027  *    Description:  
00028  */
00029 
00030 #include <e32base.h>
00031 #include <badesca.h>
00032 
00033 #include "TZLocalizerEngine.h"
00034 const TInt KBufSize=30;
00035 
00036 _LIT( KTab, "\t");
00037 _LIT( KEmpty, " ");
00038 _LIT(KDateTimeString, "%*E%*D%X%*N%*Y %1 %2 '%3 %H%:1%T%:2%S");
00039 
00040 // ---------------------------------------------------------
00041 //  CTZLocalizerEngine::NewL()
00042 //  
00043 // ---------------------------------------------------------
00044 CTZLocalizerEngine* CTZLocalizerEngine::NewL()
00045     {
00046     CTZLocalizerEngine* self = CTZLocalizerEngine::NewLC();
00047     CleanupStack::Pop( self );
00048     return self;
00049     }
00050 
00051 // ---------------------------------------------------------
00052 //  CTZLocalizerEngine::NewLC()
00053 //  
00054 // ---------------------------------------------------------
00055 CTZLocalizerEngine* CTZLocalizerEngine::NewLC()
00056     {
00057     CTZLocalizerEngine* self = new( ELeave ) CTZLocalizerEngine();
00058     CleanupStack::PushL( self );
00059 
00060     self->ConstructL();
00061     return self;
00062     }
00063 
00064 // ---------------------------------------------------------
00065 //  CTZLocalizerEngine::~CTZLocalizerEngine()
00066 //  closes Time Zone server connection
00067 // ---------------------------------------------------------
00068 CTZLocalizerEngine::~CTZLocalizerEngine()
00069     {
00070     }
00071 
00072 // ---------------------------------------------------------
00073 //  CTZLocalizerEngine::CTZLocalizerEngine()
00074 //  
00075 // ---------------------------------------------------------
00076 CTZLocalizerEngine::CTZLocalizerEngine()
00077     {
00078     }
00079 
00080 // ---------------------------------------------------------
00081 //  CTZLocalizerEngine::ConstructL()
00082 //  Connect to time zone server. 
00083 // ---------------------------------------------------------
00084 void CTZLocalizerEngine::ConstructL()
00085     {
00086     }
00087 
00088 // ---------------------------------------------------------
00089 // TPtrC CTZLocalizerEngine::GetCityName(CTzLocalizedCity *aLocalizedCity)
00090 // Get current city name.
00091 // ---------------------------------------------------------
00092 TPtrC CTZLocalizerEngine::GetCityName(CTzLocalizedCity *aLocalizedCity)
00093 {
00094     return aLocalizedCity->Name();
00095 }
00096 
00097 // ---------------------------------------------------------
00098 // CTZLocalizerEngine::GetCityTimeZoneId(CTzLocalizedCity *aLocalizedCity)
00099 // Get current city time zone id.
00100 // ---------------------------------------------------------
00101 TUint16 CTZLocalizerEngine::GetCityTimeZoneId(CTzLocalizedCity *aLocalizedCity)
00102 {
00103     return aLocalizedCity->TimeZoneId();
00104 }
00105 
00106 // ---------------------------------------------------------
00107 // CTZLocalizerEngine::GetCityGroupId(CTzLocalizedCity *aLocalizedCity)
00108 // Get current city group id.
00109 // ---------------------------------------------------------
00110 TUint8 CTZLocalizerEngine::GetCityGroupId(CTzLocalizedCity *aLocalizedCity)
00111 {
00112     return aLocalizedCity->GroupId();
00113 }
00114 
00115 // ---------------------------------------------------------
00116 // CTZLocalizerEngine::GetCityLocalTime()
00117 // Convert universal time to local time.
00118 // ---------------------------------------------------------
00119 HBufC* CTZLocalizerEngine::GetCityLocalTimeL(CTzLocalizedCity *aLocalizedCity)
00120     {   
00121     RTz tzServer;
00122     User::LeaveIfError(tzServer.Connect());
00123     CleanupClosePushL(tzServer);
00124     
00125     TBuf<KBufSize> dateTimeString;
00126     TTime time;
00127     time.HomeTime();
00128     time.FormatL(dateTimeString, KDateTimeString);
00129     // this is a 2 stage process - first convert the date/time to UTC.
00130     // No need to specify the time zone because 
00131     // this function uses the current system time zone.
00132     TInt results=tzServer.ConvertToUniversalTime(time);
00133     
00134     //get the current time zone id
00135     CTzId* timezoneId = CTzId::NewL(GetCityTimeZoneId(aLocalizedCity));
00136     CleanupStack::PushL(timezoneId);
00137     // convert UTC time to local time
00138     results=tzServer.ConvertToLocalTime(time,*timezoneId);
00139     
00140     TBuf<KBufSize> timeZoneString;
00141     timeZoneString.Copy(timezoneId->TimeZoneNameID());
00142     time.FormatL(dateTimeString, KDateTimeString);
00143         
00144     CleanupStack::PopAndDestroy(timezoneId);
00145     
00146     HBufC* temp = HBufC::NewL(dateTimeString.Size());
00147     (*temp)= dateTimeString;
00148     
00149     CleanupStack::PopAndDestroy(1); // causes tzServer.Close() to be called
00150 
00151     return temp;        
00152     }
00153 
00154 // ---------------------------------------------------------
00155 // CTZLocalizerEngine::FindCityL()
00156 // Find the specific city.
00157 // return localizedcity
00158 // ---------------------------------------------------------
00159 CTzLocalizedCity* CTZLocalizerEngine::FindCityL(const TDesC& aCityName )
00160     {
00161     CTzLocalizer* loc = CTzLocalizer::NewL(); 
00162     CleanupStack::PushL(loc); 
00163 
00164     CTzLocalizedCity* localizedCity = loc->FindCityByNameL(aCityName);
00165     
00166     CleanupStack::PopAndDestroy(loc); 
00167 
00168     return localizedCity;   
00169     }
00170     
00171 // ---------------------------------------------------------
00172 //  CTZLocalizerEngine::GetAllTimeZonesL()
00173 //  Fetch the valid time zone ID's
00174 // ---------------------------------------------------------
00175 CDesC16ArrayFlat* CTZLocalizerEngine::GetAllTimeZonesL()
00176     {
00177     CTzLocalizer* loc = CTzLocalizer::NewL(); 
00178     CleanupStack::PushL(loc); 
00179 
00180     CTzLocalizedCityArray* locCityArray = 
00181         loc->GetCitiesL(loc->ETzUTCAscending);
00182     CleanupStack::PushL(locCityArray); 
00183     
00184     CDesC16ArrayFlat* timeZones = 
00185         new (ELeave)CDesC16ArrayFlat( locCityArray->Count() );
00186         
00187     TBuf<KBufSize> temp;
00188     TInt result;
00189     TInt k = 0;
00190     for(TInt i=0; i<locCityArray->Count();i++)
00191         {
00192         result = 0;
00193         //get the time zone id
00194         temp.Num((locCityArray->At(i)).TimeZoneId());
00195         result = timeZones->Find(temp, k);
00196         //add if not allready exist
00197         if(result!=0)
00198             {
00199             timeZones->AppendL(temp);
00200             }           
00201         }
00202 
00203     timeZones->Sort();
00204 
00205     CleanupStack::PopAndDestroy(2); //loc and locCityArray  
00206     return timeZones;
00207     }    
00208     
00209 // ---------------------------------------------------------
00210 // CTZLocalizerEngine::GetAllGroupID()
00211 // Fetch the valid city group ID's
00212 // ---------------------------------------------------------
00213 CDesC16ArrayFlat* CTZLocalizerEngine::GetAllGroupIDL()
00214     {
00215     CTzLocalizer* loc = CTzLocalizer::NewL(); 
00216     CleanupStack::PushL(loc); 
00217 
00218     CTzLocalizedCityArray* locCityArray = 
00219         loc->GetCitiesL(loc->ETzUTCAscending);
00220     CleanupStack::PushL(locCityArray); 
00221     
00222     CDesC16ArrayFlat* groupIDs = 
00223         new (ELeave)CDesC16ArrayFlat( locCityArray->Count() );
00224         
00225     TBuf<KBufSize> temp;
00226     TInt result;
00227     TInt k = 0;
00228     for(TInt i=0; i<locCityArray->Count();i++)
00229         {
00230         result = 0;
00231         //get the group id
00232         temp.Num((locCityArray->At(i)).GroupId());
00233         result = groupIDs->Find(temp, k);
00234         //add if not allready exist
00235         if(result!=0)
00236             {
00237             groupIDs->AppendL(temp);    
00238             }       
00239         }
00240         
00241     groupIDs->Sort();
00242 
00243     CleanupStack::PopAndDestroy(2); //loc and locCityArray  
00244     return groupIDs;
00245     }      
00246 
00247 // ---------------------------------------------------------
00248 // CDesC16ArrayFlat* CTZLocalizerEngine::GetAllCitiesL()
00249 // Fetch the localized cities
00250 // ---------------------------------------------------------
00251 CDesC16ArrayFlat* CTZLocalizerEngine::GetAllCitiesL()
00252     {
00253     CTzLocalizer* loc = CTzLocalizer::NewL(); 
00254     CleanupStack::PushL(loc); 
00255 
00256     CTzLocalizedCityArray* locCityArray = 
00257         loc->GetCitiesL(loc->ETzAlphaNameAscending);
00258     CleanupStack::PushL(locCityArray); 
00259     
00260     CDesC16ArrayFlat* locCity = 
00261         new (ELeave)CDesC16ArrayFlat( locCityArray->Count() );
00262 
00263     for(TInt i=0; i<locCityArray->Count();i++)
00264         {
00265         locCity->AppendL((locCityArray->At(i)).Name()); 
00266         }   
00267 
00268     CleanupStack::PopAndDestroy(2); //loc and locCityArray
00269 
00270     return locCity;
00271     }
00272 
00273 
00274 // ---------------------------------------------------------
00275 //  CTZLocalizerEngine::AddCityL()
00276 //  Add the city in database.
00277 //  return localized city
00278 // ---------------------------------------------------------    
00279 CTzLocalizedCity* CTZLocalizerEngine::AddCityL(TInt aTimeZoneId, 
00280     const TDesC &aCityName, TInt aGroupId)
00281 {
00282     CTzLocalizer* loc = CTzLocalizer::NewL(); 
00283     CleanupStack::PushL(loc);
00284     CTzLocalizedCity* localizedCity = NULL;
00285     TRAPD(error,localizedCity = loc->AddCityL(aTimeZoneId, aCityName, aGroupId));
00286     
00287     if(error!=KErrNone)
00288         {
00289         localizedCity = NULL;
00290         }
00291     CleanupStack::PopAndDestroy(loc); 
00292     
00293     return localizedCity;
00294 }
00295 
00296 
00297 // ---------------------------------------------------------
00298 // CTZLocalizerEngine::FindCitiesInGroupL()
00299 // Find the cities with same group id.
00300 // ---------------------------------------------------------
00301 CDesC16ArrayFlat* CTZLocalizerEngine::FindCitiesInGroupL( TInt aGroupID )
00302     {   
00303     CTzLocalizer* loc = CTzLocalizer::NewL(); 
00304     CleanupStack::PushL(loc); 
00305     
00306     CTzLocalizedCityArray* locCityArray
00307         = loc->GetCitiesInGroupL(aGroupID, loc->ETzAlphaNameAscending);
00308     CleanupStack::PushL(locCityArray); 
00309     
00310     CDesC16ArrayFlat* locCity = 
00311         new (ELeave)CDesC16ArrayFlat( locCityArray->Count() );
00312 
00313     TBuf<KBufSize> temp;
00314 
00315     for(TInt i=0; i<locCityArray->Count();i++)
00316         {
00317         temp.Copy( KTab );
00318         temp.Append((locCityArray->At(i)).Name());
00319         temp.Append( KTab );
00320         temp.Append( KEmpty );
00321         locCity->AppendL(temp);
00322         }   
00323 
00324     CleanupStack::PopAndDestroy(2); //loc and locCityArray
00325 
00326     return locCity;
00327     }
00328 
00329 // ---------------------------------------------------------
00330 // CTZLocalizerEngine::RemoveCityL()
00331 // Removes a city from the user defined city collection 
00332 // ---------------------------------------------------------
00333 void CTZLocalizerEngine::RemoveCityL(const TDesC& aCityName )
00334     {
00335     CTzLocalizer* loc = CTzLocalizer::NewL(); 
00336     CleanupStack::PushL(loc); 
00337     CTzLocalizedCity* localizedCity = FindCityL(aCityName);
00338     //Remove a city from the user defined collection
00339     loc->RemoveCityL(localizedCity);
00340     CleanupStack::PopAndDestroy(loc);
00341     }
00342 
00343 //EOF

Generated by  doxygen 1.6.2