srsf/nssvasapi/nssvasdb/inc/nssvascvasdb.h
changeset 13 57b735022c18
parent 1 b13cd05eeb2f
equal deleted inserted replaced
1:b13cd05eeb2f 13:57b735022c18
     1 /*
       
     2 * Copyright (c) 2004-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:  
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #ifndef CNSSVASDB_H
       
    20 #define CNSSVASDB_H
       
    21 
       
    22 #include <d32dbms.h>
       
    23 #include <e32def.h>
       
    24 #include "nssvascvasdbsrvdef.h"
       
    25 #include "nssvasdbkonsts.h"
       
    26 #include "nssvascvasdbdataexchanger.h"
       
    27 #include "nssvastvasdbcreator.h"
       
    28 #include "nssvascspeechitembuilder.h"
       
    29 #include "nssvasccontextbuilder.h"
       
    30 
       
    31 #include "nssvasctag.h"
       
    32 
       
    33 
       
    34 class CNssVasBackupObserver;
       
    35 
       
    36 
       
    37 // CLASS DECLARATION
       
    38 
       
    39 /**
       
    40 *
       
    41 *  The CNssVasDb class definition. 
       
    42 * 
       
    43 *  @lib NssVASApi.lib
       
    44 *  @since 2.8
       
    45 */
       
    46 NONSHARABLE_CLASS( CNssVasDb ) : public CBase
       
    47     {
       
    48 public:
       
    49 	
       
    50     /**
       
    51     * Two-phased constructor.
       
    52 	* @since 2.0
       
    53 	* @param aClient - client thread
       
    54 	* @param aServer - vasdb server
       
    55     */
       
    56 	static CNssVasDb* NewL( CNssContextBuilder& aContextBuilder,
       
    57 	                        CNssSpeechItemBuilder& aSpeechItemBuilder );
       
    58 
       
    59 	/**
       
    60     * Destructor.  
       
    61     * @since 2.0
       
    62     * @param 
       
    63     * @return void
       
    64     */
       
    65 	~CNssVasDb();	
       
    66 
       
    67 public:
       
    68 
       
    69     /**
       
    70     * Read a context from the database. If context with this name is not present,
       
    71     * leave with KErrNotFound.
       
    72     * @since 2.8
       
    73     * @param aName the name of the context
       
    74     * @return CArrayFixFlat<CNssContext> A List of contexts.
       
    75     */
       
    76     CArrayPtrFlat<CNssContext>* GetContextByNameL( const TDesC& aName );
       
    77 
       
    78     /**
       
    79     * Read all global contexts from the database. Contexts have a flag, which
       
    80     * tells if they are global or not.
       
    81     * Global context = "Context, which is recognized when the user presses
       
    82     *                   the voice key"
       
    83     * Currently, SIND names and command words are global.
       
    84     * @since 2.8
       
    85     * @return CArrayFixFlat<CNssContext> A List of contexts.
       
    86     */
       
    87     CArrayPtrFlat<CNssContext>* GetGlobalContexts();
       
    88 
       
    89     /**
       
    90     * Read all global contexts from the database. 
       
    91     * @since 2.8
       
    92     * @return CArrayFixFlat<CNssContext> A List of contexts.
       
    93     */
       
    94     CArrayPtrFlat<CNssContext>* GetAllContexts();
       
    95 
       
    96     /**
       
    97     * Create a new database.
       
    98     * @since 2.8
       
    99     * @return none
       
   100     */
       
   101 	void CreateDatabaseL();
       
   102 
       
   103     /**
       
   104     * Open database.
       
   105     * @since 2.8
       
   106     * @return none
       
   107     */
       
   108 	void OpenDatabaseL();
       
   109 
       
   110     /**
       
   111     * Close database.
       
   112     * @since 2.8
       
   113     * @return Symbian-wide error codes
       
   114     */
       
   115 	TInt  CloseDatabase();
       
   116 
       
   117     /**
       
   118     * Get model bank id and lexicon id from some context. All contexts
       
   119     * have the same model bank id.
       
   120     * @since 2.8
       
   121     * @param aModelBankId Place to store model bank ID.
       
   122     * @param aLexiconId   Place to store lexicon ID.
       
   123     * @return ETrue if successful (leaves, if not successful)
       
   124     */
       
   125 	TBool GetModelBankIdLexiconIdL( TUint32& aModelBankId, TUint32& aLexiconId );
       
   126 
       
   127     /**
       
   128     * Check if a context has tags or not.
       
   129     * @since 2.8
       
   130     * @param aContextId context id
       
   131     * @return ETrue if there are tags, EFalse if not.
       
   132     */
       
   133     TBool TagExistL( TInt aContextId );
       
   134 
       
   135     /**
       
   136     * How many tags there are in this context?
       
   137     * @since 2.8
       
   138     * @param aContextId context id
       
   139     * @return The number of tags
       
   140     */
       
   141 	TInt  TagCountL( TInt aContextId );
       
   142 
       
   143     /**
       
   144     * Saves a context. When a context is save, a unique Context ID
       
   145     * is assigned to it. This ID is returned in aNewContextId reference variable.
       
   146     * @since 2.8
       
   147     * @param aContext The context
       
   148     * @param aNewContextId Place to store the newly assigned context ID.
       
   149     * @return none
       
   150     */
       
   151     void SaveContextL( CNssContext& aContext, TInt& aNewContextId );
       
   152 
       
   153     /**
       
   154     * Updates the information of an existing context.
       
   155     * @since 2.8
       
   156     * @param aContext The context
       
   157     * @return none
       
   158     */
       
   159 	void UpdateContextL( CNssContext& aContext );
       
   160 
       
   161     /**
       
   162     * Updates the client data of an existing context.
       
   163     * @since 2.8
       
   164     * @param aContext The context
       
   165     * @return none
       
   166     */
       
   167 	void UpdateContextClientDataL( CNssContext& aContext );
       
   168 
       
   169     /**
       
   170     * Saves a tag. When a tag is saved, a unique ID is assigned to it
       
   171     * @since 2.8
       
   172     * @param aContext The context
       
   173     * @return none
       
   174     */
       
   175     void SaveTagL( CNssTag& aTag, TInt& aNewId );
       
   176 
       
   177     /**
       
   178     * Get tag reference list. A tag reference contains only the name and the ID.
       
   179     * @since 2.8
       
   180     * @param aContextId  The context
       
   181     * @return Tag references for all tags in the context.
       
   182     */
       
   183 	TNssTagReferenceListArray* GetTagReferenceListL( TInt aContextId );
       
   184 
       
   185     /**
       
   186     * Get a tag, given the grammar ID and rule ID of the tag.
       
   187     * Used when resolving recognition result.
       
   188     * @since 2.8
       
   189     * @param aGrammarIdRuleId Dedicated struct to pass gramamr ID and rule ID.
       
   190     * @return Tag with the given rule ID.
       
   191     */
       
   192 	CArrayPtrFlat<CNssTag>* GetTagL( TNssGrammarIdRuleId aGrammarIdRuleId );
       
   193 
       
   194     /**
       
   195     * Get a list of tags tag, given the list of grammar IDs and rule IDs of 
       
   196     * the tags.
       
   197     * Used when resolving recognition result.
       
   198     * @since 2.8
       
   199     * @param aGrammarIdRuleIds Array of gramamr ID and rule ID pairs
       
   200     * @return List of tags with the given rule ID and grammar ID.
       
   201     */
       
   202 	CArrayPtrFlat<CNssTag>* GetTagsL( TNssGrammarIdRuleIdListArray& aGrammarIdRuleIds );
       
   203 	
       
   204 	/**
       
   205     * Get a tag, given the recognition phrase.
       
   206     * @since 2.8
       
   207     * @param aName Recognition phrase of the tag
       
   208     * @return Tag with the given rule ID.
       
   209     */
       
   210 	CArrayPtrFlat<CNssTag>* GetTagL( const TDesC& aName );
       
   211 
       
   212     /**
       
   213     * Get a list of tags based on RRD integer data.
       
   214     * The client can save 5 integer for each tag. This function gets those tags,
       
   215     * which have value aNum in position aPosition.
       
   216     * @since 2.8
       
   217     * @param aContextId Context ID
       
   218     * @param aNum The number we search for.
       
   219     * @param aPosition RRD integer position. There are 5 slots, numbered 0-4.
       
   220     * @return List of tags
       
   221     */
       
   222 	CArrayPtrFlat<CNssTag>* GetTagL( TInt aContextId, TInt aNum, TInt aPosition );
       
   223 
       
   224     /**
       
   225     * Get a list of tags based on RRD text data.
       
   226     * The client can save 5 strings for each tag. This function gets those tags,
       
   227     * for which the text data at slot aPosition matches to aText.
       
   228     * @since 2.8
       
   229     * @param aContextId Context ID
       
   230     * @param aText The text we search for.
       
   231     * @param aPosition RRD text position. There are 5 slots, numbered 0-4.
       
   232     * @return List of tags
       
   233     */
       
   234 	CArrayPtrFlat<CNssTag>* GetTagL( TInt aContextId, const TDesC& aText, TInt aPosition );
       
   235 
       
   236     /**
       
   237     * Gets all tags from this context.
       
   238     * @since 2.8
       
   239     * @param aContext A context
       
   240     * @return List of tags
       
   241     */
       
   242 	CArrayPtrFlat<CNssTag>* GetTagL( const CNssContext& aContext );
       
   243 
       
   244     /**
       
   245     * Get a tag, given a tag ID.
       
   246     * @since 2.8
       
   247     * @param aTagId tag id
       
   248     * @return List of tags
       
   249     */    
       
   250 	CArrayPtrFlat<CNssTag>* GetTagL( TUint32 aTagId );
       
   251 
       
   252     /**
       
   253     * Method to get the default model bank id
       
   254     * @since 2.8
       
   255     * @param aId reference where to put the model bank Id
       
   256     * @return symbian-wide error code
       
   257     */
       
   258     void GetDefaultModelBankIdL( TUint32& aId );
       
   259 
       
   260     /**
       
   261     * Modifies the context table so that all contexts start using
       
   262     * the new id as their model bank id.
       
   263     * @since 2.8
       
   264     * @param aNewId new model bank id
       
   265     * @return symbian-wide error code
       
   266     */
       
   267     void ResetModelBankL( TUint32 aNewId );
       
   268 
       
   269     /**
       
   270     * Deletes a context.
       
   271     * @since 2.8
       
   272     * @param aName Name of the context to be deleted.
       
   273     * @return none
       
   274     */
       
   275 	void DeleteContextL( const TDesC& aName );
       
   276 
       
   277     /**
       
   278     * Deletes a tag.
       
   279     * @since 2.8
       
   280     * @param aName Name of the tag to be deleted.
       
   281     * @return none
       
   282     */
       
   283 	void DeleteTagL( const TDesC& aName );
       
   284 
       
   285     /**
       
   286     * Deletes a tag.
       
   287     * @since 2.8
       
   288     * @param aTagId The unique ID of the tag.
       
   289     * @return none
       
   290     */
       
   291 	void DeleteTagL( TUint32 aTagId );
       
   292 
       
   293     /**
       
   294     * Deletes a tag without starting a transaction.
       
   295     * @since 2.8
       
   296     * @param aTagId Tag id
       
   297     * @return none
       
   298     */
       
   299     void DeleteTagInsideTransactionL( TUint32 aTagId );
       
   300 
       
   301     /**
       
   302     * Deletes a list of tags.
       
   303     * @since 2.8
       
   304     * @param aTagArray A list of tags
       
   305     */
       
   306     void DeleteTagsL( const RArray<TUint32>& aTagIdArray );
       
   307 
       
   308     /**
       
   309     * Updates the rule ids of some tags.
       
   310     * The rule ID changes, if an already trained tag is retrained.
       
   311     * @since 2.8
       
   312     * @param aRetrainedTags Tags which need an update for rule ID.
       
   313     * @return TInt Success status.
       
   314     */
       
   315     void UpdateTagRuleIDsL( const RArray<TNssSpeechItem>& aRetrainedTags );
       
   316 
       
   317     /**
       
   318     * Saves a list of tags.
       
   319     * @since 2.8
       
   320     * @param aTagArray A list of trained tags
       
   321     * @param aTagIdArray The newly assigned Tag IDs are placed here.
       
   322     */
       
   323     void SaveTagsL( CArrayPtrFlat<CNssTag>* aTagArray, RArray<TInt>& aTagIdArray );
       
   324 
       
   325 	/**
       
   326 	* Permit transactions. Used when e.g. backup or restore begins 
       
   327 	* @since 3.1
       
   328 	*/
       
   329 	void LockTransactionsL();
       
   330 
       
   331 	/**
       
   332 	* Allow transactions. Used when e.g. backup or restore ends 
       
   333 	* @since 3.1
       
   334 	*/
       
   335 	void UnlockTransactionsL();
       
   336 	
       
   337 private:
       
   338 
       
   339     /*
       
   340     * First phase constructor.
       
   341     */
       
   342     CNssVasDb( CNssContextBuilder& contextBuilder,
       
   343 	           CNssSpeechItemBuilder& speechItemBuilder );
       
   344 
       
   345     /*
       
   346     * Second phase constructor.
       
   347     */
       
   348 	void ConstructL();
       
   349 
       
   350     /*
       
   351     * Get a list of contexts.
       
   352     * The pipeline for the public GetContextL variants is:
       
   353     * (1) Turn parameters into an SQL query
       
   354     * (2) Call this function
       
   355     */
       
   356     CArrayPtrFlat<CNssContext>* GetContextL( const TDesC& aSqlStatement );
       
   357 
       
   358     /*
       
   359     * "workhorse" function for the the previous one. The previous function
       
   360     * takes care of locking and unlocking the database.
       
   361     */
       
   362     CArrayPtrFlat<CNssContext>* GetContextInsideTransactionL( const TDesC& aSqlQuery );
       
   363 
       
   364     /*
       
   365     * Reads a list of contexts after query. RDbView object is created
       
   366     * as a result of a query.
       
   367     */
       
   368 	void FillContextListArrayL( RDbView& aView, CArrayPtrFlat<CNssContext>& aContextList );
       
   369 
       
   370     /*
       
   371     * Get the RRD data for a tag. The RRD table is separate from tag table.
       
   372     */
       
   373 	void FillRRDL( TUint32 aTagId, CNssRRD& aRRD );
       
   374 
       
   375     /*
       
   376     * Get a speech item after a query (which created the RDbView object).
       
   377     * In addition to core tags data, it fetces the context and the RRD data
       
   378     * form separate arrays.
       
   379     */
       
   380 	void FillSpeechItemL( RDbView& aView, CNssSpeechItem& aSpeechItem, TInt& aContextId );
       
   381 
       
   382     /*
       
   383     * Get a context after query.
       
   384     */
       
   385 	void FillContextL( RDbView& aView, CNssContext &aContext );
       
   386 
       
   387     /*
       
   388     * (1) Makes a tag out of context, speech item and RRD data.
       
   389     * (2) Adds the tag to aTagList.
       
   390     */
       
   391 	void FillTagListArrayL( CArrayPtrFlat<CNssTag>& aTagList, 
       
   392 	                        CNssContext* aContext,
       
   393 	                        CNssSpeechItem* aSpeechItem,
       
   394 	                        CNssRRD* aRRD );
       
   395 
       
   396     /*
       
   397     * Saves a context.
       
   398     */
       
   399     void SaveContextL( CNssContext* aContext, TInt& aNewContextId );
       
   400 
       
   401     /*
       
   402     * Saves RRD data.
       
   403     */
       
   404 	void SaveRRDL(CNssRRD* aRRD);	
       
   405 
       
   406     /*
       
   407     * Saves a tags. When a tag is saved for the first time, an ID is assigned
       
   408     * to it. This ID is stored to aTagId.
       
   409     */
       
   410 	void SaveTagL( CNssTag* aTag, TInt& aTagId);
       
   411 
       
   412     /*
       
   413     * The public GetTagL / GetTagListL variants make an SQL query from their
       
   414     * parameters and call this function to do the rest.
       
   415     */
       
   416     CArrayPtrFlat<CNssTag>* GetTagListByQueryL(const TDesC& aSqlQuery);
       
   417 
       
   418     /*
       
   419     * Deletes RRD data
       
   420     */
       
   421 	void DeleteRRDL( TUint32 aTagId );
       
   422 
       
   423     /*
       
   424     * Update an existing tag. Locks database.
       
   425     */
       
   426 	void UpdateTagL( CNssTag& aTag );
       
   427 
       
   428     /*
       
   429     * Update an existing tag.
       
   430     */
       
   431 	void UpdateTagInTransactionL( CNssTag& aTag );
       
   432 
       
   433     /*
       
   434     * Updates an existing context. Does not lock database.
       
   435     */
       
   436 	void DoUpdateContextL( CNssContext& aContext );
       
   437 
       
   438     /*
       
   439     * Updates the client data of an existing context. Does not lock database.
       
   440     */
       
   441     void DoUpdateContextClientDataL( CNssContext& aContext );
       
   442 
       
   443     /*
       
   444     * Updates RRD data. Does not lock database.
       
   445     */
       
   446 	void UpdateRRDL( CNssRRD& aRRD );
       
   447 
       
   448     /*
       
   449     * Updates speech item. Does not lock database.
       
   450     */
       
   451 	void UpdateSpeechItemL( CNssSpeechItem& aSpeechItem );
       
   452 
       
   453     /*
       
   454     * Gets a tag list from view. A mid-phase between GetTagListByQuery and
       
   455     * the lower functions (those which get RRD and speech item).
       
   456     */
       
   457     void GetTagListFromViewL( RDbView& aView, 
       
   458                               CArrayPtrFlat<CNssTag>* aTagList,
       
   459                               CNssContext* aContext );
       
   460 
       
   461     /*
       
   462     * Locks the database with Begin().
       
   463     */
       
   464     TInt StartTransaction();
       
   465 
       
   466     /*
       
   467     * Commits a transaction.
       
   468     * If the client has locked the database (Lock()), keeps the database locked
       
   469     * by calling Begin() immediately again.
       
   470     * @param "TBool aCompact" ETrue if compact should be called
       
   471     */
       
   472     TInt CommitTransaction( TBool aCompact );
       
   473 
       
   474     /*
       
   475     * Rolls back a transaction.
       
   476     * If the client has locked the database (Lock()), keeps the database locked
       
   477     * by calling Begin() immediately again.
       
   478     */
       
   479     TInt RollbackTransaction();
       
   480 		
       
   481     /*
       
   482     * Commits or rolls back according to success status, which is one of the
       
   483     * symbian-wide error codes.
       
   484     * @param aCompactIfSuccess If ETrue and commit is going to happen, compacts 
       
   485     *        the DB after the Commit
       
   486     */
       
   487     void CommitIfSuccess( RDbDatabase aDatabase, TInt aSuccess, TBool aCompactIfCommit = EFalse );
       
   488 
       
   489     /*
       
   490     * This method is used to create a TCleanupOperation, which rolls back
       
   491     * transaction. This way, rollback can be pushed to the cleanup stack.
       
   492     */
       
   493     static void RollbackCleanupFunction( TAny* aArg );
       
   494 
       
   495     /*
       
   496     * Reserves disk space. Leaves (KErrDiskFull), if not
       
   497     * enough space.
       
   498     */
       
   499     void ReserveDiskSpaceL( TInt aRequestSize );
       
   500 
       
   501     /*
       
   502     * Release the previously reserved disk space
       
   503     */
       
   504     void ReleaseDiskSpace();
       
   505     
       
   506     /**
       
   507     * Updates the information of an existing context inside already opened transaction.
       
   508     * @since 3.0
       
   509     * @param aContext The context
       
   510     * @return none
       
   511     */
       
   512 	void UpdateContextInsideTransactionL( CNssContext& aContext );
       
   513 	
       
   514 	/**
       
   515 	* Creates a cleanup item and pushes it to the cleanup stack. In case of a 
       
   516 	* leave, PopAndDestroying this item will call RollbackCleanupFunction
       
   517 	*/
       
   518 	void CreatePushRollbackItemLC();
       
   519 	
       
   520 private:
       
   521 	// Data
       
   522 
       
   523     // DB session
       
   524 	RDbs					iDbSession;
       
   525 
       
   526     // DB
       
   527 	RDbNamedDatabase		iDatabase;
       
   528 
       
   529     // Have we yet opened the DB file
       
   530 	TBool					iClientHasOpenedDatabase;
       
   531 
       
   532     // Used to check drive space
       
   533     TInt iDrive;
       
   534 
       
   535     // Used to store an SQL query.
       
   536 	TBuf<KNssMaxSQLLength>		iSQLStatement;
       
   537 
       
   538     // Used to create the database
       
   539 	TNssVasDbCreator           iDbcreator;
       
   540 
       
   541     // Is the databas locked to us
       
   542     TBool                   iLocked;
       
   543 
       
   544     // Should the database be relocked after commit
       
   545     // to ensure it stays locked for us
       
   546     TBool                   iShouldBeLocked;
       
   547 
       
   548     // Mutex which makes sure that multiple processes don't get the exclusive
       
   549     // write lock at the same time
       
   550     RMutex iMutex;
       
   551     
       
   552     // Critical section is used in locking transactions inside one object
       
   553     RCriticalSection iCriticalSection;
       
   554     
       
   555     // backup and restore observer
       
   556     CNssVasBackupObserver* iBackupObserver;
       
   557     
       
   558     // For creating empty CNssContext-objects
       
   559     CNssContextBuilder&          iContextBuilder;
       
   560     
       
   561     // For creating empty CNssSpeechItem-objects
       
   562     CNssSpeechItemBuilder&       iSpeechItemBuilder;
       
   563     };
       
   564 
       
   565 #endif // __CVASDB_H
       
   566