Update contrib.
1 // Copyright (c) 1996-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of the License "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // f32\etshell\ts_clicomp.cpp
18 #include "ts_clicomp.h"
21 CCliCompleter::CCliCompleter()
25 CCliCompleter* CCliCompleter::NewL()
27 CCliCompleter* self = new(ELeave) CCliCompleter();
28 CleanupStack::PushL(self);
35 void CCliCompleter::ConstructL()
37 User::LeaveIfError(iFs.Connect());
40 CCliCompleter::~CCliCompleter()
45 void CCliCompleter::EstablishCompletionContext(TDesC& aBuf, TUint aPos, TDes& aKnownPart)
47 // Try and locate a double-quote to the left of the current cursor position
48 TInt contextStart = aBuf.Mid(0, aPos).LocateReverse(TChar('"'));
51 if(contextStart != KErrNotFound)
52 { // Double-quote found
53 contextEnd = aPos - contextStart;
54 // Grab a copy of the text we will be trying to complete
55 aKnownPart = aBuf.Mid(contextStart, contextEnd);
58 { // Try from the last space character
59 contextStart = aBuf.Mid(0, aPos).LocateReverse(TChar(' '));
61 if(contextStart == KErrNotFound)
70 contextEnd = aPos - contextStart;
72 aKnownPart = aBuf.Mid(contextStart, contextEnd);
77 TBool CCliCompleter::AttemptCompletionL(TDes& aKnownPart, RPointerArray<HBufC>& aAlternatives)
79 // Turn the known part into something with a path
80 TParse parsedKnownPart;
82 // ShellFunction::GetFullPath() modifies the source path so create
83 // a temporary buffer for a throwaway copy
84 TBuf<KMaxPath> tmpKnownPart = aKnownPart;
85 ShellFunction::StripQuotes(tmpKnownPart);
86 tmpKnownPart.Append(KMatchAny);
88 if(ShellFunction::GetFullPath(tmpKnownPart, parsedKnownPart) != KErrNone)
94 if(iFs.GetDir(parsedKnownPart.FullName(), KEntryAttNormal | KEntryAttDir, ESortByName, entries) != KErrNone)
99 CleanupStack::PushL(entries);
104 if(entries->Count() == 1)
106 entry = (*entries)[0];
109 completedPart.Set(entry.iName.Mid(parsedKnownPart.NameAndExt().Length() - 1));
110 aKnownPart.Append(completedPart);
112 if(entry.IsDir()) aKnownPart.Append(KPathDelimiter);
114 if(((TUint)aKnownPart[0] != TChar('"')) &&
115 (aKnownPart.LocateF(TChar(' ')) != KErrNotFound))
117 aKnownPart.Insert(0, _L("\""));
122 else if(entries->Count() > 1)
126 // Find the smallest matching entry so as to not run off the end of
127 // an index when trying to establish the greatest overlap between the
128 // matches. We're also caching the raw entries in a seperate array so
129 // we can use them when displaying the ambiguous matches.
130 TInt minLength = KMaxFileName;
132 for(entryIdx=0;entryIdx<entries->Count();entryIdx++)
134 entry = (*entries)[entryIdx];
136 HBufC* buf = HBufC::NewLC(entry.iName.Length() + 100);
140 buf->Des().Append(KPathDelimiter);
142 aAlternatives.AppendL(buf);
144 if(entry.iName.Length() < minLength)
146 minLength = entry.iName.Length();
149 CleanupStack::Pop(entries->Count());
151 // Find the greatest overlap between the matches. Even though we can't
152 // get a final match we can at least make a best-effort completion based
153 // on anything they've not told us but which matches anyway.
155 // There's an asterisk on the end of the parsed filename that we want
156 // to disregard when calculating the length of what we already know.
157 TInt knownPartLength = parsedKnownPart.NameAndExt().Length() - 1;
159 TInt matchLength = knownPartLength;
160 TBool matching = ETrue;
161 while(matching && matchLength < minLength)
163 for(entryIdx=1;entryIdx<entries->Count();entryIdx++)
165 if(((*entries)[0].iName[matchLength]) != ((*entries)[entryIdx].iName[matchLength]))
170 if(matching) matchLength++;
173 if(matchLength > knownPartLength)
175 entry = (*entries)[0];
177 TUint additionalKnownStart = knownPartLength;
178 TUint additionalKnownLength = matchLength - knownPartLength;
180 aKnownPart.Append(entry.iName.Mid(additionalKnownStart, additionalKnownLength));
190 CleanupStack::PopAndDestroy(entries);
197 Note: method has the potential to modify the size of aAlternatives as
198 a side-effect of the call to ShellFunction::AlignTextIntoColumns().
200 void CCliCompleter::DisplayAlternatives(RPointerArray<HBufC>& aAlternatives)
202 ShellFunction::AlignTextIntoColumns(aAlternatives);
205 for(TInt entryIdx=0;entryIdx<aAlternatives.Count();entryIdx++)
207 CShell::OutputStringToConsole(EFalse, *aAlternatives[entryIdx]);