core/src/job.cpp
changeset 0 7f656887cf89
equal deleted inserted replaced
-1:000000000000 0:7f656887cf89
       
     1 // job.cpp
       
     2 // 
       
     3 // Copyright (c) 2006 - 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 "job.h"
       
    14 #include "command_factory.h"
       
    15 
       
    16 
       
    17 //
       
    18 // CJob.
       
    19 //
       
    20 
       
    21 CJob* CJob::NewL(TInt aId, const TDesC& aCommandLine, RIoSession& aIoSession, RIoReadHandle& aStdin, RIoWriteHandle& aStdout, RIoWriteHandle& aStderr, IoUtils::CEnvironment& aEnv, CCommandFactory& aFactory, MJobObserver* aObserver)
       
    22 	{
       
    23 	CJob* self = new(ELeave) CJob(aId, aObserver, aStderr, aEnv);
       
    24 	CleanupStack::PushL(self);
       
    25 	self->ConstructL(aCommandLine, aIoSession, aStdin, aStdout, aStderr, aEnv, aFactory);
       
    26 	CleanupStack::Pop(self);
       
    27 	return self;
       
    28 	}
       
    29 
       
    30 CJob::~CJob()
       
    31 	{
       
    32 	delete iParser;
       
    33 	delete iCommandLine;
       
    34 	delete iCompletionCallBack;
       
    35 	}
       
    36 
       
    37 void CJob::Start(TBool& aIsForeground)
       
    38 	{
       
    39 	iParser->Start(aIsForeground);
       
    40 	}
       
    41 
       
    42 CJob::CJob(TInt aId, MJobObserver* aObserver, RIoWriteHandle& aStderr, IoUtils::CEnvironment& aEnv)
       
    43 	: iId(aId), iStatus(EPending), iObserver(aObserver), iCompletionError(aStderr, aEnv)
       
    44 	{
       
    45 	}
       
    46 
       
    47 void CJob::Kill()
       
    48 	{
       
    49 	iParser->Kill();
       
    50 	}
       
    51 
       
    52 void CJob::Suspend()
       
    53 	{
       
    54 	TInt err = iParser->Suspend();
       
    55 	if (err == KErrNone)
       
    56 		{
       
    57 		iStatus = EStopped;
       
    58 		}
       
    59 	}
       
    60 
       
    61 void CJob::Resume()
       
    62 	{
       
    63 	TInt err = iParser->Resume();
       
    64 	if (err == KErrNone)
       
    65 		{
       
    66 		iStatus = ERunning;
       
    67 		}
       
    68 	}
       
    69 
       
    70 TInt CJob::BringToForeground()
       
    71 	{
       
    72 	return iParser->BringToForeground();
       
    73 	}
       
    74 
       
    75 void CJob::SendToBackground()
       
    76 	{
       
    77 	iParser->SendToBackground();
       
    78 	}
       
    79 
       
    80 TInt CJob::Id() const
       
    81 	{
       
    82 	return iId;
       
    83 	}
       
    84 
       
    85 const TDesC* CJob::Name() const
       
    86 	{
       
    87 	return iCommandLine;
       
    88 	}
       
    89 
       
    90 CJob::TStatus CJob::Status() const
       
    91 	{
       
    92 	return iStatus;
       
    93 	}
       
    94 
       
    95 TInt CJob::Reattach(RIoEndPoint& aStdinEndPoint, RIoEndPoint& aStdoutEndPoint, RIoEndPoint& aStderrEndPoint)
       
    96 	{
       
    97 	return iParser->Reattach(aStdinEndPoint, aStdoutEndPoint, aStderrEndPoint);
       
    98 	}
       
    99 
       
   100 TBool CJob::IsDisownable() const
       
   101 	{
       
   102 	return iParser->IsDisownable();
       
   103 	}
       
   104 
       
   105 void CJob::Disown()
       
   106 	{
       
   107 	iParser->Disown();
       
   108 	}
       
   109 
       
   110 void CJob::ConstructL(const TDesC& aCommandLine, RIoSession& aIoSession, RIoReadHandle& aStdin, RIoWriteHandle& aStdout, RIoWriteHandle& aStderr, IoUtils::CEnvironment& aEnv, CCommandFactory& aFactory)
       
   111 	{
       
   112 	if (iObserver)
       
   113 		{
       
   114 		iCompletionCallBack = new(ELeave) CAsyncCallBack(TCallBack(CompletionCallBack, this), CActive::EPriorityStandard);
       
   115 		}
       
   116 	iCommandLine = aCommandLine.AllocL();
       
   117 	iParser = CParser::NewL(CParser::ENormal, *iCommandLine, aIoSession, aStdin, aStdout, aStderr, aEnv, aFactory, this);
       
   118 	iStatus = ERunning;
       
   119 	}
       
   120 
       
   121 TInt CJob::CompletionCallBack(TAny* aSelf)
       
   122 	{
       
   123 	CJob* self = static_cast<CJob*>(aSelf);
       
   124 	self->iObserver->HandleJobComplete(*self, self->iCompletionError);
       
   125 	return KErrNone;
       
   126 	}
       
   127 
       
   128 void CJob::HandleParserComplete(CParser& aParser, const TError& aError)
       
   129 	{
       
   130 	ASSERT(iParser == &aParser);
       
   131 	iCompletionError = aError;
       
   132 	if (iObserver)
       
   133 		{
       
   134 		iCompletionCallBack->CallBack();
       
   135 		}
       
   136 	}