metadataengine/client/src/mdequery.cpp
changeset 0 c53acadfccc6
child 6 646a02f170b9
equal deleted inserted replaced
-1:000000000000 0:c53acadfccc6
       
     1 /*
       
     2 * Copyright (c) 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:  Query base class
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "mdequery.h"
       
    20 
       
    21 #include "mdeorderrule.h"
       
    22 #include "mdeinstanceitem.h"
       
    23 #include "mdelogiccondition.h"
       
    24 #include "mdenamespacedef.h"
       
    25 #include "mdepanic.h"
       
    26 
       
    27 
       
    28 CMdEQuery::CMdEQuery(TQueryType aType, CMdESession& aSession, CMdENamespaceDef& aNamespaceDef)
       
    29         : iQueryId( 0 ), iSession(aSession), iNamespaceDef(aNamespaceDef), iType(aType), 
       
    30           iResultMode(EQueryResultModeItem), iState(EStateNew), iError(KErrNone), 
       
    31           iObjectResult(NULL), iCountResult(0), iDistinctResults(NULL)
       
    32     {
       
    33     }
       
    34 
       
    35 
       
    36 void CMdEQuery::QueryConstructL()
       
    37     {
       
    38     iConditions = CMdELogicCondition::NewL(ELogicConditionOperatorAnd);
       
    39     }
       
    40 
       
    41 
       
    42 CMdEQuery::~CMdEQuery()
       
    43 	{
       
    44 	Cancel();
       
    45   
       
    46     // This will destroy the entire conditions tree.
       
    47    	delete iConditions;
       
    48 
       
    49    	const TInt count = iResults.Count();
       
    50    	
       
    51     // Destroy all result items this query instance owns.
       
    52     for(TInt i = 0; i < count; ++i)
       
    53         {
       
    54         if(iResults[i].iOwned)
       
    55             {
       
    56             delete iResults[i].iItem;
       
    57             }
       
    58         }
       
    59 
       
    60     // Destroy object result item this query instance owns  
       
    61     if(iObjectResult.iItem)
       
    62         {
       
    63         if(iObjectResult.iOwned)
       
    64         	{
       
    65         	delete iObjectResult.iItem;
       
    66         	iObjectResult.iItem = NULL;
       
    67         	}
       
    68         }
       
    69 
       
    70 	iCountResult = 0;
       
    71 
       
    72 	iResults.Reset();
       
    73 	iResults.Close();
       
    74 	iIdResults.Reset();
       
    75 	iIdResults.Close();
       
    76 
       
    77     iOrderRules.Close();
       
    78     iObservers.Close();
       
    79 
       
    80    	delete iDistinctResults;
       
    81 	}
       
    82 
       
    83 
       
    84 EXPORT_C TQueryType CMdEQuery::Type() const
       
    85     {
       
    86     return iType;
       
    87     }
       
    88 
       
    89 EXPORT_C CMdENamespaceDef& CMdEQuery::NamespaceDef() const
       
    90 	{
       
    91 	return iNamespaceDef;
       
    92 	}
       
    93     
       
    94 EXPORT_C CMdESession& CMdEQuery::Session() const
       
    95     {
       
    96     return iSession;
       
    97     }
       
    98 
       
    99 
       
   100 EXPORT_C CMdELogicCondition& CMdEQuery::Conditions() const
       
   101     {
       
   102     __ASSERT_DEBUG(iConditions , 
       
   103                     TMdEPanic::Panic(TMdEPanic::EInternal));
       
   104     return *iConditions;
       
   105     }
       
   106     
       
   107     
       
   108 EXPORT_C void CMdEQuery::AppendOrderRuleL(const TMdEOrderRule& aRule)
       
   109     {
       
   110     AssertInState(EStateNew);
       
   111     iOrderRules.AppendL( aRule );
       
   112     }
       
   113 
       
   114 
       
   115 EXPORT_C void CMdEQuery::InsertOrderRuleL(const TMdEOrderRule& aRule, TInt aPos)
       
   116     {
       
   117     AssertInState(EStateNew);
       
   118     User::LeaveIfError( iOrderRules.Insert(aRule, aPos) );   
       
   119     }
       
   120 
       
   121 
       
   122 EXPORT_C TInt CMdEQuery::OrderRuleCount() const
       
   123     {
       
   124     return iOrderRules.Count();    
       
   125     }
       
   126 
       
   127 
       
   128 EXPORT_C void CMdEQuery::RemoveOrderRule(TInt aIndex)
       
   129     {
       
   130     AssertInState(EStateNew);
       
   131     iOrderRules.Remove(aIndex);
       
   132     }
       
   133 
       
   134 
       
   135 EXPORT_C TMdEOrderRule CMdEQuery::OrderRule(TInt aIndex) const
       
   136     {
       
   137     return iOrderRules[aIndex];        
       
   138     }
       
   139     
       
   140 
       
   141 EXPORT_C void CMdEQuery::FindL(TUint aMaxCount, TUint aNotifyCount)
       
   142     {
       
   143     AssertNotInState(EStateError);
       
   144 
       
   145 	// leave if query already running    
       
   146     if(iState == EStateSearching)
       
   147         {
       
   148         User::Leave(KErrNotReady);
       
   149         }
       
   150     
       
   151     const TInt count = iResults.Count();
       
   152     
       
   153 	// clear old results
       
   154 	// Destroy all result items this query instance owns.
       
   155     for(TInt i = 0; i < count; ++i)
       
   156         {
       
   157         if(iResults[i].iOwned)
       
   158             {
       
   159             delete iResults[i].iItem;
       
   160             }
       
   161         }
       
   162 	iResults.Reset();
       
   163 
       
   164     // Destroy object result item this query instance owns  
       
   165     if( iObjectResult.iItem )
       
   166         {
       
   167         if( iObjectResult.iOwned )
       
   168         	{
       
   169         	delete iObjectResult.iItem;
       
   170         	iObjectResult.iItem = NULL;
       
   171         	}
       
   172         }
       
   173 
       
   174 	iIdResults.Reset();
       
   175 
       
   176     if( iDistinctResults )
       
   177     	{    	
       
   178     	delete iDistinctResults;
       
   179     	iDistinctResults = NULL;
       
   180     	}
       
   181 
       
   182 	iCountResult = 0;
       
   183 	// end of clearing
       
   184 
       
   185     DoFindL(aMaxCount, aNotifyCount);
       
   186     }
       
   187 
       
   188 
       
   189 EXPORT_C void CMdEQuery::Cancel()
       
   190     {
       
   191     // is query incomplete before canceling
       
   192     TBool incomplete = !IsComplete();
       
   193     
       
   194     if( incomplete )
       
   195     	{
       
   196     	DoCancel();
       
   197     	}
       
   198 
       
   199 	if( iConditions )
       
   200 		{		
       
   201     	iConditions->SetLocked( EFalse );
       
   202 		}
       
   203     
       
   204     if( incomplete || IsComplete() == EFalse )
       
   205     	{
       
   206     	iState = EStateError;
       
   207     	NotifyCompleted( KErrCancel );
       
   208     	}
       
   209     }
       
   210 
       
   211 
       
   212 EXPORT_C TBool CMdEQuery::IsComplete() const
       
   213     {
       
   214     return iState != EStateSearching ? ETrue : EFalse;
       
   215     }
       
   216 
       
   217 
       
   218 EXPORT_C TInt CMdEQuery::Error() const
       
   219     {
       
   220     return iError;
       
   221     }
       
   222 
       
   223 
       
   224 EXPORT_C TInt CMdEQuery::Count() const
       
   225     {
       
   226     if ( iResultMode == EQueryResultModeItem || iResultMode == EQueryResultModeObjectWithFreetexts )
       
   227         {
       
   228         return iResults.Count();
       
   229         }
       
   230     else if ( iResultMode == EQueryResultModeId )
       
   231         {
       
   232         return iIdResults.Count();
       
   233         }
       
   234     else if ( iResultMode == EQueryResultModeDistinctValues )
       
   235     	{
       
   236     	if( iDistinctResults )
       
   237     		{    		
       
   238     		return iDistinctResults->Count();
       
   239     		}
       
   240     	else
       
   241     		{
       
   242     		return 0;
       
   243     		}
       
   244     	}
       
   245     else // count query
       
   246         {
       
   247         return iCountResult;
       
   248         }
       
   249     }
       
   250 
       
   251 EXPORT_C CMdEItem& CMdEQuery::ResultItem(TInt aIndex) const
       
   252     {
       
   253     return *iResults[aIndex].iItem;
       
   254     }
       
   255 
       
   256 
       
   257 EXPORT_C TItemId CMdEQuery::ResultId(TInt aIndex) const
       
   258     {
       
   259     return iIdResults[aIndex];
       
   260     }
       
   261 
       
   262 EXPORT_C const RArray<TItemId>& CMdEQuery::ResultIds() const
       
   263 	{
       
   264 	return iIdResults;
       
   265 	}
       
   266 
       
   267 EXPORT_C CMdEItem* CMdEQuery::TakeOwnershipOfResult(TInt aIndex)
       
   268     {
       
   269     // Panic if an item is taken twice.
       
   270     __ASSERT_DEBUG(iResults[aIndex].iOwned,
       
   271                     TMdEPanic::Panic(TMdEPanic::EQueryDoesntOwnResult));
       
   272 
       
   273     // Applies only instance ownership
       
   274     if ( iResultMode == EQueryResultModeItem || iResultMode == EQueryResultModeObjectWithFreetexts )
       
   275         {
       
   276         iResults[aIndex].iOwned = EFalse;
       
   277         return static_cast<CMdEItem*>( iResults[aIndex].iItem );
       
   278         }
       
   279     else
       
   280     	{
       
   281     	return NULL;
       
   282     	}
       
   283     }
       
   284 
       
   285 
       
   286 EXPORT_C TBool CMdEQuery::OwnsResult(TInt aIndex)
       
   287     {
       
   288     return iResults[aIndex].iOwned;
       
   289     }
       
   290 
       
   291 
       
   292 EXPORT_C CMdEQuery::TState CMdEQuery::State() const
       
   293     {
       
   294     return iState;
       
   295     }
       
   296 
       
   297 void CMdEQuery::DoCancel()
       
   298     {
       
   299     SetState( EStateError );
       
   300     }
       
   301 
       
   302 void CMdEQuery::SetState(TState aState)
       
   303     {
       
   304     if(aState == iState)
       
   305         {
       
   306         // Already in the specified state.
       
   307         return;
       
   308         }
       
   309         
       
   310     // It's impossible to get out of the Error state.
       
   311     AssertNotInState(EStateError);
       
   312 
       
   313     // It's impossible to return to the New state.
       
   314     __ASSERT_ALWAYS(aState > EStateFirst && aState < EStateLast && 
       
   315                     aState != EStateNew, 
       
   316                     TMdEPanic::Panic(TMdEPanic::EInternal));
       
   317 
       
   318     // Move out of the old state.
       
   319     switch(iState)
       
   320         {
       
   321         case EStateNew:
       
   322             // Moving out of the New state causes the search conditions to 
       
   323             // be locked.
       
   324             iConditions->SetLocked();
       
   325             break;    
       
   326             
       
   327         case EStateSearching:
       
   328             // Searching has been completed.
       
   329             break;
       
   330 
       
   331         case EStateCompleted:
       
   332             // Searching is been restarted.
       
   333             break;
       
   334             
       
   335         default:
       
   336             break;
       
   337         }
       
   338 
       
   339     // Set the new state.
       
   340     iState = aState;
       
   341 
       
   342     // Move into the new state.
       
   343     switch(iState)
       
   344         {
       
   345         case EStateSearching:
       
   346             // Searching has been started.
       
   347             break;
       
   348 
       
   349         case EStateCompleted:
       
   350             // Searching has been completed.
       
   351             break;
       
   352 
       
   353         case EStateError:
       
   354             // The query has entered the Error state.
       
   355             break;
       
   356             
       
   357         default:
       
   358             break;
       
   359         }    
       
   360     }
       
   361 
       
   362 
       
   363 void CMdEQuery::AssertInState(TState aState)
       
   364     {
       
   365     __ASSERT_ALWAYS(aState == iState,
       
   366                     TMdEPanic::Panic(TMdEPanic::EQueryStateIllegalOperation));
       
   367     }
       
   368     
       
   369 
       
   370 void CMdEQuery::AssertNotInState(TState aState)
       
   371     {
       
   372     __ASSERT_ALWAYS(aState != iState,
       
   373                     TMdEPanic::Panic(TMdEPanic::EQueryStateIllegalOperation));
       
   374     }
       
   375     
       
   376 
       
   377 void CMdEQuery::NotifyNewResultsL(const RPointerArray<CMdEInstanceItem>& aNewResults)
       
   378     {
       
   379     __ASSERT_ALWAYS(iState != EStateNew && iState != EStateError,
       
   380                     TMdEPanic::Panic(TMdEPanic::EInternal));
       
   381 
       
   382 	TInt firstNewItemIndex = iResults.Count();
       
   383 	TInt i = 0;
       
   384 
       
   385     // Query gets ownership of the results only if don't leave.
       
   386     // If a leave occurs, the results array is restored to its original
       
   387     // condition before any new results were added.
       
   388 
       
   389     const TInt oldCount = iResults.Count();
       
   390 
       
   391     TRAPD(err, AppendResultsL(aNewResults));
       
   392     if (err != KErrNone)
       
   393         {
       
   394         // Cleanup: remove the ones that were added.
       
   395         while (iResults.Count() > oldCount)
       
   396             {
       
   397             iResults.Remove(iResults.Count() - 1);
       
   398             }
       
   399         // Leave with the same error.
       
   400         User::Leave(err);
       
   401         }
       
   402 
       
   403     // After this no leave can occur.
       
   404 
       
   405     const TInt newCount = aNewResults.Count();
       
   406     
       
   407     // Transfer ownership of the results to the query.
       
   408     for (i = 0; i < newCount; i++)
       
   409         {
       
   410         iResults[firstNewItemIndex + i].iOwned = ETrue;
       
   411         }
       
   412 
       
   413     const TInt observerCount = iObservers.Count();
       
   414     
       
   415 	for (i = 0; i < observerCount; i++)
       
   416 		{
       
   417 		MMdEQueryObserver* observer = iObservers[i];
       
   418 		observer->HandleQueryNewResults( *this, firstNewItemIndex, newCount );
       
   419 		}
       
   420     }
       
   421 
       
   422 
       
   423 void CMdEQuery::NotifyNewResultsL(const RArray<TItemId>& aNewResults)
       
   424     {
       
   425     __ASSERT_ALWAYS(iState != EStateNew && iState != EStateError,
       
   426                     TMdEPanic::Panic(TMdEPanic::EInternal));
       
   427 
       
   428 	TInt firstNewItemIndex = iIdResults.Count();
       
   429 	TInt i = 0;
       
   430 
       
   431     const TInt oldCount = iIdResults.Count();
       
   432 
       
   433     TRAPD(err, AppendResultsL(aNewResults));
       
   434     if (err != KErrNone)
       
   435         {
       
   436         // Cleanup: remove the ones that were added.
       
   437         while (iIdResults.Count() > oldCount)
       
   438             {
       
   439             iIdResults.Remove(iIdResults.Count() - 1);
       
   440             }
       
   441         // Leave with the same error.
       
   442         User::Leave(err);
       
   443         }
       
   444 
       
   445     const TInt observerCount = iObservers.Count();
       
   446     
       
   447 	for (i = 0; i < observerCount; i++)
       
   448 		{
       
   449 		MMdEQueryObserver* observer = iObservers[i];
       
   450 		observer->HandleQueryNewResults(*this, firstNewItemIndex, 
       
   451                                         aNewResults.Count());
       
   452 		}
       
   453     }
       
   454 
       
   455 void CMdEQuery::NotifyNewResultsL( const CDesCArray& aNewResults )
       
   456     {
       
   457 	TInt firstNewItemIndex = aNewResults.Count();
       
   458 	TInt i = 0;
       
   459 
       
   460 	TInt oldCount = 0;
       
   461 	if( iDistinctResults )
       
   462 		{
       
   463 		oldCount = iDistinctResults->Count();
       
   464 		}
       
   465 	else
       
   466 		{
       
   467 	    iDistinctResults = new (ELeave) CDesCArrayFlat( 8 );
       
   468 		}
       
   469 
       
   470     TRAPD( err, AppendResultsL( aNewResults ) );
       
   471     if( err != KErrNone )
       
   472         {
       
   473         // Cleanup: remove the ones that were added.
       
   474         while( iDistinctResults->Count() > oldCount )
       
   475             {
       
   476             iDistinctResults->Delete( iDistinctResults->Count() - 1 );
       
   477             }
       
   478         // Leave with the same error.
       
   479         User::Leave(err);
       
   480         }
       
   481 
       
   482     const TInt observerCount = iObservers.Count();
       
   483     
       
   484 	for (i = 0; i < observerCount; i++)
       
   485 		{
       
   486 		MMdEQueryObserver* observer = iObservers[i];
       
   487 		observer->HandleQueryNewResults( *this, firstNewItemIndex, 
       
   488                                          aNewResults.Count() );
       
   489 		}
       
   490     }
       
   491 
       
   492 void CMdEQuery::NotifyNewResults(TUint32 aResult)
       
   493 	{
       
   494     // Don't call observer here, since only one result
       
   495     iCountResult = aResult;
       
   496     }
       
   497 
       
   498 void CMdEQuery::AppendResultsL(const RPointerArray<CMdEInstanceItem>& aNewResults)
       
   499     {
       
   500     const TInt newResultCount = aNewResults.Count();
       
   501     iResults.ReserveL( iResults.Count() + newResultCount );
       
   502 
       
   503 	for (TInt i = 0; i < newResultCount; ++i)
       
   504         {
       
   505         // At this point the ownership isn't transferred yet.
       
   506         TResult result( aNewResults[i] );
       
   507 		iResults.AppendL( result );
       
   508 		}
       
   509     }
       
   510 
       
   511 void CMdEQuery::AppendResultsL(const RArray<TItemId>& aNewResults)
       
   512     {
       
   513     const TInt newResultCount = aNewResults.Count();
       
   514     iIdResults.ReserveL( iResults.Count() + newResultCount );
       
   515 
       
   516 	for (TInt i = 0; i < newResultCount; ++i)
       
   517         {
       
   518         // Add new results to query's list
       
   519 		iIdResults.AppendL( aNewResults[i] );
       
   520 		}
       
   521     }
       
   522 
       
   523 void CMdEQuery::AppendResultsL( const CDesCArray& aNewResults )
       
   524     {
       
   525     const TInt count = aNewResults.Count();
       
   526     
       
   527     for ( TInt i = 0; i < count; ++i )
       
   528     	{
       
   529     	iDistinctResults->AppendL( aNewResults[i] );
       
   530     	}
       
   531     }
       
   532     
       
   533 void CMdEQuery::NotifyCompleted(TInt aError)
       
   534     {
       
   535     // The implementation must move out of the Searching state before
       
   536     // notifying the base class.
       
   537     __ASSERT_ALWAYS(iState != EStateSearching, TMdEPanic::Panic(TMdEPanic::EInternal));
       
   538 
       
   539     // Update the latest error code.
       
   540     iError = aError;
       
   541 	for (TInt i = iObservers.Count() - 1; i >= 0; --i)
       
   542 		{
       
   543 		MMdEQueryObserver* observer = iObservers[i];
       
   544 		observer->HandleQueryCompleted(*this, aError);
       
   545 		}
       
   546     }
       
   547 
       
   548 
       
   549 EXPORT_C void CMdEQuery::AddObserverL(MMdEQueryObserver& aObserver)
       
   550 	{
       
   551 	__ASSERT_ALWAYS(iObservers.Find(&aObserver) == KErrNotFound,
       
   552 				    TMdEPanic::Panic(TMdEPanic::EQueryDuplicateObserver));
       
   553 
       
   554 	iObservers.AppendL( &aObserver );
       
   555 	}
       
   556 
       
   557 
       
   558 EXPORT_C void CMdEQuery::RemoveObserver(MMdEQueryObserver& aObserver)
       
   559 	{
       
   560 	TInt index = iObservers.Find(&aObserver);
       
   561 
       
   562 	__ASSERT_ALWAYS(index != KErrNotFound,
       
   563 				    TMdEPanic::Panic(TMdEPanic::EQueryUnknownObserver));
       
   564 
       
   565 	iObservers.Remove(index);
       
   566 	}
       
   567 
       
   568 
       
   569 EXPORT_C void CMdEQuery::SetResultMode( TQueryResultMode aMode )
       
   570     {    
       
   571     iResultMode = aMode;
       
   572     }
       
   573 
       
   574 EXPORT_C TQueryResultMode CMdEQuery::ResultMode() const
       
   575     {
       
   576     return iResultMode;
       
   577     }
       
   578 EXPORT_C CMdEItem& CMdEQuery::ResultObjectItem() const
       
   579     {
       
   580     return *iObjectResult.iItem;
       
   581     }
       
   582 
       
   583 EXPORT_C TPtrC16 CMdEQuery::ResultDistinctValue(TInt aIndex) const
       
   584 	{
       
   585 	return (*iDistinctResults)[aIndex];
       
   586 	}
       
   587 
       
   588 RArray<TMdEOrderRule>& CMdEQuery::OrderRules()
       
   589 	{
       
   590 	return iOrderRules;
       
   591 	}