commands/sms/sms.cpp
changeset 0 7f656887cf89
equal deleted inserted replaced
-1:000000000000 0:7f656887cf89
       
     1 // sms.cpp
       
     2 // 
       
     3 // Copyright (c) 2008 - 2010 Accenture. All rights reserved.
       
     4 // This component and the accompanying materials are made available
       
     5 // under the terms of the "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 // Accenture - Initial contribution
       
    11 //
       
    12 
       
    13 #include "sms.h"
       
    14 
       
    15 CCmdSms* CCmdSms::NewLC()
       
    16 	{
       
    17 	CCmdSms* self = new (ELeave) CCmdSms();
       
    18 	CleanupStack::PushL(self);
       
    19 	self->BaseConstructL();
       
    20 	return self;
       
    21 	}
       
    22 
       
    23 CCmdSms::~CCmdSms()
       
    24 	{
       
    25 	delete iCompletionCallBack;
       
    26 	delete iWatch;
       
    27 	delete iSendMsg;
       
    28 	delete iMsgToSend;
       
    29 	delete iDestination;
       
    30 	delete iMessageFromStdin;
       
    31 	iMatchStrings.ResetAndDestroy();
       
    32 	}
       
    33 
       
    34 CCmdSms::CCmdSms():
       
    35 CCommandBase(CCommandBase::EManualComplete)
       
    36 	{
       
    37 	}
       
    38 
       
    39 const TDesC& CCmdSms::Name() const
       
    40 	{
       
    41 	_LIT(KName, "sms");	
       
    42 	return KName;
       
    43 	}
       
    44 
       
    45 //
       
    46 // CCmdSms::DoRunL
       
    47 //
       
    48 void CCmdSms::DoRunL()
       
    49 	{
       
    50 	iCompletionCallBack = new(ELeave) CAsyncCallBack(TCallBack(CompletionCallBack, this), CActive::EPriorityStandard);
       
    51 
       
    52 	switch (iOperation)
       
    53 		{
       
    54 	case ESend:
       
    55 			{
       
    56 			if (!iDestination)
       
    57 				{
       
    58 				LeaveIfErr(KErrArgument, _L("Option --destination not specified for send."));
       
    59 				}
       
    60 			if (!iMsgToSend)
       
    61 				{
       
    62 				iMessageFromStdin = CTextBuffer::NewL(160);
       
    63 				Stdin().SetReadMode(RIoReadHandle::EFull);
       
    64 				TBuf<0x100> buf;
       
    65 				while (Stdin().Read(buf) == KErrNone)
       
    66 					{
       
    67 					iMessageFromStdin->AppendL(buf);
       
    68 					}
       
    69 				
       
    70 				iSendMsg = CSmsSender::NewL(FsL(), *this, iMessageFromStdin->Descriptor(), *iDestination);
       
    71 				}
       
    72 			else
       
    73 				{
       
    74 				iSendMsg = CSmsSender::NewL(FsL(), *this, *iMsgToSend, *iDestination);
       
    75 				}
       
    76 			iSendMsg->StartL();
       
    77 			}
       
    78 		break;
       
    79 	case EReceive:
       
    80 			{
       
    81 			FsL(); // So that CSmsWatcher can call Fs() if it needs to
       
    82 			iWatch = CSmsWatcher::NewL(*this);
       
    83 			iWatch->WaitForMessage();
       
    84 			}
       
    85 		break;
       
    86 		};
       
    87 	}
       
    88 
       
    89 void CCmdSms::ArgumentsL(RCommandArgumentList& aArguments)
       
    90 	{
       
    91 	aArguments.AppendEnumL((TInt&)iOperation, _L("command"));
       
    92 	aArguments.AppendStringL(iMsgToSend, _L("message"));
       
    93 	}
       
    94 
       
    95 //
       
    96 // CCmdSms::OptionsL
       
    97 //
       
    98 void CCmdSms::OptionsL(RCommandOptionList& aOptions)
       
    99 	{
       
   100 	// send sms options
       
   101 	_LIT(KOptDestination, "destination");
       
   102 	aOptions.AppendStringL(iDestination, KOptDestination);
       
   103 	
       
   104 	// receive sms options
       
   105 	_LIT(KOptDebug, "no-delete");
       
   106 	aOptions.AppendBoolL(iDebug, KOptDebug);
       
   107 	
       
   108 	_LIT(KOptMatch, "match");
       
   109 	aOptions.AppendStringL(iMatchStrings, KOptMatch);
       
   110 	}
       
   111 
       
   112 //
       
   113 // CCmdSms::SendComplete
       
   114 // up-call from the underlying smssender to indicate
       
   115 // it has finished its task
       
   116 //
       
   117 void CCmdSms::SendComplete(TInt aError)
       
   118 	{
       
   119 	EventsFinished(aError);
       
   120 	}
       
   121 
       
   122 //
       
   123 // CCmdSms::EventsFinished
       
   124 // up-call from the underlying smswatcher to indicate
       
   125 // it has finished its task(s)
       
   126 //
       
   127 void CCmdSms::EventsFinished(const TInt aError)
       
   128 	{
       
   129 	iCompletionError = aError;
       
   130 	iCompletionCallBack->CallBack();
       
   131 	}
       
   132 
       
   133 //
       
   134 // CCmdSms::CompletionCallBack
       
   135 // static function called in the event the cmd has finished executing
       
   136 // 
       
   137 TInt CCmdSms::CompletionCallBack(TAny* aEvent)
       
   138 	{
       
   139 	CCmdSms* self = static_cast<CCmdSms*>(aEvent);
       
   140 	self->Cleanup();
       
   141 	return KErrNone;
       
   142 	}
       
   143 
       
   144 //
       
   145 // CCmdSms::Cleanup
       
   146 // 
       
   147 void CCmdSms::Cleanup()
       
   148 	{
       
   149 	if (iCompletionError)
       
   150 		PrintError(iCompletionError, _L("amsms.exe failure"));
       
   151 	else
       
   152 		{
       
   153 		if (iWatch)
       
   154 			{
       
   155 			// dump any underlying messages (if any!)
       
   156 			const TDesC& msg = iWatch->Message();
       
   157 			if (msg.Length() > 0)
       
   158 				Printf(_L("Message received: \r\n"), &msg); // dump the message to stdout
       
   159 			}
       
   160 		}
       
   161 	// complete the fshell command
       
   162 	Complete(iCompletionError);
       
   163 	}
       
   164 
       
   165 EXE_BOILER_PLATE(CCmdSms);
       
   166