uifw/AvKon/src/AknQueryValueDate.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 // AknQueryValueDate.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 "AknQueryValueDate.h"
       
    29 #include "AknQueryDialog.h"
       
    30 #include "aknmfnesettingpage.h"
       
    31 
       
    32 #include "AknPanic.h"
       
    33 
       
    34 //-------------------------------------------------
       
    35 // class CAknQueryValueDate
       
    36 //-------------------------------------------------
       
    37 
       
    38 /**
       
    39  * First stage of two stage construction.
       
    40  */
       
    41 EXPORT_C CAknQueryValueDate* CAknQueryValueDate::NewL()
       
    42 	{
       
    43 	CAknQueryValueDate* self = NewLC();
       
    44 	CleanupStack::Pop();
       
    45 	return self;
       
    46 	}
       
    47 
       
    48 /**
       
    49  * First stage of two stage construction
       
    50  */
       
    51 EXPORT_C CAknQueryValueDate* CAknQueryValueDate::NewLC()
       
    52 	{
       
    53 	CAknQueryValueDate* self = new(ELeave) CAknQueryValueDate;
       
    54 	CleanupStack::PushL(self);
       
    55 	self->ConstructL();
       
    56 	return self;
       
    57 	}
       
    58 
       
    59 /**
       
    60  * Destructor.
       
    61  */
       
    62 EXPORT_C CAknQueryValueDate::~CAknQueryValueDate()
       
    63 	{
       
    64 	}
       
    65 
       
    66 /**
       
    67  * Set the array.
       
    68  * 
       
    69  * @param aArray pointer to array, ownership is not passed
       
    70  */
       
    71 EXPORT_C void CAknQueryValueDate::SetArrayL(const CAknQueryValueDateArray* 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 CAknQueryValueDate::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 CAknQueryValueDate::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* CAknQueryValueDate::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* CAknQueryValueDate::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(EAknPanicQueryDateArrayTimeFormatLeave);
       
   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 CAknQueryValueDate::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 CAknQueryValueDate::SetCurrentValueIndex(const TInt aIndex)
       
   147 	{
       
   148 	iCurrentIndex = aIndex;
       
   149 	const CAknQueryValueDateArray::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 CAknQueryValueDate::CreateEditorL()
       
   162 	{
       
   163 	TBool result = EFalse;
       
   164 
       
   165 	HBufC* queryString = CEikonEnv::Static()->AllocReadResourceLC(iQueryCaptionId);
       
   166 	TInt keyPressed;
       
   167 
       
   168 	if ( iFlags.IsSet( ESettingPageModeBitIndex ) )
       
   169 		{
       
   170 		CAknDateSettingPage* dlg =  new ( ELeave ) CAknDateSettingPage ( queryString, 
       
   171 			EAknSettingPageNoOrdinalDisplayed, EEikCtDateEditor,
       
   172 			R_AVKON_DEFAULT_SETTING_PAGE_DATE_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 		dlg->PrepareLC(R_AVKON_DIALOG_QUERY_VALUE_DATE);
       
   184 		dlg->SetPromptL(*queryString);
       
   185 		keyPressed = dlg->RunLD();
       
   186 		}
       
   187 
       
   188 	CleanupStack::PopAndDestroy(); // queryString
       
   189 
       
   190 	switch(keyPressed)
       
   191 		{
       
   192 	case EAknSoftkeyOk:
       
   193 		result = ETrue;
       
   194 		if ( iFlags.IsSet( EAutoAppendBitIndex ) )
       
   195 			{
       
   196 			AppendValueIfNewL();
       
   197 			}
       
   198 		CalculateCurrentIndex();
       
   199 		break;
       
   200 	case EAknSoftkeyCancel:
       
   201 		// fall through
       
   202 	case EAknSoftkeyBack:
       
   203 		// fall through
       
   204 	default:
       
   205 		result = EFalse;
       
   206 		break;
       
   207 		}
       
   208 
       
   209 	return result;
       
   210 	}
       
   211 
       
   212 /**
       
   213  * Two stage construction
       
   214  *
       
   215  */
       
   216 void CAknQueryValueDate::ConstructL()
       
   217 	{
       
   218 	iQueryCaptionId = R_AVKON_TBUF_POPUP_FIELD_DEFAULT_DATE_QUERY_PROMPT;
       
   219 	}
       
   220 
       
   221 /**
       
   222  * Constructor. Does nothing.
       
   223  *
       
   224  */
       
   225 CAknQueryValueDate::CAknQueryValueDate()
       
   226 	{
       
   227 	}
       
   228 
       
   229 /**
       
   230  * Search for the current time in the list, and set the curent index 
       
   231  * to be the found index. If not found, set current index to 1 after the size of the array.
       
   232  *
       
   233  */
       
   234 void CAknQueryValueDate::CalculateCurrentIndex()
       
   235 	{
       
   236 	// ensure that if the new value is not in the list, the current index is set to the beginning
       
   237 	TInt index = 0; 
       
   238 	const CAknQueryValueDateArray::TimeArray* timeArray = iArray->Array();
       
   239 	TInt length = timeArray->Count();
       
   240 	// search through array to find match for current value
       
   241 	TBool match = EFalse;
       
   242 	for(TInt i = 0; i < length; i++)
       
   243 		{
       
   244 		if((*timeArray)[i] == iTime)
       
   245 			{
       
   246 			index = i;
       
   247 			match = ETrue;
       
   248 			break;
       
   249 			}
       
   250 		}
       
   251 	if(!match)
       
   252 		index = length;
       
   253 	iCurrentIndex = index;
       
   254 	}
       
   255 
       
   256 /**
       
   257  * Append the current value if it is new
       
   258  *
       
   259  *
       
   260  */
       
   261 void CAknQueryValueDate::AppendValueIfNewL()
       
   262 	{
       
   263 	const CAknQueryValueDateArray::TimeArray* array = iArray->Array();
       
   264 	CalculateCurrentIndex();
       
   265 	if ( iCurrentIndex == array->Count() ) 
       
   266 		{
       
   267 		CAknQueryValueDateArray::TimeArray* array = iArray->Array();
       
   268 		array->AppendL( iTime );
       
   269 		}
       
   270 	}
       
   271 
       
   272 EXPORT_C void CAknQueryValueDate::Reserved_MAknQueryValue()
       
   273 	{
       
   274 
       
   275 	};
       
   276 
       
   277 
       
   278 //-------------------------------------------------
       
   279 // class CAknQueryValueDateArray
       
   280 //-------------------------------------------------
       
   281 
       
   282 /**
       
   283  * NewL.
       
   284  *
       
   285  * @param aResourceId	Recource id of a TBUF containing a TTime format string. Client
       
   286  *							can use R_AVKON_TBUF_POPUP_FIELD_DEFAULT_DATE_FORMAT
       
   287  *							if desired, or provide its own resource.
       
   288  */
       
   289 EXPORT_C CAknQueryValueDateArray* CAknQueryValueDateArray::NewL(TInt aResourceId)
       
   290 	{
       
   291 	CAknQueryValueDateArray* self = NewLC(aResourceId);
       
   292 	CleanupStack::Pop();
       
   293 	return self;
       
   294 	}
       
   295 
       
   296 /**
       
   297  * NewLC.
       
   298  *
       
   299  * @param aResourceId	Recource id of a TBUF containing a TTime format string. Client
       
   300  *							can use R_AVKON_TBUF_POPUP_FIELD_DEFAULT_DATE_FORMAT
       
   301  *							if desired, or provide its own resource.
       
   302  */
       
   303 EXPORT_C CAknQueryValueDateArray* CAknQueryValueDateArray::NewLC(TInt aResourceId)
       
   304 	{
       
   305 	CAknQueryValueDateArray* self = new(ELeave) CAknQueryValueDateArray;
       
   306 	CleanupStack::PushL(self);
       
   307 	self->ConstructL(aResourceId);
       
   308 	return self;
       
   309 	}
       
   310 
       
   311 /**
       
   312  * Destructor.
       
   313  */
       
   314 EXPORT_C CAknQueryValueDateArray::~CAknQueryValueDateArray()
       
   315 	{
       
   316 	delete iLastGeneratedTextValue;
       
   317 	delete iFormatString;
       
   318 	}
       
   319 
       
   320 /**
       
   321  * Set the array of values. 
       
   322  * Note that client can use any implementation of array class, but must pass in a 
       
   323  * TArray generated from it (by calling the Array() method on the array class)
       
   324  *
       
   325  * @param array of values, ownership is not passed
       
   326  */
       
   327 EXPORT_C void CAknQueryValueDateArray::SetArray(TimeArray& aArray)
       
   328 	{
       
   329 	iArray = &aArray;
       
   330 	}
       
   331 
       
   332 /**
       
   333  * Get the array of values as a TArray. 
       
   334  * Note that client can use any implementation of array class, but the array is 
       
   335  * treated as a TArray.
       
   336  *
       
   337  * @return array of values, ownership is not passed
       
   338  */
       
   339 EXPORT_C CAknQueryValueDateArray::TimeArray* CAknQueryValueDateArray::Array() const
       
   340 	{
       
   341 	return iArray;
       
   342 	}
       
   343 
       
   344 /**
       
   345  * Return the TTime format string that is used to generate MDesCArray values
       
   346  *
       
   347  * @return format string
       
   348  */
       
   349 EXPORT_C const HBufC* CAknQueryValueDateArray::FormatString() const
       
   350 	{
       
   351 	return iFormatString;
       
   352 	}
       
   353 
       
   354 /**
       
   355  * Return the size of a maximal time string formated using the format string 
       
   356  * that was supplied during construction of this instance.
       
   357  * Two versions to eliminate compiler warnings.
       
   358  *	
       
   359  * @return length of formatted string.
       
   360  */
       
   361 #ifdef __WINS__
       
   362 EXPORT_C const TInt CAknQueryValueDateArray::FormattedStringSize() const
       
   363 	{
       
   364 	return iFormattedStringSize;
       
   365 	}
       
   366 #else
       
   367 EXPORT_C TInt CAknQueryValueDateArray::FormattedStringSize() const
       
   368 	{
       
   369 	return iFormattedStringSize;
       
   370 	}
       
   371 #endif // __WINS__
       
   372 
       
   373 /**
       
   374  * Reports count of contained array
       
   375  *
       
   376  * @return count of contained array
       
   377  */
       
   378 EXPORT_C TInt CAknQueryValueDateArray::MdcaCount() const
       
   379 	{
       
   380 	return iArray->Count();
       
   381 	}
       
   382 
       
   383 /**
       
   384  * Returns array element, converting value to text. 
       
   385  * <p> WARNING: the returned pointer is only valid until the next time this
       
   386  * method is invoked. 
       
   387  *
       
   388  * @param index of element to return
       
   389  * @return descriptor representing array element, ownership is not passed
       
   390  */
       
   391 EXPORT_C TPtrC CAknQueryValueDateArray::MdcaPoint(TInt aIndex) const
       
   392 	{
       
   393 	// only storing the last used string saves memory by avoiding keeping an array 
       
   394 	// of descriptors in memory. 
       
   395 	TTime time = (*iArray)[aIndex];
       
   396 	TPtr textPtr = iLastGeneratedTextValue->Des();
       
   397 
       
   398 	// clear the last value in case of formatting failure
       
   399 	textPtr.Zero();
       
   400 
       
   401 	// we have set the size so that it should have enough room
       
   402 	TRAPD(err, time.FormatL(textPtr, *iFormatString));
       
   403 	if(err != KErrNone)
       
   404 		{
       
   405 #ifdef _DEBUG
       
   406 		// only other cause for leave is bad formatting, that must be 
       
   407 		// caught during debugging of application code
       
   408 		Panic(EAknPanicQueryDateArrayTimeFormatLeave);
       
   409 #endif
       
   410 		}
       
   411     AknTextUtils::DisplayTextLanguageSpecificNumberConversion( textPtr );
       
   412 	return textPtr;
       
   413 	}
       
   414 
       
   415 /**
       
   416  * Constructor.
       
   417  */
       
   418 CAknQueryValueDateArray::CAknQueryValueDateArray()
       
   419 	{
       
   420 
       
   421 	}
       
   422 
       
   423 /**
       
   424  * ConstructL
       
   425  *
       
   426  * @param aResourceId	id of a resource containing a time format string
       
   427  */
       
   428 void CAknQueryValueDateArray::ConstructL(TInt aResourceId)
       
   429 	{
       
   430 	iFormatString = CEikonEnv::Static()->AllocReadResourceL(aResourceId);
       
   431 
       
   432 	// work out size of a formatted time string, and use this for time string size
       
   433 	_LIT(KTime, "20030829:"); // should use max characters, "Wednesday 30th September 2003"
       
   434 	TTime tempTime(KTime);
       
   435 	HBufC* tempString = HBufC::NewLC(KSafeSizeOfDescriptorForTTimeFormat);
       
   436 	TPtr ptr = tempString->Des();
       
   437 	tempTime.FormatL(ptr, *iFormatString);
       
   438 	iFormattedStringSize = tempString->Length();
       
   439 	iFormattedStringSize++;
       
   440 	CleanupStack::PopAndDestroy(); // tempString
       
   441 
       
   442 	iLastGeneratedTextValue = HBufC::NewL(iFormattedStringSize);
       
   443 	}
       
   444 
       
   445 // End of File