mmsengine/mmsserversettings/src/mmsregisteredapplications.cpp
changeset 0 72b543305e3a
child 23 238255e8b033
equal deleted inserted replaced
-1:000000000000 0:72b543305e3a
       
     1 /*
       
     2 * Copyright (c) 2005-2006 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  mmsregisteredapplications implementation
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19    
       
    20 // INCLUDE FILES   
       
    21 #include <centralrepository.h>
       
    22 #include <msvids.h>
       
    23 
       
    24 // USERINCLUDE FILES
       
    25 #include "mmsregisteredapplications.h"
       
    26 #include "mmssettings.h"
       
    27 #include "mmsdebuglogging.h"
       
    28 #include "MmsEngineDomainCRKeys.h"
       
    29 
       
    30 // EXTERNAL DATA STRUCTURES
       
    31 
       
    32 // EXTERNAL FUNCTION PROTOTYPES  
       
    33 
       
    34 // CONSTANTS
       
    35 const TInt KMmsApplicationIdArrayGranularity = 4;
       
    36 const TInt KMmsApplicationNameDefaultLength = 64; // a reasonable guess for the length
       
    37 
       
    38 // MACROS
       
    39 
       
    40 // LOCAL CONSTANTS AND MACROS
       
    41 
       
    42 // MODULE DATA STRUCTURES
       
    43 
       
    44 // LOCAL FUNCTION PROTOTYPES
       
    45 
       
    46 // ============================== LOCAL FUNCTIONS ==============================
       
    47 
       
    48 // ============================== MEMBER FUNCTIONS =============================
       
    49 
       
    50 // -----------------------------------------------------------------------------
       
    51 // CMmsRegisteredApplications::NewL
       
    52 //
       
    53 // -----------------------------------------------------------------------------
       
    54 //
       
    55 EXPORT_C CMmsRegisteredApplications* CMmsRegisteredApplications::NewL()
       
    56     {
       
    57     CMmsRegisteredApplications* self = new(ELeave) CMmsRegisteredApplications();
       
    58     CleanupStack::PushL( self );
       
    59     self->ConstructL();
       
    60     CleanupStack::Pop( self );
       
    61     return self;
       
    62     }
       
    63 
       
    64 // -----------------------------------------------------------------------------
       
    65 // CMmsRegisteredApplications::CMmsRegisteredApplications
       
    66 //
       
    67 // -----------------------------------------------------------------------------
       
    68 //
       
    69 CMmsRegisteredApplications::CMmsRegisteredApplications()
       
    70     {
       
    71     }
       
    72 
       
    73 // -----------------------------------------------------------------------------
       
    74 // CMmsRegisteredApplications::~CMmsRegisteredApplications
       
    75 //
       
    76 // -----------------------------------------------------------------------------
       
    77 //
       
    78 CMmsRegisteredApplications::~CMmsRegisteredApplications()
       
    79     {
       
    80     LOG(_L("~CMmsRegisteredApplications"));
       
    81     delete iUserFriendlyName;
       
    82     delete iRepository;
       
    83     delete iAppIdArray;  
       
    84     }
       
    85 
       
    86 // -----------------------------------------------------------------------------
       
    87 // CMmsRegisteredApplications::ConstructL
       
    88 //
       
    89 // -----------------------------------------------------------------------------
       
    90 //
       
    91 void CMmsRegisteredApplications::ConstructL()
       
    92     {
       
    93    
       
    94     iAppIdArray = new ( ELeave )CDesCArrayFlat( KMmsApplicationIdArrayGranularity );
       
    95 
       
    96     iRepository = CRepository::NewL( KCRUidMMSApplicationRegistrations ); 
       
    97     // The length may change later if the names are long.
       
    98     iUserFriendlyName = HBufC::NewL( KMmsApplicationNameDefaultLength );
       
    99     }
       
   100 
       
   101 // -----------------------------------------------------------------------------
       
   102 // CMmsRegisteredApplications::LoadRegisteredApplicationsL
       
   103 //
       
   104 // -----------------------------------------------------------------------------
       
   105 //
       
   106 
       
   107 EXPORT_C void CMmsRegisteredApplications::LoadRegisteredApplicationsL()
       
   108     {
       
   109     LOG(_L("CMmsRegisteredApplications::LoadRegisteredApplicationsL"));
       
   110     iAppIdArray->Reset();
       
   111     TInt count = 0;
       
   112     TInt error = iRepository->Get( KMmsEngineNbrOfRegisteredApplications, count );
       
   113     if ( error == KErrNotFound )
       
   114         {
       
   115         iRepository->Set( KMmsEngineNbrOfRegisteredApplications, 0 );   
       
   116         }
       
   117     
       
   118     // KMaxUnicodeStringLength is the maximum length of string in repository -
       
   119     // The strings will always fit into the bugger.
       
   120     HBufC* buffer = HBufC::NewL( NCentralRepositoryConstants::KMaxUnicodeStringLength );
       
   121     CleanupStack::PushL( buffer );
       
   122     TPtr ptr = buffer->Des();
       
   123     // We use the same buffer for reading everything - AppendL will always allocate
       
   124     // a new buffer anyway. No need to keep allocating a new read buffer every time
       
   125     for( TInt i = 0; i < count; i++ )
       
   126         {
       
   127         error = iRepository->Get( KMmsEngineRegisteredApplicationsArrayBase + i, ptr ) ;
       
   128         if ( error == KErrNone )
       
   129             { 
       
   130             iAppIdArray->AppendL( ptr );
       
   131             }
       
   132         else
       
   133             {
       
   134             LOG3(_L("- Loading registered appid array item %d returned error %d"), i, error );
       
   135             }
       
   136         }   
       
   137     CleanupStack::PopAndDestroy( buffer );
       
   138     buffer = NULL;
       
   139     
       
   140     }
       
   141 
       
   142 
       
   143 // -----------------------------------------------------------------------------
       
   144 // CMmsRegisteredApplications::NumberOfRegisteredApplicationsL
       
   145 //
       
   146 // -----------------------------------------------------------------------------
       
   147 //
       
   148 EXPORT_C TInt CMmsRegisteredApplications::NumberOfRegisteredApplicationsL() const
       
   149     {
       
   150     return iAppIdArray->MdcaCount();
       
   151     }
       
   152     
       
   153 // -----------------------------------------------------------------------------
       
   154 // CMmsRegisteredApplications::RegisteredL
       
   155 // -----------------------------------------------------------------------------
       
   156 //
       
   157 EXPORT_C TBool CMmsRegisteredApplications::RegisteredL( const TDesC& aApplicationId )
       
   158     {
       
   159     TInt index = -1;
       
   160     return Registered( aApplicationId, index );
       
   161     }  
       
   162 
       
   163 
       
   164     
       
   165 // -----------------------------------------------------------------------------
       
   166 // CMmsRegisteredApplications::RegisterL
       
   167 // -----------------------------------------------------------------------------
       
   168 //
       
   169 EXPORT_C TInt CMmsRegisteredApplications::RegisterL( const TDesC& aApplicationId )
       
   170     {
       
   171     return RegisterL( aApplicationId, TPtrC() );
       
   172     }
       
   173 
       
   174 // -----------------------------------------------------------------------------
       
   175 // CMmsRegisteredApplications::UnregisterL
       
   176 // -----------------------------------------------------------------------------
       
   177 //
       
   178 EXPORT_C TInt CMmsRegisteredApplications::UnregisterL( const TDesC& aApplicationId )
       
   179     {
       
   180     TInt index = 0;
       
   181     if ( !Registered( aApplicationId, index ) )
       
   182         {
       
   183         return KErrNone;            
       
   184         }
       
   185     LOG2(_L("CMmsRegisteredApplications::Unregister: array[%d]"), index );
       
   186     
       
   187     TInt error = KErrNone;
       
   188     
       
   189     // if item is not last one in the array. move the last item here.
       
   190     TInt lastIndex = iAppIdArray->MdcaCount() - 1;
       
   191     if ( index != lastIndex )
       
   192         {
       
   193         HBufC* buffer = HBufC::NewL( NCentralRepositoryConstants::KMaxUnicodeStringLength );
       
   194         CleanupStack::PushL( buffer );
       
   195         TPtr ptr = buffer->Des();
       
   196     
       
   197         error = iRepository->Get(
       
   198             KMmsEngineRegisteredApplicationsArrayBase + lastIndex, ptr );
       
   199         if ( error == KErrNone )
       
   200             {
       
   201             error = iRepository->Set(
       
   202                 KMmsEngineRegisteredApplicationsArrayBase + index, ptr );
       
   203             }
       
   204         // local array has to follow the same order
       
   205         if ( error == KErrNone )
       
   206             {
       
   207             // delete the old user friendly name.
       
   208             // The new one will replace it in a moment, but we make sure 
       
   209             // there is no incorrect name left in place in case InsertL
       
   210             // leaves because of lack of memory.
       
   211             iRepository->Delete( KMmsEngineApplicationNameArrayBase + index );
       
   212             // array index should be safe
       
   213             iAppIdArray->InsertL( index, ptr );
       
   214             iAppIdArray->Delete( index + 1 ); 
       
   215             }
       
   216         if ( error == KErrNone )
       
   217             {
       
   218             error = iRepository->Get( KMmsEngineApplicationNameArrayBase + lastIndex, ptr );
       
   219             if ( error == KErrNone && ptr.Length() > 0 )
       
   220                 {
       
   221                 error = iRepository->Set(
       
   222                     KMmsEngineApplicationNameArrayBase + index, ptr );
       
   223                 }
       
   224             // The setting of the name must not prevent final cleanup    
       
   225             error = KErrNone;    
       
   226             }
       
   227             
       
   228         CleanupStack::PopAndDestroy( buffer );
       
   229         buffer = NULL;
       
   230         }
       
   231     // delete the last item from the arrays
       
   232     if ( error == KErrNone )
       
   233         {
       
   234         error = iRepository->Delete( KMmsEngineRegisteredApplicationsArrayBase + lastIndex );
       
   235         if ( error == KErrNone )
       
   236             {
       
   237             iAppIdArray->Delete( lastIndex );
       
   238             iAppIdArray->Compress();
       
   239             SaveCount();        
       
   240             }
       
   241         // The user friendly name of the last item is deleted in any case
       
   242         // We are trying to make sure there are no friendly names pointing
       
   243         // to incorrect application ids left over.
       
   244         // The name can be fixed by trying to register the application again
       
   245         // Even if the application is already registered, the user friedly name
       
   246         // can be changed by registering the same application id again. 
       
   247         iRepository->Delete( KMmsEngineApplicationNameArrayBase + lastIndex );
       
   248         }
       
   249     return error;       
       
   250   
       
   251     }
       
   252     
       
   253 // -----------------------------------------------------------------------------
       
   254 // 
       
   255 // -----------------------------------------------------------------------------
       
   256 //
       
   257 EXPORT_C const CDesCArray& CMmsRegisteredApplications::RegisteredApplicationList() const
       
   258     {
       
   259     return *iAppIdArray; 
       
   260     }
       
   261     
       
   262 // -----------------------------------------------------------------------------
       
   263 // 
       
   264 // -----------------------------------------------------------------------------
       
   265 //
       
   266 EXPORT_C TInt CMmsRegisteredApplications::RegisterL(
       
   267     const TDesC& aApplicationId,
       
   268     const TDesC& aUserFriendlyName )
       
   269     {
       
   270     TInt error = KErrNone;
       
   271     TInt index = -1;
       
   272     if ( aApplicationId.Length() > NCentralRepositoryConstants::KMaxUnicodeStringLength )
       
   273         {
       
   274         // The application id is too long, it cannot be registerd
       
   275         return KErrArgument;
       
   276         }
       
   277     if ( aUserFriendlyName.Length() > NCentralRepositoryConstants::KMaxUnicodeStringLength )
       
   278         {
       
   279         return KErrArgument;
       
   280         }
       
   281         
       
   282     if ( !Registered( aApplicationId, index ) )
       
   283         {
       
   284         // add new app id to last item in the array.
       
   285         index = iAppIdArray->MdcaCount();
       
   286         error = iRepository->Create(
       
   287             KMmsEngineRegisteredApplicationsArrayBase + index, aApplicationId );
       
   288         if ( error == KErrNone || error == KErrAlreadyExists )
       
   289             {
       
   290             // We should not get "already exists" unless someone has 
       
   291             // edited the registry behind our back...
       
   292             error = KErrNone;
       
   293             // The index now points to the newly created application id
       
   294             iAppIdArray->AppendL( aApplicationId );
       
   295             // update count in cenrep.
       
   296             SaveCount();
       
   297             }
       
   298         }
       
   299         
       
   300     // If index == -1 at this point, the registeration has failed.    
       
   301     if ( aUserFriendlyName.Length() > 0 && error == KErrNone && index != -1 )
       
   302         {
       
   303         error = iRepository->Set( KMmsEngineApplicationNameArrayBase + index,
       
   304             aUserFriendlyName );
       
   305         if ( error == KErrAlreadyExists )
       
   306             {
       
   307             error = KErrNone;
       
   308             }
       
   309         }
       
   310         
       
   311     return error;
       
   312     }
       
   313     
       
   314 // -----------------------------------------------------------------------------
       
   315 // 
       
   316 // -----------------------------------------------------------------------------
       
   317 //
       
   318 EXPORT_C TPtrC CMmsRegisteredApplications::UserFriendlyName( const TDesC& aApplicationId )
       
   319     {
       
   320     // This is a "best effort" function. 
       
   321     // If memory runs out or some other error occurs, an empty string is returned
       
   322     TInt index = -1;
       
   323     if ( !Registered( aApplicationId, index ) )
       
   324         {
       
   325         return TPtrC();
       
   326         }
       
   327     
       
   328     TInt error = KErrNone;
       
   329     if ( !iUserFriendlyName )
       
   330         {
       
   331         // iUserFriendlyName may be NULL only if we have ran out of memory earlier
       
   332         TRAP( error, iUserFriendlyName = HBufC::NewL( KMmsApplicationNameDefaultLength ) );
       
   333         }
       
   334     if ( error != KErrNone )
       
   335         {
       
   336         // If no memory, return empty string
       
   337         return TPtrC();
       
   338         }
       
   339         
       
   340     TPtr ptr = iUserFriendlyName->Des();
       
   341     TInt actualLength = 0;
       
   342 
       
   343     error = iRepository->Get( KMmsEngineApplicationNameArrayBase + index, ptr, actualLength );
       
   344     if ( error == KErrOverflow )
       
   345         {
       
   346         delete iUserFriendlyName;
       
   347         iUserFriendlyName = NULL;
       
   348         TRAP( error,
       
   349             {
       
   350             iUserFriendlyName = HBufC::NewL( actualLength );
       
   351             ptr.Set( iUserFriendlyName->Des() ); 
       
   352             error = iRepository->Get( KMmsEngineApplicationNameArrayBase + index, ptr );
       
   353             });
       
   354         }
       
   355     if ( error == KErrNone )
       
   356         {
       
   357         return iUserFriendlyName->Des();
       
   358         }
       
   359     return TPtrC();
       
   360         
       
   361     }
       
   362 
       
   363     
       
   364 // -----------------------------------------------------------------------------
       
   365 // CMmsRegisteredApplications::Registered
       
   366 // -----------------------------------------------------------------------------
       
   367 //
       
   368 TBool CMmsRegisteredApplications::Registered( const TDesC& aApplicationId, TInt& aIndex )
       
   369     {
       
   370     aIndex = -1; 
       
   371     TInt count = iAppIdArray->MdcaCount();
       
   372     for( TInt i = 0; i < count && aIndex == -1; i++ )
       
   373         {
       
   374         TPtrC16 ptr = iAppIdArray->MdcaPoint( i );
       
   375         if ( ptr.Compare( aApplicationId ) == 0 )
       
   376             {
       
   377             aIndex = i; 
       
   378             }
       
   379         }
       
   380     return ( aIndex != -1 );        
       
   381     } 
       
   382 
       
   383 // -----------------------------------------------------------------------------
       
   384 // CMmsRegisteredApplications::SaveCount
       
   385 // -----------------------------------------------------------------------------
       
   386 //
       
   387 TInt CMmsRegisteredApplications::SaveCount()
       
   388     {
       
   389     TInt count = iAppIdArray->MdcaCount();
       
   390     // Set also creates a new key if it does not exist
       
   391     TInt error = iRepository->Set( KMmsEngineNbrOfRegisteredApplications, count );
       
   392     if( error != KErrNone )
       
   393         {
       
   394         LOG3( _L("-ERROR saving appid count %d: %d"), KMmsEngineNbrOfRegisteredApplications, error );
       
   395         }
       
   396     return error;   
       
   397     }
       
   398     
       
   399     
       
   400 // =========================== OTHER EXPORTED FUNCTIONS ========================
       
   401 
       
   402 //  End of File