mobilemessaging/audiomsg/src/audiomessagesaveoperation.cpp
changeset 0 72b543305e3a
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:   Sets resipients, finalize message entry. 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #include <mmsclient.h>
       
    21 #include <MsgAddressControl.h>  
       
    22 #include <mmsgenutils.h> 		
       
    23 #include <StringLoader.h>        
       
    24 #include "audiomessagesaveoperation.h"
       
    25 
       
    26 _LIT( KAddressSeparator, ";" );
       
    27 const TInt  KMaxDetailsLength  = 64;   // Copy max this many chars to TMsvEntry iDetails
       
    28 
       
    29 // ======== MEMBER FUNCTIONS ========
       
    30 
       
    31 // ---------------------------------------------------------
       
    32 // CAudioMessageSaveOperation::NewL
       
    33 // ---------------------------------------------------------
       
    34 //
       
    35 CAudioMessageSaveOperation* CAudioMessageSaveOperation::NewL( 
       
    36 		CMsgAddressControl& aAddressCtrl,
       
    37         MAmsOperationObserver& aObserver,
       
    38         CAudioMessageDocument& aDocument,
       
    39         CMsgEditorView& aView )
       
    40     {
       
    41     CAudioMessageSaveOperation* self = 
       
    42         new ( ELeave ) CAudioMessageSaveOperation( aAddressCtrl,aObserver, aDocument, aView);
       
    43     return self;
       
    44     }
       
    45 
       
    46 // ---------------------------------------------------------
       
    47 // CAudioMessageSaveOperation::Constructor
       
    48 // ---------------------------------------------------------
       
    49 //
       
    50 CAudioMessageSaveOperation::CAudioMessageSaveOperation(
       
    51 		CMsgAddressControl& aAddressCtrl,
       
    52         MAmsOperationObserver& aObserver,
       
    53         CAudioMessageDocument& aDocument,
       
    54         CMsgEditorView& aView ) :
       
    55     CAudioMessageOperation( aObserver, aDocument ),
       
    56     iView( aView ), iAddressCtrl( aAddressCtrl )
       
    57     {
       
    58     }
       
    59 
       
    60 // ---------------------------------------------------------
       
    61 // CAudioMessageSaveOperation::Destructor
       
    62 // ---------------------------------------------------------
       
    63 //
       
    64 CAudioMessageSaveOperation::~CAudioMessageSaveOperation()
       
    65     {
       
    66     Cancel();
       
    67     }
       
    68 
       
    69 // ---------------------------------------------------------
       
    70 // CAudioMessageSaveOperation::Save
       
    71 // ---------------------------------------------------------
       
    72 //
       
    73 void CAudioMessageSaveOperation::Save()
       
    74     {
       
    75     ResetError();
       
    76     iSaveState = EAmsSaveCheck;
       
    77     CompleteSelf( KErrNone );
       
    78     }
       
    79 
       
    80 
       
    81 // ---------------------------------------------------------
       
    82 // CAudioMessageSaveOperation::DoSaveStepL
       
    83 // ---------------------------------------------------------
       
    84 //
       
    85 void CAudioMessageSaveOperation::DoSaveStepL()
       
    86     {
       
    87     switch ( iSaveState )
       
    88         {
       
    89        	case EAmsSaveCheck:
       
    90             DoSaveChecksL();
       
    91             break;
       
    92         case EAmsSaveFinalize:
       
    93             DoFinalizeSaveL();
       
    94             break;
       
    95         case EAmsSaveEnd:
       
    96             iObserver.EditorOperationEvent(
       
    97                 EAmsOperationSave,
       
    98                 EAmsOperationComplete );
       
    99             break;
       
   100         default:
       
   101             iObserver.EditorOperationEvent(
       
   102                 EAmsOperationSave,
       
   103                 EAmsOperationError );
       
   104             break;
       
   105         }
       
   106     }
       
   107 
       
   108 // ---------------------------------------------------------
       
   109 // CAudioMessageSaveOperation::DoSaveChecksL
       
   110 // ---------------------------------------------------------
       
   111 //
       
   112 void CAudioMessageSaveOperation::DoSaveChecksL()
       
   113     {
       
   114     iSaveState = EAmsSaveFinalize;
       
   115    	CMmsClientMtm& mtm = iDocument.Mtm();
       
   116    	
       
   117    	mtm.SetSubjectL( KNullDesC() );
       
   118 	for ( TInt i = mtm.AddresseeList().Count(); --i >=  0; )
       
   119     	{
       
   120     	mtm.RemoveAddressee( i );
       
   121     	}
       
   122 	const CMsgRecipientArray* recipients = iAddressCtrl.GetRecipientsL();
       
   123 
       
   124 	// inserts addresses from address control to clientMtm
       
   125 	TInt addrCnt = recipients->Count();
       
   126 	for ( TInt ii = 0; ii < addrCnt ; ii++ )
       
   127     	{
       
   128     	CMsgRecipientItem* addrItem = recipients->At(ii);
       
   129     	TPtr realAddress = addrItem->Address()->Des();
       
   130     	TPtr alias = addrItem->Name()->Des();
       
   131     	// Check that neither string contains illegal characters.
       
   132     	// If they does strip illegal chars away and save
       
   133    		RemoveIllegalAddrChars( realAddress );
       
   134     	if ( alias.Length() > 0)
       
   135         	{
       
   136      		RemoveIllegalAddrChars( alias );
       
   137         	}
       
   138     	mtm.AddAddresseeL( EMsvRecipientTo, realAddress, alias );
       
   139     	}
       
   140 	mtm.SaveMessageL();
       
   141 	CompleteSelf( KErrNone );
       
   142     }
       
   143 
       
   144 // ---------------------------------------------------------
       
   145 // CAudioMessageSaveOperation::RemoveIllegalAddrChars
       
   146 // ---------------------------------------------------------
       
   147 //
       
   148 TBool CAudioMessageSaveOperation::RemoveIllegalAddrChars( TDes& aString )
       
   149     {
       
   150     TBool ret( EFalse );
       
   151     for ( TInt i = 0; i < aString.Length(); i++ )
       
   152         {
       
   153         if ( !IsValidAddrChar( (TChar) aString[ i ] ) ) 
       
   154             {
       
   155             aString.Delete( i, 1 );
       
   156             i--;
       
   157             ret = ETrue;
       
   158             }
       
   159         }
       
   160     return ret;
       
   161     }
       
   162 
       
   163 // ---------------------------------------------------------
       
   164 // CAudioMessageSaveOperation::IsValidAddrChar
       
   165 // ---------------------------------------------------------
       
   166 //
       
   167 TBool CAudioMessageSaveOperation::IsValidAddrChar( const TChar& aChar )
       
   168     {
       
   169     return ( aChar != '<' && aChar != '>' );
       
   170     }
       
   171     
       
   172 // ---------------------------------------------------------
       
   173 // CAudioMessageSaveOperation::DoFinalizeSaveL
       
   174 // ---------------------------------------------------------
       
   175 //
       
   176 void CAudioMessageSaveOperation::DoFinalizeSaveL()
       
   177     {
       
   178     iSaveState = EAmsSaveEnd;
       
   179     CMmsClientMtm& mtm = iDocument.Mtm();
       
   180 
       
   181     TMmsMsvEntry tEntry = static_cast<TMmsMsvEntry>( iDocument.Entry() );
       
   182 
       
   183     // Set details
       
   184     TBuf<KMaxDetailsLength> aDetails; 
       
   185     const CMsvRecipientList& addresses = mtm.AddresseeList();
       
   186     TInt addrCnt = addresses.Count();
       
   187 
       
   188     TPtrC stringToAdd;
       
   189     for ( TInt i = 0; i < addrCnt; i++)
       
   190         {
       
   191         TPtrC alias = TMmsGenUtils::Alias( addresses[i] );
       
   192         TPtrC address = TMmsGenUtils::PureAddress( addresses[i] );
       
   193 
       
   194         if ( alias.Length() != 0 )
       
   195             {
       
   196             stringToAdd.Set( alias );
       
   197             }
       
   198         else
       
   199             {
       
   200             stringToAdd.Set( address );
       
   201             }
       
   202         
       
   203         if ( ( aDetails.Length() != 0 ) &&   // Not a first address
       
   204              ( aDetails.Length() + KAddressSeparator().Length() < KMaxDetailsLength ) )
       
   205             {
       
   206             // Add separator
       
   207             aDetails.Append( KAddressSeparator() );
       
   208             }
       
   209 
       
   210         if ( aDetails.Length() + stringToAdd.Length() < KMaxDetailsLength ) 
       
   211             {
       
   212             // whole string fits. Add it.
       
   213             aDetails.Append( stringToAdd );
       
   214             }
       
   215         else
       
   216             {
       
   217             // Only part of the string fits
       
   218             TInt charsToAdd = KMaxDetailsLength - aDetails.Length();
       
   219 
       
   220             if ( charsToAdd <= 0 )
       
   221                 {
       
   222                 // Cannot add any more chars 
       
   223                 break;
       
   224                 }
       
   225 
       
   226             if ( charsToAdd >= stringToAdd.Length() )
       
   227                 {
       
   228                 // Guarantee that charsToAdd is not larger that stringToAdd lenght 
       
   229                 charsToAdd = stringToAdd.Length();
       
   230                 }
       
   231 
       
   232             aDetails.Append( stringToAdd.Left( charsToAdd ) );
       
   233             break;
       
   234             }
       
   235         }
       
   236     AknTextUtils::ConvertDigitsTo( aDetails, EDigitTypeWestern );
       
   237     tEntry.iDetails.Set( aDetails );
       
   238 
       
   239     if ( iDocument.IsForward() )
       
   240         {
       
   241         tEntry.SetForwardedMessage( EFalse );
       
   242         }
       
   243 
       
   244     if ( !tEntry.EditorOriented() )
       
   245         {
       
   246         tEntry.SetEditorOriented( ETrue );
       
   247         tEntry.iMtmData1 &= ~KMmsMessageMobileTerminated;
       
   248         tEntry.iMtmData1 |= KMmsMessageEditorOriented;
       
   249         }
       
   250 
       
   251     // Update timestamp  
       
   252     tEntry.iDate.UniversalTime();
       
   253 
       
   254     if ( !tEntry.Visible() )
       
   255         {
       
   256         // Save from close or exit save.
       
   257         // Message should be visible after save
       
   258         tEntry.SetVisible( ETrue );
       
   259         tEntry.SetInPreparation( EFalse );
       
   260        	tEntry.iBioType = KUidMsgSubTypeMmsAudioMsg.iUid;
       
   261         }
       
   262 
       
   263     iDocument.CurrentEntry().ChangeL( tEntry );
       
   264     iDocument.SetHeaderModified( EFalse );
       
   265     iDocument.SetBodyModified( EFalse );
       
   266     CompleteSelf( KErrNone );
       
   267     }
       
   268     
       
   269 // ---------------------------------------------------------
       
   270 // CAudioMessageSaveOperation::DoCancel
       
   271 // ---------------------------------------------------------
       
   272 //
       
   273 void CAudioMessageSaveOperation::DoCancel()
       
   274     {
       
   275     }
       
   276 
       
   277 // ---------------------------------------------------------
       
   278 // CAudioMessageSaveOperation::RunL
       
   279 // ---------------------------------------------------------
       
   280 //
       
   281 void CAudioMessageSaveOperation::RunL()
       
   282     {
       
   283     if ( iStatus.Int() != KErrNone )
       
   284         {
       
   285         SetError( iStatus.Int() );
       
   286         iObserver.EditorOperationEvent(
       
   287             EAmsOperationSave,
       
   288             EAmsOperationError );
       
   289         }
       
   290     else
       
   291         {
       
   292         DoSaveStepL();
       
   293         }
       
   294     }