emailservices/emailstore/message_store/server/src/ContainerStoreSortingTable.cpp
changeset 0 8466d47a6819
child 8 e1b6206813b4
equal deleted inserted replaced
-1:000000000000 0:8466d47a6819
       
     1 /*
       
     2 * Copyright (c) 2006 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:  Container store sorting table implementation.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #include "ContainerStoreSortingTable.h"
       
    21 #include "MsgStoreTypes.h"
       
    22 #include "MessageStoreClientServer.h"
       
    23 #include "ContainerStoreDefs.h"
       
    24 #include "MsgStoreSortResultRowSet.h"
       
    25 #include "MsgStoreStringSortResultRowSet.h"
       
    26 #include "ContainerStoreUtils.h"
       
    27 #include "ContainerStoreEncryption.h"
       
    28 #include "MsgStoreInMemorySortRowSet.h"
       
    29 	
       
    30 _LIT( KSelect,  "SELECT "   );
       
    31 _LIT( KDelete,  "DELETE "   );
       
    32 _LIT( KFrom,    " FROM "    );
       
    33 _LIT( KWhere,   " WHERE "   );
       
    34 _LIT( KEquals,  " = "       );
       
    35 _LIT( KOrderBy, " ORDER BY ");
       
    36 _LIT( KComma,   ", "        );
       
    37 _LIT( KAsc,     " ASC"      );
       
    38 _LIT( KDesc,    " DESC"     );
       
    39 _LIT( KStar,    "* "        );
       
    40 
       
    41 const TUint KQuerryBufSize = 200;
       
    42 const TUint KQuerrySizeSmall = 60;
       
    43 
       
    44 enum TMsgStorePriorityScore
       
    45 	{
       
    46 	EPriorityScoreLow    = -10,
       
    47 	EPriorityScoreNormal =   0,
       
    48 	EPriorityScoreHigh   =  10
       
    49 	};
       
    50 
       
    51     enum TMsgStoreReadScore
       
    52         {
       
    53         EReadScoreUnread     = 0,
       
    54         EReadScoreRead       = 1,
       
    55         EReadScoreReadLocal  = 2,
       
    56         EReadScoreReadBoth   = 3
       
    57         };
       
    58 
       
    59     enum TMsgStoreFlagStatusScore
       
    60     	{
       
    61     	EFlagStatusScoreNone             = 0,
       
    62     	EFlagStatusScoreFollowUpComplete = 10,
       
    63     	EFlagStatusScoreFollowUp         = 20 
       
    64     	};
       
    65 
       
    66     
       
    67 // ==================================
       
    68 // CLASS: CContainerStoreSortingTable
       
    69 // ==================================
       
    70 // ==========================================================================
       
    71 // FUNCTION: OpenL
       
    72 // ==========================================================================
       
    73 CContainerStoreSortingTable* CContainerStoreSortingTable::OpenL( CContainerStoreUtils&        aUtils,
       
    74                                                                  CContainerStoreEncryption&   aEncryption,
       
    75                                                                  MSortingTableObserver&       aObserver )
       
    76 	{
       
    77 	CContainerStoreSortingTable* self = new(ELeave) CContainerStoreSortingTable( aUtils, aEncryption, aObserver );
       
    78 	CleanupStack::PushL( self );
       
    79 	self->OpenTableL();
       
    80 	CleanupStack::Pop( self );
       
    81 	return self;
       
    82 	}
       
    83 		
       
    84 // ==========================================================================
       
    85 // FUNCTION: CreateL
       
    86 // ==========================================================================
       
    87 CContainerStoreSortingTable* CContainerStoreSortingTable::CreateL( CContainerStoreUtils&        aUtils,
       
    88                                                                    CContainerStoreEncryption&   aEncryption,
       
    89                                                                    MSortingTableObserver&       aObserver )
       
    90 	{
       
    91 	CContainerStoreSortingTable* self = new(ELeave) CContainerStoreSortingTable( aUtils, aEncryption, aObserver );
       
    92 	CleanupStack::PushL( self );
       
    93 	self->CreateTableL();
       
    94 	CleanupStack::Pop( self );
       
    95 	return self;
       
    96 	}
       
    97 
       
    98 // ==========================================================================
       
    99 // FUNCTION: Constructor
       
   100 // ==========================================================================
       
   101 CContainerStoreSortingTable::CContainerStoreSortingTable( CContainerStoreUtils&        aUtils,
       
   102                                                           CContainerStoreEncryption&   aEncryption,
       
   103                                                           MSortingTableObserver&       aObserver )
       
   104 : CContainerStoreTable( aUtils ), iEncryption( aEncryption ), iObserver( aObserver )
       
   105 	{
       
   106 	__LOG_CONSTRUCT( "msg", "CContainerStoreSortingTable" )
       
   107 	__LOG_CLOSE_BETWEEN_WRITES
       
   108 	} // end constructor
       
   109 		
       
   110 // ==========================================================================
       
   111 // FUNCTION: Destructor
       
   112 // ==========================================================================
       
   113 CContainerStoreSortingTable::~CContainerStoreSortingTable()
       
   114 	{
       
   115 	iUtils.CloseTable( iTable );
       
   116     iEncryptedBuffer.Close();
       
   117 	__LOG_DESTRUCT	
       
   118 	} // end destructor
       
   119 
       
   120 // ==========================================================================
       
   121 // FUNCTION: OpenTableL
       
   122 // ==========================================================================
       
   123 void CContainerStoreSortingTable::OpenTableL()
       
   124 	{
       
   125 	__LOG_ENTER( "OpenTableL" )
       
   126 
       
   127 	iUtils.OpenTableL( iTable, KSortingTableName );
       
   128 	
       
   129 	// Get the column numbers for the containers table.
       
   130 	CDbColSet* colSet = iTable.ColSetL();
       
   131 	CleanupStack::PushL( colSet );
       
   132 
       
   133     iMessageIdColNum      = colSet->ColNo( KSortingTableMessageIdCol );
       
   134     iFolderIdColNum       = colSet->ColNo( KSortingTableFolderIdCol );
       
   135     iMailBoxIdColNum      = colSet->ColNo( KSortingTableMailBoxIdCol );
       
   136     iReceivedDateColNum   = colSet->ColNo( KSortingTableReceivedDateCol );
       
   137     iReadUnreadColNum     = colSet->ColNo( KSortingTableReadUnreadCol );
       
   138     iPriotiryColNum       = colSet->ColNo( KSortingTablePriorityCol );
       
   139     iFlagStatusColNum     = colSet->ColNo( KSortingTableFlagStatusCol );
       
   140     iSizeColNum           = colSet->ColNo( KSortingTableSizeCol );
       
   141     iAttachmentFlagColNum = colSet->ColNo( KSortingTableAttachmentFlagCol );
       
   142     iFromColNum           = colSet->ColNo( KSortingTableFromCol );
       
   143     iToColNum             = colSet->ColNo( KSortingTableToCol );
       
   144     iSubjectColNum        = colSet->ColNo( KSortingTableSubjectCol );
       
   145     iIsEncryptedColNum    = colSet->ColNo( KSortingTableIsEncryptedCol );
       
   146 	
       
   147 	CleanupStack::PopAndDestroy( colSet );
       
   148 	
       
   149 	// Set the table's index to the ID index.
       
   150 	User::LeaveIfError( iTable.SetIndex( KSortingTableMessageIdIndex ) );
       
   151 
       
   152 	__LOG_EXIT	
       
   153 	}
       
   154 		
       
   155 // ==========================================================================
       
   156 // FUNCTION: CreateTableL
       
   157 // ==========================================================================
       
   158 void CContainerStoreSortingTable::CreateTableL()
       
   159 	{
       
   160 	__LOG_ENTER( "CreateTableL" )
       
   161 	
       
   162 	// Create table columns
       
   163 	CDbColSet* colSet = CDbColSet::NewLC();
       
   164 	
       
   165 	TDbCol msgIdCol( KSortingTableMessageIdCol, EDbColUint32 );
       
   166 	colSet->AddL( msgIdCol );	
       
   167 	
       
   168 	TDbCol folderIdCol( KSortingTableFolderIdCol, EDbColUint32 );
       
   169 	colSet->AddL( folderIdCol );	
       
   170 	
       
   171 	TDbCol mailboxIdCol( KSortingTableMailBoxIdCol, EDbColUint32 );
       
   172 	colSet->AddL( mailboxIdCol );	
       
   173 	
       
   174 	TDbCol receivedDateCol( KSortingTableReceivedDateCol, EDbColInt64 );
       
   175 	colSet->AddL( receivedDateCol );	
       
   176 		
       
   177 	TDbCol readUnreadCol( KSortingTableReadUnreadCol, EDbColInt8 );
       
   178 	colSet->AddL( readUnreadCol );	
       
   179 	
       
   180 	TDbCol priorityCol( KSortingTablePriorityCol, EDbColInt8 );
       
   181 	colSet->AddL( priorityCol );	
       
   182 	
       
   183 	TDbCol flagStatusCol( KSortingTableFlagStatusCol, EDbColInt8 );
       
   184 	colSet->AddL( flagStatusCol );	
       
   185 	
       
   186 	TDbCol sizeCol( KSortingTableSizeCol, EDbColUint32 );
       
   187 	colSet->AddL( sizeCol );	
       
   188 	
       
   189 	TDbCol attCol( KSortingTableAttachmentFlagCol, EDbColInt8 );
       
   190 	colSet->AddL( attCol );	
       
   191     
       
   192     TDbCol fromCol( KSortingTableFromCol, EDbColLongBinary );
       
   193     colSet->AddL( fromCol );
       
   194     
       
   195     TDbCol toCol( KSortingTableToCol, EDbColLongBinary );
       
   196     colSet->AddL( toCol );  
       
   197 	
       
   198     TDbCol subjectCol( KSortingTableSubjectCol, EDbColLongBinary );
       
   199     colSet->AddL( subjectCol );  
       
   200     
       
   201     TDbCol encryptedCol( KSortingTableIsEncryptedCol, EDbColUint8 );
       
   202     colSet->AddL( encryptedCol );  
       
   203     
       
   204 	// Create table.
       
   205 	iUtils.CreateTableL( KSortingTableName, *colSet );
       
   206 	
       
   207 	CleanupStack::PopAndDestroy( colSet );
       
   208 	
       
   209 	// Create index for every column except for mailBoxId
       
   210 	CreateIndexL( KSortingTableName, KSortingTableMessageIdIndex,      KSortingTableMessageIdCol    );
       
   211 	CreateIndexL( KSortingTableName, KSortingTableFolderIdIndex,       KSortingTableFolderIdCol     );
       
   212     CreateIndexL( KSortingTableName, KSortingTableMailBoxIdIndex,      KSortingTableMailBoxIdCol    );
       
   213 	CreateIndexL( KSortingTableName, KSortingTableReceivedDateIndex,   KSortingTableReceivedDateCol );
       
   214 	CreateIndexL( KSortingTableName, KSortingTableReadUnreadIndex,     KSortingTableReadUnreadCol,     KSortingTableReceivedDateCol );
       
   215 	CreateIndexL( KSortingTableName, KSortingTablePriorityIndex,       KSortingTablePriorityCol,       KSortingTableReceivedDateCol );
       
   216 	CreateIndexL( KSortingTableName, KSortingTableFlagStatusIndex,     KSortingTableFlagStatusCol,     KSortingTableReceivedDateCol );
       
   217 	CreateIndexL( KSortingTableName, KSortingTableSizeIndex,           KSortingTableSizeCol,           KSortingTableReceivedDateCol );
       
   218 	CreateIndexL( KSortingTableName, KSortingTableAttachmentFlagIndex, KSortingTableAttachmentFlagCol, KSortingTableReceivedDateCol );
       
   219 	
       
   220 	OpenTableL();
       
   221 			
       
   222 	__LOG_EXIT
       
   223 	}
       
   224 
       
   225 // ==========================================================================
       
   226 // FUNCTION: AddMessageL
       
   227 // ==========================================================================
       
   228 void CContainerStoreSortingTable::AddMessageL( TContainerId             aMessageId, 
       
   229 											   TContainerId             aFolderId, 
       
   230 											   TContainerId             aMailboxId, 
       
   231                                                RMsgStoreSortableFields& aSortableFields )
       
   232 	{
       
   233 	__LOG_ENTER_SUPPRESS( "AddMessageL" )
       
   234 	
       
   235 	__LOG_WRITE8_FORMAT3_DEBUG3( "msgId=%x, folderId=%x, mailBoxId=%x", aMessageId, aFolderId, aMailboxId )
       
   236 	
       
   237 	iTable.LastL();
       
   238 	
       
   239 	InsertRowLC();
       
   240 	 
       
   241 	iTable.SetColL( iMessageIdColNum,    aMessageId );
       
   242 	iTable.SetColL( iFolderIdColNum,     aFolderId );
       
   243 	iTable.SetColL( iMailBoxIdColNum,    aMailboxId );
       
   244     iTable.SetColL( iIsEncryptedColNum, static_cast<TUint8>( iEncryption.IsEncryptionOn() ) );
       
   245 	
       
   246 	//write the rest of the fields
       
   247 	WriteFieldsL( aSortableFields );
       
   248 	
       
   249 	PutRowUpdatesL();
       
   250     
       
   251     //Notify the observer
       
   252     iObserver.MessageUpdate( aMessageId, 
       
   253     						 aFolderId, 
       
   254     						 EMsgStoreAdd, 
       
   255     						 iFieldsChanged, 
       
   256                              aSortableFields.iFrom, 
       
   257                              aSortableFields.iTo, 
       
   258                              aSortableFields.iSubject, 
       
   259                              aSortableFields.iReceivedDate );
       
   260 	}
       
   261 		
       
   262 // ==========================================================================
       
   263 // FUNCTION: DeleteMessageL
       
   264 // ==========================================================================
       
   265 void CContainerStoreSortingTable::DeleteMessageL( TContainerId aMessageId )
       
   266 	{
       
   267 	__LOG_ENTER_SUPPRESS( "DeleteMessageL" )
       
   268 	
       
   269 	__LOG_WRITE8_FORMAT1_DEBUG3( "msgId=%x", aMessageId )
       
   270 	
       
   271     SeekL( aMessageId );
       
   272     iTable.GetL();
       
   273     TContainerId folderId = iTable.ColUint32( iFolderIdColNum );
       
   274     
       
   275     iTable.DeleteL();
       
   276     
       
   277     //Notify the observer
       
   278     iObserver.MessageUpdate( aMessageId, folderId, EMsgStoreDelete );
       
   279 	}
       
   280 
       
   281 // ==========================================================================
       
   282 // FUNCTION: DeleteMessagesByFolderIdL
       
   283 // ==========================================================================
       
   284 void CContainerStoreSortingTable::DeleteMessagesByFolderIdL( TContainerId aFolderId )
       
   285 	{
       
   286 	__LOG_ENTER_SUPPRESS( "DeleteMessagesByFolderIdL" )
       
   287 	__LOG_WRITE8_FORMAT1_DEBUG3( "aFolderId=%x", aFolderId )
       
   288 	
       
   289 	TBuf<KQuerryBufSize> queryString;
       
   290 	queryString.Copy( KDelete );
       
   291 	queryString.Append( KFrom );
       
   292 	queryString.Append( KSortingTableName );
       
   293 	queryString.Append( KWhere );
       
   294 	queryString.Append( KSortingTableFolderIdCol );
       
   295 	queryString.Append( KEquals );
       
   296 	queryString.AppendNum( aFolderId );
       
   297 	
       
   298 	iUtils.Execute( queryString );
       
   299     
       
   300     //Notify the observer
       
   301     iObserver.FolderDeleted( aFolderId );
       
   302 	}
       
   303 
       
   304 // ==========================================================================
       
   305 // FUNCTION: DeleteMessagesByMailBoxIdL
       
   306 // ==========================================================================
       
   307 void CContainerStoreSortingTable::DeleteMessagesByMailBoxIdL( TContainerId aMailBoxId )
       
   308     {
       
   309     __LOG_ENTER_SUPPRESS( "DeleteMessagesByMailBoxIdL" )
       
   310     __LOG_WRITE8_FORMAT1_DEBUG3( "aMailBoxId=%x", aMailBoxId )
       
   311     
       
   312     TBuf<KQuerryBufSize> queryString;
       
   313     queryString.Copy( KDelete );
       
   314     queryString.Append( KFrom );
       
   315     queryString.Append( KSortingTableName );
       
   316     queryString.Append( KWhere );
       
   317     queryString.Append( KSortingTableMailBoxIdCol );
       
   318     queryString.Append( KEquals );
       
   319     queryString.AppendNum( aMailBoxId );
       
   320     
       
   321     iUtils.Execute( queryString );
       
   322     
       
   323     //Notify the observer
       
   324     iObserver.MailBoxDeleted( aMailBoxId );
       
   325     }
       
   326 
       
   327 // ==========================================================================
       
   328 // FUNCTION: UpdateMessageL
       
   329 // ==========================================================================
       
   330 void CContainerStoreSortingTable::UpdateMessageL( TContainerId aMessageId, 
       
   331                                                   RMsgStoreSortableFields& aSortableFields )
       
   332 	{
       
   333 	__LOG_ENTER_SUPPRESS( "UpdateMessageL" )
       
   334 	
       
   335 	__LOG_WRITE8_FORMAT4_DEBUG3( "msgId=%x, flags=%x, ssize=%d csize=%d", aMessageId, 
       
   336                                                                 aSortableFields.iFlags, 
       
   337                                                                 aSortableFields.iSizeOnServer,
       
   338                                                                 aSortableFields.iSizeOnClient )
       
   339 	
       
   340 	SeekL( aMessageId );
       
   341     
       
   342     iTable.GetL();
       
   343     TContainerId folderId = iTable.ColUint32( iFolderIdColNum );
       
   344 	
       
   345 	PrepareRowForUpdateLC();
       
   346 	
       
   347 	WriteFieldsL( aSortableFields, EFalse );
       
   348 	
       
   349 	PutRowUpdatesL();
       
   350     
       
   351     if ( iFieldsChanged > 0 )
       
   352         {
       
   353         iObserver.MessageUpdate( aMessageId, 
       
   354         						 folderId, 
       
   355         						 EMsgStoreUpdateProperties, 
       
   356         						 iFieldsChanged,
       
   357                                  aSortableFields.iFrom, 
       
   358                                  aSortableFields.iTo, 
       
   359                                  aSortableFields.iSubject, 
       
   360                                  aSortableFields.iReceivedDate );
       
   361         }
       
   362 	}
       
   363 
       
   364 // ==========================================================================
       
   365 // FUNCTION: UpdateMessageFolderL
       
   366 // ==========================================================================
       
   367 void CContainerStoreSortingTable::UpdateMessageFolderL( TContainerId aMessageId, TContainerId aNewFolderId )
       
   368 	{
       
   369 	__LOG_ENTER_SUPPRESS( "UpdateMessageFolderL" )
       
   370 	
       
   371 	__LOG_WRITE8_FORMAT2_DEBUG3( "msgId=%x, aNewFolder=%x", aMessageId, aNewFolderId )
       
   372     
       
   373 	SeekL( aMessageId );
       
   374     iTable.GetL();
       
   375     
       
   376     TContainerId oldFolderId = iTable.ColUint32( iFolderIdColNum );
       
   377     
       
   378     if ( oldFolderId != aNewFolderId )
       
   379         {
       
   380     	
       
   381     	PrepareRowForUpdateLC();
       
   382     	
       
   383     	iTable.SetColL( iFolderIdColNum, aNewFolderId );
       
   384     	
       
   385     	PutRowUpdatesL();
       
   386         
       
   387         //send a notification for delete from old folder 
       
   388         iObserver.MessageUpdate( aMessageId, oldFolderId, EMsgStoreDelete );
       
   389         
       
   390         //send a notification for new message to the new folder
       
   391         RBuf from;
       
   392         CleanupClosePushL( from );
       
   393         ReadStringFieldL( iTable, iFromColNum, from );
       
   394         
       
   395         RBuf to;
       
   396         CleanupClosePushL( to );
       
   397         ReadStringFieldL( iTable, iToColNum, to );
       
   398         
       
   399         RBuf subject;
       
   400         CleanupClosePushL( subject );
       
   401         ReadStringFieldL( iTable, iSubjectColNum, subject );
       
   402         
       
   403         TInt64 date = iTable.ColInt64( iReceivedDateColNum );
       
   404         
       
   405         iObserver.MessageUpdate( aMessageId, aNewFolderId, EMsgStoreAdd, 0, from, to, subject, date );
       
   406         
       
   407         CleanupStack::PopAndDestroy( &subject );
       
   408         CleanupStack::PopAndDestroy( &to );
       
   409         CleanupStack::PopAndDestroy( &from );
       
   410         
       
   411         }    
       
   412 	}
       
   413 		
       
   414 // ==========================================================================
       
   415 // FUNCTION: SortL
       
   416 // ==========================================================================
       
   417 CMsgStoreSortResultRowSet* CContainerStoreSortingTable::SortL( const TMsgStoreSortCriteria& aSortCriteria,  TBool aInMemorySort  )
       
   418 	{
       
   419 	__LOG_ENTER_SUPPRESS( "SortL" )
       
   420     
       
   421 	CMsgStoreSortResultRowSet* result = NULL;
       
   422     
       
   423     if ( aSortCriteria.iSortBy == EMsgStoreSortBySender ||
       
   424          aSortCriteria.iSortBy == EMsgStoreSortByRecipient     ||   
       
   425          aSortCriteria.iSortBy == EMsgStoreSortBySubject )
       
   426         {
       
   427         result = SortByStringFieldL( aSortCriteria );
       
   428         }
       
   429     else if ( aInMemorySort )
       
   430     	{
       
   431     	result = InMemorySortL( aSortCriteria );
       
   432     	}
       
   433     else
       
   434         {
       
   435 
       
   436     	TBuf<KQuerryBufSize> queryString;
       
   437     	queryString.Copy( KSelect );
       
   438         queryString.Append( KStar );
       
   439     	queryString.Append( KFrom );
       
   440     	queryString.Append( KSortingTableName );
       
   441     	queryString.Append( KWhere );
       
   442     	queryString.Append( KSortingTableFolderIdCol );
       
   443     	queryString.Append( KEquals );
       
   444     	queryString.AppendNum( aSortCriteria.iFolderId );
       
   445     	queryString.Append( KOrderBy );
       
   446     	
       
   447     	switch( aSortCriteria.iSortBy )
       
   448     		{
       
   449     		case EMsgStoreSortByReceivedDate:
       
   450     			queryString.Append( KSortingTableReceivedDateCol );
       
   451     			break;
       
   452     		
       
   453     		case EMsgStoreSortByPriority:
       
   454     			queryString.Append( KSortingTablePriorityCol );
       
   455     			break;
       
   456     			
       
   457     		case EMsgStoreSortByFlagStatus:
       
   458     			queryString.Append( KSortingTableFlagStatusCol );
       
   459     			break;
       
   460     		
       
   461     		case EMsgStoreSortByUnRead:
       
   462     			queryString.Append( KSortingTableReadUnreadCol );
       
   463     			break;
       
   464     		
       
   465     		case EMsgStoreSortBySize:
       
   466     			queryString.Append( KSortingTableSizeCol );
       
   467     			break;
       
   468     		
       
   469     		case EMsgStoreSortByAttachment:
       
   470     			queryString.Append( KSortingTableAttachmentFlagCol );
       
   471     			break;
       
   472     		
       
   473     		default:
       
   474     			User::Leave( KErrNotSupported );
       
   475     			break;
       
   476     		}
       
   477     	
       
   478     	//add the sort order
       
   479     	AppendSortOrder( queryString, aSortCriteria.iSortOrder );
       
   480     	
       
   481     	//add the secondary sort field if primary is not received date
       
   482     	if ( aSortCriteria.iSortBy != EMsgStoreSortByReceivedDate )
       
   483     		{
       
   484     		queryString.Append( KComma );
       
   485     		queryString.Append( KSortingTableReceivedDateCol );
       
   486     		AppendSortOrder( queryString, aSortCriteria.iSecondarySortOrder );
       
   487     		}
       
   488     	
       
   489     	__LOG_WRITE_FORMAT1_INFO( "query=%S", &queryString );
       
   490     
       
   491     	result = CMsgStoreSortResultRowSet::NewL( aSortCriteria, 
       
   492     	                                          iMessageIdColNum,
       
   493     	                                          iReceivedDateColNum,
       
   494     	                                          iReadUnreadColNum,
       
   495     	                                          iPriotiryColNum,
       
   496     	                                          iFlagStatusColNum,
       
   497     	                                          iSizeColNum,
       
   498     	                                          iAttachmentFlagColNum );
       
   499     	CleanupStack::PushL( result );
       
   500     	
       
   501         iUtils.PopulateViewL( result->DbView(), queryString );
       
   502         
       
   503         __LOG_WRITE_FORMAT1_INFO( "rowCounts=%d", result->DbView().CountL() );
       
   504         
       
   505         CleanupStack::Pop( result );
       
   506         }
       
   507     
       
   508         return result;
       
   509 	}
       
   510 
       
   511 
       
   512 // ==========================================================================
       
   513 // FUNCTION: SortByStringFieldL
       
   514 // ==========================================================================
       
   515 CMsgStoreStringSortResultRowSet* CContainerStoreSortingTable::SortByStringFieldL( const TMsgStoreSortCriteria& aSortCriteria  )
       
   516     {
       
   517     __LOG_ENTER_SUPPRESS( "SortByStringFieldL" )
       
   518     
       
   519     CMsgStoreStringSortResultRowSet* result = CMsgStoreStringSortResultRowSet::NewL( aSortCriteria, *this );
       
   520     CleanupStack::PushL( result );
       
   521     TBuf<KQuerryBufSize> queryString;
       
   522     queryString.Copy( KSelect );
       
   523     queryString.Append( KStar );
       
   524     queryString.Append( KFrom );
       
   525     queryString.Append( KSortingTableName );
       
   526     queryString.Append( KWhere );
       
   527     queryString.Append( KSortingTableFolderIdCol );
       
   528     queryString.Append( KEquals );
       
   529     queryString.AppendNum( aSortCriteria.iFolderId );
       
   530 
       
   531     iUtils.PopulateViewL( result->DbView(), queryString );
       
   532     
       
   533     ReEvaluateStringViewL( *result, aSortCriteria.iSortBy );
       
   534 
       
   535     result->SortL();
       
   536     
       
   537     CleanupStack::Pop( result );
       
   538     
       
   539     return result;
       
   540     }
       
   541 
       
   542 // ==========================================================================
       
   543 // FUNCTION: InMemorySortL
       
   544 // ==========================================================================
       
   545 CMsgStoreInMemorySortRowSet* CContainerStoreSortingTable::InMemorySortL( const TMsgStoreSortCriteria& aSortCriteria  )
       
   546     {
       
   547     __LOG_ENTER_SUPPRESS( "InMemorySortL" )
       
   548     
       
   549     CMsgStoreInMemorySortRowSet* result = CMsgStoreInMemorySortRowSet::NewL( aSortCriteria, *this );
       
   550     CleanupStack::PushL( result );
       
   551     
       
   552     TBuf<KQuerryBufSize> queryString;
       
   553     queryString.Copy( KSelect );
       
   554     queryString.Append( KStar );
       
   555     queryString.Append( KFrom );
       
   556     queryString.Append( KSortingTableName );
       
   557     queryString.Append( KWhere );
       
   558     queryString.Append( KSortingTableFolderIdCol );
       
   559     queryString.Append( KEquals );
       
   560     queryString.AppendNum( aSortCriteria.iFolderId );
       
   561 
       
   562     iUtils.PopulateViewL( result->DbView(), queryString );
       
   563     
       
   564     ReEvaluateInMemoryViewL( *result, aSortCriteria.iSortBy );
       
   565 
       
   566     result->SortL();
       
   567     
       
   568     CleanupStack::Pop( result );
       
   569     
       
   570     return result;
       
   571     }
       
   572 
       
   573 
       
   574 // ==========================================================================
       
   575 // FUNCTION: ReEvaluateStringViewL
       
   576 // ==========================================================================
       
   577 void CContainerStoreSortingTable::ReEvaluateStringViewL( CMsgStoreStringSortResultRowSet& aResult, TMsgStoreSortByField aSortBy )
       
   578     {
       
   579     __LOG_ENTER( "ReEvaluateStringViewL" )
       
   580     
       
   581     iUtils.SuspendCompactionLC();
       
   582     
       
   583     TInt stringColNum = GetColumnNumL( aSortBy );
       
   584     
       
   585     RDbView& view = aResult.DbView();
       
   586     
       
   587     TBool found = view.FirstL();
       
   588     
       
   589     CStringRow* reuseRow = NULL;
       
   590     
       
   591     RBuf stringVal;
       
   592     CleanupClosePushL( stringVal );
       
   593     
       
   594     while( found )
       
   595         {
       
   596         view.GetL();
       
   597         
       
   598         //get the message id, received date, and the desired string
       
   599         TContainerId msgId = view.ColUint32( iMessageIdColNum );  
       
   600         TInt64 receivedDate = view.ColInt64( iReceivedDateColNum );
       
   601         
       
   602         ReadStringFieldL( view, stringColNum, stringVal );
       
   603         
       
   604         //To reduce memory fragmentation, we re-use the row object whenever possible
       
   605         CStringRow* row;
       
   606         if ( reuseRow  )
       
   607             {
       
   608             row = reuseRow;
       
   609             row->ResetL( msgId, receivedDate, stringVal );
       
   610             CleanupStack::Pop( reuseRow );
       
   611             }
       
   612         else
       
   613             {
       
   614             row = CStringRow::NewL( msgId, receivedDate, stringVal );
       
   615             }
       
   616         
       
   617         reuseRow = aResult.AppendL( row );
       
   618         
       
   619         if ( reuseRow )
       
   620             {
       
   621             CleanupStack::PushL( reuseRow );
       
   622             }
       
   623         
       
   624         found = view.NextL();
       
   625         
       
   626         if ( !found && reuseRow )
       
   627             {
       
   628             //no more rows, delete the reuseRow
       
   629             CleanupStack::PopAndDestroy( reuseRow );
       
   630             }
       
   631             
       
   632         } // end while
       
   633     
       
   634     CleanupStack::PopAndDestroy( &stringVal );
       
   635     
       
   636     iUtils.ResumeCompaction();
       
   637     
       
   638     __LOG_EXIT
       
   639     }
       
   640 
       
   641 // ==========================================================================
       
   642 // FUNCTION: ReEvaluateInMemoryViewL
       
   643 // ==========================================================================
       
   644 void CContainerStoreSortingTable::ReEvaluateInMemoryViewL( CMsgStoreInMemorySortRowSet& aResult, TMsgStoreSortByField aSortBy )
       
   645     {
       
   646     __LOG_ENTER( "ReEvaluateInMemoryViewL" )
       
   647     
       
   648     iUtils.SuspendCompactionLC();
       
   649     
       
   650     RDbView& view = aResult.DbView();
       
   651     TBool found = view.FirstL();
       
   652     
       
   653     while( found )
       
   654         {
       
   655         view.GetL();
       
   656         
       
   657         //get the message id, received date, and the desired string
       
   658         TContainerId msgId = view.ColUint32( iMessageIdColNum );  
       
   659         TInt64 receivedDate = view.ColInt64( iReceivedDateColNum );
       
   660 
       
   661         TInt32 value = GetIntValue( view, aSortBy );
       
   662         
       
   663         CIntegerRow* row = new(ELeave) CIntegerRow( msgId, receivedDate, value );
       
   664         CleanupStack::PushL( row );
       
   665         
       
   666         aResult.AppendL( row );
       
   667         
       
   668         CleanupStack::Pop( row );
       
   669         
       
   670         found = view.NextL();            
       
   671         } // end while
       
   672    
       
   673     iUtils.ResumeCompaction();
       
   674     
       
   675     __LOG_EXIT
       
   676     }
       
   677 
       
   678 
       
   679 // ==========================================================================
       
   680 // FUNCTION: GetStringRowL
       
   681 // ==========================================================================
       
   682 CStringRow* CContainerStoreSortingTable::GetStringRowL( RDbView& aView, TContainerId aMessageId, TMsgStoreSortByField aSortBy )
       
   683     {
       
   684 
       
   685     TBuf<KQuerrySizeSmall> queryString;
       
   686     
       
   687     queryString.Copy( KSortingTableMessageIdCol );
       
   688     queryString.Append( KEquals );
       
   689     queryString.AppendNum( aMessageId );
       
   690     
       
   691     User::LeaveIfError( aView.FindL( RDbRowSet::EForwards, queryString ) );
       
   692     
       
   693     TInt stringColNum = GetColumnNumL( aSortBy ) ;
       
   694     
       
   695     aView.GetL();
       
   696     
       
   697     //get the message id, received date, and the desired string
       
   698     TContainerId msgId = aView.ColUint32( iMessageIdColNum );  
       
   699     TInt64 receivedDate = aView.ColInt64( iReceivedDateColNum );
       
   700     
       
   701     RBuf stringVal;
       
   702     CleanupClosePushL( stringVal );
       
   703     
       
   704     ReadStringFieldL( aView, stringColNum, stringVal );
       
   705     
       
   706     CStringRow* row = CStringRow::NewL( msgId, receivedDate, stringVal );
       
   707     
       
   708     CleanupStack::PopAndDestroy( &stringVal );
       
   709     
       
   710     return row;
       
   711     }
       
   712 
       
   713 // ==========================================================================
       
   714 // FUNCTION: GetIntValue
       
   715 // ==========================================================================
       
   716 TInt32 CContainerStoreSortingTable::GetIntValue( RDbRowSet& aRowSet, TMsgStoreSortByField aSortBy )
       
   717 	{
       
   718 	TInt32 value = 0;
       
   719 	if ( aSortBy !=  EMsgStoreSortByReceivedDate )
       
   720 		{
       
   721 		switch( aSortBy )
       
   722 			{
       
   723 			case EMsgStoreSortByPriority:
       
   724 			    value = aRowSet.ColInt8( iPriotiryColNum );
       
   725 				break;
       
   726 				
       
   727 			case EMsgStoreSortByFlagStatus:
       
   728 			    value = aRowSet.ColInt8( iFlagStatusColNum );
       
   729 				break;
       
   730 			
       
   731 			case EMsgStoreSortByUnRead:
       
   732 				value = aRowSet.ColInt8( iReadUnreadColNum );
       
   733 				break;
       
   734 			
       
   735 			case EMsgStoreSortBySize:
       
   736 				value = static_cast<TInt32>(aRowSet.ColUint32 ( iSizeColNum ));
       
   737 				break;
       
   738 			
       
   739 			case EMsgStoreSortByAttachment:
       
   740 				value = aRowSet.ColInt8( iAttachmentFlagColNum );
       
   741 				break;
       
   742 			
       
   743 			default:
       
   744 				break;
       
   745 			}
       
   746 		}
       
   747 	return value;
       
   748 	}
       
   749 
       
   750 // ==========================================================================
       
   751 // FUNCTION: GetIntValueL
       
   752 // ==========================================================================
       
   753 TInt32 CContainerStoreSortingTable::GetIntValueL( TMsgStoreSortByField aSortBy, TContainerId aMessageId )
       
   754 	{
       
   755     TBuf<KQuerrySizeSmall> queryString;
       
   756     
       
   757     queryString.Copy( KSortingTableMessageIdCol );
       
   758     queryString.Append( KEquals );
       
   759     queryString.AppendNum( aMessageId );
       
   760     
       
   761     User::LeaveIfError( iTable.FindL( RDbRowSet::EForwards, queryString ) );
       
   762     
       
   763     iTable.GetL();
       
   764     
       
   765     return GetIntValue( iTable, aSortBy );	
       
   766 	}
       
   767 
       
   768 // ==========================================================================
       
   769 // FUNCTION: SeekL
       
   770 // ==========================================================================
       
   771 void CContainerStoreSortingTable::SeekL( TContainerId aMessageId )
       
   772     {
       
   773     __LOG_ENTER_SUPPRESS("SeekL")
       
   774     if ( !iTable.SeekL( aMessageId ) )
       
   775         {
       
   776         __LOG_WRITE8_FORMAT1_ERROR( "Message id %x not found", aMessageId )
       
   777         User::Leave( KErrNotFound );
       
   778         }
       
   779     }
       
   780 
       
   781 // ==========================================================================
       
   782 // FUNCTION: WriteFieldsL
       
   783 // ==========================================================================
       
   784 void CContainerStoreSortingTable::WriteFieldsL( RMsgStoreSortableFields& aSortableFields, TBool aIsNew )
       
   785 	{
       
   786     
       
   787     iFieldsChanged = 0;
       
   788     
       
   789     SetFlagIfChangedInt64( aSortableFields.iReceivedDate, iReceivedDateColNum, aIsNew, EMaskReceivedDate );
       
   790 	iTable.SetColL( iReceivedDateColNum, aSortableFields.iReceivedDate );
       
   791 	
       
   792     /******************************
       
   793 	* process the read/unread flag
       
   794     *******************************/
       
   795     TUint flags = aSortableFields.iFlags;
       
   796     
       
   797 	TInt8 readFlag = EReadScoreUnread; 
       
   798 	if ( (flags & EMsgStoreFlag_Read) && (flags & EMsgStoreFlag_Read_Locally) ) 
       
   799 	    {
       
   800 	    readFlag = EReadScoreReadBoth;
       
   801 	    }
       
   802 	else if ( flags & EMsgStoreFlag_Read )
       
   803 	    {
       
   804         readFlag = EReadScoreRead;
       
   805 	    }
       
   806 	else if ( flags & EMsgStoreFlag_Read_Locally )
       
   807 	    {
       
   808         readFlag = EReadScoreReadLocal;
       
   809 	    }
       
   810 	
       
   811     SetFlagIfChangedInt8( readFlag, iReadUnreadColNum, aIsNew, EMaskReadUnread );
       
   812     
       
   813     //set the new value
       
   814 	iTable.SetColL( iReadUnreadColNum, readFlag );
       
   815     
       
   816     /******************************
       
   817     * process the priority flag
       
   818     *******************************/
       
   819 	TInt8 priority = EPriorityScoreNormal;
       
   820 	if ( flags & EMsgStoreFlag_Important )
       
   821 		{
       
   822 		priority = EPriorityScoreHigh;
       
   823 		}
       
   824 	else if ( flags & EMsgStoreFlag_Low )
       
   825 		{
       
   826 		priority = EPriorityScoreLow;
       
   827 		}
       
   828     SetFlagIfChangedInt8( priority, iPriotiryColNum, aIsNew, EMaskPriotiry );
       
   829 	iTable.SetColL( iPriotiryColNum, priority );
       
   830 	
       
   831     /******************************
       
   832      * process the follow up flag
       
   833      *******************************/
       
   834 	TInt8 flagStatus = EFlagStatusScoreNone;
       
   835 	if ( flags & EMsgStoreFlag_FollowUp )
       
   836 		{
       
   837 		flagStatus = EFlagStatusScoreFollowUp;
       
   838 		}
       
   839 	else if ( flags &  EMsgStoreFlag_FollowUpComplete )
       
   840 		{
       
   841 		flagStatus = EFlagStatusScoreFollowUpComplete;
       
   842 		}
       
   843     SetFlagIfChangedInt8( flagStatus, iFlagStatusColNum, aIsNew, EMaskFlagStatus );
       
   844 	iTable.SetColL( iFlagStatusColNum, flagStatus );
       
   845     
       
   846     //process the attachment flag
       
   847     TInt8 attachmentFalg = static_cast<TInt8>(flags & EMsgStoreFlag_Attachments);
       
   848     SetFlagIfChangedInt8( attachmentFalg, iAttachmentFlagColNum, aIsNew, EMaskAttachmentFlag );
       
   849 	iTable.SetColL( iAttachmentFlagColNum, attachmentFalg );
       
   850 	
       
   851     //if size on server is available, use it, otherwise, use the size on client
       
   852     TUint size = aSortableFields.iSizeOnServer > 0 ? aSortableFields.iSizeOnServer : aSortableFields.iSizeOnClient;
       
   853     SetFlagIfChangedUint( size, iSizeColNum, aIsNew, EMaskSize );
       
   854 	iTable.SetColL( iSizeColNum, size );
       
   855     
       
   856     //write the subject
       
   857     WriteStringFieldL( iSubjectColNum, aSortableFields.iSubject, aIsNew, EMaskSubject );
       
   858     
       
   859     //write the from
       
   860     WriteStringFieldL( iFromColNum, aSortableFields.iFrom, aIsNew, EMaskFrom );
       
   861     
       
   862     //write the to field
       
   863     WriteStringFieldL( iToColNum, aSortableFields.iTo, aIsNew, EMaskTo );
       
   864 	}
       
   865 
       
   866 // ==========================================================================
       
   867 // FUNCTION: AppendSortOrder
       
   868 // ==========================================================================
       
   869 void CContainerStoreSortingTable::AppendSortOrder( TDes& aQuery, TMsgStoreSortOrder aOrder )
       
   870 	{
       
   871 	if ( aOrder == EMsgStoreSortDescending )
       
   872 		{
       
   873 		aQuery.Append( KDesc );
       
   874 		}
       
   875 	else
       
   876 		{
       
   877 		aQuery.Append( KAsc );
       
   878 		}
       
   879 	}
       
   880 
       
   881 // ==========================================================================
       
   882 // FUNCTION: WriteStringFieldL
       
   883 // ==========================================================================
       
   884 void CContainerStoreSortingTable::WriteStringFieldL( TInt                 aColumnNum, 
       
   885                                                      const TDesC&         aString, 
       
   886                                                      TBool                aIsNew, 
       
   887                                                      TSortableFieldsMasks aMask )
       
   888     {
       
   889     __LOG_ENTER_SUPPRESS( "WriteStringFieldL" )
       
   890     __LOG_WRITE_FORMAT2_DEBUG3( "col=%d, string=%S", aColumnNum, &aString )
       
   891     
       
   892     TBool changed = ETrue;
       
   893     
       
   894     if ( !aIsNew )
       
   895         {
       
   896         RBuf curVal;
       
   897         CleanupClosePushL( curVal );
       
   898         ReadStringFieldL( iTable, aColumnNum, curVal );
       
   899         if ( curVal == aString )
       
   900             {
       
   901             changed = EFalse;
       
   902             }
       
   903         CleanupStack::PopAndDestroy( &curVal );
       
   904         }
       
   905         
       
   906     if ( changed )
       
   907         {
       
   908         iFieldsChanged |= aMask;
       
   909         }
       
   910     
       
   911     if ( aString.Length() == 0 )
       
   912         {
       
   913         iTable.SetColNullL( aColumnNum );
       
   914         }
       
   915     else if ( changed )
       
   916         {
       
   917         WriteStringL( aColumnNum, aString, IsEncrypted( iTable ) );
       
   918         }  //end if
       
   919     }
       
   920 
       
   921 // ==========================================================================
       
   922 // FUNCTION: WriteStringL
       
   923 // ==========================================================================
       
   924 void CContainerStoreSortingTable::WriteStringL( TInt         aColumnNum, 
       
   925                                                 const TDesC& aString, 
       
   926                                                 TBool        aIsRowEncrypted )
       
   927     {
       
   928     
       
   929     const TUint8* valuePtr8 = reinterpret_cast<const TUint8*>( aString.Ptr() );
       
   930     TPtrC8 string8;
       
   931     string8.Set( valuePtr8, aString.Length() * 2 );
       
   932     
       
   933     if ( aIsRowEncrypted && string8.Length() > 0 )
       
   934         {
       
   935         RBuf8 encryptedBuffer;
       
   936         encryptedBuffer.CreateL( string8.Length() + iEncryption.BlockSizeL() );
       
   937         CleanupClosePushL( encryptedBuffer );
       
   938         
       
   939         RBuf8 paddedBuffer;
       
   940         paddedBuffer.CreateL( string8.Length() + iEncryption.BlockSizeL() );       
       
   941         CleanupClosePushL( paddedBuffer );
       
   942         
       
   943         paddedBuffer.Copy( string8 );
       
   944         iEncryption.AddPaddingL( paddedBuffer, iEncryption.BlockSizeL() );
       
   945         
       
   946         iEncryption.EncryptL( paddedBuffer, encryptedBuffer );
       
   947         
       
   948         CleanupStack::PopAndDestroy( &paddedBuffer );
       
   949         
       
   950         iUtils.WriteLongColumnL( iTable, aColumnNum, encryptedBuffer );
       
   951         
       
   952         CleanupStack::PopAndDestroy( &encryptedBuffer );
       
   953         }
       
   954     else
       
   955         {
       
   956         iUtils.WriteLongColumnL( iTable, aColumnNum, string8 );
       
   957         }
       
   958     
       
   959     }
       
   960 
       
   961 
       
   962 // ==========================================================================
       
   963 // FUNCTION: ReadStringFieldL
       
   964 // ==========================================================================
       
   965 void CContainerStoreSortingTable::ReadStringFieldL( RDbRowSet& aView, TInt aColumnNum, RBuf& aString )
       
   966     {
       
   967     __LOG_ENTER_SUPPRESS( "ReadStringFieldL" )
       
   968     
       
   969     aString.SetLength( 0 );
       
   970     
       
   971     iUtils.ReadLongColumnL( aView, aColumnNum, iEncryptedBuffer );
       
   972     
       
   973     RBuf8 string8;
       
   974     CleanupClosePushL( string8 );
       
   975     string8.Create( iEncryptedBuffer.Length() );
       
   976     
       
   977     if( aString.Size() < iEncryptedBuffer.Length() )
       
   978         {        
       
   979         aString.ReAllocL( iEncryptedBuffer.Length() );
       
   980         } // end if
       
   981     
       
   982     if ( IsEncrypted( aView ) )
       
   983         {
       
   984         if( iEncryptedBuffer.Length() > 0 )
       
   985             {        
       
   986             iEncryption.DecryptL( iEncryptedBuffer, string8 );        
       
   987             iEncryption.RemovePaddingL( string8 );
       
   988             } // end if
       
   989         }
       
   990     else
       
   991         {
       
   992         string8.Copy( iEncryptedBuffer );
       
   993         }
       
   994     
       
   995     const TUint16* valuePtr16 = reinterpret_cast<const TUint16*>( string8.Ptr() );
       
   996     TPtrC16 string16;
       
   997     string16.Set( valuePtr16, string8.Length() / 2 );
       
   998     aString.Copy( string16 );
       
   999     
       
  1000     CleanupStack::PopAndDestroy( &string8 );
       
  1001     
       
  1002     __LOG_WRITE_FORMAT2_DEBUG3( "col=%d, string=%S", aColumnNum, &aString )
       
  1003     }
       
  1004 
       
  1005 
       
  1006 // ==========================================================================
       
  1007 // FUNCTION: EncryptFirstL
       
  1008 // ==========================================================================
       
  1009 TBool CContainerStoreSortingTable::EncryptFirstL( TDbBookmark& aNextRow )
       
  1010     {
       
  1011     __LOG_ENTER( "EncryptFirstL" )
       
  1012     
       
  1013     TBool found = iUtils.FindFirstEncryptedOrUnencryptedL( iTable, KSortingTableIsEncryptedCol, EFalse, aNextRow );
       
  1014     
       
  1015     __LOG_EXIT
       
  1016     
       
  1017     return found;
       
  1018     }
       
  1019 
       
  1020 // ==========================================================================
       
  1021 // FUNCTION: EncryptNextL
       
  1022 // ==========================================================================
       
  1023 TBool CContainerStoreSortingTable::EncryptNextL( TDbBookmark& aNextRow )
       
  1024     {
       
  1025     __LOG_ENTER( "EncryptNextL" )
       
  1026     
       
  1027     TBool hasMore = EFalse;
       
  1028     
       
  1029     TRAPD( err, iTable.GotoL( aNextRow ) );
       
  1030     if ( err == KErrNotFound )
       
  1031         {
       
  1032         hasMore = EncryptFirstL( aNextRow );
       
  1033         }
       
  1034     else if ( err != KErrNone )
       
  1035         {
       
  1036         User::Leave( err );        
       
  1037         }
       
  1038     else
       
  1039         {
       
  1040         iTable.GetL();
       
  1041         if ( !IsEncrypted( iTable ) )
       
  1042             {
       
  1043             RBuf string;
       
  1044             CleanupClosePushL( string );
       
  1045             
       
  1046             PrepareRowForUpdateLC();
       
  1047             
       
  1048             //process the from field
       
  1049             ReadStringFieldL( iTable, iFromColNum, string );
       
  1050             WriteStringL( iFromColNum, string, ETrue );  //encrypt before write
       
  1051             
       
  1052             //process the to field
       
  1053             ReadStringFieldL( iTable, iToColNum, string );
       
  1054             WriteStringL( iToColNum, string, ETrue );    //encrypt before write
       
  1055             
       
  1056             //process the subject field
       
  1057             ReadStringFieldL( iTable, iSubjectColNum, string );
       
  1058             WriteStringL( iSubjectColNum, string, ETrue );  //encrypt before write
       
  1059             
       
  1060             //update the isEncrypted column
       
  1061             SetEncryptedL( ETrue );
       
  1062             
       
  1063             PutRowUpdatesL();
       
  1064             
       
  1065             CleanupStack::PopAndDestroy( &string );
       
  1066             }
       
  1067         
       
  1068         hasMore = iTable.NextL();
       
  1069         if ( hasMore )
       
  1070             {
       
  1071             __LOG_WRITE_INFO("hasMore is TRUE")
       
  1072             aNextRow = iTable.Bookmark();
       
  1073             }
       
  1074         }
       
  1075 
       
  1076     __LOG_EXIT
       
  1077     
       
  1078     return hasMore;
       
  1079     }
       
  1080 
       
  1081 // ==========================================================================
       
  1082 // FUNCTION: DecryptFirstL
       
  1083 // ==========================================================================
       
  1084 TBool CContainerStoreSortingTable::DecryptFirstL( TDbBookmark& aNextRow )
       
  1085     {
       
  1086     __LOG_ENTER( "DecryptFirstL" )
       
  1087     
       
  1088     TBool found = iUtils.FindFirstEncryptedOrUnencryptedL( iTable, KSortingTableIsEncryptedCol, ETrue, aNextRow );
       
  1089     
       
  1090     __LOG_EXIT
       
  1091     
       
  1092     return found;
       
  1093     }
       
  1094 
       
  1095 // ==========================================================================
       
  1096 // FUNCTION: DecryptNextL
       
  1097 // ==========================================================================
       
  1098 TBool CContainerStoreSortingTable::DecryptNextL( TDbBookmark& aNextRow )
       
  1099     {
       
  1100     __LOG_ENTER( "DecryptNextL" )
       
  1101     
       
  1102     TBool hasMore = EFalse;
       
  1103     
       
  1104     TRAPD( err, iTable.GotoL( aNextRow ) );
       
  1105     if ( err == KErrNotFound )
       
  1106         {
       
  1107         hasMore = DecryptFirstL( aNextRow );
       
  1108         }
       
  1109     else if ( err != KErrNone )
       
  1110         {
       
  1111         User::Leave( err );        
       
  1112         }
       
  1113     else
       
  1114         {
       
  1115         iTable.GetL();
       
  1116         if ( IsEncrypted( iTable ) )
       
  1117             {
       
  1118             RBuf string;
       
  1119             CleanupClosePushL( string );
       
  1120             
       
  1121             PrepareRowForUpdateLC();
       
  1122             
       
  1123             //process the from field
       
  1124             ReadStringFieldL( iTable, iFromColNum, string );
       
  1125             WriteStringL( iFromColNum, string, EFalse );  //do not encrypt
       
  1126             
       
  1127             //process the to field
       
  1128             ReadStringFieldL( iTable, iToColNum, string );
       
  1129             WriteStringL( iToColNum, string, EFalse );  //do not encrypt
       
  1130             
       
  1131             //process the subject field
       
  1132             ReadStringFieldL( iTable, iSubjectColNum, string );
       
  1133             WriteStringL( iSubjectColNum, string, EFalse );  //do not encrypt
       
  1134             
       
  1135             //update the isEncrypted column
       
  1136             SetEncryptedL( EFalse );
       
  1137             
       
  1138             PutRowUpdatesL();
       
  1139             
       
  1140             CleanupStack::PopAndDestroy( &string );
       
  1141             }
       
  1142         
       
  1143         hasMore = iTable.NextL();
       
  1144         if ( hasMore )
       
  1145             {
       
  1146             __LOG_WRITE_INFO("hasMore is TRUE")
       
  1147             aNextRow = iTable.Bookmark();
       
  1148             }
       
  1149         }
       
  1150 
       
  1151     __LOG_EXIT
       
  1152     
       
  1153     return hasMore;
       
  1154     }
       
  1155 
       
  1156 // ==========================================================================
       
  1157 // FUNCTION: SetFlagIfChangedInt8
       
  1158 // ==========================================================================
       
  1159 void CContainerStoreSortingTable::SetFlagIfChangedInt8( TInt8 newVal, TInt aColNum, TBool aIsNew, TSortableFieldsMasks aMask )
       
  1160     {
       
  1161     TInt8 curVal = newVal;
       
  1162     if ( !aIsNew )
       
  1163         {
       
  1164         curVal = iTable.ColInt8( aColNum );
       
  1165         }
       
  1166     
       
  1167     if ( aIsNew || curVal != newVal )
       
  1168         {
       
  1169         iFieldsChanged |= aMask;
       
  1170         }
       
  1171     }
       
  1172 
       
  1173 // ==========================================================================
       
  1174 // FUNCTION: SetFlagIfChangedUint
       
  1175 // ==========================================================================
       
  1176 void CContainerStoreSortingTable::SetFlagIfChangedUint( TUint newVal, TInt aColNum, TBool aIsNew, TSortableFieldsMasks aMask )
       
  1177     {
       
  1178     TInt8 curVal = newVal;
       
  1179     if ( !aIsNew )
       
  1180         {
       
  1181         curVal = iTable.ColUint32( aColNum );
       
  1182         }
       
  1183     
       
  1184     if ( aIsNew || curVal != newVal )
       
  1185         {
       
  1186         iFieldsChanged |= aMask;
       
  1187         }
       
  1188     }
       
  1189 
       
  1190 // ==========================================================================
       
  1191 // FUNCTION: SetFlagIfChangedInt64
       
  1192 // ==========================================================================
       
  1193 void CContainerStoreSortingTable::SetFlagIfChangedInt64( TInt64 newVal, TInt aColNum, TBool aIsNew, TSortableFieldsMasks aMask )
       
  1194     {
       
  1195     TInt64 curVal = newVal;
       
  1196     if ( !aIsNew )
       
  1197         {
       
  1198         curVal = iTable.ColInt64( aColNum );
       
  1199         }
       
  1200     
       
  1201     if ( aIsNew || curVal != newVal )
       
  1202         {
       
  1203         iFieldsChanged |= aMask;
       
  1204         }
       
  1205     }
       
  1206 
       
  1207 // ==========================================================================
       
  1208 // FUNCTION: GetColumnNumL
       
  1209 // ==========================================================================
       
  1210 TInt CContainerStoreSortingTable::GetColumnNumL( TMsgStoreSortByField aSortBy )
       
  1211     {
       
  1212     TInt stringColNum = 0;
       
  1213     if ( aSortBy == EMsgStoreSortBySender )
       
  1214         {
       
  1215         stringColNum = iFromColNum;
       
  1216         }
       
  1217     else if ( aSortBy == EMsgStoreSortByRecipient )
       
  1218         {
       
  1219         stringColNum = iToColNum;
       
  1220         }
       
  1221     else if ( aSortBy == EMsgStoreSortBySubject )
       
  1222         {
       
  1223         stringColNum = iSubjectColNum;
       
  1224         }
       
  1225     else
       
  1226         {
       
  1227         User::Leave(KErrArgument);
       
  1228         }
       
  1229     return stringColNum;
       
  1230     }
       
  1231 
       
  1232 // ==========================================================================
       
  1233 // FUNCTION: IsEncrypted
       
  1234 // ==========================================================================
       
  1235 TBool CContainerStoreSortingTable::IsEncrypted( RDbRowSet& aRowSet )
       
  1236     {
       
  1237     return static_cast<TBool>( aRowSet.ColUint8( iIsEncryptedColNum ) );
       
  1238     }
       
  1239 
       
  1240 // ==========================================================================
       
  1241 // FUNCTION: SetEncryptedL
       
  1242 // ==========================================================================
       
  1243 void CContainerStoreSortingTable::SetEncryptedL( TBool aIsEncrypted )
       
  1244     {
       
  1245     iTable.SetColL( iIsEncryptedColNum, static_cast<TUint8>(aIsEncrypted) );
       
  1246     }
       
  1247