cbs/CbsServer/ServerInc/CCbsDbImpTopicList.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 CCbsDmImpTopicList class
       
    15 *    
       
    16 *                This class represents the topic list stored in the database.
       
    17 *
       
    18 */
       
    19 
       
    20 
       
    21 #ifndef     CCBSDBIMPTOPICLIST_H
       
    22 #define     CCBSDBIMPTOPICLIST_H
       
    23 
       
    24 // INCLUDES
       
    25 #include <f32file.h>
       
    26 
       
    27 #ifndef __SECURE_BACKUP__
       
    28 #include <babackup.h>
       
    29 #endif
       
    30 
       
    31 #include "CbsCommon.h"
       
    32 #include "CCbsDbImp.H"
       
    33 
       
    34 // FORWARD DECLARATIONS
       
    35 //class CCbsDbImp;
       
    36 class CCbsDbImpTopicMessages;
       
    37 class MCbsDbTopicListObserver;
       
    38 class CFileStore;
       
    39 //  CLASS DECLARATION 
       
    40 
       
    41 /**
       
    42 *   CCbsDbImpTopicList is the database topic list manager.
       
    43 *   
       
    44 */
       
    45 class CCbsDbImpTopicList : public CBase
       
    46 #ifndef __SECURE_BACKUP__
       
    47 , public MBackupObserver
       
    48 #endif
       
    49     {
       
    50     #ifndef __SECURE_BACKUP__
       
    51     public: // MBackupObserver
       
    52         /**
       
    53         *   Closes or reopens the settings file if requested by a backup.
       
    54         *
       
    55         *   @param aFileName                The name of the file to close.
       
    56         *   @param aFlags                   The action to be performed.
       
    57         *
       
    58         */
       
    59         void ChangeFileLockL( const TDesC& aFileName, TFileLockFlags aFlags );
       
    60     #else
       
    61     
       
    62     public:
       
    63         /**
       
    64         *   Closes or reopens the settings file if requested by a backup.
       
    65         *
       
    66         *   @param aRequest                 Current request        
       
    67         *
       
    68         */
       
    69         void ChangeFileLockL( const TCbsBackupRequest& aRequest );
       
    70         
       
    71     #endif
       
    72 
       
    73     private: 
       
    74         /**
       
    75         *   Check if the server has a file lock. Leaves with
       
    76         *   KErrLocked if not.
       
    77         */
       
    78         void CheckFileLockL() const;
       
    79 
       
    80     public:     // New functions
       
    81         
       
    82         /**
       
    83         *   Creates an instance of the class.
       
    84         *
       
    85         *   @param aFs                      Open file server session.
       
    86         *   @param aTopicsFile              Name of the file containing topics.
       
    87         *   @param aUnsavedMessagesFile     Name of the file containing unsaved
       
    88         *                                   messages.
       
    89         *   @param aDatabase                Reference to database object
       
    90         *   @return                         Pointer to the created instance.
       
    91         */
       
    92         static CCbsDbImpTopicList* NewL( RFs& aFs, 
       
    93             const TDesC& aTopicsFile, 
       
    94             const TDesC& aUnsavedMessagesFile,
       
    95             CCbsDbImp& aDatabase );
       
    96 
       
    97         /**
       
    98         *   Destructor.
       
    99         */
       
   100         ~CCbsDbImpTopicList();
       
   101 
       
   102     public:
       
   103 
       
   104         /**
       
   105         *   Creates a new topic list
       
   106 		*
       
   107         *	@param	aTopicListName		Name of the new topic list
       
   108 		*	@param	aTopicListIndex		Returns: Index number of the new list
       
   109         */
       
   110         void CreateNewTopicListL( const TDesC& aTopicListName );
       
   111 
       
   112 
       
   113 		/**
       
   114         *	Deletes old read messages. The clean up interval is set
       
   115 		*	by the user.
       
   116 		*
       
   117         */
       
   118         void CleanupReadMessagesL();        
       
   119 
       
   120         /**
       
   121         *   Sets the topic messages manager.
       
   122         *
       
   123         *   Note that this function does not transfer ownership
       
   124         *   of aMessages.
       
   125         *
       
   126         *   @param aMessages            Pointer to the topic messages 
       
   127         *                               manager.
       
   128         */
       
   129         void SetTopicMessages( CCbsDbImpTopicMessages* aMessages );
       
   130 
       
   131         /**
       
   132         *   Returns pointer to the current store which contains topics
       
   133         *   of the server (i.e., Topics-file) and saved messages. 
       
   134         *   Leaves with KErrLocked if the store is locked by backup.
       
   135         *
       
   136         *   @return                     Pointer to topics store.
       
   137         */
       
   138         CFileStore* TopicStoreL() const;
       
   139 
       
   140         /**
       
   141         *   Returns a pointer to the name of the file, which contains unsaved
       
   142         *   messages of the server.
       
   143         *
       
   144         *   @return                     Reference to the file name.
       
   145         */
       
   146         const TDesC& TopicFilename() const;
       
   147 
       
   148         /**
       
   149         *   Returns a pointer to the store, which contains unsaved
       
   150         *   messages of the server.
       
   151         *
       
   152         *   @return                     Pointer to unsaved messages store.
       
   153         */
       
   154         CFileStore* UnsavedMessagesStore() const;
       
   155 
       
   156         /**
       
   157         *   Returns a pointer to the name of the file, which contains unsaved
       
   158         *   messages of the server (Unsaved Messages-file, "cbs4.dat").
       
   159         *
       
   160         *   @return                     Reference to the file name.
       
   161         */
       
   162         const TDesC& UnsavedMessagesFilename() const;
       
   163 
       
   164         /**
       
   165         *   Returns the topic messages stream id by topic handle.
       
   166         *
       
   167         *   @param  aNumber             Topic number
       
   168         *   @param  aId                 Returns: the stream id to the topic
       
   169         *                               messages stream.
       
   170         */
       
   171         void GetTopicMessagesIdL( TCbsDbTopicNumber aNumber,
       
   172             TStreamId& aId ) const;
       
   173 
       
   174         /**
       
   175         *   Updates the topic messages stream id by topic handle. The new 
       
   176         *   identifier is expected not to be a null id.
       
   177         *
       
   178         *   Note that the method will not commit changes to the store. 
       
   179         *   It also changes the internal state and thus if the method leaves, 
       
   180         *   it is good to reload the whole root stream.
       
   181         *
       
   182         *   The method will delete the old topic messages stream.
       
   183         *
       
   184         *   @param aNumber              Topic number
       
   185         *   @param aNewId               New stream id.
       
   186         */
       
   187         void UpdateTopicMessagesIdL( TCbsDbTopicNumber aNumber, 
       
   188             const TStreamId& aNewId );
       
   189 
       
   190         /**
       
   191         *   Generates a message handle.
       
   192         *
       
   193         *   Note that it must be checked that the message handle is unique 
       
   194         *   in that topic.
       
   195         *
       
   196         *   @param  aNumber             Topic number
       
   197         *   @param  aRandom             Number generated in some way.
       
   198         *   @return                     New message handle.
       
   199         */
       
   200         TCbsDbMessageHandle GenerateMessageHandle( 
       
   201             const TCbsDbTopicNumber aNumber, TUint16 aRandom ) const;
       
   202 
       
   203         /**
       
   204         *   Informs that a new message has been received in a topic.
       
   205         *
       
   206         *   @param  aMessageHandle      Handle of the received message.
       
   207         */
       
   208         void InformNewMessageReceivedL(
       
   209             const TCbsDbMessageHandle& aMessageHandle  );
       
   210 
       
   211         /**
       
   212         *   Informs that a message has been set as saved.
       
   213         *
       
   214         *   @param  aMessageHandle      Handle of the saved message.
       
   215         */
       
   216         void InformMessageSavedL( const TCbsDbMessageHandle& aMessageHandle );
       
   217 
       
   218         /**
       
   219         *   Informs that an unread message has been read.
       
   220         *
       
   221         *   @param  aMessageHandle      Handle of the read message.
       
   222         */
       
   223         void InformUnreadMessageReadL( 
       
   224             const TCbsDbMessageHandle& aMessageHandle );
       
   225 
       
   226         /**
       
   227         *   Informs that a message has been deleted.
       
   228         *
       
   229         *   @param  aMessageHandle      Handle of the deleted message.
       
   230         *   @param  aPermanent          ETrue, if the message was permanent.
       
   231         *   @param  aRead               ETrue, if the message was read.
       
   232         */
       
   233         void InformMessageDeletedL( 
       
   234             const TCbsDbMessageHandle& aMessageHandle, 
       
   235             TBool aPermanent, TBool aRead );
       
   236 
       
   237         /**
       
   238         *   Extracts topic handle from message handle.
       
   239         *
       
   240         *   Note that the method does not check that the message exists.
       
   241         *
       
   242         *   @param  aHandle             Handle to a message.
       
   243         *   @return                     Returns the number of the topic to 
       
   244         *                               which the message belongs.
       
   245         */
       
   246         TCbsDbTopicNumber ExtractTopicNumber( 
       
   247             const TCbsDbMessageHandle& aHandle ) const;
       
   248         
       
   249         /**
       
   250         *   Initialises the whole topic list. 
       
   251         */
       
   252         void InitializeListL( const TBool aFileOpenFailed );
       
   253 
       
   254         /**
       
   255         *   Returns the total amount of saved messages.
       
   256         *
       
   257         *   @return                     Number of saved messages.
       
   258         */
       
   259         TInt TotalSavedMessages() const;
       
   260 
       
   261         /**
       
   262         *   Returns the amount of topics the list contains.
       
   263         *
       
   264         *   @param  aCount              Returns: number of topics in topic list
       
   265         */
       
   266         void GetTopicCount( TInt& aCount ) const;
       
   267     
       
   268         /**
       
   269         *   Returns a topic by index.
       
   270         *
       
   271         *   Return values:
       
   272         *   KErrNone                    Parameter aTopic contains the requested
       
   273         *                               topic.
       
   274         *   KErrArgument                Index out of bounds.
       
   275         *
       
   276         *   Leaves if file read fails.
       
   277         *
       
   278         *   @param  aIndex              Index to the topic.
       
   279         *   @param  aTopic              Returns: topic information.
       
   280         */
       
   281         void GetTopicL( TInt aIndex, TCbsDbTopic& aTopic );
       
   282 
       
   283         /**
       
   284         *   Returns a topic by topic number.
       
   285         *
       
   286         *   Leaves with KErrNotFound, if a topic with the given number
       
   287         *   is not found.
       
   288         *
       
   289         *   @param    aNumber           Number of the topic.
       
   290         *   @param    aTopic            Returns: topic information.
       
   291         */
       
   292         void FindTopicByNumberL( TCbsDbTopicNumber aNumber, 
       
   293             TCbsDbTopic& aTopic );
       
   294 
       
   295         /**
       
   296         *   Reads the name of the index topic from resource.
       
   297         *
       
   298         *   @return     Name of the index topic
       
   299         */
       
   300         HBufC* ReadIndexTopicNameLC();
       
   301 
       
   302         /**
       
   303         *   Adds a new topic to the list.
       
   304         *
       
   305         *   @param    aTopic            Topic information.
       
   306         *   @param    aDetected         Was the topic detected automatically
       
   307         */
       
   308         void AddTopicL( TCbsDbTopic& aTopic, const TBool aDetected );
       
   309 
       
   310         /**
       
   311         *   Updates the topic name and number.
       
   312         *
       
   313         *   @param    aOldNumber        Old number of the topic.
       
   314         *   @param    aNewNumber        New number of the topic.
       
   315         *   @param    aName             New name of the topic.
       
   316         */
       
   317         void UpdateTopicNameAndNumberL( 
       
   318             TCbsDbTopicNumber aOldNumber, 
       
   319             TCbsDbTopicNumber aNewNumber, 
       
   320             const TCbsDbTopicName& aName );
       
   321 
       
   322         /**
       
   323         *   Updates the topic subscription status.
       
   324         *
       
   325         *   @param    aNumber           Topic number
       
   326         *   @param    aStatus           New subscription status.
       
   327         */
       
   328         void UpdateTopicSubscriptionStatusL( 
       
   329             TCbsDbTopicNumber aNumber, 
       
   330             TBool aStatus );
       
   331 
       
   332         /**
       
   333         *   Updates the topic hotmark status.
       
   334         *
       
   335         *   @param    aNumber           Topic number
       
   336         *   @param    aStatus           New hotmark status.
       
   337         */
       
   338         void UpdateTopicHotmarkStatusL( 
       
   339             TCbsDbTopicNumber aNumber, 
       
   340             TBool aStatus );
       
   341 
       
   342         /**
       
   343         *   Deletes an existing topic and all its messages.
       
   344         *
       
   345         *   @param    aNumber           Number of the topic to be deleted.
       
   346         */
       
   347         void DeleteTopicL( TCbsDbTopicNumber aNumber );
       
   348 
       
   349         /**
       
   350         *   Gets the total amount of unread messages.
       
   351         *
       
   352         *   @param  aCount              Returns: number of unread topics. 
       
   353         */
       
   354         void GetUnreadMessageCount( TInt& aCount ) const;
       
   355 
       
   356         /**
       
   357         *   Returns the handle to the latest hotmarked message. 
       
   358         *   
       
   359         *   The method leaves if there is no such message.
       
   360         *
       
   361         *   @param  aMessage            Returns: Handle to a message that has         
       
   362         *                               been  received under a hotmarked topic
       
   363         */
       
   364         void GetHotmarkedMessageHandleL( TCbsDbMessageHandle& aMessage );
       
   365 
       
   366         /**
       
   367         *   Adds a topic list observer. 
       
   368         *   
       
   369         *   After an observer is added to the topic list, it will be notified 
       
   370         *   whenever an event occurs.
       
   371         *
       
   372         *   @param  aObserver           New observer for the list.
       
   373         */
       
   374         void AddObserverL( MCbsDbTopicListObserver* aObserver );
       
   375 
       
   376         /**
       
   377         *   Removes a topic list observer. 
       
   378         *   
       
   379         *   If aObserver is not in the list, the method will panic.
       
   380         *
       
   381         *   @param  aObserver           Pointer to the observer to be removed.
       
   382         */
       
   383         void RemoveObserver( const MCbsDbTopicListObserver* aObserver );
       
   384 
       
   385         /**
       
   386         *   Adds numbers of subscribed topics to the given array.
       
   387         *
       
   388         *   @param aSubscriptions       Dynamic array into which the topic
       
   389         *                               numbers are inserted.
       
   390         */
       
   391         void AppendSubscribedTopicsL( CArrayFixFlat< TUint16 >& aSubscriptions )
       
   392             const;
       
   393 
       
   394         /**
       
   395         *   Reverts all changes made to three datafiles handled by this
       
   396         *   class.
       
   397         */
       
   398         void RevertFilesL();
       
   399 
       
   400         /**
       
   401         *   Commits all changes made to three datafiles handled by this
       
   402         *   class.
       
   403         */
       
   404         void CommitFilesL();
       
   405 
       
   406         /**
       
   407         *   Returns the number of the topic that was added last
       
   408         *   to the database by topic detection feature.
       
   409         *
       
   410         *   Result code KErrNotFound indicates that no topic has ever
       
   411         *   been added to the database.
       
   412         *
       
   413         *   @param aNumber              Returns: topic number
       
   414         *   @return                     Result code
       
   415         */
       
   416         TInt GetLatestTopicNumber( TCbsTopicNumber& aNumber ) const;
       
   417 
       
   418         /**
       
   419         *   Finds the index of the topic matching the given topic number
       
   420         *   in the topic list.
       
   421         *
       
   422         *   @param aNumber              Topic number
       
   423         *   @return                     Index to the position of topic in 
       
   424         *                               the topic list.
       
   425         */
       
   426         TInt TopicIndexInList( TCbsDbTopicNumber aNumber ) const;
       
   427 
       
   428         /**
       
   429         *   Returns the number of unread messages in hotmarked topics.
       
   430         */
       
   431         TInt UnreadHotmarkedMessageCount() const;
       
   432 
       
   433         /**
       
   434         *   Retrieves numbers of topics that precede and succeed the topic
       
   435         *   with number aCurrentTopic.
       
   436         *
       
   437         *   @param  aCurrentTopic   Topic number of current topic
       
   438         *   @param  aNextTopic      Topic number of next topic
       
   439         *   @param  aPrevTopic      Topic number of previous topic
       
   440         *   @param  aPosition       Position of topic in the topic list
       
   441         */
       
   442         void GetNextAndPrevTopicNumberL( const TCbsTopicNumber& aCurrentTopic,
       
   443             TCbsTopicNumber& aNextTopic, TCbsTopicNumber& aPrevTopic,
       
   444             TInt& aPosition );
       
   445 
       
   446         /**
       
   447         *   Notifies each observer that a new message has arrived to a topic.
       
   448         *
       
   449         *   @param    aHandle                    Handle to the topic.
       
   450         */
       
   451         void NotifyNewMessageArrivedL( const TCbsDbMessageHandle& aHandle );
       
   452 
       
   453         /**
       
   454         *   Notifies each observer that a topic has been modified.
       
   455         *
       
   456         *   @param    aNumber                    Topic number
       
   457         */
       
   458         void NotifyTopicModifiedL( TCbsDbTopicNumber aNumber );
       
   459 
       
   460         /**
       
   461         *   Loads the topic stream to the memory.
       
   462         *
       
   463         */
       
   464         void LoadDefaultTopicStreamL();
       
   465 
       
   466     private:                
       
   467 
       
   468         /**
       
   469         *   Constructor.
       
   470         *
       
   471         *   @param aFs                  Open file server session.
       
   472         *   @param aDatabase            Reference to database object
       
   473         */
       
   474         CCbsDbImpTopicList( RFs& aFs, CCbsDbImp& aDatabase );
       
   475 
       
   476         /**
       
   477         *   Returns the total amount of topics the list contains.
       
   478         *
       
   479         *   @return                     The total amount of topics in the list.
       
   480         */
       
   481         TInt TopicCount() const;
       
   482 
       
   483 		/**
       
   484         *   Returns the total amount of topic lists.
       
   485         *
       
   486         *   @return                     The total amount of topic lists.
       
   487         */
       
   488         TInt TopicListCount() const;
       
   489 
       
   490         /**    
       
   491         *   Finalizes the construction.
       
   492         *
       
   493         *   @param aTopicFile           File name of the topic file
       
   494         *   @param aUnsavedMessagesFile File name of the unsaved messages file.
       
   495         */
       
   496         void ConstructL( const TDesC& aTopicsFile, 
       
   497             const TDesC& aUnsavedMessagesFile );
       
   498 
       
   499         /**
       
   500         *   Loads the root stream to the memory.
       
   501         *
       
   502         *   Root stream contains stream ids for each topic list and the total
       
   503         *   count of topic lists.        
       
   504         */
       
   505         void LoadRootStreamL();
       
   506 
       
   507         /**
       
   508         *   Reloads the root stream to the memory.
       
   509         */
       
   510         void ReloadRootStreamL();
       
   511        
       
   512 		/**
       
   513         *   Reads all information on topic list found with stream ID.        
       
   514         *
       
   515         *   @param aId                  Stream id to the topic information
       
   516         *   @param aTopicList           Returns: topic list information
       
   517         */
       
   518 		void ReadTopicListInformationL(const TStreamId& aId, 
       
   519 			TCbsDbImpTopicList& aTopicList ) const;
       
   520 
       
   521         /**
       
   522         *   Reads all information on topic found in stream aId
       
   523         *   into aTopic.
       
   524         *
       
   525         *   This function is called when details of a topic are needed.
       
   526         *
       
   527         *   @param aId                  Stream id to the topic information
       
   528         *   @param aTopic               Returns: topic information.
       
   529         */
       
   530         void ReadTopicInformationL( const TStreamId& aId, TCbsDbImpTopic& aTopic ) const;
       
   531 
       
   532         /**
       
   533         *   Adds a topic to the database.
       
   534         *
       
   535         *   @param aTopic               Information on added topic.
       
   536         */
       
   537         void DoAddTopicL( const TCbsDbTopic& aTopic );
       
   538 
       
   539         /**
       
   540         *   Updates a topic information.
       
   541         *
       
   542         *   @param aTopic               Contains topic information.
       
   543         *   @param aNeedToChange        Indicates whether or not it is required
       
   544         *                               to change position in the array
       
   545         *   @param aOldPosition         Old position.
       
   546         *   @param aDeleting            Whether the call is due to a deletion.
       
   547         */
       
   548         void DoUpdateTopicL( const TCbsDbTopic& aTopic, 
       
   549             TBool aNeedToChange, TInt aOldPosition, TBool aDeleting );
       
   550 
       
   551         /**
       
   552         *   Deletes an existing topic.
       
   553         *
       
   554         *   @param aPosition            Position of the topic to be deleted.
       
   555         */
       
   556         void DoDeleteTopicL( TInt aPosition );
       
   557 
       
   558         /**
       
   559         *   Updates a topic list information.
       
   560         *
       
   561         *   @param aTopicList       Contains topic list information.
       
   562         *   @param aDeleting        Info if we are deleting an entry.
       
   563         */
       
   564         void UpdateTopicListStreamL( TCbsDbImpTopicList& aTopicList, 
       
   565             TBool aDeleting );
       
   566 
       
   567         /**
       
   568         *   Updates the root stream.
       
   569         *   If required, it will create a new stream.
       
   570         *
       
   571         *   @param aDeleting        Info if we are deleting an entry.
       
   572         */
       
   573         void UpdateRootStreamL( TBool aDeleting );
       
   574 
       
   575         /**
       
   576         *   Updates the topic information.
       
   577         *
       
   578         *   @param aTopic               Updated topic information.
       
   579         */
       
   580         void UpdateTopicL( TCbsDbTopicNumber aTopicNumber, 
       
   581             TCbsDbTopic& aTopic );
       
   582 
       
   583         /**
       
   584         *   Checks that the topic number is valid. 
       
   585         *
       
   586         *   @param  aNumber             The topic number.
       
   587         *   @return                     Boolean value indicating if number 
       
   588         *                               is ok.
       
   589         */
       
   590         TBool CheckTopicNumber( TCbsDbTopicNumber aNumber ) const;
       
   591 
       
   592         /**
       
   593         *   Write the root stream.
       
   594         *
       
   595         *   @param    aOut              The stream to which the data is 
       
   596         *                               written.
       
   597         */
       
   598         void WriteRootStreamL( RWriteStream& aOut ) const;
       
   599 
       
   600         /**
       
   601         *   Write the Topic list stream
       
   602         *
       
   603         *   @param    aOut              The stream to which the data is 
       
   604         *                               written.
       
   605         */
       
   606         void WriteTopicListStreamL( RWriteStream& aOut, TCbsDbImpTopicList& aTopicList ) const;
       
   607 
       
   608         /**
       
   609         *   Notifies each observer that the topic list has been initialized.
       
   610         */
       
   611         void NotifyTopicListInitializedL();
       
   612 
       
   613         /**
       
   614         *   Notifies each observer that a topic has been added.
       
   615         *
       
   616         *   @param    aNumber                    Topic number
       
   617         */
       
   618         void NotifyTopicAddedL( TCbsDbTopicNumber aNumber );
       
   619 
       
   620         /**
       
   621         *   Notifies each observer that a topic has been deleted.
       
   622         *
       
   623         *   @param    aNumber                    Topic number
       
   624         */
       
   625         void NotifyTopicDeletedL( TCbsDbTopicNumber aNumber );
       
   626 
       
   627         /**
       
   628         *   Sets the hotmarked message handle.
       
   629         *
       
   630         *   @param    aMessageHandle             Handle to the message.
       
   631         */
       
   632         void SetHotmarkedMessage( const TCbsDbMessageHandle& aMessageHandle );
       
   633 
       
   634         /**
       
   635         *   Creates a default root stream.
       
   636         *
       
   637         *   The method may leave, if the creation or writing fails.
       
   638         *
       
   639         *   @param    aStore            The stream to to which the root
       
   640         *                               stream is written.
       
   641         *   @return                     Returns the stream identifier to 
       
   642         *                               that created stream.
       
   643         */
       
   644         TStreamId CreateDefaultRootStreamL( CStreamStore& aStore ) const;
       
   645 
       
   646         /**
       
   647         *   Writes topic information to the stream.
       
   648         *
       
   649         *   @param    aOutstream        The stream to which the data is written.
       
   650         *   @param    aTopic            Topic information.
       
   651         *   @param    aTopicMessagesId  The stream identifier to the 
       
   652         *                               topic messages.
       
   653         */
       
   654         void WriteTopicInformationL( RWriteStream& aOutstream, 
       
   655             const TCbsDbTopic& aTopic, const TStreamId& aTopicMessagesId ) const;
       
   656 
       
   657         /**
       
   658         *   Extracts topic information (name, number, subscription and
       
   659         *   hotmark status) from a factory defaults topics line
       
   660         *   and adds that topic to the topic list.
       
   661         *
       
   662         *   @param aLine                Factory defaults line.
       
   663         */
       
   664         void AddDefaultTopicL( const TDesC& aLine );
       
   665 
       
   666         /**
       
   667         *   After a call to this function, the file stores can be assumed
       
   668         *   to be open and initialized.
       
   669         *
       
   670         *   @param aDeleteExistingFiles     A flag whether to recreate and 
       
   671         *                                   initialize the files or not
       
   672         *   @param  aCreateNewTopicList     ETrue, if the first topic list
       
   673         *                                   needs to be created
       
   674         */
       
   675         void OpenFilesL( TBool aDeleteExistingFiles, TBool aCreateNewTopicList );
       
   676 
       
   677         /**
       
   678         *   Creates CBS files, if appropriate.
       
   679         *
       
   680         *   @param  aUnsavedMsgFileExisted  Returns ETrue if unsaved messages
       
   681         *                                   file already existed.
       
   682         */
       
   683         void CreateFilesIfNecessaryL( TBool& aUnsavedMsgFileExisted );
       
   684 
       
   685         /**
       
   686         *   Tries to open topic and unsaved messages files.
       
   687         *
       
   688         *   @param  aDeleteUnsavedMsgStreamIds  ETrue, if unsaved msg file 
       
   689         *                                       has been deleted
       
   690         *   @param  aCreateNewTopicList         ETrue, if the first topic list
       
   691         *                                       needs to be created
       
   692         */
       
   693         void TryToOpenFilesL( TBool aDeleteUnsavedMsgStreamIds, TBool aCreateNewTopicList );
       
   694 
       
   695         /**
       
   696         *   Adds index topic to topic list.
       
   697         */
       
   698         void AddIndexTopicL();
       
   699 
       
   700         /**
       
   701         *   Creates an empty file store with the given filename.
       
   702         *
       
   703         *   @param aFilename            Filename to be created.
       
   704         */
       
   705         void DoCreateStoreL( const TDesC& aFilename );
       
   706 
       
   707         /**
       
   708         *   Deletes the unsaved messages' store and rebuilds it.
       
   709         */
       
   710         void RebuildUnsavedMessageStoreL();
       
   711 
       
   712         /**
       
   713         *   Deletes the topics' and unsaved messages' store and rebuilds them.
       
   714         */
       
   715         void RebuildTopicAndUnsavedStoresL();
       
   716 
       
   717         /**
       
   718         *  Called to repair the database when Unsaved messages -file
       
   719         *  has been deleted, but Topics-file still contains information
       
   720         *  on unsaved messages.
       
   721         * 
       
   722         *  Things that are done here: 
       
   723         *       - Reset unread message counters, because a saved message
       
   724         *         is always also read.
       
   725         *       - Remove stream ids to unsaved messages from 
       
   726         *         Topic messages -streams.
       
   727         */
       
   728         void HandleDeletionOfUnsavedMessagesFileL();
       
   729 
       
   730         /**
       
   731         *   Called by HandleDeletionOfUnsavedMessagesFileL().
       
   732         *   Executes the necessary file operations.
       
   733         */
       
   734         void DoHandleDeletionOfUnsavedMessagesFileL();
       
   735 
       
   736         /**
       
   737         *   Replaces the given Topic message -stream with a stream which
       
   738         *   contains all ids of saved messages but no ids of unsaved messages.
       
   739         *
       
   740         *   @param  aMsgStreamId        Topic messages -stream id
       
   741         */
       
   742         void DeleteUnsavedMessageStreamIdsL( const TStreamId& aMsgStreamId ) const;
       
   743 
       
   744         /**
       
   745         *   Resolves the topic position in topic list and uses this information
       
   746         *   to call DoUpdateTopicL. Changes to stores are NOT commited.
       
   747         *   Call to DoUpdateTopicL is not trapped.
       
   748         *
       
   749         *   @param  aTopic              Topic to be written into stream.
       
   750         *   @param  aDeleting           Whether the call is due to a deletion.
       
   751         */
       
   752         void UpdateTopicCountersL( const TCbsDbTopic& aTopic,
       
   753             const TBool aDeleting );
       
   754 
       
   755         /**
       
   756         *   Creates a standard topic list, which has it's topics and other settings
       
   757         *   stored in Shared Data.
       
   758         *
       
   759         */
       
   760         void CreateStandardTopicListL();
       
   761 
       
   762         /**
       
   763         *   Load the topic IDs belonging to a specified topic list
       
   764         *
       
   765         *   @param aTopicListStreamId   Stream ID of the topic list
       
   766         */
       
   767         void LoadTopicsIdsL( const TStreamId& aTopicListStreamId );
       
   768 
       
   769         /**
       
   770         *   Load the topics specified by the topic list stream ID
       
   771         *
       
   772         *   @param aTopicListStreamId   Stream ID of the topic list
       
   773         */
       
   774         void LoadTopicsL( const TStreamId& aTopicListStreamId );
       
   775 
       
   776         /**
       
   777         *   Updates the topic stream IDs in topic lists
       
   778         *   
       
   779         *   @param  aTopicNumber    The topic number
       
   780         */
       
   781         void UpdateTopicStreamIdsL( const TCbsDbTopicNumber aTopicNumber );
       
   782 
       
   783     private:    // Data
       
   784 
       
   785         // Ref: Database
       
   786         CCbsDbImp& iDatabase;
       
   787 
       
   788         // Ref: Topic messages manager.
       
   789         CCbsDbImpTopicMessages* iMessages;
       
   790         
       
   791         // Session to the file server.
       
   792         RFs& iFs;
       
   793         
       
   794         // Own: Store which contains topics.
       
   795         CFileStore* iTopicStore;
       
   796 
       
   797 		// Own: Store which contains unsaved messages.
       
   798         CFileStore* iUnsavedMessageStore;
       
   799 
       
   800         // The last time iTopicStore and iUnsavedMessageStore were compacted
       
   801         TTime iPreviousCompact;
       
   802 
       
   803         // Current topic list in use
       
   804         TCbsDbImpTopicList iCurrentTopicList;
       
   805 
       
   806         // Own: Cached topic information.
       
   807         CArrayFixFlat< TCbsDbImpTopic >* iTopics;
       
   808 
       
   809         // Own: Registered topic list observers.
       
   810         CArrayFixFlat< MCbsDbTopicListObserver* >* iObservers;
       
   811 
       
   812         // Last topic added to topic list with topic detection.
       
   813         TCbsTopicNumber iLastTopicNumber;
       
   814 
       
   815         // Indicates whether there is a hotmarked message
       
   816         TBool iIsHotmarkedMessage;
       
   817 
       
   818         // Handle to the hotmarked message.
       
   819         TCbsDbMessageHandle iMessageHandle;
       
   820 
       
   821         // Own: Buffer containing the filename of topics file
       
   822         HBufC* iTopicsFilename;
       
   823 
       
   824         // Own: Buffer containing the filename of unsaved messages file
       
   825         HBufC* iUnsavedMessagesFilename;
       
   826 
       
   827         // Own: Cached topic list information.
       
   828         CArrayFixFlat< TCbsDbImpTopicList >* iTopicLists;
       
   829 
       
   830         // Own: Cached topic IDs
       
   831         CArrayFixFlat< TStreamId >* iTopicIds;               
       
   832 
       
   833         // Global topic count
       
   834         TInt iTopicCount;
       
   835 
       
   836         // Are we initializing Standard Topic list
       
   837         TBool iInitializing; 
       
   838 
       
   839         // Are we deleting all topics from current topic list
       
   840         TBool iDeleteAllTopics;
       
   841         
       
   842         // Local variation bits
       
   843         TInt iLVBits;
       
   844 
       
   845         __DECLARE_TEST;
       
   846     };
       
   847 
       
   848 #endif      // CCBSDBIMPTOPICLIST_H
       
   849             
       
   850 // End of File
       
   851 
       
   852