commands/clipboard/clipboard.cpp
changeset 0 7f656887cf89
equal deleted inserted replaced
-1:000000000000 0:7f656887cf89
       
     1 // clipboard.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 <fshell/ioutils.h>
       
    14 #include <fshell/ltkutils.h>
       
    15 
       
    16 using namespace IoUtils;
       
    17 
       
    18 class CCmdClipboard : public CCommandBase
       
    19 	{
       
    20 public:
       
    21 	static CCommandBase* NewLC();
       
    22 	~CCmdClipboard();
       
    23 private:
       
    24 	CCmdClipboard();
       
    25 	void CopyToClipboardL(const TDesC& aBuf);
       
    26 	HBufC* GetFromClipboardL();
       
    27 
       
    28 private: // From CCommandBase.
       
    29 	virtual const TDesC& Name() const;
       
    30 	virtual void DoRunL();
       
    31 	virtual void ArgumentsL(RCommandArgumentList& aArguments);
       
    32 	virtual void OptionsL(RCommandOptionList& aOptions);
       
    33 private:
       
    34 	HBufC* iString;
       
    35 	TBool iPipe;
       
    36 	};
       
    37 
       
    38 
       
    39 CCommandBase* CCmdClipboard::NewLC()
       
    40 	{
       
    41 	CCmdClipboard* self = new(ELeave) CCmdClipboard();
       
    42 	CleanupStack::PushL(self);
       
    43 	self->BaseConstructL();
       
    44 	return self;
       
    45 	}
       
    46 
       
    47 CCmdClipboard::~CCmdClipboard()
       
    48 	{
       
    49 	delete iString;
       
    50 	}
       
    51 
       
    52 CCmdClipboard::CCmdClipboard()
       
    53 	{
       
    54 	}
       
    55 
       
    56 const TDesC& CCmdClipboard::Name() const
       
    57 	{
       
    58 	_LIT(KName, "clipboard");	
       
    59 	return KName;
       
    60 	}
       
    61 
       
    62 void CCmdClipboard::ArgumentsL(RCommandArgumentList& aArguments)
       
    63 	{
       
    64 	_LIT(KArg, "text");
       
    65 	aArguments.AppendStringL(iString, KArg);
       
    66 	}
       
    67 
       
    68 void CCmdClipboard::OptionsL(RCommandOptionList& aOptions)
       
    69 	{
       
    70 	_LIT(KOptStdin, "stdin");
       
    71 	aOptions.AppendBoolL(iPipe, KOptStdin);
       
    72 	}
       
    73 
       
    74 void CCmdClipboard::DoRunL()
       
    75 	{
       
    76 	if (iString)
       
    77 		{
       
    78 		CopyToClipboardL(*iString);
       
    79 		}
       
    80 	else if (iPipe)
       
    81 		{
       
    82 		// Copy to clipboard from stdin
       
    83 		CTextBuffer* buffer = CTextBuffer::NewLC(0x100);
       
    84 		Stdin().SetReadMode(RIoReadHandle::EOneOrMore);
       
    85 		TBuf<0x100> buf;
       
    86 		while (Stdin().Read(buf) == KErrNone)
       
    87 			{
       
    88 			buffer->AppendL(buf);
       
    89 			}
       
    90 
       
    91 		CopyToClipboardL(buffer->Descriptor());
       
    92 		CleanupStack::PopAndDestroy(buffer);
       
    93 		}
       
    94 	else
       
    95 		{
       
    96 		HBufC* res = NULL;
       
    97 		TRAPD(err, res = GetFromClipboardL());
       
    98 		LeaveIfErr(err, _L("Couldn't read clipboard (clipboard is empty?)"));
       
    99 
       
   100 		TBool cons = Stdout().AttachedToConsole();
       
   101 		if (cons) Printf(_L("Clipboard contents, length=%d:\r\n"), res->Length());
       
   102 		Write(*res);
       
   103 		if (cons) Write(_L("\r\n"));
       
   104 		delete res;
       
   105 		}
       
   106 	}
       
   107 
       
   108 void CCmdClipboard::CopyToClipboardL(const TDesC& aBuf)
       
   109 	{
       
   110 	LtkUtils::CopyToClipboardL(aBuf, &FsL());
       
   111 	}
       
   112 
       
   113 HBufC* CCmdClipboard::GetFromClipboardL()
       
   114 	{
       
   115 	return LtkUtils::GetFromClipboardL(&FsL());
       
   116 	}
       
   117 
       
   118 EXE_BOILER_PLATE(CCmdClipboard)
       
   119