calendarui/regionalplugins/calenregionalutil/src/calenlunarlocalizer.cpp
changeset 0 f979ecb2b13e
child 5 42814f902fe6
child 18 c198609911f9
equal deleted inserted replaced
-1:000000000000 0:f979ecb2b13e
       
     1 /*
       
     2 * Copyright (c) 2007 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:   Calendar Lunar Plugin 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "calendarui_debug.h"
       
    20 
       
    21 #include "calenlunarlocalizer.h"
       
    22 
       
    23 #include "calenlunarinfo.h"
       
    24 #include "calenlunarlocalizedinfo.h"
       
    25 #include "calenextrarowformatter.h"
       
    26 #include <calenregionalutil.rsg>
       
    27 
       
    28 #include <AknBidiTextUtils.h>
       
    29 #include <avkon.rsg>
       
    30 #include <badesca.h> 
       
    31 #include <eikenv.h>
       
    32 #include <StringLoader.h>
       
    33 #include <bautils.h>
       
    34 #include <data_caging_path_literals.hrh>
       
    35 
       
    36 //CONSTANTS
       
    37 const TInt KCharMonth = 0x6708;
       
    38 const TInt KCharFirstMonth = 0x6b63;
       
    39 const TInt KCharChu = 0x521d;
       
    40 const TInt KCharTen = 0x5341;
       
    41 const TInt KCharTwenty= 0x5eff;
       
    42 _LIT(KChineseDigits, 
       
    43      "\x0030\x4e00\x4e8c\x4e09\x56db\x4e94\x516d\x4e03\x516b\x4e5d");
       
    44 
       
    45 _LIT( KResourceChinesePluginFile, "calenregionalutil.rsc" );
       
    46 #define KResourcePath KDC_RESOURCE_FILES_DIR
       
    47 
       
    48 
       
    49 // -----------------------------------------------------------------------------
       
    50 // CCalenLunarLocalizer::NewL
       
    51 // -----------------------------------------------------------------------------
       
    52 //
       
    53 EXPORT_C CCalenLunarLocalizer* CCalenLunarLocalizer::NewL()
       
    54     {
       
    55     TRACE_ENTRY_POINT;
       
    56     
       
    57     CCalenLunarLocalizer* self = new (ELeave) CCalenLunarLocalizer;
       
    58     CleanupStack::PushL(self);
       
    59     self->ConstructL();
       
    60     CleanupStack::Pop(self);
       
    61     
       
    62     TRACE_EXIT_POINT;
       
    63     return self;
       
    64     }
       
    65 
       
    66 // -----------------------------------------------------------------------------
       
    67 // CCalenLunarLocalizer::~CCalenLunarLocalizer
       
    68 // -----------------------------------------------------------------------------
       
    69 //
       
    70 EXPORT_C CCalenLunarLocalizer::~CCalenLunarLocalizer()
       
    71     {
       
    72     TRACE_ENTRY_POINT;
       
    73     
       
    74     delete iFestivalNames;
       
    75     delete iSolarTermNames;
       
    76     delete iHeavenlyStemNames;
       
    77     delete iTerrestialBranchNames;
       
    78     delete iAnimalYearNames;
       
    79     delete iGregorianDateFormat;
       
    80     delete iRowFormatter;
       
    81     
       
    82     if( iResourceFileOffset )
       
    83         {
       
    84         CCoeEnv::Static()->DeleteResourceFile( iResourceFileOffset );
       
    85         }
       
    86     
       
    87     TRACE_EXIT_POINT;
       
    88     }
       
    89 
       
    90 // -----------------------------------------------------------------------------
       
    91 // CCalenLunarLocalizer::CCalenLunarLocalizer
       
    92 // -----------------------------------------------------------------------------
       
    93 //
       
    94 EXPORT_C CCalenLunarLocalizer::CCalenLunarLocalizer() : iEikEnv( CEikonEnv::Static() )
       
    95     {
       
    96     TRACE_ENTRY_POINT;
       
    97     TRACE_EXIT_POINT;
       
    98     }
       
    99 
       
   100 // -----------------------------------------------------------------------------
       
   101 // CCalenLunarLocalizer::ConstructL
       
   102 // -----------------------------------------------------------------------------
       
   103 //
       
   104 EXPORT_C void CCalenLunarLocalizer::ConstructL()
       
   105     {
       
   106     TRACE_ENTRY_POINT;
       
   107     iRowFormatter = CCalenExtraRowFormatter::NewL();
       
   108     
       
   109     TFileName dllName;
       
   110     // Get the complate path of the DLL from where it is currently loaded
       
   111     Dll::FileName( dllName );
       
   112     
       
   113     TFileName resourceFilename;
       
   114     resourceFilename.Append(dllName.Mid(0,2));
       
   115     resourceFilename.Append(KResourcePath);
       
   116     resourceFilename.Append(KResourceChinesePluginFile);
       
   117     BaflUtils::NearestLanguageFile( CEikonEnv::Static()->FsSession(), resourceFilename );
       
   118     // Add the resource file.
       
   119     iResourceFileOffset = CEikonEnv::Static()->AddResourceFileL( resourceFilename );
       
   120     
       
   121 
       
   122     iFestivalNames = iEikEnv->ReadDesCArrayResourceL(R_CALEN_CHI_FESTIVALS);
       
   123     iSolarTermNames = iEikEnv->ReadDesCArrayResourceL(R_CALEN_CHI_SOLAR_ITEMS);
       
   124     iHeavenlyStemNames = iEikEnv->ReadDesCArrayResourceL(R_CALEN_CHI_HEAV_STEMS);
       
   125     iTerrestialBranchNames = iEikEnv->ReadDesCArrayResourceL(R_CALEN_CHI_TERR_BRANCHES);
       
   126     iAnimalYearNames = iEikEnv->ReadDesCArrayResourceL(R_CALEN_CHI_ANIMAL_YEARS);
       
   127 
       
   128     iGregorianDateFormat = StringLoader::LoadL(R_QTN_DATE_USUAL_WITH_ZERO);
       
   129 
       
   130 
       
   131 #ifdef _DEBUG
       
   132     // Validate assumptions
       
   133     TInt maxStem = -1;
       
   134     for (TInt i = 0; i < iHeavenlyStemNames->Count(); i++)
       
   135         {
       
   136         maxStem = Max( maxStem, iHeavenlyStemNames->MdcaPoint(i).Length() );
       
   137         }
       
   138     
       
   139     TInt maxBranch = -1;
       
   140     for (TInt i = 0; i < iTerrestialBranchNames->MdcaPoint(i).Length() ; i++)
       
   141         {
       
   142         maxBranch = Max( maxBranch, iTerrestialBranchNames->MdcaPoint(i).Length() );        
       
   143         }
       
   144     ASSERT( maxStem + maxBranch <= 100);
       
   145 #endif
       
   146     
       
   147     TRACE_EXIT_POINT;
       
   148     }
       
   149 
       
   150 // -----------------------------------------------------------------------------
       
   151 // CCalenLunarLocalizer::LocalizeL
       
   152 // -----------------------------------------------------------------------------
       
   153 //
       
   154 EXPORT_C CCalenLunarLocalizedInfo* CCalenLunarLocalizer::LocalizeL( TCalenLunarInfo& aInfo )
       
   155     {
       
   156     TRACE_ENTRY_POINT;
       
   157    
       
   158     
       
   159     CCalenLunarLocalizedInfo* localized = CCalenLunarLocalizedInfo::NewL();
       
   160     CleanupStack::PushL( localized );
       
   161 
       
   162     if ( aInfo.HasFestival() ) 
       
   163         {
       
   164         localized->iFestival.Set( iFestivalNames->MdcaPoint( aInfo.iFestival ) );
       
   165         }
       
   166     
       
   167     if ( aInfo.HasSolarTerm() ) 
       
   168         {
       
   169         localized->iSolarTerm.Set( iSolarTermNames->MdcaPoint( aInfo.iSolarTerm ) );
       
   170         }
       
   171 
       
   172     // Animal year
       
   173     localized->iAnimalYear.Set( iAnimalYearNames->MdcaPoint( aInfo.AnimalYear() ) );
       
   174     
       
   175 
       
   176     // Lunar year    
       
   177     CPtrCArray* yearSubs = new (ELeave) CPtrCArray(2);
       
   178     CleanupStack::PushL( yearSubs );
       
   179     yearSubs->AppendL( iHeavenlyStemNames->MdcaPoint( aInfo.iHeavenlyStem ) );
       
   180     yearSubs->AppendL( iTerrestialBranchNames->MdcaPoint( aInfo.iTerrestialBranch ) );
       
   181 
       
   182     HBufC* tmp = StringLoader::LoadLC( R_CALE_LUNAR_YEAR, *yearSubs, iEikEnv );
       
   183     RDebug::Print( *tmp );
       
   184     localized->iLunarYear = *tmp;
       
   185     CleanupStack::PopAndDestroy( tmp );
       
   186     CleanupStack::PopAndDestroy( yearSubs );
       
   187 
       
   188     RDebug::Print(     localized->iLunarYear  );
       
   189 
       
   190     LocalizeMonthAndDayL(localized, aInfo);
       
   191     
       
   192     // Full lunar date
       
   193     CPtrCArray* subs = new (ELeave) CPtrCArray(2);
       
   194     CleanupStack::PushL( subs );
       
   195     subs->AppendL( localized->LunarMonthAndDay() );
       
   196     subs->AppendL( localized->LunarYear() );
       
   197     
       
   198     tmp = StringLoader::LoadLC( R_CALE_LUNAR_FULL_DATE, *subs );
       
   199     localized->iFullLunarDate = *tmp;
       
   200     CleanupStack::PopAndDestroy( tmp );
       
   201     CleanupStack::PopAndDestroy( subs );
       
   202     
       
   203     
       
   204     aInfo.iGregorianDate.FormatL( localized->iGregorianDate, 
       
   205                                  *iGregorianDateFormat );
       
   206     CleanupStack::Pop( localized );
       
   207     
       
   208     
       
   209     
       
   210     TRACE_EXIT_POINT;
       
   211     return localized;
       
   212     }
       
   213 
       
   214 
       
   215 // -----------------------------------------------------------------------------
       
   216 // CCalenLunarLocalizer::GetExtraRowTextL
       
   217 // -----------------------------------------------------------------------------
       
   218 //
       
   219 EXPORT_C TPtrC CCalenLunarLocalizer::GetExtraRowTextL( CCalenLunarLocalizedInfo& aLocInfo, TInt aMaxWidth, 
       
   220                                               const CFont& aFont, TBool aTwoLines )
       
   221     {
       
   222     TRACE_ENTRY_POINT;
       
   223     
       
   224     RArray<CCalenLunarLocalizedInfo::TField> fields;
       
   225     CleanupClosePushL( fields );
       
   226     fields.AppendL( CCalenLunarLocalizedInfo::EFestival );
       
   227     fields.AppendL( CCalenLunarLocalizedInfo::ESolarTerm );
       
   228     fields.AppendL( CCalenLunarLocalizedInfo::ELunarMonthAndDay );
       
   229     fields.AppendL( CCalenLunarLocalizedInfo::EAnimalYear );
       
   230     fields.AppendL( CCalenLunarLocalizedInfo::ELunarYear );
       
   231     
       
   232     TPtrC text = iRowFormatter->FormatExtraRowInformationL( aLocInfo, fields, aMaxWidth, aFont
       
   233 
       
   234                                                             , aTwoLines 
       
   235 
       
   236                                                             );
       
   237     CleanupStack::PopAndDestroy( &fields );
       
   238     
       
   239     
       
   240     TRACE_EXIT_POINT;
       
   241     return text;
       
   242     }
       
   243 
       
   244 // -----------------------------------------------------------------------------
       
   245 // CCalenLunarLocalizer::LocalizeMonthAndDayL
       
   246 // -----------------------------------------------------------------------------
       
   247 //
       
   248 void CCalenLunarLocalizer::LocalizeMonthAndDayL(CCalenLunarLocalizedInfo* aLocInfo,
       
   249                                                 TCalenLunarInfo& aInfo)
       
   250     {
       
   251     // Lunar Month and Day
       
   252     TInt dateResource = aInfo.iLunarDate.iLeapMonth ? R_CALE_LUNAR_LEAP_DATE : R_CALE_LUNAR_DATE;
       
   253     TBuf<10> month;
       
   254     TBuf<10> day;
       
   255     GetChineseMonth(aInfo.iLunarDate, month);
       
   256     GetChineseDay(aInfo.iLunarDate, day);
       
   257 
       
   258     CPtrCArray* monthAndDaySubs = new (ELeave) CPtrCArray(2);
       
   259     CleanupStack::PushL( monthAndDaySubs );
       
   260     monthAndDaySubs->AppendL(month);
       
   261     monthAndDaySubs->AppendL(day);
       
   262     
       
   263     HBufC* tmp = StringLoader::LoadLC( dateResource, *monthAndDaySubs );
       
   264     aLocInfo->iLunarMonthAndDay = *tmp;
       
   265     CleanupStack::PopAndDestroy( tmp );
       
   266     CleanupStack::PopAndDestroy( monthAndDaySubs );
       
   267     }
       
   268 
       
   269 // -----------------------------------------------------------------------------
       
   270 // CCalenLunarLocalizer::GetChineseMonth
       
   271 // -----------------------------------------------------------------------------
       
   272 //
       
   273 void CCalenLunarLocalizer::GetChineseMonth(const TChineseDate& aChineseDate,
       
   274                                            TDes& aDes)
       
   275     {
       
   276     TRACE_ENTRY_POINT;
       
   277 
       
   278     if (aChineseDate.iMonth == 1)
       
   279         {
       
   280         aDes.Append(KCharFirstMonth); // Special character for first month
       
   281         }
       
   282     else
       
   283         {
       
   284         GetChineseDigits(aChineseDate.iMonth, aDes);
       
   285         }
       
   286     
       
   287     aDes.Append(KCharMonth);
       
   288     
       
   289     TRACE_EXIT_POINT;
       
   290     }
       
   291     
       
   292 // -----------------------------------------------------------------------------
       
   293 // CCalenLunarLocalizer::GetChineseDay
       
   294 // -----------------------------------------------------------------------------
       
   295 //
       
   296 void CCalenLunarLocalizer::GetChineseDay(const TChineseDate& aChineseDate,
       
   297                                          TDes& aDes)
       
   298     {
       
   299     TRACE_ENTRY_POINT;
       
   300     
       
   301     if (aChineseDate.iDay <= 10) // day
       
   302         {
       
   303         aDes.Append(KCharChu); 
       
   304         }
       
   305     
       
   306     GetChineseDigits(aChineseDate.iDay, aDes);
       
   307     
       
   308     TRACE_EXIT_POINT;
       
   309     }
       
   310 
       
   311 // -----------------------------------------------------------------------------
       
   312 // CCalenLunarLocalizer::GetChineseDigits
       
   313 // -----------------------------------------------------------------------------
       
   314 //
       
   315 void CCalenLunarLocalizer::GetChineseDigits(TInt aNum, TDes& aDes)
       
   316     {
       
   317     TRACE_ENTRY_POINT;
       
   318     
       
   319     ASSERT( 0 < aNum && aNum <= 30 );
       
   320     TInt tens = aNum / 10;
       
   321     TInt ones = aNum % 10;
       
   322     if (aNum < 10) 
       
   323         {
       
   324         aDes.Append((KChineseDigits())[ones]);
       
   325         }
       
   326     else if (10 == aNum)
       
   327         {
       
   328         aDes.Append(KCharTen);
       
   329         }
       
   330     else if (10 < aNum && aNum < 20)
       
   331         {
       
   332         aDes.Append(KCharTen);
       
   333         aDes.Append((KChineseDigits())[ones]);
       
   334         }
       
   335     else if (20 == aNum)
       
   336         {
       
   337         aDes.Append((KChineseDigits())[tens]);
       
   338         aDes.Append(KCharTen);
       
   339         }
       
   340     else if (20 < aNum && aNum < 30)
       
   341         {
       
   342         aDes.Append(KCharTwenty);
       
   343         aDes.Append((KChineseDigits())[ones]);
       
   344         }
       
   345     else if (30 == aNum)
       
   346         {
       
   347         aDes.Append((KChineseDigits())[tens]);
       
   348         aDes.Append(KCharTen);
       
   349         }
       
   350     else 
       
   351         {
       
   352         __ASSERT_DEBUG(EFalse, User::Invariant());
       
   353         }
       
   354     
       
   355     TRACE_EXIT_POINT;
       
   356     }
       
   357 
       
   358 //EOF
       
   359 
       
   360