messagingappbase/ncnlist/src/NcnOutboxSendOperation.cpp
changeset 0 72b543305e3a
equal deleted inserted replaced
-1:000000000000 0:72b543305e3a
       
     1 /*
       
     2 * Copyright (c) 2004 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:   Implements class
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include "NcnDebug.h"
       
    22 #include "NcnOutboxSendOperation.h"   // header
       
    23 #include "NcnModel.h"
       
    24 
       
    25 #include    <SenduiMtmUids.h>           // MTM Uids
       
    26 #include    <msvids.h>                  // Entry Ids
       
    27 #include    <muiumsvuiserviceutilitiesinternal.h> // Messaging utilites
       
    28 #include    <gsmerror.h>                // SMS sending failure error codes
       
    29 
       
    30 
       
    31 // CONSTANTS
       
    32 const TInt KNcnSendPriority=CActive::EPriorityStandard;
       
    33 const TInt KNcnSendSelectionGranularity=4;
       
    34 
       
    35 // ================= MEMBER FUNCTIONS =======================
       
    36 
       
    37 // Two-phased constructor.
       
    38 CNcnOutboxSendOperation* CNcnOutboxSendOperation::NewL(
       
    39     CMsvSession& aMsvSession,
       
    40     TRequestStatus& aObserverRequestStatus )
       
    41     {
       
    42     // Create the instance of sending operation
       
    43     CNcnOutboxSendOperation* self =
       
    44         new (ELeave) CNcnOutboxSendOperation(
       
    45             aMsvSession, aObserverRequestStatus );
       
    46 
       
    47     // Push self into cleanup stack during construction
       
    48     CleanupStack::PushL(self);
       
    49     self->ConstructL();
       
    50     CleanupStack::Pop();
       
    51 
       
    52     // Return the object
       
    53     return self;
       
    54     }
       
    55 
       
    56 // C++ default constructor can NOT contain any code that
       
    57 // might leave.
       
    58 //
       
    59 CNcnOutboxSendOperation::CNcnOutboxSendOperation(
       
    60     CMsvSession& aMsvSession,
       
    61     TRequestStatus& aObserverRequestStatus )
       
    62     :
       
    63     CMsvOperation( aMsvSession, KNcnSendPriority, aObserverRequestStatus ),
       
    64     iSelections( KNcnSendSelectionGranularity ),
       
    65     iServices(),
       
    66     iSupportedMsgs( CNcnOutboxSendOperation::ENcnSupportsSmsSending )
       
    67     {
       
    68     // Start scheduler
       
    69     CActiveScheduler::Add(this);
       
    70     }
       
    71 
       
    72 //destructor
       
    73 CNcnOutboxSendOperation::~CNcnOutboxSendOperation()
       
    74     {
       
    75     // Cancel sending
       
    76     Cancel();
       
    77 
       
    78     // Delete sending operation
       
    79     delete iOperation;
       
    80     iOperation = NULL;
       
    81 
       
    82     // Delete entry
       
    83     delete iEntry;
       
    84     iEntry = NULL;
       
    85 
       
    86     // Remove services from queue and destroy message selections
       
    87     iServices.Reset();
       
    88     iSelections.ResetAndDestroy();
       
    89     }
       
    90 
       
    91 // ----------------------------------------------------
       
    92 // CNcnOutboxSendOperation::ConstructL
       
    93 // ----------------------------------------------------
       
    94 //
       
    95 void CNcnOutboxSendOperation::ConstructL()
       
    96     {
       
    97     // Get rootindex entry
       
    98     iEntry = iMsvSession.GetEntryL( KMsvRootIndexEntryId );
       
    99     iEntry->SetSortTypeL(
       
   100         TMsvSelectionOrdering( KMsvNoGrouping, EMsvSortByNone, ETrue ) );
       
   101 
       
   102     // Set sending flags
       
   103     iSupportedMsgs |= ENcnSendSms;
       
   104 
       
   105     // Start sending
       
   106     StartSendingL();
       
   107     }
       
   108 
       
   109 // ----------------------------------------------------
       
   110 // CNcnOutboxSendOperation::RunL
       
   111 // ----------------------------------------------------
       
   112 //
       
   113 void CNcnOutboxSendOperation::RunL()
       
   114     {
       
   115     NCN_RDEBUG(_L("CNcnOutboxSendOperation::RunL"));
       
   116     User::LeaveIfError( iStatus.Int() );
       
   117 
       
   118     // Check and start sending, if needed
       
   119     TUid sendMtm;
       
   120 
       
   121     // Check if messages needs to be sent
       
   122     if( IsSendingNeeded( sendMtm ) )
       
   123         {
       
   124         StartSendingL();
       
   125         }
       
   126     // Nothing to send, complete operation
       
   127     else
       
   128         {
       
   129         CompleteObserver( iStatus.Int() );
       
   130         }
       
   131     }
       
   132 
       
   133 // ----------------------------------------------------
       
   134 // CNcnOutboxSendOperation::RunError
       
   135 // ----------------------------------------------------
       
   136 //
       
   137 TInt CNcnOutboxSendOperation::RunError( TInt aError )
       
   138     {
       
   139     NCN_RDEBUG_INT(_L("CNcnOutboxSendOperation::RunError - Error %d"), aError );
       
   140     CompleteObserver( aError );
       
   141     return aError;
       
   142     }
       
   143 
       
   144 // ----------------------------------------------------
       
   145 // CNcnOutboxSendOperation::DoCancel
       
   146 // ----------------------------------------------------
       
   147 //
       
   148 void CNcnOutboxSendOperation::DoCancel()
       
   149     {
       
   150     // Check if sending operation is running
       
   151     if( iOperation )
       
   152         {
       
   153         // Cancel it
       
   154         iOperation->Cancel();
       
   155         }
       
   156 
       
   157     // Complete operation with current status
       
   158     CompleteObserver( iStatus.Int() );
       
   159     }
       
   160 
       
   161 // ----------------------------------------------------
       
   162 // CNcnOutboxSendOperation::ProgressL
       
   163 // ----------------------------------------------------
       
   164 //
       
   165 const TDesC8& CNcnOutboxSendOperation::ProgressL()
       
   166     {
       
   167     // Check if operation exists
       
   168     if( iOperation )
       
   169         {
       
   170         // Return the operation
       
   171         return iOperation->ProgressL();
       
   172         }
       
   173 
       
   174     // If no operation, return blank information
       
   175     return KNcnBlankBuffer;
       
   176     }
       
   177 
       
   178 // ---------------------------------------------------------
       
   179 // CNcnOutboxSendOperation::CompleteOperation
       
   180 // ---------------------------------------------------------
       
   181 //
       
   182 void CNcnOutboxSendOperation::CompleteObserver( TInt aStatus )
       
   183     {
       
   184     // Get the observer status
       
   185     TRequestStatus* status = &iObserverRequestStatus;
       
   186     User::RequestComplete( status, aStatus );
       
   187     }
       
   188 
       
   189 // ----------------------------------------------------
       
   190 // CNcnOutboxSendOperation::StartSendingL
       
   191 // ----------------------------------------------------
       
   192 //
       
   193 void CNcnOutboxSendOperation::StartSendingL()
       
   194     {
       
   195     // Remove any sending operation that currently may be running
       
   196     delete iOperation;
       
   197     iOperation = NULL;
       
   198 
       
   199     // Check if selection can be created for selected
       
   200 
       
   201     // Check if there was errors with creating selection
       
   202     if  ( CheckAndCreateSelectionL() )
       
   203         {
       
   204         // Create new operation and trap any errors
       
   205         SendWaitingSMSMessages();
       
   206 
       
   207         // Clear selection after the operation is complete
       
   208         RemoveSelection();
       
   209         }
       
   210     else
       
   211         {
       
   212     // Set state to pending
       
   213         CompleteSelf( KErrNone );
       
   214         }
       
   215     }
       
   216 
       
   217     // if error, then complete this pass with the error code
       
   218 void CNcnOutboxSendOperation::SendWaitingSMSMessages()
       
   219     {
       
   220     TRAPD( err, SendWaitingSMSMessagesL() );
       
   221     if  ( err != KErrNone )
       
   222         {
       
   223         ASSERT( !IsActive() );
       
   224         CompleteSelf( err );
       
   225         }
       
   226     }
       
   227 
       
   228 // ----------------------------------------------------
       
   229 // CNcnOutboxSendOperation::SendWaitingSMSMessagesL
       
   230 // ----------------------------------------------------
       
   231 //
       
   232 void CNcnOutboxSendOperation::SendWaitingSMSMessagesL()
       
   233     {
       
   234     // Get first selection from queue
       
   235     CMsvEntrySelection& selection = ( *iSelections[0] );
       
   236 
       
   237     // Get count of messages in queue
       
   238     TInt count = selection.Count();
       
   239 
       
   240     // Go through all messages
       
   241     while( count-- )
       
   242         {
       
   243         // Select message
       
   244         iEntry->SetEntryL( selection[count] );
       
   245         TMsvEntry entry( iEntry->Entry() );
       
   246 
       
   247         // Check if the message is tried to send when in offline
       
   248         if( ( entry.SendingState() == KMsvSendStateSuspended ||
       
   249               entry.SendingState() == KMsvSendStateFailed ) &&
       
   250             ( entry.iError == KErrGsmOfflineOpNotAllowed ||
       
   251               entry.iError == KErrGsmOfflineSimOpNotAllowed ) )
       
   252             {
       
   253             // Set message to wait sending
       
   254             NCN_RDEBUG(_L("SendWaitingSMSMessagesL: entry qualified" ) );
       
   255             entry.SetSendingState( KMsvSendStateWaiting );
       
   256             iEntry->ChangeL( entry );
       
   257             }
       
   258 		else
       
   259 			{
       
   260 			NCN_RDEBUG_INT2(_L("SendWaitingSMSMessagesL: entry not qualified: state %d, error %d" ), entry.SendingState(), entry.iError );
       
   261 			selection.Delete( count );
       
   262 			}
       
   263         }
       
   264     selection.Compress();
       
   265 
       
   266     // Set entry to outbox
       
   267     iMtm = iEntry->Entry().iMtm;
       
   268     iEntry->SetEntryL( KMsvGlobalOutBoxIndexEntryId );
       
   269 
       
   270 	NCN_RDEBUG_INT(_L("SendWaitingSMSMessagesL: %d entries to send" ), selection.Count() );
       
   271     // Start sending
       
   272 	if ( selection.Count() )
       
   273 		{
       
   274 		iOperation = iEntry->CopyL( selection, iServices[0], iStatus );
       
   275         SetActive();
       
   276 		}
       
   277     else
       
   278         {
       
   279         // Nothing to send, but we must complete the observer via our RunL callback
       
   280         CompleteSelf( KErrNone );
       
   281         }
       
   282     }
       
   283 
       
   284 
       
   285 // ----------------------------------------------------
       
   286 // CNcnOutboxSendOperation::RemoveSelection
       
   287 // ----------------------------------------------------
       
   288 //
       
   289 void CNcnOutboxSendOperation::RemoveSelection()
       
   290     {
       
   291     // Clear the the current selection.
       
   292     iServices.Delete(0);
       
   293 
       
   294     // Delete selection object and index
       
   295     delete iSelections[0];
       
   296     iSelections.Delete(0);
       
   297     }
       
   298 
       
   299 // ----------------------------------------------------
       
   300 // CNcnOutboxSendOperation::CreateSelectionL
       
   301 // ----------------------------------------------------
       
   302 //
       
   303 void CNcnOutboxSendOperation::CreateSelectionL(
       
   304     const TUid &aUidForSel,
       
   305     const TMsvId& aServiceId,
       
   306     CMsvEntrySelection*& aSelection )
       
   307     {
       
   308     // Set entry to outbox and get messages from outbox
       
   309     iEntry->SetEntryL( KMsvGlobalOutBoxIndexEntryId );
       
   310     aSelection = iEntry->ChildrenWithMtmL( *&aUidForSel );
       
   311 
       
   312     // Check if there is anything to put into array
       
   313     if( aSelection->Count() )
       
   314         {
       
   315         // Put selection to queue
       
   316         CleanupStack::PushL( aSelection );
       
   317         iSelections.AppendL( aSelection );
       
   318         CleanupStack::Pop( aSelection );
       
   319 
       
   320         // Put service to queue
       
   321         iServices.AppendL( aServiceId );
       
   322         }
       
   323     else
       
   324         {
       
   325         // Remove selection
       
   326         delete aSelection;
       
   327         aSelection = NULL;
       
   328         }
       
   329     }
       
   330 
       
   331 // ----------------------------------------------------
       
   332 // CNcnOutboxSendOperation::CheckAndCreateSelectionL
       
   333 // ----------------------------------------------------
       
   334 //
       
   335 TBool CNcnOutboxSendOperation::CheckAndCreateSelectionL()
       
   336     {
       
   337     // Get root index
       
   338     iEntry->SetEntryL( KMsvRootIndexEntryId );
       
   339 
       
   340     // MTM, for which the selection is collected
       
   341     TUid sendMtm;
       
   342 
       
   343     // Selection of messages for sending
       
   344     CMsvEntrySelection* smsSelection = NULL;
       
   345 
       
   346     // While MTM's available for sending
       
   347     while( smsSelection == NULL && IsSendingNeeded( sendMtm ) )
       
   348         {
       
   349         // Find default SMS service
       
   350         TMsvId serviceId =
       
   351             MsvUiServiceUtilitiesInternal::DefaultServiceForMTML(
       
   352                 iMsvSession, *&sendMtm, ETrue );
       
   353 
       
   354         // Check if the service ID is found
       
   355         if( serviceId != KMsvNullIndexEntryId )
       
   356             {
       
   357             // Create selection of messages of specified MTM
       
   358             CreateSelectionL( sendMtm, serviceId, smsSelection );
       
   359             }
       
   360 		else
       
   361 			{
       
   362 			NCN_RDEBUG(_L("CNcnOutboxSendOperation::CheckAndCreateSelectionL: default SMS service not found"));
       
   363 			}
       
   364 
       
   365         // Selection has been created, remove the flag
       
   366         RemoveSendingFlag( *&sendMtm );
       
   367         }
       
   368 
       
   369     const TBool selectionAvailable = ( smsSelection != NULL );
       
   370     return selectionAvailable;
       
   371     }
       
   372 
       
   373 // ----------------------------------------------------
       
   374 // CNcnOutboxSendOperation::RemoveSendingFlag
       
   375 // ----------------------------------------------------
       
   376 //
       
   377 void CNcnOutboxSendOperation::RemoveSendingFlag( const TUid& aMtm )
       
   378     {
       
   379     // Decide actions by mtm
       
   380     switch( aMtm.iUid )
       
   381         {
       
   382         // SMS-messages
       
   383         case KSenduiMtmSmsUidValue:
       
   384             iSupportedMsgs &= ~ENcnSendSms;
       
   385             break;
       
   386         // Do nothing
       
   387         default:
       
   388             break;
       
   389         }
       
   390     }
       
   391 
       
   392 // ----------------------------------------------------
       
   393 // CNcnOutboxSendOperation::IsSendingNeeded
       
   394 // ----------------------------------------------------
       
   395 //
       
   396 TBool CNcnOutboxSendOperation::IsSendingNeeded(
       
   397     TUid& aMtm ) const
       
   398     {
       
   399     // Set starting condition
       
   400     TBool needSending = EFalse;
       
   401 
       
   402     // Check if sms-sending is supported and messages need to be sent
       
   403     if( iSupportedMsgs & ENcnSupportsSmsSending &&
       
   404         iSupportedMsgs & ENcnSendSms )
       
   405         {
       
   406         aMtm = KSenduiMtmSmsUid;
       
   407         needSending = ETrue;
       
   408         }
       
   409     // Otherwise nothing needs to be done
       
   410     else
       
   411         {
       
   412         aMtm.iUid = 0;
       
   413         needSending = EFalse;
       
   414         }
       
   415 
       
   416     // Return the result
       
   417     return needSending;
       
   418     }
       
   419 
       
   420 // ----------------------------------------------------
       
   421 // CNcnOutboxSendOperation::CompleteSelf
       
   422 // ----------------------------------------------------
       
   423 //
       
   424 void CNcnOutboxSendOperation::CompleteSelf( TInt aValue )
       
   425     {
       
   426     TRequestStatus* status = &iStatus;
       
   427     User::RequestComplete( status, aValue );
       
   428     SetActive();
       
   429     }
       
   430     
       
   431 // End of file