cbsatplugin/atmisccmdplugin/src/cmgdcommandhandler.cpp
branchRCL_3
changeset 72 4b59561a31c0
parent 64 1934667b0e2b
equal deleted inserted replaced
64:1934667b0e2b 72:4b59561a31c0
     1 /*
       
     2  * Copyright (c) 2010 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  * Initial Contributors:
       
     9  * Nokia Corporation - initial contribution.
       
    10  *
       
    11  * Contributors:
       
    12  * Description :
       
    13  *
       
    14  */
       
    15 
       
    16 #include "cmgdcommandhandler.h"
       
    17 #include <mmtsy_names.h> 
       
    18 #include <mmretrieve.h>
       
    19 #include <exterror.h>
       
    20 
       
    21 #include "atmisccmdpluginconsts.h"
       
    22 #include "debug.h"
       
    23 
       
    24 const TInt KDefaultListLength = 256;
       
    25 
       
    26 CCMGDCommandHandler* CCMGDCommandHandler::NewL(MATMiscCmdPlugin* aCallback, TAtCommandParser& aATCmdParser, RMobilePhone& aPhone)
       
    27     {
       
    28     TRACE_FUNC_ENTRY
       
    29     CCMGDCommandHandler* self = new (ELeave) CCMGDCommandHandler(aCallback, aATCmdParser, aPhone);
       
    30     CleanupStack::PushL(self);
       
    31     self->ConstructL();
       
    32     CleanupStack::Pop(self);
       
    33     TRACE_FUNC_EXIT
       
    34     return self;
       
    35     }
       
    36 
       
    37 CCMGDCommandHandler::CCMGDCommandHandler(MATMiscCmdPlugin* aCallback, TAtCommandParser& aATCmdParser, RMobilePhone& aPhone) :
       
    38     CATCmdAsyncBase(aCallback, aATCmdParser, aPhone)
       
    39     {
       
    40     TRACE_FUNC_ENTRY
       
    41 
       
    42     TRACE_FUNC_EXIT
       
    43     }
       
    44 
       
    45 void CCMGDCommandHandler::ConstructL()
       
    46     {
       
    47     TRACE_FUNC_ENTRY
       
    48     
       
    49     iReply.CreateL(KDefaultCmdBufLength);
       
    50     
       
    51     User::LeaveIfError( iMobileSmsMessaging.Open(iPhone) );
       
    52 
       
    53     TInt err = iMobileSmsStore.Open(iMobileSmsMessaging, KETelIccSmsStore);
       
    54     if (err != KErrNone)
       
    55         {
       
    56         iHandlerState = ECMGDStateSimStoreNotSupported;
       
    57         }
       
    58     
       
    59     iRetrieveSmsList = CRetrieveMobilePhoneSmsList::NewL(iMobileSmsStore, RMobileSmsStore::KETelMobileGsmSmsEntryV1);
       
    60     
       
    61     TRACE_FUNC_EXIT
       
    62     }
       
    63 
       
    64 CCMGDCommandHandler::~CCMGDCommandHandler()
       
    65     {
       
    66     TRACE_FUNC_ENTRY
       
    67     Cancel();
       
    68     delete iGsmSmsList;
       
    69     delete iRetrieveSmsList;
       
    70     iMobileSmsStore.Close();
       
    71     iMobileSmsMessaging.Close();
       
    72     iReply.Close();
       
    73     iDeleteList.Close();
       
    74     TRACE_FUNC_EXIT
       
    75     }
       
    76 
       
    77 void CCMGDCommandHandler::HandleCommand(const TDesC8& /*aCmd*/, RBuf8& /*aReply*/, TBool /*aReplyNeeded*/)
       
    78     {
       
    79     TRACE_FUNC_ENTRY
       
    80     
       
    81     if (iHandlerState == ECMGDStateSimStoreNotSupported)
       
    82         {
       
    83         // Reply "ERROR" if SIM store is not supported
       
    84         iCallback->CreateReplyAndComplete(EReplyTypeError);
       
    85         TRACE_FUNC_EXIT
       
    86         return;
       
    87         }
       
    88     
       
    89     TInt ret = KErrNone;
       
    90     
       
    91     TAtCommandParser::TCommandHandlerType cmdHandlerType = iATCmdParser.CommandHandlerType();
       
    92     
       
    93     switch (cmdHandlerType)
       
    94         {
       
    95         case (TAtCommandParser::ECmdHandlerTypeTest):
       
    96             {
       
    97             iRetrieveSmsList->Start(iStatus);
       
    98             iHandlerState = ECMGDStateRetrieveAllIndices;
       
    99             SetActive();
       
   100             }
       
   101             break;
       
   102         case (TAtCommandParser::ECmdHandlerTypeSet):
       
   103             {
       
   104             ret = ParseParameters();
       
   105             if (ret == KErrNone)
       
   106                 {
       
   107                 Trace(_L("index = %d"), iIndex);
       
   108                 Trace(_L("delflag = %d"), iDelFlag);
       
   109                 ret = DoHandleCommand();
       
   110                 }
       
   111             else
       
   112                 {
       
   113                 ret = KErrArgument;
       
   114                 }
       
   115             break;
       
   116             }
       
   117         default:
       
   118             {
       
   119             ret = KErrNotSupported;
       
   120             break;
       
   121             }
       
   122         }
       
   123     if (ret == KErrGsmSMSInvalidMemoryIndex)
       
   124         {
       
   125         iCallback->CreateCMSReplyAndComplete(ret);
       
   126         }
       
   127     else if (ret != KErrNone)
       
   128         {
       
   129         iCallback->CreateReplyAndComplete(EReplyTypeError);
       
   130         }
       
   131 
       
   132     TRACE_FUNC_EXIT
       
   133     }
       
   134 
       
   135 void CCMGDCommandHandler::RunL()
       
   136     {
       
   137     TRACE_FUNC_ENTRY
       
   138     
       
   139     Trace(_L("Error = %d"), iStatus.Int());
       
   140     Trace(_L("State = %d"), iHandlerState);
       
   141     
       
   142     iReply.Zero();
       
   143     
       
   144     TInt err = iStatus.Int();
       
   145     if(err == KErrNone)
       
   146         {
       
   147         switch (iHandlerState)
       
   148             {
       
   149             case ECMGDStateRetrieveAllIndices:
       
   150                 {
       
   151                 GetMessageIndexListL();
       
   152                 iDeleteList.Reset();
       
   153                 break;
       
   154                 }
       
   155             case ECMGDStateDeleteOneEntry:                
       
   156             case ECMGDStateDeleteAllEntries:
       
   157                 {
       
   158                 // do nothing
       
   159                 break;
       
   160                 }
       
   161             case ECMGDStateDeleteFilteredEntries:
       
   162                 {
       
   163                 FilterMessageListL();
       
   164                 break;
       
   165                 }
       
   166             default:
       
   167                 {
       
   168                 iHandlerState = ECMGDStateIdle;
       
   169                 iCallback->CreateReplyAndComplete(EReplyTypeError);
       
   170                 TRACE_FUNC_EXIT
       
   171                 return;
       
   172                 }
       
   173             }
       
   174         
       
   175         if (iDeleteList.Count() != 0)
       
   176             {
       
   177             // delete the first entry in the list
       
   178             iMobileSmsStore.Delete(iStatus, iDeleteList[0]);
       
   179             iDeleteList.Remove(0);
       
   180             iHandlerState = ECMGDStateDeleteOneEntry;
       
   181             SetActive();                    
       
   182             }
       
   183         else
       
   184             {
       
   185             // deleting finished
       
   186             iCallback->CreateReplyAndComplete(EReplyTypeOk, iReply);
       
   187             }
       
   188         }
       
   189     else if (err == KErrNotFound)
       
   190         {
       
   191         // no message found in SIM
       
   192         switch (iHandlerState)
       
   193             {
       
   194             case ECMGDStateRetrieveAllIndices:
       
   195                 {
       
   196                 iHandlerState = ECMGDStateIdle;
       
   197                 iReply.Format(KCMGDSupportedCmdsList, &KNullDesC8);
       
   198                 iCallback->CreateReplyAndComplete(EReplyTypeOk, iReply);
       
   199                 break;
       
   200                 }
       
   201             case ECMGDStateDeleteFilteredEntries:
       
   202                 {
       
   203                 iMobileSmsStore.DeleteAll(iStatus);
       
   204                 iHandlerState = ECMGDStateDeleteAllEntries;
       
   205                 SetActive();
       
   206                 break;
       
   207                 }
       
   208             default:
       
   209                 iHandlerState = ECMGDStateIdle;
       
   210                 iCallback->CreateCMSReplyAndComplete(err);
       
   211                 break;
       
   212             }
       
   213         }
       
   214     else 
       
   215         {
       
   216         iHandlerState = ECMGDStateIdle;
       
   217         iCallback->CreateCMSReplyAndComplete(err);
       
   218         }
       
   219 
       
   220     TRACE_FUNC_EXIT
       
   221     }
       
   222 
       
   223 TInt CCMGDCommandHandler::RunError(TInt aError)
       
   224     {
       
   225     TRACE_FUNC_ENTRY
       
   226     
       
   227     iCallback->CreateCMSReplyAndComplete(aError);
       
   228     
       
   229     TRACE_FUNC_EXIT
       
   230     return KErrNone;
       
   231     }
       
   232 
       
   233 void CCMGDCommandHandler::DoCancel() 
       
   234     {
       
   235     TRACE_FUNC_ENTRY
       
   236     
       
   237     switch (iHandlerState)
       
   238         {
       
   239         case ECMGDStateRetrieveAllIndices:
       
   240         case ECMGDStateDeleteFilteredEntries:
       
   241             {
       
   242             iRetrieveSmsList->Cancel();
       
   243             break;
       
   244             }
       
   245         case ECMGDStateDeleteOneEntry:
       
   246             {
       
   247             iMobileSmsStore.CancelAsyncRequest(EMobilePhoneStoreDelete);
       
   248             break;
       
   249             }
       
   250         case ECMGDStateDeleteAllEntries:
       
   251             {
       
   252             iMobileSmsStore.CancelAsyncRequest(EMobilePhoneStoreDeleteAll);
       
   253             break;
       
   254             }
       
   255         }
       
   256 
       
   257     TRACE_FUNC_EXIT
       
   258     }
       
   259 
       
   260 TInt CCMGDCommandHandler::ParseParameters()
       
   261     {
       
   262     TRACE_FUNC_ENTRY
       
   263     
       
   264     TInt ret = KErrNone;
       
   265     TInt index = 0;
       
   266     TInt delflag = 0; // default value
       
   267     TInt otherParams = 0;
       
   268     
       
   269     // get index 
       
   270     TInt retIndex = iATCmdParser.NextIntParam(index);
       
   271     // get delflag
       
   272     TInt retDelflag = iATCmdParser.NextIntParam(delflag);
       
   273     // get other params
       
   274     TInt retOther = iATCmdParser.NextIntParam(otherParams);
       
   275     // Syntax error happens if
       
   276     // a)there is no param2 or param2 error 
       
   277     // b)there is no param1 but param2==0
       
   278     // c)there are too many params
       
   279     TBool noParam = (retIndex!=KErrNone && retDelflag!=KErrNone || retDelflag==KErrGeneral );
       
   280     TBool noParam1 = (retIndex!=KErrNone && retDelflag==KErrNone && delflag==0);
       
   281     TBool tooManyParams = (retOther!=KErrNotFound);
       
   282     
       
   283     if (noParam || noParam1 || tooManyParams)
       
   284         {
       
   285         ret = KErrArgument;
       
   286         }
       
   287     else
       
   288         {
       
   289         iIndex = index;
       
   290         iDelFlag = delflag;
       
   291         }
       
   292 
       
   293     TRACE_FUNC_EXIT
       
   294     return ret;
       
   295     }
       
   296 
       
   297 TInt CCMGDCommandHandler::DoHandleCommand()
       
   298     {
       
   299     TRACE_FUNC_ENTRY
       
   300     
       
   301     TInt err = KErrNone;
       
   302     
       
   303     switch (iDelFlag)
       
   304         {
       
   305         case 0:
       
   306             // deletes the designated message with <index>.
       
   307             {
       
   308             if (iIndex == 0)
       
   309                 {
       
   310                 // The first position in SIM store is 1,
       
   311                 // so zero is not in the supported range.
       
   312                 err = KErrGsmSMSInvalidMemoryIndex;
       
   313                 }
       
   314             else
       
   315                 {
       
   316                 iDeleteList.Reset();
       
   317                 iMobileSmsStore.Delete(iStatus, iIndex);
       
   318                 iHandlerState = ECMGDStateDeleteOneEntry;
       
   319                 SetActive();
       
   320                 }
       
   321             }
       
   322             break;
       
   323         case 1:
       
   324             // deletes all read message in first-choice memory and reserves unread message, 
       
   325             // sent message and unsent message.
       
   326             {
       
   327             iRetrieveSmsList->Start(iStatus);
       
   328             iHandlerState = ECMGDStateDeleteFilteredEntries;
       
   329             iFilterType = ECMGDFilterReadMessagesOnly;
       
   330             SetActive();
       
   331             }
       
   332             break;
       
   333         case 2:
       
   334             // deletes all read message and sent message in first-choice memory 
       
   335             // and reserves unread message and unsent message.
       
   336             {
       
   337             iRetrieveSmsList->Start(iStatus);
       
   338             iHandlerState = ECMGDStateDeleteFilteredEntries;
       
   339             iFilterType = ECMGDFilterReadAndSentMessages;
       
   340             SetActive();            
       
   341             }
       
   342             break;
       
   343         case 3:
       
   344             // deletes all read message, sent message and unsent message in first-choice memory 
       
   345             // and reserves unread message.
       
   346             {
       
   347             iRetrieveSmsList->Start(iStatus);
       
   348             iHandlerState = ECMGDStateDeleteFilteredEntries;
       
   349             iFilterType = ECMGDFilterReadSentAndUnsentMessages;
       
   350             SetActive();            
       
   351             }
       
   352             break;
       
   353         case 4:
       
   354             // deletes all messages in first-choice memory, including unread message.
       
   355             {
       
   356             iDeleteList.Reset();
       
   357             iMobileSmsStore.DeleteAll(iStatus);
       
   358             iHandlerState = ECMGDStateDeleteAllEntries;
       
   359             SetActive();
       
   360             }
       
   361             break;
       
   362             
       
   363         default:
       
   364             err = KErrNotSupported;
       
   365             break;
       
   366         }
       
   367     
       
   368     TRACE_FUNC_EXIT
       
   369     return err;
       
   370     }
       
   371 
       
   372 void CCMGDCommandHandler::GetMessageIndexListL()
       
   373     {
       
   374     TRACE_FUNC_ENTRY
       
   375     
       
   376     iReply.Zero();
       
   377     delete iGsmSmsList;
       
   378     iGsmSmsList = NULL;
       
   379     
       
   380     RBuf8 list;
       
   381     list.CreateL(KDefaultListLength);
       
   382     CleanupClosePushL(list);
       
   383 
       
   384     iGsmSmsList = iRetrieveSmsList->RetrieveGsmListL();
       
   385     TInt entriesNum = iGsmSmsList->Enumerate();
       
   386     
       
   387     if (entriesNum > 0)
       
   388         {
       
   389         // Append 1st item
       
   390         RMobileSmsStore::TMobileGsmSmsEntryV1 entry;
       
   391         entry = iGsmSmsList->GetEntryL(0);
       
   392         list.AppendNum(entry.iIndex);
       
   393         
       
   394         for (TInt i=1; i<entriesNum; ++i)
       
   395             {
       
   396             entry = iGsmSmsList->GetEntryL(i);
       
   397             list.Append(',');
       
   398             list.AppendNum(entry.iIndex);
       
   399             }
       
   400         }
       
   401     iReply.Format(KCMGDSupportedCmdsList, &list);
       
   402     
       
   403     CleanupStack::PopAndDestroy(&list); //list
       
   404     
       
   405     TRACE_FUNC_EXIT
       
   406     }
       
   407 
       
   408 /**
       
   409  * Filter message list with iFilterType
       
   410  * ECMGDFilterReadSentAndUnsentMessages: read, sent and unsent messages
       
   411  * ECMGDFilterReadAndSentMessages: read and sent messages
       
   412  * ECMGDFilterReadMessagesOnly: read messages
       
   413  */
       
   414 void CCMGDCommandHandler::FilterMessageListL()
       
   415     {
       
   416     TRACE_FUNC_ENTRY
       
   417     
       
   418     delete iGsmSmsList;
       
   419     iGsmSmsList = NULL;
       
   420         
       
   421     iDeleteList.Reset();
       
   422     
       
   423     iGsmSmsList = iRetrieveSmsList->RetrieveGsmListL();
       
   424     TInt entriesNum = iGsmSmsList->Enumerate();
       
   425     
       
   426     for (TInt i=0; i<entriesNum; ++i)
       
   427         {
       
   428         RMobileSmsStore::TMobileGsmSmsEntryV1 entry = iGsmSmsList->GetEntryL(i);
       
   429         switch (iFilterType)
       
   430             {
       
   431             case ECMGDFilterReadSentAndUnsentMessages:
       
   432                 {
       
   433                 // add unsent message 
       
   434                 if (entry.iMsgStatus == RMobileSmsStore::EStoredMessageUnsent)
       
   435                     {
       
   436                     iDeleteList.AppendL(entry.iIndex);
       
   437                     break;
       
   438                     }
       
   439                 }
       
   440             case ECMGDFilterReadAndSentMessages:
       
   441                 {
       
   442                 // add sent message 
       
   443                 if (entry.iMsgStatus == RMobileSmsStore::EStoredMessageSent)
       
   444                     {
       
   445                     iDeleteList.AppendL(entry.iIndex);
       
   446                     break;
       
   447                     }
       
   448                 }
       
   449             case ECMGDFilterReadMessagesOnly:
       
   450                 {
       
   451                 // add read message 
       
   452                 if (entry.iMsgStatus == RMobileSmsStore::EStoredMessageRead )
       
   453                     {
       
   454                     iDeleteList.AppendL(entry.iIndex);
       
   455                     break;
       
   456                     }
       
   457                 }
       
   458             }
       
   459         }
       
   460     
       
   461     TRACE_FUNC_EXIT
       
   462     }