cbs/CbsServer/ServerInc/CCbsDbImpTopicMessages.h
changeset 46 2fa1fa551b0b
parent 42 35488577e233
child 48 78df25012fda
equal deleted inserted replaced
42:35488577e233 46:2fa1fa551b0b
     1 /*
       
     2 * Copyright (c) 2003 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:  This file contains the header file of the CCbsDbImpTopicMessages class.
       
    15 *    
       
    16 *                This class represents the topic messages stored in the database.
       
    17 *
       
    18 */
       
    19 
       
    20 
       
    21 
       
    22 #ifndef CCBSDBIMPTOPICMESSAGES_H
       
    23 #define CCBSDBIMPTOPICMESSAGES_H
       
    24 
       
    25 // INCLUDES
       
    26 #include <e32std.h>
       
    27 #include <s32file.h>
       
    28 #include "CbsCommon.h"
       
    29 
       
    30 // FORWARD DECLARATIONS
       
    31 class CCbsDbImp;
       
    32 class CCbsDbImpTopicList;
       
    33 class CCbsRecEtel;
       
    34 
       
    35 //  CLASS DECLARATION 
       
    36 
       
    37 /**
       
    38 *   CCbsDbImpTopicMessages represents CB messages stored in persistent memory.
       
    39 *   CBS server has two files to accommodate messages: one for unsaved 
       
    40 *   and one for saved messages. "Topic messages"-stream contains information
       
    41 *   on whether a message is saved or not, and what is message's stream id.
       
    42 *   CCbsDbImpTopicMessages determines the correct data file depending on
       
    43 *   message's save status.
       
    44 *
       
    45 *   Changes are written to stores immediately after operations. Message cache
       
    46 *   iMessageList contains header information of all messages in a single topic
       
    47 *   at once. A call to LoadCacheL() is thus made prior to any operation to
       
    48 *   make sure that the appropriate topic is being used.
       
    49 */
       
    50 class CCbsDbImpTopicMessages : public CBase
       
    51     {
       
    52     public:        // New functions
       
    53         /**
       
    54         *   Creates an instance of the class.
       
    55         *
       
    56         *   @param  aTopicList          Topic list database manager
       
    57         *   @param  aFs                 File server session
       
    58         *   @return                     Pointer to the created instance
       
    59         */
       
    60         static CCbsDbImpTopicMessages* NewL( CCbsDbImpTopicList& aTopicList,
       
    61             RFs& aFs );
       
    62 
       
    63         /**
       
    64         *   Destructor.
       
    65         */
       
    66         ~CCbsDbImpTopicMessages();
       
    67 
       
    68     public:
       
    69         /**
       
    70         *   Invalidates the cache.
       
    71         */
       
    72         void InvalidateCache();
       
    73 
       
    74         /**
       
    75         *   Invalidates the cache if the cache contains messages of the topic.
       
    76         *
       
    77         *   @param aNumber              Number of the topic that is 
       
    78         *                               invalidated.
       
    79         */
       
    80         void InvalidateCacheIfTopic( const TCbsDbTopicNumber& aNumber );
       
    81 
       
    82         /**
       
    83         *   Creates a default topic messages stream.
       
    84         *
       
    85         *   @param  aStore              Store to which the stream is created.
       
    86         *   @return                     Stream identifier to the created stream
       
    87         */
       
    88         static TStreamId CreateDefaultTopicMessagesStreamL( 
       
    89             CStreamStore& aStore );
       
    90 
       
    91         /**
       
    92         *   Delete all topics messages.
       
    93         *
       
    94         *   @param  aSavedStore         Store which contains saved messages.
       
    95         *   @param  aUnsavedStore       Store which contains unsaved messages.
       
    96         *   @param  aIn                 Stream for topic messages.
       
    97         */
       
    98         static void DeleteAllTopicMessagesL( CStreamStore& aSavedStore, 
       
    99             CStreamStore& aUnsavedStore, RReadStream& aIn );
       
   100 
       
   101         /**
       
   102         *   Determines whether or not there are any locked messages in 
       
   103         *   the topic.
       
   104         *
       
   105         *   @param  aNumber             Number of the topic.
       
   106         *   @return                     Boolean value indicating if there are 
       
   107         *                               locked messages in topic.
       
   108         */
       
   109         TBool IsLockedMessagesInTopic( const TCbsDbTopicNumber& aNumber ) 
       
   110             const;
       
   111 
       
   112         /**
       
   113         *   Determines whether there is at least one locked message.
       
   114         *
       
   115         *   @return                     ETrue, if there's at least one locked
       
   116         *                               message.
       
   117         */
       
   118         TBool IsLockedMessages() const;
       
   119 
       
   120         /**
       
   121         *   Returns the total amount of messages the topic contains.
       
   122         *
       
   123         *   @param aNumber              Number of the topic.
       
   124         *   @param aCount               Returns: number of messages in given
       
   125         *                               topic
       
   126         */
       
   127         void GetMessageCountL( const TCbsDbTopicNumber& aNumber, 
       
   128             TInt& aCount );
       
   129 
       
   130         /**
       
   131         *   Returns message information.
       
   132         *
       
   133         *   @param aNumber              Number of the topic.
       
   134         *   @param aIndex               Index to the message in topic.
       
   135         *   @param aMessage             Returns: message information.
       
   136         */
       
   137         void GetMessageL( const TCbsDbTopicNumber& aNumber, 
       
   138             TInt aIndex, TCbsDbMessage& aMessage );
       
   139 
       
   140         /**
       
   141         *   Finds the message for given handle and returns it.
       
   142         *
       
   143         *   @param aHandle              Handle to the message.
       
   144         *   @param aMessage             Returns: message information.
       
   145         */
       
   146         void FindMessageByHandleL( const TCbsDbMessageHandle& aHandle, 
       
   147             TCbsDbMessage& aMessage );
       
   148 
       
   149         /**
       
   150         *   Finds the message by message key.
       
   151         *
       
   152         *   @param  aNumber             Number of the topic.
       
   153         *   @param  aKey                Message key to be looked for.
       
   154         *   @return                     Handle to the message, or KErrNotFound if no 
       
   155         *                               such message was found.
       
   156         */
       
   157         TInt FindMessageByKeyL( TCbsDbTopicNumber aNumber, 
       
   158             TCbsDbMessageKey aKey,
       
   159             TCbsDbMessage& aMessage );
       
   160 
       
   161         /**
       
   162         *   Stores the contents of the message to the buffer aContents. 
       
   163         *   If aContents is too small to contain the whole message
       
   164         *   body, the message body is truncated.
       
   165         *
       
   166         *   @param aHandle              Handle to the message.
       
   167         *   @param aContents            Returns: message body
       
   168         *   @param aSize                Number of bytes to read
       
   169         */
       
   170         void GetMessageContentsL( const TCbsDbMessageHandle& aHandle, 
       
   171             TPtr& aContents, TUint aSize );
       
   172 
       
   173         /**
       
   174         *   Deletes a message from the topic.
       
   175         *
       
   176         *   @param aHandle      Handle to the message to be deleted.
       
   177         *   @param  aReceiver   Receiver.
       
   178         */
       
   179         void DeleteMessageL( const TCbsDbMessageHandle& aHandle,
       
   180             const CCbsRecEtel& aReceiver );
       
   181         
       
   182         /**
       
   183         *   Creates a new message to the topic.
       
   184         *
       
   185         *   FFS critical level check is made prior to operation.
       
   186         *
       
   187         *   @param aNumber              Number of the topic to which 
       
   188         *                               the message is to be added.
       
   189         *   @param aMessage             Contains the message information 
       
   190         *                               (i.e., header, not body).
       
   191         *   @param aContents            The contents of the message.
       
   192         */
       
   193         void AddMessageL( const TCbsDbTopicNumber& aNumber, 
       
   194             TCbsDbMessage& aMessage, const TPtrC& aContents );
       
   195 
       
   196         /**
       
   197         *   Saves the message.
       
   198         *
       
   199         *   FFS critical level check is made prior to the operation.
       
   200         *
       
   201         *   @param aHandle      Handle to the message to be saved.
       
   202         *   @param  aReceiver   Receiver.
       
   203         */
       
   204         void SaveMessageL( const TCbsDbMessageHandle& aHandle,
       
   205             const CCbsRecEtel& aReceiver );
       
   206 
       
   207         /**
       
   208         *   Sets the message as read.
       
   209         *
       
   210         *   @param aHandle      Handle to the message to be set read.
       
   211         *   @param  aReceiver   Receiver.
       
   212         */
       
   213         void ReadMessageL( const TCbsDbMessageHandle& aHandle,
       
   214             const CCbsRecEtel& aReceiver );
       
   215 
       
   216         /**
       
   217         *   Updates handles of messages in the topic with the number given in
       
   218         *   aOldTopicNumber to match new topic number.
       
   219         *
       
   220         *   @param  aOldTopicNumber     Number of topic to be changed
       
   221         *   @param  aNewTopicNumber     Change handles to match this number
       
   222         */
       
   223         void UpdateHandlesOfTopicMessagesL(
       
   224             const TCbsDbTopicNumber& aOldTopicNumber, 
       
   225             const TCbsDbTopicNumber& aNewTopicNumber );
       
   226 
       
   227         /**
       
   228         *   Unlocks one message and then locks another message.
       
   229         *
       
   230         *   Message that is locked can not be removed. Topic that 
       
   231         *   contains a locked message can not be removed.
       
   232         *
       
   233         *   Note that locking status is not persistent in the sense that 
       
   234         *   when the power is switched off and turned on, no messages 
       
   235         *   are automatically locked.
       
   236         *
       
   237         *   @param aUnlock       Handle to the message to be unlocked.
       
   238         *   @param aLock         Handle to the message to be locked.
       
   239         */
       
   240         void LockMessageL( const TCbsDbMessageHandle& aUnlock, 
       
   241             const TCbsDbMessageHandle& aLock );
       
   242 
       
   243         /**
       
   244         *   Retrieves handles of messages that precede and succeed the message
       
   245         *   with handle aCurrentMsg.
       
   246         */
       
   247         void GetNextAndPrevMsgHandleL(
       
   248             const TCbsDbMessageHandle& aCurrentMsg,
       
   249             TCbsDbMessageHandle& aNextMsg, 
       
   250             TCbsDbMessageHandle& aPrevMsg, 
       
   251             TInt& aPosition);
       
   252     
       
   253         /**
       
   254         *   Finds message position by message handle.
       
   255         *
       
   256         *   The method leaves in case the message is not found.
       
   257         *
       
   258         *   @param  aHandle             Handle to the message to be looked for.
       
   259         *   @return                     The position of the message.
       
   260         */
       
   261         TInt FindMessagePositionByHandleL( 
       
   262             const TCbsDbMessageHandle& aMessage );        
       
   263 
       
   264     private:
       
   265 
       
   266         // The structure that represents the topic messages cache item.
       
   267         struct TCbsDbImpTopicMessagesCacheItem
       
   268             {   
       
   269             // The stream identifier for the message information.
       
   270             TStreamId iId;
       
   271 
       
   272             // Whether this message is saved or not
       
   273             TBool iIsSaved;
       
   274 
       
   275             // Indicates whether iMessage and iContentsId are valid.
       
   276             TBool iIsMessage;
       
   277 
       
   278             // Message header - valid only if iIsMessage is true.
       
   279             TCbsDbMessage iMessage;
       
   280 
       
   281             // Stream identifier for message body-valid if iIsMessage is ETrue
       
   282             TStreamId iContentsId;
       
   283             };
       
   284 
       
   285         typedef CArrayFixFlat< TCbsDbImpTopicMessagesCacheItem > 
       
   286             TCbsDbImpTopicMessagesCache;
       
   287 
       
   288         /**
       
   289         *   Constructor.
       
   290         *
       
   291         *   @param aTopicList           Topic list database object
       
   292         *   @param aFs                  File server session
       
   293         */
       
   294         CCbsDbImpTopicMessages( CCbsDbImpTopicList& aTopicList, RFs& aFs );
       
   295 
       
   296         /**
       
   297         *   Finalizes the construction.
       
   298         */
       
   299         void ConstructL();
       
   300 
       
   301         /**
       
   302         *   Adds a message.
       
   303         *
       
   304         *   @param aMessage             Contains the message information.
       
   305         *   @param aContents            Contains the message contents.
       
   306         */
       
   307         void DoAddMessageL( TCbsDbMessage& aMessage, const TDesC& aContents );
       
   308 
       
   309         /**
       
   310         *   Deletes a message.
       
   311         *
       
   312         *   @param aPosition            Position in the internal array where 
       
   313         *                               the message is.
       
   314         */
       
   315         void DoDeleteMessageL( TInt aPosition );
       
   316 
       
   317         /**
       
   318         *   Updates message information.
       
   319         *
       
   320         *   @param aId                  Stream id to which the information is
       
   321         *                               written.
       
   322         *   @param aMessage             Contains the message information.
       
   323         *   @param aContentsId          Contains the contents id.
       
   324         */
       
   325         void DoUpdateMessageL( const TStreamId& aId, 
       
   326             const TCbsDbMessage& aMessage, const TStreamId& aContentsId ) const;
       
   327 
       
   328         /**
       
   329         *   Sets the message read.
       
   330         *
       
   331         *   @param  aId                 Stream id to which the information is 
       
   332         *                               written.
       
   333         *   @param  aMessage            Contains the message information.
       
   334         *   @param  aContentsId         Stream idenfitier to the contents.
       
   335         */
       
   336         void DoReadMessageL( const TStreamId& aId, const TCbsDbMessage& aMessage, 
       
   337             const TStreamId& aContentsId );
       
   338 
       
   339         /**
       
   340         *   Deletes messages that are not supposed to be in the topic.
       
   341         *
       
   342         *   Because of message locking it is possible that there are more 
       
   343         *   received messages than there should be. This method tries to 
       
   344         *   delete this kind of message.
       
   345         */
       
   346         void DoDeleteL();
       
   347 
       
   348         /**
       
   349         *   Loads the cache, if it is not already loaded with this topic 
       
   350         *   information.
       
   351         *
       
   352         *   The method does not load any message information to the cache 
       
   353         *   - only what is really necessary.
       
   354         *
       
   355         *   @param aNumber              Number of the topic to be loaded 
       
   356         *                               to the cache.
       
   357         */
       
   358         void LoadCacheL( const TCbsDbTopicNumber& aNumber );
       
   359 
       
   360         /**
       
   361         *   Loads the message header to the cache.
       
   362         *
       
   363         *   Note that it is assumed that the index is in proper range! 
       
   364         *   And the cache is already loaded!
       
   365         *
       
   366         *   @param aIndex               The index to the message in cache.
       
   367         *   @param aMessage             Returns: message information.
       
   368         */
       
   369         void LoadMessageL( TInt aIndex, TCbsDbMessage& aMessage );
       
   370 
       
   371         /**
       
   372         *   Loads message contents (as much as it fits).
       
   373         *
       
   374         *   Save status of message is required because saved and unsaved
       
   375         *   messages are stored on separate files.
       
   376         *
       
   377         *   @param aContentsId          Stream identifier to the content.
       
   378         *   @param aContents            Returns: message body.
       
   379         *   @param aIsSaved             ETrue, if the message is saved.
       
   380 		*	@param aSize				Size of the message.
       
   381         */
       
   382         void LoadMessageContentsL( const TStreamId& aContentsId, TPtr& aContents, 
       
   383             TBool aIsSaved, TUint aSize ) const;
       
   384 
       
   385         /**
       
   386         *   Generates a new message handle.
       
   387         *
       
   388         *   @param  aNumber             Number of the topic which will contain 
       
   389         *                               the message.
       
   390         *   @return                     Returns the new message handle.
       
   391         */
       
   392         TCbsDbMessageHandle GenerateNewMessageHandle( 
       
   393             const TCbsDbTopicNumber& aNumber );
       
   394 
       
   395         /**
       
   396         *   Checks that if there are too many received messages in 
       
   397         *   the internal cache, then they will be deleted if they
       
   398         *   are not locked.
       
   399         *
       
   400         *   @return                     Boolean value indicating if some 
       
   401         *                               changes was made.
       
   402         */
       
   403         TBool DeleteReceivedIfNecessaryL();
       
   404 
       
   405         /**
       
   406         *   Deletes message by its position in the internal cache.
       
   407         *
       
   408         *   Note that after the call the internal cache won't contain 
       
   409         *   the item.
       
   410         *
       
   411         *   @param aPosition            The position to the message.
       
   412         */
       
   413         void DeleteMessageByPositionL( TInt index );
       
   414 
       
   415         /**
       
   416         *   Determines whether or not the message is locked.
       
   417         *
       
   418         *   @param  aHandle             The handle to the message.
       
   419         *   @return                     Boolean value indicating if 
       
   420         *                               the message is locked.
       
   421         */
       
   422         TBool IsMessageLocked( const TCbsDbMessageHandle& aHandle ) const;
       
   423 
       
   424         /**
       
   425         *   Deletes from the locked messages.
       
   426         *
       
   427         *   @param aHandle              Message to be unlocked.
       
   428         */
       
   429         void DeleteFromLocked( const TCbsDbMessageHandle& aHandle );
       
   430 
       
   431         /**
       
   432         *   Adds a handle to the locked messages.
       
   433         *
       
   434         *   @param aHandle              Message to be locked.
       
   435         */
       
   436         void InsertToLockedL( const TCbsDbMessageHandle& aHandle );
       
   437 
       
   438         /**
       
   439         *   Updates topic messages stream from the internal cache.
       
   440         *
       
   441         *   @param aDeleting            Whether the call is due to a deletion.
       
   442         */
       
   443         void UpdateTopicMessagesStreamL( TBool aDeleting );
       
   444 
       
   445         /**
       
   446         *   Writes topic messages stream from the internal cache.
       
   447         *
       
   448         *   @param aOut                 Stream to which data is written.
       
   449         */
       
   450         void WriteTopicMessagesStreamL( RWriteStream& aOut );
       
   451 
       
   452         /**
       
   453         *   Saves the message.
       
   454         *
       
   455         *   @param aMessage             The message to be saved.
       
   456         *   @param aItem                The cache item to the previous instance of the
       
   457         *                               message.
       
   458         *   @param aContentPtr          The contents of the message.
       
   459         *   @param aPosition            The position of the message in the cache.
       
   460         */
       
   461         void DoSaveMessageL( 
       
   462             TCbsDbMessage& aMessage,
       
   463             const TCbsDbImpTopicMessagesCacheItem& aItem,
       
   464             const TDesC& aContentPtr,
       
   465             TInt aPosition );
       
   466 
       
   467         /**
       
   468         *   Reverts any not commited file operation in data files,
       
   469         *   reloads cache and then leaves with aReason.
       
   470         *
       
   471         *   @param  aReason             Leave code
       
   472         */
       
   473         void RevertFileOperationL( TInt aReason );
       
   474 
       
   475         /**
       
   476         *   Creates a new message content stream.
       
   477         *
       
   478         *   @param aStore               Store to which the stream is created.
       
   479         *   @param aContents            Message content.
       
   480         */
       
   481         static TStreamId CreateMessageContentsStreamL( CStreamStore& aStore,
       
   482             const TDesC& aContents );
       
   483         
       
   484         /**
       
   485         *   Creates a new message stream.
       
   486         *
       
   487         *   @param aStore               Store to which the stream is created.
       
   488         *   @param aMessage             Message information.
       
   489         *   @param aContents            Stream identifier to the contents.
       
   490         */
       
   491         static TStreamId CreateMessageStreamL( CStreamStore& aStore, 
       
   492             TCbsDbMessage& aMessage, const TStreamId& aContents );
       
   493 
       
   494         /**
       
   495         *   Reads all cache items for a topic.
       
   496         *
       
   497         *   @param aStore               Reference to the store.
       
   498         *   @param aId                  Stream id to the topic messages stream.
       
   499         *   @param aCache               Empty cache where it is supposed that 
       
   500         *                               these messages are put.
       
   501         *   @param aSpace               Contain the amount of space for 
       
   502         *                               messages.
       
   503         */
       
   504         static void ReadTopicMessagesCacheItemsL( const CStreamStore& aStore, 
       
   505             const TStreamId& aId, 
       
   506             TCbsDbImpTopicMessagesCache& aCache, 
       
   507             TInt& aSpace );
       
   508 
       
   509         /**
       
   510         *   Reads necessary message information from the store.
       
   511         *
       
   512         *   @param aStore               Store that contains the information.
       
   513         *   @param aId                  Stream identifier to the message 
       
   514         *                               stream.
       
   515         *   @param aMessage             Contain message information.
       
   516         *   @param aContentsId          Contain the stream identifier to 
       
   517         *                               the contents.
       
   518         */
       
   519         static void ReadMessageInformationL( 
       
   520             const CStreamStore& aStore, 
       
   521             const TStreamId& aId, 
       
   522             TCbsDbMessage& aMessage, 
       
   523             TStreamId& aContentsId );
       
   524 
       
   525         /**
       
   526         *   Writes message information to the store.
       
   527         *
       
   528         *   @param aOut                 Stream to which data is written.
       
   529         *   @param aMessage             Contains the message information to be
       
   530         *                               written.
       
   531         *   @param aContentsId          Stream identifier to the contents.
       
   532         */
       
   533         static void WriteMessageInformationL( RWriteStream& aOut, 
       
   534             const TCbsDbMessage& aMessage, const TStreamId& aContentsId );
       
   535 
       
   536         /**
       
   537         *   Updates the soft notification (dialog) when unread message
       
   538         *   count changes.
       
   539         *
       
   540         *   @param aHandle      Handle to the message to be set as read
       
   541         *   @param  aReceiver   Receiver.
       
   542         */            
       
   543         void UpdateSoftNotificationL( const TCbsDbMessageHandle& aHandle,
       
   544            const CCbsRecEtel& aReceiver );
       
   545 
       
   546     private:
       
   547 
       
   548         // Topic list manager.
       
   549         CCbsDbImpTopicList& iTopicList;
       
   550 
       
   551         // Indicates whether the cache is valid or not.
       
   552         TBool iCacheValid;
       
   553 
       
   554         // If cache is valid, then it contains the topic handle of the 
       
   555         // cached topic.        
       
   556         TCbsDbTopicNumber iCachedTopicNumber;
       
   557 
       
   558         // If cache is valid, then it tells how many messages the stream 
       
   559         // can hold.        
       
   560         TInt iSpaceForMessages;
       
   561        
       
   562         // Own: If cache is valid, it contains some information of the 
       
   563         // topic messages for fast access.        
       
   564         TCbsDbImpTopicMessagesCache* iMessageList;
       
   565 
       
   566         // Own: handles of the locked messages.
       
   567         CArrayFixFlat< TCbsDbMessageHandle >* iLockedMessages;
       
   568 
       
   569         // Reference to a file server session of CCbsDbImp
       
   570         RFs& iFs;
       
   571 
       
   572     };
       
   573 
       
   574 #endif      //  CCBSDBIMPTOPICMESSAGES_H   
       
   575             
       
   576 // End of File