email/pop3andsmtpmtm/servermtmutils/src/IMSKSCR.CPP
changeset 0 72b543305e3a
equal deleted inserted replaced
-1:000000000000 0:72b543305e3a
       
     1 // Copyright (c) 1998-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 "IMSKSCR.H"
       
    17 #include "IMSK.H"
       
    18 #include "IMCVTEXT.H"
       
    19 
       
    20 
       
    21 _LIT8(K_IM_SCRIPT_WAIT_STRING, "** Read cancelled");
       
    22 
       
    23 CImTextServerScript::CImTextServerScript() : CActive(EPriorityStandard)
       
    24 	{
       
    25 	}
       
    26 
       
    27 CImTextServerScript *CImTextServerScript::NewL(TInt aPortNum)
       
    28 	{
       
    29 	CImTextServerScript* self =new(ELeave)CImTextServerScript();
       
    30 	CleanupStack::PushL(self);
       
    31 	self->ConstructL(aPortNum);
       
    32 	CleanupStack::Pop();
       
    33 	return self;
       
    34 	}
       
    35 //
       
    36 // 2nd phase of construction
       
    37 //
       
    38 void CImTextServerScript::ConstructL(TInt aPortNum)
       
    39 	{
       
    40 	CActiveScheduler::Add(this);
       
    41 	User::LeaveIfError(iFs.Connect());
       
    42 
       
    43 	TBuf<35> scriptFile; 
       
    44 	scriptFile.Format(TRefByValue<const TDesC>_L("c:\\Logs\\email\\imsk%d.scr"),aPortNum);
       
    45 	User::LeaveIfError(iFile.Open(iFs,scriptFile,EFileShareAny));
       
    46 
       
    47 #ifndef _UNICODE
       
    48 	iScriptFileText.Set(iFile);
       
    49 #endif
       
    50 
       
    51 	}
       
    52 
       
    53 CImTextServerScript::~CImTextServerScript()
       
    54 	{
       
    55 	iFile.Close();
       
    56 	iFs.Close();
       
    57 	}
       
    58 
       
    59 void CImTextServerScript::RetrieveResponse(TDes8& theBuffer, TRequestStatus& aStatus)
       
    60 	{
       
    61 	iReportStatus = &aStatus;
       
    62     TInt fileReadErr = KErrNone;
       
    63 
       
    64 #ifndef _UNICODE
       
    65 	fileReadErr = iScriptFileText.Read(iData);
       
    66 #else
       
    67     //  _UNICODE read one line ASCII data
       
    68     fileReadErr = ReadLine();
       
    69 #endif
       
    70 
       
    71 	iNextFileSeg.Copy(iData);
       
    72 
       
    73 	if( fileReadErr == KErrNone)
       
    74 		{
       
    75 		theBuffer.Append(iNextFileSeg);
       
    76 		}
       
    77 	else if(fileReadErr == KErrTooBig)
       
    78 		{
       
    79 		for(;;)
       
    80 			{
       
    81 			// read response from file
       
    82 #ifndef _UNICODE
       
    83 			fileReadErr = iScriptFileText.Read(iData);
       
    84 #else
       
    85             //  _UNICODE read one line ASCII data
       
    86             fileReadErr = ReadLine();
       
    87 #endif
       
    88 			iNextFileSeg.Copy(iData);
       
    89 			TInt crLfPos = iNextFileSeg.Find(KImcvCRLF);
       
    90 			if(crLfPos == KErrNotFound)
       
    91 				{
       
    92 				theBuffer.Append(iNextFileSeg);
       
    93 				break;
       
    94 				}
       
    95 			else
       
    96 				{
       
    97 				theBuffer.Append(iNextFileSeg.Left(crLfPos));
       
    98 				}
       
    99 			}
       
   100 		}
       
   101 
       
   102 	iStatus = KRequestPending;
       
   103 	aStatus = KRequestPending;
       
   104 	SetActive();
       
   105 
       
   106 	if ((theBuffer.CompareF(K_IM_SCRIPT_WAIT_STRING) != 0)
       
   107 		&& (fileReadErr != KErrEof))
       
   108 		{
       
   109 		theBuffer.Append(KImcvCRLF);
       
   110 		TRequestStatus* status = &iStatus;
       
   111 		User::RequestComplete(status, KErrNone);
       
   112 		}
       
   113 	}
       
   114 
       
   115 //
       
   116 //  ReadLine() read ASCII into TBuf8 thingy if unicode build.
       
   117 //
       
   118 #ifdef _UNICODE
       
   119 TInt CImTextServerScript::ReadLine()
       
   120     {
       
   121     TInt err = KErrNone;
       
   122 
       
   123     //  Read into the buffer
       
   124     TBuf8<256> buffer;              // Max read of the Read() function..    
       
   125     iData.Zero();
       
   126 
       
   127     //  Get the current file position
       
   128     TInt filePos = 0;
       
   129     iFile.Seek(ESeekCurrent, filePos);
       
   130 
       
   131     //  Read the buffer
       
   132     err = iFile.Read(buffer);
       
   133     
       
   134     //end of file?
       
   135     TInt s = buffer.Length();
       
   136     if( s == 0)
       
   137         {
       
   138         err = KErrEof;
       
   139         }
       
   140 
       
   141     if(err == KErrNone)
       
   142         {
       
   143         //  Copy to the lfcr and then set the file pointer
       
   144         //  to the point after that...
       
   145         TInt pos = buffer.Find(KImcvCRLF);
       
   146         if( pos != -1)
       
   147             {
       
   148             iData.Justify(buffer, pos, ELeft, ' ');
       
   149             filePos += (pos+2);
       
   150 
       
   151             //  Set the file pointer back to after the lfcr..
       
   152             iFile.Seek(ESeekStart, filePos);                
       
   153             }
       
   154         //  Else fill the whole buffer 256 chars..
       
   155         else
       
   156             {
       
   157             iData.Copy(buffer);
       
   158             }
       
   159         }
       
   160     return err;
       
   161     }
       
   162 #endif
       
   163 
       
   164 void CImTextServerScript::RunL()
       
   165 	{
       
   166 	User::RequestComplete(iReportStatus, KErrNone);
       
   167 	}
       
   168 
       
   169 void CImTextServerScript::DoCancel()
       
   170 	{
       
   171 	if (IsActive())
       
   172 		{
       
   173 		if (iStatus == KRequestPending)
       
   174 			{
       
   175 			TRequestStatus* status = &iStatus;
       
   176 			User::RequestComplete(status, KErrNone);
       
   177 			}
       
   178 		User::RequestComplete(iReportStatus, KErrNone);
       
   179 		}
       
   180 	}
       
   181