uifw/AvKon/src/AknQueryValueTime.cpp
changeset 0 2f259fa3e83a
equal deleted inserted replaced
-1:000000000000 0:2f259fa3e83a
       
     1 /*
       
     2 * Copyright (c) 2002 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: 
       
    15 *
       
    16 */
       
    17 
       
    18 // AknQueryValueTime.cpp
       
    19 //
       
    20 // Copyright (c) 2001 Symbian Ltd.  All rights reserved.
       
    21 //
       
    22 
       
    23 #include "eikenv.h"
       
    24 
       
    25 #include <eikmfne.h>
       
    26 #include <avkon.hrh>
       
    27 #include <avkon.rsg>
       
    28 #include "AknQueryValueTime.h"
       
    29 #include "AknQueryDialog.h"
       
    30 #include "aknmfnesettingpage.h"
       
    31 
       
    32 #include "AknPanic.h"
       
    33 
       
    34 //-------------------------------------------------
       
    35 // class CAknQueryValueTime
       
    36 //-------------------------------------------------
       
    37 
       
    38 /**
       
    39  * First stage of two stage construction.
       
    40  */
       
    41 EXPORT_C CAknQueryValueTime* CAknQueryValueTime::NewL()
       
    42 	{
       
    43 	CAknQueryValueTime* self = NewLC();
       
    44 	CleanupStack::Pop();
       
    45 	return self;
       
    46 	}
       
    47 
       
    48 /**
       
    49  * First stage of two stage construction.
       
    50  */
       
    51 EXPORT_C CAknQueryValueTime* CAknQueryValueTime::NewLC()
       
    52 	{
       
    53 	CAknQueryValueTime* self = new(ELeave) CAknQueryValueTime;
       
    54 	CleanupStack::PushL(self);
       
    55 	self->ConstructL();
       
    56 	return self;
       
    57 	}
       
    58 
       
    59 /**
       
    60  * Destructor.
       
    61  */
       
    62 EXPORT_C CAknQueryValueTime::~CAknQueryValueTime()
       
    63 	{
       
    64 	}
       
    65 
       
    66 /**
       
    67  * Set the array.
       
    68  * 
       
    69  * @param aArray pointer to array, ownership is not passed
       
    70  */
       
    71 EXPORT_C void CAknQueryValueTime::SetArrayL(const CAknQueryValueTimeArray* aArray)
       
    72 	{
       
    73 	iArray = aArray;
       
    74 	}
       
    75 
       
    76 /**
       
    77  * Set the string used for the query caption to be a string other than the default.
       
    78  *
       
    79  * @param aResourceId		resource id of string to use for caption.
       
    80  *
       
    81  */
       
    82 EXPORT_C void CAknQueryValueTime::SetQueryCaption(TInt aResourceId)
       
    83 	{
       
    84 	iQueryCaptionId = aResourceId;
       
    85 	}
       
    86 
       
    87 /**
       
    88  * Return the current value, which may have been set by the user
       
    89  *
       
    90  * @return The current value
       
    91  *
       
    92  */
       
    93 EXPORT_C TTime CAknQueryValueTime::Value() const
       
    94 	{
       
    95 	return iTime;
       
    96 	}
       
    97 
       
    98 /**
       
    99  * Returns the array as a descriptor array
       
   100  * 
       
   101  * @return descriptor array, ownership is not passed
       
   102  */
       
   103 EXPORT_C const MDesCArray* CAknQueryValueTime::MdcArray() const
       
   104 	{
       
   105 	return iArray;
       
   106 	}
       
   107 
       
   108 /**
       
   109  * Returns the current value as text
       
   110  * 
       
   111  * @return	descriptor representing current value, new 
       
   112  * descriptor is created and left on cleanup stack, ownership passed back to client
       
   113  */
       
   114 EXPORT_C HBufC* CAknQueryValueTime::CurrentValueTextLC()
       
   115 	{
       
   116 	HBufC* buf = HBufC::NewMaxLC(iArray->FormattedStringSize());
       
   117 	TPtr ptr = buf->Des();
       
   118 #ifndef _DEBUG
       
   119 	iTime.FormatL(ptr, *(iArray->FormatString()));
       
   120 #else
       
   121 	TRAPD(err, iTime.FormatL(ptr, *(iArray->FormatString())));
       
   122 	if(err != KErrNone)
       
   123 		Panic(EAknPanicQueryTimeArrayTimeFormatLeave);
       
   124 #endif
       
   125     AknTextUtils::DisplayTextLanguageSpecificNumberConversion( ptr );
       
   126 	return buf;
       
   127 	}
       
   128 
       
   129 /**
       
   130  * Returns the index in the array of the current value. 
       
   131  * If there are duplicates, returns the index of the first match.
       
   132  * If there are no matches, returns zero;
       
   133  *
       
   134  * @return index in array of current value
       
   135  */
       
   136 EXPORT_C TInt CAknQueryValueTime::CurrentValueIndex() const
       
   137 	{
       
   138 	return iCurrentIndex;
       
   139 	}
       
   140 
       
   141 /**
       
   142  * Changes the current value to correspond to a value in the array.
       
   143  *
       
   144  * @param aIndex	index in array of value to set as current
       
   145  */
       
   146 EXPORT_C void CAknQueryValueTime::SetCurrentValueIndex(const TInt aIndex)
       
   147 	{
       
   148 	iCurrentIndex = aIndex;
       
   149 	const CAknQueryValueTimeArray::TimeArray* timeArray = iArray->Array();
       
   150 	if (IsValidIndex(aIndex))
       
   151 	    iTime = (*timeArray)[aIndex];
       
   152 	}
       
   153 
       
   154 /**
       
   155  * Creates a dialog containing a query control. If the value is edited and the Dialog
       
   156  * OK'd, the new value will be set as the current value. Otherwise the current value 
       
   157  * remains unchanged.
       
   158  *
       
   159  * @return	ETrue if current value was altered; EFalse otherwise
       
   160  */
       
   161 EXPORT_C TBool CAknQueryValueTime::CreateEditorL()
       
   162 	{
       
   163 	TBool result = EFalse;
       
   164 	TInt keyPressed;
       
   165 
       
   166 	HBufC* queryString = CEikonEnv::Static()->AllocReadResourceLC(iQueryCaptionId);
       
   167 
       
   168 	if ( iFlags.IsSet( ESettingPageModeBitIndex ) )
       
   169 		{
       
   170 		CAknTimeSettingPage* dlg =  new ( ELeave ) CAknTimeSettingPage ( queryString, 
       
   171 			EAknSettingPageNoOrdinalDisplayed, EEikCtTimeEditor,
       
   172 			R_AVKON_DEFAULT_SETTING_PAGE_TIME_EDITOR, 0, iTime );
       
   173 		result =  dlg->ExecuteLD( );
       
   174 
       
   175 		if ( result )
       
   176 			keyPressed = EAknSoftkeyOk;
       
   177 		else
       
   178 			keyPressed = EAknSoftkeyCancel;
       
   179 		}
       
   180 	else	
       
   181 		{
       
   182 		CAknTimeQueryDialog* dlg = CAknTimeQueryDialog::NewL(iTime);
       
   183 		keyPressed = dlg->ExecuteLD(R_AVKON_DIALOG_QUERY_VALUE_TIME,*queryString);
       
   184 		}
       
   185 
       
   186 	CleanupStack::PopAndDestroy(); // queryString
       
   187 
       
   188 	switch(keyPressed)
       
   189 		{
       
   190 	case EAknSoftkeyOk:
       
   191 		result = ETrue;
       
   192 		if ( iFlags.IsSet( EAutoAppendBitIndex ) )
       
   193 			{
       
   194 			AppendValueIfNewL();
       
   195 			}
       
   196 		CalculateCurrentIndex();
       
   197 		break;
       
   198 	case EAknSoftkeyCancel:
       
   199 		// fall through
       
   200 	case EAknSoftkeyBack:
       
   201 		// fall through
       
   202 	default:
       
   203 		result = EFalse;
       
   204 		break;
       
   205 		}
       
   206 
       
   207 	return result;
       
   208 	}
       
   209 
       
   210 /**
       
   211  * Two stage construction
       
   212  *
       
   213  */
       
   214 void CAknQueryValueTime::ConstructL()
       
   215 	{
       
   216 	iQueryCaptionId = R_AVKON_TBUF_POPUP_FIELD_DEFAULT_TIME_QUERY_PROMPT;
       
   217 	}
       
   218 
       
   219 /**
       
   220  * Constructor.
       
   221  *
       
   222  */
       
   223 CAknQueryValueTime::CAknQueryValueTime()
       
   224 	{
       
   225 	}
       
   226 
       
   227 /**
       
   228  * Search for the current time in the list, and set the curent index 
       
   229  * to be the found index. If not found, set current index to 1 after the size of the array.
       
   230  *
       
   231  */
       
   232 void CAknQueryValueTime::CalculateCurrentIndex()
       
   233 	{
       
   234 	// ensure that if the new value is not in the list, the current index is set to the beginning
       
   235 	TInt index = 0; 
       
   236 	const CAknQueryValueTimeArray::TimeArray* timeArray = iArray->Array();
       
   237 	TInt length = timeArray->Count();
       
   238 	// search through array to find match for current value
       
   239 	TBool match = EFalse;
       
   240 	for(TInt i = 0; i < length; i++)
       
   241 		{
       
   242 		if((*timeArray)[i] == iTime)
       
   243 			{
       
   244 			index = i;
       
   245 			match = ETrue;
       
   246 			break;
       
   247 			}
       
   248 		}
       
   249 	if(!match)
       
   250 		index = length;
       
   251 	iCurrentIndex = index;
       
   252 	}
       
   253 
       
   254 /**
       
   255  * Append the current value if it is new
       
   256  */
       
   257 void CAknQueryValueTime::AppendValueIfNewL()
       
   258 	{
       
   259 	const CAknQueryValueTimeArray::TimeArray* array = iArray->Array();
       
   260 	CalculateCurrentIndex();
       
   261 	if ( iCurrentIndex == array->Count() ) 
       
   262 		{
       
   263 		CAknQueryValueTimeArray::TimeArray* array = iArray->Array();
       
   264 		array->AppendL( iTime );
       
   265 		}
       
   266 	}
       
   267 
       
   268 EXPORT_C void CAknQueryValueTime::Reserved_MAknQueryValue()
       
   269 	{
       
   270 	};
       
   271 
       
   272 
       
   273 //-------------------------------------------------
       
   274 // class CAknQueryValueTimeArray
       
   275 //-------------------------------------------------
       
   276 
       
   277 /**
       
   278  * NewL.
       
   279  *
       
   280  * @param aResourceId	Recource id of a TBUF containing a TTime format string. Client
       
   281  *							can use R_AVKON_TBUF_POPUP_FIELD_DEFAULT_TIME_FORMAT
       
   282  *							if desired, or provide its own resource.
       
   283  */
       
   284 EXPORT_C CAknQueryValueTimeArray* CAknQueryValueTimeArray::NewL(TInt aResourceId)
       
   285 	{
       
   286 	CAknQueryValueTimeArray* self = NewLC(aResourceId);
       
   287 	CleanupStack::Pop();
       
   288 	return self;
       
   289 	}
       
   290 
       
   291 /**
       
   292  * NewLC.
       
   293  *
       
   294  * @param aResourceId	Recource id of a TBUF containing a TTime format string. Client
       
   295  *							can use R_AVKON_TBUF_POPUP_FIELD_DEFAULT_TIME_FORMAT
       
   296  *							if desired, or provide its own resource.
       
   297  */
       
   298 EXPORT_C CAknQueryValueTimeArray* CAknQueryValueTimeArray::NewLC(TInt aResourceId)
       
   299 	{
       
   300 	CAknQueryValueTimeArray* self = new(ELeave) CAknQueryValueTimeArray;
       
   301 	CleanupStack::PushL(self);
       
   302 	self->ConstructL(aResourceId);
       
   303 	return self;
       
   304 	}
       
   305 
       
   306 /**
       
   307  * Destructor.
       
   308  */
       
   309 EXPORT_C CAknQueryValueTimeArray::~CAknQueryValueTimeArray()
       
   310 	{
       
   311 	delete iLastGeneratedTextValue;
       
   312 	delete iFormatString;
       
   313 	}
       
   314 
       
   315 /**
       
   316  * Set the array of values. 
       
   317  * Note that client can use any implementation of array class, but must pass in a 
       
   318  * TArray generated from it (by calling the Array() method on the array class)
       
   319  *
       
   320  * @param array of values, ownership is not passed
       
   321  */
       
   322 EXPORT_C void CAknQueryValueTimeArray::SetArray(TimeArray& aArray)
       
   323 	{
       
   324 	iArray = &aArray;
       
   325 	}
       
   326 
       
   327 /**
       
   328  * Get the array of values as a TArray. 
       
   329  * Note that client can use any implementation of array class, but the array is 
       
   330  * treated as a TArray.
       
   331  *
       
   332  * @return array of values, ownership is not passed
       
   333  */
       
   334 EXPORT_C CAknQueryValueTimeArray::TimeArray* CAknQueryValueTimeArray::Array() const
       
   335 	{
       
   336 	return iArray;
       
   337 	}
       
   338 
       
   339 /**
       
   340  * Return the TTime format string that is used to generate MDesCArray values
       
   341  *
       
   342  * @return format string
       
   343  */
       
   344 EXPORT_C const HBufC* CAknQueryValueTimeArray::FormatString() const
       
   345 	{
       
   346 	return iFormatString;
       
   347 	}
       
   348 
       
   349 /**
       
   350  * Return the size of a maximal time string formated using the format string 
       
   351  * that was supplied during construction of this instance.
       
   352  * Two versions to eliminate compiler warnings.
       
   353  *
       
   354  * @return length of formatted string.
       
   355  */
       
   356 #ifdef __WINS__
       
   357 EXPORT_C const TInt CAknQueryValueTimeArray::FormattedStringSize() const
       
   358 	{
       
   359 	return iFormattedStringSize;
       
   360 	}
       
   361 #else
       
   362 EXPORT_C TInt CAknQueryValueTimeArray::FormattedStringSize() const
       
   363 	{
       
   364 	return iFormattedStringSize;
       
   365 	}
       
   366 #endif // __WINS__
       
   367 
       
   368 /**
       
   369  * Reports count of contained array
       
   370  *
       
   371  * @return count of contained array
       
   372  */
       
   373 EXPORT_C TInt CAknQueryValueTimeArray::MdcaCount() const
       
   374 	{
       
   375 	return iArray->Count();
       
   376 	}
       
   377 
       
   378 /**
       
   379  * Returns array element, converting value to text. 
       
   380  * <p> WARNING: the returned pointer is only valid until the next time this
       
   381  * method is invoked. 
       
   382  *
       
   383  * @param index of element to return
       
   384  * @return descriptor representing array element, ownership is not passed
       
   385  */
       
   386 EXPORT_C TPtrC CAknQueryValueTimeArray::MdcaPoint(TInt aIndex) const
       
   387 	{
       
   388 	// only storing the last used string saves memory by avoiding keeping an array 
       
   389 	// of descriptors in memory. 
       
   390 	TTime time = (*iArray)[aIndex];
       
   391 	TPtr textPtr = iLastGeneratedTextValue->Des();
       
   392 
       
   393 	// clear the last value in case of formatting failure
       
   394 	textPtr.Zero();
       
   395 
       
   396 	// we have set the size so that it should have enough room
       
   397 	TRAPD(err, time.FormatL(textPtr, *iFormatString));
       
   398 	if(err != KErrNone)
       
   399 		{
       
   400 #ifdef _DEBUG
       
   401 		// only other cause for leave is bad formatting, that must be 
       
   402 		// caught during debugging of application code
       
   403 		Panic(EAknPanicQueryTimeArrayTimeFormatLeave);
       
   404 #endif
       
   405 		}
       
   406     AknTextUtils::DisplayTextLanguageSpecificNumberConversion( textPtr );
       
   407 	return textPtr;
       
   408 	}
       
   409 
       
   410 /**
       
   411  * Constructor.
       
   412  */
       
   413 CAknQueryValueTimeArray::CAknQueryValueTimeArray()
       
   414 	{
       
   415 
       
   416 	}
       
   417 
       
   418 /**
       
   419  * ConstructL
       
   420  *
       
   421  * @param aResourceId	id of a resource containing a time format string
       
   422  */
       
   423 void CAknQueryValueTimeArray::ConstructL(TInt aResourceId)
       
   424 	{
       
   425 	iFormatString = CEikonEnv::Static()->AllocReadResourceL(aResourceId);
       
   426 
       
   427 	// work out size of a formatted time string, and use this for time string size
       
   428 	_LIT(KTime, "235959.999999"); // should use max characters, where e.g. 9am may not
       
   429 	TTime tempTime(KTime);
       
   430 	HBufC* tempString = HBufC::NewLC(KSafeSizeOfDescriptorForTTimeFormat);
       
   431 	TPtr ptr = tempString->Des();
       
   432 	tempTime.FormatL(ptr, *iFormatString);
       
   433 	iFormattedStringSize = tempString->Length()+KMaxAmPmName+1; // need to add 1 to this length as TTime needs it (this is a defect in TTime)
       
   434 	CleanupStack::PopAndDestroy(); // tempString
       
   435 
       
   436 	iLastGeneratedTextValue = HBufC::NewL(iFormattedStringSize);
       
   437 	}
       
   438 
       
   439 // End of File