photosgallery/viewframework/medialists/src/glxustringconverter.cpp
changeset 0 4e91876724a2
child 25 191387a8b767
equal deleted inserted replaced
-1:000000000000 0:4e91876724a2
       
     1 /*
       
     2 * Copyright (c) 2008-2009 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:    UString converter implementation class that converts symbian data 
       
    15 *                types to UString types
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 
       
    21 
       
    22 #include "glxustringconverter.h"
       
    23 #include "glxmediageneraldefs.h"
       
    24 
       
    25 #include <AknUtils.h> 
       
    26 #include <aknlocationed.h>      // CAknLocationEditor
       
    27 #include <avkon.rsg>
       
    28 #include <utf.h>                // CnvUtfConverter
       
    29 #include <osn/osnnew.h>             // for new(EMM) 
       
    30 #include <StringLoader.h>
       
    31 #include <glxtracer.h> 
       
    32 #include <glxmetadatadialog.rsg>
       
    33 #include <lbsposition.h>        // for TCoordinate
       
    34 #include <glxsettingsmodel.h>   // For Cenrep Keys
       
    35 
       
    36 const TInt KBytesInKB = 1024;
       
    37 const TInt KBytesInMB = 1024 * 1024;
       
    38 _LIT( KBlankText, " " );
       
    39 
       
    40 
       
    41 // -----------------------------------------------------------------------------
       
    42 // Constructor
       
    43 // -----------------------------------------------------------------------------
       
    44 //
       
    45 CGlxUStringConverter::CGlxUStringConverter()
       
    46     {
       
    47     TRACER("CGlxUStringConverter::CGlxUStringConverter()");
       
    48     
       
    49     }
       
    50 
       
    51 // -----------------------------------------------------------------------------
       
    52 // Two phase Constructor
       
    53 // -----------------------------------------------------------------------------
       
    54 //
       
    55 EXPORT_C CGlxUStringConverter* CGlxUStringConverter::NewL()
       
    56     {
       
    57     TRACER("CGlxUStringConverter::NewL");
       
    58     CGlxUStringConverter* self =  CGlxUStringConverter::NewLC();
       
    59     
       
    60     CleanupStack::Pop(self);
       
    61     return self;
       
    62     }
       
    63 
       
    64 // -----------------------------------------------------------------------------
       
    65 // Two phase Constructor
       
    66 // -----------------------------------------------------------------------------
       
    67 //   
       
    68 CGlxUStringConverter* CGlxUStringConverter::NewLC()
       
    69     {
       
    70     TRACER("CGlxUStringConverter::NewLC");
       
    71     CGlxUStringConverter* self = new (ELeave)  CGlxUStringConverter();
       
    72     CleanupStack::PushL(self );
       
    73     return self;
       
    74     }
       
    75 
       
    76 CGlxUStringConverter::~CGlxUStringConverter()
       
    77     {
       
    78     
       
    79     }
       
    80 // -----------------------------------------------------------------------------
       
    81 // Converts the TDesC into UString type
       
    82 // -----------------------------------------------------------------------------
       
    83 //  
       
    84 EXPORT_C void CGlxUStringConverter::AsStringL(const TGlxMedia& aMedia, const TMPXAttribute& aAttribute, TInt aFormatString, HBufC*& aString ) const
       
    85     {
       
    86     TRACER("CGlxUStringConverter::AsStringL");
       
    87     const CGlxMedia* media = aMedia.Properties();
       
    88     if( media )
       
    89         {
       
    90         switch( media->AttributeTypeId(aAttribute) )
       
    91     		{
       
    92     		case EMPXTypeText:
       
    93                 {
       
    94                 aString = ( media->ValueText(aAttribute) ).Alloc();
       
    95                 }
       
    96            break;
       
    97 
       
    98 
       
    99     		case EMPXTypeTObject:
       
   100     			{
       
   101     			if( aAttribute == KMPXMediaGeneralDate )
       
   102     				{
       
   103     				TTime date(0);
       
   104     				if( aFormatString == R_QTN_DATE_USUAL_WITH_ZERO )
       
   105                         {
       
   106                         HBufC* dateString = HBufC::NewLC(KMaxLongDateFormatSpec);
       
   107                         TPtr dateStringPtr (dateString->Des());
       
   108                         media->GetValueTObject(date,KMPXMediaGeneralDate);
       
   109                         HBufC* dateFormat = CCoeEnv::Static()->AllocReadResourceLC
       
   110                             ( R_QTN_DATE_USUAL_WITH_ZERO );
       
   111                         date.FormatL( dateStringPtr , *dateFormat );
       
   112                         CleanupStack::PopAndDestroy(dateFormat);
       
   113                         AknTextUtils::LanguageSpecificNumberConversion( dateStringPtr );
       
   114                         
       
   115                         HBufC* timeString = HBufC::NewLC(KMaxTimeFormatSpec);
       
   116                         TPtr timeStringPtr (timeString->Des());
       
   117                         media->GetValueTObject(date,KMPXMediaGeneralDate);
       
   118                         HBufC* timeFormat = CCoeEnv::Static()->AllocReadResourceLC
       
   119                              ( R_QTN_TIME_USUAL_WITH_ZERO );
       
   120                         date.FormatL(  timeStringPtr , *timeFormat );
       
   121                         CleanupStack::PopAndDestroy(timeFormat);
       
   122                         AknTextUtils::LanguageSpecificNumberConversion( timeStringPtr );
       
   123                          
       
   124                         HBufC* dateAndTime = HBufC::NewLC(dateStringPtr.Length()+timeStringPtr.Length()+1);
       
   125                         TPtr dateAndTimePtr (dateAndTime->Des());
       
   126                         dateAndTimePtr = dateStringPtr;
       
   127                         dateAndTimePtr.Append( KBlankText );
       
   128                         dateAndTimePtr.Append(timeStringPtr);
       
   129                         aString = dateAndTime->Alloc();                       
       
   130                         CleanupStack::PopAndDestroy(dateAndTime);
       
   131                         CleanupStack::PopAndDestroy(timeString);
       
   132                         CleanupStack::PopAndDestroy(dateString);
       
   133                         }
       
   134     				else if( aFormatString == R_QTN_TIME_USUAL_WITH_ZERO)
       
   135     					{
       
   136     					TBuf<20> timeString;
       
   137     					media->GetValueTObject(date,KMPXMediaGeneralDate);
       
   138     					HBufC* timeFormat = CCoeEnv::Static()->AllocReadResourceLC
       
   139     						( R_QTN_TIME_USUAL_WITH_ZERO );
       
   140     					
       
   141     					date.FormatL(  timeString , *timeFormat );
       
   142     					CleanupStack::PopAndDestroy(timeFormat);
       
   143     					AknTextUtils::LanguageSpecificNumberConversion( timeString );
       
   144     					aString = timeString.Alloc();
       
   145     					}
       
   146                     else if( aFormatString == R_QTN_DATE_USUAL)
       
   147                         {
       
   148                         TBuf<20> dateString;
       
   149                         media->GetValueTObject(date,KMPXMediaGeneralDate);
       
   150                         HBufC* dateFormat = CCoeEnv::Static()->AllocReadResourceLC
       
   151                             ( R_QTN_DATE_USUAL );
       
   152                         
       
   153                         date.FormatL(  dateString , *dateFormat );
       
   154                         CleanupStack::PopAndDestroy(dateFormat);
       
   155                         AknTextUtils::LanguageSpecificNumberConversion( dateString );
       
   156                         aString = dateString.Alloc();
       
   157                         }
       
   158 
       
   159     				}
       
   160     			else if( aAttribute == KMPXMediaGeneralSize )
       
   161     				{
       
   162                     GetFormattedItemSizeL(*media, aString);
       
   163     				}
       
   164     			else if( aAttribute == KGlxMediaGeneralLocation )
       
   165     				{
       
   166     				TCoordinate coordinate;
       
   167     				if(aMedia.GetCoordinate( coordinate ))
       
   168     					{
       
   169     					GetFormatLocationL(coordinate, aString);
       
   170     					}
       
   171     				else
       
   172     				    {
       
   173     				    HBufC* locationTextBuf = HBufC::NewLC(1); 
       
   174     				    TPtr ptr(locationTextBuf->Des());
       
   175     				    _LIT(KFormat," ");
       
   176      				    ptr.Append(KFormat);
       
   177     				    aString = locationTextBuf->Alloc();
       
   178     				    CleanupStack::PopAndDestroy(locationTextBuf);
       
   179     				    }
       
   180     				}
       
   181     			else if( aAttribute == KMPXMediaGeneralDuration )
       
   182     				{
       
   183     				TReal32 duration( 0 );
       
   184     				aMedia.GetDuration( duration );
       
   185     				TBuf<20> timeBuf(0);
       
   186     				if(duration)
       
   187     					{
       
   188     					GetFormattedDurationL(duration, aString);	
       
   189     					}
       
   190 				    }
       
   191     			else if( aAttribute == KGlxMediaGeneralDimensions )
       
   192     				{
       
   193     				TSize resolution;
       
   194     				if(aMedia.GetDimensions(resolution))
       
   195     					{
       
   196     					GetFormatResolutionL(resolution, aString);
       
   197     					}
       
   198     				}
       
   199     			else 
       
   200     				{
       
   201     				// no implementation
       
   202     				}
       
   203     			}
       
   204     		break;
       
   205     		default:
       
   206     		break;
       
   207     		}
       
   208         }
       
   209     }
       
   210  
       
   211 
       
   212 // ---------------------------------------------------------------------------
       
   213 // Returns the item size in the required format
       
   214 // ---------------------------------------------------------------------------
       
   215 //
       
   216 void CGlxUStringConverter::GetFormattedItemSizeL(const CGlxMedia& aMedia, HBufC*& aString) const
       
   217  	{
       
   218  	TRACER("CGlxUStringConverter::GetFormattedItemSizeL");
       
   219     TInt size(0);
       
   220     if(aMedia.GetValueTObject( size, KMPXMediaGeneralSize ))
       
   221         {
       
   222 		if(size >= KBytesInMB)
       
   223             {
       
   224             TInt mbSize = size / KBytesInMB ; // Size in MB
       
   225         	aString = StringLoader::LoadL(R_QTN_SIZE_MB, mbSize);
       
   226             }
       
   227         else if(size >= KBytesInKB)
       
   228             {
       
   229             aString = HBufC::NewL(64);
       
   230             TInt kBsize = size / KBytesInKB;  // bytes to kB
       
   231             HBufC* textSizeKb = CCoeEnv::Static()->AllocReadResourceLC( R_QTN_SIZE_KB );
       
   232             TPtr ptr = aString->Des();
       
   233             TPtr sizePtr = textSizeKb->Des();
       
   234             StringLoader::Format(ptr,sizePtr, -1, kBsize);
       
   235             CleanupStack::PopAndDestroy(textSizeKb);
       
   236             }
       
   237         else
       
   238 			{
       
   239 			HBufC* textSizeB = CCoeEnv::Static()->AllocReadResourceLC( R_QTN_SIZE_B );
       
   240 			aString = HBufC::NewL(64);
       
   241 			TPtr ptr = aString->Des();
       
   242             TPtr sizePtr = textSizeB->Des();
       
   243             StringLoader::Format(ptr, sizePtr, -1, size);
       
   244             CleanupStack::PopAndDestroy(textSizeB);
       
   245             }
       
   246         if(aString)
       
   247             {
       
   248             TPtr ptr = aString->Des();
       
   249             AknTextUtils::LanguageSpecificNumberConversion(ptr);
       
   250             }
       
   251         }
       
   252  	}
       
   253  
       
   254 // ---------------------------------------------------------------------------
       
   255 // Returns the item location in the required format
       
   256 // ---------------------------------------------------------------------------
       
   257 //	
       
   258 void CGlxUStringConverter::GetFormatLocationL(const TCoordinate& aCoordinate, HBufC*& aString ) const
       
   259 	{
       
   260 	TRACER("CGlxUStringConverter::GetFormatLocationL");
       
   261     CGlxSettingsModel* settingsModel = CGlxSettingsModel::InstanceL();
       
   262     CleanupClosePushL(*settingsModel);
       
   263     TBool showGeoCoordinates = settingsModel->ShowGeoCoordinatesInDisplay();
       
   264     CleanupStack::PopAndDestroy(settingsModel);  
       
   265 
       
   266 	if (showGeoCoordinates)
       
   267 		{
       
   268 		//Show geocoordinates
       
   269 		TPosition origin;
       
   270 		origin.SetCoordinate(aCoordinate.Latitude(),aCoordinate.Longitude());
       
   271 		HBufC* latBuf = CAknLocationEditor::DisplayableLocationL( origin, CAknLocationEditor::ELatitudeOnly );
       
   272 		HBufC* longBuf = CAknLocationEditor::DisplayableLocationL( origin, CAknLocationEditor::ELongitudeOnly );
       
   273 		HBufC* combinedLocationTextBuf = HBufC::NewLC( latBuf->Length() + longBuf->Length() + 2 ); 
       
   274 		TPtr ptr(combinedLocationTextBuf->Des());
       
   275 		_LIT(KFormat,", ");
       
   276 		ptr.Append(*latBuf);
       
   277 		ptr.Append(KFormat);
       
   278 		ptr.Append(*longBuf);
       
   279 	    aString = combinedLocationTextBuf->Alloc();
       
   280 		CleanupStack::PopAndDestroy(combinedLocationTextBuf);
       
   281 	    if( latBuf )
       
   282 	        {
       
   283 	        delete latBuf;
       
   284 	        latBuf = NULL;
       
   285 	        }
       
   286 	    if( longBuf )
       
   287 	        {
       
   288 	        delete longBuf;
       
   289 	        longBuf = NULL;
       
   290 	        }
       
   291 		}
       
   292 	else
       
   293 		{
       
   294 		//Hide geocoordinates
       
   295 		TBuf<KMaxInfoName> noLocationTextBuf;
       
   296 		noLocationTextBuf.AppendNum(0);
       
   297 		noLocationTextBuf.AppendNum(0);
       
   298 	    aString = noLocationTextBuf.Alloc();
       
   299         }
       
   300     }
       
   301     
       
   302 // ---------------------------------------------------------------------------
       
   303 // Returns the item duration in the required format
       
   304 // ---------------------------------------------------------------------------
       
   305 //
       
   306  void CGlxUStringConverter::GetFormattedDurationL(const TReal& aDuration , HBufC*& aString ) const
       
   307  	{
       
   308  	TRACER("CGlxUStringConverter::GetFormattedDurationL");
       
   309  	TInt resourceId = R_QTN_TIME_DURAT_LONG;
       
   310 	if( aDuration <= 3600 ) //60 seconds *60 minutes
       
   311 		{
       
   312 		// minute:seconds format
       
   313 		resourceId = R_QTN_TIME_DURAT_MIN_SEC;
       
   314 		}
       
   315 	
       
   316 	// This class does not have access to a CEikonEnv and hence 
       
   317 	// pls ignore the code scanner warning - Using CEikonEnv::Static
       
   318 	HBufC* timeFormat = CEikonEnv::Static()->AllocReadResourceLC( resourceId );
       
   319 
       
   320 	TTime time( 0 );
       
   321 	TBuf<20> timeBuf;
       
   322 	time += TTimeIntervalSeconds ( aDuration );
       
   323 	time.FormatL( timeBuf , *timeFormat );
       
   324     //to convert between arabic-indic digits and european digits.
       
   325     //based on existing language setting.
       
   326     AknTextUtils::LanguageSpecificNumberConversion(timeBuf);
       
   327 	CleanupStack::PopAndDestroy(timeFormat);
       
   328 	aString = timeBuf.Alloc();
       
   329  	}
       
   330 
       
   331 // ---------------------------------------------------------------------------
       
   332 // Returns the item resolution in the required format
       
   333 // ---------------------------------------------------------------------------
       
   334 //	
       
   335 void CGlxUStringConverter::GetFormatResolutionL(const TSize& aSize, HBufC*& aString  ) const
       
   336     {
       
   337     TRACER("CGlxUStringConverter::GetFormatResolutionL");
       
   338     CArrayFix<TInt>* array = new (ELeave) CArrayFixFlat<TInt>(2);
       
   339 	CleanupStack::PushL(array);
       
   340 	array->AppendL(aSize.iWidth);
       
   341 	array->AppendL(aSize.iHeight);
       
   342 	
       
   343 	//@todo: to be included when the metadata dialog's resource file is ready.
       
   344     HBufC* formatString = 
       
   345     StringLoader::LoadL(R_GLX_METADATA_VIEW_RESOLUTION_DETAIL_NSERIES,*array,CCoeEnv::Static());
       
   346 	CleanupStack::PopAndDestroy(array);
       
   347 	aString = formatString->Alloc();
       
   348 	if( formatString )
       
   349 	    {
       
   350     	delete formatString;
       
   351     	formatString = NULL;
       
   352 	    }
       
   353     }