creator/src/creator_imps.cpp
branchRCL_3
changeset 22 fad26422216a
parent 0 d6fe6244b863
equal deleted inserted replaced
21:b3cee849fa46 22:fad26422216a
       
     1 /*
       
     2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #ifdef __PRESENCE
       
    20 
       
    21 #include "creator_imps.h" 
       
    22 #include "creator_traces.h"
       
    23 
       
    24 #include <cimpssapsettingslist.h>
       
    25 
       
    26 static const TInt KIMPSFieldLength = 128;
       
    27 
       
    28 _LIT(KCreatorIMPSPrefixName, "CR_");
       
    29 
       
    30 
       
    31 //----------------------------------------------------------------------------
       
    32 
       
    33 CIMPSParameters::CIMPSParameters()
       
    34     {
       
    35     LOGSTRING("Creator: CIMPSParameters::CIMPSParameters");
       
    36 
       
    37     iServerName = HBufC::New(KIMPSFieldLength);
       
    38     iServerURL = HBufC::New(KIMPSFieldLength);
       
    39     iServerUsername = HBufC::New(KIMPSFieldLength);
       
    40     iServerPassword = HBufC::New(KIMPSFieldLength);
       
    41     iServerAccessPointName = HBufC::New(KIMPSFieldLength);
       
    42     // User needs to set connectionmethod manually to be able to use the IMPS setting
       
    43     iServerAccessPointName->Des().Copy(_L("DEFAULT-IAP"));
       
    44     }
       
    45 
       
    46 CIMPSParameters::~CIMPSParameters()
       
    47     {
       
    48     LOGSTRING("Creator: CIMPSParameters::~CIMPSParameters");
       
    49 
       
    50     delete iServerAccessPointName;
       
    51     delete iServerPassword;
       
    52     delete iServerUsername;
       
    53     delete iServerURL;
       
    54     delete iServerName;
       
    55     }
       
    56 
       
    57 void CIMPSParameters::SetRandomNameL(CCreatorEngine& aEngine)
       
    58     {
       
    59     if(iServerName == 0)
       
    60         {
       
    61         iServerName = HBufC::New(KIMPSFieldLength);
       
    62         }
       
    63     TBuf<160> company = aEngine.RandomString(CCreatorEngine::ECompany);
       
    64     iServerName->Des() = KCreatorIMPSPrefixName;
       
    65     iServerName->Des().Append( company );
       
    66     iServerName->Des().Append( _L(" #") );
       
    67     iServerName->Des().AppendNum( aEngine.RandomNumber(1000, 9999) );    
       
    68     }
       
    69 
       
    70 void CIMPSParameters::SetRandomUrlL(CCreatorEngine& aEngine)
       
    71     {
       
    72     if(iServerURL == 0)
       
    73         {
       
    74         iServerURL = HBufC::New(KIMPSFieldLength);
       
    75         }
       
    76     TBuf<160> company = aEngine.RandomString(CCreatorEngine::ECompany);
       
    77     iServerURL->Des() = _L("http://imps.");
       
    78     iServerURL->Des().Append( company );
       
    79     iServerURL->Des().Append( _L(".com:18080/CSP/csp") );
       
    80     }
       
    81 
       
    82 void CIMPSParameters::SetRandomUsernameL(CCreatorEngine& aEngine)
       
    83     {
       
    84     iServerUsername->Des() = aEngine.RandomString(CCreatorEngine::EFirstName);
       
    85     iServerUsername->Des().LowerCase();
       
    86     }
       
    87 
       
    88 void CIMPSParameters::SetRandomPasswordL(CCreatorEngine& aEngine)
       
    89     {
       
    90     iServerPassword->Des() = aEngine.RandomString(CCreatorEngine::ESurname);
       
    91     iServerPassword->Des().LowerCase();
       
    92     }
       
    93 void CIMPSParameters::SetDefaultAccessPointL()
       
    94     {
       
    95     iServerAccessPointName->Des().Copy(_L("DEFAULT-IAP"));
       
    96     }
       
    97 //----------------------------------------------------------------------------
       
    98 
       
    99 CCreatorIMPS* CCreatorIMPS::NewL(CCreatorEngine* aEngine)
       
   100     {
       
   101     CCreatorIMPS* self = CCreatorIMPS::NewLC(aEngine);
       
   102     CleanupStack::Pop(self);
       
   103     return self;
       
   104     }
       
   105 
       
   106 CCreatorIMPS* CCreatorIMPS::NewLC(CCreatorEngine* aEngine)
       
   107     {
       
   108     CCreatorIMPS* self = new (ELeave) CCreatorIMPS;
       
   109     CleanupStack::PushL(self);
       
   110     self->ConstructL(aEngine);
       
   111     return self;
       
   112     }
       
   113 
       
   114 CCreatorIMPS::CCreatorIMPS()
       
   115     {
       
   116     }
       
   117 
       
   118 void CCreatorIMPS::ConstructL(CCreatorEngine* aEngine)
       
   119     {
       
   120     LOGSTRING("Creator: CCreatorIMPS::ConstructL");
       
   121 
       
   122     iEngine = aEngine;
       
   123 
       
   124     iIMPSSAPSettingsStore = CIMPSSAPSettingsStore::NewL();    
       
   125     }
       
   126 
       
   127 CCreatorIMPS::~CCreatorIMPS()
       
   128     {
       
   129     LOGSTRING("Creator: CCreatorIMPS::~CCreatorIMPS");
       
   130 
       
   131     if ( iEngine && iEntryIds.Count() )
       
   132         {
       
   133         TRAP_IGNORE( iEngine->WriteEntryIdsToStoreL( iEntryIds, KUidDictionaryUidIMPS ) );
       
   134         }
       
   135     iEntryIds.Reset();
       
   136     iEntryIds.Close();
       
   137     
       
   138     delete iIMPSSAPSettingsStore;
       
   139     
       
   140     if (iParameters)
       
   141         delete iParameters;
       
   142     }
       
   143 
       
   144 //----------------------------------------------------------------------------
       
   145 
       
   146 TBool CCreatorIMPS::AskDataFromUserL(TInt aCommand, TInt& aNumberOfEntries)
       
   147     {
       
   148     LOGSTRING("Creator: CCreatorIMPS::AskDataFromUserL");
       
   149     if ( aCommand == ECmdDeleteIMPSs )
       
   150         {
       
   151         return iEngine->YesNoQueryDialogL( _L("Delete all IMPS server entries?") );
       
   152         }
       
   153     else if ( aCommand ==  ECmdDeleteCreatorIMPSs )
       
   154         {
       
   155         return iEngine->YesNoQueryDialogL( _L("Delete all IMPS server entries created with Creator?") );
       
   156         }
       
   157     return iEngine->EntriesQueryDialogL(aNumberOfEntries, _L("How many entries to create?"));
       
   158     }
       
   159 
       
   160 
       
   161 //----------------------------------------------------------------------------
       
   162 
       
   163 TInt CCreatorIMPS::CreateIMPSServerEntryL(CIMPSParameters *aParameters)
       
   164     {
       
   165     LOGSTRING("Creator: CCreatorIMPS::CreateIMPSServerEntryL");
       
   166 
       
   167     // clear any existing parameter definations
       
   168     delete iParameters;
       
   169     iParameters = NULL;
       
   170     
       
   171     CIMPSParameters* parameters = aParameters;
       
   172     
       
   173     // random data needed if no predefined data available
       
   174     if (!parameters)
       
   175         {
       
   176         iParameters = new(ELeave) CIMPSParameters;
       
   177         parameters = iParameters;                
       
   178         parameters->SetRandomNameL(*iEngine);
       
   179         parameters->SetRandomUrlL(*iEngine);
       
   180         parameters->SetRandomUsernameL(*iEngine);
       
   181         parameters->SetRandomPasswordL(*iEngine);
       
   182         parameters->SetDefaultAccessPointL();
       
   183         }
       
   184 
       
   185     TInt err = KErrNone;
       
   186 
       
   187     // set data to save item
       
   188     CIMPSSAPSettings* settings = CIMPSSAPSettings::NewLC();
       
   189     TUint32 iapId = iEngine->AccessPointNameToIdL(parameters->iServerAccessPointName->Des(), ETrue );
       
   190     err = settings->SetAccessPoint( iapId );
       
   191     if( err != KErrNone )
       
   192     	{
       
   193     	LOGSTRING("Creator: CCreatorIMPS::CreateIMPSServerEntryL - Unable to resolve access point.");
       
   194     	err = KErrNone;
       
   195     	}
       
   196     settings->SetSAPNameL( parameters->iServerName->Des() );
       
   197     settings->SetSAPAddressL( parameters->iServerURL->Des() );
       
   198     settings->SetSAPUserIdL( parameters->iServerUsername->Des() );
       
   199     settings->SetSAPUserPasswordL( parameters->iServerPassword->Des() );
       
   200     
       
   201     // save the item
       
   202     TRAP(err, iIMPSSAPSettingsStore->StoreNewSAPL( settings, EIMPSIMAccessGroup ));
       
   203     
       
   204     // id has been generated, store it for being able to delete
       
   205     // only entries created with Creator
       
   206     iEntryIds.Append( settings->Uid() );
       
   207     
       
   208     TRAP(err, iIMPSSAPSettingsStore->StoreNewSAPL( settings, EIMPSPECAccessGroup ));
       
   209 
       
   210     // id has been generated, store it for being able to delete
       
   211     // only entries created with Creator
       
   212     iEntryIds.Append( settings->Uid() );
       
   213     
       
   214     CleanupStack::PopAndDestroy(); //settings
       
   215     return err;
       
   216     }
       
   217 
       
   218 
       
   219 //----------------------------------------------------------------------------
       
   220 void CCreatorIMPS::DeleteAllL()
       
   221     {
       
   222     LOGSTRING("Creator: CCreatorIMPS::DeleteAllL");
       
   223     CIMPSSAPSettingsList* imps = CIMPSSAPSettingsList::NewLC();
       
   224     iIMPSSAPSettingsStore->PopulateSAPSettingsListL( *imps, EIMPSAccessFilterAll );
       
   225     for ( TInt i = 0; i < imps->Count(); i++ )
       
   226         {
       
   227         iIMPSSAPSettingsStore->DeleteSAPL( imps->UidForIndex( i ) ); 
       
   228         }
       
   229     CleanupStack::PopAndDestroy( imps );
       
   230     
       
   231     // reset must be done here, because iEntryIds is stored in destructor
       
   232     iEntryIds.Reset();
       
   233     
       
   234     // all entries deleted, remove the IMPS related registry
       
   235     iEngine->RemoveStoreL( KUidDictionaryUidIMPS );
       
   236     }
       
   237 
       
   238 //----------------------------------------------------------------------------
       
   239 void CCreatorIMPS::DeleteAllCreatedByCreatorL()
       
   240     {
       
   241     LOGSTRING("Creator: CCreatorIMPS::DeleteAllCreatedByCreatorL");
       
   242     iEntryIds.Reset();
       
   243     
       
   244     // fetch ids of entries created by Creator
       
   245     iEngine->ReadEntryIdsFromStoreL( iEntryIds, KUidDictionaryUidIMPS );
       
   246     
       
   247     // delete entries
       
   248     for ( TInt i = 0; i < iEntryIds.Count(); i++ )
       
   249         {
       
   250         TRAP_IGNORE( iIMPSSAPSettingsStore->DeleteSAPL( iEntryIds[i] ) ); 
       
   251         }
       
   252     // reset must be done here, because iEntryIds is stored in destructor
       
   253     iEntryIds.Reset();
       
   254     
       
   255     // all entries deleted, remove the IMPS related registry
       
   256     iEngine->RemoveStoreL( KUidDictionaryUidIMPS );
       
   257     }
       
   258 
       
   259 #endif //__PRESENCE
       
   260