testexecmgmt/ucc/Source/UCCSDeviceControl/CNetworkEmulatorSetupCommdb.cpp
changeset 0 3da2a79470a7
equal deleted inserted replaced
-1:000000000000 0:3da2a79470a7
       
     1 /*
       
     2 * Copyright (c) 2005-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:  
       
    15 * System Includes
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 
       
    21 #include <cdbcols.h>
       
    22 #include <e32std.h>
       
    23 #include <d32dbms.h>
       
    24 #include <stdlib.h>
       
    25 #include <charconv.h>
       
    26 #include <f32file.h> 
       
    27 #include <libc/string.h>
       
    28 
       
    29 
       
    30 /*****************************************************************************
       
    31  *
       
    32  * Local Includes
       
    33  *
       
    34  ****************************************************************************/
       
    35 #include "assert.h"
       
    36 #include "NetworkEmulatorControl.h"
       
    37 
       
    38 
       
    39 /*****************************************************************************
       
    40  *
       
    41  *	PUBLIC METHOD: Constructor
       
    42  *
       
    43  ****************************************************************************/	
       
    44 EXPORT_C CSetUpCommDb::CSetUpCommDb()
       
    45 {
       
    46 	iStatus = TCOMMBD_IDLE;
       
    47 }
       
    48 
       
    49 /*****************************************************************************
       
    50  *
       
    51  *	PUBLIC METHOD: Destructor
       
    52  *
       
    53  ****************************************************************************/
       
    54 EXPORT_C CSetUpCommDb::~CSetUpCommDb()
       
    55 {
       
    56 	if( iStatus == TCOMMBD_CONNECTED ) {
       
    57 		assert( iDbComms != NULL ); 
       
    58 		delete iDbComms;
       
    59 		iDbComms = NULL;
       
    60 		iStatus = TCOMMBD_IDLE; 
       
    61 	}
       
    62 	assert( iStatus == TCOMMBD_IDLE );
       
    63 }
       
    64 
       
    65 /*****************************************************************************
       
    66  *
       
    67  *	PUBLIC METHOD: Initialise the database
       
    68  *
       
    69  ****************************************************************************/
       
    70 EXPORT_C TCommDBError CSetUpCommDb::initialise(void)
       
    71 {
       
    72 	assert( iStatus == TCOMMBD_IDLE);
       
    73 
       
    74 	// Connect to the database.
       
    75 	// Specifying type as EFalse, so if the dB does not exist, function leaves.
       
    76 	TRAPD (r, iDbComms = CCommsDatabase::NewL(EFalse));
       
    77 	if( r != KErrNone ) {
       
    78 		return 	TCommDB_ErrorInitCommDb;
       
    79 	}
       
    80 	assert ( iDbComms != NULL );
       
    81 	iStatus = TCOMMBD_CONNECTED;
       
    82 	return TCommDB_Success;
       
    83 }
       
    84 
       
    85 
       
    86 /*****************************************************************************
       
    87  *
       
    88  *	SECTION: updateEntry
       
    89  *
       
    90  ****************************************************************************/
       
    91 /*****************************************************************************
       
    92  *
       
    93  *	PUBLIC METHOD: updateEntry
       
    94  *
       
    95  ****************************************************************************/
       
    96 EXPORT_C TCommDBError CSetUpCommDb::updateEntry(TPtrC aTable, TUint32 aRecordID, TPtrC aFieldName, char* aFieldValue, int* aErrCode)
       
    97 {
       
    98 	CCommsDbTableView *table_handle = NULL;
       
    99 	int ret = KErrNone;
       
   100 
       
   101 	// Check the params
       
   102 	assert ( aFieldValue != NULL );
       
   103 	assert ( aErrCode != NULL );
       
   104 	*aErrCode = 0;
       
   105 
       
   106 	// Check the state
       
   107 	assert( iStatus == TCOMMBD_CONNECTED );
       
   108 	assert ( iDbComms != NULL );
       
   109 
       
   110 	// Open the table we are looking for
       
   111 	TRAP( ret, (table_handle = openTableL(iDbComms,aTable)) );
       
   112 	if( (ret != KErrNone) || (table_handle == NULL) ) {
       
   113 		*aErrCode = ret;
       
   114 		return TCommDB_ErrorTableNotFound;
       
   115 	}
       
   116 
       
   117 	// find and update the record
       
   118 	FindAndUpdateRecord( table_handle, aRecordID, aFieldName, aFieldValue, aErrCode );
       
   119 
       
   120 	// cleanup and return
       
   121 	delete table_handle;
       
   122 	table_handle = NULL;
       
   123 	return TCommDB_Success;
       
   124 }
       
   125 
       
   126 
       
   127 /*****************************************************************************
       
   128  *
       
   129  *	PRIVATE METHOD: FindAndUpdateRecord
       
   130  *
       
   131  ****************************************************************************/
       
   132 TCommDBError CSetUpCommDb::FindAndUpdateRecord( CCommsDbTableView *aTable, TUint32 aRecordID, TPtrC aFieldName, char* aFieldValue, int* aErrCode)
       
   133 {
       
   134 	int err, ret;
       
   135 	TUint32 id = 0;
       
   136 
       
   137 	// Look for the matching record in the table
       
   138 	err = aTable->GotoFirstRecord();
       
   139 	while( err == KErrNone ) {
       
   140 
       
   141 		// Read the id for each record in the table and compare to the one we are looking for.
       
   142 		TRAP( ret, (aTable->ReadUintL(TPtrC(COMMDB_ID),id)) );
       
   143 		if( ret != KErrNone ) {	
       
   144 			*aErrCode = ret;
       
   145 			return TCommDB_ErrorReadingRecord;
       
   146 		}
       
   147 
       
   148 		// We have found the record we are looking for then update the field	
       
   149 		if( id == aRecordID) {
       
   150 			return UpdateField( aTable, aFieldName, aFieldValue, aErrCode );
       
   151 		}
       
   152 	}
       
   153 
       
   154 	// no matching record found
       
   155 	return TCommDB_RecNotFound;
       
   156 }
       
   157 
       
   158 
       
   159 /*****************************************************************************
       
   160  *
       
   161  *	PRIVATE METHOD: UpdateField
       
   162  *
       
   163  ****************************************************************************/
       
   164 TCommDBError CSetUpCommDb::UpdateField( CCommsDbTableView *aTable, TPtrC aFieldName, char* aFieldValue, int* aErrCode )
       
   165 {
       
   166 	int err, ret;
       
   167 	TDbColType colType = EDbColBinary;
       
   168 	TUint32 attrib;
       
   169 	TUint32 entry_integer;
       
   170 
       
   171 	// Get the type of the field we are writing to
       
   172 	TRAP( ret, (aTable->ReadTypeAttribL(aFieldName,colType,attrib)) );
       
   173 	if( ret != KErrNone ) {		
       
   174 		*aErrCode = ret;
       
   175 		return TCommDB_ErrorColNotFound;
       
   176 	}
       
   177 
       
   178 	// Must call update record before editing any existing record.
       
   179 	err = aTable->UpdateRecord();
       
   180 	if( err != KErrNone ) {	
       
   181 		*aErrCode = err;
       
   182 		return TCommDB_ErrorUpdatingRecord;
       
   183 	}
       
   184 
       
   185 	// Convert the string value to the appropriate type and write entry to database
       
   186 	switch (colType) {
       
   187 
       
   188 	// convert the string to an integer and update the entry
       
   189 	case EDbColUint16:
       
   190 	case EDbColUint32:
       
   191 	case EDbColUint8:
       
   192 		entry_integer = (TUint32)atoi((const char*)aFieldValue);
       
   193 		TRAP( ret, (aTable->WriteUintL(aFieldName,entry_integer)) );
       
   194 		if( ret != KErrNone ) {		
       
   195 			*aErrCode = ret;
       
   196 			return TCommDB_ErrorUpdatingRecord;
       
   197 		}
       
   198 		break;
       
   199 
       
   200 	// no need for conversion - just add the string
       
   201 	case EDbColText8:
       
   202 	{
       
   203 		TPtrC8 acsiiPtr((TUint8*)aFieldValue);
       
   204 		TRAP( ret, (aTable->WriteTextL(aFieldName,acsiiPtr)) );
       
   205 		if( ret != KErrNone ) {
       
   206 			*aErrCode = ret;
       
   207 			return TCommDB_ErrorUpdatingRecord;
       
   208 		}
       
   209 		break;
       
   210 	}
       
   211 
       
   212 	// convert the ascii string to unicode and update the entry
       
   213 	case EDbColText16:
       
   214 		err = SetUnicodeString( aTable, aFieldName, aFieldValue, aErrCode );
       
   215 		if( err != TCommDB_Success ) {
       
   216 			return TCommDB_ErrorUpdatingRecord;	
       
   217 		}
       
   218 		break;
       
   219 
       
   220 	// no need for conversion - just add the string
       
   221 	case EDbColLongText8:
       
   222 	{
       
   223 		TPtrC8 asciiPtrLong((TUint8*)aFieldValue);
       
   224 		TRAP( ret, (aTable->WriteTextL(aFieldName,asciiPtrLong)) );
       
   225 		if( ret != KErrNone ) {		
       
   226 			*aErrCode = ret;
       
   227 			return TCommDB_ErrorUpdatingRecord;
       
   228 		}
       
   229 		break;
       
   230 	}
       
   231 
       
   232 	// convert the ascii string to unicode and update the entry
       
   233 	case EDbColLongText16:
       
   234 		ret = SetUnicodeStringLong( aTable, aFieldName, aFieldValue, aErrCode );
       
   235 		if( ret != TCommDB_Success ) {
       
   236 			return TCommDB_ErrorUpdatingRecord;	
       
   237 		}
       
   238 		break;
       
   239 
       
   240 	// unknown column type
       
   241 	default:
       
   242 		return TCommDB_ErrorColTypeValueNotSupported;
       
   243 	}
       
   244 
       
   245 	// commit the changes
       
   246 	err = aTable->PutRecordChanges();
       
   247 	if( err != KErrNone ) {
       
   248 		*aErrCode = ret;
       
   249 		return TCommDB_ErrorUpdatingRecord;
       
   250 	}
       
   251 
       
   252 	// done
       
   253 	return TCommDB_Success;
       
   254 }
       
   255 
       
   256 
       
   257 /*****************************************************************************
       
   258  *
       
   259  *	SECTION: Get...
       
   260  *
       
   261  ****************************************************************************/
       
   262 /*****************************************************************************
       
   263  *
       
   264  *	PUBLIC METHOD: getIntEntry
       
   265  *
       
   266  ****************************************************************************/
       
   267 EXPORT_C TCommDBError CSetUpCommDb::getIntEntry( TPtrC aTable, TUint32 aRecordID, TPtrC aFieldName, TUint32& aFieldValue, int* aErrCode )
       
   268 {
       
   269 	int ret;
       
   270 	TCommDBError rv;
       
   271 	CCommsDbTableView* table = NULL;
       
   272 	TDbColType colType;
       
   273 
       
   274 	// check params and state
       
   275 	assert( iStatus == TCOMMBD_CONNECTED );
       
   276 	assert ( iDbComms != NULL );
       
   277 	assert( aErrCode != NULL );
       
   278 	*aErrCode = 0;
       
   279 
       
   280 	// open the table
       
   281 	TRAP( ret, (table = openTableL(iDbComms, aTable)) );
       
   282 	if( (ret != KErrNone) || (table == NULL) ) {
       
   283 		*aErrCode = ret;
       
   284 		return TCommDB_ErrorTableNotFound;
       
   285 	}
       
   286 
       
   287 	// get to the record 
       
   288 	rv = FindRecordAndGetType( table, aRecordID, aFieldName, &colType, aErrCode );
       
   289 	if( rv != TCommDB_Success ) {
       
   290 		delete table;
       
   291 		return rv;
       
   292 	}
       
   293 
       
   294 	// check the field has the expected type
       
   295 	if( (colType != EDbColUint16) && (colType != EDbColUint8) ) {
       
   296 		delete table;
       
   297 		return TCommDB_ErrorUnexpectedColType;
       
   298 	}
       
   299 
       
   300 	// get the field
       
   301 	TRAP( ret, (table->ReadUintL(aFieldName,aFieldValue)) );
       
   302 	if ( ret != KErrNone ) {	
       
   303 		delete table;
       
   304 		*aErrCode = ret;
       
   305 		return TCommDB_ErrorReadingRecord;
       
   306 	}
       
   307 
       
   308 	// done
       
   309 	delete table;
       
   310 	return TCommDB_Success;
       
   311 }
       
   312 
       
   313 
       
   314 /*****************************************************************************
       
   315  *
       
   316  *	PUBLIC METHOD: getBoolEntry
       
   317  *
       
   318  ****************************************************************************/
       
   319 EXPORT_C TCommDBError CSetUpCommDb::getBoolEntry(TPtrC aTable, TUint32 aRecordID, TPtrC aFieldName, TBool& aFieldValue, int* aErrCode)
       
   320 {
       
   321 	int ret;
       
   322 	TCommDBError rv;
       
   323 	CCommsDbTableView* table = NULL;
       
   324 	TDbColType colType;
       
   325 
       
   326 	// check params and state
       
   327 	assert( iStatus == TCOMMBD_CONNECTED );
       
   328 	assert ( iDbComms != NULL );
       
   329 	assert( aErrCode != NULL );
       
   330 	*aErrCode = 0;
       
   331 
       
   332 	// open the table
       
   333 	TRAP( ret, (table = openTableL(iDbComms, aTable)) );
       
   334 	if( (ret != KErrNone) || (table == NULL) ) {
       
   335 		*aErrCode = ret;
       
   336 		return TCommDB_ErrorTableNotFound;
       
   337 	}
       
   338 
       
   339 	// get to the record 
       
   340 	rv = FindRecordAndGetType( table, aRecordID, aFieldName, &colType, aErrCode );
       
   341 	if( rv != TCommDB_Success ) {
       
   342 		delete table;
       
   343 		return rv;
       
   344 	}
       
   345 
       
   346 	// check the field has the expected type
       
   347 	if( colType != EDbColBit ) {
       
   348 		delete table;
       
   349 		return TCommDB_ErrorUnexpectedColType;
       
   350 	}
       
   351 
       
   352 	// get the field
       
   353 	TRAP( ret, (table->ReadBoolL(aFieldName,aFieldValue)) );
       
   354 	if ( ret != KErrNone ) {	
       
   355 		delete table;
       
   356 		*aErrCode = ret;
       
   357 		return TCommDB_ErrorReadingRecord;
       
   358 	}
       
   359 
       
   360 	// done
       
   361 	delete table;
       
   362 	return TCommDB_Success;
       
   363 }
       
   364 
       
   365 
       
   366 /*****************************************************************************
       
   367  *
       
   368  *	PUBLIC METHOD: getAsciiEntry
       
   369  *
       
   370  ****************************************************************************/
       
   371 EXPORT_C TCommDBError CSetUpCommDb::getAsciiEntry(TPtrC aTable, TUint32 aRecordID, TPtrC aFieldName, TDes8& aFieldValue, int* aErrCode)
       
   372 {
       
   373 	int ret;
       
   374 	TCommDBError rv;
       
   375 	CCommsDbTableView* table = NULL;
       
   376 	TDbColType colType;
       
   377 
       
   378 	// check params and state
       
   379 	assert( iStatus == TCOMMBD_CONNECTED );
       
   380 	assert ( iDbComms != NULL );
       
   381 	assert( aErrCode != NULL );
       
   382 	*aErrCode = 0;
       
   383 
       
   384 	// open the table
       
   385 	TRAP( ret, (table = openTableL(iDbComms, aTable)) );
       
   386 	if( (ret != KErrNone) || (table == NULL) ) {
       
   387 		*aErrCode = ret;
       
   388 		return TCommDB_ErrorTableNotFound;
       
   389 	}
       
   390 
       
   391 	// get to the record 
       
   392 	rv = FindRecordAndGetType( table, aRecordID, aFieldName, &colType, aErrCode );
       
   393 	if( rv != TCommDB_Success ) {
       
   394 		delete table;
       
   395 		return rv;
       
   396 	}
       
   397 
       
   398 	// check the field has the expected type
       
   399 	if( colType != EDbColText8 ) {
       
   400 		delete table;
       
   401 		return TCommDB_ErrorUnexpectedColType;
       
   402 	}
       
   403 
       
   404 	// get the field
       
   405 	TRAP( ret, (table->ReadTextL(aFieldName,aFieldValue)) );
       
   406 	if ( ret != KErrNone ) {	
       
   407 		delete table;
       
   408 		*aErrCode = ret;
       
   409 		return TCommDB_ErrorReadingRecord;
       
   410 	}
       
   411 
       
   412 	// done
       
   413 	delete table;
       
   414 	return TCommDB_Success;
       
   415 }
       
   416 
       
   417 
       
   418 /*****************************************************************************
       
   419  *
       
   420  *	PUBLIC METHOD: getUnicodeEntry
       
   421  *
       
   422  ****************************************************************************/
       
   423 EXPORT_C TCommDBError CSetUpCommDb::getUnicodeEntry(TPtrC aTable, TUint32 aRecordID, TPtrC aFieldName, TDes16& aFieldValue, int* aErrCode)
       
   424 {
       
   425 	int ret;
       
   426 	TCommDBError rv;
       
   427 	CCommsDbTableView* table = NULL;
       
   428 	TDbColType colType;
       
   429 
       
   430 	// check params and state
       
   431 	assert( iStatus == TCOMMBD_CONNECTED );
       
   432 	assert ( iDbComms != NULL );
       
   433 	assert( aErrCode != NULL );
       
   434 	*aErrCode = 0;
       
   435 
       
   436 	// open the table
       
   437 	TRAP( ret, (table = openTableL(iDbComms, aTable)) );
       
   438 	if( (ret != KErrNone) || (table == NULL) ) {
       
   439 		*aErrCode = ret;
       
   440 		return TCommDB_ErrorTableNotFound;
       
   441 	}
       
   442 
       
   443 	// get to the record 
       
   444 	rv = FindRecordAndGetType( table, aRecordID, aFieldName, &colType, aErrCode );
       
   445 	if( rv != TCommDB_Success ) {
       
   446 		delete table;
       
   447 		return rv;
       
   448 	}
       
   449 
       
   450 	// check the field has the expected type
       
   451 	if( colType != EDbColText16 ) {
       
   452 		delete table;
       
   453 		return TCommDB_ErrorUnexpectedColType;
       
   454 	}
       
   455 
       
   456 	// get the field
       
   457 	TRAP( ret, (table->ReadTextL(aFieldName,aFieldValue)) );
       
   458 	if ( ret != KErrNone ) {	
       
   459 		delete table;
       
   460 		*aErrCode = ret;
       
   461 		return TCommDB_ErrorReadingRecord;
       
   462 	}
       
   463 
       
   464 	// done
       
   465 	delete table;
       
   466 	return TCommDB_Success;
       
   467 }
       
   468 
       
   469 
       
   470 /*****************************************************************************
       
   471  *
       
   472  * PRIVATE METHOD: FindRecordAndGetType
       
   473  *
       
   474  ****************************************************************************/
       
   475 TCommDBError CSetUpCommDb::FindRecordAndGetType( CCommsDbTableView *aTable, TUint32 aRecordID, TPtrC aFieldName, TDbColType *aColType, int* aErrCode )
       
   476 {
       
   477 	int err = KErrNone;
       
   478 	int ret = KErrNone;
       
   479 	TUint32 id = 0;
       
   480 	TDbColType colType = EDbColBit;
       
   481 	TUint32 attrib;
       
   482 
       
   483 	// check params
       
   484 	assert( aTable != NULL );
       
   485 	assert( aErrCode != NULL );
       
   486 	*aErrCode = 0;
       
   487 
       
   488 	// find the matching record in the table
       
   489 	err = aTable->GotoFirstRecord();
       
   490 	while( err == KErrNone) {
       
   491 
       
   492 		// get the id field
       
   493 		TRAP( ret, (aTable->ReadUintL(TPtrC(COMMDB_ID),id)) );
       
   494 		if( ret != KErrNone ) {	
       
   495 			*aErrCode = ret;
       
   496 			return TCommDB_ErrorReadingRecord;
       
   497 		}
       
   498 
       
   499 		// see if the id matches the requested id
       
   500 		if( id == aRecordID) {
       
   501 			break;
       
   502 		}
       
   503 
       
   504 		// go to the next record
       
   505 		err = aTable->GotoNextRecord();
       
   506 	}
       
   507 
       
   508 	// see if no record was found
       
   509 	if( err != KErrNone ) {
       
   510 		return TCommDB_RecNotFound;
       
   511 	}
       
   512 
       
   513 	// check the type of the field and see if it matches Int
       
   514 	TRAP( ret, (aTable->ReadTypeAttribL(aFieldName,colType,attrib)) );
       
   515 	if( ret != KErrNone ) {
       
   516 		*aErrCode = ret;
       
   517 		return TCommDB_ErrorColNotFound;
       
   518 	}
       
   519 
       
   520 	// return the coltype
       
   521 	*aColType = colType;
       
   522 
       
   523 	// done
       
   524 	return TCommDB_Success;
       
   525 }
       
   526 
       
   527 
       
   528 /*****************************************************************************
       
   529  *
       
   530  *	SECTION: Data Writers
       
   531  *
       
   532  ****************************************************************************/
       
   533 /*****************************************************************************
       
   534  *
       
   535  *	Set a unicode string in the db
       
   536  *
       
   537  ****************************************************************************/
       
   538 int CSetUpCommDb::SetUnicodeString( CCommsDbTableView* aTable, TPtrC aFieldName, char* aFieldValue, int* aErrCode )
       
   539 {
       
   540 	int ret;
       
   541 	HBufC16 *unicode_string;
       
   542 
       
   543 	// check params
       
   544 	assert( aTable != NULL );
       
   545 	assert( aFieldValue != NULL );
       
   546 	assert( aErrCode != NULL );
       
   547 	*aErrCode = 0;
       
   548 
       
   549 	// convert the passed string to unicode			
       
   550 	unicode_string = ConvertAsciiToUnicode( aFieldValue );
       
   551 	if( unicode_string == NULL ) {
       
   552 		return TCommDB_ErrorConvertingToUnicode;
       
   553 	}
       
   554 	TPtr16 unicode_string_ptr( unicode_string->Des() );
       
   555 
       
   556 	// update the entry 
       
   557 	TRAP( ret, (aTable->WriteTextL(aFieldName,unicode_string_ptr)) );
       
   558 	if( ret != KErrNone ) {
       
   559 		delete unicode_string;
       
   560 		return TCommDB_ErrorUpdatingRecord;
       
   561 	}
       
   562 	
       
   563 	// done 
       
   564 	delete unicode_string;
       
   565 	return TCommDB_Success;
       
   566 }
       
   567 
       
   568 
       
   569 /*****************************************************************************
       
   570  *
       
   571  *	Set a long unicode string in the db
       
   572  *
       
   573  ****************************************************************************/
       
   574 int CSetUpCommDb::SetUnicodeStringLong(CCommsDbTableView* aTable, TPtrC aFieldName, char* aFieldValue, int *aErrCode)
       
   575 {
       
   576 	int ret, i, value_length;
       
   577 	unsigned short *unicode_string;
       
   578 
       
   579 	// check params
       
   580 	assert( aTable != NULL );
       
   581 	assert( aFieldValue != NULL );
       
   582 	assert( aErrCode != NULL );
       
   583 	*aErrCode = 0;
       
   584 	unicode_string = (unsigned short *)aFieldValue;
       
   585 
       
   586 	// get the length of the (wide) string
       
   587 	for( i = 0; unicode_string[i] != 0; i++ )
       
   588 		;
       
   589 	value_length = i;
       
   590 
       
   591 	// convert the passed descriptors into the correct format
       
   592 	TPtrC field_value_ptr( unicode_string, value_length );
       
   593 
       
   594 	// update the entry 
       
   595 	TRAP( ret, (aTable->WriteLongTextL(aFieldName,field_value_ptr)) );
       
   596 	if( ret != KErrNone ) {
       
   597 		return TCommDB_ErrorUpdatingRecord;
       
   598 	}
       
   599 	
       
   600 	// done 
       
   601 	return TCommDB_Success;
       
   602 }	
       
   603 
       
   604 
       
   605 /*****************************************************************************
       
   606  *
       
   607  *	SECTION: Helpers
       
   608  *
       
   609  ****************************************************************************/
       
   610 /*****************************************************************************
       
   611  *
       
   612  *	Open the given table
       
   613  *
       
   614  ****************************************************************************/
       
   615 CCommsDbTableView* CSetUpCommDb::openTableL( CCommsDatabase *aDbComms, TPtrC aTable )
       
   616 {
       
   617 	assert ( aDbComms != NULL );
       
   618 	CCommsDbTableView *rv = NULL;
       
   619 	rv = aDbComms->OpenTableLC( aTable );
       
   620 	CleanupStack::Pop();
       
   621 	return rv;
       
   622 }
       
   623 
       
   624 
       
   625 /*****************************************************************************
       
   626  *
       
   627  *	Convert an ascii string to unicode
       
   628  *
       
   629  ****************************************************************************/
       
   630 HBufC16 *CSetUpCommDb::ConvertAsciiToUnicode( char *aAsciiString )
       
   631 {
       
   632 	RFs aFs;
       
   633 	int error_thrown, err;
       
   634 	TInt state;
       
   635 	HBufC16* unicode_string = NULL;
       
   636 	CCnvCharacterSetConverter* conv = NULL;
       
   637 	CCnvCharacterSetConverter::TAvailability available;
       
   638 
       
   639 	// Setup the descriptors and allocate the memory
       
   640 	const TPtrC8 textData( (TUint8*)aAsciiString );
       
   641 	TRAP( error_thrown, (unicode_string = HBufC16::NewL(textData.Length())) );	
       
   642 	if( (error_thrown != KErrNone) || (unicode_string == NULL) ) {
       
   643 		return NULL;
       
   644 	}
       
   645 	TPtr16 textPtr( unicode_string->Des() );
       
   646 
       
   647 	// connect to the file-system to do the conversion
       
   648 	err = aFs.Connect();				
       
   649 	if( err != KErrNone) {	
       
   650 		return NULL;
       
   651 	}
       
   652 
       
   653 	// get the character convertor
       
   654 	TRAP( error_thrown, (conv = CCnvCharacterSetConverter::NewL()) );
       
   655 	if( (error_thrown != KErrNone) || (conv == NULL) ) {
       
   656 		delete unicode_string;
       
   657 		return NULL;
       
   658 	}
       
   659 
       
   660 	// Check if the required character set we are converting too is available
       
   661 	available = conv->PrepareToConvertToOrFromL( KCharacterSetIdentifierUtf8, aFs );  
       
   662 	if( available == CCnvCharacterSetConverter::ENotAvailable ) {
       
   663 		delete unicode_string;
       
   664 		delete conv;
       
   665 		return NULL;
       
   666 	}
       
   667 
       
   668 	// finally, do the actual conversion
       
   669 	state = CCnvCharacterSetConverter::KStateDefault;
       
   670 	conv->ConvertToUnicode( textPtr, textData, state );
       
   671 
       
   672 	// ok we are done - return the new string
       
   673 	delete conv;
       
   674 	return unicode_string;
       
   675 }
       
   676