networkingtestandutils/networkingintegrationtest/NTRas/CHAT.CPP
changeset 0 af10295192d8
equal deleted inserted replaced
-1:000000000000 0:af10295192d8
       
     1 // Copyright (c) 1997-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 "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".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include "chat.h"
       
    17 
       
    18 CCommChatString* CCommChatString::NewL(const TDesC8& aDes, TBool aIsFolded)
       
    19 //
       
    20 //
       
    21 //
       
    22 	{
       
    23 	CCommChatString* cs = new (ELeave) CCommChatString;
       
    24 	CleanupStack::PushL(cs);
       
    25 	cs->CreateL(aDes, aIsFolded);
       
    26 	CleanupStack::Pop();
       
    27 	return cs;
       
    28 	}
       
    29 
       
    30 CCommChatString::CCommChatString()
       
    31 //
       
    32 //
       
    33 //
       
    34 	{
       
    35 	__DECLARE_NAME(_S("CCommChatString"));
       
    36 	}
       
    37 
       
    38 CCommChatString::~CCommChatString()
       
    39 //
       
    40 //
       
    41 //
       
    42 	{
       
    43 	User::Free(iString);
       
    44 	}
       
    45 
       
    46 void CCommChatString::CreateL(const TDesC8& aDes, TBool aIsFolded)
       
    47 //
       
    48 //
       
    49 //
       
    50 	{
       
    51 	iIsFolded = aIsFolded;
       
    52 	iString = (TText8*) User::AllocL(aDes.Length());
       
    53 	iLastChar = (iString+aDes.Length())-1;
       
    54 	Mem::Copy(iString, (TUint8*)aDes.Ptr(), aDes.Length());
       
    55 	}
       
    56 
       
    57 CCommChatter::CCommChatter(MCommChatNotify* aNotify, TInt aPriority)
       
    58 //
       
    59 //
       
    60 //
       
    61 	: CTimer(aPriority), iNotify(aNotify)
       
    62 	{
       
    63 	__DECLARE_NAME(_S("CCommChatter"));
       
    64 	CActiveScheduler::Add(this);
       
    65 	iList.SetOffset(_FOFF(CCommChatString,iLink));
       
    66 	}
       
    67 
       
    68 CCommChatter::~CCommChatter()
       
    69 //
       
    70 //
       
    71 //
       
    72 	{
       
    73 	DeleteAllAndStop();
       
    74 	User::Free(iBuffer);
       
    75 	}
       
    76 
       
    77 void CCommChatter::CreateL(TInt aBufSize)
       
    78 //
       
    79 //
       
    80 //
       
    81 	{
       
    82 	CTimer::ConstructL();
       
    83 	iBuffer = (TText8*)User::AllocL(aBufSize);
       
    84 	iBufferEnd = (iBuffer+aBufSize)-1;
       
    85 	ClearHistory();
       
    86 	}
       
    87 
       
    88 void CCommChatter::ClearHistory()
       
    89 //
       
    90 // Empty history
       
    91 //
       
    92 	{
       
    93 	iLastChar = iBuffer;
       
    94 	iCount = 0;
       
    95 	}
       
    96 
       
    97 void CCommChatter::AddChar(TText8 aChar)
       
    98 //
       
    99 // Add a character to the history buffer
       
   100 // Scan all strings to find any matches that may
       
   101 // be completed.
       
   102 //
       
   103 	{
       
   104 	// Add char to buffer
       
   105 	if (++iLastChar>iBufferEnd)
       
   106 		iLastChar = iBuffer;
       
   107 	*iLastChar = aChar;
       
   108 	++iCount;
       
   109 
       
   110 	TText8 fchar = (TText8)User::Fold(aChar);
       
   111 
       
   112 	// Scan for matching last character	
       
   113 	CCommChatString* cs;
       
   114 	TDblQueIter<CCommChatString> iter(iList);
       
   115 	
       
   116 	while (cs = iter++, cs!=NULL)
       
   117 		{
       
   118 		if (cs->IsFolded()
       
   119 			? (cs->LastChar()==fchar && MatchF(cs))
       
   120 			: (cs->LastChar()==aChar && Match(cs)) )
       
   121 			{
       
   122 			iNotify->ChatStringMatch(cs);
       
   123 			cs = iter;	// In case user removed cs;
       
   124 			}
       
   125 		}
       
   126 	}
       
   127 
       
   128 
       
   129 TBool CCommChatter::Match(const CCommChatString* aString) const
       
   130 //
       
   131 // Match a chat sgring against the history buffer
       
   132 // (Case sensitive)
       
   133 //
       
   134 	{
       
   135 	const TText8* s = aString->Ptr();
       
   136 	const TText8* sp = aString->EndPtr();
       
   137 	const TText8* bp = iLastChar;
       
   138 
       
   139 	if (iCount<aString->Length())
       
   140 		return EFalse;
       
   141 
       
   142 	while (*bp==*sp && sp>=s)
       
   143 		{
       
   144 		--sp;
       
   145 		if (--bp<iBuffer)
       
   146 			bp = iBufferEnd;
       
   147 		}
       
   148 
       
   149 	return sp<s;
       
   150 	}
       
   151 
       
   152 TBool CCommChatter::MatchF(const CCommChatString* aString) const
       
   153 //
       
   154 // Match a folded chat sgring against the history buffer.
       
   155 // (Case insensitive)
       
   156 //
       
   157 	{
       
   158 	const TText8* s = aString->Ptr();
       
   159 	const TText8* sp = aString->EndPtr();
       
   160 	const TText8* bp = iLastChar;
       
   161 
       
   162 	if (iCount<aString->Length())
       
   163 		return EFalse;
       
   164 
       
   165 	while (User::Fold(*bp)==*sp && sp>=s)
       
   166 		{
       
   167 		--sp;
       
   168 		if (--bp<iBuffer)
       
   169 			bp = iBufferEnd;
       
   170 		}
       
   171 
       
   172 	return sp<s;
       
   173 	}
       
   174 
       
   175 void CCommChatter::RunL()
       
   176 //
       
   177 // Timer completed
       
   178 //
       
   179 	{
       
   180 	iNotify->ChatTimeout();
       
   181 	}
       
   182 
       
   183 void CCommChatter::AddString(CCommChatString* aString)
       
   184 //
       
   185 // Add a string to the list
       
   186 //
       
   187 	{
       
   188 	iList.AddLast(*aString);
       
   189 	}
       
   190 
       
   191 void CCommChatter::RemoveString(CCommChatString* aString)
       
   192 //
       
   193 // Remove a string
       
   194 //
       
   195 	{
       
   196 	aString->iLink.Deque();
       
   197 	}
       
   198 
       
   199 void CCommChatter::DeleteAllAndStop()
       
   200 //
       
   201 // Remove a string
       
   202 //
       
   203 	{
       
   204 	StopTimer();
       
   205 	CCommChatString* cs;
       
   206 	while (!iList.IsEmpty())
       
   207 		{
       
   208 		cs = iList.First();
       
   209 		RemoveString(cs);
       
   210 		delete cs;
       
   211 		}
       
   212 	}
       
   213 
       
   214 void CCommChatter::StartTimer(TTimeIntervalMicroSeconds32 aTimeout)
       
   215 //
       
   216 //
       
   217 //
       
   218 	{
       
   219 	if (IsActive())
       
   220 		Cancel();
       
   221 	After(aTimeout);
       
   222 	}
       
   223 
       
   224 void CCommChatter::StopTimer()
       
   225 //
       
   226 //
       
   227 //
       
   228 	{
       
   229 	Cancel();
       
   230 	}