htiui/HtiServicePlugins/HtiMessagesServicePlugin/src/HtiMsgSettingsHandler.cpp
branchRCL_3
changeset 18 48060abbbeaf
parent 17 d40e813b23c0
child 19 b3cee849fa46
equal deleted inserted replaced
17:d40e813b23c0 18:48060abbbeaf
     1 /*
       
     2 * Copyright (c) 2009 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:  Implementation of message settings handling.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include "HtiMessagesServicePlugin.h"
       
    21 #include "HtiMsgSettingsHandler.h"
       
    22 
       
    23 #include <HtiDispatcherInterface.h>
       
    24 #include <HtiLogging.h>
       
    25 
       
    26 #include <cmconnectionmethod.h>
       
    27 #include <cmconnectionmethoddef.h>
       
    28 #include <cmdestination.h>
       
    29 #include <cmmanager.h>
       
    30 
       
    31 #include <csmsaccount.h>
       
    32 #include <mmssettings.h>
       
    33 #include <smutset.h>
       
    34 #include <centralrepository.h>
       
    35 
       
    36 // EXTERNAL DATA STRUCTURES
       
    37 
       
    38 // EXTERNAL FUNCTION PROTOTYPES
       
    39 
       
    40 // CONSTANTS
       
    41 const TUid KCRUidSmum = { 0x101F87EF }; // from MessagingInternalCRKeys.h
       
    42 
       
    43 const TInt KMinSetDefaultSmsCenterLength = 4;
       
    44 const TInt KMinDeleteSmsCenterLength     = 2;
       
    45 const TInt KMinSetMmsSettingsLength      = 10;
       
    46 
       
    47 // set SMS settings has a fixed length
       
    48 const TInt KSetSmsSettingsLength         = 6;
       
    49 
       
    50 // message validity times in seconds
       
    51 const TInt KValidityTimeMax  = 0;
       
    52 const TInt KValidityTime1h   = 3600;
       
    53 const TInt KValidityTime6h   = 6 * 3600;
       
    54 const TInt KValidityTime24h  = 24 * 3600;
       
    55 const TInt KValidityTime3d   = 3 * 24 * 3600;
       
    56 const TInt KValidityTime1w   = 7 * 24 * 3600;
       
    57 
       
    58 // from MmsSettingsDefs.h
       
    59 const TInt KMmsUniImageSmallWidth = 640;
       
    60 const TInt KMmsUniImageSmallHeight = 480;
       
    61 const TInt KMmsUniImageLargeWidth = 1600;
       
    62 const TInt KMmsUniImageLargeHeight = 1200;
       
    63 
       
    64 const TInt KApMaxConnNameLength = 30;
       
    65 
       
    66 // MACROS
       
    67 
       
    68 // LOCAL CONSTANTS AND MACROS
       
    69 
       
    70 _LIT8( KErrorInvalidParameters, "Invalid command parameters" );
       
    71 _LIT8( KErrorSetDefaultSmsCenterFailed, "Setting default SMS center failed" );
       
    72 _LIT8( KErrorDeleteSmsCenterFailed, "SMS center deletion failed" );
       
    73 _LIT8( KErrorSmsCenterAlreadyExists, "SMS center with given name already exists with different number" );
       
    74 _LIT8( KErrorSmsCenterNotFound, "SMS center with given name not found" );
       
    75 _LIT8( KErrorSetSmsSettingsFailed, "Setting SMS service settings failed" );
       
    76 _LIT8( KErrorSetMmsSettingsFailed, "Setting MMS service settings failed" );
       
    77 _LIT8( KErrorApNotFound, "Access Point not found" );
       
    78 _LIT8( KErrorUnrecognizedCommand, "Unrecognized command" );
       
    79 
       
    80 // MODULE DATA STRUCTURES
       
    81 
       
    82 // LOCAL FUNCTION PROTOTYPES
       
    83 
       
    84 // FORWARD DECLARATIONS
       
    85 
       
    86 // ============================ MEMBER FUNCTIONS ===============================
       
    87 
       
    88 // -----------------------------------------------------------------------------
       
    89 // CHtiMsgSettingsHandler::NewL
       
    90 // Two-phased constructor.
       
    91 // -----------------------------------------------------------------------------
       
    92 CHtiMsgSettingsHandler* CHtiMsgSettingsHandler::NewL()
       
    93     {
       
    94     HTI_LOG_FUNC_IN( "CHtiMsgSettingsHandler::NewL" );
       
    95     CHtiMsgSettingsHandler* self = new (ELeave) CHtiMsgSettingsHandler();
       
    96     CleanupStack::PushL ( self );
       
    97     self->ConstructL();
       
    98     CleanupStack::Pop();
       
    99     HTI_LOG_FUNC_OUT( "CHtiMsgSettingsHandler::NewL" );
       
   100     return self;
       
   101     }
       
   102 
       
   103 
       
   104 // ----------------------------------------------------------------------------
       
   105 // CHtiMsgSettingsHandler::CHtiMsgSettingsHandler
       
   106 // C++ default constructor can NOT contain any code, that
       
   107 // might leave.
       
   108 // ----------------------------------------------------------------------------
       
   109 CHtiMsgSettingsHandler::CHtiMsgSettingsHandler()
       
   110     {
       
   111     HTI_LOG_FUNC_IN( "CHtiMsgSettingsHandler::CHtiMsgSettingsHandler" );
       
   112 
       
   113     HTI_LOG_FUNC_OUT( "CHtiMsgSettingsHandler::CHtiMsgSettingsHandler" );
       
   114     }
       
   115 
       
   116 
       
   117 // -----------------------------------------------------------------------------
       
   118 // CHtiMsgSettingsHandler::~CHtiMsgSettingsHandler
       
   119 // Destructor.
       
   120 // -----------------------------------------------------------------------------
       
   121 CHtiMsgSettingsHandler::~CHtiMsgSettingsHandler()
       
   122     {
       
   123     HTI_LOG_FUNC_IN( "CHtiMsgSettingsHandler::~CHtiMsgSettingsHandler" );
       
   124 
       
   125     HTI_LOG_FUNC_OUT( "CHtiMsgSettingsHandler::~CHtiMsgSettingsHandler" );
       
   126     }
       
   127 
       
   128 
       
   129 // -----------------------------------------------------------------------------
       
   130 // CHtiMsgSettingsHandler::ConstructL
       
   131 // Symbian 2nd phase constructor can leave.
       
   132 // -----------------------------------------------------------------------------
       
   133 void CHtiMsgSettingsHandler::ConstructL()
       
   134     {
       
   135     HTI_LOG_FUNC_IN( "CHtiMsgSettingsHandler::ConstructL" );
       
   136 
       
   137     HTI_LOG_FUNC_OUT( "CHtiMsgSettingsHandler::ConstructL" );
       
   138     }
       
   139 
       
   140 
       
   141 // -----------------------------------------------------------------------------
       
   142 // CHtiMsgSettingsHandler::SetDispatcher
       
   143 // Sets the dispatcher pointer.
       
   144 // -----------------------------------------------------------------------------
       
   145 
       
   146 void CHtiMsgSettingsHandler::SetDispatcher( MHtiDispatcher* aDispatcher )
       
   147     {
       
   148     HTI_LOG_FUNC_IN( "CHtiMsgSettingsHandler::SetDispatcher" );
       
   149     iDispatcher = aDispatcher;
       
   150     HTI_LOG_FUNC_OUT( "CHtiMsgSettingsHandler::SetDispatcher" );
       
   151     }
       
   152 
       
   153 
       
   154 // -----------------------------------------------------------------------------
       
   155 // CHtiMsgSettingsHandler::ProcessMessageL
       
   156 // Parses the received message and calls handler functions.
       
   157 // -----------------------------------------------------------------------------
       
   158 void CHtiMsgSettingsHandler::ProcessMessageL( const TDesC8& aMessage,
       
   159                                               THtiMessagePriority /*aPriority*/ )
       
   160     {
       
   161     HTI_LOG_FUNC_IN( "CHtiMsgSettingsHandler::ProcessMessageL" );
       
   162 
       
   163     // Zero length message and command code validity already checked
       
   164     // in HtiMessagesServicePlugin.
       
   165 
       
   166     if ( aMessage[0] == CHtiMessagesServicePlugin::ESetDefaultSmsCenter )
       
   167         {
       
   168         if ( aMessage.Length() < 1 + KMinSetDefaultSmsCenterLength )
       
   169             {
       
   170             SendErrorMessageL( KErrArgument, KErrorInvalidParameters );
       
   171             }
       
   172         else
       
   173             {
       
   174             TRAPD( err, HandleSetDefaultSmsCenterL(
       
   175                     aMessage.Right( aMessage.Length() - 1 ) ) );
       
   176             if ( err != KErrNone )
       
   177                 {
       
   178                 SendErrorMessageL( err, KErrorSetDefaultSmsCenterFailed );
       
   179                 }
       
   180             }
       
   181         }
       
   182 
       
   183     else if ( aMessage[0] == CHtiMessagesServicePlugin::EDeleteSmsCenter )
       
   184         {
       
   185         if ( aMessage.Length() < 1 + KMinDeleteSmsCenterLength )
       
   186             {
       
   187             SendErrorMessageL( KErrArgument, KErrorInvalidParameters );
       
   188             }
       
   189         else
       
   190             {
       
   191             TRAPD( err, HandleDeleteSmsCenterL(
       
   192                     aMessage.Right( aMessage.Length() - 1 ) ) );
       
   193             if ( err != KErrNone )
       
   194                 {
       
   195                 SendErrorMessageL( err, KErrorDeleteSmsCenterFailed );
       
   196                 }
       
   197             }
       
   198         }
       
   199 
       
   200     else if ( aMessage[0] == CHtiMessagesServicePlugin::ESetSmsSettings )
       
   201         {
       
   202         if ( aMessage.Length() != 1 + KSetSmsSettingsLength )
       
   203             {
       
   204             SendErrorMessageL( KErrArgument, KErrorInvalidParameters );
       
   205             }
       
   206         else
       
   207             {
       
   208             TRAPD( err, HandleSetSmsSettingsL(
       
   209                     aMessage.Right( aMessage.Length() - 1 ) ) );
       
   210             if ( err != KErrNone )
       
   211                 {
       
   212                 SendErrorMessageL( err, KErrorSetSmsSettingsFailed );
       
   213                 }
       
   214             }
       
   215         }
       
   216 
       
   217     else if ( aMessage[0] == CHtiMessagesServicePlugin::ESetMmsSettings )
       
   218         {
       
   219         if ( aMessage.Length() < 1 + KMinSetMmsSettingsLength )
       
   220             {
       
   221             SendErrorMessageL( KErrArgument, KErrorInvalidParameters );
       
   222             }
       
   223         else
       
   224             {
       
   225             TRAPD( err, HandleSetMmsSettingsL(
       
   226                     aMessage.Right( aMessage.Length() - 1 ) ) );
       
   227             if ( err != KErrNone )
       
   228                 {
       
   229                 SendErrorMessageL( err, KErrorSetMmsSettingsFailed );
       
   230                 }
       
   231             }
       
   232         }
       
   233 
       
   234     else
       
   235         {
       
   236         SendErrorMessageL( KErrArgument, KErrorUnrecognizedCommand );
       
   237         }
       
   238 
       
   239     HTI_LOG_FUNC_OUT( "CHtiMsgSettingsHandler::ProcessMessageL" );
       
   240     }
       
   241 
       
   242 
       
   243 // ----------------------------------------------------------------------------
       
   244 // CHtiMsgSettingsHandler::HandleSetDefaultSmsCenterL
       
   245 // Creates new default SMS center.
       
   246 // ----------------------------------------------------------------------------
       
   247 void CHtiMsgSettingsHandler::HandleSetDefaultSmsCenterL( const TDesC8& aData )
       
   248     {
       
   249     HTI_LOG_FUNC_IN( "CHtiMsgSettingsHandler::HandleSetDefaultSmsCenterL" );
       
   250 
       
   251     TBuf<255> name;
       
   252     TBuf<255> number;
       
   253 
       
   254     TInt dataLength = aData.Length();
       
   255     TInt offset = 0;
       
   256     TInt fieldLength = aData[offset];
       
   257     offset++;
       
   258 
       
   259     if ( offset + fieldLength > dataLength )
       
   260         {
       
   261         HTI_LOG_TEXT( "Invalid length specified for name" );
       
   262         SendErrorMessageL( KErrArgument, KErrorInvalidParameters );
       
   263         return;
       
   264         }
       
   265     name.Copy( aData.Mid( offset, fieldLength ) );
       
   266     offset += fieldLength;
       
   267     fieldLength = aData[offset];
       
   268     offset++;
       
   269 
       
   270     if ( offset + fieldLength > dataLength )
       
   271         {
       
   272         HTI_LOG_TEXT( "Invalid length specified for number" );
       
   273         SendErrorMessageL( KErrArgument, KErrorInvalidParameters );
       
   274         return;
       
   275         }
       
   276     number.Copy( aData.Mid( offset, fieldLength ) );
       
   277 
       
   278     HTI_LOG_FORMAT( "SMS center name: %S", &name );
       
   279     HTI_LOG_FORMAT( "SMS center number: %S", &number );
       
   280 
       
   281     HTI_LOG_TEXT( "Loading current settings..." );
       
   282     CSmsSettings* smsSettings = CSmsSettings::NewLC();
       
   283     CSmsAccount* smsAccount = CSmsAccount::NewLC();
       
   284     smsAccount->LoadSettingsL( *smsSettings );
       
   285 
       
   286     HTI_LOG_TEXT( "Looping through existing SMS centers..." );
       
   287     TInt i = 0;
       
   288     for ( i = 0; i < smsSettings->ServiceCenterCount(); i++ )
       
   289         {
       
   290         CSmsServiceCenter &center = smsSettings->GetServiceCenter( i );
       
   291 
       
   292         HTI_LOG_FORMAT( "Found SMS center: %S", &( center.Name() ) );
       
   293 
       
   294         if ( center.Name() == name && center.Address() != number )
       
   295             {
       
   296             HTI_LOG_TEXT( "Error, SMS center with given name already exists" );
       
   297             SendErrorMessageL( KErrAlreadyExists, KErrorSmsCenterAlreadyExists );
       
   298             CleanupStack::PopAndDestroy( 2 ); // smsAccount, smsSettings
       
   299             return;
       
   300             }
       
   301         else if ( center.Name() == name && center.Address() == number )
       
   302             {
       
   303             HTI_LOG_TEXT( "The defined SMS center already exists, setting it to default" );
       
   304             smsSettings->SetDefaultServiceCenter( i );
       
   305             smsAccount->SaveSettingsL( *smsSettings );
       
   306 
       
   307             CleanupStack::PopAndDestroy( 2 ); // smsAccount, smsSettings
       
   308             SendOkMsgL( KNullDesC8 );
       
   309 
       
   310             return;
       
   311             }
       
   312         }
       
   313 
       
   314     HTI_LOG_TEXT( "Creating new default SMS center" );
       
   315     smsSettings->AddServiceCenterL( name, number );
       
   316     smsSettings->SetDefaultServiceCenter( i );
       
   317     smsAccount->SaveSettingsL( *smsSettings );
       
   318 
       
   319     CleanupStack::PopAndDestroy( 2 ); // smsAccount, smsSettings
       
   320 
       
   321     SendOkMsgL( KNullDesC8 );
       
   322 
       
   323     HTI_LOG_FUNC_OUT( "CHtiMsgSettingsHandler::HandleSetDefaultSmsCenterL" );
       
   324     }
       
   325 
       
   326 
       
   327 // ----------------------------------------------------------------------------
       
   328 // CHtiMsgSettingsHandler::HandleDeleteSmsCenterL
       
   329 // Deletes the defined SMS center.
       
   330 // ----------------------------------------------------------------------------
       
   331 void CHtiMsgSettingsHandler::HandleDeleteSmsCenterL( const TDesC8& aData )
       
   332     {
       
   333     HTI_LOG_FUNC_IN( "CHtiMsgSettingsHandler::HandleDeleteSmsCenterL" );
       
   334 
       
   335     TBuf<255> name;
       
   336 
       
   337     TInt dataLength = aData.Length();
       
   338     TInt offset = 0;
       
   339     TInt fieldLength = aData[offset];
       
   340     offset++;
       
   341 
       
   342     if ( offset + fieldLength > dataLength )
       
   343         {
       
   344         HTI_LOG_TEXT( "Invalid length specified for name" );
       
   345         SendErrorMessageL( KErrArgument, KErrorInvalidParameters );
       
   346         return;
       
   347         }
       
   348     name.Copy( aData.Mid( offset, fieldLength ) );
       
   349 
       
   350     HTI_LOG_FORMAT( "SMS center name: %S", &name );
       
   351 
       
   352     HTI_LOG_TEXT( "Loading current settings..." );
       
   353     CSmsSettings* smsSettings = CSmsSettings::NewLC();
       
   354     CSmsAccount* smsAccount = CSmsAccount::NewLC();
       
   355     smsAccount->LoadSettingsL( *smsSettings );
       
   356 
       
   357     HTI_LOG_TEXT( "Looping through existing SMS centers..." );
       
   358     TInt i = 0;
       
   359     for ( i = 0; i < smsSettings->ServiceCenterCount(); i++ )
       
   360         {
       
   361         CSmsServiceCenter &center = smsSettings->GetServiceCenter( i );
       
   362 
       
   363         HTI_LOG_FORMAT( "Found SMS center: %S", &( center.Name() ) );
       
   364 
       
   365         if ( center.Name() == name )
       
   366             {
       
   367             HTI_LOG_TEXT( "Name matches, deleting..." );
       
   368             smsSettings->RemoveServiceCenter( i );
       
   369             smsAccount->SaveSettingsL( *smsSettings );
       
   370 
       
   371             CleanupStack::PopAndDestroy( 2 ); // smsAccount, smsSettings
       
   372 
       
   373             SendOkMsgL( KNullDesC8 );
       
   374             return;
       
   375             }
       
   376         }
       
   377 
       
   378     CleanupStack::PopAndDestroy( 2 ); // smsAccount, smsSettings
       
   379 
       
   380     SendErrorMessageL( KErrNotFound, KErrorSmsCenterNotFound );
       
   381 
       
   382     HTI_LOG_FUNC_OUT( "CHtiMsgSettingsHandler::HandleDeleteSmsCenterL" );
       
   383     }
       
   384 
       
   385 
       
   386 // ----------------------------------------------------------------------------
       
   387 // CHtiMsgSettingsHandler::HandleSetSmsSettingsL
       
   388 // Sets the SMS service settings.
       
   389 // ----------------------------------------------------------------------------
       
   390 void CHtiMsgSettingsHandler::HandleSetSmsSettingsL( const TDesC8& aData )
       
   391     {
       
   392     HTI_LOG_FUNC_IN( "CHtiMsgSettingsHandler::HandleSetSmsSettingsL" );
       
   393 
       
   394     // extract values
       
   395     TInt offset = 0;
       
   396     TInt characterSupport = aData[offset];
       
   397     offset++;
       
   398     HTI_LOG_FORMAT( "Character support     = %d", characterSupport );
       
   399 
       
   400     TBool deliveryReport = aData[offset];
       
   401     offset++;
       
   402     HTI_LOG_FORMAT( "Delivery report       = %d", deliveryReport );
       
   403 
       
   404     TInt validityPeriod = aData[offset];
       
   405     offset++;
       
   406     HTI_LOG_FORMAT( "Validity period       = %d", validityPeriod );
       
   407 
       
   408     TInt messageConversion = aData[offset];
       
   409     offset++;
       
   410     HTI_LOG_FORMAT( "Message conversion    = %d", messageConversion );
       
   411 
       
   412     TInt preferredConnection = aData[offset];
       
   413     offset++;
       
   414     HTI_LOG_FORMAT( "Preferred connection  = %d", preferredConnection );
       
   415 
       
   416     TBool replyViaSameCenter = aData[offset];
       
   417     offset++;
       
   418     HTI_LOG_FORMAT( "Reply via same center = %d", replyViaSameCenter );
       
   419 
       
   420     // Validate values
       
   421     if ( characterSupport > 1 || validityPeriod > 5 ||
       
   422          messageConversion > 3 || preferredConnection > 1 )
       
   423         {
       
   424         HTI_LOG_TEXT( "Invalid values found" );
       
   425         SendErrorMessageL( KErrArgument, KErrorInvalidParameters );
       
   426         return;
       
   427         }
       
   428 
       
   429     HTI_LOG_TEXT( "Loading current settings..." );
       
   430     CSmsSettings* smsSettings = CSmsSettings::NewLC();
       
   431     CSmsAccount* smsAccount = CSmsAccount::NewLC();
       
   432     smsAccount->LoadSettingsL( *smsSettings );
       
   433 
       
   434     HTI_LOG_TEXT( "Opening SMS settings Central Repository" );
       
   435     CRepository* cenRep = CRepository::NewLC( KCRUidSmum );
       
   436     HTI_LOG_TEXT( "Setting character support setting to SMS settings Central Repository" );
       
   437     TUint32 KSmumCharSupport = 0x00000008; // from MessagingInternalCRKeys.h
       
   438     switch ( characterSupport )
       
   439         {
       
   440         case 0:
       
   441             cenRep->Set( KSmumCharSupport, ECharSetFull );
       
   442             break;
       
   443         case 1:
       
   444             cenRep->Set( KSmumCharSupport, ECharSetReduced );
       
   445             break;
       
   446         default:
       
   447             User::Leave( KErrArgument );
       
   448             break;
       
   449         }
       
   450     CleanupStack::PopAndDestroy( cenRep );
       
   451     HTI_LOG_TEXT( "SMS settings Central Repository closed" );
       
   452 
       
   453     smsSettings->SetDeliveryReport( deliveryReport );
       
   454 
       
   455     switch ( validityPeriod )
       
   456         {
       
   457         case 0:
       
   458             smsSettings->SetValidityPeriod( TTimeIntervalMinutes( 0 ) );
       
   459             break;
       
   460         case 1:
       
   461             smsSettings->SetValidityPeriod(
       
   462                 TTimeIntervalMinutes( KValidityTime1h / 60 ) );
       
   463             break;
       
   464         case 2:
       
   465             smsSettings->SetValidityPeriod(
       
   466                 TTimeIntervalMinutes( KValidityTime6h / 60 ) );
       
   467             break;
       
   468         case 3:
       
   469             smsSettings->SetValidityPeriod(
       
   470                 TTimeIntervalMinutes( KValidityTime24h / 60 ) );
       
   471             break;
       
   472         case 4:
       
   473             smsSettings->SetValidityPeriod(
       
   474                 TTimeIntervalMinutes( KValidityTime3d / 60 ) );
       
   475             break;
       
   476         case 5:
       
   477             smsSettings->SetValidityPeriod(
       
   478                 TTimeIntervalMinutes( KValidityTime1w / 60 ) );
       
   479             break;
       
   480         default:
       
   481             User::Leave( KErrArgument );
       
   482             break;
       
   483         }
       
   484 
       
   485     switch ( messageConversion )
       
   486         {
       
   487         case 0:
       
   488             smsSettings->SetMessageConversion( ESmsConvPIDNone );
       
   489             break;
       
   490         case 1:
       
   491             smsSettings->SetMessageConversion( ESmsConvFax );
       
   492             break;
       
   493         case 2:
       
   494             smsSettings->SetMessageConversion( ESmsConvPaging );
       
   495             break;
       
   496         case 3:
       
   497             smsSettings->SetMessageConversion( ESmsConvMail );
       
   498             break;
       
   499         default:
       
   500             User::Leave( KErrArgument );
       
   501             break;
       
   502         }
       
   503 
       
   504     switch ( preferredConnection )
       
   505         {
       
   506         case 0:
       
   507             smsSettings->SetSmsBearer( CSmsSettings::ESmsBearerCircuitPreferred );
       
   508             break;
       
   509         case 1:
       
   510             smsSettings->SetSmsBearer( CSmsSettings::ESmsBearerPacketPreferred );
       
   511             break;
       
   512         default:
       
   513             User::Leave( KErrArgument );
       
   514             break;
       
   515         }
       
   516 
       
   517     smsSettings->SetReplyPath( replyViaSameCenter );
       
   518 
       
   519     HTI_LOG_TEXT( "Saving the settings" );
       
   520     smsAccount->SaveSettingsL( *smsSettings );
       
   521 
       
   522     CleanupStack::PopAndDestroy( 2 ); // smsAccount, smsSettings
       
   523 
       
   524     SendOkMsgL( KNullDesC8 );
       
   525 
       
   526     HTI_LOG_FUNC_OUT( "CHtiMsgSettingsHandler::HandleSetSmsSettingsL" );
       
   527     }
       
   528 
       
   529 
       
   530 // ----------------------------------------------------------------------------
       
   531 // CHtiMsgSettingsHandler::HandleSetMmsSettingsL
       
   532 // Sets the MMS service settings.
       
   533 // ----------------------------------------------------------------------------
       
   534 void CHtiMsgSettingsHandler::HandleSetMmsSettingsL( const TDesC8& aData )
       
   535     {
       
   536     HTI_LOG_FUNC_IN( "CHtiMsgSettingsHandler::HandleSetMmsSettingsL" );
       
   537     TInt offset = 0;
       
   538 
       
   539     // Get the access point name and search UID for that AP.
       
   540     TInt apNameLength = aData[offset];
       
   541     offset++;
       
   542     if ( apNameLength > KApMaxConnNameLength ||
       
   543          aData.Length() != ( KMinSetMmsSettingsLength + apNameLength - 1 ) )
       
   544         {
       
   545         SendErrorMessageL( KErrArgument, KErrorInvalidParameters );
       
   546         return;
       
   547         }
       
   548 
       
   549     TBuf<KApMaxConnNameLength> apName;
       
   550     apName.Copy( aData.Mid( offset, apNameLength ) );
       
   551     offset += apNameLength;
       
   552     HTI_LOG_FORMAT( "Searching AP with name: %S", &apName );
       
   553     TInt apUid = GetAccessPointUIDL( apName );
       
   554     if ( apUid == KErrNotFound )
       
   555         {
       
   556         SendErrorMessageL( KErrNotFound, KErrorApNotFound );
       
   557         return;
       
   558         }
       
   559 
       
   560     // Extract other values
       
   561     TMmsCreationMode creationMode = ( TMmsCreationMode ) aData[offset];
       
   562     offset++;
       
   563     HTI_LOG_FORMAT( "creation mode      = %d", creationMode );
       
   564 
       
   565     TInt imageSize = aData[offset];
       
   566     offset++;
       
   567     HTI_LOG_FORMAT( "image size         = %d", imageSize );
       
   568 
       
   569     TInt receptionMode = aData[offset];
       
   570     offset++;
       
   571     HTI_LOG_FORMAT( "reception mode     = %d", receptionMode );
       
   572 
       
   573     TBool receiveAnonymous = aData[offset];
       
   574     offset++;
       
   575     HTI_LOG_FORMAT( "receive anonymous  = %d", receiveAnonymous );
       
   576 
       
   577     TBool receiveAdvert = aData[offset];
       
   578     offset++;
       
   579     HTI_LOG_FORMAT( "receive adverts    = %d", receiveAdvert );
       
   580 
       
   581     TBool requestDeliveryReport = aData[offset];
       
   582     offset++;
       
   583     HTI_LOG_FORMAT( "request deliv reps = %d", requestDeliveryReport );
       
   584 
       
   585     TBool sendDeliveryReport = aData[offset];
       
   586     offset++;
       
   587     HTI_LOG_FORMAT( "send deliv reps    = %d", sendDeliveryReport );
       
   588 
       
   589     TInt validityPeriod = aData[offset];
       
   590     offset++;
       
   591     HTI_LOG_FORMAT( "validity period    = %d", validityPeriod );
       
   592 
       
   593     // Validate values
       
   594     if ( creationMode > 2 || imageSize > 2 ||
       
   595          receptionMode > 3 || validityPeriod > 5 ||
       
   596          ( creationMode == 0 && imageSize == 2 ) )
       
   597         {
       
   598         HTI_LOG_TEXT( "Invalid values found" );
       
   599         SendErrorMessageL( KErrArgument, KErrorInvalidParameters );
       
   600         return;
       
   601         }
       
   602 
       
   603     // Load current settings
       
   604     CMmsSettings* mmsSettings = CMmsSettings::NewL();
       
   605     CleanupStack::PushL( mmsSettings );
       
   606     mmsSettings->LoadSettingsL();
       
   607     HTI_LOG_TEXT( "Settings loaded - now updating" );
       
   608 
       
   609     // Update the values
       
   610     if ( mmsSettings->AccessPointCount() > 0 )
       
   611         {
       
   612         mmsSettings->DeleteAccessPointL( 0 );
       
   613         }
       
   614     mmsSettings->AddAccessPointL( apUid, 0 );
       
   615 
       
   616     mmsSettings->SetCreationMode( creationMode );
       
   617 
       
   618     switch ( imageSize )
       
   619         {
       
   620         case 0:
       
   621             mmsSettings->SetImageWidth( KMmsUniImageSmallWidth );
       
   622             mmsSettings->SetImageHeight( KMmsUniImageSmallHeight );
       
   623             break;
       
   624         case 1:
       
   625             mmsSettings->SetImageWidth( KMmsUniImageLargeWidth );
       
   626             mmsSettings->SetImageHeight( KMmsUniImageLargeHeight );
       
   627             break;
       
   628         default:
       
   629             User::Leave( KErrArgument );
       
   630             break;
       
   631         }
       
   632 
       
   633     switch ( receptionMode )
       
   634         {
       
   635         case 0:
       
   636             mmsSettings->SetReceivingModeHome( EMmsReceivingAutomatic );
       
   637             mmsSettings->SetReceivingModeForeign( EMmsReceivingAutomatic );
       
   638             break;
       
   639         case 1:
       
   640             mmsSettings->SetReceivingModeHome( EMmsReceivingAutomatic );
       
   641             mmsSettings->SetReceivingModeForeign( EMmsReceivingManual );
       
   642             break;
       
   643         case 2:
       
   644             mmsSettings->SetReceivingModeHome( EMmsReceivingManual );
       
   645             mmsSettings->SetReceivingModeForeign( EMmsReceivingManual );
       
   646             break;
       
   647         case 3:
       
   648             mmsSettings->SetReceivingModeHome( EMmsReceivingReject );
       
   649             mmsSettings->SetReceivingModeForeign( EMmsReceivingReject );
       
   650             break;
       
   651         default:
       
   652             User::Leave( KErrArgument );
       
   653             break;
       
   654         }
       
   655 
       
   656     mmsSettings->SetAcceptAnonymousMessages( receiveAnonymous );
       
   657     mmsSettings->SetAcceptAdvertisementMessages( receiveAdvert );
       
   658 
       
   659     if ( requestDeliveryReport )
       
   660         {
       
   661         mmsSettings->SetDeliveryReportWanted( KMmsYes );
       
   662         }
       
   663     else
       
   664         {
       
   665         mmsSettings->SetDeliveryReportWanted( KMmsNo );
       
   666         }
       
   667 
       
   668     if ( sendDeliveryReport )
       
   669         {
       
   670         mmsSettings->SetDeliveryReportSendingAllowed( KMmsYes );
       
   671         }
       
   672     else
       
   673         {
       
   674         mmsSettings->SetDeliveryReportSendingAllowed( KMmsNo );
       
   675         }
       
   676 
       
   677     switch ( validityPeriod )
       
   678         {
       
   679         case 0:
       
   680             mmsSettings->SetExpiryInterval( KValidityTimeMax );
       
   681             break;
       
   682         case 1:
       
   683             mmsSettings->SetExpiryInterval( KValidityTime1h );
       
   684             break;
       
   685         case 2:
       
   686             mmsSettings->SetExpiryInterval( KValidityTime6h );
       
   687             break;
       
   688         case 3:
       
   689             mmsSettings->SetExpiryInterval( KValidityTime24h );
       
   690             break;
       
   691         case 4:
       
   692             mmsSettings->SetExpiryInterval( KValidityTime3d );
       
   693             break;
       
   694         case 5:
       
   695             mmsSettings->SetExpiryInterval( KValidityTime1w );
       
   696             break;
       
   697         default:
       
   698             User::Leave( KErrArgument );
       
   699             break;
       
   700         }
       
   701 
       
   702     // Check validity of updated settings and save if valid.
       
   703     if ( mmsSettings->ValidateSettings() != KErrNone )
       
   704         {
       
   705         HTI_LOG_TEXT( "Settings not valid - won't save" );
       
   706         SendErrorMessageL( KErrArgument, KErrorInvalidParameters );
       
   707         }
       
   708     else
       
   709         {
       
   710         HTI_LOG_TEXT( "Settings valid - now saving" );
       
   711         mmsSettings->SaveSettingsL();
       
   712         SendOkMsgL( KNullDesC8 );
       
   713         }
       
   714 
       
   715     CleanupStack::PopAndDestroy(); // mmsSettings
       
   716     HTI_LOG_FUNC_OUT( "CHtiMsgSettingsHandler::HandleSetMmsSettingsL" );
       
   717     }
       
   718 
       
   719 
       
   720 // ----------------------------------------------------------------------------
       
   721 // CHtiMsgSettingsHandler::GetAccessPointUIDL
       
   722 // Gets the UID of Access Point named by aApName.
       
   723 // Returns KErrNotFound if AP not found.
       
   724 // ----------------------------------------------------------------------------
       
   725 TInt CHtiMsgSettingsHandler::GetAccessPointUIDL( const TDesC& aApName )
       
   726     {
       
   727     HTI_LOG_FUNC_IN( "CHtiMsgSettingsHandler::GetAccessPointUIDL" );
       
   728     TInt uid = KErrNotFound;
       
   729 
       
   730     RCmManager cmManager;
       
   731     cmManager.OpenL();
       
   732     CleanupClosePushL( cmManager );
       
   733 
       
   734     // Search from uncategorised first
       
   735     RArray<TUint32> array = RArray<TUint32>();
       
   736     cmManager.ConnectionMethodL( array );
       
   737     CleanupClosePushL( array );
       
   738     TInt i = 0;
       
   739     while ( i < array.Count() && uid == KErrNotFound )
       
   740         {
       
   741         RCmConnectionMethod cm = cmManager.ConnectionMethodL( array[i] );
       
   742         CleanupClosePushL( cm );
       
   743         HBufC* name = cm.GetStringAttributeL( CMManager::ECmName );
       
   744         HTI_LOG_FORMAT( "Found name: %S", name );
       
   745         CleanupStack::PushL( name );
       
   746         if ( aApName.Compare( *name ) == 0 )
       
   747             {
       
   748             uid = cm.GetIntAttributeL( CMManager::ECmWapId );
       
   749             HTI_LOG_FORMAT( "Match: UID = %d", uid );
       
   750             }
       
   751         CleanupStack::PopAndDestroy(); // name
       
   752         CleanupStack::PopAndDestroy(); // cm
       
   753         i++;
       
   754         }
       
   755     CleanupStack::PopAndDestroy(); // array
       
   756 
       
   757     // If not found from uncategorised, search from all destinations
       
   758     if ( uid == KErrNotFound )
       
   759         {
       
   760         HTI_LOG_TEXT( "Not found from uncategorised" );
       
   761         RArray<TUint32> destIdArray = RArray<TUint32>();
       
   762         cmManager.AllDestinationsL( destIdArray );
       
   763         CleanupClosePushL( destIdArray );
       
   764         i = 0;
       
   765         while ( i < destIdArray.Count() && uid == KErrNotFound )
       
   766             {
       
   767             RCmDestination dest = cmManager.DestinationL( destIdArray[i] );
       
   768             CleanupClosePushL( dest );
       
   769             TInt j = 0;
       
   770             while ( j < dest.ConnectionMethodCount() && uid == KErrNotFound )
       
   771                 {
       
   772                 HBufC* name = dest.ConnectionMethodL( j ).GetStringAttributeL(
       
   773                         CMManager::ECmName );
       
   774                 CleanupStack::PushL( name );
       
   775                 HTI_LOG_FORMAT( "Found name: %S", name );
       
   776                 if ( aApName.Compare( *name ) == 0 )
       
   777                     {
       
   778                     uid = dest.ConnectionMethodL( j ).GetIntAttributeL(
       
   779                             CMManager::ECmWapId );
       
   780                     HTI_LOG_FORMAT( "Match: UID = %d", uid );
       
   781                     }
       
   782                 CleanupStack::PopAndDestroy(); // name
       
   783                 j++;
       
   784                 }
       
   785             CleanupStack::PopAndDestroy(); // dest
       
   786             i++;
       
   787             }
       
   788         CleanupStack::PopAndDestroy(); // destIdArray
       
   789         }
       
   790 
       
   791     CleanupStack::PopAndDestroy(); // cmManager
       
   792 
       
   793     HTI_LOG_FUNC_OUT( "CHtiMsgSettingsHandler::GetAccessPointUIDL" );
       
   794     return uid;
       
   795     }
       
   796 
       
   797 
       
   798 // ----------------------------------------------------------------------------
       
   799 // CHtiMsgSettingsHandler::SendOkMsgL
       
   800 // Helper function for sending response messages.
       
   801 // ----------------------------------------------------------------------------
       
   802 void CHtiMsgSettingsHandler::SendOkMsgL( const TDesC8& aData )
       
   803     {
       
   804     HTI_LOG_FUNC_IN( "CHtiMsgSettingsHandler::SendOkMsgL" );
       
   805     User::LeaveIfNull( iDispatcher );
       
   806     HBufC8* temp = HBufC8::NewL( aData.Length() + 1 );
       
   807     TPtr8 response = temp->Des();
       
   808     response.Append( ( TChar ) CHtiMessagesServicePlugin::EResultOk );
       
   809     response.Append( aData );
       
   810     User::LeaveIfError( iDispatcher->DispatchOutgoingMessage(
       
   811         temp, KHtiMessagesServiceUid ) );
       
   812     HTI_LOG_FUNC_OUT( "CHtiMsgSettingsHandler::SendOkMsgL" );
       
   813     }
       
   814 
       
   815 
       
   816 // ----------------------------------------------------------------------------
       
   817 // CHtiMsgSettingsHandler::SendErrorMessageL
       
   818 // Helper function for sending error response messages.
       
   819 // ----------------------------------------------------------------------------
       
   820 void CHtiMsgSettingsHandler::SendErrorMessageL( TInt aError, const TDesC8& aDescription )
       
   821     {
       
   822     HTI_LOG_FUNC_IN( "CHtiMsgSettingsHandler::SendErrorMessageL" );
       
   823     User::LeaveIfNull( iDispatcher );
       
   824     User::LeaveIfError( iDispatcher->DispatchOutgoingErrorMessage(
       
   825         aError, aDescription, KHtiMessagesServiceUid ) );
       
   826     HTI_LOG_FUNC_OUT( "CHtiMsgSettingsHandler::SendErrorMessageL" );
       
   827     }
       
   828 
       
   829 // End of file