cbs/cbsserver/ServerSrc/CCbsDbImpTopicList.cpp
author hgs
Fri, 03 Sep 2010 13:59:20 +0300
changeset 46 2fa1fa551b0b
parent 33 8d5d7fcf9b59
permissions -rw-r--r--
201035
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
33
hgs
parents:
diff changeset
     1
/*
hgs
parents:
diff changeset
     2
* Copyright (c) 2003 Nokia Corporation and/or its subsidiary(-ies). 
hgs
parents:
diff changeset
     3
* All rights reserved.
hgs
parents:
diff changeset
     4
* This component and the accompanying materials are made available
hgs
parents:
diff changeset
     5
* under the terms of "Eclipse Public License v1.0"
hgs
parents:
diff changeset
     6
* which accompanies this distribution, and is available
hgs
parents:
diff changeset
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
hgs
parents:
diff changeset
     8
*
hgs
parents:
diff changeset
     9
* Initial Contributors:
hgs
parents:
diff changeset
    10
* Nokia Corporation - initial contribution.
hgs
parents:
diff changeset
    11
*
hgs
parents:
diff changeset
    12
* Contributors:
hgs
parents:
diff changeset
    13
*
hgs
parents:
diff changeset
    14
* Description:  This file contains the implementation of the CCbsDbImpTopicList class
hgs
parents:
diff changeset
    15
*            member functions.
hgs
parents:
diff changeset
    16
*    
hgs
parents:
diff changeset
    17
*                This class represents the topic list contained in the database.
hgs
parents:
diff changeset
    18
*
hgs
parents:
diff changeset
    19
*/
hgs
parents:
diff changeset
    20
hgs
parents:
diff changeset
    21
hgs
parents:
diff changeset
    22
hgs
parents:
diff changeset
    23
// INCLUDE FILES
hgs
parents:
diff changeset
    24
#include <e32base.h>
hgs
parents:
diff changeset
    25
#include <e32svr.h>
hgs
parents:
diff changeset
    26
#include <f32file.h>
hgs
parents:
diff changeset
    27
hgs
parents:
diff changeset
    28
#include <shareddataclient.h>   // RequestFreeDiskSpaceLC
hgs
parents:
diff changeset
    29
hgs
parents:
diff changeset
    30
#include <barsc.h>              // Resource access
hgs
parents:
diff changeset
    31
#include <barsread.h>           // Resource access
hgs
parents:
diff changeset
    32
46
hgs
parents: 33
diff changeset
    33
#include <cbsserver.rsg>
33
hgs
parents:
diff changeset
    34
hgs
parents:
diff changeset
    35
#include "CbsServerPanic.h"
hgs
parents:
diff changeset
    36
#include "CbsStreamHelper.h"
hgs
parents:
diff changeset
    37
#include "CbsDbConstants.h"
hgs
parents:
diff changeset
    38
#include "CbsUtils.h"
hgs
parents:
diff changeset
    39
#include "CCbsDbImpTopicList.h"
hgs
parents:
diff changeset
    40
#include "MCbsDbTopicListObserver.H"
hgs
parents:
diff changeset
    41
#include "CCbsDbImpTopicMessages.h"
hgs
parents:
diff changeset
    42
hgs
parents:
diff changeset
    43
#include "CbsLogger.h"
hgs
parents:
diff changeset
    44
hgs
parents:
diff changeset
    45
#include <centralrepository.h>  // for local variation
hgs
parents:
diff changeset
    46
#include "cbsinternalcrkeys.h"  // for local variation
hgs
parents:
diff changeset
    47
#include "cbsvariant.hrh"       // for local variation
hgs
parents:
diff changeset
    48
 
hgs
parents:
diff changeset
    49
// CONSTANTS
hgs
parents:
diff changeset
    50
hgs
parents:
diff changeset
    51
// Initial size for topic cache array
hgs
parents:
diff changeset
    52
const TInt KDefaultTopicListSize = 10;
hgs
parents:
diff changeset
    53
hgs
parents:
diff changeset
    54
// Size of the topic stream, used in checking against FFS free space limit
hgs
parents:
diff changeset
    55
const TInt KTopicStreamSize = 92;
hgs
parents:
diff changeset
    56
hgs
parents:
diff changeset
    57
// Size of the topic messages stream, FFS critical level check
hgs
parents:
diff changeset
    58
const TInt KEmptyTopicMessagesStreamSize = 4;
hgs
parents:
diff changeset
    59
hgs
parents:
diff changeset
    60
// Size of topic list stream, FFS critical level check
hgs
parents:
diff changeset
    61
const TInt KTopicListStreamSize = 85;
hgs
parents:
diff changeset
    62
hgs
parents:
diff changeset
    63
// Size of topic list stream (root), FFS critical level check
hgs
parents:
diff changeset
    64
const TInt KTopicListRootStreamSize = 2;
hgs
parents:
diff changeset
    65
hgs
parents:
diff changeset
    66
// Time in microseconds to wait after a critical store exception 
hgs
parents:
diff changeset
    67
// before a recovery attempt is made.
hgs
parents:
diff changeset
    68
const TInt KWaitAfterCriticalStoreException = 2000000;
hgs
parents:
diff changeset
    69
hgs
parents:
diff changeset
    70
// Used when unsaved message stream ids are deleted from topic messages 
hgs
parents:
diff changeset
    71
// stream
hgs
parents:
diff changeset
    72
const TInt KTypicalNumberOfTopicMessages = 6;
hgs
parents:
diff changeset
    73
hgs
parents:
diff changeset
    74
// Minimum interval between compacting the stores in minutes
hgs
parents:
diff changeset
    75
const TInt KMinimumCompactInterval = 30;
hgs
parents:
diff changeset
    76
hgs
parents:
diff changeset
    77
// Number of the index topic
hgs
parents:
diff changeset
    78
const TInt KIndexTopicNumber = 0;
hgs
parents:
diff changeset
    79
hgs
parents:
diff changeset
    80
// Space for reading messages
hgs
parents:
diff changeset
    81
const TInt KReadMessageSize = 92;
hgs
parents:
diff changeset
    82
hgs
parents:
diff changeset
    83
const TInt KTopicsGranularity = 1;
hgs
parents:
diff changeset
    84
hgs
parents:
diff changeset
    85
const TInt KTopicListsGranularity = 1;
hgs
parents:
diff changeset
    86
hgs
parents:
diff changeset
    87
const TInt KTopicIdsGranularity = 5;
hgs
parents:
diff changeset
    88
hgs
parents:
diff changeset
    89
// ================= MEMBER FUNCTIONS =======================
hgs
parents:
diff changeset
    90
hgs
parents:
diff changeset
    91
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
    92
// CCbsDbImpTopicList::CCbsDbImpTopicList
hgs
parents:
diff changeset
    93
// C++ default constructor can NOT contain any code, that
hgs
parents:
diff changeset
    94
// might leave.
hgs
parents:
diff changeset
    95
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
    96
//
hgs
parents:
diff changeset
    97
CCbsDbImpTopicList::CCbsDbImpTopicList( 
hgs
parents:
diff changeset
    98
    RFs& aFs, 
hgs
parents:
diff changeset
    99
    CCbsDbImp& aDatabase )
hgs
parents:
diff changeset
   100
    : iDatabase( aDatabase ), 
hgs
parents:
diff changeset
   101
    iFs( aFs ),
hgs
parents:
diff changeset
   102
    iTopicCount( 0 ),
hgs
parents:
diff changeset
   103
    iInitializing( EFalse ),
hgs
parents:
diff changeset
   104
    iDeleteAllTopics( EFalse )
hgs
parents:
diff changeset
   105
    {
hgs
parents:
diff changeset
   106
    }
hgs
parents:
diff changeset
   107
hgs
parents:
diff changeset
   108
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
   109
// CCbsDbImpTopicList::ConstructL
hgs
parents:
diff changeset
   110
// Symbian 2nd phase constructor can leave.
hgs
parents:
diff changeset
   111
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
   112
//
hgs
parents:
diff changeset
   113
void CCbsDbImpTopicList::ConstructL( 
hgs
parents:
diff changeset
   114
    const TDesC& aTopicsFile,
hgs
parents:
diff changeset
   115
    const TDesC& aUnsavedMessagesFile )
hgs
parents:
diff changeset
   116
    {
hgs
parents:
diff changeset
   117
    CBSLOGSTRING("CBSSERVER: >>> CCbsDbImpTopicList::ConstructL()");
hgs
parents:
diff changeset
   118
    
hgs
parents:
diff changeset
   119
    // Create observer array
hgs
parents:
diff changeset
   120
    iObservers = new ( ELeave ) CArrayFixFlat< MCbsDbTopicListObserver* >( 
hgs
parents:
diff changeset
   121
        KCbsDbObserverArraySize );
hgs
parents:
diff changeset
   122
hgs
parents:
diff changeset
   123
    // Create topic array, additional memory size in new alloc, 1*192 B = 192 B
hgs
parents:
diff changeset
   124
    iTopics = new ( ELeave ) CArrayFixFlat< TCbsDbImpTopic >( KTopicsGranularity );
hgs
parents:
diff changeset
   125
hgs
parents:
diff changeset
   126
    // Create the root item table, additional memory size in new alloc, 1*184 B = 184 B
hgs
parents:
diff changeset
   127
    iTopicLists = new ( ELeave ) CArrayFixFlat< TCbsDbImpTopicList >( KTopicListsGranularity ); 
hgs
parents:
diff changeset
   128
hgs
parents:
diff changeset
   129
    // Create topic ID array, additional memory size in new alloc, 5*4 B = 20 B
hgs
parents:
diff changeset
   130
    iTopicIds = new ( ELeave ) CArrayFixFlat< TStreamId >( KTopicIdsGranularity ); 
hgs
parents:
diff changeset
   131
hgs
parents:
diff changeset
   132
    // Initialize iPreviousCompact to now
hgs
parents:
diff changeset
   133
    iPreviousCompact.UniversalTime();
hgs
parents:
diff changeset
   134
hgs
parents:
diff changeset
   135
    // Copy datafile names from parameters
hgs
parents:
diff changeset
   136
    iTopicsFilename = aTopicsFile.AllocL();
hgs
parents:
diff changeset
   137
    iUnsavedMessagesFilename = aUnsavedMessagesFile.AllocL();
hgs
parents:
diff changeset
   138
    
hgs
parents:
diff changeset
   139
    // Fetch local variation bits from CenRep    
hgs
parents:
diff changeset
   140
    CRepository* repository = CRepository::NewL( KCRUidCbsVariation );    
hgs
parents:
diff changeset
   141
    TInt err = repository->Get( KCbsVariationFlags, iLVBits );
hgs
parents:
diff changeset
   142
    CBSLOGSTRING2("CBSSERVER: CCbsRecEtel::ConstructL(): CenRep error: %d", err );    
hgs
parents:
diff changeset
   143
    if ( err )
hgs
parents:
diff changeset
   144
        {
hgs
parents:
diff changeset
   145
        iLVBits = 0;
hgs
parents:
diff changeset
   146
        }    
hgs
parents:
diff changeset
   147
    delete repository;
hgs
parents:
diff changeset
   148
hgs
parents:
diff changeset
   149
    // If aLoadFactorySettings, then the files are recreated and initialized.
hgs
parents:
diff changeset
   150
    // If not, then the nonexisting files are created, files opened and
hgs
parents:
diff changeset
   151
    // data internalized.
hgs
parents:
diff changeset
   152
hgs
parents:
diff changeset
   153
    TBool unsavedMsgFileExists( CbsUtils::ExistsL( iFs, *iUnsavedMessagesFilename ) );
hgs
parents:
diff changeset
   154
hgs
parents:
diff changeset
   155
    TBool loadFactorySettings( !CbsUtils::ExistsL( iFs, aTopicsFile ) );
hgs
parents:
diff changeset
   156
    TRAPD( error, OpenFilesL( loadFactorySettings, EFalse ) );
hgs
parents:
diff changeset
   157
    
hgs
parents:
diff changeset
   158
    CBSLOGSTRING2("CBSSERVER: CCbsDbImpTopicList::ConstructL(): OpenFilesL() error: %d", error );
hgs
parents:
diff changeset
   159
    
hgs
parents:
diff changeset
   160
    if ( error == KErrDiskFull )
hgs
parents:
diff changeset
   161
        {
hgs
parents:
diff changeset
   162
        // Delete the store and open is again
hgs
parents:
diff changeset
   163
        CBSLOGSTRING("CBSSERVER: CCbsDbImpTopicList::ConstructL(): About to delete iTopicStore..." );
hgs
parents:
diff changeset
   164
        
hgs
parents:
diff changeset
   165
        delete iTopicStore;
hgs
parents:
diff changeset
   166
        iTopicStore = NULL;
hgs
parents:
diff changeset
   167
        
hgs
parents:
diff changeset
   168
        CBSLOGSTRING("CBSSERVER: CCbsDbImpTopicList::ConstructL(): iTopicStore deletion finished." );
hgs
parents:
diff changeset
   169
hgs
parents:
diff changeset
   170
        // Try to open the store again   
hgs
parents:
diff changeset
   171
        TRAPD( error, iTopicStore = CFileStore::OpenL( iFs, *iTopicsFilename, EFileRead | EFileWrite ) );
hgs
parents:
diff changeset
   172
        CBSLOGSTRING2("CBSSERVER: CCbsDbImpTopicList::ConstructL(); iTopicStore OpenL() error: %d", error );
hgs
parents:
diff changeset
   173
        if ( error )
hgs
parents:
diff changeset
   174
            {
hgs
parents:
diff changeset
   175
            InitializeListL( ETrue );
hgs
parents:
diff changeset
   176
            }        
hgs
parents:
diff changeset
   177
        }
hgs
parents:
diff changeset
   178
    else
hgs
parents:
diff changeset
   179
        {
hgs
parents:
diff changeset
   180
        if ( loadFactorySettings )
hgs
parents:
diff changeset
   181
            {
hgs
parents:
diff changeset
   182
            // Create a topic list for standard topics
hgs
parents:
diff changeset
   183
            if ( iTopicLists->Count() == 0 )
hgs
parents:
diff changeset
   184
                {
hgs
parents:
diff changeset
   185
                CreateStandardTopicListL();
hgs
parents:
diff changeset
   186
                }                
hgs
parents:
diff changeset
   187
            }
hgs
parents:
diff changeset
   188
hgs
parents:
diff changeset
   189
        // Only load the topics, if not already loaded when opening the files
hgs
parents:
diff changeset
   190
        if ( loadFactorySettings || unsavedMsgFileExists )
hgs
parents:
diff changeset
   191
            {
hgs
parents:
diff changeset
   192
            // Load the topics
hgs
parents:
diff changeset
   193
            LoadDefaultTopicStreamL();
hgs
parents:
diff changeset
   194
            }
hgs
parents:
diff changeset
   195
        
hgs
parents:
diff changeset
   196
        // Compact the topic store
hgs
parents:
diff changeset
   197
        TopicStoreL()->CompactL();
hgs
parents:
diff changeset
   198
        TopicStoreL()->CommitL();
hgs
parents:
diff changeset
   199
        }            
hgs
parents:
diff changeset
   200
    
hgs
parents:
diff changeset
   201
    // Reset the unread message count to be sure that UI client gets the correct information,
hgs
parents:
diff changeset
   202
    // at this point count is always zero.
hgs
parents:
diff changeset
   203
    for ( TInt i( 0 ); i < iTopics->Count(); ++i )
hgs
parents:
diff changeset
   204
        {
hgs
parents:
diff changeset
   205
        iTopics->At( i ).iTopicData.iUnreadMessages = 0;
hgs
parents:
diff changeset
   206
        }
hgs
parents:
diff changeset
   207
        
hgs
parents:
diff changeset
   208
    __TEST_INVARIANT;
hgs
parents:
diff changeset
   209
    
hgs
parents:
diff changeset
   210
    CBSLOGSTRING("CBSSERVER: <<< CCbsDbImpTopicList::ConstructL()");
hgs
parents:
diff changeset
   211
    }
hgs
parents:
diff changeset
   212
hgs
parents:
diff changeset
   213
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
   214
// CCbsDbImpTopicList::NewL
hgs
parents:
diff changeset
   215
// Two-phased constructor.
hgs
parents:
diff changeset
   216
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
   217
//
hgs
parents:
diff changeset
   218
CCbsDbImpTopicList* CCbsDbImpTopicList::NewL( 
hgs
parents:
diff changeset
   219
    RFs& aFs, 
hgs
parents:
diff changeset
   220
    const TDesC& aTopicsFile, 
hgs
parents:
diff changeset
   221
    const TDesC& aUnsavedMessagesFile,
hgs
parents:
diff changeset
   222
    CCbsDbImp& aDatabase )
hgs
parents:
diff changeset
   223
    {
hgs
parents:
diff changeset
   224
    CCbsDbImpTopicList* self = 
hgs
parents:
diff changeset
   225
        new ( ELeave ) CCbsDbImpTopicList( aFs, aDatabase );
hgs
parents:
diff changeset
   226
    CleanupStack::PushL( self );
hgs
parents:
diff changeset
   227
    self->ConstructL( aTopicsFile, aUnsavedMessagesFile );
hgs
parents:
diff changeset
   228
    CleanupStack::Pop();
hgs
parents:
diff changeset
   229
    return self;
hgs
parents:
diff changeset
   230
    }
hgs
parents:
diff changeset
   231
hgs
parents:
diff changeset
   232
// Destructor
hgs
parents:
diff changeset
   233
CCbsDbImpTopicList::~CCbsDbImpTopicList()
hgs
parents:
diff changeset
   234
    {
hgs
parents:
diff changeset
   235
    CBSLOGSTRING("CBSSERVER: >>> CCbsDbImpTopicList::~CCbsDbImpTopicList()");
hgs
parents:
diff changeset
   236
    delete iTopics;
hgs
parents:
diff changeset
   237
    delete iTopicStore;
hgs
parents:
diff changeset
   238
    delete iTopicLists;
hgs
parents:
diff changeset
   239
    delete iTopicIds;
hgs
parents:
diff changeset
   240
    delete iUnsavedMessageStore;
hgs
parents:
diff changeset
   241
    delete iObservers;
hgs
parents:
diff changeset
   242
    delete iUnsavedMessagesFilename;
hgs
parents:
diff changeset
   243
    if ( iTopicsFilename )
hgs
parents:
diff changeset
   244
        {
hgs
parents:
diff changeset
   245
        delete iTopicsFilename;
hgs
parents:
diff changeset
   246
        }
hgs
parents:
diff changeset
   247
    CBSLOGSTRING("CBSSERVER: <<< CCbsDbImpTopicList::~CCbsDbImpTopicList()");
hgs
parents:
diff changeset
   248
    }
hgs
parents:
diff changeset
   249
hgs
parents:
diff changeset
   250
#ifndef __SECURE_BACKUP__
hgs
parents:
diff changeset
   251
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
   252
// CCbsDbImpTopicList::ChangeFileLockL
hgs
parents:
diff changeset
   253
// Closes or reopens the settings file if requested by a backup.
hgs
parents:
diff changeset
   254
// (other items were commented in a header).
hgs
parents:
diff changeset
   255
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
   256
//
hgs
parents:
diff changeset
   257
void CCbsDbImpTopicList::ChangeFileLockL( 
hgs
parents:
diff changeset
   258
    const TDesC& aFileName,
hgs
parents:
diff changeset
   259
    TFileLockFlags aFlags )
hgs
parents:
diff changeset
   260
    {
hgs
parents:
diff changeset
   261
    CBSLOGSTRING2("CBSSERVER: >>> CCbsDbImpTopicList::ChangeFileLockL() (1): flag: %d", aFlags );
hgs
parents:
diff changeset
   262
    
hgs
parents:
diff changeset
   263
    if ( aFlags == ETakeLock && iTopicStore == NULL )
hgs
parents:
diff changeset
   264
        {
hgs
parents:
diff changeset
   265
        CBSLOGSTRING("CBSSERVER: CCbsDbImpTopicList::ChangeFileLockL() (1): Try to open store...");
hgs
parents:
diff changeset
   266
        
hgs
parents:
diff changeset
   267
        // Try to open the store.
hgs
parents:
diff changeset
   268
        iTopicStore = CPermanentFileStore::OpenL( iFs, 
hgs
parents:
diff changeset
   269
            aFileName, EFileRead | EFileWrite );
hgs
parents:
diff changeset
   270
            
hgs
parents:
diff changeset
   271
        CBSLOGSTRING("CBSSERVER: CCbsDbImpTopicList::ChangeFileLockL() (1): Store opened.");
hgs
parents:
diff changeset
   272
        }
hgs
parents:
diff changeset
   273
    else if ( aFlags != ETakeLock )
hgs
parents:
diff changeset
   274
        {
hgs
parents:
diff changeset
   275
        CBSLOGSTRING("CBSSERVER: CCbsDbImpTopicList::ChangeFileLockL() (1): Deleting iTopicStore...");
hgs
parents:
diff changeset
   276
        
hgs
parents:
diff changeset
   277
        delete iTopicStore; 
hgs
parents:
diff changeset
   278
        iTopicStore = NULL;
hgs
parents:
diff changeset
   279
        
hgs
parents:
diff changeset
   280
        CBSLOGSTRING("CBSSERVER: CCbsDbImpTopicList::ChangeFileLockL() (1): iTopicStore deleted.");
hgs
parents:
diff changeset
   281
        }
hgs
parents:
diff changeset
   282
    CBSLOGSTRING("CBSSERVER: <<< CCbsDbImpTopicList::ChangeFileLockL() (1)");
hgs
parents:
diff changeset
   283
    }
hgs
parents:
diff changeset
   284
#else
hgs
parents:
diff changeset
   285
hgs
parents:
diff changeset
   286
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
   287
// CCbsDbImpSettings::ChangeFileLockL
hgs
parents:
diff changeset
   288
// Closes or reopens the settings file if requested by backup.
hgs
parents:
diff changeset
   289
// (other items were commented in a header).
hgs
parents:
diff changeset
   290
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
   291
//    
hgs
parents:
diff changeset
   292
void CCbsDbImpTopicList::ChangeFileLockL( const TCbsBackupRequest& aRequest )
hgs
parents:
diff changeset
   293
    {
hgs
parents:
diff changeset
   294
    CBSLOGSTRING2("CBSSERVER: >>> CCbsDbImpTopicList::ChangeFileLockL() (2): aRequest: %d", aRequest );
hgs
parents:
diff changeset
   295
    
hgs
parents:
diff changeset
   296
    // If backing up or restoring, release locks
hgs
parents:
diff changeset
   297
    if ( ( aRequest == ECbsBackup || 
hgs
parents:
diff changeset
   298
        aRequest == ECbsRestore ) )
hgs
parents:
diff changeset
   299
        {
hgs
parents:
diff changeset
   300
        delete iTopicStore; 
hgs
parents:
diff changeset
   301
        iTopicStore = NULL;
hgs
parents:
diff changeset
   302
        
hgs
parents:
diff changeset
   303
        CBSLOGSTRING("CBSSERVER: CCbsDbImpTopicList::ChangeFileLockL() (2): iTopicStore deleted.");
hgs
parents:
diff changeset
   304
        }
hgs
parents:
diff changeset
   305
    // Else take files into use again
hgs
parents:
diff changeset
   306
    else if ( ( aRequest == ECbsNoBackup || 
hgs
parents:
diff changeset
   307
        aRequest == ECbsBackupNotDefined ) && 
hgs
parents:
diff changeset
   308
        iTopicStore == NULL)
hgs
parents:
diff changeset
   309
        {
hgs
parents:
diff changeset
   310
        CBSLOGSTRING("CBSSERVER: CCbsDbImpTopicList::ChangeFileLockL() (2): Calling CPermanentFileStore::OpenL()...");
hgs
parents:
diff changeset
   311
        // Try to open the store.
hgs
parents:
diff changeset
   312
        iTopicStore = CPermanentFileStore::OpenL( iFs, 
hgs
parents:
diff changeset
   313
            iTopicsFilename->Des(), EFileRead | EFileWrite );
hgs
parents:
diff changeset
   314
        CBSLOGSTRING("CBSSERVER: CCbsDbImpTopicList::ChangeFileLockL() (2): CPermanentFileStore::OpenL() finished.");
hgs
parents:
diff changeset
   315
        }
hgs
parents:
diff changeset
   316
    CBSLOGSTRING("CBSSERVER: <<< CCbsDbImpTopicList::ChangeFileLockL() (2)");
hgs
parents:
diff changeset
   317
    }
hgs
parents:
diff changeset
   318
hgs
parents:
diff changeset
   319
#endif
hgs
parents:
diff changeset
   320
hgs
parents:
diff changeset
   321
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
   322
// CCbsDbImpTopicList::CheckFileLockL
hgs
parents:
diff changeset
   323
// Check if the server has a file lock.
hgs
parents:
diff changeset
   324
// (other items were commented in a header).
hgs
parents:
diff changeset
   325
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
   326
//
hgs
parents:
diff changeset
   327
void CCbsDbImpTopicList::CheckFileLockL() const
hgs
parents:
diff changeset
   328
    {
hgs
parents:
diff changeset
   329
    CBSLOGSTRING("CBSSERVER: >>> CCbsDbImpTopicList::CheckFileLockL()");
hgs
parents:
diff changeset
   330
    if ( iTopicStore == NULL )
hgs
parents:
diff changeset
   331
        {
hgs
parents:
diff changeset
   332
        CBSLOGSTRING("CBSSERVER: CCbsDbImpTopicList::CheckFileLockL(): iTopicStore == NULL, leaving with KErrLocked...");
hgs
parents:
diff changeset
   333
        User::Leave( KErrLocked );
hgs
parents:
diff changeset
   334
        }
hgs
parents:
diff changeset
   335
    CBSLOGSTRING("CBSSERVER: <<< CCbsDbImpTopicList::CheckFileLockL()");
hgs
parents:
diff changeset
   336
    }
hgs
parents:
diff changeset
   337
hgs
parents:
diff changeset
   338
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
   339
// CCbsDbImpTopicList::CreateNewTopicListL
hgs
parents:
diff changeset
   340
// Creates a new topic list.
hgs
parents:
diff changeset
   341
// (other items were commented in a header).
hgs
parents:
diff changeset
   342
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
   343
//
hgs
parents:
diff changeset
   344
void CCbsDbImpTopicList::CreateNewTopicListL( const TDesC& aTopicListName )
hgs
parents:
diff changeset
   345
    {
hgs
parents:
diff changeset
   346
    // FFS critical level check
hgs
parents:
diff changeset
   347
    CbsUtils::FFSCriticalLevelCheckL( KTopicListStreamSize + 
hgs
parents:
diff changeset
   348
        KCbsDbTopicArraySize, iFs );
hgs
parents:
diff changeset
   349
hgs
parents:
diff changeset
   350
    TInt topicListCount = iTopicLists->Count();
hgs
parents:
diff changeset
   351
hgs
parents:
diff changeset
   352
    // Check if there already is 10 topic lists
hgs
parents:
diff changeset
   353
    if ( topicListCount < KCbsRootItemsSize )
hgs
parents:
diff changeset
   354
        {                  
hgs
parents:
diff changeset
   355
        // Create the stream for this topic list entry
hgs
parents:
diff changeset
   356
        RStoreWriteStream outstream;
hgs
parents:
diff changeset
   357
        TStreamId topicListStreamId( 
hgs
parents:
diff changeset
   358
            outstream.CreateLC( *TopicStoreL() ) ); // on CS        
hgs
parents:
diff changeset
   359
hgs
parents:
diff changeset
   360
        // Set the values for this new topic list and set it as "current".
hgs
parents:
diff changeset
   361
        // Reset the topic count of this topic list.
hgs
parents:
diff changeset
   362
        iCurrentTopicList.iTopicCount = 0;
hgs
parents:
diff changeset
   363
hgs
parents:
diff changeset
   364
        // Topic list stream id for this list
hgs
parents:
diff changeset
   365
        iCurrentTopicList.iTopicListId = topicListStreamId;
hgs
parents:
diff changeset
   366
    
hgs
parents:
diff changeset
   367
        // List name
hgs
parents:
diff changeset
   368
        iCurrentTopicList.iTopicListName = aTopicListName; 
hgs
parents:
diff changeset
   369
hgs
parents:
diff changeset
   370
        // Is this the default list
hgs
parents:
diff changeset
   371
        iCurrentTopicList.iIsDefaultTopicList = ETrue;
hgs
parents:
diff changeset
   372
hgs
parents:
diff changeset
   373
        // Number of this list, which is a int value between 0...9
hgs
parents:
diff changeset
   374
        iCurrentTopicList.iNumber = iTopicLists->Count();
hgs
parents:
diff changeset
   375
hgs
parents:
diff changeset
   376
        // Add this list to the list array        
hgs
parents:
diff changeset
   377
        TKeyArrayFix key( _FOFF( TCbsDbImpTopicList, iNumber ), ECmpTUint16 );
hgs
parents:
diff changeset
   378
        iTopicLists->InsertIsqL( iCurrentTopicList, key );
hgs
parents:
diff changeset
   379
    
hgs
parents:
diff changeset
   380
        // Write the values to this topic list's stream
hgs
parents:
diff changeset
   381
        outstream << iCurrentTopicList.iTopicListName;
hgs
parents:
diff changeset
   382
        CbsStreamHelper::WriteBoolL( outstream, iCurrentTopicList.iIsDefaultTopicList );
hgs
parents:
diff changeset
   383
        outstream.WriteInt16L( iCurrentTopicList.iNumber );
hgs
parents:
diff changeset
   384
        outstream.WriteInt16L( iCurrentTopicList.iTopicCount );
hgs
parents:
diff changeset
   385
hgs
parents:
diff changeset
   386
        // Write space for topic stream IDs        
hgs
parents:
diff changeset
   387
        for ( TInt i( 0 ); i < KCbsDbTopicArraySize; i++ )
hgs
parents:
diff changeset
   388
            {
hgs
parents:
diff changeset
   389
            outstream << TStreamId( 0 );
hgs
parents:
diff changeset
   390
            }        
hgs
parents:
diff changeset
   391
           
hgs
parents:
diff changeset
   392
        outstream.CommitL();
hgs
parents:
diff changeset
   393
        CleanupStack::PopAndDestroy(); // outstream    
hgs
parents:
diff changeset
   394
        
hgs
parents:
diff changeset
   395
        // Reset the topic array and add an index topic to this
hgs
parents:
diff changeset
   396
        // topic list
hgs
parents:
diff changeset
   397
        iTopics->Reset();
hgs
parents:
diff changeset
   398
        AddIndexTopicL();
hgs
parents:
diff changeset
   399
            
hgs
parents:
diff changeset
   400
        // Update the root stream
hgs
parents:
diff changeset
   401
        UpdateRootStreamL( EFalse );
hgs
parents:
diff changeset
   402
    
hgs
parents:
diff changeset
   403
        // Commit file changes
hgs
parents:
diff changeset
   404
        CommitFilesL();
hgs
parents:
diff changeset
   405
hgs
parents:
diff changeset
   406
        // Load the topics (count = 1, Index topic only), 
hgs
parents:
diff changeset
   407
        // so that the array is up to date
hgs
parents:
diff changeset
   408
        LoadTopicsL( iCurrentTopicList.iTopicListId );            
hgs
parents:
diff changeset
   409
        } 
hgs
parents:
diff changeset
   410
    else
hgs
parents:
diff changeset
   411
        {
hgs
parents:
diff changeset
   412
        User::Leave( KErrGeneral );
hgs
parents:
diff changeset
   413
        }   
hgs
parents:
diff changeset
   414
    }
hgs
parents:
diff changeset
   415
hgs
parents:
diff changeset
   416
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
   417
// CCbsDbImpTopicList::SetTopicMessages
hgs
parents:
diff changeset
   418
// Sets the topic messages db object for this topic list.
hgs
parents:
diff changeset
   419
// Note that this function does not transfer ownership
hgs
parents:
diff changeset
   420
// of aMessages.
hgs
parents:
diff changeset
   421
// (other items were commented in a header).
hgs
parents:
diff changeset
   422
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
   423
//
hgs
parents:
diff changeset
   424
void CCbsDbImpTopicList::SetTopicMessages( 
hgs
parents:
diff changeset
   425
    CCbsDbImpTopicMessages* aMessages )
hgs
parents:
diff changeset
   426
    {
hgs
parents:
diff changeset
   427
    __ASSERT_ALWAYS( aMessages != NULL,
hgs
parents:
diff changeset
   428
        CbsServerPanic( ECbsTopicMessagesNull ) );
hgs
parents:
diff changeset
   429
    iMessages = aMessages;
hgs
parents:
diff changeset
   430
    }
hgs
parents:
diff changeset
   431
hgs
parents:
diff changeset
   432
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
   433
// CCbsDbImpTopicList::TopicCount
hgs
parents:
diff changeset
   434
// Returns the total amount of topics the list contains.
hgs
parents:
diff changeset
   435
// (other items were commented in a header).
hgs
parents:
diff changeset
   436
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
   437
//
hgs
parents:
diff changeset
   438
TInt CCbsDbImpTopicList::TopicCount() const
hgs
parents:
diff changeset
   439
    {
hgs
parents:
diff changeset
   440
    return iTopics->Count();
hgs
parents:
diff changeset
   441
    }
hgs
parents:
diff changeset
   442
hgs
parents:
diff changeset
   443
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
   444
// CCbsDbImpTopicList::TopicListCount
hgs
parents:
diff changeset
   445
// Get topic list count.
hgs
parents:
diff changeset
   446
// (other items were commented in a header).
hgs
parents:
diff changeset
   447
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
   448
//
hgs
parents:
diff changeset
   449
TInt CCbsDbImpTopicList::TopicListCount() const
hgs
parents:
diff changeset
   450
    {
hgs
parents:
diff changeset
   451
    return iTopicLists->Count();
hgs
parents:
diff changeset
   452
    }
hgs
parents:
diff changeset
   453
hgs
parents:
diff changeset
   454
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
   455
// CCbsDbImpTopicList::TopicStoreL
hgs
parents:
diff changeset
   456
// Returns pointer to the current store which contains topics
hgs
parents:
diff changeset
   457
// of the server (i.e., Topics-file) and saved messages. 
hgs
parents:
diff changeset
   458
// (other items were commented in a header).
hgs
parents:
diff changeset
   459
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
   460
//
hgs
parents:
diff changeset
   461
CFileStore* CCbsDbImpTopicList::TopicStoreL() const
hgs
parents:
diff changeset
   462
    {
hgs
parents:
diff changeset
   463
    CheckFileLockL();
hgs
parents:
diff changeset
   464
    return iTopicStore;
hgs
parents:
diff changeset
   465
    }
hgs
parents:
diff changeset
   466
hgs
parents:
diff changeset
   467
// ---------------------------------------------------------
hgs
parents:
diff changeset
   468
// TopicFilename() 
hgs
parents:
diff changeset
   469
//
hgs
parents:
diff changeset
   470
// ---------------------------------------------------------
hgs
parents:
diff changeset
   471
const TDesC& CCbsDbImpTopicList::TopicFilename() const
hgs
parents:
diff changeset
   472
    {
hgs
parents:
diff changeset
   473
    return *iTopicsFilename;
hgs
parents:
diff changeset
   474
    }
hgs
parents:
diff changeset
   475
hgs
parents:
diff changeset
   476
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
   477
// CCbsDbImpTopicList::UnsavedMessagesStore
hgs
parents:
diff changeset
   478
// Returns a pointer to the store, which contains unsaved
hgs
parents:
diff changeset
   479
// messages of the server (Unsaved Messages-file, "cbs4.dat").
hgs
parents:
diff changeset
   480
// (other items were commented in a header).
hgs
parents:
diff changeset
   481
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
   482
//
hgs
parents:
diff changeset
   483
CFileStore* CCbsDbImpTopicList::UnsavedMessagesStore() const
hgs
parents:
diff changeset
   484
    {
hgs
parents:
diff changeset
   485
    return iUnsavedMessageStore;
hgs
parents:
diff changeset
   486
    }
hgs
parents:
diff changeset
   487
hgs
parents:
diff changeset
   488
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
   489
// CCbsDbImpTopicList::UnsavedMessagesFilename
hgs
parents:
diff changeset
   490
// Returns a reference to the name of the file, which contains unsaved
hgs
parents:
diff changeset
   491
// messages of the server (Unsaved Messages-file, "cbs4.dat").
hgs
parents:
diff changeset
   492
// (other items were commented in a header).
hgs
parents:
diff changeset
   493
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
   494
//
hgs
parents:
diff changeset
   495
const TDesC& CCbsDbImpTopicList::UnsavedMessagesFilename() const
hgs
parents:
diff changeset
   496
    {
hgs
parents:
diff changeset
   497
    return *iUnsavedMessagesFilename;
hgs
parents:
diff changeset
   498
    }
hgs
parents:
diff changeset
   499
hgs
parents:
diff changeset
   500
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
   501
// CCbsDbImpTopicList::ExtractTopicNumber
hgs
parents:
diff changeset
   502
// Extracts topic handle from message handle.
hgs
parents:
diff changeset
   503
// Note that the method does not check that the message exists.
hgs
parents:
diff changeset
   504
// (other items were commented in a header).
hgs
parents:
diff changeset
   505
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
   506
//
hgs
parents:
diff changeset
   507
TCbsDbTopicNumber CCbsDbImpTopicList::ExtractTopicNumber( 
hgs
parents:
diff changeset
   508
    const TCbsDbMessageHandle& aHandle ) const
hgs
parents:
diff changeset
   509
    {    
hgs
parents:
diff changeset
   510
    return TCbsDbTopicNumber( aHandle >> 16 );
hgs
parents:
diff changeset
   511
    }
hgs
parents:
diff changeset
   512
hgs
parents:
diff changeset
   513
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
   514
// CCbsDbImpTopicList::GetTopicMessagesIdL
hgs
parents:
diff changeset
   515
// Returns the topic messages stream id by topic handle.
hgs
parents:
diff changeset
   516
// (other items were commented in a header).
hgs
parents:
diff changeset
   517
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
   518
//
hgs
parents:
diff changeset
   519
void CCbsDbImpTopicList::GetTopicMessagesIdL( 
hgs
parents:
diff changeset
   520
    TCbsDbTopicNumber aNumber, TStreamId& aId ) const
hgs
parents:
diff changeset
   521
    {
hgs
parents:
diff changeset
   522
    CBSLOGSTRING("CBSSERVER: >>> CCbsDbImpTopicList::GetTopicMessagesIdL()");
hgs
parents:
diff changeset
   523
hgs
parents:
diff changeset
   524
    // Find the topic.
hgs
parents:
diff changeset
   525
    TInt index( TopicIndexInList( aNumber ) );
hgs
parents:
diff changeset
   526
    CBSLOGSTRING2("CBSSERVER: CCbsDbImpTopicList::GetTopicMessagesIdL(): Leaving if index != 0. Index: %d.", index );
hgs
parents:
diff changeset
   527
    User::LeaveIfError( index );
hgs
parents:
diff changeset
   528
hgs
parents:
diff changeset
   529
    // Return the topic message stream id.
hgs
parents:
diff changeset
   530
    aId = iTopics->At( index ).iTopicMessagesId;
hgs
parents:
diff changeset
   531
    
hgs
parents:
diff changeset
   532
    CBSLOGSTRING("CBSSERVER: <<< CCbsDbImpTopicList::GetTopicMessagesIdL()");
hgs
parents:
diff changeset
   533
    }
hgs
parents:
diff changeset
   534
hgs
parents:
diff changeset
   535
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
   536
// CCbsDbImpTopicList::UpdateTopicMessagesIdL
hgs
parents:
diff changeset
   537
// Updates the topic messages stream id by topic handle. 
hgs
parents:
diff changeset
   538
// The new identifier is expected not to be a null id.
hgs
parents:
diff changeset
   539
// Note that the method will not commit changes to the store. 
hgs
parents:
diff changeset
   540
// It also changes the internal state and thus if the method leaves, 
hgs
parents:
diff changeset
   541
// it is good to reload the whole root stream.
hgs
parents:
diff changeset
   542
// The method will delete the old topic messages stream.
hgs
parents:
diff changeset
   543
// (other items were commented in a header).
hgs
parents:
diff changeset
   544
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
   545
//
hgs
parents:
diff changeset
   546
void CCbsDbImpTopicList::UpdateTopicMessagesIdL( 
hgs
parents:
diff changeset
   547
    TCbsDbTopicNumber aNumber, 
hgs
parents:
diff changeset
   548
    const TStreamId& aNewId )
hgs
parents:
diff changeset
   549
    {
hgs
parents:
diff changeset
   550
    __TEST_INVARIANT;
hgs
parents:
diff changeset
   551
    // Find position.
hgs
parents:
diff changeset
   552
    TInt index( TopicIndexInList( aNumber ) );
hgs
parents:
diff changeset
   553
    User::LeaveIfError( index );
hgs
parents:
diff changeset
   554
hgs
parents:
diff changeset
   555
    // Get the old id.
hgs
parents:
diff changeset
   556
    TStreamId oldId( iTopics->At( index ).iTopicMessagesId );
hgs
parents:
diff changeset
   557
hgs
parents:
diff changeset
   558
    // Get the topic information.
hgs
parents:
diff changeset
   559
    TCbsDbTopic topic;
hgs
parents:
diff changeset
   560
    GetTopicL( index, topic );
hgs
parents:
diff changeset
   561
hgs
parents:
diff changeset
   562
    // Write information to the stream.
hgs
parents:
diff changeset
   563
    RStoreWriteStream outstream;
hgs
parents:
diff changeset
   564
    outstream.OpenLC( *TopicStoreL(), iTopics->At( index ).iTopicId ); // on CS
hgs
parents:
diff changeset
   565
    WriteTopicInformationL( outstream, topic, aNewId );
hgs
parents:
diff changeset
   566
    outstream.CommitL();
hgs
parents:
diff changeset
   567
    CleanupStack::PopAndDestroy();
hgs
parents:
diff changeset
   568
hgs
parents:
diff changeset
   569
    // Delete the old stream.
hgs
parents:
diff changeset
   570
    TopicStoreL()->DeleteL( oldId );
hgs
parents:
diff changeset
   571
hgs
parents:
diff changeset
   572
    // Update the topic messages id.
hgs
parents:
diff changeset
   573
    iTopics->At( index ).iTopicMessagesId = aNewId;
hgs
parents:
diff changeset
   574
    __TEST_INVARIANT;
hgs
parents:
diff changeset
   575
    }
hgs
parents:
diff changeset
   576
hgs
parents:
diff changeset
   577
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
   578
// CCbsDbImpTopicList::GenerateMessageHandle
hgs
parents:
diff changeset
   579
// Generates a new message handle using the topic 
hgs
parents:
diff changeset
   580
// handle of the message and a given random value.
hgs
parents:
diff changeset
   581
// Note that it must be checked that the message handle is unique 
hgs
parents:
diff changeset
   582
// in that topic.
hgs
parents:
diff changeset
   583
// (other items were commented in a header).
hgs
parents:
diff changeset
   584
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
   585
//
hgs
parents:
diff changeset
   586
TCbsDbMessageHandle CCbsDbImpTopicList::GenerateMessageHandle( 
hgs
parents:
diff changeset
   587
    const TCbsDbTopicNumber aNumber, 
hgs
parents:
diff changeset
   588
    TUint16 aRandom ) const
hgs
parents:
diff changeset
   589
    {
hgs
parents:
diff changeset
   590
    return ( aNumber << 16 ) + aRandom;
hgs
parents:
diff changeset
   591
    }
hgs
parents:
diff changeset
   592
hgs
parents:
diff changeset
   593
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
   594
// CCbsDbImpTopicList::ReloadRootStreamL
hgs
parents:
diff changeset
   595
// Reloads the root stream to the memory.
hgs
parents:
diff changeset
   596
// (other items were commented in a header).
hgs
parents:
diff changeset
   597
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
   598
//
hgs
parents:
diff changeset
   599
void CCbsDbImpTopicList::ReloadRootStreamL()
hgs
parents:
diff changeset
   600
    {
hgs
parents:
diff changeset
   601
    LoadRootStreamL();
hgs
parents:
diff changeset
   602
    __TEST_INVARIANT;
hgs
parents:
diff changeset
   603
    }
hgs
parents:
diff changeset
   604
hgs
parents:
diff changeset
   605
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
   606
// CCbsDbImpTopicList::InformNewMessageReceivedL
hgs
parents:
diff changeset
   607
// Informs that a new message has been received in a topic.
hgs
parents:
diff changeset
   608
// This method is called by CCbsDbImpTopicMessages. After internal 
hgs
parents:
diff changeset
   609
// records are changed, the observers are informed of this event.
hgs
parents:
diff changeset
   610
// Note: leaves changes in stores uncommited.
hgs
parents:
diff changeset
   611
// (other items were commented in a header).
hgs
parents:
diff changeset
   612
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
   613
//
hgs
parents:
diff changeset
   614
void CCbsDbImpTopicList::InformNewMessageReceivedL( 
hgs
parents:
diff changeset
   615
    const TCbsDbMessageHandle& aMessageHandle )
hgs
parents:
diff changeset
   616
    {
hgs
parents:
diff changeset
   617
    __TEST_INVARIANT;
hgs
parents:
diff changeset
   618
    // Find topic by handle.
hgs
parents:
diff changeset
   619
    TCbsDbTopicNumber number( ExtractTopicNumber( aMessageHandle ) );
hgs
parents:
diff changeset
   620
hgs
parents:
diff changeset
   621
    TCbsDbTopic topic;
hgs
parents:
diff changeset
   622
    FindTopicByNumberL( number, topic );
hgs
parents:
diff changeset
   623
hgs
parents:
diff changeset
   624
    topic.iUnreadMessages++;
hgs
parents:
diff changeset
   625
hgs
parents:
diff changeset
   626
    // Increase the counter in cache
hgs
parents:
diff changeset
   627
    TInt position = TopicIndexInList( topic.iNumber );
hgs
parents:
diff changeset
   628
    iTopics->At( position ).iTopicData.iUnreadMessages++;
hgs
parents:
diff changeset
   629
hgs
parents:
diff changeset
   630
    // Write topic information to topic stream but leave changes uncommited.
hgs
parents:
diff changeset
   631
    UpdateTopicCountersL( topic, EFalse );    
hgs
parents:
diff changeset
   632
hgs
parents:
diff changeset
   633
    if ( topic.iHotmarked )
hgs
parents:
diff changeset
   634
        {
hgs
parents:
diff changeset
   635
        SetHotmarkedMessage( aMessageHandle );
hgs
parents:
diff changeset
   636
        }
hgs
parents:
diff changeset
   637
hgs
parents:
diff changeset
   638
    __TEST_INVARIANT;
hgs
parents:
diff changeset
   639
    }
hgs
parents:
diff changeset
   640
hgs
parents:
diff changeset
   641
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
   642
// CCbsDbImpTopicList::InformMessageSavedL
hgs
parents:
diff changeset
   643
// Informs that a message has been set as saved.
hgs
parents:
diff changeset
   644
// Updates the saved messages counters
hgs
parents:
diff changeset
   645
// for the topic of the message and the whole system.
hgs
parents:
diff changeset
   646
// Called by CCbsDbTopicMessages.
hgs
parents:
diff changeset
   647
// (other items were commented in a header).
hgs
parents:
diff changeset
   648
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
   649
//
hgs
parents:
diff changeset
   650
void CCbsDbImpTopicList::InformMessageSavedL( 
hgs
parents:
diff changeset
   651
    const TCbsDbMessageHandle& aMessageHandle )
hgs
parents:
diff changeset
   652
    {
hgs
parents:
diff changeset
   653
    __TEST_INVARIANT;
hgs
parents:
diff changeset
   654
    // Find topic by handle.
hgs
parents:
diff changeset
   655
    TCbsDbTopicNumber number( ExtractTopicNumber( aMessageHandle ) );
hgs
parents:
diff changeset
   656
hgs
parents:
diff changeset
   657
    TCbsDbTopic topic;
hgs
parents:
diff changeset
   658
    FindTopicByNumberL( number, topic );
hgs
parents:
diff changeset
   659
hgs
parents:
diff changeset
   660
    topic.iSavedMessages++;
hgs
parents:
diff changeset
   661
hgs
parents:
diff changeset
   662
    // Increase the counter in cache
hgs
parents:
diff changeset
   663
    TInt position = TopicIndexInList( topic.iNumber );
hgs
parents:
diff changeset
   664
    iTopics->At( position ).iTopicData.iSavedMessages++;
hgs
parents:
diff changeset
   665
hgs
parents:
diff changeset
   666
    // Write topic information to topic stream but leave changes uncommited.
hgs
parents:
diff changeset
   667
    UpdateTopicCountersL( topic, EFalse );
hgs
parents:
diff changeset
   668
hgs
parents:
diff changeset
   669
    __TEST_INVARIANT;
hgs
parents:
diff changeset
   670
    }
hgs
parents:
diff changeset
   671
hgs
parents:
diff changeset
   672
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
   673
// CCbsDbImpTopicList::InformUnreadMessageReadL
hgs
parents:
diff changeset
   674
// Informs that an unread message has been read.
hgs
parents:
diff changeset
   675
// Updates the counters when a message is read by the client. 
hgs
parents:
diff changeset
   676
// Note: leaves changes in stores uncommited.
hgs
parents:
diff changeset
   677
// (other items were commented in a header).
hgs
parents:
diff changeset
   678
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
   679
//
hgs
parents:
diff changeset
   680
void CCbsDbImpTopicList::InformUnreadMessageReadL( 
hgs
parents:
diff changeset
   681
    const TCbsDbMessageHandle& aMessageHandle )
hgs
parents:
diff changeset
   682
    {
hgs
parents:
diff changeset
   683
    CBSLOGSTRING("CBSSERVER: >>> CCbsDbImpTopicList::InformUnreadMessageReadL()");
hgs
parents:
diff changeset
   684
    
hgs
parents:
diff changeset
   685
    __TEST_INVARIANT;
hgs
parents:
diff changeset
   686
hgs
parents:
diff changeset
   687
    // Check for file lock
hgs
parents:
diff changeset
   688
    CheckFileLockL();
hgs
parents:
diff changeset
   689
hgs
parents:
diff changeset
   690
    // Check disk space
hgs
parents:
diff changeset
   691
    TRAPD( error, CbsUtils::FFSCriticalLevelCheckL( KReadMessageSize, iFs ) );
hgs
parents:
diff changeset
   692
    CBSLOGSTRING2("CBSSERVER: CCbsDbImpTopicList::InformUnreadMessageReadL(): FFSCriticalLevelCheckL returned: %d", error );
hgs
parents:
diff changeset
   693
    if ( error == KErrDiskFull )
hgs
parents:
diff changeset
   694
        {
hgs
parents:
diff changeset
   695
        return;
hgs
parents:
diff changeset
   696
        }
hgs
parents:
diff changeset
   697
hgs
parents:
diff changeset
   698
    // Find topic by number.
hgs
parents:
diff changeset
   699
    TCbsDbTopicNumber number( ExtractTopicNumber( aMessageHandle ) );
hgs
parents:
diff changeset
   700
hgs
parents:
diff changeset
   701
    TCbsDbTopic topic;
hgs
parents:
diff changeset
   702
    FindTopicByNumberL( number, topic );
hgs
parents:
diff changeset
   703
hgs
parents:
diff changeset
   704
    // Decrease the counter
hgs
parents:
diff changeset
   705
    topic.iUnreadMessages--;
hgs
parents:
diff changeset
   706
hgs
parents:
diff changeset
   707
    // Decrease the counter in cache
hgs
parents:
diff changeset
   708
    TInt position = TopicIndexInList( topic.iNumber );
hgs
parents:
diff changeset
   709
    iTopics->At( position ).iTopicData.iUnreadMessages--;
hgs
parents:
diff changeset
   710
hgs
parents:
diff changeset
   711
    // Write topic information to topic stream but leave changes uncommited.
hgs
parents:
diff changeset
   712
    UpdateTopicCountersL( topic, EFalse );
hgs
parents:
diff changeset
   713
hgs
parents:
diff changeset
   714
    __TEST_INVARIANT;
hgs
parents:
diff changeset
   715
    
hgs
parents:
diff changeset
   716
   CBSLOGSTRING("CBSSERVER: <<< CCbsDbImpTopicList::InformUnreadMessageReadL()");
hgs
parents:
diff changeset
   717
    }
hgs
parents:
diff changeset
   718
hgs
parents:
diff changeset
   719
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
   720
// CCbsDbImpTopicList::InformMessageDeletedL
hgs
parents:
diff changeset
   721
// Informs that an unread message has been deleted.
hgs
parents:
diff changeset
   722
// Updates the counters when a message is deleted. 
hgs
parents:
diff changeset
   723
// Note: leaves changes in stores uncommited.
hgs
parents:
diff changeset
   724
// (other items were commented in a header).
hgs
parents:
diff changeset
   725
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
   726
//
hgs
parents:
diff changeset
   727
void CCbsDbImpTopicList::InformMessageDeletedL( 
hgs
parents:
diff changeset
   728
    const TCbsDbMessageHandle& aMessageHandle,
hgs
parents:
diff changeset
   729
    TBool aPermanent, 
hgs
parents:
diff changeset
   730
    TBool aRead )
hgs
parents:
diff changeset
   731
    {
hgs
parents:
diff changeset
   732
    // Find topic by handle.
hgs
parents:
diff changeset
   733
    TCbsDbTopicNumber number( ExtractTopicNumber( aMessageHandle ) );
hgs
parents:
diff changeset
   734
hgs
parents:
diff changeset
   735
    TCbsDbTopic topic;
hgs
parents:
diff changeset
   736
    FindTopicByNumberL( number, topic );
hgs
parents:
diff changeset
   737
hgs
parents:
diff changeset
   738
    TInt position = TopicIndexInList( topic.iNumber );    
hgs
parents:
diff changeset
   739
hgs
parents:
diff changeset
   740
    if ( aPermanent )
hgs
parents:
diff changeset
   741
        {
hgs
parents:
diff changeset
   742
        topic.iSavedMessages--;
hgs
parents:
diff changeset
   743
        iTopics->At( position ).iTopicData.iSavedMessages--;
hgs
parents:
diff changeset
   744
        }
hgs
parents:
diff changeset
   745
    
hgs
parents:
diff changeset
   746
    if ( !aRead )
hgs
parents:
diff changeset
   747
        {        
hgs
parents:
diff changeset
   748
        topic.iUnreadMessages--;
hgs
parents:
diff changeset
   749
        iTopics->At( position ).iTopicData.iUnreadMessages--;
hgs
parents:
diff changeset
   750
        }
hgs
parents:
diff changeset
   751
hgs
parents:
diff changeset
   752
    // Write topic information to topic stream but leave changes uncommited.
hgs
parents:
diff changeset
   753
    UpdateTopicCountersL( topic, ETrue );
hgs
parents:
diff changeset
   754
    }
hgs
parents:
diff changeset
   755
hgs
parents:
diff changeset
   756
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
   757
// CCbsDbImpTopicList::TotalSavedMessages
hgs
parents:
diff changeset
   758
// Returns the number of saved messages in the current topic list.
hgs
parents:
diff changeset
   759
// (other items were commented in a header).
hgs
parents:
diff changeset
   760
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
   761
//
hgs
parents:
diff changeset
   762
TInt CCbsDbImpTopicList::TotalSavedMessages() const
hgs
parents:
diff changeset
   763
    {
hgs
parents:
diff changeset
   764
    // Return the total amount of saved messages.
hgs
parents:
diff changeset
   765
    TInt count( 0 );
hgs
parents:
diff changeset
   766
    for ( TInt i( 0 ); i < iTopics->Count(); i++ )
hgs
parents:
diff changeset
   767
        {
hgs
parents:
diff changeset
   768
        count += iTopics->At( i ).iTopicData.iSavedMessages;
hgs
parents:
diff changeset
   769
        }
hgs
parents:
diff changeset
   770
hgs
parents:
diff changeset
   771
    return count;
hgs
parents:
diff changeset
   772
    }
hgs
parents:
diff changeset
   773
hgs
parents:
diff changeset
   774
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
   775
// CCbsDbImpTopicList::GetTopicCount
hgs
parents:
diff changeset
   776
// Returns the number of topic stored in this topic list.
hgs
parents:
diff changeset
   777
// (other items were commented in a header).
hgs
parents:
diff changeset
   778
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
   779
//
hgs
parents:
diff changeset
   780
void CCbsDbImpTopicList::GetTopicCount( 
hgs
parents:
diff changeset
   781
    TInt& aCount ) const
hgs
parents:
diff changeset
   782
    {
hgs
parents:
diff changeset
   783
    // Return the total amount of topics.
hgs
parents:
diff changeset
   784
    aCount = TopicCount();
hgs
parents:
diff changeset
   785
    }
hgs
parents:
diff changeset
   786
hgs
parents:
diff changeset
   787
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
   788
// CCbsDbImpTopicList::InitializeListL
hgs
parents:
diff changeset
   789
// Initializes the whole topic list. 
hgs
parents:
diff changeset
   790
// Creates and opens the topic list file, 
hgs
parents:
diff changeset
   791
// invalidates the cache and informs the observers.
hgs
parents:
diff changeset
   792
// (other items were commented in a header).
hgs
parents:
diff changeset
   793
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
   794
//
hgs
parents:
diff changeset
   795
void CCbsDbImpTopicList::InitializeListL( const TBool aFileOpenFailed )
hgs
parents:
diff changeset
   796
    {
hgs
parents:
diff changeset
   797
    __TEST_INVARIANT;
hgs
parents:
diff changeset
   798
hgs
parents:
diff changeset
   799
    if ( !aFileOpenFailed )
hgs
parents:
diff changeset
   800
        {
hgs
parents:
diff changeset
   801
        // Check for file lock
hgs
parents:
diff changeset
   802
        CheckFileLockL();
hgs
parents:
diff changeset
   803
        }    
hgs
parents:
diff changeset
   804
hgs
parents:
diff changeset
   805
    // About to write to FFS: make critical level check
hgs
parents:
diff changeset
   806
    CbsUtils::FFSCriticalLevelCheckL( 0, iFs );
hgs
parents:
diff changeset
   807
hgs
parents:
diff changeset
   808
    if ( iMessages != NULL && iMessages->IsLockedMessages() )
hgs
parents:
diff changeset
   809
        {
hgs
parents:
diff changeset
   810
        User::Leave( KErrAccessDenied );
hgs
parents:
diff changeset
   811
        }    
hgs
parents:
diff changeset
   812
hgs
parents:
diff changeset
   813
    // If only one topic list exists, just delete and recreate the whole file
hgs
parents:
diff changeset
   814
    if ( iTopicLists->Count() == 1 || aFileOpenFailed == 1 )
hgs
parents:
diff changeset
   815
        {
hgs
parents:
diff changeset
   816
        delete iTopicStore; 
hgs
parents:
diff changeset
   817
        iTopicStore = NULL;
hgs
parents:
diff changeset
   818
        delete iUnsavedMessageStore; 
hgs
parents:
diff changeset
   819
        iUnsavedMessageStore = NULL;
hgs
parents:
diff changeset
   820
        CbsUtils::DeleteFileL( iFs, *iTopicsFilename  );
hgs
parents:
diff changeset
   821
        CbsUtils::DeleteFileL( iFs, *iUnsavedMessagesFilename );
hgs
parents:
diff changeset
   822
hgs
parents:
diff changeset
   823
        iTopicLists->Reset();
hgs
parents:
diff changeset
   824
       
hgs
parents:
diff changeset
   825
        // Create new files.
hgs
parents:
diff changeset
   826
        OpenFilesL( EFalse, ETrue );
hgs
parents:
diff changeset
   827
        }
hgs
parents:
diff changeset
   828
    
hgs
parents:
diff changeset
   829
    iIsHotmarkedMessage = EFalse;
hgs
parents:
diff changeset
   830
    iLastTopicNumber = 0;
hgs
parents:
diff changeset
   831
    iMessageHandle = 0;
hgs
parents:
diff changeset
   832
hgs
parents:
diff changeset
   833
    // Inform the message manager.
hgs
parents:
diff changeset
   834
    if ( iMessages )
hgs
parents:
diff changeset
   835
        {
hgs
parents:
diff changeset
   836
        iMessages->InvalidateCache();
hgs
parents:
diff changeset
   837
        }
hgs
parents:
diff changeset
   838
hgs
parents:
diff changeset
   839
    // Notify everyone.
hgs
parents:
diff changeset
   840
    NotifyTopicListInitializedL();
hgs
parents:
diff changeset
   841
hgs
parents:
diff changeset
   842
    __TEST_INVARIANT;
hgs
parents:
diff changeset
   843
    }
hgs
parents:
diff changeset
   844
hgs
parents:
diff changeset
   845
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
   846
// CCbsDbImpTopicList::GetTopicL
hgs
parents:
diff changeset
   847
// Returns a topic matching the given index.
hgs
parents:
diff changeset
   848
// (other items were commented in a header).
hgs
parents:
diff changeset
   849
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
   850
//
hgs
parents:
diff changeset
   851
void CCbsDbImpTopicList::GetTopicL( 
hgs
parents:
diff changeset
   852
    TInt aIndex, 
hgs
parents:
diff changeset
   853
    TCbsDbTopic& aTopic )
hgs
parents:
diff changeset
   854
    {
hgs
parents:
diff changeset
   855
    CBSLOGSTRING("CBSSERVER: >>> CCbsDbImpTopicList::GetTopicL()");
hgs
parents:
diff changeset
   856
hgs
parents:
diff changeset
   857
    __TEST_INVARIANT;
hgs
parents:
diff changeset
   858
    
hgs
parents:
diff changeset
   859
    // Check that aIndex is in proper range.
hgs
parents:
diff changeset
   860
    if ( ( aIndex < 0 ) || ( aIndex >= iTopics->Count() ) )
hgs
parents:
diff changeset
   861
        {
hgs
parents:
diff changeset
   862
        CBSLOGSTRING("CBSSERVER: CCbsDbImpTopicList::GetTopicL(): Leaving with KErrArgument...");
hgs
parents:
diff changeset
   863
        User::Leave( KErrArgument );
hgs
parents:
diff changeset
   864
        }
hgs
parents:
diff changeset
   865
hgs
parents:
diff changeset
   866
    // And then get the information from the array.
hgs
parents:
diff changeset
   867
    aTopic = iTopics->At( aIndex ).iTopicData;
hgs
parents:
diff changeset
   868
hgs
parents:
diff changeset
   869
    CBSLOGSTRING("CBSSERVER: <<< CCbsDbImpTopicList::GetTopicL()");
hgs
parents:
diff changeset
   870
    
hgs
parents:
diff changeset
   871
    __TEST_INVARIANT;
hgs
parents:
diff changeset
   872
    }
hgs
parents:
diff changeset
   873
hgs
parents:
diff changeset
   874
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
   875
// CCbsDbImpTopicList::FindTopicByNumberL
hgs
parents:
diff changeset
   876
// Returns a topic matching the given topic number 
hgs
parents:
diff changeset
   877
// (in GSM Specs this is called the Message Identifier)
hgs
parents:
diff changeset
   878
// (other items were commented in a header).
hgs
parents:
diff changeset
   879
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
   880
//
hgs
parents:
diff changeset
   881
void CCbsDbImpTopicList::FindTopicByNumberL( 
hgs
parents:
diff changeset
   882
    TCbsDbTopicNumber aNumber, 
hgs
parents:
diff changeset
   883
    TCbsDbTopic& aTopic )
hgs
parents:
diff changeset
   884
    {
hgs
parents:
diff changeset
   885
    CBSLOGSTRING("CBSSERVER: >>> CCbsDbImpTopicList::FindTopicByNumberL()");
hgs
parents:
diff changeset
   886
    
hgs
parents:
diff changeset
   887
    __TEST_INVARIANT;
hgs
parents:
diff changeset
   888
hgs
parents:
diff changeset
   889
    TInt topicIndex( TopicIndexInList( aNumber ) );
hgs
parents:
diff changeset
   890
    CBSLOGSTRING2("CBSSERVER: CCbsDbImpTopicList::FindTopicByNumberL(): Leaving if topicIndex < 0: topicIndex: %d.", topicIndex );
hgs
parents:
diff changeset
   891
    User::LeaveIfError( topicIndex );
hgs
parents:
diff changeset
   892
hgs
parents:
diff changeset
   893
    GetTopicL( topicIndex, aTopic );
hgs
parents:
diff changeset
   894
hgs
parents:
diff changeset
   895
    CBSLOGSTRING("CBSSERVER: <<< CCbsDbImpTopicList::FindTopicByNumberL()");
hgs
parents:
diff changeset
   896
    
hgs
parents:
diff changeset
   897
    __TEST_INVARIANT;
hgs
parents:
diff changeset
   898
    }
hgs
parents:
diff changeset
   899
hgs
parents:
diff changeset
   900
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
   901
// CCbsDbImpTopicList::AddTopicL
hgs
parents:
diff changeset
   902
// Adds a new topic to the list.
hgs
parents:
diff changeset
   903
// (other items were commented in a header).
hgs
parents:
diff changeset
   904
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
   905
//
hgs
parents:
diff changeset
   906
void CCbsDbImpTopicList::AddTopicL( 
hgs
parents:
diff changeset
   907
    TCbsDbTopic& aTopic, const TBool aDetected )
hgs
parents:
diff changeset
   908
    {
hgs
parents:
diff changeset
   909
    __TEST_INVARIANT;
hgs
parents:
diff changeset
   910
hgs
parents:
diff changeset
   911
    // Check for file lock
hgs
parents:
diff changeset
   912
    CheckFileLockL();
hgs
parents:
diff changeset
   913
    
hgs
parents:
diff changeset
   914
    // Check that topic number is in proper range
hgs
parents:
diff changeset
   915
    if ( !CheckTopicNumber( aTopic.iNumber ) )
hgs
parents:
diff changeset
   916
        {
hgs
parents:
diff changeset
   917
        User::Leave( KErrArgument );
hgs
parents:
diff changeset
   918
        }    
hgs
parents:
diff changeset
   919
hgs
parents:
diff changeset
   920
    // Check if there is a topic with the 
hgs
parents:
diff changeset
   921
    // same topic number in current topic list.
hgs
parents:
diff changeset
   922
    if ( TopicIndexInList( aTopic.iNumber ) != KErrNotFound )
hgs
parents:
diff changeset
   923
        {
hgs
parents:
diff changeset
   924
        User::Leave( KErrAlreadyExists );
hgs
parents:
diff changeset
   925
        } 
hgs
parents:
diff changeset
   926
hgs
parents:
diff changeset
   927
    // There should not be any saved or unread messages
hgs
parents:
diff changeset
   928
    aTopic.iSavedMessages = 0;
hgs
parents:
diff changeset
   929
    aTopic.iUnreadMessages = 0;
hgs
parents:
diff changeset
   930
hgs
parents:
diff changeset
   931
    // Variated feature. Also, only topics that were not detected automatically
hgs
parents:
diff changeset
   932
    // are subscribed by default.
hgs
parents:
diff changeset
   933
    if ( iLVBits & KCbsLVFlagTopicSubscription && !aDetected )
hgs
parents:
diff changeset
   934
        {
hgs
parents:
diff changeset
   935
        aTopic.iSubscribed = ETrue;
hgs
parents:
diff changeset
   936
        }
hgs
parents:
diff changeset
   937
    else
hgs
parents:
diff changeset
   938
        {
hgs
parents:
diff changeset
   939
        aTopic.iSubscribed = EFalse;        
hgs
parents:
diff changeset
   940
        }
hgs
parents:
diff changeset
   941
        
hgs
parents:
diff changeset
   942
    // Now we have the handle, so let's add the topic.
hgs
parents:
diff changeset
   943
    TRAPD( error, DoAddTopicL( aTopic ) );
hgs
parents:
diff changeset
   944
    if ( error != KErrNone )
hgs
parents:
diff changeset
   945
        {
hgs
parents:
diff changeset
   946
        RevertFilesL();
hgs
parents:
diff changeset
   947
        __TEST_INVARIANT;
hgs
parents:
diff changeset
   948
        User::Leave( error );
hgs
parents:
diff changeset
   949
        }
hgs
parents:
diff changeset
   950
    else
hgs
parents:
diff changeset
   951
        {        
hgs
parents:
diff changeset
   952
        iLastTopicNumber = aTopic.iNumber;
hgs
parents:
diff changeset
   953
        NotifyTopicAddedL( aTopic.iNumber );        
hgs
parents:
diff changeset
   954
        }        
hgs
parents:
diff changeset
   955
    
hgs
parents:
diff changeset
   956
    __TEST_INVARIANT;
hgs
parents:
diff changeset
   957
    }
hgs
parents:
diff changeset
   958
hgs
parents:
diff changeset
   959
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
   960
// CCbsDbImpTopicList::UpdateTopicNameAndNumberL
hgs
parents:
diff changeset
   961
// Updates the name and the topic number of a topic 
hgs
parents:
diff changeset
   962
// matching the given handle to the database.
hgs
parents:
diff changeset
   963
// (other items were commented in a header).
hgs
parents:
diff changeset
   964
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
   965
//
hgs
parents:
diff changeset
   966
void CCbsDbImpTopicList::UpdateTopicNameAndNumberL( 
hgs
parents:
diff changeset
   967
    TCbsDbTopicNumber aOldNumber,
hgs
parents:
diff changeset
   968
    TCbsDbTopicNumber aNewNumber, 
hgs
parents:
diff changeset
   969
    const TCbsDbTopicName& aName )
hgs
parents:
diff changeset
   970
    {
hgs
parents:
diff changeset
   971
    // Check for file lock
hgs
parents:
diff changeset
   972
    CheckFileLockL();
hgs
parents:
diff changeset
   973
hgs
parents:
diff changeset
   974
    // First, check that the new number is ok.
hgs
parents:
diff changeset
   975
    if ( !CheckTopicNumber( aNewNumber ) )
hgs
parents:
diff changeset
   976
        {
hgs
parents:
diff changeset
   977
        User::Leave( KErrArgument );
hgs
parents:
diff changeset
   978
        }
hgs
parents:
diff changeset
   979
hgs
parents:
diff changeset
   980
    // First find and then update.
hgs
parents:
diff changeset
   981
    TCbsDbTopic topic;
hgs
parents:
diff changeset
   982
    FindTopicByNumberL( aOldNumber, topic );
hgs
parents:
diff changeset
   983
hgs
parents:
diff changeset
   984
    // If no changes to topic, no need to update
hgs
parents:
diff changeset
   985
    if ( !( aOldNumber == aNewNumber && topic.iName == aName ) )
hgs
parents:
diff changeset
   986
        {        
hgs
parents:
diff changeset
   987
        if ( topic.iProtected )
hgs
parents:
diff changeset
   988
            {
hgs
parents:
diff changeset
   989
            User::Leave( KErrAccessDenied );
hgs
parents:
diff changeset
   990
            }
hgs
parents:
diff changeset
   991
hgs
parents:
diff changeset
   992
        topic.iName = aName;
hgs
parents:
diff changeset
   993
        topic.iNumber = aNewNumber;
hgs
parents:
diff changeset
   994
hgs
parents:
diff changeset
   995
        UpdateTopicL( aOldNumber, topic );        
hgs
parents:
diff changeset
   996
        }    
hgs
parents:
diff changeset
   997
    }
hgs
parents:
diff changeset
   998
hgs
parents:
diff changeset
   999
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  1000
// CCbsDbImpTopicList::UpdateTopicSubscriptionStatusL
hgs
parents:
diff changeset
  1001
// Updates the new topic subscription status to the database.
hgs
parents:
diff changeset
  1002
// (other items were commented in a header).
hgs
parents:
diff changeset
  1003
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  1004
//
hgs
parents:
diff changeset
  1005
void CCbsDbImpTopicList::UpdateTopicSubscriptionStatusL( 
hgs
parents:
diff changeset
  1006
    TCbsDbTopicNumber aNumber, 
hgs
parents:
diff changeset
  1007
    TBool aStatus )
hgs
parents:
diff changeset
  1008
    {
hgs
parents:
diff changeset
  1009
    __TEST_INVARIANT;
hgs
parents:
diff changeset
  1010
hgs
parents:
diff changeset
  1011
    // Update topic subsciption status.
hgs
parents:
diff changeset
  1012
    TCbsDbTopic topic;
hgs
parents:
diff changeset
  1013
    FindTopicByNumberL( aNumber, topic );
hgs
parents:
diff changeset
  1014
hgs
parents:
diff changeset
  1015
    topic.iSubscribed = aStatus;
hgs
parents:
diff changeset
  1016
    UpdateTopicL( aNumber, topic );
hgs
parents:
diff changeset
  1017
    __TEST_INVARIANT;
hgs
parents:
diff changeset
  1018
hgs
parents:
diff changeset
  1019
    }
hgs
parents:
diff changeset
  1020
hgs
parents:
diff changeset
  1021
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  1022
// CCbsDbImpTopicList::UpdateTopicHotmarkStatusL
hgs
parents:
diff changeset
  1023
// Updates the new topic hotmarking status to the database.
hgs
parents:
diff changeset
  1024
// (other items were commented in a header).
hgs
parents:
diff changeset
  1025
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  1026
//
hgs
parents:
diff changeset
  1027
void CCbsDbImpTopicList::UpdateTopicHotmarkStatusL( 
hgs
parents:
diff changeset
  1028
    TCbsDbTopicNumber aNumber, 
hgs
parents:
diff changeset
  1029
    TBool aStatus )
hgs
parents:
diff changeset
  1030
    {
hgs
parents:
diff changeset
  1031
    __TEST_INVARIANT;
hgs
parents:
diff changeset
  1032
hgs
parents:
diff changeset
  1033
    // Check for file lock
hgs
parents:
diff changeset
  1034
    CheckFileLockL();
hgs
parents:
diff changeset
  1035
hgs
parents:
diff changeset
  1036
    // Update topic hotmark status.
hgs
parents:
diff changeset
  1037
    TCbsDbTopic topic;
hgs
parents:
diff changeset
  1038
    FindTopicByNumberL( aNumber, topic );
hgs
parents:
diff changeset
  1039
hgs
parents:
diff changeset
  1040
    topic.iHotmarked = aStatus;
hgs
parents:
diff changeset
  1041
    UpdateTopicL( aNumber, topic );
hgs
parents:
diff changeset
  1042
    __TEST_INVARIANT;
hgs
parents:
diff changeset
  1043
    }
hgs
parents:
diff changeset
  1044
hgs
parents:
diff changeset
  1045
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  1046
// CCbsDbImpTopicList::DeleteTopicL
hgs
parents:
diff changeset
  1047
// Deletes an existing topic and all its messages.
hgs
parents:
diff changeset
  1048
// (other items were commented in a header).
hgs
parents:
diff changeset
  1049
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  1050
//
hgs
parents:
diff changeset
  1051
void CCbsDbImpTopicList::DeleteTopicL( 
hgs
parents:
diff changeset
  1052
    TCbsDbTopicNumber aNumber )
hgs
parents:
diff changeset
  1053
    {
hgs
parents:
diff changeset
  1054
    __TEST_INVARIANT;
hgs
parents:
diff changeset
  1055
hgs
parents:
diff changeset
  1056
    // Try to find the position. If it is not found, leave.
hgs
parents:
diff changeset
  1057
    TInt position( TopicIndexInList( aNumber ) );
hgs
parents:
diff changeset
  1058
    User::LeaveIfError( position );
hgs
parents:
diff changeset
  1059
hgs
parents:
diff changeset
  1060
    // Check that there are no locked messages in the topic.
hgs
parents:
diff changeset
  1061
    if ( iMessages->IsLockedMessagesInTopic( aNumber ) )
hgs
parents:
diff changeset
  1062
        {
hgs
parents:
diff changeset
  1063
        User::Leave( KErrAccessDenied );
hgs
parents:
diff changeset
  1064
        }
hgs
parents:
diff changeset
  1065
hgs
parents:
diff changeset
  1066
    // Topic only in one topic list, not an index topic, so delete the topic.
hgs
parents:
diff changeset
  1067
    if ( aNumber != 0 )
hgs
parents:
diff changeset
  1068
        {
hgs
parents:
diff changeset
  1069
        // Just try to delete
hgs
parents:
diff changeset
  1070
        TRAPD( error, DoDeleteTopicL( position ) );
hgs
parents:
diff changeset
  1071
        if ( error != KErrNone )
hgs
parents:
diff changeset
  1072
            {
hgs
parents:
diff changeset
  1073
            // It failed, so we must revert.
hgs
parents:
diff changeset
  1074
            RevertFilesL();
hgs
parents:
diff changeset
  1075
hgs
parents:
diff changeset
  1076
            // Inform the topic messages.
hgs
parents:
diff changeset
  1077
            iMessages->InvalidateCacheIfTopic( aNumber );
hgs
parents:
diff changeset
  1078
hgs
parents:
diff changeset
  1079
            __TEST_INVARIANT;
hgs
parents:
diff changeset
  1080
            User::Leave( error );
hgs
parents:
diff changeset
  1081
            }
hgs
parents:
diff changeset
  1082
        else
hgs
parents:
diff changeset
  1083
            {               
hgs
parents:
diff changeset
  1084
            // Notify the observers
hgs
parents:
diff changeset
  1085
            NotifyTopicDeletedL( aNumber );
hgs
parents:
diff changeset
  1086
            }
hgs
parents:
diff changeset
  1087
        }
hgs
parents:
diff changeset
  1088
    __TEST_INVARIANT;
hgs
parents:
diff changeset
  1089
    }
hgs
parents:
diff changeset
  1090
hgs
parents:
diff changeset
  1091
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  1092
// CCbsDbImpTopicList::GetUnreadMessageCount
hgs
parents:
diff changeset
  1093
// Gets the total amount of unread messages.
hgs
parents:
diff changeset
  1094
// (other items were commented in a header).
hgs
parents:
diff changeset
  1095
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  1096
//
hgs
parents:
diff changeset
  1097
void CCbsDbImpTopicList::GetUnreadMessageCount( 
hgs
parents:
diff changeset
  1098
    TInt& aCount ) const
hgs
parents:
diff changeset
  1099
    {
hgs
parents:
diff changeset
  1100
    CBSLOGSTRING("CBSSERVER: >>> CCbsDbImpTopicList::GetUnreadMessageCount()");
hgs
parents:
diff changeset
  1101
    CBSLOGSTRING2("CBSSERVER: CCbsDbImpTopicList::GetUnreadMessageCount(): Topic count: %d", iTopics->Count() );
hgs
parents:
diff changeset
  1102
    
hgs
parents:
diff changeset
  1103
    // Return the total amount of unread messages.
hgs
parents:
diff changeset
  1104
    TInt count( 0 );
hgs
parents:
diff changeset
  1105
    for ( TInt i( 0 ); i < iTopics->Count(); i++ )
hgs
parents:
diff changeset
  1106
        {
hgs
parents:
diff changeset
  1107
        count += iTopics->At( i ).iTopicData.iUnreadMessages;
hgs
parents:
diff changeset
  1108
        }
hgs
parents:
diff changeset
  1109
hgs
parents:
diff changeset
  1110
    aCount = count;
hgs
parents:
diff changeset
  1111
    
hgs
parents:
diff changeset
  1112
    CBSLOGSTRING2("CBSSERVER: CCbsDbImpTopicList::GetUnreadMessageCount(): Unread msgs found: %d", count );
hgs
parents:
diff changeset
  1113
    CBSLOGSTRING("CBSSERVER: <<< CCbsDbImpTopicList::GetUnreadMessageCount()");
hgs
parents:
diff changeset
  1114
    }
hgs
parents:
diff changeset
  1115
hgs
parents:
diff changeset
  1116
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  1117
// CCbsDbImpTopicList::GetHotmarkedMessageHandleL
hgs
parents:
diff changeset
  1118
// Returns the handle to the latest hotmarked message.      
hgs
parents:
diff changeset
  1119
// (other items were commented in a header).
hgs
parents:
diff changeset
  1120
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  1121
//
hgs
parents:
diff changeset
  1122
void CCbsDbImpTopicList::GetHotmarkedMessageHandleL( 
hgs
parents:
diff changeset
  1123
    TCbsDbMessageHandle& aMessage )
hgs
parents:
diff changeset
  1124
    {
hgs
parents:
diff changeset
  1125
    __TEST_INVARIANT;
hgs
parents:
diff changeset
  1126
    
hgs
parents:
diff changeset
  1127
    // Check if there is a hotmarked message.
hgs
parents:
diff changeset
  1128
    if ( iIsHotmarkedMessage )
hgs
parents:
diff changeset
  1129
        {
hgs
parents:
diff changeset
  1130
        // If there is, then return it.
hgs
parents:
diff changeset
  1131
        aMessage = iMessageHandle;
hgs
parents:
diff changeset
  1132
        iIsHotmarkedMessage = EFalse;
hgs
parents:
diff changeset
  1133
        }
hgs
parents:
diff changeset
  1134
    else
hgs
parents:
diff changeset
  1135
        {
hgs
parents:
diff changeset
  1136
        // Otherwise leave.
hgs
parents:
diff changeset
  1137
        __TEST_INVARIANT;
hgs
parents:
diff changeset
  1138
        User::Leave( KErrNotFound );
hgs
parents:
diff changeset
  1139
        }
hgs
parents:
diff changeset
  1140
    __TEST_INVARIANT;
hgs
parents:
diff changeset
  1141
    }
hgs
parents:
diff changeset
  1142
hgs
parents:
diff changeset
  1143
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  1144
// CCbsDbImpTopicList::UnreadHotmarkedMessageCount
hgs
parents:
diff changeset
  1145
// Returns the handle to the latest hotmarked message.      
hgs
parents:
diff changeset
  1146
// (other items were commented in a header).
hgs
parents:
diff changeset
  1147
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  1148
//
hgs
parents:
diff changeset
  1149
TInt CCbsDbImpTopicList::UnreadHotmarkedMessageCount() const
hgs
parents:
diff changeset
  1150
    {
hgs
parents:
diff changeset
  1151
    // Return the total amount of unread messages in hotmarked topics.
hgs
parents:
diff changeset
  1152
    TInt count( 0 );
hgs
parents:
diff changeset
  1153
    for ( TInt i( 0 ); i < iTopics->Count(); i++ )
hgs
parents:
diff changeset
  1154
        {
hgs
parents:
diff changeset
  1155
        TCbsDbTopic& topic = iTopics->At( i ).iTopicData;
hgs
parents:
diff changeset
  1156
        if ( topic.iHotmarked )
hgs
parents:
diff changeset
  1157
            {
hgs
parents:
diff changeset
  1158
            count += topic.iUnreadMessages;
hgs
parents:
diff changeset
  1159
            }
hgs
parents:
diff changeset
  1160
        }
hgs
parents:
diff changeset
  1161
    return count;
hgs
parents:
diff changeset
  1162
    }
hgs
parents:
diff changeset
  1163
hgs
parents:
diff changeset
  1164
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  1165
// CCbsDbImpTopicList::AddObserverL
hgs
parents:
diff changeset
  1166
// Adds a topic list observer. 
hgs
parents:
diff changeset
  1167
// After an observer is added to the topic list, 
hgs
parents:
diff changeset
  1168
// it will be notified whenever an event occurs.      
hgs
parents:
diff changeset
  1169
// (other items were commented in a header).
hgs
parents:
diff changeset
  1170
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  1171
//
hgs
parents:
diff changeset
  1172
void CCbsDbImpTopicList::AddObserverL(
hgs
parents:
diff changeset
  1173
    MCbsDbTopicListObserver* aObserver )
hgs
parents:
diff changeset
  1174
    {
hgs
parents:
diff changeset
  1175
    __ASSERT_DEBUG( aObserver != 0, CbsServerPanic( ECbsObserverNull ) );
hgs
parents:
diff changeset
  1176
    iObservers->AppendL( aObserver );
hgs
parents:
diff changeset
  1177
    }
hgs
parents:
diff changeset
  1178
hgs
parents:
diff changeset
  1179
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  1180
// CCbsDbImpTopicList::RemoveObserver
hgs
parents:
diff changeset
  1181
// Removes a topic list observer. 
hgs
parents:
diff changeset
  1182
// If aObserver is not in the list, the method will panic.  
hgs
parents:
diff changeset
  1183
// (other items were commented in a header).
hgs
parents:
diff changeset
  1184
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  1185
//
hgs
parents:
diff changeset
  1186
void CCbsDbImpTopicList::RemoveObserver( 
hgs
parents:
diff changeset
  1187
    const MCbsDbTopicListObserver* aObserver )
hgs
parents:
diff changeset
  1188
    {
hgs
parents:
diff changeset
  1189
    __TEST_INVARIANT;
hgs
parents:
diff changeset
  1190
    __ASSERT_DEBUG( aObserver != 0, CbsServerPanic( ECbsObserverNull ) );
hgs
parents:
diff changeset
  1191
    
hgs
parents:
diff changeset
  1192
    TBool observerFound( EFalse );
hgs
parents:
diff changeset
  1193
    TInt amountOfObservers( iObservers->Count() );
hgs
parents:
diff changeset
  1194
hgs
parents:
diff changeset
  1195
    // Check if the observer exists and 
hgs
parents:
diff changeset
  1196
    // find its possible location index in the array.
hgs
parents:
diff changeset
  1197
    for ( TInt index( 0 ); ( index < amountOfObservers ) && !observerFound; index++ )
hgs
parents:
diff changeset
  1198
        {
hgs
parents:
diff changeset
  1199
        if ( aObserver == iObservers->At( index ) )
hgs
parents:
diff changeset
  1200
            {
hgs
parents:
diff changeset
  1201
            iObservers->Delete( index );
hgs
parents:
diff changeset
  1202
            observerFound = ETrue;
hgs
parents:
diff changeset
  1203
            }
hgs
parents:
diff changeset
  1204
        }
hgs
parents:
diff changeset
  1205
hgs
parents:
diff changeset
  1206
    __TEST_INVARIANT;
hgs
parents:
diff changeset
  1207
hgs
parents:
diff changeset
  1208
#ifdef _DEBUG
hgs
parents:
diff changeset
  1209
    if ( !observerFound )
hgs
parents:
diff changeset
  1210
        {
hgs
parents:
diff changeset
  1211
        CbsServerPanic( ECbsObserverNotFound );
hgs
parents:
diff changeset
  1212
        }
hgs
parents:
diff changeset
  1213
#endif
hgs
parents:
diff changeset
  1214
hgs
parents:
diff changeset
  1215
    }
hgs
parents:
diff changeset
  1216
hgs
parents:
diff changeset
  1217
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  1218
// CCbsDbImpTopicList::TopicIndexInList
hgs
parents:
diff changeset
  1219
// Finds the index of the topic matching the given topic number
hgs
parents:
diff changeset
  1220
// in the topic list.
hgs
parents:
diff changeset
  1221
// (other items were commented in a header).
hgs
parents:
diff changeset
  1222
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  1223
//
hgs
parents:
diff changeset
  1224
TInt CCbsDbImpTopicList::TopicIndexInList( 
hgs
parents:
diff changeset
  1225
    TCbsDbTopicNumber aNumber ) const
hgs
parents:
diff changeset
  1226
    {
hgs
parents:
diff changeset
  1227
    // Create a TCbsDbImpTopic to compare against and use a binary search.
hgs
parents:
diff changeset
  1228
    TKeyArrayFix key( _FOFF( TCbsDbImpTopic, iTopicData.iNumber ), ECmpTUint16 );
hgs
parents:
diff changeset
  1229
    TCbsDbImpTopic dummy;
hgs
parents:
diff changeset
  1230
    TInt position;
hgs
parents:
diff changeset
  1231
hgs
parents:
diff changeset
  1232
    dummy.iTopicData.iNumber = aNumber;
hgs
parents:
diff changeset
  1233
    TInt result( iTopics->FindIsq( dummy, key, position ) );
hgs
parents:
diff changeset
  1234
    
hgs
parents:
diff changeset
  1235
    if ( result != KErrNone )
hgs
parents:
diff changeset
  1236
        {
hgs
parents:
diff changeset
  1237
        position = KErrNotFound;
hgs
parents:
diff changeset
  1238
        }
hgs
parents:
diff changeset
  1239
hgs
parents:
diff changeset
  1240
    return position;
hgs
parents:
diff changeset
  1241
    }
hgs
parents:
diff changeset
  1242
hgs
parents:
diff changeset
  1243
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  1244
// CCbsDbImpTopicList::GetNextAndPrevTopicNumberL
hgs
parents:
diff changeset
  1245
// Retrieves numbers of topics that precede and succeed the topic
hgs
parents:
diff changeset
  1246
// with number aCurrentTopic.
hgs
parents:
diff changeset
  1247
// (other items were commented in a header).
hgs
parents:
diff changeset
  1248
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  1249
//
hgs
parents:
diff changeset
  1250
void CCbsDbImpTopicList::GetNextAndPrevTopicNumberL( 
hgs
parents:
diff changeset
  1251
    const TCbsTopicNumber& aCurrentTopic,
hgs
parents:
diff changeset
  1252
    TCbsTopicNumber& aNextTopic, 
hgs
parents:
diff changeset
  1253
    TCbsTopicNumber& aPrevTopic,
hgs
parents:
diff changeset
  1254
    TInt& aPosition )
hgs
parents:
diff changeset
  1255
    {
hgs
parents:
diff changeset
  1256
    // Determine position of requested topic in topic list.
hgs
parents:
diff changeset
  1257
    TInt index( TopicIndexInList( aCurrentTopic ) );
hgs
parents:
diff changeset
  1258
    User::LeaveIfError( index );    // if KErrNotFound
hgs
parents:
diff changeset
  1259
    
hgs
parents:
diff changeset
  1260
    // Determine position indications
hgs
parents:
diff changeset
  1261
    aPosition = 0;
hgs
parents:
diff changeset
  1262
    if ( index == 0 )
hgs
parents:
diff changeset
  1263
        {
hgs
parents:
diff changeset
  1264
        aPosition |= ECbsHead;
hgs
parents:
diff changeset
  1265
        }
hgs
parents:
diff changeset
  1266
    else
hgs
parents:
diff changeset
  1267
        {
hgs
parents:
diff changeset
  1268
        aPrevTopic = iTopics->At( index-1 ).iTopicData.iNumber;
hgs
parents:
diff changeset
  1269
        }
hgs
parents:
diff changeset
  1270
hgs
parents:
diff changeset
  1271
    if ( index == iTopics->Count()-1 )
hgs
parents:
diff changeset
  1272
        {
hgs
parents:
diff changeset
  1273
        aPosition |= ECbsTail;
hgs
parents:
diff changeset
  1274
        }
hgs
parents:
diff changeset
  1275
    else
hgs
parents:
diff changeset
  1276
        {
hgs
parents:
diff changeset
  1277
        aNextTopic = iTopics->At( index+1 ).iTopicData.iNumber;
hgs
parents:
diff changeset
  1278
        }
hgs
parents:
diff changeset
  1279
    }
hgs
parents:
diff changeset
  1280
hgs
parents:
diff changeset
  1281
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  1282
// CCbsDbImpTopicList::NotifyNewMessageArrivedL
hgs
parents:
diff changeset
  1283
// Notifies each observer that a new message has arrived to a topic.
hgs
parents:
diff changeset
  1284
// (other items were commented in a header).
hgs
parents:
diff changeset
  1285
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  1286
//
hgs
parents:
diff changeset
  1287
void CCbsDbImpTopicList::NotifyNewMessageArrivedL(
hgs
parents:
diff changeset
  1288
    const TCbsDbMessageHandle& aHandle )
hgs
parents:
diff changeset
  1289
    {
hgs
parents:
diff changeset
  1290
    // Notify each observer.
hgs
parents:
diff changeset
  1291
    TInt count( iObservers->Count() );
hgs
parents:
diff changeset
  1292
    for ( TInt index( 0 ); index < count; index++ )
hgs
parents:
diff changeset
  1293
        {
hgs
parents:
diff changeset
  1294
        iObservers->At( index )->TopicNewMessageReceivedIndL( aHandle );
hgs
parents:
diff changeset
  1295
        }
hgs
parents:
diff changeset
  1296
    }
hgs
parents:
diff changeset
  1297
hgs
parents:
diff changeset
  1298
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  1299
// CCbsDbImpTopicList::AppendSubscribedTopicsL
hgs
parents:
diff changeset
  1300
// Adds numbers of subscribed topics to the given array.
hgs
parents:
diff changeset
  1301
// (other items were commented in a header).
hgs
parents:
diff changeset
  1302
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  1303
//
hgs
parents:
diff changeset
  1304
void CCbsDbImpTopicList::AppendSubscribedTopicsL( 
hgs
parents:
diff changeset
  1305
    CArrayFixFlat<TUint16>& aSubscriptions ) const
hgs
parents:
diff changeset
  1306
    {
hgs
parents:
diff changeset
  1307
    TInt count( iTopics->Count() );
hgs
parents:
diff changeset
  1308
    for ( TInt i( 0 ); i < count; i++ ) 
hgs
parents:
diff changeset
  1309
        {
hgs
parents:
diff changeset
  1310
        TCbsDbImpTopic& topic = iTopics->At( i );
hgs
parents:
diff changeset
  1311
        if ( topic.iTopicData.iSubscribed )
hgs
parents:
diff changeset
  1312
            {
hgs
parents:
diff changeset
  1313
            aSubscriptions.AppendL( topic.iTopicData.iNumber );
hgs
parents:
diff changeset
  1314
            }
hgs
parents:
diff changeset
  1315
        }
hgs
parents:
diff changeset
  1316
    }
hgs
parents:
diff changeset
  1317
hgs
parents:
diff changeset
  1318
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  1319
// CCbsDbImpTopicList::LoadRootStreamL
hgs
parents:
diff changeset
  1320
// Loads the root stream to the memory.
hgs
parents:
diff changeset
  1321
// (other items were commented in a header).
hgs
parents:
diff changeset
  1322
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  1323
//
hgs
parents:
diff changeset
  1324
void CCbsDbImpTopicList::LoadRootStreamL()
hgs
parents:
diff changeset
  1325
    {
hgs
parents:
diff changeset
  1326
    __ASSERT_DEBUG( iTopicLists != NULL, 
hgs
parents:
diff changeset
  1327
        CbsServerPanic( ECbsTopicListArrayNull ) );
hgs
parents:
diff changeset
  1328
hgs
parents:
diff changeset
  1329
    // Get the root stream and open it.
hgs
parents:
diff changeset
  1330
    TStreamId id( TopicStoreL()->Root() );
hgs
parents:
diff changeset
  1331
    RStoreReadStream instream;
hgs
parents:
diff changeset
  1332
    instream.OpenLC( *TopicStoreL(), id ); // on CS
hgs
parents:
diff changeset
  1333
hgs
parents:
diff changeset
  1334
    // Load the topic list count
hgs
parents:
diff changeset
  1335
    TInt topicListCount( instream.ReadInt16L() );
hgs
parents:
diff changeset
  1336
hgs
parents:
diff changeset
  1337
    // Sanity check
hgs
parents:
diff changeset
  1338
    if ( topicListCount < 0 )
hgs
parents:
diff changeset
  1339
        {
hgs
parents:
diff changeset
  1340
        User::Leave( KErrCorrupt );
hgs
parents:
diff changeset
  1341
        }
hgs
parents:
diff changeset
  1342
hgs
parents:
diff changeset
  1343
    // Reset the topic list array
hgs
parents:
diff changeset
  1344
    iTopicLists->Reset();    
hgs
parents:
diff changeset
  1345
hgs
parents:
diff changeset
  1346
    // Load the topic list information
hgs
parents:
diff changeset
  1347
    TCbsDbImpTopicList item; 
hgs
parents:
diff changeset
  1348
    instream >> item.iTopicListId;
hgs
parents:
diff changeset
  1349
    ReadTopicListInformationL( item.iTopicListId, item );
hgs
parents:
diff changeset
  1350
    iTopicLists->AppendL( item );
hgs
parents:
diff changeset
  1351
        
hgs
parents:
diff changeset
  1352
    // Destroy the stream.
hgs
parents:
diff changeset
  1353
    CleanupStack::PopAndDestroy(); // instream
hgs
parents:
diff changeset
  1354
    }
hgs
parents:
diff changeset
  1355
hgs
parents:
diff changeset
  1356
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  1357
// CCbsDbImpTopicList::LoadDefaultTopicStreamL
hgs
parents:
diff changeset
  1358
// Loads the default topic list to the memory.
hgs
parents:
diff changeset
  1359
// (other items were commented in a header).
hgs
parents:
diff changeset
  1360
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  1361
//
hgs
parents:
diff changeset
  1362
void CCbsDbImpTopicList::LoadDefaultTopicStreamL()
hgs
parents:
diff changeset
  1363
    {
hgs
parents:
diff changeset
  1364
    __ASSERT_DEBUG( iTopics != NULL, 
hgs
parents:
diff changeset
  1365
        CbsServerPanic( ECbsTopicListArrayNull ) );
hgs
parents:
diff changeset
  1366
hgs
parents:
diff changeset
  1367
    // Read root item count
hgs
parents:
diff changeset
  1368
    TInt topicListCount = iTopicLists->Count();
hgs
parents:
diff changeset
  1369
hgs
parents:
diff changeset
  1370
    // If there isn't any, create a topic list for standard topics here
hgs
parents:
diff changeset
  1371
    if ( topicListCount == 0 )
hgs
parents:
diff changeset
  1372
        {
hgs
parents:
diff changeset
  1373
        CreateStandardTopicListL();
hgs
parents:
diff changeset
  1374
        topicListCount = iTopicLists->Count();
hgs
parents:
diff changeset
  1375
        }
hgs
parents:
diff changeset
  1376
hgs
parents:
diff changeset
  1377
    TStreamId defaultTopicListId( 0 );
hgs
parents:
diff changeset
  1378
    TBool quitSearch( EFalse );
hgs
parents:
diff changeset
  1379
hgs
parents:
diff changeset
  1380
    TInt i;
hgs
parents:
diff changeset
  1381
    // Find the default topic list      
hgs
parents:
diff changeset
  1382
    for ( i = 0; ( i < topicListCount ) && !quitSearch; ++i )
hgs
parents:
diff changeset
  1383
        {
hgs
parents:
diff changeset
  1384
        if ( iTopicLists->At( i ).iIsDefaultTopicList )
hgs
parents:
diff changeset
  1385
            {            
hgs
parents:
diff changeset
  1386
            defaultTopicListId = iTopicLists->At( i ).iTopicListId;
hgs
parents:
diff changeset
  1387
            quitSearch = ETrue;
hgs
parents:
diff changeset
  1388
            }        
hgs
parents:
diff changeset
  1389
        }
hgs
parents:
diff changeset
  1390
        
hgs
parents:
diff changeset
  1391
    CArrayFixFlat< TStreamId >* topicIds = 
hgs
parents:
diff changeset
  1392
        new ( ELeave ) CArrayFixFlat< TStreamId >( KTopicIdsGranularity );
hgs
parents:
diff changeset
  1393
hgs
parents:
diff changeset
  1394
    // Open the default topic list stream
hgs
parents:
diff changeset
  1395
    RStoreReadStream instream;
hgs
parents:
diff changeset
  1396
    instream.OpenLC( *TopicStoreL(), defaultTopicListId ); // on CS
hgs
parents:
diff changeset
  1397
hgs
parents:
diff changeset
  1398
    // Set the ID
hgs
parents:
diff changeset
  1399
    iCurrentTopicList.iTopicListId = defaultTopicListId;
hgs
parents:
diff changeset
  1400
    
hgs
parents:
diff changeset
  1401
    // Set the name
hgs
parents:
diff changeset
  1402
    HBufC* topicListName = HBufC::NewL( instream, KCbsDbTopicNameLength );
hgs
parents:
diff changeset
  1403
    iCurrentTopicList.iTopicListName.Copy( topicListName->Des() );
hgs
parents:
diff changeset
  1404
    delete topicListName;
hgs
parents:
diff changeset
  1405
    topicListName = NULL;
hgs
parents:
diff changeset
  1406
hgs
parents:
diff changeset
  1407
    // Skip default list status, since it is always true in default topic list
hgs
parents:
diff changeset
  1408
    CbsStreamHelper::ReadBoolL( instream );
hgs
parents:
diff changeset
  1409
    iCurrentTopicList.iIsDefaultTopicList = ETrue;
hgs
parents:
diff changeset
  1410
hgs
parents:
diff changeset
  1411
    // Set the topic list number
hgs
parents:
diff changeset
  1412
    iCurrentTopicList.iNumber = instream.ReadInt16L();
hgs
parents:
diff changeset
  1413
    
hgs
parents:
diff changeset
  1414
    // Read the amount of topics
hgs
parents:
diff changeset
  1415
    TInt topicCount = instream.ReadInt16L();
hgs
parents:
diff changeset
  1416
    iCurrentTopicList.iTopicCount = topicCount;
hgs
parents:
diff changeset
  1417
hgs
parents:
diff changeset
  1418
    // Clear the topic array.
hgs
parents:
diff changeset
  1419
    iTopics->ResizeL( 0 );
hgs
parents:
diff changeset
  1420
hgs
parents:
diff changeset
  1421
    TStreamId id( 0 );
hgs
parents:
diff changeset
  1422
hgs
parents:
diff changeset
  1423
    // Load the topic IDs       
hgs
parents:
diff changeset
  1424
    for ( i = 0; i < topicCount; i++ )
hgs
parents:
diff changeset
  1425
        {
hgs
parents:
diff changeset
  1426
        instream >> id;
hgs
parents:
diff changeset
  1427
        topicIds->AppendL( id );
hgs
parents:
diff changeset
  1428
        }   
hgs
parents:
diff changeset
  1429
    
hgs
parents:
diff changeset
  1430
    // Destroy the stream.
hgs
parents:
diff changeset
  1431
    CleanupStack::PopAndDestroy();
hgs
parents:
diff changeset
  1432
       
hgs
parents:
diff changeset
  1433
    // Load necessary information for each topic
hgs
parents:
diff changeset
  1434
    TInt count = topicIds->Count();
hgs
parents:
diff changeset
  1435
    TCbsDbImpTopic topic;
hgs
parents:
diff changeset
  1436
    for ( i = 0; i < count; i++ )
hgs
parents:
diff changeset
  1437
        {
hgs
parents:
diff changeset
  1438
        ReadTopicInformationL( topicIds->At( i ), topic );
hgs
parents:
diff changeset
  1439
hgs
parents:
diff changeset
  1440
        if ( topic.iTopicData.iNumber == KIndexTopicNumber )
hgs
parents:
diff changeset
  1441
            {
hgs
parents:
diff changeset
  1442
            HBufC* indexName = ReadIndexTopicNameLC(); // on CS
hgs
parents:
diff changeset
  1443
            topic.iTopicData.iName.Copy( *indexName );
hgs
parents:
diff changeset
  1444
            CleanupStack::PopAndDestroy(); // indexName
hgs
parents:
diff changeset
  1445
            }
hgs
parents:
diff changeset
  1446
hgs
parents:
diff changeset
  1447
        iTopics->AppendL( topic );
hgs
parents:
diff changeset
  1448
        }
hgs
parents:
diff changeset
  1449
    delete topicIds;
hgs
parents:
diff changeset
  1450
    topicIds = NULL;
hgs
parents:
diff changeset
  1451
    }
hgs
parents:
diff changeset
  1452
hgs
parents:
diff changeset
  1453
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  1454
// CCbsDbImpTopicList::ReadTopicListInformationL
hgs
parents:
diff changeset
  1455
// Reads topic list information from stream.
hgs
parents:
diff changeset
  1456
// (other items were commented in a header).
hgs
parents:
diff changeset
  1457
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  1458
//
hgs
parents:
diff changeset
  1459
void CCbsDbImpTopicList::ReadTopicListInformationL( 
hgs
parents:
diff changeset
  1460
    const TStreamId& aId, 
hgs
parents:
diff changeset
  1461
    TCbsDbImpTopicList& aTopicList ) const
hgs
parents:
diff changeset
  1462
    {
hgs
parents:
diff changeset
  1463
    RStoreReadStream instream;
hgs
parents:
diff changeset
  1464
    instream.OpenLC( *TopicStoreL(), aId ); // on CS
hgs
parents:
diff changeset
  1465
hgs
parents:
diff changeset
  1466
    // Topic List name
hgs
parents:
diff changeset
  1467
    HBufC* topicListName = HBufC::NewL( instream, KCbsDbTopicNameLength );
hgs
parents:
diff changeset
  1468
    aTopicList.iTopicListName.Copy( topicListName->Des() );
hgs
parents:
diff changeset
  1469
    delete topicListName;
hgs
parents:
diff changeset
  1470
    topicListName = NULL;
hgs
parents:
diff changeset
  1471
hgs
parents:
diff changeset
  1472
    // Default list status
hgs
parents:
diff changeset
  1473
    aTopicList.iIsDefaultTopicList = CbsStreamHelper::ReadBoolL( instream );
hgs
parents:
diff changeset
  1474
hgs
parents:
diff changeset
  1475
    // Topic List number
hgs
parents:
diff changeset
  1476
    aTopicList.iNumber = instream.ReadInt16L();
hgs
parents:
diff changeset
  1477
hgs
parents:
diff changeset
  1478
    // Topic count of this topic list
hgs
parents:
diff changeset
  1479
    aTopicList.iTopicCount = instream.ReadInt16L();   
hgs
parents:
diff changeset
  1480
hgs
parents:
diff changeset
  1481
    CleanupStack::PopAndDestroy(); // instream
hgs
parents:
diff changeset
  1482
    }
hgs
parents:
diff changeset
  1483
hgs
parents:
diff changeset
  1484
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  1485
// CCbsDbImpTopicList::ReadTopicInformationL
hgs
parents:
diff changeset
  1486
// Reads all information on topic found in stream aId
hgs
parents:
diff changeset
  1487
// into aTopic.
hgs
parents:
diff changeset
  1488
// (other items were commented in a header).
hgs
parents:
diff changeset
  1489
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  1490
//
hgs
parents:
diff changeset
  1491
void CCbsDbImpTopicList::ReadTopicInformationL( 
hgs
parents:
diff changeset
  1492
    const TStreamId& aId, 
hgs
parents:
diff changeset
  1493
    TCbsDbImpTopic& aTopic ) const
hgs
parents:
diff changeset
  1494
    {
hgs
parents:
diff changeset
  1495
    RStoreReadStream instream;
hgs
parents:
diff changeset
  1496
    instream.OpenLC( *TopicStoreL(), aId ); // on CS
hgs
parents:
diff changeset
  1497
hgs
parents:
diff changeset
  1498
    // Topic ID
hgs
parents:
diff changeset
  1499
    aTopic.iTopicId = aId;
hgs
parents:
diff changeset
  1500
hgs
parents:
diff changeset
  1501
    // Read saved messages.
hgs
parents:
diff changeset
  1502
    aTopic.iTopicData.iSavedMessages = instream.ReadInt16L();
hgs
parents:
diff changeset
  1503
hgs
parents:
diff changeset
  1504
    // Read name.
hgs
parents:
diff changeset
  1505
    instream >> aTopic.iTopicData.iName;
hgs
parents:
diff changeset
  1506
    
hgs
parents:
diff changeset
  1507
    // Read topic number (message identifier)
hgs
parents:
diff changeset
  1508
    aTopic.iTopicData.iNumber = instream.ReadInt16L();
hgs
parents:
diff changeset
  1509
hgs
parents:
diff changeset
  1510
    // Read statuses
hgs
parents:
diff changeset
  1511
    aTopic.iTopicData.iProtected = CbsStreamHelper::ReadBoolL( instream ); // Protected
hgs
parents:
diff changeset
  1512
    aTopic.iTopicData.iSubscribed = CbsStreamHelper::ReadBoolL( instream );// Subscribed
hgs
parents:
diff changeset
  1513
    aTopic.iTopicData.iHotmarked = CbsStreamHelper::ReadBoolL( instream ); // Hotmarked
hgs
parents:
diff changeset
  1514
    
hgs
parents:
diff changeset
  1515
    // Read unread messages count
hgs
parents:
diff changeset
  1516
    aTopic.iTopicData.iUnreadMessages = instream.ReadInt16L();
hgs
parents:
diff changeset
  1517
hgs
parents:
diff changeset
  1518
    // Topic messages' stream ID
hgs
parents:
diff changeset
  1519
    instream >> aTopic.iTopicMessagesId;
hgs
parents:
diff changeset
  1520
hgs
parents:
diff changeset
  1521
    // Sanity check
hgs
parents:
diff changeset
  1522
    if ( aTopic.iTopicData.iSavedMessages > KCbsDbMaxSavedMessages
hgs
parents:
diff changeset
  1523
        || aTopic.iTopicData.iNumber > KCbsMaxValidTopicNumber
hgs
parents:
diff changeset
  1524
        || aTopic.iTopicData.iUnreadMessages > KCbsDbMaxReceivedMessages )
hgs
parents:
diff changeset
  1525
        {
hgs
parents:
diff changeset
  1526
        User::Leave( KErrCorrupt );
hgs
parents:
diff changeset
  1527
        }
hgs
parents:
diff changeset
  1528
hgs
parents:
diff changeset
  1529
    CleanupStack::PopAndDestroy(); // instream
hgs
parents:
diff changeset
  1530
    }
hgs
parents:
diff changeset
  1531
hgs
parents:
diff changeset
  1532
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  1533
// CCbsDbImpTopicList::DoAddTopicL
hgs
parents:
diff changeset
  1534
// Adds a topic into the database.
hgs
parents:
diff changeset
  1535
// Assumes aTopic is not a duplicate.
hgs
parents:
diff changeset
  1536
// (other items were commented in a header).
hgs
parents:
diff changeset
  1537
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  1538
//
hgs
parents:
diff changeset
  1539
void CCbsDbImpTopicList::DoAddTopicL( 
hgs
parents:
diff changeset
  1540
    const TCbsDbTopic& aTopic )
hgs
parents:
diff changeset
  1541
    {
hgs
parents:
diff changeset
  1542
    // About to write to FFS: make critical level check
hgs
parents:
diff changeset
  1543
    CbsUtils::FFSCriticalLevelCheckL( KTopicStreamSize + 
hgs
parents:
diff changeset
  1544
        KEmptyTopicMessagesStreamSize, iFs );  
hgs
parents:
diff changeset
  1545
hgs
parents:
diff changeset
  1546
    // Generate general information about topic.
hgs
parents:
diff changeset
  1547
    TCbsDbImpTopic topic;
hgs
parents:
diff changeset
  1548
    topic.iTopicData = aTopic;
hgs
parents:
diff changeset
  1549
    
hgs
parents:
diff changeset
  1550
    // Write stream for messages.
hgs
parents:
diff changeset
  1551
    topic.iTopicMessagesId = 
hgs
parents:
diff changeset
  1552
        CCbsDbImpTopicMessages::CreateDefaultTopicMessagesStreamL( 
hgs
parents:
diff changeset
  1553
            *TopicStoreL() );
hgs
parents:
diff changeset
  1554
hgs
parents:
diff changeset
  1555
    // Create stream for topic information.
hgs
parents:
diff changeset
  1556
    RStoreWriteStream outstream;
hgs
parents:
diff changeset
  1557
    topic.iTopicId = outstream.CreateLC( *TopicStoreL() ); // on CS
hgs
parents:
diff changeset
  1558
    
hgs
parents:
diff changeset
  1559
    WriteTopicInformationL( outstream, aTopic, topic.iTopicMessagesId );
hgs
parents:
diff changeset
  1560
hgs
parents:
diff changeset
  1561
    outstream.CommitL();
hgs
parents:
diff changeset
  1562
    CleanupStack::PopAndDestroy(); // outstream
hgs
parents:
diff changeset
  1563
hgs
parents:
diff changeset
  1564
    // Now, insert the new topic information to the array
hgs
parents:
diff changeset
  1565
    TKeyArrayFix key( _FOFF( TCbsDbImpTopic, iTopicData.iNumber ), ECmpTUint16 );
hgs
parents:
diff changeset
  1566
    iTopics->InsertIsqL( topic, key );    
hgs
parents:
diff changeset
  1567
hgs
parents:
diff changeset
  1568
    // Update the topic list stream
hgs
parents:
diff changeset
  1569
    UpdateTopicListStreamL( iCurrentTopicList, EFalse );
hgs
parents:
diff changeset
  1570
  
hgs
parents:
diff changeset
  1571
    // We have modified only iTopicStore, so if CommitFilesL() leaves,
hgs
parents:
diff changeset
  1572
    // we either get all committed or nothing committed.
hgs
parents:
diff changeset
  1573
    CommitFilesL();
hgs
parents:
diff changeset
  1574
    }
hgs
parents:
diff changeset
  1575
hgs
parents:
diff changeset
  1576
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  1577
// CCbsDbImpTopicList::DoUpdateTopicL
hgs
parents:
diff changeset
  1578
// Updates the data for a topic into the database.
hgs
parents:
diff changeset
  1579
// The position of the topic in the list is
hgs
parents:
diff changeset
  1580
// changed if necessary. 
hgs
parents:
diff changeset
  1581
// If the position has to be changed, the handles of messages
hgs
parents:
diff changeset
  1582
// contained in the topic have to be changed as well because
hgs
parents:
diff changeset
  1583
// the high word of the handle identifies the topic by it's
hgs
parents:
diff changeset
  1584
// number.
hgs
parents:
diff changeset
  1585
// Please note that standard EPOC DoSomethingL protocol
hgs
parents:
diff changeset
  1586
// is not followed here as this function does NOT commit
hgs
parents:
diff changeset
  1587
// changes made to the store. Instead, the caller must ensure
hgs
parents:
diff changeset
  1588
// use of proper commit/revert operations.
hgs
parents:
diff changeset
  1589
// (other items were commented in a header).
hgs
parents:
diff changeset
  1590
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  1591
//
hgs
parents:
diff changeset
  1592
void CCbsDbImpTopicList::DoUpdateTopicL( 
hgs
parents:
diff changeset
  1593
    const TCbsDbTopic& aTopic, 
hgs
parents:
diff changeset
  1594
    TBool aNeedToChange, 
hgs
parents:
diff changeset
  1595
    TInt aOldPosition,
hgs
parents:
diff changeset
  1596
    TBool aDeleting )
hgs
parents:
diff changeset
  1597
    {
hgs
parents:
diff changeset
  1598
    CBSLOGSTRING("CBSSERVER: >>> CCbsDbImpTopicList::DoUpdateTopicL()");
hgs
parents:
diff changeset
  1599
    
hgs
parents:
diff changeset
  1600
    RSharedDataClient sharedData;
hgs
parents:
diff changeset
  1601
    // About to write to FFS: make critical level check
hgs
parents:
diff changeset
  1602
    if ( aDeleting )
hgs
parents:
diff changeset
  1603
        {
hgs
parents:
diff changeset
  1604
        User::LeaveIfError( sharedData.Connect() );
hgs
parents:
diff changeset
  1605
        sharedData.RequestFreeDiskSpaceLC( KTopicStreamSize ); // on CS
hgs
parents:
diff changeset
  1606
        }
hgs
parents:
diff changeset
  1607
    else
hgs
parents:
diff changeset
  1608
        {
hgs
parents:
diff changeset
  1609
        CbsUtils::FFSCriticalLevelCheckL( KTopicStreamSize, iFs );
hgs
parents:
diff changeset
  1610
        }
hgs
parents:
diff changeset
  1611
hgs
parents:
diff changeset
  1612
    TInt newPosition( aOldPosition );
hgs
parents:
diff changeset
  1613
hgs
parents:
diff changeset
  1614
    // If there is need to change position, change it.
hgs
parents:
diff changeset
  1615
    if ( aNeedToChange )
hgs
parents:
diff changeset
  1616
        {
hgs
parents:
diff changeset
  1617
        TCbsDbImpTopic topic( iTopics->At( aOldPosition ) );
hgs
parents:
diff changeset
  1618
hgs
parents:
diff changeset
  1619
        // Adjust handles of topic messages to match the changed topic number.
hgs
parents:
diff changeset
  1620
        iMessages->UpdateHandlesOfTopicMessagesL( 
hgs
parents:
diff changeset
  1621
            topic.iTopicData.iNumber, aTopic.iNumber );
hgs
parents:
diff changeset
  1622
            
hgs
parents:
diff changeset
  1623
        // Delete topic from the array.
hgs
parents:
diff changeset
  1624
        iTopics->Delete( aOldPosition );        
hgs
parents:
diff changeset
  1625
hgs
parents:
diff changeset
  1626
        // Set the number and reinsert into array
hgs
parents:
diff changeset
  1627
        topic.iTopicData.iNumber = aTopic.iNumber;
hgs
parents:
diff changeset
  1628
        TKeyArrayFix key( _FOFF( TCbsDbImpTopic, iTopicData.iNumber ), ECmpTUint16 );
hgs
parents:
diff changeset
  1629
        newPosition = iTopics->InsertIsqL( topic, key );
hgs
parents:
diff changeset
  1630
        }
hgs
parents:
diff changeset
  1631
hgs
parents:
diff changeset
  1632
    iTopics->At( newPosition ).iTopicData.iSubscribed = aTopic.iSubscribed;
hgs
parents:
diff changeset
  1633
    iTopics->At( newPosition ).iTopicData.iHotmarked = aTopic.iHotmarked;
hgs
parents:
diff changeset
  1634
    iTopics->At( newPosition ).iTopicData.iName = aTopic.iName;
hgs
parents:
diff changeset
  1635
    iTopics->At( newPosition ).iTopicData.iSavedMessages = aTopic.iSavedMessages;
hgs
parents:
diff changeset
  1636
    iTopics->At( newPosition ).iTopicData.iUnreadMessages = aTopic.iUnreadMessages;
hgs
parents:
diff changeset
  1637
    
hgs
parents:
diff changeset
  1638
    // Replace existing stream.
hgs
parents:
diff changeset
  1639
    RStoreWriteStream outstream;
hgs
parents:
diff changeset
  1640
    outstream.ReplaceLC( *TopicStoreL(), 
hgs
parents:
diff changeset
  1641
        iTopics->At( newPosition ).iTopicId ); // on CS
hgs
parents:
diff changeset
  1642
    WriteTopicInformationL( outstream, aTopic, 
hgs
parents:
diff changeset
  1643
        iTopics->At( newPosition ).iTopicMessagesId );
hgs
parents:
diff changeset
  1644
hgs
parents:
diff changeset
  1645
    outstream.CommitL();
hgs
parents:
diff changeset
  1646
    CleanupStack::PopAndDestroy(); // outstream
hgs
parents:
diff changeset
  1647
hgs
parents:
diff changeset
  1648
    // Free the reserved space
hgs
parents:
diff changeset
  1649
    if ( aDeleting )
hgs
parents:
diff changeset
  1650
        {
hgs
parents:
diff changeset
  1651
        CleanupStack::PopAndDestroy(); // disk space
hgs
parents:
diff changeset
  1652
        sharedData.Close();
hgs
parents:
diff changeset
  1653
        }
hgs
parents:
diff changeset
  1654
    
hgs
parents:
diff changeset
  1655
    // Update topic list stream, if necessary.
hgs
parents:
diff changeset
  1656
    if ( aNeedToChange )
hgs
parents:
diff changeset
  1657
        {
hgs
parents:
diff changeset
  1658
        UpdateTopicListStreamL( iCurrentTopicList, EFalse );
hgs
parents:
diff changeset
  1659
        }
hgs
parents:
diff changeset
  1660
    
hgs
parents:
diff changeset
  1661
    CBSLOGSTRING("CBSSERVER: <<< CCbsDbImpTopicList::DoUpdateTopicL()");
hgs
parents:
diff changeset
  1662
    }
hgs
parents:
diff changeset
  1663
hgs
parents:
diff changeset
  1664
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  1665
// CCbsDbImpTopicList::DoDeleteTopicL
hgs
parents:
diff changeset
  1666
// Deletes a topic from the given position in the database.
hgs
parents:
diff changeset
  1667
// (other items were commented in a header).
hgs
parents:
diff changeset
  1668
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  1669
//
hgs
parents:
diff changeset
  1670
void CCbsDbImpTopicList::DoDeleteTopicL( 
hgs
parents:
diff changeset
  1671
    TInt aPosition )
hgs
parents:
diff changeset
  1672
    {
hgs
parents:
diff changeset
  1673
    // Check that the topic is not protected.
hgs
parents:
diff changeset
  1674
    TCbsDbTopic topic;
hgs
parents:
diff changeset
  1675
    GetTopicL( aPosition, topic );
hgs
parents:
diff changeset
  1676
hgs
parents:
diff changeset
  1677
    // It is not allowed to delete a topic that is protected. It must
hgs
parents:
diff changeset
  1678
    // first be updated to be not protected.
hgs
parents:
diff changeset
  1679
    if ( topic.iProtected )
hgs
parents:
diff changeset
  1680
        {
hgs
parents:
diff changeset
  1681
        User::Leave( KErrAccessDenied );
hgs
parents:
diff changeset
  1682
        }
hgs
parents:
diff changeset
  1683
hgs
parents:
diff changeset
  1684
    // First delete all messages the topic contains.
hgs
parents:
diff changeset
  1685
    RStoreReadStream instream;    
hgs
parents:
diff changeset
  1686
    instream.OpenLC( *TopicStoreL(), 
hgs
parents:
diff changeset
  1687
        iTopics->At( aPosition ).iTopicMessagesId ); // on CS
hgs
parents:
diff changeset
  1688
    CCbsDbImpTopicMessages::DeleteAllTopicMessagesL( *TopicStoreL(), 
hgs
parents:
diff changeset
  1689
        *iUnsavedMessageStore, instream );
hgs
parents:
diff changeset
  1690
    iMessages->InvalidateCache();
hgs
parents:
diff changeset
  1691
    CleanupStack::PopAndDestroy(); // instream
hgs
parents:
diff changeset
  1692
hgs
parents:
diff changeset
  1693
    // Delete topic and topic messages streams.
hgs
parents:
diff changeset
  1694
    TopicStoreL()->DeleteL( iTopics->At( aPosition ).iTopicMessagesId );
hgs
parents:
diff changeset
  1695
    TopicStoreL()->DeleteL( iTopics->At( aPosition ).iTopicId );    
hgs
parents:
diff changeset
  1696
hgs
parents:
diff changeset
  1697
    // Remove from the internal cache.
hgs
parents:
diff changeset
  1698
    iTopics->Delete( aPosition );
hgs
parents:
diff changeset
  1699
hgs
parents:
diff changeset
  1700
    iCurrentTopicList.iTopicCount--;                
hgs
parents:
diff changeset
  1701
    UpdateTopicListStreamL( iCurrentTopicList, ETrue );
hgs
parents:
diff changeset
  1702
    CommitFilesL();
hgs
parents:
diff changeset
  1703
    }
hgs
parents:
diff changeset
  1704
hgs
parents:
diff changeset
  1705
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  1706
// CCbsDbImpTopicList::UpdateRootStreamL
hgs
parents:
diff changeset
  1707
// Updates the root stream. 
hgs
parents:
diff changeset
  1708
// (other items were commented in a header).
hgs
parents:
diff changeset
  1709
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  1710
//
hgs
parents:
diff changeset
  1711
void CCbsDbImpTopicList::UpdateRootStreamL(
hgs
parents:
diff changeset
  1712
    TBool aDeleting )
hgs
parents:
diff changeset
  1713
    {      
hgs
parents:
diff changeset
  1714
    // Check the free space
hgs
parents:
diff changeset
  1715
    TInt neededSpace( KTopicListRootStreamSize + iTopicLists->Count() * 2 );        
hgs
parents:
diff changeset
  1716
hgs
parents:
diff changeset
  1717
    RSharedDataClient sharedData;
hgs
parents:
diff changeset
  1718
    // About to write to FFS: make critical level check
hgs
parents:
diff changeset
  1719
    if ( aDeleting )
hgs
parents:
diff changeset
  1720
        {
hgs
parents:
diff changeset
  1721
        User::LeaveIfError( sharedData.Connect() );
hgs
parents:
diff changeset
  1722
        sharedData.RequestFreeDiskSpaceLC( neededSpace ); // on CS
hgs
parents:
diff changeset
  1723
        }
hgs
parents:
diff changeset
  1724
    else
hgs
parents:
diff changeset
  1725
        {
hgs
parents:
diff changeset
  1726
        CbsUtils::FFSCriticalLevelCheckL( neededSpace, iFs );
hgs
parents:
diff changeset
  1727
        }
hgs
parents:
diff changeset
  1728
hgs
parents:
diff changeset
  1729
    // Now there is room for all topics. So we can just replace.
hgs
parents:
diff changeset
  1730
    RStoreWriteStream outstream;
hgs
parents:
diff changeset
  1731
    outstream.ReplaceLC( *TopicStoreL(), TopicStoreL()->Root() ); // on CS        
hgs
parents:
diff changeset
  1732
hgs
parents:
diff changeset
  1733
    // Write root stream
hgs
parents:
diff changeset
  1734
    WriteRootStreamL( outstream );
hgs
parents:
diff changeset
  1735
    CleanupStack::PopAndDestroy(); // outstream
hgs
parents:
diff changeset
  1736
hgs
parents:
diff changeset
  1737
    // Free the reserved space
hgs
parents:
diff changeset
  1738
    if ( aDeleting )
hgs
parents:
diff changeset
  1739
        {
hgs
parents:
diff changeset
  1740
        CleanupStack::PopAndDestroy(); // disk space
hgs
parents:
diff changeset
  1741
        sharedData.Close();
hgs
parents:
diff changeset
  1742
        }
hgs
parents:
diff changeset
  1743
    }
hgs
parents:
diff changeset
  1744
hgs
parents:
diff changeset
  1745
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  1746
// CCbsDbImpTopicList::UpdateTopicListStreamL
hgs
parents:
diff changeset
  1747
// Updates topic list stream. 
hgs
parents:
diff changeset
  1748
// (other items were commented in a header).
hgs
parents:
diff changeset
  1749
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  1750
//
hgs
parents:
diff changeset
  1751
void CCbsDbImpTopicList::UpdateTopicListStreamL( 
hgs
parents:
diff changeset
  1752
    TCbsDbImpTopicList& aTopicList,
hgs
parents:
diff changeset
  1753
    TBool aDeleting )
hgs
parents:
diff changeset
  1754
    {        
hgs
parents:
diff changeset
  1755
    TInt neededSpace( KTopicListStreamSize + iTopics->Count() * 2 );
hgs
parents:
diff changeset
  1756
    
hgs
parents:
diff changeset
  1757
    RSharedDataClient sharedData;
hgs
parents:
diff changeset
  1758
    // About to write to FFS: make critical level check
hgs
parents:
diff changeset
  1759
    if ( aDeleting )
hgs
parents:
diff changeset
  1760
        {
hgs
parents:
diff changeset
  1761
        User::LeaveIfError( sharedData.Connect() );
hgs
parents:
diff changeset
  1762
        sharedData.RequestFreeDiskSpaceLC( neededSpace ); // on CS
hgs
parents:
diff changeset
  1763
        }
hgs
parents:
diff changeset
  1764
    else
hgs
parents:
diff changeset
  1765
        {
hgs
parents:
diff changeset
  1766
        CbsUtils::FFSCriticalLevelCheckL( neededSpace, iFs );
hgs
parents:
diff changeset
  1767
        }
hgs
parents:
diff changeset
  1768
    
hgs
parents:
diff changeset
  1769
    // Replace the stream
hgs
parents:
diff changeset
  1770
    RStoreWriteStream outstream;
hgs
parents:
diff changeset
  1771
    outstream.ReplaceLC( *TopicStoreL(), aTopicList.iTopicListId ); // on CS
hgs
parents:
diff changeset
  1772
    
hgs
parents:
diff changeset
  1773
    // Write root stream
hgs
parents:
diff changeset
  1774
    WriteTopicListStreamL( outstream, aTopicList );
hgs
parents:
diff changeset
  1775
    CleanupStack::PopAndDestroy(); // outstream
hgs
parents:
diff changeset
  1776
    
hgs
parents:
diff changeset
  1777
    // Free the reserved space
hgs
parents:
diff changeset
  1778
    if ( aDeleting )
hgs
parents:
diff changeset
  1779
        {
hgs
parents:
diff changeset
  1780
        CleanupStack::PopAndDestroy(); // disk space
hgs
parents:
diff changeset
  1781
        sharedData.Close();
hgs
parents:
diff changeset
  1782
        }
hgs
parents:
diff changeset
  1783
    }
hgs
parents:
diff changeset
  1784
hgs
parents:
diff changeset
  1785
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  1786
// CCbsDbImpTopicList::UpdateTopicL
hgs
parents:
diff changeset
  1787
// Updates the information for a topic already existing 
hgs
parents:
diff changeset
  1788
// in the database.
hgs
parents:
diff changeset
  1789
// (other items were commented in a header).
hgs
parents:
diff changeset
  1790
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  1791
//
hgs
parents:
diff changeset
  1792
void CCbsDbImpTopicList::UpdateTopicL( 
hgs
parents:
diff changeset
  1793
    TCbsDbTopicNumber aTopicNumber, 
hgs
parents:
diff changeset
  1794
    TCbsDbTopic& aTopic )
hgs
parents:
diff changeset
  1795
    {
hgs
parents:
diff changeset
  1796
    // Check that the new topic number is unique.
hgs
parents:
diff changeset
  1797
    TBool needToChangePosition( ETrue );
hgs
parents:
diff changeset
  1798
hgs
parents:
diff changeset
  1799
    // Try to find the topic and leave if it was not found.
hgs
parents:
diff changeset
  1800
    TInt oldPosition( TopicIndexInList( aTopicNumber ) );
hgs
parents:
diff changeset
  1801
    User::LeaveIfError( oldPosition );
hgs
parents:
diff changeset
  1802
hgs
parents:
diff changeset
  1803
    // Check if we have to change the position of the topic in the 
hgs
parents:
diff changeset
  1804
    // internal array
hgs
parents:
diff changeset
  1805
    if ( aTopic.iNumber == aTopicNumber )
hgs
parents:
diff changeset
  1806
        {
hgs
parents:
diff changeset
  1807
        needToChangePosition = EFalse;
hgs
parents:
diff changeset
  1808
        }
hgs
parents:
diff changeset
  1809
    else
hgs
parents:
diff changeset
  1810
        {
hgs
parents:
diff changeset
  1811
        TInt topicWithTheNewNumber( TopicIndexInList( aTopic.iNumber ) );
hgs
parents:
diff changeset
  1812
        if ( topicWithTheNewNumber != KErrNotFound && 
hgs
parents:
diff changeset
  1813
             topicWithTheNewNumber != oldPosition )
hgs
parents:
diff changeset
  1814
            {
hgs
parents:
diff changeset
  1815
            User::Leave( KErrAlreadyExists );
hgs
parents:
diff changeset
  1816
            }
hgs
parents:
diff changeset
  1817
        }
hgs
parents:
diff changeset
  1818
hgs
parents:
diff changeset
  1819
    // Write data to store.
hgs
parents:
diff changeset
  1820
    // Deviation from EPOC standards: DoUpdateTopicL does NOT commit 
hgs
parents:
diff changeset
  1821
    // the store.
hgs
parents:
diff changeset
  1822
    TRAPD( result, DoUpdateTopicL( aTopic, needToChangePosition, 
hgs
parents:
diff changeset
  1823
        oldPosition, EFalse ) );
hgs
parents:
diff changeset
  1824
hgs
parents:
diff changeset
  1825
    // Commit both topic and unsaved msgs store
hgs
parents:
diff changeset
  1826
    if ( result == KErrNone )
hgs
parents:
diff changeset
  1827
        {
hgs
parents:
diff changeset
  1828
        TRAP( result, CommitFilesL() );
hgs
parents:
diff changeset
  1829
        }
hgs
parents:
diff changeset
  1830
hgs
parents:
diff changeset
  1831
    // If either DoUpdateTopicL or CommitFilesL fails => revert.
hgs
parents:
diff changeset
  1832
    if ( result != KErrNone )
hgs
parents:
diff changeset
  1833
        {
hgs
parents:
diff changeset
  1834
        TopicStoreL()->Revert();
hgs
parents:
diff changeset
  1835
        ReloadRootStreamL();
hgs
parents:
diff changeset
  1836
        User::Leave( result );
hgs
parents:
diff changeset
  1837
        }    
hgs
parents:
diff changeset
  1838
    else
hgs
parents:
diff changeset
  1839
        {
hgs
parents:
diff changeset
  1840
        // Notify the observers.
hgs
parents:
diff changeset
  1841
        NotifyTopicModifiedL( aTopicNumber );
hgs
parents:
diff changeset
  1842
        }
hgs
parents:
diff changeset
  1843
    }
hgs
parents:
diff changeset
  1844
hgs
parents:
diff changeset
  1845
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  1846
// CCbsDbImpTopicList::CheckTopicNumber
hgs
parents:
diff changeset
  1847
// Checks if a topic number is valid.
hgs
parents:
diff changeset
  1848
// The valid topic number range in this implementation is 
hgs
parents:
diff changeset
  1849
// 000..999, with 000 indicating an index message.
hgs
parents:
diff changeset
  1850
// (other items were commented in a header).
hgs
parents:
diff changeset
  1851
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  1852
//
hgs
parents:
diff changeset
  1853
TBool CCbsDbImpTopicList::CheckTopicNumber( 
hgs
parents:
diff changeset
  1854
    TCbsDbTopicNumber aNumber ) const
hgs
parents:
diff changeset
  1855
    {
hgs
parents:
diff changeset
  1856
    // Check that the number is in proper range
hgs
parents:
diff changeset
  1857
    return aNumber <= KCbsMaxValidTopicNumber;        
hgs
parents:
diff changeset
  1858
    }
hgs
parents:
diff changeset
  1859
hgs
parents:
diff changeset
  1860
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  1861
// CCbsDbImpTopicList::WriteRootStreamL
hgs
parents:
diff changeset
  1862
// Write the root stream.
hgs
parents:
diff changeset
  1863
// (other items were commented in a header).
hgs
parents:
diff changeset
  1864
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  1865
//
hgs
parents:
diff changeset
  1866
void CCbsDbImpTopicList::WriteRootStreamL( 
hgs
parents:
diff changeset
  1867
    RWriteStream& aOut ) const
hgs
parents:
diff changeset
  1868
    {
hgs
parents:
diff changeset
  1869
    // Write the total amount of topic lists 
hgs
parents:
diff changeset
  1870
    TInt topicListCount( iTopicLists->Count() );
hgs
parents:
diff changeset
  1871
    aOut.WriteInt16L( topicListCount );
hgs
parents:
diff changeset
  1872
    
hgs
parents:
diff changeset
  1873
    // Write space for topic list stream ids
hgs
parents:
diff changeset
  1874
    TInt index;
hgs
parents:
diff changeset
  1875
    for ( index = 0; index < topicListCount; index++ )
hgs
parents:
diff changeset
  1876
        {
hgs
parents:
diff changeset
  1877
        aOut << iTopicLists->At( index ).iTopicListId;
hgs
parents:
diff changeset
  1878
        }
hgs
parents:
diff changeset
  1879
    
hgs
parents:
diff changeset
  1880
    // Write null stream ids
hgs
parents:
diff changeset
  1881
    for ( ; index < KCbsRootItemsSize; index++ )
hgs
parents:
diff changeset
  1882
        {
hgs
parents:
diff changeset
  1883
        aOut << TStreamId( 0 );
hgs
parents:
diff changeset
  1884
        }
hgs
parents:
diff changeset
  1885
    
hgs
parents:
diff changeset
  1886
    aOut.CommitL();
hgs
parents:
diff changeset
  1887
    }
hgs
parents:
diff changeset
  1888
hgs
parents:
diff changeset
  1889
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  1890
// CCbsDbImpTopicList::WriteTopicListStreamL
hgs
parents:
diff changeset
  1891
// Writes topic list information to specified stream.
hgs
parents:
diff changeset
  1892
// (other items were commented in a header).
hgs
parents:
diff changeset
  1893
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  1894
//
hgs
parents:
diff changeset
  1895
void CCbsDbImpTopicList::WriteTopicListStreamL( 
hgs
parents:
diff changeset
  1896
    RWriteStream& aOut, 
hgs
parents:
diff changeset
  1897
    TCbsDbImpTopicList& aTopicList ) const
hgs
parents:
diff changeset
  1898
    {
hgs
parents:
diff changeset
  1899
    // Write the values to this topic list's stream
hgs
parents:
diff changeset
  1900
hgs
parents:
diff changeset
  1901
    // Topic List name
hgs
parents:
diff changeset
  1902
    aOut << aTopicList.iTopicListName;
hgs
parents:
diff changeset
  1903
hgs
parents:
diff changeset
  1904
    // Is this the default list
hgs
parents:
diff changeset
  1905
    CbsStreamHelper::WriteBoolL( aOut, aTopicList.iIsDefaultTopicList );
hgs
parents:
diff changeset
  1906
hgs
parents:
diff changeset
  1907
    // Topic List number
hgs
parents:
diff changeset
  1908
    aOut.WriteInt16L( aTopicList.iNumber );
hgs
parents:
diff changeset
  1909
hgs
parents:
diff changeset
  1910
    // NUmber of topics in this list
hgs
parents:
diff changeset
  1911
    aOut.WriteInt16L( iTopics->Count() );
hgs
parents:
diff changeset
  1912
hgs
parents:
diff changeset
  1913
    // Write the stream IDs of the topics belonging to this list    
hgs
parents:
diff changeset
  1914
    TInt i;
hgs
parents:
diff changeset
  1915
    for ( i = 0; i < iTopics->Count(); i++ )
hgs
parents:
diff changeset
  1916
        {
hgs
parents:
diff changeset
  1917
        aOut << iTopics->At( i ).iTopicId;
hgs
parents:
diff changeset
  1918
        }
hgs
parents:
diff changeset
  1919
hgs
parents:
diff changeset
  1920
    // Write space for the rest topic stream IDs    
hgs
parents:
diff changeset
  1921
    for ( ; i < KCbsDbTopicArraySize; i++ )
hgs
parents:
diff changeset
  1922
        {
hgs
parents:
diff changeset
  1923
        aOut << TStreamId( 0 );
hgs
parents:
diff changeset
  1924
        }        
hgs
parents:
diff changeset
  1925
           
hgs
parents:
diff changeset
  1926
    aOut.CommitL();
hgs
parents:
diff changeset
  1927
    }
hgs
parents:
diff changeset
  1928
hgs
parents:
diff changeset
  1929
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  1930
// CCbsDbImpTopicList::NotifyTopicListInitializedL
hgs
parents:
diff changeset
  1931
// Notifies each observer that the topic list has been initialized.
hgs
parents:
diff changeset
  1932
// (other items were commented in a header).
hgs
parents:
diff changeset
  1933
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  1934
//
hgs
parents:
diff changeset
  1935
void CCbsDbImpTopicList::NotifyTopicListInitializedL()
hgs
parents:
diff changeset
  1936
    {
hgs
parents:
diff changeset
  1937
    // Notify each observer.
hgs
parents:
diff changeset
  1938
    TInt count( iObservers->Count() );
hgs
parents:
diff changeset
  1939
    for( TInt index( 0 ); index < count; index++ )
hgs
parents:
diff changeset
  1940
        {
hgs
parents:
diff changeset
  1941
        iObservers->At( index )->TopicListInitializedIndL();
hgs
parents:
diff changeset
  1942
        }
hgs
parents:
diff changeset
  1943
    }
hgs
parents:
diff changeset
  1944
hgs
parents:
diff changeset
  1945
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  1946
// CCbsDbImpTopicList::NotifyTopicAddedL
hgs
parents:
diff changeset
  1947
// Notifies each observer that a topic has been added.
hgs
parents:
diff changeset
  1948
// (other items were commented in a header).
hgs
parents:
diff changeset
  1949
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  1950
//
hgs
parents:
diff changeset
  1951
void CCbsDbImpTopicList::NotifyTopicAddedL( 
hgs
parents:
diff changeset
  1952
    TCbsDbTopicNumber aNumber )
hgs
parents:
diff changeset
  1953
    {
hgs
parents:
diff changeset
  1954
    // Notify each observer.
hgs
parents:
diff changeset
  1955
    TInt count( iObservers->Count() );
hgs
parents:
diff changeset
  1956
    for ( TInt index( 0 ); index < count; index++ )
hgs
parents:
diff changeset
  1957
        {
hgs
parents:
diff changeset
  1958
        iObservers->At( index )->TopicAddedIndL( aNumber );
hgs
parents:
diff changeset
  1959
        }
hgs
parents:
diff changeset
  1960
    }
hgs
parents:
diff changeset
  1961
hgs
parents:
diff changeset
  1962
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  1963
// CCbsDbImpTopicList::NotifyTopicModifiedL
hgs
parents:
diff changeset
  1964
// Notifies each observer that a topic has been modified.
hgs
parents:
diff changeset
  1965
// (other items were commented in a header).
hgs
parents:
diff changeset
  1966
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  1967
//
hgs
parents:
diff changeset
  1968
void CCbsDbImpTopicList::NotifyTopicModifiedL( 
hgs
parents:
diff changeset
  1969
    TCbsDbTopicNumber aNumber )
hgs
parents:
diff changeset
  1970
    {
hgs
parents:
diff changeset
  1971
    // Notify each observer.
hgs
parents:
diff changeset
  1972
    TInt count( iObservers->Count() );
hgs
parents:
diff changeset
  1973
    for( TInt index( 0 ); index < count; index++ )
hgs
parents:
diff changeset
  1974
        {
hgs
parents:
diff changeset
  1975
        iObservers->At( index )->TopicModifiedIndL( aNumber );
hgs
parents:
diff changeset
  1976
        }
hgs
parents:
diff changeset
  1977
    }
hgs
parents:
diff changeset
  1978
hgs
parents:
diff changeset
  1979
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  1980
// CCbsDbImpTopicList::NotifyTopicDeletedL
hgs
parents:
diff changeset
  1981
// Notifies each observer that a topic has been deleted.
hgs
parents:
diff changeset
  1982
// (other items were commented in a header).
hgs
parents:
diff changeset
  1983
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  1984
//
hgs
parents:
diff changeset
  1985
void CCbsDbImpTopicList::NotifyTopicDeletedL( 
hgs
parents:
diff changeset
  1986
    TCbsDbTopicNumber aNumber )
hgs
parents:
diff changeset
  1987
    {
hgs
parents:
diff changeset
  1988
    // Notify each observer.
hgs
parents:
diff changeset
  1989
    TInt count( iObservers->Count() );
hgs
parents:
diff changeset
  1990
    for( TInt index( 0 ); index < count; index++ )
hgs
parents:
diff changeset
  1991
        {
hgs
parents:
diff changeset
  1992
        iObservers->At( index )->TopicDeletedIndL( aNumber );
hgs
parents:
diff changeset
  1993
        }
hgs
parents:
diff changeset
  1994
    }
hgs
parents:
diff changeset
  1995
hgs
parents:
diff changeset
  1996
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  1997
// CCbsDbImpTopicList::SetHotmarkedMessage
hgs
parents:
diff changeset
  1998
// Sets the hotmarked message handle.
hgs
parents:
diff changeset
  1999
// (other items were commented in a header).
hgs
parents:
diff changeset
  2000
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  2001
//
hgs
parents:
diff changeset
  2002
void CCbsDbImpTopicList::SetHotmarkedMessage( 
hgs
parents:
diff changeset
  2003
    const TCbsDbMessageHandle& aMessageHandle )
hgs
parents:
diff changeset
  2004
    {
hgs
parents:
diff changeset
  2005
    // Set the hotmarked message.
hgs
parents:
diff changeset
  2006
    iIsHotmarkedMessage = ETrue;
hgs
parents:
diff changeset
  2007
    iMessageHandle = aMessageHandle;
hgs
parents:
diff changeset
  2008
    }
hgs
parents:
diff changeset
  2009
hgs
parents:
diff changeset
  2010
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  2011
// CCbsDbImpTopicList::CreateDefaultRootStreamL
hgs
parents:
diff changeset
  2012
// Creates a root stream and writes default values into it. 
hgs
parents:
diff changeset
  2013
// (other items were commented in a header).
hgs
parents:
diff changeset
  2014
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  2015
//
hgs
parents:
diff changeset
  2016
TStreamId CCbsDbImpTopicList::CreateDefaultRootStreamL( 
hgs
parents:
diff changeset
  2017
    CStreamStore& aStore ) const
hgs
parents:
diff changeset
  2018
    {
hgs
parents:
diff changeset
  2019
    // Create the stream
hgs
parents:
diff changeset
  2020
    RStoreWriteStream outstream;
hgs
parents:
diff changeset
  2021
    TStreamId id( outstream.CreateLC( aStore ) ); // on CS
hgs
parents:
diff changeset
  2022
hgs
parents:
diff changeset
  2023
    // Write the amount of topic lists
hgs
parents:
diff changeset
  2024
    outstream.WriteInt16L( 0 );
hgs
parents:
diff changeset
  2025
hgs
parents:
diff changeset
  2026
    // Write space for topic list stream ids
hgs
parents:
diff changeset
  2027
    for ( TInt index( 0 ); index < KCbsRootItemsSize; index++ )
hgs
parents:
diff changeset
  2028
        {
hgs
parents:
diff changeset
  2029
        outstream << TStreamId( 0 );
hgs
parents:
diff changeset
  2030
        } 
hgs
parents:
diff changeset
  2031
hgs
parents:
diff changeset
  2032
    outstream.CommitL();
hgs
parents:
diff changeset
  2033
    CleanupStack::PopAndDestroy(); // aStore
hgs
parents:
diff changeset
  2034
hgs
parents:
diff changeset
  2035
    // Return the stream id.
hgs
parents:
diff changeset
  2036
    return id;
hgs
parents:
diff changeset
  2037
    }
hgs
parents:
diff changeset
  2038
hgs
parents:
diff changeset
  2039
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  2040
// CCbsDbImpTopicList::WriteTopicInformationL
hgs
parents:
diff changeset
  2041
// Writes topic data into a stream.
hgs
parents:
diff changeset
  2042
// This includes number of saved messages, 
hgs
parents:
diff changeset
  2043
// number of unread messages, topic handle, 
hgs
parents:
diff changeset
  2044
// topic name, topic number, protection status, 
hgs
parents:
diff changeset
  2045
// subscription status and hotmark status.
hgs
parents:
diff changeset
  2046
// (other items were commented in a header).
hgs
parents:
diff changeset
  2047
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  2048
//
hgs
parents:
diff changeset
  2049
void CCbsDbImpTopicList::WriteTopicInformationL( 
hgs
parents:
diff changeset
  2050
    RWriteStream& aOut, 
hgs
parents:
diff changeset
  2051
    const TCbsDbTopic& aTopic, 
hgs
parents:
diff changeset
  2052
    const TStreamId& aTopicMessagesId ) const
hgs
parents:
diff changeset
  2053
    {
hgs
parents:
diff changeset
  2054
    // Write saved messages.
hgs
parents:
diff changeset
  2055
    aOut.WriteInt16L( aTopic.iSavedMessages );
hgs
parents:
diff changeset
  2056
hgs
parents:
diff changeset
  2057
    // Write name.
hgs
parents:
diff changeset
  2058
    aOut << aTopic.iName;
hgs
parents:
diff changeset
  2059
    
hgs
parents:
diff changeset
  2060
    // Write topic number (message identifier)
hgs
parents:
diff changeset
  2061
    aOut.WriteInt16L( aTopic.iNumber );
hgs
parents:
diff changeset
  2062
hgs
parents:
diff changeset
  2063
    // Write statuses
hgs
parents:
diff changeset
  2064
    CbsStreamHelper::WriteBoolL( aOut, aTopic.iProtected ); // Protected
hgs
parents:
diff changeset
  2065
    CbsStreamHelper::WriteBoolL( aOut, aTopic.iSubscribed ); // Subscribed
hgs
parents:
diff changeset
  2066
    CbsStreamHelper::WriteBoolL( aOut, aTopic.iHotmarked ); // Hotmarked
hgs
parents:
diff changeset
  2067
    
hgs
parents:
diff changeset
  2068
    // Write unread messages count
hgs
parents:
diff changeset
  2069
    aOut.WriteInt16L( aTopic.iUnreadMessages );
hgs
parents:
diff changeset
  2070
hgs
parents:
diff changeset
  2071
    // And then write the topic messages stream id.
hgs
parents:
diff changeset
  2072
    aOut << aTopicMessagesId;
hgs
parents:
diff changeset
  2073
    }
hgs
parents:
diff changeset
  2074
hgs
parents:
diff changeset
  2075
hgs
parents:
diff changeset
  2076
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  2077
// CCbsDbImpTopicList::RevertFilesL
hgs
parents:
diff changeset
  2078
// Reverts all changes made to three datafiles handled by this class.
hgs
parents:
diff changeset
  2079
// (other items were commented in a header).
hgs
parents:
diff changeset
  2080
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  2081
//
hgs
parents:
diff changeset
  2082
void CCbsDbImpTopicList::RevertFilesL()
hgs
parents:
diff changeset
  2083
    {
hgs
parents:
diff changeset
  2084
hgs
parents:
diff changeset
  2085
    // Check for file lock
hgs
parents:
diff changeset
  2086
    CheckFileLockL();
hgs
parents:
diff changeset
  2087
hgs
parents:
diff changeset
  2088
    iTopicStore->Revert();   
hgs
parents:
diff changeset
  2089
    iUnsavedMessageStore->Revert();
hgs
parents:
diff changeset
  2090
    ReloadRootStreamL();
hgs
parents:
diff changeset
  2091
    }
hgs
parents:
diff changeset
  2092
hgs
parents:
diff changeset
  2093
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  2094
// CCbsDbImpTopicList::CommitFilesL
hgs
parents:
diff changeset
  2095
// Commits all changes made to two datafiles handled by this class.
hgs
parents:
diff changeset
  2096
// (other items were commented in a header).
hgs
parents:
diff changeset
  2097
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  2098
//
hgs
parents:
diff changeset
  2099
void CCbsDbImpTopicList::CommitFilesL()
hgs
parents:
diff changeset
  2100
    {
hgs
parents:
diff changeset
  2101
    TopicStoreL()->CommitL();
hgs
parents:
diff changeset
  2102
    TInt errorCode( iUnsavedMessageStore->Commit() );
hgs
parents:
diff changeset
  2103
hgs
parents:
diff changeset
  2104
    // If committing of the unsaved msg store fails, remove all
hgs
parents:
diff changeset
  2105
    // messages in the store and rebuild the topic list
hgs
parents:
diff changeset
  2106
    if ( errorCode != KErrNone )
hgs
parents:
diff changeset
  2107
        {
hgs
parents:
diff changeset
  2108
        TRAPD( errorCode2, RebuildUnsavedMessageStoreL() );
hgs
parents:
diff changeset
  2109
        if ( errorCode2 != KErrNone )
hgs
parents:
diff changeset
  2110
            {
hgs
parents:
diff changeset
  2111
            CActiveScheduler::Stop();
hgs
parents:
diff changeset
  2112
            User::Leave( KErrServerTerminated );
hgs
parents:
diff changeset
  2113
            }
hgs
parents:
diff changeset
  2114
hgs
parents:
diff changeset
  2115
        // Tell the caller that something went wrong
hgs
parents:
diff changeset
  2116
        User::Leave( errorCode );
hgs
parents:
diff changeset
  2117
        }
hgs
parents:
diff changeset
  2118
hgs
parents:
diff changeset
  2119
    // Check if we should compact
hgs
parents:
diff changeset
  2120
    TTime now;
hgs
parents:
diff changeset
  2121
    TTimeIntervalMinutes interval;
hgs
parents:
diff changeset
  2122
    now.UniversalTime();
hgs
parents:
diff changeset
  2123
    now.MinutesFrom( iPreviousCompact, interval );
hgs
parents:
diff changeset
  2124
    if ( interval.Int() >= KMinimumCompactInterval )
hgs
parents:
diff changeset
  2125
        {
hgs
parents:
diff changeset
  2126
        TopicStoreL()->CompactL();
hgs
parents:
diff changeset
  2127
        iUnsavedMessageStore->CompactL();
hgs
parents:
diff changeset
  2128
        iPreviousCompact = now;
hgs
parents:
diff changeset
  2129
        }
hgs
parents:
diff changeset
  2130
hgs
parents:
diff changeset
  2131
    }
hgs
parents:
diff changeset
  2132
hgs
parents:
diff changeset
  2133
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  2134
// CCbsDbImpTopicList::RebuildUnsavedMessageStoreL
hgs
parents:
diff changeset
  2135
// Deletes the unsaved messages' store and rebuilds it.
hgs
parents:
diff changeset
  2136
// (other items were commented in a header).
hgs
parents:
diff changeset
  2137
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  2138
//
hgs
parents:
diff changeset
  2139
void CCbsDbImpTopicList::RebuildUnsavedMessageStoreL() 
hgs
parents:
diff changeset
  2140
    {
hgs
parents:
diff changeset
  2141
    // Close the stores and delete the unsaved store
hgs
parents:
diff changeset
  2142
    delete iUnsavedMessageStore; iUnsavedMessageStore = NULL;
hgs
parents:
diff changeset
  2143
    delete iTopicStore; iTopicStore = NULL;
hgs
parents:
diff changeset
  2144
    CbsUtils::DeleteFileL( iFs, *iUnsavedMessagesFilename );
hgs
parents:
diff changeset
  2145
hgs
parents:
diff changeset
  2146
    // Re-create the store
hgs
parents:
diff changeset
  2147
    DoCreateStoreL( *iUnsavedMessagesFilename );
hgs
parents:
diff changeset
  2148
    TryToOpenFilesL( ETrue, EFalse );
hgs
parents:
diff changeset
  2149
hgs
parents:
diff changeset
  2150
    // Remove the stream ids to unsaved messages, because
hgs
parents:
diff changeset
  2151
    // they were all just deleted.
hgs
parents:
diff changeset
  2152
    HandleDeletionOfUnsavedMessagesFileL();
hgs
parents:
diff changeset
  2153
hgs
parents:
diff changeset
  2154
    // Compact the topic list
hgs
parents:
diff changeset
  2155
    TopicStoreL()->CommitL();
hgs
parents:
diff changeset
  2156
    TopicStoreL()->CompactL();
hgs
parents:
diff changeset
  2157
    }
hgs
parents:
diff changeset
  2158
hgs
parents:
diff changeset
  2159
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  2160
// CCbsDbImpTopicList::RebuildTopicAndUnsavedStoresL
hgs
parents:
diff changeset
  2161
// Deletes and rebuilds topic/topic list and unsaved message stores.
hgs
parents:
diff changeset
  2162
// Loads Standard Topic List into memory.
hgs
parents:
diff changeset
  2163
// (other items were commented in a header).
hgs
parents:
diff changeset
  2164
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  2165
//
hgs
parents:
diff changeset
  2166
void CCbsDbImpTopicList::RebuildTopicAndUnsavedStoresL()
hgs
parents:
diff changeset
  2167
    {
hgs
parents:
diff changeset
  2168
    __TEST_INVARIANT;
hgs
parents:
diff changeset
  2169
hgs
parents:
diff changeset
  2170
    // Check for file lock
hgs
parents:
diff changeset
  2171
    CheckFileLockL();
hgs
parents:
diff changeset
  2172
hgs
parents:
diff changeset
  2173
    // About to write to FFS: make critical level check
hgs
parents:
diff changeset
  2174
    CbsUtils::FFSCriticalLevelCheckL( KTopicListRootStreamSize + 
hgs
parents:
diff changeset
  2175
        KDefaultTopicListSize * 2, iFs );
hgs
parents:
diff changeset
  2176
    
hgs
parents:
diff changeset
  2177
    if ( iMessages != NULL && iMessages->IsLockedMessages() )
hgs
parents:
diff changeset
  2178
        {
hgs
parents:
diff changeset
  2179
        User::Leave( KErrAccessDenied );
hgs
parents:
diff changeset
  2180
        }
hgs
parents:
diff changeset
  2181
hgs
parents:
diff changeset
  2182
    delete iTopicStore; 
hgs
parents:
diff changeset
  2183
    iTopicStore = NULL;
hgs
parents:
diff changeset
  2184
    delete iUnsavedMessageStore; 
hgs
parents:
diff changeset
  2185
    iUnsavedMessageStore = NULL;
hgs
parents:
diff changeset
  2186
    CbsUtils::DeleteFileL( iFs, *iTopicsFilename  );
hgs
parents:
diff changeset
  2187
    CbsUtils::DeleteFileL( iFs, *iUnsavedMessagesFilename );
hgs
parents:
diff changeset
  2188
hgs
parents:
diff changeset
  2189
    // Create new files.
hgs
parents:
diff changeset
  2190
    OpenFilesL( ETrue, EFalse );
hgs
parents:
diff changeset
  2191
hgs
parents:
diff changeset
  2192
    // Add standard index topic.
hgs
parents:
diff changeset
  2193
    AddIndexTopicL();
hgs
parents:
diff changeset
  2194
    
hgs
parents:
diff changeset
  2195
    // Load the Standard Topic list
hgs
parents:
diff changeset
  2196
    LoadDefaultTopicStreamL();
hgs
parents:
diff changeset
  2197
hgs
parents:
diff changeset
  2198
    iIsHotmarkedMessage = EFalse;
hgs
parents:
diff changeset
  2199
    iLastTopicNumber = 0;
hgs
parents:
diff changeset
  2200
    iMessageHandle = 0;
hgs
parents:
diff changeset
  2201
hgs
parents:
diff changeset
  2202
    // Inform the message manager.
hgs
parents:
diff changeset
  2203
    if ( iMessages )
hgs
parents:
diff changeset
  2204
        {
hgs
parents:
diff changeset
  2205
        iMessages->InvalidateCache();
hgs
parents:
diff changeset
  2206
        }
hgs
parents:
diff changeset
  2207
hgs
parents:
diff changeset
  2208
    // Notify everyone.
hgs
parents:
diff changeset
  2209
    NotifyTopicListInitializedL();
hgs
parents:
diff changeset
  2210
hgs
parents:
diff changeset
  2211
    __TEST_INVARIANT;
hgs
parents:
diff changeset
  2212
    }
hgs
parents:
diff changeset
  2213
hgs
parents:
diff changeset
  2214
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  2215
// CCbsDbImpTopicList::GetLatestTopicNumber
hgs
parents:
diff changeset
  2216
// Returns the number of the topic that was added last
hgs
parents:
diff changeset
  2217
// to the database by topic detection feature.
hgs
parents:
diff changeset
  2218
// (other items were commented in a header).
hgs
parents:
diff changeset
  2219
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  2220
//
hgs
parents:
diff changeset
  2221
TInt CCbsDbImpTopicList::GetLatestTopicNumber( 
hgs
parents:
diff changeset
  2222
    TCbsTopicNumber& aNumber ) const
hgs
parents:
diff changeset
  2223
    {
hgs
parents:
diff changeset
  2224
    TInt result( KErrNone );
hgs
parents:
diff changeset
  2225
    if ( iLastTopicNumber == 0 )
hgs
parents:
diff changeset
  2226
        {
hgs
parents:
diff changeset
  2227
        result = KErrNotFound;
hgs
parents:
diff changeset
  2228
        }
hgs
parents:
diff changeset
  2229
    aNumber = iLastTopicNumber;
hgs
parents:
diff changeset
  2230
    return result;
hgs
parents:
diff changeset
  2231
    }
hgs
parents:
diff changeset
  2232
hgs
parents:
diff changeset
  2233
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  2234
// CCbsDbImpTopicList::OpenFilesL
hgs
parents:
diff changeset
  2235
// After a call to this function, the file stores can be assumed
hgs
parents:
diff changeset
  2236
// to be open and initialized.
hgs
parents:
diff changeset
  2237
// (other items were commented in a header).
hgs
parents:
diff changeset
  2238
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  2239
//
hgs
parents:
diff changeset
  2240
void CCbsDbImpTopicList::OpenFilesL( 
hgs
parents:
diff changeset
  2241
    TBool aDeleteExistingFiles,
hgs
parents:
diff changeset
  2242
    TBool aCreateNewTopicList )
hgs
parents:
diff changeset
  2243
    {
hgs
parents:
diff changeset
  2244
    __ASSERT_DEBUG( iTopicsFilename->Length() > 0, 
hgs
parents:
diff changeset
  2245
        CbsServerPanic( ECbsInvalidFilenameDescriptor ) );
hgs
parents:
diff changeset
  2246
    __ASSERT_DEBUG( iUnsavedMessagesFilename->Length() > 0, 
hgs
parents:
diff changeset
  2247
        CbsServerPanic( ECbsInvalidFilenameDescriptor ) );
hgs
parents:
diff changeset
  2248
hgs
parents:
diff changeset
  2249
    // if LFS, delete files, create files, open files, write root stream
hgs
parents:
diff changeset
  2250
    // if not LFS, create file if it doesn't exist, open files
hgs
parents:
diff changeset
  2251
hgs
parents:
diff changeset
  2252
    // Close file stores.
hgs
parents:
diff changeset
  2253
    delete iTopicStore; 
hgs
parents:
diff changeset
  2254
    iTopicStore = NULL;
hgs
parents:
diff changeset
  2255
    delete iUnsavedMessageStore; 
hgs
parents:
diff changeset
  2256
    iUnsavedMessageStore = NULL;
hgs
parents:
diff changeset
  2257
hgs
parents:
diff changeset
  2258
    // If any of files doesn't exist, create the file. Also writes the root 
hgs
parents:
diff changeset
  2259
    // stream of the topic file, if necessary.
hgs
parents:
diff changeset
  2260
    // It is possible that this operation fails because FFS is full.
hgs
parents:
diff changeset
  2261
    // In this case the server will take a break and retry. If the second
hgs
parents:
diff changeset
  2262
    // attempt fails, the server is shut down.
hgs
parents:
diff changeset
  2263
    TBool unsavedMsgFileExists( EFalse );
hgs
parents:
diff changeset
  2264
    TRAPD( result, CreateFilesIfNecessaryL( unsavedMsgFileExists ) );
hgs
parents:
diff changeset
  2265
    if ( result != KErrNone )
hgs
parents:
diff changeset
  2266
        {
hgs
parents:
diff changeset
  2267
        // Critical exception: wait for a while and retry.
hgs
parents:
diff changeset
  2268
        User::After( KWaitAfterCriticalStoreException );
hgs
parents:
diff changeset
  2269
        TBool ignoreThis( EFalse ); // value of unsavedMsgFileExists preserved
hgs
parents:
diff changeset
  2270
        TRAP( result, CreateFilesIfNecessaryL( ignoreThis ) );
hgs
parents:
diff changeset
  2271
        if ( result != KErrNone )
hgs
parents:
diff changeset
  2272
            {
hgs
parents:
diff changeset
  2273
            __DEBUGGER();
hgs
parents:
diff changeset
  2274
            // Recovery is not possible: shut the server down.
hgs
parents:
diff changeset
  2275
            CActiveScheduler::Stop();
hgs
parents:
diff changeset
  2276
            User::Leave( KErrServerTerminated );
hgs
parents:
diff changeset
  2277
            }
hgs
parents:
diff changeset
  2278
        }
hgs
parents:
diff changeset
  2279
hgs
parents:
diff changeset
  2280
    // Open the files for use. Also reads the topic file root stream.
hgs
parents:
diff changeset
  2281
    TryToOpenFilesL( unsavedMsgFileExists == EFalse && 
hgs
parents:
diff changeset
  2282
                     aDeleteExistingFiles == EFalse, 
hgs
parents:
diff changeset
  2283
                     aCreateNewTopicList );
hgs
parents:
diff changeset
  2284
hgs
parents:
diff changeset
  2285
    }
hgs
parents:
diff changeset
  2286
hgs
parents:
diff changeset
  2287
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  2288
// CCbsDbImpTopicList::CreateFilesIfNecessaryL
hgs
parents:
diff changeset
  2289
// Creates CBS files, if appropriate.
hgs
parents:
diff changeset
  2290
// (other items were commented in a header).
hgs
parents:
diff changeset
  2291
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  2292
//
hgs
parents:
diff changeset
  2293
void CCbsDbImpTopicList::CreateFilesIfNecessaryL( 
hgs
parents:
diff changeset
  2294
    TBool& aUnsavedMsgFileExisted )
hgs
parents:
diff changeset
  2295
    {    
hgs
parents:
diff changeset
  2296
    if ( CbsUtils::ExistsL( iFs, *iTopicsFilename ) == EFalse )
hgs
parents:
diff changeset
  2297
        {
hgs
parents:
diff changeset
  2298
        CbsUtils::FFSCriticalLevelCheckL( KTopicListRootStreamSize + 
hgs
parents:
diff changeset
  2299
            KDefaultTopicListSize * 2, iFs );
hgs
parents:
diff changeset
  2300
   
hgs
parents:
diff changeset
  2301
        CPermanentFileStore* store = CPermanentFileStore::CreateLC( iFs,
hgs
parents:
diff changeset
  2302
            *iTopicsFilename, EFileWrite ); // on CS
hgs
parents:
diff changeset
  2303
    
hgs
parents:
diff changeset
  2304
        store->SetTypeL( store->Layout() );
hgs
parents:
diff changeset
  2305
        TStreamId id( CreateDefaultRootStreamL( *store ) );
hgs
parents:
diff changeset
  2306
        store->SetRootL( id );
hgs
parents:
diff changeset
  2307
        store->CommitL();
hgs
parents:
diff changeset
  2308
        CleanupStack::PopAndDestroy(); // store
hgs
parents:
diff changeset
  2309
        }    
hgs
parents:
diff changeset
  2310
hgs
parents:
diff changeset
  2311
    aUnsavedMsgFileExisted = CbsUtils::ExistsL( iFs, 
hgs
parents:
diff changeset
  2312
        *iUnsavedMessagesFilename );
hgs
parents:
diff changeset
  2313
    if ( aUnsavedMsgFileExisted == EFalse )
hgs
parents:
diff changeset
  2314
        {
hgs
parents:
diff changeset
  2315
        // No critical level check, because unsaved msgs reside on ramdisk
hgs
parents:
diff changeset
  2316
        DoCreateStoreL( *iUnsavedMessagesFilename );
hgs
parents:
diff changeset
  2317
        }
hgs
parents:
diff changeset
  2318
    }
hgs
parents:
diff changeset
  2319
hgs
parents:
diff changeset
  2320
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  2321
// CCbsDbImpTopicList::DoCreateStoreL
hgs
parents:
diff changeset
  2322
// Creates an empty file store with the given filename.
hgs
parents:
diff changeset
  2323
// (other items were commented in a header).
hgs
parents:
diff changeset
  2324
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  2325
//
hgs
parents:
diff changeset
  2326
void CCbsDbImpTopicList::DoCreateStoreL( 
hgs
parents:
diff changeset
  2327
    const TDesC& aFilename )
hgs
parents:
diff changeset
  2328
    {
hgs
parents:
diff changeset
  2329
    CFileStore* store = CPermanentFileStore::CreateLC( iFs, 
hgs
parents:
diff changeset
  2330
        aFilename, EFileWrite ); // on CS
hgs
parents:
diff changeset
  2331
    store->SetTypeL( store->Layout() );
hgs
parents:
diff changeset
  2332
    store->CommitL();
hgs
parents:
diff changeset
  2333
    CleanupStack::PopAndDestroy();
hgs
parents:
diff changeset
  2334
    }
hgs
parents:
diff changeset
  2335
hgs
parents:
diff changeset
  2336
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  2337
// CCbsDbImpTopicList::TryToOpenFilesL
hgs
parents:
diff changeset
  2338
// Tries to open topic and unsaved messages files.
hgs
parents:
diff changeset
  2339
// (other items were commented in a header).
hgs
parents:
diff changeset
  2340
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  2341
//
hgs
parents:
diff changeset
  2342
void CCbsDbImpTopicList::TryToOpenFilesL( 
hgs
parents:
diff changeset
  2343
    TBool aDeleteUnsavedMsgStreamIds,
hgs
parents:
diff changeset
  2344
    TBool aCreateNewTopicList )
hgs
parents:
diff changeset
  2345
    {
hgs
parents:
diff changeset
  2346
    CBSLOGSTRING("CBSSERVER: >>> CCbsDbImpTopicList::TryToOpenFilesL()" );
hgs
parents:
diff changeset
  2347
    
hgs
parents:
diff changeset
  2348
    if ( !iTopicStore )
hgs
parents:
diff changeset
  2349
        {
hgs
parents:
diff changeset
  2350
        CBSLOGSTRING("CBSSERVER: CCbsDbImpTopicList::TryToOpenFilesL(): *** NO iTopicStore (1) ***" );    
hgs
parents:
diff changeset
  2351
        }
hgs
parents:
diff changeset
  2352
    
hgs
parents:
diff changeset
  2353
    // Try to open the store    
hgs
parents:
diff changeset
  2354
    TRAPD( error, iTopicStore = CFileStore::OpenL( iFs, *iTopicsFilename, EFileRead | EFileWrite ) );
hgs
parents:
diff changeset
  2355
    CBSLOGSTRING2("CBSSERVER: CCbsDbImpTopicList::TryToOpenFilesL(); iTopicStore OpenL() error: %d", error );
hgs
parents:
diff changeset
  2356
    
hgs
parents:
diff changeset
  2357
    TRAPD( error2, iUnsavedMessageStore = CFileStore::OpenL( iFs, *iUnsavedMessagesFilename, EFileRead | EFileWrite ) );
hgs
parents:
diff changeset
  2358
    CBSLOGSTRING2("CBSSERVER: CCbsDbImpTopicList::TryToOpenFilesL(); iUnsavedMessageStore OpenL() error: %d", error2 );
hgs
parents:
diff changeset
  2359
       
hgs
parents:
diff changeset
  2360
    if ( error || error2 )
hgs
parents:
diff changeset
  2361
        {
hgs
parents:
diff changeset
  2362
        CBSLOGSTRING("CBSSERVER: CCbsDbImpTopicList::TryToOpenFilesL(): Calling InitializeListL( ETrue )..." );
hgs
parents:
diff changeset
  2363
        
hgs
parents:
diff changeset
  2364
        InitializeListL( ETrue );
hgs
parents:
diff changeset
  2365
        
hgs
parents:
diff changeset
  2366
        CBSLOGSTRING("CBSSERVER: CCbsDbImpTopicList::TryToOpenFilesL(): Calling InitializeListL( ETrue ) finished." );
hgs
parents:
diff changeset
  2367
        }
hgs
parents:
diff changeset
  2368
    else
hgs
parents:
diff changeset
  2369
        {
hgs
parents:
diff changeset
  2370
        if ( iTopicLists->Count() == 0 && 
hgs
parents:
diff changeset
  2371
            aDeleteUnsavedMsgStreamIds && 
hgs
parents:
diff changeset
  2372
            aCreateNewTopicList )
hgs
parents:
diff changeset
  2373
            {
hgs
parents:
diff changeset
  2374
            // Create first topic list, since it was deleted with the file
hgs
parents:
diff changeset
  2375
            CreateStandardTopicListL();
hgs
parents:
diff changeset
  2376
            }
hgs
parents:
diff changeset
  2377
                                               
hgs
parents:
diff changeset
  2378
        // Load the root stream for topic store.
hgs
parents:
diff changeset
  2379
        LoadRootStreamL();
hgs
parents:
diff changeset
  2380
hgs
parents:
diff changeset
  2381
        if ( aDeleteUnsavedMsgStreamIds )
hgs
parents:
diff changeset
  2382
            {
hgs
parents:
diff changeset
  2383
            // Load the topics and repair the topic file 
hgs
parents:
diff changeset
  2384
            // since unsaved msg file has been deleted
hgs
parents:
diff changeset
  2385
            LoadDefaultTopicStreamL();
hgs
parents:
diff changeset
  2386
            HandleDeletionOfUnsavedMessagesFileL();
hgs
parents:
diff changeset
  2387
            }
hgs
parents:
diff changeset
  2388
        }
hgs
parents:
diff changeset
  2389
    CBSLOGSTRING("CBSSERVER: <<< CCbsDbImpTopicList::TryToOpenFilesL()" );
hgs
parents:
diff changeset
  2390
    }
hgs
parents:
diff changeset
  2391
hgs
parents:
diff changeset
  2392
// ---------------------------------------------------------
hgs
parents:
diff changeset
  2393
// CCbsDbImpTopicList::ReadIndexTopicNameLC()
hgs
parents:
diff changeset
  2394
// Reads the localized index topic name
hgs
parents:
diff changeset
  2395
// (other items were commented in a header).
hgs
parents:
diff changeset
  2396
// ---------------------------------------------------------
hgs
parents:
diff changeset
  2397
HBufC* CCbsDbImpTopicList::ReadIndexTopicNameLC()
hgs
parents:
diff changeset
  2398
    {
hgs
parents:
diff changeset
  2399
    // Open localized resource file.
hgs
parents:
diff changeset
  2400
    RResourceFile resourceFile;
hgs
parents:
diff changeset
  2401
hgs
parents:
diff changeset
  2402
    CbsUtils::FindAndOpenDefaultResourceFileLC( iFs, resourceFile ); // on CS
hgs
parents:
diff changeset
  2403
    // Read "Index"-string.
hgs
parents:
diff changeset
  2404
    TResourceReader reader;
hgs
parents:
diff changeset
  2405
    reader.SetBuffer( resourceFile.AllocReadLC( R_TEXT_INDEX_TOPIC ) );//on CS
hgs
parents:
diff changeset
  2406
    HBufC* text = reader.ReadHBufCL();
hgs
parents:
diff changeset
  2407
    CleanupStack::PopAndDestroy(2); // readerBuf, resourceFile
hgs
parents:
diff changeset
  2408
    CleanupStack::PushL( text );
hgs
parents:
diff changeset
  2409
hgs
parents:
diff changeset
  2410
    return text;
hgs
parents:
diff changeset
  2411
    }
hgs
parents:
diff changeset
  2412
hgs
parents:
diff changeset
  2413
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  2414
// CCbsDbImpTopicList::AddIndexTopicL
hgs
parents:
diff changeset
  2415
// Adds index topic to topic list.
hgs
parents:
diff changeset
  2416
// (other items were commented in a header).
hgs
parents:
diff changeset
  2417
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  2418
//
hgs
parents:
diff changeset
  2419
void CCbsDbImpTopicList::AddIndexTopicL()
hgs
parents:
diff changeset
  2420
    {
hgs
parents:
diff changeset
  2421
    // Open localized resource file.
hgs
parents:
diff changeset
  2422
    HBufC* text = ReadIndexTopicNameLC();
hgs
parents:
diff changeset
  2423
hgs
parents:
diff changeset
  2424
    TCbsDbTopic topic;
hgs
parents:
diff changeset
  2425
    topic.iName.Copy( *text );
hgs
parents:
diff changeset
  2426
    topic.iNumber = 0;
hgs
parents:
diff changeset
  2427
    topic.iHotmarked = EFalse;
hgs
parents:
diff changeset
  2428
    topic.iProtected = ETrue;
hgs
parents:
diff changeset
  2429
    topic.iSubscribed = ETrue;
hgs
parents:
diff changeset
  2430
    topic.iSavedMessages = 0;
hgs
parents:
diff changeset
  2431
    topic.iUnreadMessages = 0;
hgs
parents:
diff changeset
  2432
hgs
parents:
diff changeset
  2433
    // Add the topic without notifying anybody.
hgs
parents:
diff changeset
  2434
    TRAPD( error, DoAddTopicL( topic ) );
hgs
parents:
diff changeset
  2435
    if ( error != KErrNone )
hgs
parents:
diff changeset
  2436
        {
hgs
parents:
diff changeset
  2437
        RevertFilesL();
hgs
parents:
diff changeset
  2438
        __TEST_INVARIANT;
hgs
parents:
diff changeset
  2439
        User::Leave( error );
hgs
parents:
diff changeset
  2440
        }
hgs
parents:
diff changeset
  2441
hgs
parents:
diff changeset
  2442
    CleanupStack::PopAndDestroy();  // text
hgs
parents:
diff changeset
  2443
    }
hgs
parents:
diff changeset
  2444
hgs
parents:
diff changeset
  2445
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  2446
// CCbsDbImpTopicList::HandleDeletionOfUnsavedMessagesFileL
hgs
parents:
diff changeset
  2447
// Called to repair the database when Unsaved messages -file
hgs
parents:
diff changeset
  2448
// has been deleted, but Topics-file still contains information
hgs
parents:
diff changeset
  2449
// on unsaved messages.
hgs
parents:
diff changeset
  2450
//  
hgs
parents:
diff changeset
  2451
// Things to do here: 
hgs
parents:
diff changeset
  2452
//      - Reset unread message counters, because a saved message
hgs
parents:
diff changeset
  2453
//        is always also read.
hgs
parents:
diff changeset
  2454
//      - Remove stream ids to unsaved messages from 
hgs
parents:
diff changeset
  2455
//        Topic messages -streams.
hgs
parents:
diff changeset
  2456
// (other items were commented in a header).
hgs
parents:
diff changeset
  2457
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  2458
//
hgs
parents:
diff changeset
  2459
void CCbsDbImpTopicList::HandleDeletionOfUnsavedMessagesFileL()
hgs
parents:
diff changeset
  2460
    {
hgs
parents:
diff changeset
  2461
    TRAPD( result, DoHandleDeletionOfUnsavedMessagesFileL() );
hgs
parents:
diff changeset
  2462
    if ( result != KErrNone )
hgs
parents:
diff changeset
  2463
        {
hgs
parents:
diff changeset
  2464
        // Recovery impossible -> reset the database.
hgs
parents:
diff changeset
  2465
        // Note that this function is not called when the database
hgs
parents:
diff changeset
  2466
        // is initialized.
hgs
parents:
diff changeset
  2467
        RebuildTopicAndUnsavedStoresL();
hgs
parents:
diff changeset
  2468
        }
hgs
parents:
diff changeset
  2469
    }
hgs
parents:
diff changeset
  2470
hgs
parents:
diff changeset
  2471
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  2472
// CCbsDbImpTopicList::DoHandleDeletionOfUnsavedMessagesFileL
hgs
parents:
diff changeset
  2473
// Does the work for HandleDeletionOfUnsavedMessagesFileL().
hgs
parents:
diff changeset
  2474
// (other items were commented in a header).
hgs
parents:
diff changeset
  2475
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  2476
//
hgs
parents:
diff changeset
  2477
void CCbsDbImpTopicList::DoHandleDeletionOfUnsavedMessagesFileL()
hgs
parents:
diff changeset
  2478
    {
hgs
parents:
diff changeset
  2479
    // Reset the unread counter of each topic and delete stream ids of 
hgs
parents:
diff changeset
  2480
    // unsaved messages.
hgs
parents:
diff changeset
  2481
    TInt numberOfTopics( iTopics->Count() );
hgs
parents:
diff changeset
  2482
    for ( TInt i( 0 ); i < numberOfTopics; i++ )
hgs
parents:
diff changeset
  2483
        {
hgs
parents:
diff changeset
  2484
        TCbsDbTopic topic;
hgs
parents:
diff changeset
  2485
        FindTopicByNumberL( iTopics->At( i ).iTopicData.iNumber, topic );
hgs
parents:
diff changeset
  2486
        topic.iUnreadMessages = 0;
hgs
parents:
diff changeset
  2487
        DoUpdateTopicL( topic, EFalse, i, EFalse );
hgs
parents:
diff changeset
  2488
        DeleteUnsavedMessageStreamIdsL( iTopics->At( i ).iTopicMessagesId );
hgs
parents:
diff changeset
  2489
        }
hgs
parents:
diff changeset
  2490
hgs
parents:
diff changeset
  2491
    UpdateTopicListStreamL( iCurrentTopicList, ETrue );
hgs
parents:
diff changeset
  2492
    TopicStoreL()->CommitL();
hgs
parents:
diff changeset
  2493
    }
hgs
parents:
diff changeset
  2494
hgs
parents:
diff changeset
  2495
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  2496
// CCbsDbImpTopicList::DeleteUnsavedMessageStreamIdsL
hgs
parents:
diff changeset
  2497
// This function is called when the unsaved messages file has
hgs
parents:
diff changeset
  2498
// been deleted but the Topic messages -stream still contains
hgs
parents:
diff changeset
  2499
// stream ids to deleted message streams.
hgs
parents:
diff changeset
  2500
// All stream ids found in the given topic messages -stream 
hgs
parents:
diff changeset
  2501
// pointing to the unsaved messages file are removed.
hgs
parents:
diff changeset
  2502
// (other items were commented in a header).
hgs
parents:
diff changeset
  2503
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  2504
//
hgs
parents:
diff changeset
  2505
void CCbsDbImpTopicList::DeleteUnsavedMessageStreamIdsL( 
hgs
parents:
diff changeset
  2506
    const TStreamId& aMsgStreamId ) const
hgs
parents:
diff changeset
  2507
    {
hgs
parents:
diff changeset
  2508
    CArrayFixFlat< TStreamId >* streamIds = 
hgs
parents:
diff changeset
  2509
        new ( ELeave ) CArrayFixFlat< TStreamId >( KTypicalNumberOfTopicMessages );
hgs
parents:
diff changeset
  2510
    CleanupStack::PushL( streamIds );
hgs
parents:
diff changeset
  2511
hgs
parents:
diff changeset
  2512
    RStoreReadStream instream;
hgs
parents:
diff changeset
  2513
    instream.OpenLC( *TopicStoreL(), aMsgStreamId ); // on CS
hgs
parents:
diff changeset
  2514
hgs
parents:
diff changeset
  2515
    // Read msg count
hgs
parents:
diff changeset
  2516
    TInt numberOfMsgs( instream.ReadInt16L() );
hgs
parents:
diff changeset
  2517
hgs
parents:
diff changeset
  2518
    // Read max msg count
hgs
parents:
diff changeset
  2519
    TInt maxNumberOfMsgs( instream.ReadInt16L() );
hgs
parents:
diff changeset
  2520
hgs
parents:
diff changeset
  2521
    TInt index( 0 );
hgs
parents:
diff changeset
  2522
hgs
parents:
diff changeset
  2523
    for ( ; index < numberOfMsgs; index++ )
hgs
parents:
diff changeset
  2524
        {
hgs
parents:
diff changeset
  2525
        TBool saved( CbsStreamHelper::ReadBoolL( instream ) );
hgs
parents:
diff changeset
  2526
        TStreamId id( 0 );
hgs
parents:
diff changeset
  2527
        instream >> id;
hgs
parents:
diff changeset
  2528
        if ( saved )
hgs
parents:
diff changeset
  2529
            {
hgs
parents:
diff changeset
  2530
            streamIds->AppendL( id );
hgs
parents:
diff changeset
  2531
            }
hgs
parents:
diff changeset
  2532
        }
hgs
parents:
diff changeset
  2533
hgs
parents:
diff changeset
  2534
    CleanupStack::PopAndDestroy();  // instream
hgs
parents:
diff changeset
  2535
hgs
parents:
diff changeset
  2536
    // Write stream ids of saved messages into the new topic message stream
hgs
parents:
diff changeset
  2537
    // which replaces the stream identified with aMsgStreamId.
hgs
parents:
diff changeset
  2538
    RStoreWriteStream outstream;    
hgs
parents:
diff changeset
  2539
    outstream.ReplaceLC( *TopicStoreL(), aMsgStreamId ); // on CS
hgs
parents:
diff changeset
  2540
hgs
parents:
diff changeset
  2541
    TInt numberOfSavedmessages( streamIds->Count() ); 
hgs
parents:
diff changeset
  2542
hgs
parents:
diff changeset
  2543
    // Number of messages = number of saved messages
hgs
parents:
diff changeset
  2544
    outstream.WriteInt16L( numberOfSavedmessages );
hgs
parents:
diff changeset
  2545
hgs
parents:
diff changeset
  2546
    // Number of saved messages <= number of messages <= maxNumberOfMsgs
hgs
parents:
diff changeset
  2547
    outstream.WriteInt16L( maxNumberOfMsgs );
hgs
parents:
diff changeset
  2548
hgs
parents:
diff changeset
  2549
    for ( index = 0; index < numberOfSavedmessages; index++ )
hgs
parents:
diff changeset
  2550
        {
hgs
parents:
diff changeset
  2551
        // All messages are saved (i.e., permanent)
hgs
parents:
diff changeset
  2552
        CbsStreamHelper::WriteBoolL( outstream, ETrue );
hgs
parents:
diff changeset
  2553
        outstream << streamIds->At( index );
hgs
parents:
diff changeset
  2554
        }
hgs
parents:
diff changeset
  2555
hgs
parents:
diff changeset
  2556
    for ( ; index < maxNumberOfMsgs; index ++ )
hgs
parents:
diff changeset
  2557
        {
hgs
parents:
diff changeset
  2558
        CbsStreamHelper::WriteBoolL( outstream, EFalse );
hgs
parents:
diff changeset
  2559
        outstream << TStreamId( 0 );
hgs
parents:
diff changeset
  2560
        }
hgs
parents:
diff changeset
  2561
hgs
parents:
diff changeset
  2562
    outstream.CommitL();
hgs
parents:
diff changeset
  2563
    CleanupStack::PopAndDestroy(2);  // outstream, streamIds
hgs
parents:
diff changeset
  2564
    
hgs
parents:
diff changeset
  2565
    }
hgs
parents:
diff changeset
  2566
hgs
parents:
diff changeset
  2567
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  2568
// CCbsDbImpTopicList::UpdateTopicCountersL
hgs
parents:
diff changeset
  2569
// Resolves the topic position in topic list and uses this information
hgs
parents:
diff changeset
  2570
// to call DoUpdateTopicL.       
hgs
parents:
diff changeset
  2571
// Changes to stores are NOT commited.
hgs
parents:
diff changeset
  2572
// Call to DoUpdateTopicL is not trapped.
hgs
parents:
diff changeset
  2573
// (other items were commented in a header).
hgs
parents:
diff changeset
  2574
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  2575
//
hgs
parents:
diff changeset
  2576
void CCbsDbImpTopicList::UpdateTopicCountersL( 
hgs
parents:
diff changeset
  2577
    const TCbsDbTopic& aTopic,
hgs
parents:
diff changeset
  2578
    const TBool aDeleting )
hgs
parents:
diff changeset
  2579
    {
hgs
parents:
diff changeset
  2580
    CBSLOGSTRING("CBSSERVER: >>> CCbsDbImpTopicList::UpdateTopicCountersL()");
hgs
parents:
diff changeset
  2581
    
hgs
parents:
diff changeset
  2582
    // Find out the position of the topic in topic list.    
hgs
parents:
diff changeset
  2583
    TInt index( TopicIndexInList( aTopic.iNumber ) );
hgs
parents:
diff changeset
  2584
    if ( index == KErrNotFound )
hgs
parents:
diff changeset
  2585
        {
hgs
parents:
diff changeset
  2586
        User::Leave( KErrNotFound );
hgs
parents:
diff changeset
  2587
        }   
hgs
parents:
diff changeset
  2588
hgs
parents:
diff changeset
  2589
    // DoUpdateTopicL leaves changes uncommited. EFalse for not having to
hgs
parents:
diff changeset
  2590
    // change topic position in topic list.
hgs
parents:
diff changeset
  2591
    DoUpdateTopicL( aTopic, EFalse, index, aDeleting );
hgs
parents:
diff changeset
  2592
    
hgs
parents:
diff changeset
  2593
    CBSLOGSTRING("CBSSERVER: <<< CCbsDbImpTopicList::UpdateTopicCountersL()");
hgs
parents:
diff changeset
  2594
    }
hgs
parents:
diff changeset
  2595
hgs
parents:
diff changeset
  2596
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  2597
// CCbsDbImpTopicList::CreateStandardTopicListL
hgs
parents:
diff changeset
  2598
// Creates the Standard topic list (topic list no. 0)
hgs
parents:
diff changeset
  2599
// (other items were commented in a header).
hgs
parents:
diff changeset
  2600
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  2601
//
hgs
parents:
diff changeset
  2602
void CCbsDbImpTopicList::CreateStandardTopicListL()
hgs
parents:
diff changeset
  2603
    {
hgs
parents:
diff changeset
  2604
    // Read standard topic list name from the resource file
hgs
parents:
diff changeset
  2605
    RResourceFile resourceFile;
hgs
parents:
diff changeset
  2606
    CbsUtils::FindAndOpenDefaultResourceFileLC( iFs, resourceFile ); // on CS
hgs
parents:
diff changeset
  2607
hgs
parents:
diff changeset
  2608
    TResourceReader reader;
hgs
parents:
diff changeset
  2609
    reader.SetBuffer( resourceFile.AllocReadLC( R_TEXT_STANDARD_TOPIC_LIST ) ); // on CS
hgs
parents:
diff changeset
  2610
    HBufC* standardTopicListName = reader.ReadHBufCL();
hgs
parents:
diff changeset
  2611
    CleanupStack::PushL( standardTopicListName );
hgs
parents:
diff changeset
  2612
hgs
parents:
diff changeset
  2613
    // Create a new topic list
hgs
parents:
diff changeset
  2614
    CreateNewTopicListL( standardTopicListName->Des() );
hgs
parents:
diff changeset
  2615
hgs
parents:
diff changeset
  2616
    CleanupStack::PopAndDestroy( 3 ); // resourceFile, reader, standardTopicListName
hgs
parents:
diff changeset
  2617
    }
hgs
parents:
diff changeset
  2618
hgs
parents:
diff changeset
  2619
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  2620
// CCbsDbImpTopicList::LoadTopicsIdsL
hgs
parents:
diff changeset
  2621
// Reads topic IDs of a topic list from the stream.
hgs
parents:
diff changeset
  2622
// (other items were commented in a header).
hgs
parents:
diff changeset
  2623
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  2624
//
hgs
parents:
diff changeset
  2625
void CCbsDbImpTopicList::LoadTopicsIdsL( 
hgs
parents:
diff changeset
  2626
    const TStreamId& aTopicListStreamId )
hgs
parents:
diff changeset
  2627
    {
hgs
parents:
diff changeset
  2628
    // Open the topic list stream
hgs
parents:
diff changeset
  2629
    RStoreReadStream instream;
hgs
parents:
diff changeset
  2630
    instream.OpenLC( *TopicStoreL(), aTopicListStreamId ); // on CS
hgs
parents:
diff changeset
  2631
hgs
parents:
diff changeset
  2632
    // Skip topic list name
hgs
parents:
diff changeset
  2633
    delete HBufC::NewL( instream, KCbsDbTopicNameLength );
hgs
parents:
diff changeset
  2634
hgs
parents:
diff changeset
  2635
    // Skip default list status
hgs
parents:
diff changeset
  2636
    CbsStreamHelper::ReadBoolL( instream );
hgs
parents:
diff changeset
  2637
hgs
parents:
diff changeset
  2638
    // Skip Topic List number
hgs
parents:
diff changeset
  2639
    instream.ReadInt16L();
hgs
parents:
diff changeset
  2640
    
hgs
parents:
diff changeset
  2641
    // Skip the amount of topics
hgs
parents:
diff changeset
  2642
    TInt topicCount = instream.ReadInt16L();
hgs
parents:
diff changeset
  2643
hgs
parents:
diff changeset
  2644
    // Clear the array
hgs
parents:
diff changeset
  2645
    iTopicIds->ResizeL( 0 );
hgs
parents:
diff changeset
  2646
hgs
parents:
diff changeset
  2647
    TStreamId id( 0 );
hgs
parents:
diff changeset
  2648
hgs
parents:
diff changeset
  2649
    // Load the topic IDs       
hgs
parents:
diff changeset
  2650
    for ( TInt i = 0; i < topicCount; i++ )
hgs
parents:
diff changeset
  2651
        {
hgs
parents:
diff changeset
  2652
        instream >> id;
hgs
parents:
diff changeset
  2653
        iTopicIds->AppendL( id );
hgs
parents:
diff changeset
  2654
        }   
hgs
parents:
diff changeset
  2655
    
hgs
parents:
diff changeset
  2656
    CleanupStack::PopAndDestroy(); // instream
hgs
parents:
diff changeset
  2657
    }
hgs
parents:
diff changeset
  2658
hgs
parents:
diff changeset
  2659
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  2660
// CCbsDbImpTopicList::LoadTopicsL
hgs
parents:
diff changeset
  2661
// Loads topics of a specified topic list.
hgs
parents:
diff changeset
  2662
// (other items were commented in a header).
hgs
parents:
diff changeset
  2663
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  2664
//
hgs
parents:
diff changeset
  2665
void CCbsDbImpTopicList::LoadTopicsL( 
hgs
parents:
diff changeset
  2666
    const TStreamId& aTopicListStreamId )
hgs
parents:
diff changeset
  2667
    {
hgs
parents:
diff changeset
  2668
    // Open the topic list stream
hgs
parents:
diff changeset
  2669
    RStoreReadStream instream;
hgs
parents:
diff changeset
  2670
    instream.OpenLC( *TopicStoreL(), aTopicListStreamId ); // on CS
hgs
parents:
diff changeset
  2671
hgs
parents:
diff changeset
  2672
    // Skip topic list name
hgs
parents:
diff changeset
  2673
    delete HBufC::NewL( instream, KCbsDbTopicNameLength );
hgs
parents:
diff changeset
  2674
hgs
parents:
diff changeset
  2675
    // Skip default list status
hgs
parents:
diff changeset
  2676
    CbsStreamHelper::ReadBoolL( instream );
hgs
parents:
diff changeset
  2677
hgs
parents:
diff changeset
  2678
    // Skip Topic List number
hgs
parents:
diff changeset
  2679
    instream.ReadInt16L();
hgs
parents:
diff changeset
  2680
    
hgs
parents:
diff changeset
  2681
    // Skip the amount of topics
hgs
parents:
diff changeset
  2682
    TInt topicCount = instream.ReadInt16L();
hgs
parents:
diff changeset
  2683
hgs
parents:
diff changeset
  2684
    // Clear the arrays
hgs
parents:
diff changeset
  2685
    iTopics->ResizeL( 0 );
hgs
parents:
diff changeset
  2686
    iTopicIds->ResizeL( 0 );
hgs
parents:
diff changeset
  2687
hgs
parents:
diff changeset
  2688
    TStreamId id( 0 );
hgs
parents:
diff changeset
  2689
hgs
parents:
diff changeset
  2690
    // Load the topic IDs 
hgs
parents:
diff changeset
  2691
    TInt i;
hgs
parents:
diff changeset
  2692
    for ( i = 0; i < topicCount; i++ )
hgs
parents:
diff changeset
  2693
        {
hgs
parents:
diff changeset
  2694
        instream >> id;
hgs
parents:
diff changeset
  2695
        iTopicIds->AppendL( id );
hgs
parents:
diff changeset
  2696
        }   
hgs
parents:
diff changeset
  2697
    
hgs
parents:
diff changeset
  2698
    CleanupStack::PopAndDestroy(); // instream
hgs
parents:
diff changeset
  2699
   
hgs
parents:
diff changeset
  2700
    // Load necessary information for each topic
hgs
parents:
diff changeset
  2701
    TInt count = iTopicIds->Count();
hgs
parents:
diff changeset
  2702
    TCbsDbImpTopic topic;
hgs
parents:
diff changeset
  2703
    for ( i = 0; i < count; i++ )
hgs
parents:
diff changeset
  2704
        {
hgs
parents:
diff changeset
  2705
        ReadTopicInformationL( iTopicIds->At( i ), topic );
hgs
parents:
diff changeset
  2706
hgs
parents:
diff changeset
  2707
        if ( topic.iTopicData.iNumber == KIndexTopicNumber )
hgs
parents:
diff changeset
  2708
            {
hgs
parents:
diff changeset
  2709
            HBufC* indexName = ReadIndexTopicNameLC(); // on CS
hgs
parents:
diff changeset
  2710
            topic.iTopicData.iName.Copy( *indexName );
hgs
parents:
diff changeset
  2711
            CleanupStack::PopAndDestroy(); // indexName
hgs
parents:
diff changeset
  2712
            }
hgs
parents:
diff changeset
  2713
        iTopics->AppendL( topic );
hgs
parents:
diff changeset
  2714
        }    
hgs
parents:
diff changeset
  2715
    }
hgs
parents:
diff changeset
  2716
hgs
parents:
diff changeset
  2717
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  2718
// CCbsDbImpTopicList::UpdateTopicStreamIdsL
hgs
parents:
diff changeset
  2719
// Updates the topic stream IDs.
hgs
parents:
diff changeset
  2720
// (other items were commented in a header).
hgs
parents:
diff changeset
  2721
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  2722
//
hgs
parents:
diff changeset
  2723
void CCbsDbImpTopicList::UpdateTopicStreamIdsL( 
hgs
parents:
diff changeset
  2724
    const TCbsDbTopicNumber aTopicNumber )
hgs
parents:
diff changeset
  2725
    {
hgs
parents:
diff changeset
  2726
    // Try to find the topic from current topic list
hgs
parents:
diff changeset
  2727
    TInt position( TopicIndexInList( aTopicNumber ) );
hgs
parents:
diff changeset
  2728
    if ( position >= 0 )
hgs
parents:
diff changeset
  2729
        {
hgs
parents:
diff changeset
  2730
        // Remove from the internal cache of this topic list
hgs
parents:
diff changeset
  2731
        iTopics->Delete( position );
hgs
parents:
diff changeset
  2732
hgs
parents:
diff changeset
  2733
        iCurrentTopicList.iTopicCount--;
hgs
parents:
diff changeset
  2734
hgs
parents:
diff changeset
  2735
        // Update the stream
hgs
parents:
diff changeset
  2736
        UpdateTopicListStreamL( iCurrentTopicList, EFalse );
hgs
parents:
diff changeset
  2737
        CommitFilesL();
hgs
parents:
diff changeset
  2738
        }
hgs
parents:
diff changeset
  2739
    }
hgs
parents:
diff changeset
  2740
hgs
parents:
diff changeset
  2741
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  2742
// CCbsDbImpTopicList::__DbgTestInvariant
hgs
parents:
diff changeset
  2743
// Checks that the object is in a valid state, and panics if it is not.
hgs
parents:
diff changeset
  2744
// (other items were commented in a header).
hgs
parents:
diff changeset
  2745
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
  2746
//
hgs
parents:
diff changeset
  2747
void CCbsDbImpTopicList::__DbgTestInvariant() const
hgs
parents:
diff changeset
  2748
    {
hgs
parents:
diff changeset
  2749
    /*
hgs
parents:
diff changeset
  2750
#if defined(_DEBUG)
hgs
parents:
diff changeset
  2751
#endif
hgs
parents:
diff changeset
  2752
    */
hgs
parents:
diff changeset
  2753
    }
hgs
parents:
diff changeset
  2754
hgs
parents:
diff changeset
  2755
// ========================== OTHER EXPORTED FUNCTIONS =========================
hgs
parents:
diff changeset
  2756
hgs
parents:
diff changeset
  2757
//  End of File