| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023 | 
							- //---------------------------------------------------------------------------
 
- #include <vcl.h>
 
- #pragma hdrstop
 
- #include "Common.h"
 
- #include "Terminal.h"
 
- #include "Queue.h"
 
- #include "Exceptions.h"
 
- #include <System.DateUtils.hpp>
 
- //---------------------------------------------------------------------------
 
- #pragma package(smart_init)
 
- //---------------------------------------------------------------------------
 
- class TBackgroundTerminal;
 
- //---------------------------------------------------------------------------
 
- class TParallelTransferQueueItem : public TLocatedQueueItem
 
- {
 
- public:
 
-   __fastcall TParallelTransferQueueItem(const TLocatedQueueItem * ParentItem, TParallelOperation * ParallelOperation);
 
- protected:
 
-   virtual void __fastcall DoExecute(TTerminal * Terminal);
 
- private:
 
-   TParallelOperation * FParallelOperation;
 
- };
 
- //---------------------------------------------------------------------------
 
- class TUserAction
 
- {
 
- public:
 
-   virtual __fastcall ~TUserAction() {}
 
-   virtual void __fastcall Execute(void * Arg) = 0;
 
-   virtual bool __fastcall Force() { return false; }
 
- };
 
- //---------------------------------------------------------------------------
 
- class TNotifyAction : public TUserAction
 
- {
 
- public:
 
-   TNotifyAction(TNotifyEvent AOnNotify) :
 
-     OnNotify(AOnNotify)
 
-   {
 
-   }
 
-   virtual void __fastcall Execute(void * Arg)
 
-   {
 
-     if (OnNotify != NULL)
 
-     {
 
-       OnNotify(Sender);
 
-     }
 
-   }
 
-   TNotifyEvent OnNotify;
 
-   TObject * Sender;
 
- };
 
- //---------------------------------------------------------------------------
 
- class TInformationUserAction : public TUserAction
 
- {
 
- public:
 
-   TInformationUserAction(TInformationEvent AOnInformation) :
 
-     OnInformation(AOnInformation)
 
-   {
 
-   }
 
-   virtual void __fastcall Execute(void * Arg)
 
-   {
 
-     if (OnInformation != NULL)
 
-     {
 
-       OnInformation(Terminal, Str, Status, Phase, Additional);
 
-     }
 
-   }
 
-   virtual bool __fastcall Force()
 
-   {
 
-     // we need to propagate mainly the end-phase event even, when user cancels
 
-     // the connection, so that authentication window is closed
 
-     return TUserAction::Force() || (Phase >= 0);
 
-   }
 
-   TInformationEvent OnInformation;
 
-   TTerminal * Terminal;
 
-   UnicodeString Str;
 
-   bool Status;
 
-   int Phase;
 
-   UnicodeString Additional;
 
- };
 
- //---------------------------------------------------------------------------
 
- class TQueryUserAction : public TUserAction
 
- {
 
- public:
 
-   TQueryUserAction(TQueryUserEvent AOnQueryUser) :
 
-     OnQueryUser(AOnQueryUser)
 
-   {
 
-   }
 
-   virtual void __fastcall Execute(void * Arg)
 
-   {
 
-     if (OnQueryUser != NULL)
 
-     {
 
-       OnQueryUser(Sender, Query, MoreMessages, Answers, Params, Answer, Type, Arg);
 
-     }
 
-   }
 
-   TQueryUserEvent OnQueryUser;
 
-   TObject * Sender;
 
-   UnicodeString Query;
 
-   TStrings * MoreMessages;
 
-   unsigned int Answers;
 
-   const TQueryParams * Params;
 
-   unsigned int Answer;
 
-   TQueryType Type;
 
- };
 
- //---------------------------------------------------------------------------
 
- class TPromptUserAction : public TUserAction
 
- {
 
- public:
 
-   __fastcall TPromptUserAction(TPromptUserEvent AOnPromptUser) :
 
-     OnPromptUser(AOnPromptUser), Results(new TStringList())
 
-   {
 
-   }
 
-   virtual __fastcall ~TPromptUserAction()
 
-   {
 
-     delete Results;
 
-   }
 
-   virtual void __fastcall Execute(void * Arg)
 
-   {
 
-     if (OnPromptUser != NULL)
 
-     {
 
-       OnPromptUser(Terminal, Kind, Name, Instructions, Prompts, Results, Result, Arg);
 
-     }
 
-   }
 
-   TPromptUserEvent OnPromptUser;
 
-   TTerminal * Terminal;
 
-   TPromptKind Kind;
 
-   UnicodeString Name;
 
-   UnicodeString Instructions;
 
-   TStrings * Prompts;
 
-   TStrings * Results;
 
-   bool Result;
 
- };
 
- //---------------------------------------------------------------------------
 
- class TShowExtendedExceptionAction : public TUserAction
 
- {
 
- public:
 
-   __fastcall TShowExtendedExceptionAction(TExtendedExceptionEvent AOnShowExtendedException) :
 
-     OnShowExtendedException(AOnShowExtendedException)
 
-   {
 
-   }
 
-   virtual void __fastcall Execute(void * Arg)
 
-   {
 
-     if (OnShowExtendedException != NULL)
 
-     {
 
-       OnShowExtendedException(Terminal, E, Arg);
 
-     }
 
-   }
 
-   TExtendedExceptionEvent OnShowExtendedException;
 
-   TTerminal * Terminal;
 
-   Exception * E;
 
- };
 
- //---------------------------------------------------------------------------
 
- class TDisplayBannerAction : public TUserAction
 
- {
 
- public:
 
-   __fastcall TDisplayBannerAction (TDisplayBannerEvent AOnDisplayBanner) :
 
-     OnDisplayBanner(AOnDisplayBanner)
 
-   {
 
-   }
 
-   virtual void __fastcall Execute(void * Arg)
 
-   {
 
-     if (OnDisplayBanner != NULL)
 
-     {
 
-       OnDisplayBanner(Terminal, SessionName, Banner, NeverShowAgain, Options, Params);
 
-     }
 
-   }
 
-   TDisplayBannerEvent OnDisplayBanner;
 
-   TTerminal * Terminal;
 
-   UnicodeString SessionName;
 
-   UnicodeString Banner;
 
-   bool NeverShowAgain;
 
-   int Options;
 
-   unsigned int Params;
 
- };
 
- //---------------------------------------------------------------------------
 
- class TReadDirectoryAction : public TUserAction
 
- {
 
- public:
 
-   TReadDirectoryAction(TReadDirectoryEvent AOnReadDirectory) :
 
-     OnReadDirectory(AOnReadDirectory)
 
-   {
 
-   }
 
-   virtual void __fastcall Execute(void * Arg)
 
-   {
 
-     if (OnReadDirectory != NULL)
 
-     {
 
-       OnReadDirectory(Sender, ReloadOnly);
 
-     }
 
-   }
 
-   TReadDirectoryEvent OnReadDirectory;
 
-   TObject * Sender;
 
-   bool ReloadOnly;
 
- };
 
- //---------------------------------------------------------------------------
 
- class TReadDirectoryProgressAction : public TUserAction
 
- {
 
- public:
 
-   TReadDirectoryProgressAction(TReadDirectoryProgressEvent AOnReadDirectoryProgress) :
 
-     OnReadDirectoryProgress(AOnReadDirectoryProgress)
 
-   {
 
-   }
 
-   virtual void __fastcall Execute(void * Arg)
 
-   {
 
-     if (OnReadDirectoryProgress != NULL)
 
-     {
 
-       OnReadDirectoryProgress(Sender, Progress, ResolvedLinks, Cancel);
 
-     }
 
-   }
 
-   TReadDirectoryProgressEvent OnReadDirectoryProgress;
 
-   TObject * Sender;
 
-   int Progress;
 
-   int ResolvedLinks;
 
-   bool Cancel;
 
- };
 
- //---------------------------------------------------------------------------
 
- class TTerminalItem : public TSignalThread
 
- {
 
- friend class TQueueItem;
 
- friend class TBackgroundTerminal;
 
- public:
 
-   __fastcall TTerminalItem(TTerminalQueue * Queue, int Index);
 
-   virtual __fastcall ~TTerminalItem();
 
-   void __fastcall Process(TQueueItem * Item);
 
-   bool __fastcall ProcessUserAction(void * Arg);
 
-   void __fastcall Cancel();
 
-   void __fastcall Idle();
 
-   bool __fastcall Pause();
 
-   bool __fastcall Resume();
 
- protected:
 
-   TTerminalQueue * FQueue;
 
-   TBackgroundTerminal * FTerminal;
 
-   TQueueItem * FItem;
 
-   TCriticalSection * FCriticalSection;
 
-   TUserAction * FUserAction;
 
-   bool FCancel;
 
-   bool FPause;
 
-   virtual void __fastcall ProcessEvent();
 
-   virtual bool __fastcall Finished();
 
-   bool __fastcall WaitForUserAction(TQueueItem::TStatus ItemStatus, TUserAction * UserAction);
 
-   bool __fastcall OverrideItemStatus(TQueueItem::TStatus & ItemStatus);
 
-   void __fastcall TerminalQueryUser(TObject * Sender,
 
-     const UnicodeString Query, TStrings * MoreMessages, unsigned int Answers,
 
-     const TQueryParams * Params, unsigned int & Answer, TQueryType Type, void * Arg);
 
-   void __fastcall TerminalPromptUser(TTerminal * Terminal, TPromptKind Kind,
 
-     UnicodeString Name, UnicodeString Instructions,
 
-     TStrings * Prompts, TStrings * Results, bool & Result, void * Arg);
 
-   void __fastcall TerminalShowExtendedException(TTerminal * Terminal,
 
-     Exception * E, void * Arg);
 
-   void __fastcall OperationFinished(TFileOperation Operation, TOperationSide Side,
 
-     bool Temp, const UnicodeString & FileName, bool Success,
 
-     TOnceDoneOperation & OnceDoneOperation);
 
-   void __fastcall OperationProgress(TFileOperationProgressType & ProgressData);
 
- };
 
- //---------------------------------------------------------------------------
 
- // TSignalThread
 
- //---------------------------------------------------------------------------
 
- int __fastcall TSimpleThread::ThreadProc(void * Thread)
 
- {
 
-   TSimpleThread * SimpleThread = reinterpret_cast<TSimpleThread*>(Thread);
 
-   DebugAssert(SimpleThread != NULL);
 
-   try
 
-   {
 
-     SimpleThread->Execute();
 
-   }
 
-   catch(...)
 
-   {
 
-     // we do not expect thread to be terminated with exception
 
-     DebugFail();
 
-   }
 
-   SimpleThread->FFinished = true;
 
-   if (SimpleThread->Finished())
 
-   {
 
-     delete SimpleThread;
 
-   }
 
-   return 0;
 
- }
 
- //---------------------------------------------------------------------------
 
- __fastcall TSimpleThread::TSimpleThread() :
 
-   FThread(NULL), FFinished(true)
 
- {
 
-   FThread = reinterpret_cast<HANDLE>(
 
-     StartThread(NULL, 0, ThreadProc, this, CREATE_SUSPENDED, FThreadId));
 
- }
 
- //---------------------------------------------------------------------------
 
- __fastcall TSimpleThread::~TSimpleThread()
 
- {
 
-   // This is turn calls pure virtual Terminate, what does not work as intended, do not rely on it and remove the call eventually
 
-   Close();
 
-   if (FThread != NULL)
 
-   {
 
-     CloseHandle(FThread);
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- bool __fastcall TSimpleThread::IsFinished()
 
- {
 
-   return FFinished;
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TSimpleThread::Start()
 
- {
 
-   if (ResumeThread(FThread) == 1)
 
-   {
 
-     FFinished = false;
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- bool __fastcall TSimpleThread::Finished()
 
- {
 
-   return false;
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TSimpleThread::Close()
 
- {
 
-   if (!FFinished)
 
-   {
 
-     Terminate();
 
-     WaitFor();
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TSimpleThread::WaitFor(unsigned int Milliseconds)
 
- {
 
-   WaitForSingleObject(FThread, Milliseconds);
 
- }
 
- //---------------------------------------------------------------------------
 
- // TSignalThread
 
- //---------------------------------------------------------------------------
 
- __fastcall TSignalThread::TSignalThread(bool LowPriority, HANDLE Event) :
 
-   TSimpleThread(),
 
-   FTerminated(true), FEvent(NULL)
 
- {
 
-   if (Event == NULL)
 
-   {
 
-     FEvent = CreateEvent(NULL, false, false, NULL);
 
-   }
 
-   else
 
-   {
 
-     FEvent = Event;
 
-   }
 
-   DebugAssert(FEvent != NULL);
 
-   if (LowPriority)
 
-   {
 
-     ::SetThreadPriority(FThread, THREAD_PRIORITY_BELOW_NORMAL);
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- __fastcall TSignalThread::~TSignalThread()
 
- {
 
-   // cannot leave closing to TSimpleThread as we need to close it before
 
-   // destroying the event
 
-   Close();
 
-   if (FEvent)
 
-   {
 
-     CloseHandle(FEvent);
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TSignalThread::Start()
 
- {
 
-   FTerminated = false;
 
-   TSimpleThread::Start();
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TSignalThread::TriggerEvent()
 
- {
 
-   SetEvent(FEvent);
 
- }
 
- //---------------------------------------------------------------------------
 
- bool __fastcall TSignalThread::WaitForEvent()
 
- {
 
-   // should never return -1, so it is only about 0 or 1
 
-   return (WaitForEvent(INFINITE) > 0);
 
- }
 
- //---------------------------------------------------------------------------
 
- int __fastcall TSignalThread::WaitForEvent(unsigned int Timeout)
 
- {
 
-   unsigned int Result = WaitForSingleObject(FEvent, Timeout);
 
-   int Return;
 
-   if ((Result == WAIT_TIMEOUT) && !FTerminated)
 
-   {
 
-     Return = -1;
 
-   }
 
-   else
 
-   {
 
-     Return = ((Result == WAIT_OBJECT_0) && !FTerminated) ? 1 : 0;
 
-   }
 
-   return Return;
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TSignalThread::Execute()
 
- {
 
-   while (!FTerminated)
 
-   {
 
-     if (WaitForEvent())
 
-     {
 
-       ProcessEvent();
 
-     }
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TSignalThread::Terminate()
 
- {
 
-   FTerminated = true;
 
-   TriggerEvent();
 
- }
 
- //---------------------------------------------------------------------------
 
- // TTerminalQueue
 
- //---------------------------------------------------------------------------
 
- __fastcall TTerminalQueue::TTerminalQueue(TTerminal * Terminal,
 
-   TConfiguration * Configuration) :
 
-   TSignalThread(true),
 
-   FTerminal(Terminal), FTransfersLimit(2), FKeepDoneItemsFor(0), FEnabled(true),
 
-   FConfiguration(Configuration), FSessionData(NULL), FItems(NULL), FDoneItems(NULL),
 
-   FTerminals(NULL), FItemsSection(NULL), FFreeTerminals(0),
 
-   FItemsInProcess(0), FTemporaryTerminals(0), FOverallTerminals(0)
 
- {
 
-   FOnQueryUser = NULL;
 
-   FOnPromptUser = NULL;
 
-   FOnShowExtendedException = NULL;
 
-   FOnQueueItemUpdate = NULL;
 
-   FOnListUpdate = NULL;
 
-   FOnEvent = NULL;
 
-   FLastIdle = Now();
 
-   FIdleInterval = EncodeTimeVerbose(0, 0, 2, 0);
 
-   DebugAssert(Terminal != NULL);
 
-   FSessionData = new TSessionData(L"");
 
-   FSessionData->Assign(Terminal->SessionData);
 
-   FItems = new TList();
 
-   FDoneItems = new TList();
 
-   FTerminals = new TList();
 
-   FForcedItems = new TList();
 
-   FItemsSection = new TCriticalSection();
 
-   Start();
 
- }
 
- //---------------------------------------------------------------------------
 
- __fastcall TTerminalQueue::~TTerminalQueue()
 
- {
 
-   Close();
 
-   {
 
-     TGuard Guard(FItemsSection);
 
-     TTerminalItem * TerminalItem;
 
-     while (FTerminals->Count > 0)
 
-     {
 
-       TerminalItem = reinterpret_cast<TTerminalItem*>(FTerminals->Items[0]);
 
-       FTerminals->Delete(0);
 
-       TerminalItem->Terminate();
 
-       TerminalItem->WaitFor();
 
-       delete TerminalItem;
 
-     }
 
-     delete FTerminals;
 
-     delete FForcedItems;
 
-     FreeItemsList(FItems);
 
-     FreeItemsList(FDoneItems);
 
-   }
 
-   delete FItemsSection;
 
-   delete FSessionData;
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TTerminalQueue::FreeItemsList(TList * List)
 
- {
 
-   for (int Index = 0; Index < List->Count; Index++)
 
-   {
 
-     delete GetItem(List, Index);
 
-   }
 
-   delete List;
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TTerminalQueue::TerminalFinished(TTerminalItem * TerminalItem)
 
- {
 
-   if (!FTerminated)
 
-   {
 
-     {
 
-       TGuard Guard(FItemsSection);
 
-       int Index = FTerminals->IndexOf(TerminalItem);
 
-       DebugAssert(Index >= 0);
 
-       if (Index < FFreeTerminals)
 
-       {
 
-         FFreeTerminals--;
 
-       }
 
-       // Index may be >= FTransfersLimit also when the transfer limit was
 
-       // recently decreased, then
 
-       // FTemporaryTerminals < FTerminals->Count - FTransfersLimit
 
-       if ((FTransfersLimit >= 0) && (Index >= FTransfersLimit) && (FTemporaryTerminals > 0))
 
-       {
 
-         FTemporaryTerminals--;
 
-       }
 
-       FTerminals->Extract(TerminalItem);
 
-       delete TerminalItem;
 
-     }
 
-     TriggerEvent();
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- bool __fastcall TTerminalQueue::TerminalFree(TTerminalItem * TerminalItem)
 
- {
 
-   bool Result = true;
 
-   if (!FTerminated)
 
-   {
 
-     {
 
-       TGuard Guard(FItemsSection);
 
-       int Index = FTerminals->IndexOf(TerminalItem);
 
-       DebugAssert(Index >= 0);
 
-       DebugAssert(Index >= FFreeTerminals);
 
-       Result = (FTransfersLimit < 0) || (Index < FTransfersLimit);
 
-       if (Result)
 
-       {
 
-         FTerminals->Move(Index, 0);
 
-         FFreeTerminals++;
 
-       }
 
-     }
 
-     TriggerEvent();
 
-   }
 
-   return Result;
 
- }
 
- //---------------------------------------------------------------------------
 
- int __fastcall TTerminalQueue::GetParallelDurationThreshold()
 
- {
 
-   return FConfiguration->ParallelDurationThreshold;
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TTerminalQueue::AddItem(TQueueItem * Item)
 
- {
 
-   DebugAssert(!FTerminated);
 
-   Item->SetStatus(TQueueItem::qsPending);
 
-   {
 
-     TGuard Guard(FItemsSection);
 
-     FItems->Add(Item);
 
-     Item->FQueue = this;
 
-   }
 
-   DoListUpdate();
 
-   TriggerEvent();
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TTerminalQueue::RetryItem(TQueueItem * Item)
 
- {
 
-   if (!FTerminated)
 
-   {
 
-     {
 
-       TGuard Guard(FItemsSection);
 
-       int Index = FItems->Remove(Item);
 
-       DebugAssert(Index < FItemsInProcess);
 
-       DebugUsedParam(Index);
 
-       FItemsInProcess--;
 
-       FItems->Add(Item);
 
-     }
 
-     DoListUpdate();
 
-     TriggerEvent();
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TTerminalQueue::DeleteItem(TQueueItem * Item, bool CanKeep)
 
- {
 
-   if (!FTerminated)
 
-   {
 
-     bool Empty;
 
-     bool EmptyButMonitored;
 
-     bool Monitored;
 
-     {
 
-       TGuard Guard(FItemsSection);
 
-       // does this need to be within guard?
 
-       Monitored = (Item->CompleteEvent != INVALID_HANDLE_VALUE);
 
-       int Index = FItems->Remove(Item);
 
-       DebugAssert(Index < FItemsInProcess);
 
-       DebugUsedParam(Index);
 
-       FItemsInProcess--;
 
-       FForcedItems->Remove(Item);
 
-       // =0  do not keep
 
-       // <0  infinity
 
-       if ((FKeepDoneItemsFor != 0) && CanKeep && Item->Complete())
 
-       {
 
-         DebugAssert(Item->Status == TQueueItem::qsDone);
 
-         FDoneItems->Add(Item);
 
-       }
 
-       else
 
-       {
 
-         delete Item;
 
-       }
 
-       EmptyButMonitored = true;
 
-       Index = 0;
 
-       while (EmptyButMonitored && (Index < FItems->Count))
 
-       {
 
-         EmptyButMonitored = (GetItem(FItems, Index)->CompleteEvent != INVALID_HANDLE_VALUE);
 
-         Index++;
 
-       }
 
-       Empty = (FItems->Count == 0);
 
-     }
 
-     DoListUpdate();
 
-     // report empty but/except for monitored, if queue is empty or only monitored items are pending.
 
-     // do not report if current item was the last, but was monitored.
 
-     if (!Monitored && EmptyButMonitored)
 
-     {
 
-       DoEvent(qeEmptyButMonitored);
 
-     }
 
-     if (Empty)
 
-     {
 
-       DoEvent(qeEmpty);
 
-     }
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- TQueueItem * __fastcall TTerminalQueue::GetItem(TList * List, int Index)
 
- {
 
-   return reinterpret_cast<TQueueItem*>(List->Items[Index]);
 
- }
 
- //---------------------------------------------------------------------------
 
- TQueueItem * __fastcall TTerminalQueue::GetItem(int Index)
 
- {
 
-   return GetItem(FItems, Index);
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TTerminalQueue::UpdateStatusForList(
 
-   TTerminalQueueStatus * Status, TList * List, TTerminalQueueStatus * Current)
 
- {
 
-   TQueueItem * Item;
 
-   TQueueItemProxy * ItemProxy;
 
-   for (int Index = 0; Index < List->Count; Index++)
 
-   {
 
-     Item = GetItem(List, Index);
 
-     if (Current != NULL)
 
-     {
 
-       ItemProxy = Current->FindByQueueItem(Item);
 
-     }
 
-     else
 
-     {
 
-       ItemProxy = NULL;
 
-     }
 
-     if (ItemProxy != NULL)
 
-     {
 
-       Current->Delete(ItemProxy);
 
-       Status->Add(ItemProxy);
 
-       ItemProxy->Update();
 
-     }
 
-     else
 
-     {
 
-       Status->Add(new TQueueItemProxy(this, Item));
 
-     }
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- TTerminalQueueStatus * __fastcall TTerminalQueue::CreateStatus(TTerminalQueueStatus * Current)
 
- {
 
-   TTerminalQueueStatus * Status = new TTerminalQueueStatus();
 
-   try
 
-   {
 
-     try
 
-     {
 
-       TGuard Guard(FItemsSection);
 
-       UpdateStatusForList(Status, FDoneItems, Current);
 
-       Status->SetDoneCount(Status->Count);
 
-       UpdateStatusForList(Status, FItems, Current);
 
-     }
 
-     __finally
 
-     {
 
-       if (Current != NULL)
 
-       {
 
-         delete Current;
 
-       }
 
-     }
 
-   }
 
-   catch(...)
 
-   {
 
-     delete Status;
 
-     throw;
 
-   }
 
-   return Status;
 
- }
 
- //---------------------------------------------------------------------------
 
- bool __fastcall TTerminalQueue::ItemGetData(TQueueItem * Item, TQueueItemProxy * Proxy, TQueueFileList * FileList)
 
- {
 
-   // to prevent deadlocks when closing queue from other thread
 
-   bool Result = !FFinished;
 
-   if (Result)
 
-   {
 
-     TGuard Guard(FItemsSection);
 
-     Result = (FDoneItems->IndexOf(Item) >= 0) || (FItems->IndexOf(Item) >= 0);
 
-     if (Result)
 
-     {
 
-       if (FileList != NULL)
 
-       {
 
-         Result = Item->UpdateFileList(FileList);
 
-       }
 
-       else
 
-       {
 
-         Item->GetData(Proxy);
 
-       }
 
-     }
 
-   }
 
-   return Result;
 
- }
 
- //---------------------------------------------------------------------------
 
- bool __fastcall TTerminalQueue::ItemProcessUserAction(TQueueItem * Item, void * Arg)
 
- {
 
-   // to prevent deadlocks when closing queue from other thread
 
-   bool Result = !FFinished;
 
-   if (Result)
 
-   {
 
-     TTerminalItem * TerminalItem;
 
-     {
 
-       TGuard Guard(FItemsSection);
 
-       Result = (FItems->IndexOf(Item) >= 0) &&
 
-         TQueueItem::IsUserActionStatus(Item->Status);
 
-       if (Result)
 
-       {
 
-         TerminalItem = Item->FTerminalItem;
 
-       }
 
-     }
 
-     if (Result)
 
-     {
 
-       Result = TerminalItem->ProcessUserAction(Arg);
 
-     }
 
-   }
 
-   return Result;
 
- }
 
- //---------------------------------------------------------------------------
 
- bool __fastcall TTerminalQueue::ItemMove(TQueueItem * Item, TQueueItem * BeforeItem)
 
- {
 
-   // to prevent deadlocks when closing queue from other thread
 
-   bool Result = !FFinished;
 
-   if (Result)
 
-   {
 
-     {
 
-       TGuard Guard(FItemsSection);
 
-       int Index = FItems->IndexOf(Item);
 
-       int IndexDest = FItems->IndexOf(BeforeItem);
 
-       Result = (Index >= 0) && (IndexDest >= 0) &&
 
-         (Item->GetStatus() == TQueueItem::qsPending) &&
 
-         (BeforeItem->GetStatus() == TQueueItem::qsPending);
 
-       if (Result)
 
-       {
 
-         FItems->Move(Index, IndexDest);
 
-       }
 
-     }
 
-     if (Result)
 
-     {
 
-       DoListUpdate();
 
-       TriggerEvent();
 
-     }
 
-   }
 
-   return Result;
 
- }
 
- //---------------------------------------------------------------------------
 
- bool __fastcall TTerminalQueue::ItemExecuteNow(TQueueItem * Item)
 
- {
 
-   // to prevent deadlocks when closing queue from other thread
 
-   bool Result = !FFinished;
 
-   if (Result)
 
-   {
 
-     {
 
-       TGuard Guard(FItemsSection);
 
-       int Index = FItems->IndexOf(Item);
 
-       Result = (Index >= 0) && (Item->GetStatus() == TQueueItem::qsPending) &&
 
-         // prevent double-initiation when "execute" is clicked twice too fast
 
-         (Index >= FItemsInProcess);
 
-       if (Result)
 
-       {
 
-         if (Index > FItemsInProcess)
 
-         {
 
-           FItems->Move(Index, FItemsInProcess);
 
-         }
 
-         if ((FTransfersLimit >= 0) && (FTerminals->Count >= FTransfersLimit) &&
 
-             // when queue is disabled, we may have idle terminals,
 
-             // even when there are pending queue items
 
-             (FFreeTerminals == 0))
 
-         {
 
-           FTemporaryTerminals++;
 
-         }
 
-         FForcedItems->Add(Item);
 
-       }
 
-     }
 
-     if (Result)
 
-     {
 
-       DoListUpdate();
 
-       TriggerEvent();
 
-     }
 
-   }
 
-   return Result;
 
- }
 
- //---------------------------------------------------------------------------
 
- bool __fastcall TTerminalQueue::ItemDelete(TQueueItem * Item)
 
- {
 
-   // to prevent deadlocks when closing queue from other thread
 
-   bool Result = !FFinished;
 
-   if (Result)
 
-   {
 
-     bool UpdateList = false;
 
-     {
 
-       TGuard Guard(FItemsSection);
 
-       int Index = FItems->IndexOf(Item);
 
-       Result = (Index >= 0);
 
-       if (Result)
 
-       {
 
-         if (Item->Status == TQueueItem::qsPending)
 
-         {
 
-           FItems->Delete(Index);
 
-           FForcedItems->Remove(Item);
 
-           delete Item;
 
-           UpdateList = true;
 
-         }
 
-         else
 
-         {
 
-           Item->FTerminalItem->Cancel();
 
-         }
 
-       }
 
-       else
 
-       {
 
-         Index = FDoneItems->IndexOf(Item);
 
-         Result = (Index >= 0);
 
-         if (Result)
 
-         {
 
-           FDoneItems->Delete(Index);
 
-           delete Item;
 
-           UpdateList = true;
 
-         }
 
-       }
 
-     }
 
-     if (UpdateList)
 
-     {
 
-       DoListUpdate();
 
-       TriggerEvent();
 
-     }
 
-   }
 
-   return Result;
 
- }
 
- //---------------------------------------------------------------------------
 
- bool __fastcall TTerminalQueue::ItemPause(TQueueItem * Item, bool Pause)
 
- {
 
-   // to prevent deadlocks when closing queue from other thread
 
-   bool Result = !FFinished;
 
-   if (Result)
 
-   {
 
-     TTerminalItem * TerminalItem;
 
-     {
 
-       TGuard Guard(FItemsSection);
 
-       Result = (FItems->IndexOf(Item) >= 0) &&
 
-         ((Pause && (Item->Status == TQueueItem::qsProcessing)) ||
 
-          (!Pause && (Item->Status == TQueueItem::qsPaused)));
 
-       if (Result)
 
-       {
 
-         TerminalItem = Item->FTerminalItem;
 
-       }
 
-     }
 
-     if (Result)
 
-     {
 
-       if (Pause)
 
-       {
 
-         Result = TerminalItem->Pause();
 
-       }
 
-       else
 
-       {
 
-         Result = TerminalItem->Resume();
 
-       }
 
-     }
 
-   }
 
-   return Result;
 
- }
 
- //---------------------------------------------------------------------------
 
- bool __fastcall TTerminalQueue::ItemSetCPSLimit(TQueueItem * Item, unsigned long CPSLimit)
 
- {
 
-   // to prevent deadlocks when closing queue from other thread
 
-   bool Result = !FFinished;
 
-   if (Result)
 
-   {
 
-     TGuard Guard(FItemsSection);
 
-     Result = (FItems->IndexOf(Item) >= 0);
 
-     if (Result)
 
-     {
 
-       Item->SetCPSLimit(CPSLimit);
 
-     }
 
-   }
 
-   return Result;
 
- }
 
- //---------------------------------------------------------------------------
 
- bool __fastcall TTerminalQueue::ItemGetCPSLimit(TQueueItem * Item, unsigned long & CPSLimit)
 
- {
 
-   CPSLimit = 0;
 
-   // to prevent deadlocks when closing queue from other thread
 
-   bool Result = !FFinished;
 
-   if (Result)
 
-   {
 
-     TGuard Guard(FItemsSection);
 
-     Result = (FItems->IndexOf(Item) >= 0);
 
-     if (Result)
 
-     {
 
-       CPSLimit = Item->GetCPSLimit();
 
-     }
 
-   }
 
-   return Result;
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TTerminalQueue::Idle()
 
- {
 
-   TDateTime N = Now();
 
-   if (N - FLastIdle > FIdleInterval)
 
-   {
 
-     FLastIdle = N;
 
-     TTerminalItem * TerminalItem = NULL;
 
-     if (FFreeTerminals > 0)
 
-     {
 
-       TGuard Guard(FItemsSection);
 
-       if (FFreeTerminals > 0)
 
-       {
 
-         // take the last free terminal, because TerminalFree() puts it to the
 
-         // front, this ensures we cycle thru all free terminals
 
-         TerminalItem = reinterpret_cast<TTerminalItem*>(FTerminals->Items[FFreeTerminals - 1]);
 
-         FTerminals->Move(FFreeTerminals - 1, FTerminals->Count - 1);
 
-         FFreeTerminals--;
 
-       }
 
-     }
 
-     if (TerminalItem != NULL)
 
-     {
 
-       TerminalItem->Idle();
 
-     }
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- bool __fastcall TTerminalQueue::WaitForEvent()
 
- {
 
-   // terminate loop regularly, so that we can check for expired done items
 
-   bool Result = (TSignalThread::WaitForEvent(1000) != 0);
 
-   return Result;
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TTerminalQueue::ProcessEvent()
 
- {
 
-   TTerminalItem * TerminalItem;
 
-   TQueueItem * Item;
 
-   do
 
-   {
 
-     TerminalItem = NULL;
 
-     Item = NULL;
 
-     {
 
-       TGuard Guard(FItemsSection);
 
-       // =0  do not keep
 
-       // <0  infinity
 
-       if (FKeepDoneItemsFor >= 0)
 
-       {
 
-         TDateTime RemoveDoneItemsBefore = Now();
 
-         if (FKeepDoneItemsFor > 0)
 
-         {
 
-           RemoveDoneItemsBefore = IncSecond(RemoveDoneItemsBefore, -FKeepDoneItemsFor);
 
-         }
 
-         for (int Index = 0; Index < FDoneItems->Count; Index++)
 
-         {
 
-           TQueueItem * Item = GetItem(FDoneItems, Index);
 
-           if (Item->FDoneAt <= RemoveDoneItemsBefore)
 
-           {
 
-             FDoneItems->Delete(Index);
 
-             delete Item;
 
-             Index--;
 
-             DoListUpdate();
 
-           }
 
-         }
 
-       }
 
-       if (FItems->Count > FItemsInProcess)
 
-       {
 
-         Item = GetItem(FItemsInProcess);
 
-         int ForcedIndex = FForcedItems->IndexOf(Item);
 
-         if (FEnabled || (ForcedIndex >= 0))
 
-         {
 
-           if ((FFreeTerminals == 0) &&
 
-               ((FTransfersLimit < 0) ||
 
-                (FTerminals->Count < FTransfersLimit + FTemporaryTerminals)))
 
-           {
 
-             FOverallTerminals++;
 
-             TerminalItem = new TTerminalItem(this, FOverallTerminals);
 
-             FTerminals->Add(TerminalItem);
 
-           }
 
-           else if (FFreeTerminals > 0)
 
-           {
 
-             TerminalItem = reinterpret_cast<TTerminalItem*>(FTerminals->Items[0]);
 
-             FTerminals->Move(0, FTerminals->Count - 1);
 
-             FFreeTerminals--;
 
-           }
 
-           if (TerminalItem != NULL)
 
-           {
 
-             if (ForcedIndex >= 0)
 
-             {
 
-               FForcedItems->Delete(ForcedIndex);
 
-             }
 
-             FItemsInProcess++;
 
-           }
 
-         }
 
-       }
 
-     }
 
-     if (TerminalItem != NULL)
 
-     {
 
-       TerminalItem->Process(Item);
 
-     }
 
-   }
 
-   while (!FTerminated && (TerminalItem != NULL));
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TTerminalQueue::DoQueueItemUpdate(TQueueItem * Item)
 
- {
 
-   if (OnQueueItemUpdate != NULL)
 
-   {
 
-     OnQueueItemUpdate(this, Item);
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TTerminalQueue::DoListUpdate()
 
- {
 
-   if (OnListUpdate != NULL)
 
-   {
 
-     OnListUpdate(this);
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TTerminalQueue::DoEvent(TQueueEvent Event)
 
- {
 
-   if (OnEvent != NULL)
 
-   {
 
-     OnEvent(this, Event);
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TTerminalQueue::SetTransfersLimit(int value)
 
- {
 
-   if (FTransfersLimit != value)
 
-   {
 
-     {
 
-       TGuard Guard(FItemsSection);
 
-       if ((value >= 0) && (value < FItemsInProcess))
 
-       {
 
-         FTemporaryTerminals = (FItemsInProcess - value);
 
-       }
 
-       else
 
-       {
 
-         FTemporaryTerminals = 0;
 
-       }
 
-       FTransfersLimit = value;
 
-     }
 
-     TriggerEvent();
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TTerminalQueue::SetKeepDoneItemsFor(int value)
 
- {
 
-   if (FKeepDoneItemsFor != value)
 
-   {
 
-     {
 
-       TGuard Guard(FItemsSection);
 
-       FKeepDoneItemsFor = value;
 
-     }
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TTerminalQueue::SetEnabled(bool value)
 
- {
 
-   if (FEnabled != value)
 
-   {
 
-     {
 
-       TGuard Guard(FItemsSection);
 
-       FEnabled = value;
 
-     }
 
-     TriggerEvent();
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- bool __fastcall TTerminalQueue::GetIsEmpty()
 
- {
 
-   TGuard Guard(FItemsSection);
 
-   return (FItems->Count == 0);
 
- }
 
- //---------------------------------------------------------------------------
 
- bool __fastcall TTerminalQueue::TryAddParallelOperation(TQueueItem * Item, bool Force)
 
- {
 
-   TGuard Guard(FItemsSection);
 
-   bool Result =
 
-     (FFreeTerminals > 0) ||
 
-     (Force && (FItemsInProcess < FTransfersLimit));
 
-   if (Result)
 
-   {
 
-     TQueueItem * ParallelItem = DebugNotNull(Item->CreateParallelOperation());
 
-     if (!FEnabled)
 
-     {
 
-       FForcedItems->Add(ParallelItem);
 
-     }
 
-     AddItem(ParallelItem);
 
-   }
 
-   return Result;
 
- }
 
- //---------------------------------------------------------------------------
 
- bool __fastcall TTerminalQueue::ContinueParallelOperation()
 
- {
 
-   TGuard Guard(FItemsSection);
 
-   return
 
-     (FItems->Count <= FItemsInProcess) ||
 
-     // When queue auto processing is not enabled, keep using all connections for the transfers that were manually started
 
-     !FEnabled;
 
- }
 
- //---------------------------------------------------------------------------
 
- // TBackgroundItem
 
- //---------------------------------------------------------------------------
 
- class TBackgroundTerminal : public TSecondaryTerminal
 
- {
 
- friend class TTerminalItem;
 
- public:
 
-   __fastcall TBackgroundTerminal(TTerminal * MainTerminal,
 
-     TSessionData * SessionData, TConfiguration * Configuration,
 
-     TTerminalItem * Item, const UnicodeString & Name);
 
- protected:
 
-   virtual bool __fastcall DoQueryReopen(Exception * E);
 
- private:
 
-   TTerminalItem * FItem;
 
- };
 
- //---------------------------------------------------------------------------
 
- __fastcall TBackgroundTerminal::TBackgroundTerminal(TTerminal * MainTerminal,
 
-     TSessionData * SessionData, TConfiguration * Configuration, TTerminalItem * Item,
 
-     const UnicodeString & Name) :
 
-   TSecondaryTerminal(MainTerminal, SessionData, Configuration, Name, NULL), FItem(Item)
 
- {
 
-   ActionLog->Enabled = false;
 
- }
 
- //---------------------------------------------------------------------------
 
- bool __fastcall TBackgroundTerminal::DoQueryReopen(Exception * /*E*/)
 
- {
 
-   bool Result;
 
-   if (FItem->FTerminated || FItem->FCancel)
 
-   {
 
-     // avoid reconnection if we are closing
 
-     Result = false;
 
-   }
 
-   else
 
-   {
 
-     Sleep(Configuration->SessionReopenBackground);
 
-     Result = true;
 
-   }
 
-   return Result;
 
- }
 
- //---------------------------------------------------------------------------
 
- // TTerminalItem
 
- //---------------------------------------------------------------------------
 
- __fastcall TTerminalItem::TTerminalItem(TTerminalQueue * Queue, int Index) :
 
-   TSignalThread(true), FQueue(Queue), FTerminal(NULL), FItem(NULL),
 
-   FCriticalSection(NULL), FUserAction(NULL)
 
- {
 
-   FCriticalSection = new TCriticalSection();
 
-   FTerminal = new TBackgroundTerminal(FQueue->FTerminal, Queue->FSessionData,
 
-     FQueue->FConfiguration, this, FORMAT(L"Background %d", (Index)));
 
-   try
 
-   {
 
-     FTerminal->UseBusyCursor = false;
 
-     FTerminal->OnQueryUser = TerminalQueryUser;
 
-     FTerminal->OnPromptUser = TerminalPromptUser;
 
-     FTerminal->OnShowExtendedException = TerminalShowExtendedException;
 
-     FTerminal->OnProgress = OperationProgress;
 
-     FTerminal->OnFinished = OperationFinished;
 
-   }
 
-   catch(...)
 
-   {
 
-     delete FTerminal;
 
-     throw;
 
-   }
 
-   Start();
 
- }
 
- //---------------------------------------------------------------------------
 
- __fastcall TTerminalItem::~TTerminalItem()
 
- {
 
-   Close();
 
-   DebugAssert(FItem == NULL);
 
-   delete FTerminal;
 
-   delete FCriticalSection;
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TTerminalItem::Process(TQueueItem * Item)
 
- {
 
-   {
 
-     TGuard Guard(FCriticalSection);
 
-     DebugAssert(FItem == NULL);
 
-     FItem = Item;
 
-   }
 
-   TriggerEvent();
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TTerminalItem::ProcessEvent()
 
- {
 
-   TGuard Guard(FCriticalSection);
 
-   bool Retry = true;
 
-   FCancel = false;
 
-   FPause = false;
 
-   FItem->FTerminalItem = this;
 
-   try
 
-   {
 
-     DebugAssert(FItem != NULL);
 
-     if (!FTerminal->Active)
 
-     {
 
-       FItem->SetStatus(TQueueItem::qsConnecting);
 
-       FTerminal->SessionData->RemoteDirectory = FItem->StartupDirectory();
 
-       FTerminal->Open();
 
-     }
 
-     Retry = false;
 
-     if (!FCancel)
 
-     {
 
-       FTerminal->UpdateFromMain();
 
-       FItem->SetStatus(TQueueItem::qsProcessing);
 
-       FItem->Execute(this);
 
-     }
 
-   }
 
-   catch(Exception & E)
 
-   {
 
-     UnicodeString Message;
 
-     if (ExceptionMessageFormatted(&E, Message))
 
-     {
 
-       // do not show error messages, if task was cancelled anyway
 
-       // (for example if transfer is cancelled during reconnection attempts)
 
-       if (!FCancel &&
 
-           (FTerminal->QueryUserException(L"", &E, qaOK | qaCancel, NULL, qtError) == qaCancel))
 
-       {
 
-         FCancel = true;
 
-       }
 
-     }
 
-   }
 
-   FItem->SetStatus(TQueueItem::qsDone);
 
-   FItem->FTerminalItem = NULL;
 
-   TQueueItem * Item = FItem;
 
-   FItem = NULL;
 
-   if (Retry && !FCancel)
 
-   {
 
-     FQueue->RetryItem(Item);
 
-   }
 
-   else
 
-   {
 
-     FQueue->DeleteItem(Item, !FCancel);
 
-   }
 
-   if (!FTerminal->Active ||
 
-       !FQueue->TerminalFree(this))
 
-   {
 
-     Terminate();
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TTerminalItem::Idle()
 
- {
 
-   TGuard Guard(FCriticalSection);
 
-   DebugAssert(FTerminal->Active);
 
-   try
 
-   {
 
-     FTerminal->Idle();
 
-   }
 
-   catch(...)
 
-   {
 
-   }
 
-   if (!FTerminal->Active ||
 
-       !FQueue->TerminalFree(this))
 
-   {
 
-     Terminate();
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TTerminalItem::Cancel()
 
- {
 
-   FCancel = true;
 
-   if ((FItem->GetStatus() == TQueueItem::qsPaused) ||
 
-       TQueueItem::IsUserActionStatus(FItem->GetStatus()))
 
-   {
 
-     TriggerEvent();
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- bool __fastcall TTerminalItem::Pause()
 
- {
 
-   DebugAssert(FItem != NULL);
 
-   bool Result = (FItem->GetStatus() == TQueueItem::qsProcessing) && !FPause;
 
-   if (Result)
 
-   {
 
-     FPause = true;
 
-   }
 
-   return Result;
 
- }
 
- //---------------------------------------------------------------------------
 
- bool __fastcall TTerminalItem::Resume()
 
- {
 
-   DebugAssert(FItem != NULL);
 
-   bool Result = (FItem->GetStatus() == TQueueItem::qsPaused);
 
-   if (Result)
 
-   {
 
-     TriggerEvent();
 
-   }
 
-   return Result;
 
- }
 
- //---------------------------------------------------------------------------
 
- bool __fastcall TTerminalItem::ProcessUserAction(void * Arg)
 
- {
 
-   // When status is changed twice quickly, the controller when responding
 
-   // to the first change (non-user-action) can be so slow to check only after
 
-   // the second (user-action) change occurs. Thus it responds it.
 
-   // Then as reaction to the second (user-action) change there will not be
 
-   // any outstanding user-action.
 
-   bool Result = (FUserAction != NULL);
 
-   if (Result)
 
-   {
 
-     DebugAssert(FItem != NULL);
 
-     FUserAction->Execute(Arg);
 
-     FUserAction = NULL;
 
-     TriggerEvent();
 
-   }
 
-   return Result;
 
- }
 
- //---------------------------------------------------------------------------
 
- bool __fastcall TTerminalItem::WaitForUserAction(
 
-   TQueueItem::TStatus ItemStatus, TUserAction * UserAction)
 
- {
 
-   DebugAssert(FItem != NULL);
 
-   DebugAssert((FItem->GetStatus() == TQueueItem::qsProcessing) ||
 
-     (FItem->GetStatus() == TQueueItem::qsConnecting));
 
-   bool Result;
 
-   TQueueItem::TStatus PrevStatus = FItem->GetStatus();
 
-   try
 
-   {
 
-     FUserAction = UserAction;
 
-     FItem->SetStatus(ItemStatus);
 
-     FQueue->DoEvent(qePendingUserAction);
 
-     Result = !FTerminated && WaitForEvent() && !FCancel;
 
-   }
 
-   __finally
 
-   {
 
-     FUserAction = NULL;
 
-     FItem->SetStatus(PrevStatus);
 
-   }
 
-   return Result;
 
- }
 
- //---------------------------------------------------------------------------
 
- bool __fastcall TTerminalItem::Finished()
 
- {
 
-   bool Result = TSignalThread::Finished();
 
-   FQueue->TerminalFinished(this);
 
-   return Result;
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TTerminalItem::TerminalQueryUser(TObject * Sender,
 
-   const UnicodeString Query, TStrings * MoreMessages, unsigned int Answers,
 
-   const TQueryParams * Params, unsigned int & Answer, TQueryType Type, void * Arg)
 
- {
 
-   // so far query without queue item can occur only for key confirmation
 
-   // on re-key with non-cached host key. make it fail.
 
-   if (FItem != NULL)
 
-   {
 
-     DebugUsedParam(Arg);
 
-     DebugAssert(Arg == NULL);
 
-     TQueryUserAction Action(FQueue->OnQueryUser);
 
-     Action.Sender = Sender;
 
-     Action.Query = Query;
 
-     Action.MoreMessages = MoreMessages;
 
-     Action.Answers = Answers;
 
-     Action.Params = Params;
 
-     Action.Answer = Answer;
 
-     Action.Type = Type;
 
-     // if the query is "error", present it as an "error" state in UI,
 
-     // however it is still handled as query by the action.
 
-     TQueueItem::TStatus ItemStatus =
 
-       (Action.Type == qtError ? TQueueItem::qsError : TQueueItem::qsQuery);
 
-     if (WaitForUserAction(ItemStatus, &Action))
 
-     {
 
-       Answer = Action.Answer;
 
-     }
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TTerminalItem::TerminalPromptUser(TTerminal * Terminal,
 
-   TPromptKind Kind, UnicodeString Name, UnicodeString Instructions, TStrings * Prompts,
 
-   TStrings * Results, bool & Result, void * Arg)
 
- {
 
-   if (FItem == NULL)
 
-   {
 
-     // sanity, should not occur
 
-     DebugFail();
 
-     Result = false;
 
-   }
 
-   else
 
-   {
 
-     DebugUsedParam(Arg);
 
-     DebugAssert(Arg == NULL);
 
-     TPromptUserAction Action(FQueue->OnPromptUser);
 
-     Action.Terminal = Terminal;
 
-     Action.Kind = Kind;
 
-     Action.Name = Name;
 
-     Action.Instructions = Instructions;
 
-     Action.Prompts = Prompts;
 
-     Action.Results->AddStrings(Results);
 
-     if (WaitForUserAction(TQueueItem::qsPrompt, &Action))
 
-     {
 
-       Results->Clear();
 
-       Results->AddStrings(Action.Results);
 
-       Result = Action.Result;
 
-     }
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TTerminalItem::TerminalShowExtendedException(
 
-   TTerminal * Terminal, Exception * E, void * Arg)
 
- {
 
-   DebugUsedParam(Arg);
 
-   DebugAssert(Arg == NULL);
 
-   if ((FItem != NULL) &&
 
-       ShouldDisplayException(E))
 
-   {
 
-     TShowExtendedExceptionAction Action(FQueue->OnShowExtendedException);
 
-     Action.Terminal = Terminal;
 
-     Action.E = E;
 
-     WaitForUserAction(TQueueItem::qsError, &Action);
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TTerminalItem::OperationFinished(TFileOperation /*Operation*/,
 
-   TOperationSide /*Side*/, bool /*Temp*/, const UnicodeString & /*FileName*/,
 
-   bool /*Success*/, TOnceDoneOperation & /*OnceDoneOperation*/)
 
- {
 
-   // nothing
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TTerminalItem::OperationProgress(
 
-   TFileOperationProgressType & ProgressData)
 
- {
 
-   if (FPause && !FTerminated && !FCancel)
 
-   {
 
-     DebugAssert(FItem != NULL);
 
-     TQueueItem::TStatus PrevStatus = FItem->GetStatus();
 
-     DebugAssert(PrevStatus == TQueueItem::qsProcessing);
 
-     // must be set before TFileOperationProgressType::Suspend(), because
 
-     // it invokes this method back
 
-     FPause = false;
 
-     ProgressData.Suspend();
 
-     try
 
-     {
 
-       FItem->SetStatus(TQueueItem::qsPaused);
 
-       WaitForEvent();
 
-     }
 
-     __finally
 
-     {
 
-       FItem->SetStatus(PrevStatus);
 
-       ProgressData.Resume();
 
-     }
 
-   }
 
-   if (FTerminated || FCancel)
 
-   {
 
-     if (ProgressData.TransferringFile)
 
-     {
 
-       ProgressData.SetCancel(csCancelTransfer);
 
-     }
 
-     else
 
-     {
 
-       ProgressData.SetCancel(csCancel);
 
-     }
 
-   }
 
-   DebugAssert(FItem != NULL);
 
-   FItem->SetProgress(ProgressData);
 
- }
 
- //---------------------------------------------------------------------------
 
- bool __fastcall TTerminalItem::OverrideItemStatus(TQueueItem::TStatus & ItemStatus)
 
- {
 
-   DebugAssert(FTerminal != NULL);
 
-   bool Result = (FTerminal->Status < ssOpened) && (ItemStatus == TQueueItem::qsProcessing);
 
-   if (Result)
 
-   {
 
-     ItemStatus = TQueueItem::qsConnecting;
 
-   }
 
-   return Result;
 
- }
 
- //---------------------------------------------------------------------------
 
- // TQueueItem
 
- //---------------------------------------------------------------------------
 
- __fastcall TQueueItem::TQueueItem() :
 
-   FStatus(qsPending), FTerminalItem(NULL), FSection(NULL), FProgressData(NULL),
 
-   FQueue(NULL), FInfo(NULL), FCompleteEvent(INVALID_HANDLE_VALUE),
 
-   FCPSLimit(-1)
 
- {
 
-   FSection = new TCriticalSection();
 
-   FInfo = new TInfo();
 
-   FInfo->SingleFile = false;
 
-   FInfo->Primary = true;
 
-   FInfo->GroupToken = this;
 
- }
 
- //---------------------------------------------------------------------------
 
- __fastcall TQueueItem::~TQueueItem()
 
- {
 
-   // we need to keep the total transfer size even after transfer completes
 
-   delete FProgressData;
 
-   Complete();
 
-   delete FSection;
 
-   delete FInfo;
 
- }
 
- //---------------------------------------------------------------------------
 
- bool __fastcall TQueueItem::Complete()
 
- {
 
-   TGuard Guard(FSection);
 
-   if (FCompleteEvent != INVALID_HANDLE_VALUE)
 
-   {
 
-     SetEvent(FCompleteEvent);
 
-     FCompleteEvent = INVALID_HANDLE_VALUE;
 
-   }
 
-   return FInfo->Primary;
 
- }
 
- //---------------------------------------------------------------------------
 
- bool __fastcall TQueueItem::IsUserActionStatus(TStatus Status)
 
- {
 
-   return (Status == qsQuery) || (Status == qsError) || (Status == qsPrompt);
 
- }
 
- //---------------------------------------------------------------------------
 
- TQueueItem::TStatus __fastcall TQueueItem::GetStatus()
 
- {
 
-   TGuard Guard(FSection);
 
-   return FStatus;
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TQueueItem::SetStatus(TStatus Status)
 
- {
 
-   {
 
-     TGuard Guard(FSection);
 
-     FStatus = Status;
 
-     if (FStatus == qsDone)
 
-     {
 
-       FDoneAt = Now();
 
-     }
 
-   }
 
-   DebugAssert((FQueue != NULL) || (Status == qsPending));
 
-   if (FQueue != NULL)
 
-   {
 
-     FQueue->DoQueueItemUpdate(this);
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TQueueItem::ProgressUpdated()
 
- {
 
-   // noop
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TQueueItem::SetProgress(
 
-   TFileOperationProgressType & ProgressData)
 
- {
 
-   {
 
-     TGuard Guard(FSection);
 
-     // do not lose CPS limit override on "calculate size" operation,
 
-     // wait until the real transfer operation starts
 
-     if ((FCPSLimit >= 0) && ProgressData.IsTransfer())
 
-     {
 
-       ProgressData.SetCPSLimit(static_cast<unsigned long>(FCPSLimit));
 
-       FCPSLimit = -1;
 
-     }
 
-     DebugAssert(FProgressData != NULL);
 
-     FProgressData->Assign(ProgressData);
 
-     FProgressData->Reset();
 
-   }
 
-   ProgressUpdated();
 
-   FQueue->DoQueueItemUpdate(this);
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TQueueItem::GetData(TQueueItemProxy * Proxy)
 
- {
 
-   TGuard Guard(FSection);
 
-   DebugAssert(Proxy->FProgressData != NULL);
 
-   if (FProgressData != NULL)
 
-   {
 
-     Proxy->FProgressData->Assign(*FProgressData);
 
-   }
 
-   else
 
-   {
 
-     Proxy->FProgressData->Clear();
 
-   }
 
-   *Proxy->FInfo = *FInfo;
 
-   Proxy->FStatus = FStatus;
 
-   if (FTerminalItem != NULL)
 
-   {
 
-     FTerminalItem->OverrideItemStatus(Proxy->FStatus);
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- bool __fastcall TQueueItem::UpdateFileList(TQueueFileList *)
 
- {
 
-   // noop - implemented in TTransferQueueItem
 
-   return false;
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TQueueItem::Execute(TTerminalItem * TerminalItem)
 
- {
 
-   {
 
-     DebugAssert(FProgressData == NULL);
 
-     TGuard Guard(FSection);
 
-     FProgressData = new TFileOperationProgressType();
 
-   }
 
-   DoExecute(TerminalItem->FTerminal);
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TQueueItem::SetCPSLimit(unsigned long CPSLimit)
 
- {
 
-   FCPSLimit = static_cast<long>(CPSLimit);
 
- }
 
- //---------------------------------------------------------------------------
 
- unsigned long __fastcall TQueueItem::DefaultCPSLimit()
 
- {
 
-   return 0;
 
- }
 
- //---------------------------------------------------------------------------
 
- unsigned long __fastcall TQueueItem::GetCPSLimit()
 
- {
 
-   unsigned long Result;
 
-   if (FCPSLimit >= 0)
 
-   {
 
-     Result = FCPSLimit;
 
-   }
 
-   else if (FProgressData != NULL)
 
-   {
 
-     Result = FProgressData->CPSLimit;
 
-   }
 
-   else
 
-   {
 
-     Result = DefaultCPSLimit();
 
-   }
 
-   return Result;
 
- }
 
- //---------------------------------------------------------------------------
 
- TQueueItem * __fastcall TQueueItem::CreateParallelOperation()
 
- {
 
-   return NULL;
 
- }
 
- //---------------------------------------------------------------------------
 
- // TQueueItemProxy
 
- //---------------------------------------------------------------------------
 
- __fastcall TQueueItemProxy::TQueueItemProxy(TTerminalQueue * Queue,
 
-   TQueueItem * QueueItem) :
 
-   FQueue(Queue), FQueueItem(QueueItem), FProgressData(NULL),
 
-   FQueueStatus(NULL), FInfo(NULL),
 
-   FProcessingUserAction(false), FUserData(NULL)
 
- {
 
-   FProgressData = new TFileOperationProgressType();
 
-   FInfo = new TQueueItem::TInfo();
 
-   Update();
 
- }
 
- //---------------------------------------------------------------------------
 
- __fastcall TQueueItemProxy::~TQueueItemProxy()
 
- {
 
-   delete FProgressData;
 
-   delete FInfo;
 
- }
 
- //---------------------------------------------------------------------------
 
- TFileOperationProgressType * __fastcall TQueueItemProxy::GetProgressData()
 
- {
 
-   return (FProgressData->Operation == foNone) ? NULL : FProgressData;
 
- }
 
- //---------------------------------------------------------------------------
 
- __int64 __fastcall TQueueItemProxy::GetTotalTransferred()
 
- {
 
-   // want to show total transferred also for "completed" items,
 
-   // for which GetProgressData() is NULL
 
-   return
 
-     (FProgressData->Operation == Info->Operation) || (Status == TQueueItem::qsDone) ?
 
-       FProgressData->TotalTransferred : -1;
 
- }
 
- //---------------------------------------------------------------------------
 
- bool __fastcall TQueueItemProxy::Update()
 
- {
 
-   DebugAssert(FQueueItem != NULL);
 
-   TQueueItem::TStatus PrevStatus = Status;
 
-   bool Result = FQueue->ItemGetData(FQueueItem, this, NULL);
 
-   if ((FQueueStatus != NULL) && (PrevStatus != Status))
 
-   {
 
-     FQueueStatus->ResetStats();
 
-   }
 
-   return Result;
 
- }
 
- //---------------------------------------------------------------------------
 
- bool __fastcall TQueueItemProxy::UpdateFileList(TQueueFileList * FileList)
 
- {
 
-   return FQueue->ItemGetData(FQueueItem, NULL, FileList);
 
- }
 
- //---------------------------------------------------------------------------
 
- bool __fastcall TQueueItemProxy::ExecuteNow()
 
- {
 
-   return FQueue->ItemExecuteNow(FQueueItem);
 
- }
 
- //---------------------------------------------------------------------------
 
- bool __fastcall TQueueItemProxy::Move(bool Sooner)
 
- {
 
-   bool Result = false;
 
-   int I = Index;
 
-   if (Sooner)
 
-   {
 
-     if (I > 0)
 
-     {
 
-       Result = Move(FQueueStatus->Items[I - 1]);
 
-     }
 
-   }
 
-   else
 
-   {
 
-     if (I < FQueueStatus->Count - 1)
 
-     {
 
-       Result = FQueueStatus->Items[I + 1]->Move(this);
 
-     }
 
-   }
 
-   return Result;
 
- }
 
- //---------------------------------------------------------------------------
 
- bool __fastcall TQueueItemProxy::Move(TQueueItemProxy * BeforeItem)
 
- {
 
-   return FQueue->ItemMove(FQueueItem, BeforeItem->FQueueItem);
 
- }
 
- //---------------------------------------------------------------------------
 
- bool __fastcall TQueueItemProxy::Delete()
 
- {
 
-   return FQueue->ItemDelete(FQueueItem);
 
- }
 
- //---------------------------------------------------------------------------
 
- bool __fastcall TQueueItemProxy::Pause()
 
- {
 
-   return FQueue->ItemPause(FQueueItem, true);
 
- }
 
- //---------------------------------------------------------------------------
 
- bool __fastcall TQueueItemProxy::Resume()
 
- {
 
-   return FQueue->ItemPause(FQueueItem, false);
 
- }
 
- //---------------------------------------------------------------------------
 
- bool __fastcall TQueueItemProxy::ProcessUserAction()
 
- {
 
-   DebugAssert(FQueueItem != NULL);
 
-   bool Result;
 
-   FProcessingUserAction = true;
 
-   try
 
-   {
 
-     Result = FQueue->ItemProcessUserAction(FQueueItem, NULL);
 
-   }
 
-   __finally
 
-   {
 
-     FProcessingUserAction = false;
 
-   }
 
-   return Result;
 
- }
 
- //---------------------------------------------------------------------------
 
- bool __fastcall TQueueItemProxy::GetCPSLimit(unsigned long & CPSLimit)
 
- {
 
-   return FQueue->ItemGetCPSLimit(FQueueItem, CPSLimit);
 
- }
 
- //---------------------------------------------------------------------------
 
- bool __fastcall TQueueItemProxy::SetCPSLimit(unsigned long CPSLimit)
 
- {
 
-   return FQueue->ItemSetCPSLimit(FQueueItem, CPSLimit);
 
- }
 
- //---------------------------------------------------------------------------
 
- int __fastcall TQueueItemProxy::GetIndex()
 
- {
 
-   DebugAssert(FQueueStatus != NULL);
 
-   int Index = FQueueStatus->FList->IndexOf(this);
 
-   DebugAssert(Index >= 0);
 
-   return Index;
 
- }
 
- //---------------------------------------------------------------------------
 
- // TTerminalQueueStatus
 
- //---------------------------------------------------------------------------
 
- __fastcall TTerminalQueueStatus::TTerminalQueueStatus() :
 
-   FList(NULL)
 
- {
 
-   FList = new TList();
 
-   ResetStats();
 
- }
 
- //---------------------------------------------------------------------------
 
- __fastcall TTerminalQueueStatus::~TTerminalQueueStatus()
 
- {
 
-   for (int Index = 0; Index < FList->Count; Index++)
 
-   {
 
-     delete GetItem(Index);
 
-   }
 
-   delete FList;
 
-   FList = NULL;
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TTerminalQueueStatus::ResetStats()
 
- {
 
-   FActiveCount = -1;
 
-   FActivePrimaryCount = -1;
 
-   FActiveAndPendingPrimaryCount = -1;
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TTerminalQueueStatus::SetDoneCount(int Value)
 
- {
 
-   FDoneCount = Value;
 
-   ResetStats();
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TTerminalQueueStatus::NeedStats()
 
- {
 
-   if (FActiveCount < 0)
 
-   {
 
-     FActiveCount = 0;
 
-     FActivePrimaryCount = 0;
 
-     FActiveAndPendingPrimaryCount = 0;
 
-     for (int Index = DoneCount; Index < Count; Index++)
 
-     {
 
-       bool Primary = GetItem(Index)->Info->Primary;
 
-       if (GetItem(Index)->Status != TQueueItem::qsPending)
 
-       {
 
-         FActiveCount++;
 
-         if (Primary)
 
-         {
 
-           FActivePrimaryCount++;
 
-         }
 
-       }
 
-       if (Primary)
 
-       {
 
-         FActiveAndPendingPrimaryCount++;
 
-       }
 
-     }
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- int __fastcall TTerminalQueueStatus::GetActiveCount()
 
- {
 
-   NeedStats();
 
-   return FActiveCount;
 
- }
 
- //---------------------------------------------------------------------------
 
- int __fastcall TTerminalQueueStatus::GetDoneAndActiveCount()
 
- {
 
-   return DoneCount + ActiveCount;
 
- }
 
- //---------------------------------------------------------------------------
 
- int __fastcall TTerminalQueueStatus::GetActivePrimaryCount()
 
- {
 
-   NeedStats();
 
-   return FActivePrimaryCount;
 
- }
 
- //---------------------------------------------------------------------------
 
- bool __fastcall TTerminalQueueStatus::IsOnlyOneActiveAndNoPending()
 
- {
 
-   return (ActivePrimaryCount == 1) && (ActiveAndPendingPrimaryCount == 1);
 
- }
 
- //---------------------------------------------------------------------------
 
- int __fastcall TTerminalQueueStatus::GetActiveAndPendingPrimaryCount()
 
- {
 
-   NeedStats();
 
-   return FActiveAndPendingPrimaryCount;
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TTerminalQueueStatus::Add(TQueueItemProxy * ItemProxy)
 
- {
 
-   ItemProxy->FQueueStatus = this;
 
-   int Index = FList->Count;
 
-   if (!ItemProxy->Info->Primary)
 
-   {
 
-     for (int I = 0; I < FList->Count; I++)
 
-     {
 
-       if (Items[I]->Info->GroupToken == ItemProxy->Info->GroupToken)
 
-       {
 
-         Index = I + 1;
 
-       }
 
-     }
 
-     DebugAssert(Index >= 0);
 
-   }
 
-   FList->Insert(Index, ItemProxy);
 
-   ResetStats();
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TTerminalQueueStatus::Delete(TQueueItemProxy * ItemProxy)
 
- {
 
-   FList->Extract(ItemProxy);
 
-   ItemProxy->FQueueStatus = NULL;
 
-   ResetStats();
 
- }
 
- //---------------------------------------------------------------------------
 
- int __fastcall TTerminalQueueStatus::GetCount()
 
- {
 
-   return FList->Count;
 
- }
 
- //---------------------------------------------------------------------------
 
- TQueueItemProxy * __fastcall TTerminalQueueStatus::GetItem(int Index)
 
- {
 
-   return reinterpret_cast<TQueueItemProxy *>(FList->Items[Index]);
 
- }
 
- //---------------------------------------------------------------------------
 
- TQueueItemProxy * __fastcall TTerminalQueueStatus::FindByQueueItem(
 
-   TQueueItem * QueueItem)
 
- {
 
-   TQueueItemProxy * Item;
 
-   for (int Index = 0; Index < FList->Count; Index++)
 
-   {
 
-     Item = GetItem(Index);
 
-     if (Item->FQueueItem == QueueItem)
 
-     {
 
-       return Item;
 
-     }
 
-   }
 
-   return NULL;
 
- }
 
- //---------------------------------------------------------------------------
 
- bool __fastcall TTerminalQueueStatus::UpdateFileList(TQueueItemProxy * ItemProxy, TQueueFileList * FileList)
 
- {
 
-   bool Result;
 
-   if (ItemProxy != NULL)
 
-   {
 
-     Result = ItemProxy->UpdateFileList(FileList);
 
-   }
 
-   else
 
-   {
 
-     Result = (FileList->GetCount() > 0);
 
-     if (Result)
 
-     {
 
-       FileList->Clear();
 
-     }
 
-   }
 
-   return Result;
 
- }
 
- //---------------------------------------------------------------------------
 
- // TBootstrapQueueItem
 
- //---------------------------------------------------------------------------
 
- __fastcall TBootstrapQueueItem::TBootstrapQueueItem()
 
- {
 
-   FInfo->SingleFile = true;
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TBootstrapQueueItem::DoExecute(TTerminal * DebugUsedArg(Terminal))
 
- {
 
-   // noop
 
- }
 
- //---------------------------------------------------------------------------
 
- UnicodeString __fastcall TBootstrapQueueItem::StartupDirectory() const
 
- {
 
-   return UnicodeString();
 
- }
 
- //---------------------------------------------------------------------------
 
- bool __fastcall TBootstrapQueueItem::Complete()
 
- {
 
-   TQueueItem::Complete();
 
-   // To hide the item, even if "keep done items" is on
 
-   return false;
 
- }
 
- //---------------------------------------------------------------------------
 
- // TLocatedQueueItem
 
- //---------------------------------------------------------------------------
 
- __fastcall TLocatedQueueItem::TLocatedQueueItem(TTerminal * Terminal) :
 
-   TQueueItem()
 
- {
 
-   DebugAssert(Terminal != NULL);
 
-   FCurrentDir = Terminal->CurrentDirectory;
 
- }
 
- //---------------------------------------------------------------------------
 
- __fastcall TLocatedQueueItem::TLocatedQueueItem(const TLocatedQueueItem & Source) :
 
-   TQueueItem()
 
- {
 
-   FCurrentDir = Source.FCurrentDir;
 
- }
 
- //---------------------------------------------------------------------------
 
- UnicodeString __fastcall TLocatedQueueItem::StartupDirectory() const
 
- {
 
-   return FCurrentDir;
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TLocatedQueueItem::DoExecute(TTerminal * Terminal)
 
- {
 
-   DebugAssert(Terminal != NULL);
 
-   Terminal->CurrentDirectory = FCurrentDir;
 
- }
 
- //---------------------------------------------------------------------------
 
- // TTransferQueueItem
 
- //---------------------------------------------------------------------------
 
- __fastcall TTransferQueueItem::TTransferQueueItem(TTerminal * Terminal,
 
-   TStrings * FilesToCopy, const UnicodeString & TargetDir,
 
-   const TCopyParamType * CopyParam, int Params, TOperationSide Side,
 
-   bool SingleFile, bool Parallel) :
 
-   TLocatedQueueItem(Terminal), FFilesToCopy(NULL), FCopyParam(NULL)
 
- {
 
-   FInfo->Operation = (Params & cpDelete ? foMove : foCopy);
 
-   FInfo->Side = Side;
 
-   FInfo->SingleFile = SingleFile;
 
-   DebugAssert(FilesToCopy != NULL);
 
-   FFilesToCopy = new TStringList();
 
-   for (int Index = 0; Index < FilesToCopy->Count; Index++)
 
-   {
 
-     UnicodeString FileName = FilesToCopy->Strings[Index];
 
-     TRemoteFile * File = dynamic_cast<TRemoteFile *>(FilesToCopy->Objects[Index]);
 
-     FFilesToCopy->AddObject(FileName, ((File == NULL) || (Side == osLocal)) ? NULL : File->Duplicate());
 
-   }
 
-   FTargetDir = TargetDir;
 
-   DebugAssert(CopyParam != NULL);
 
-   FCopyParam = new TCopyParamType(*CopyParam);
 
-   FParams = Params;
 
-   FParallel = Parallel;
 
-   FLastParallelOperationAdded = GetTickCount();
 
- }
 
- //---------------------------------------------------------------------------
 
- __fastcall TTransferQueueItem::~TTransferQueueItem()
 
- {
 
-   for (int Index = 0; Index < FFilesToCopy->Count; Index++)
 
-   {
 
-     delete FFilesToCopy->Objects[Index];
 
-   }
 
-   delete FFilesToCopy;
 
-   delete FCopyParam;
 
- }
 
- //---------------------------------------------------------------------------
 
- unsigned long __fastcall TTransferQueueItem::DefaultCPSLimit()
 
- {
 
-   return FCopyParam->CPSLimit;
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TTransferQueueItem::DoExecute(TTerminal * Terminal)
 
- {
 
-   TLocatedQueueItem::DoExecute(Terminal);
 
-   DebugAssert(Terminal != NULL);
 
-   FParallelOperation.reset(new TParallelOperation(FInfo->Side));
 
-   try
 
-   {
 
-     DoTransferExecute(Terminal, FParallelOperation.get());
 
-   }
 
-   __finally
 
-   {
 
-     FParallelOperation->WaitFor();
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TTransferQueueItem::ProgressUpdated()
 
- {
 
-   TLocatedQueueItem::ProgressUpdated();
 
-   if (FParallel)
 
-   {
 
-     bool Add = false;
 
-     bool Force = false;
 
-     DWORD LastParallelOperationAddedPrev = 0;
 
-     {
 
-       TGuard Guard(FSection);
 
-       DebugAssert(FParallelOperation.get() != NULL);
 
-       // Won't be initialized, if the operation is not eligible for parallel transfers (like cpDelete).
 
-       // We can probably move the check outside of the guard.
 
-       if (FParallelOperation->IsInitialized())
 
-       {
 
-         DebugAssert(FProgressData->IsTransfer());
 
-         if (FProgressData->Operation == foCopy)
 
-         {
 
-           Add = FParallelOperation->ShouldAddClient();
 
-           if (Add)
 
-           {
 
-             DWORD Now = GetTickCount();
 
-             Force =
 
-               (Now - FLastParallelOperationAdded >= 5*1000) &&
 
-               (TimeToSeconds(FProgressData->TotalTimeLeft()) >= FQueue->ParallelDurationThreshold);
 
-             LastParallelOperationAddedPrev = FLastParallelOperationAdded;
 
-             // update now already to prevent race condition, but we will have to rollback it back,
 
-             // if we actually do not add the parallel operation
 
-             FLastParallelOperationAdded = Now;
 
-           }
 
-         }
 
-       }
 
-     }
 
-     if (Add)
 
-     {
 
-       if (!FQueue->TryAddParallelOperation(this, Force))
 
-       {
 
-         TGuard Guard(FSection);
 
-         FLastParallelOperationAdded = LastParallelOperationAddedPrev;
 
-       }
 
-     }
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- TQueueItem * __fastcall TTransferQueueItem::CreateParallelOperation()
 
- {
 
-   DebugAssert(FParallelOperation.get() != NULL);
 
-   FParallelOperation->AddClient();
 
-   DebugAssert(!FInfo->SingleFile || FParallelOperation->IsParallelFileTransfer);
 
-   FInfo->SingleFile = false;
 
-   return new TParallelTransferQueueItem(this, FParallelOperation.get());
 
- }
 
- //---------------------------------------------------------------------------
 
- bool __fastcall TTransferQueueItem::UpdateFileList(TQueueFileList * FileList)
 
- {
 
-   TGuard Guard(FSection);
 
-   bool Result;
 
-   if ((FParallelOperation.get() != NULL) && FParallelOperation->IsInitialized())
 
-   {
 
-     Result = FParallelOperation->UpdateFileList(FileList);
 
-   }
 
-   else
 
-   {
 
-     Result = false;
 
-   }
 
-   return Result;
 
- }
 
- //---------------------------------------------------------------------------
 
- // TUploadQueueItem
 
- //---------------------------------------------------------------------------
 
- __fastcall TUploadQueueItem::TUploadQueueItem(TTerminal * Terminal,
 
-   TStrings * FilesToCopy, const UnicodeString & TargetDir,
 
-   const TCopyParamType * CopyParam, int Params, bool SingleFile, bool Parallel) :
 
-   TTransferQueueItem(Terminal, FilesToCopy, TargetDir, CopyParam, Params, osLocal, SingleFile, Parallel)
 
- {
 
-   if (FilesToCopy->Count > 1)
 
-   {
 
-     if (FLAGSET(Params, cpTemporary))
 
-     {
 
-       FInfo->Source = L"";
 
-       FInfo->ModifiedLocal = L"";
 
-     }
 
-     else
 
-     {
 
-       ExtractCommonPath(FilesToCopy, FInfo->Source);
 
-       // this way the trailing backslash is preserved for root directories like D:\\
 
-       FInfo->Source = ExtractFileDir(IncludeTrailingBackslash(FInfo->Source));
 
-       FInfo->ModifiedLocal = FLAGCLEAR(Params, cpDelete) ? UnicodeString() :
 
-         IncludeTrailingBackslash(FInfo->Source);
 
-     }
 
-   }
 
-   else
 
-   {
 
-     if (FLAGSET(Params, cpTemporary))
 
-     {
 
-       FInfo->Source = ExtractFileName(FilesToCopy->Strings[0]);
 
-       FInfo->ModifiedLocal = L"";
 
-     }
 
-     else
 
-     {
 
-       DebugAssert(FilesToCopy->Count > 0);
 
-       FInfo->Source = FilesToCopy->Strings[0];
 
-       FInfo->ModifiedLocal = FLAGCLEAR(Params, cpDelete) ? UnicodeString() :
 
-         IncludeTrailingBackslash(ExtractFilePath(FInfo->Source));
 
-     }
 
-   }
 
-   FInfo->Destination =
 
-     UnixIncludeTrailingBackslash(TargetDir) + CopyParam->FileMask;
 
-   FInfo->ModifiedRemote = UnixIncludeTrailingBackslash(TargetDir);
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TUploadQueueItem::DoTransferExecute(TTerminal * Terminal, TParallelOperation * ParallelOperation)
 
- {
 
-   Terminal->CopyToRemote(FFilesToCopy, FTargetDir, FCopyParam, FParams, ParallelOperation);
 
- }
 
- //---------------------------------------------------------------------------
 
- // TParallelTransferQueueItem
 
- //---------------------------------------------------------------------------
 
- __fastcall TParallelTransferQueueItem::TParallelTransferQueueItem(
 
-     const TLocatedQueueItem * ParentItem, TParallelOperation * ParallelOperation) :
 
-   TLocatedQueueItem(*ParentItem),
 
-   FParallelOperation(ParallelOperation)
 
- {
 
-   // deliberately not copying the ModifiedLocal and ModifiedRemote, not to trigger panel refresh, when sub-item completes
 
-   FInfo->Operation = ParentItem->FInfo->Operation;
 
-   FInfo->Side = ParentItem->FInfo->Side;
 
-   FInfo->Source = ParentItem->FInfo->Source;
 
-   FInfo->Destination = ParentItem->FInfo->Destination;
 
-   FInfo->SingleFile = DebugAlwaysFalse(ParentItem->FInfo->SingleFile);
 
-   FInfo->Primary = false;
 
-   FInfo->GroupToken = ParentItem->FInfo->GroupToken;
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TParallelTransferQueueItem::DoExecute(TTerminal * Terminal)
 
- {
 
-   TLocatedQueueItem::DoExecute(Terminal);
 
-   Terminal->LogParallelTransfer(FParallelOperation);
 
-   TFileOperationProgressType OperationProgress(Terminal->OnProgress, Terminal->OnFinished, FParallelOperation->MainOperationProgress);
 
-   TFileOperation Operation = (FLAGSET(FParallelOperation->Params, cpDelete) ? foMove : foCopy);
 
-   bool Temp = FLAGSET(FParallelOperation->Params, cpTemporary);
 
-   OperationProgress.Start(
 
-     // CPS limit inherited from parent OperationProgress.
 
-     // Count not known and won't be needed as we will always have TotalSize as  we always transfer a single file at a time.
 
-     Operation, FParallelOperation->Side, -1, Temp, FParallelOperation->TargetDir, 0, odoIdle);
 
-   try
 
-   {
 
-     bool Continue = true;
 
-     do
 
-     {
 
-       int GotNext = Terminal->CopyToParallel(FParallelOperation, &OperationProgress);
 
-       if (GotNext < 0)
 
-       {
 
-         Continue = false;
 
-       }
 
-       else if (!FQueue->ContinueParallelOperation())
 
-       {
 
-         Continue = false;
 
-       }
 
-     }
 
-     while (Continue);
 
-   }
 
-   __finally
 
-   {
 
-     OperationProgress.Stop();
 
-     FParallelOperation->RemoveClient();
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- // TDownloadQueueItem
 
- //---------------------------------------------------------------------------
 
- static void ExtractRemoteSourcePath(TTerminal * Terminal, TStrings * Files, UnicodeString & Path)
 
- {
 
-   if (!UnixExtractCommonPath(Files, Path))
 
-   {
 
-     Path = Terminal->CurrentDirectory;
 
-   }
 
-   Path = UnixExcludeTrailingBackslash(Path);
 
- }
 
- //---------------------------------------------------------------------------
 
- __fastcall TDownloadQueueItem::TDownloadQueueItem(TTerminal * Terminal,
 
-   TStrings * FilesToCopy, const UnicodeString & TargetDir,
 
-   const TCopyParamType * CopyParam, int Params, bool SingleFile, bool Parallel) :
 
-   TTransferQueueItem(Terminal, FilesToCopy, TargetDir, CopyParam, Params, osRemote, SingleFile, Parallel)
 
- {
 
-   if (FilesToCopy->Count > 1)
 
-   {
 
-     ExtractRemoteSourcePath(Terminal, FilesToCopy, FInfo->Source);
 
-     FInfo->ModifiedRemote = FLAGCLEAR(Params, cpDelete) ? UnicodeString() :
 
-       UnixIncludeTrailingBackslash(FInfo->Source);
 
-   }
 
-   else
 
-   {
 
-     DebugAssert(FilesToCopy->Count > 0);
 
-     FInfo->Source = FilesToCopy->Strings[0];
 
-     if (UnixExtractFilePath(FInfo->Source).IsEmpty())
 
-     {
 
-       FInfo->Source = UnixIncludeTrailingBackslash(Terminal->CurrentDirectory) +
 
-         FInfo->Source;
 
-       FInfo->ModifiedRemote = FLAGCLEAR(Params, cpDelete) ? UnicodeString() :
 
-         UnixIncludeTrailingBackslash(Terminal->CurrentDirectory);
 
-     }
 
-     else
 
-     {
 
-       FInfo->ModifiedRemote = FLAGCLEAR(Params, cpDelete) ? UnicodeString() :
 
-         UnixExtractFilePath(FInfo->Source);
 
-     }
 
-   }
 
-   if (FLAGSET(Params, cpTemporary))
 
-   {
 
-     FInfo->Destination = L"";
 
-   }
 
-   else
 
-   {
 
-     FInfo->Destination =
 
-       IncludeTrailingBackslash(TargetDir) + CopyParam->FileMask;
 
-   }
 
-   FInfo->ModifiedLocal = IncludeTrailingBackslash(TargetDir);
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TDownloadQueueItem::DoTransferExecute(TTerminal * Terminal, TParallelOperation * ParallelOperation)
 
- {
 
-   Terminal->CopyToLocal(FFilesToCopy, FTargetDir, FCopyParam, FParams, ParallelOperation);
 
- }
 
- //---------------------------------------------------------------------------
 
- //---------------------------------------------------------------------------
 
- TDeleteQueueItem::TDeleteQueueItem(TTerminal * Terminal, TStrings * FilesToDelete, int Params) :
 
-   TLocatedQueueItem(Terminal)
 
- {
 
-   FInfo->Operation = foDelete;
 
-   FInfo->Side = osRemote;
 
-   DebugAssert(FilesToDelete != NULL);
 
-   FFilesToDelete.reset(TRemoteFileList::CloneStrings(FilesToDelete));
 
-   ExtractRemoteSourcePath(Terminal, FilesToDelete, FInfo->Source);
 
-   FInfo->ModifiedRemote = FInfo->Source;
 
-   FParams = Params;
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TDeleteQueueItem::DoExecute(TTerminal * Terminal)
 
- {
 
-   TLocatedQueueItem::DoExecute(Terminal);
 
-   DebugAssert(Terminal != NULL);
 
-   Terminal->DeleteFiles(FFilesToDelete.get(), FParams);
 
- }
 
- //---------------------------------------------------------------------------
 
- // TTerminalThread
 
- //---------------------------------------------------------------------------
 
- __fastcall TTerminalThread::TTerminalThread(TTerminal * Terminal) :
 
-   TSignalThread(false), FTerminal(Terminal)
 
- {
 
-   FAction = NULL;
 
-   FActionEvent = CreateEvent(NULL, false, false, NULL);
 
-   FException = NULL;
 
-   FIdleException = NULL;
 
-   FOnIdle = NULL;
 
-   FUserAction = NULL;
 
-   FCancel = false;
 
-   FAbandoned = false;
 
-   FAllowAbandon = false;
 
-   FMainThread = GetCurrentThreadId();
 
-   FSection = new TCriticalSection();
 
-   FOnInformation = FTerminal->OnInformation;
 
-   FOnQueryUser = FTerminal->OnQueryUser;
 
-   FOnPromptUser = FTerminal->OnPromptUser;
 
-   FOnShowExtendedException = FTerminal->OnShowExtendedException;
 
-   FOnDisplayBanner = FTerminal->OnDisplayBanner;
 
-   FOnChangeDirectory = FTerminal->OnChangeDirectory;
 
-   FOnReadDirectory = FTerminal->OnReadDirectory;
 
-   FOnStartReadDirectory = FTerminal->OnStartReadDirectory;
 
-   FOnReadDirectoryProgress = FTerminal->OnReadDirectoryProgress;
 
-   FOnInitializeLog = FTerminal->OnInitializeLog;
 
-   FTerminal->OnInformation = TerminalInformation;
 
-   FTerminal->OnQueryUser = TerminalQueryUser;
 
-   FTerminal->OnPromptUser = TerminalPromptUser;
 
-   FTerminal->OnShowExtendedException = TerminalShowExtendedException;
 
-   FTerminal->OnDisplayBanner = TerminalDisplayBanner;
 
-   FTerminal->OnChangeDirectory = TerminalChangeDirectory;
 
-   FTerminal->OnReadDirectory = TerminalReadDirectory;
 
-   FTerminal->OnStartReadDirectory = TerminalStartReadDirectory;
 
-   FTerminal->OnReadDirectoryProgress = TerminalReadDirectoryProgress;
 
-   FTerminal->OnInitializeLog = TerminalInitializeLog;
 
-   Start();
 
- }
 
- //---------------------------------------------------------------------------
 
- __fastcall TTerminalThread::~TTerminalThread()
 
- {
 
-   Close();
 
-   CloseHandle(FActionEvent);
 
-   DebugAssert(FTerminal->OnInformation == TerminalInformation);
 
-   DebugAssert(FTerminal->OnQueryUser == TerminalQueryUser);
 
-   DebugAssert(FTerminal->OnPromptUser == TerminalPromptUser);
 
-   DebugAssert(FTerminal->OnShowExtendedException == TerminalShowExtendedException);
 
-   DebugAssert(FTerminal->OnDisplayBanner == TerminalDisplayBanner);
 
-   DebugAssert(FTerminal->OnChangeDirectory == TerminalChangeDirectory);
 
-   DebugAssert(FTerminal->OnReadDirectory == TerminalReadDirectory);
 
-   DebugAssert(FTerminal->OnStartReadDirectory == TerminalStartReadDirectory);
 
-   DebugAssert(FTerminal->OnReadDirectoryProgress == TerminalReadDirectoryProgress);
 
-   DebugAssert(FTerminal->OnInitializeLog == TerminalInitializeLog);
 
-   FTerminal->OnInformation = FOnInformation;
 
-   FTerminal->OnQueryUser = FOnQueryUser;
 
-   FTerminal->OnPromptUser = FOnPromptUser;
 
-   FTerminal->OnShowExtendedException = FOnShowExtendedException;
 
-   FTerminal->OnDisplayBanner = FOnDisplayBanner;
 
-   FTerminal->OnChangeDirectory = FOnChangeDirectory;
 
-   FTerminal->OnReadDirectory = FOnReadDirectory;
 
-   FTerminal->OnStartReadDirectory = FOnStartReadDirectory;
 
-   FTerminal->OnReadDirectoryProgress = FOnReadDirectoryProgress;
 
-   FTerminal->OnInitializeLog = FOnInitializeLog;
 
-   delete FSection;
 
-   if (FAbandoned)
 
-   {
 
-     delete FTerminal;
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TTerminalThread::Cancel()
 
- {
 
-   FCancel = true;
 
-   FCancelAfter = IncMilliSecond(Now(), 1000);
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TTerminalThread::Idle()
 
- {
 
-   TGuard Guard(FSection);
 
-   // only when running user action already,
 
-   // so that the exception is caught, saved and actually
 
-   // passed back into the terminal thread, saved again
 
-   // and passed back to us
 
-   if ((FUserAction != NULL) && (FIdleException != NULL))
 
-   {
 
-     Rethrow(FIdleException);
 
-   }
 
-   FPendingIdle = true;
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TTerminalThread::TerminalOpen()
 
- {
 
-   RunAction(TerminalOpenEvent);
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TTerminalThread::TerminalReopen()
 
- {
 
-   RunAction(TerminalReopenEvent);
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TTerminalThread::RunAction(TNotifyEvent Action)
 
- {
 
-   DebugAssert(FAction == NULL);
 
-   DebugAssert(FException == NULL);
 
-   DebugAssert(FIdleException == NULL);
 
-   DebugAssert(FOnIdle != NULL);
 
-   FCancelled = false;
 
-   FAction = Action;
 
-   try
 
-   {
 
-     try
 
-     {
 
-       TriggerEvent();
 
-       bool Done = false;
 
-       const unsigned int MaxWait = 50;
 
-       unsigned int Wait = MaxWait;
 
-       do
 
-       {
 
-         switch (WaitForSingleObject(FActionEvent, Wait))
 
-         {
 
-           case WAIT_OBJECT_0:
 
-             Done = true;
 
-             break;
 
-           case WAIT_TIMEOUT:
 
-             if (FUserAction != NULL)
 
-             {
 
-               try
 
-               {
 
-                 FUserAction->Execute(NULL);
 
-               }
 
-               catch (Exception & E)
 
-               {
 
-                 SaveException(E, FException);
 
-               }
 
-               FUserAction = NULL;
 
-               TriggerEvent();
 
-               Wait = 0;
 
-             }
 
-             else
 
-             {
 
-               if (FOnIdle != NULL)
 
-               {
 
-                 FOnIdle(NULL);
 
-               }
 
-               Wait = std::min(Wait + 10, MaxWait);
 
-             }
 
-             break;
 
-           default:
 
-             throw Exception(L"Error waiting for background session task to complete");
 
-         }
 
-         if (AllowAbandon && !Done && FCancel && (Now() >= FCancelAfter))
 
-         {
 
-           TGuard Guard(FSection);
 
-           if (WaitForSingleObject(FActionEvent, 0) != WAIT_OBJECT_0)
 
-           {
 
-             FAbandoned = true;
 
-             FCancelled = true;
 
-             FatalAbort();
 
-           }
 
-         }
 
-       }
 
-       while (!Done);
 
-       if (Done)
 
-       {
 
-         Rethrow(FException);
 
-       }
 
-     }
 
-     __finally
 
-     {
 
-       FAction = NULL;
 
-       SAFE_DESTROY(FException);
 
-     }
 
-   }
 
-   catch(...)
 
-   {
 
-     if (FCancelled)
 
-     {
 
-       // even if the abort thrown as result of Cancel() was wrapped into
 
-       // some higher-level exception, normalize back to message-less fatal
 
-       // exception here
 
-       FatalAbort();
 
-     }
 
-     else
 
-     {
 
-       throw;
 
-     }
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TTerminalThread::TerminalOpenEvent(TObject * /*Sender*/)
 
- {
 
-   FTerminal->Open();
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TTerminalThread::TerminalReopenEvent(TObject * /*Sender*/)
 
- {
 
-   FTerminal->Reopen(0);
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TTerminalThread::ProcessEvent()
 
- {
 
-   DebugAssert(FEvent != NULL);
 
-   DebugAssert(FException == NULL);
 
-   try
 
-   {
 
-     FAction(NULL);
 
-   }
 
-   catch(Exception & E)
 
-   {
 
-     SaveException(E, FException);
 
-   }
 
-   {
 
-     TGuard Guard(FSection);
 
-     if (!FAbandoned)
 
-     {
 
-       SetEvent(FActionEvent);
 
-     }
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TTerminalThread::Rethrow(Exception *& Exception)
 
- {
 
-   if (Exception != NULL)
 
-   {
 
-     try
 
-     {
 
-       RethrowException(Exception);
 
-     }
 
-     __finally
 
-     {
 
-       SAFE_DESTROY(Exception);
 
-     }
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TTerminalThread::SaveException(Exception & E, Exception *& Exception)
 
- {
 
-   DebugAssert(Exception == NULL);
 
-   Exception = CloneException(&E);
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TTerminalThread::FatalAbort()
 
- {
 
-   if (FAbandoned)
 
-   {
 
-     // We cannot use TTerminal::FatalError as the terminal still runs on a backgroud thread,
 
-     // may have its TCallbackGuard armed right now.
 
-     throw ESshFatal(NULL, L"");
 
-   }
 
-   else
 
-   {
 
-     FTerminal->FatalAbort();
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TTerminalThread::CheckCancel()
 
- {
 
-   if (FCancel && !FCancelled)
 
-   {
 
-     FCancelled = true;
 
-     FatalAbort();
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TTerminalThread::WaitForUserAction(TUserAction * UserAction)
 
- {
 
-   DWORD Thread = GetCurrentThreadId();
 
-   // we can get called from the main thread from within Idle,
 
-   // should be only to call HandleExtendedException
 
-   if (Thread == FMainThread)
 
-   {
 
-     if (UserAction != NULL)
 
-     {
 
-       UserAction->Execute(NULL);
 
-     }
 
-   }
 
-   else
 
-   {
 
-     // we should be called from our thread only,
 
-     // with exception noted above
 
-     DebugAssert(Thread == FThreadId);
 
-     bool DoCheckCancel =
 
-       DebugAlwaysFalse(UserAction == NULL) || !UserAction->Force() || FAbandoned;
 
-     if (DoCheckCancel)
 
-     {
 
-       CheckCancel();
 
-     }
 
-     // have to save it as we can go recursive via TQueryParams::TimerEvent,
 
-     // see TTerminalThread::TerminalQueryUser
 
-     TUserAction * PrevUserAction = FUserAction;
 
-     try
 
-     {
 
-       FUserAction = UserAction;
 
-       while (true)
 
-       {
 
-         {
 
-           TGuard Guard(FSection);
 
-           // If idle exception is already set, we are only waiting
 
-           // for the main thread to pick it up
 
-           // (or at least to finish handling the user action, so
 
-           // that we rethrow the idle exception below)
 
-           // Also if idle exception is set, it is probable that terminal
 
-           // is not active anyway.
 
-           if (FTerminal->Active && FPendingIdle && (FIdleException == NULL))
 
-           {
 
-             FPendingIdle = false;
 
-             try
 
-             {
 
-               FTerminal->Idle();
 
-             }
 
-             catch (Exception & E)
 
-             {
 
-               SaveException(E, FIdleException);
 
-             }
 
-           }
 
-         }
 
-         int WaitResult = WaitForEvent(1000);
 
-         if (WaitResult == 0)
 
-         {
 
-           SAFE_DESTROY(FIdleException);
 
-           FatalAbort();
 
-         }
 
-         else if (WaitResult > 0)
 
-         {
 
-           break;
 
-         }
 
-       }
 
-       Rethrow(FException);
 
-       if (FIdleException != NULL)
 
-       {
 
-         // idle exception was not used to cancel the user action
 
-         // (if it where it would be already cloned into the FException above
 
-         // and rethrown)
 
-         Rethrow(FIdleException);
 
-       }
 
-     }
 
-     __finally
 
-     {
 
-       FUserAction = PrevUserAction;
 
-       SAFE_DESTROY(FException);
 
-     }
 
-     // Contrary to a call before, this is unconditional,
 
-     // otherwise cancelling authentication won't work,
 
-     // if it is tried only after the last user action
 
-     // (what is common, when cancelling while waiting for
 
-     // resolving of unresolvable host name, where the last user action is
 
-     // "resolving hostname" information action)
 
-     CheckCancel();
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TTerminalThread::TerminalInformation(
 
-   TTerminal * Terminal, const UnicodeString & Str, bool Status, int Phase, const UnicodeString & Additional)
 
- {
 
-   TInformationUserAction Action(FOnInformation);
 
-   Action.Terminal = Terminal;
 
-   Action.Str = Str;
 
-   Action.Status = Status;
 
-   Action.Phase = Phase;
 
-   Action.Additional = Additional;
 
-   WaitForUserAction(&Action);
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TTerminalThread::TerminalQueryUser(TObject * Sender,
 
-   const UnicodeString Query, TStrings * MoreMessages, unsigned int Answers,
 
-   const TQueryParams * Params, unsigned int & Answer, TQueryType Type, void * Arg)
 
- {
 
-   DebugUsedParam(Arg);
 
-   DebugAssert(Arg == NULL);
 
-   // note about TQueryParams::TimerEvent
 
-   // So far there is only one use for this, the TSecureShell::SendBuffer,
 
-   // which should be thread-safe, as long as the terminal thread,
 
-   // is stopped waiting for OnQueryUser to finish.
 
-   // note about TQueryButtonAlias::OnClick
 
-   // So far there is only one use for this, the TClipboardHandler,
 
-   // which is thread-safe.
 
-   TQueryUserAction Action(FOnQueryUser);
 
-   Action.Sender = Sender;
 
-   Action.Query = Query;
 
-   Action.MoreMessages = MoreMessages;
 
-   Action.Answers = Answers;
 
-   Action.Params = Params;
 
-   Action.Answer = Answer;
 
-   Action.Type = Type;
 
-   WaitForUserAction(&Action);
 
-   Answer = Action.Answer;
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TTerminalThread::TerminalInitializeLog(TObject * Sender)
 
- {
 
-   if (FOnInitializeLog != NULL)
 
-   {
 
-     // never used, so not tested either
 
-     DebugFail();
 
-     TNotifyAction Action(FOnInitializeLog);
 
-     Action.Sender = Sender;
 
-     WaitForUserAction(&Action);
 
-   }
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TTerminalThread::TerminalPromptUser(TTerminal * Terminal,
 
-   TPromptKind Kind, UnicodeString Name, UnicodeString Instructions, TStrings * Prompts,
 
-   TStrings * Results, bool & Result, void * Arg)
 
- {
 
-   DebugUsedParam(Arg);
 
-   DebugAssert(Arg == NULL);
 
-   TPromptUserAction Action(FOnPromptUser);
 
-   Action.Terminal = Terminal;
 
-   Action.Kind = Kind;
 
-   Action.Name = Name;
 
-   Action.Instructions = Instructions;
 
-   Action.Prompts = Prompts;
 
-   Action.Results->AddStrings(Results);
 
-   WaitForUserAction(&Action);
 
-   Results->Clear();
 
-   Results->AddStrings(Action.Results);
 
-   Result = Action.Result;
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TTerminalThread::TerminalShowExtendedException(
 
-   TTerminal * Terminal, Exception * E, void * Arg)
 
- {
 
-   DebugUsedParam(Arg);
 
-   DebugAssert(Arg == NULL);
 
-   TShowExtendedExceptionAction Action(FOnShowExtendedException);
 
-   Action.Terminal = Terminal;
 
-   Action.E = E;
 
-   WaitForUserAction(&Action);
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TTerminalThread::TerminalDisplayBanner(TTerminal * Terminal,
 
-   UnicodeString SessionName, const UnicodeString & Banner,
 
-   bool & NeverShowAgain, int Options, unsigned int & Params)
 
- {
 
-   TDisplayBannerAction Action(FOnDisplayBanner);
 
-   Action.Terminal = Terminal;
 
-   Action.SessionName = SessionName;
 
-   Action.Banner = Banner;
 
-   Action.NeverShowAgain = NeverShowAgain;
 
-   Action.Options = Options;
 
-   Action.Params = Params;
 
-   WaitForUserAction(&Action);
 
-   NeverShowAgain = Action.NeverShowAgain;
 
-   Params = Action.Params;
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TTerminalThread::TerminalChangeDirectory(TObject * Sender)
 
- {
 
-   TNotifyAction Action(FOnChangeDirectory);
 
-   Action.Sender = Sender;
 
-   WaitForUserAction(&Action);
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TTerminalThread::TerminalReadDirectory(TObject * Sender, Boolean ReloadOnly)
 
- {
 
-   TReadDirectoryAction Action(FOnReadDirectory);
 
-   Action.Sender = Sender;
 
-   Action.ReloadOnly = ReloadOnly;
 
-   WaitForUserAction(&Action);
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TTerminalThread::TerminalStartReadDirectory(TObject * Sender)
 
- {
 
-   TNotifyAction Action(FOnStartReadDirectory);
 
-   Action.Sender = Sender;
 
-   WaitForUserAction(&Action);
 
- }
 
- //---------------------------------------------------------------------------
 
- void __fastcall TTerminalThread::TerminalReadDirectoryProgress(
 
-   TObject * Sender, int Progress, int ResolvedLinks, bool & Cancel)
 
- {
 
-   TReadDirectoryProgressAction Action(FOnReadDirectoryProgress);
 
-   Action.Sender = Sender;
 
-   Action.Progress = Progress;
 
-   Action.ResolvedLinks = ResolvedLinks;
 
-   Action.Cancel = Cancel;
 
-   WaitForUserAction(&Action);
 
-   Cancel = Action.Cancel;
 
- }
 
- //---------------------------------------------------------------------------
 
- bool __fastcall TTerminalThread::Release()
 
- {
 
-   bool Result = !FAbandoned;
 
-   if (Result)
 
-   {
 
-     delete this;
 
-   }
 
-   return Result;
 
- }
 
- //---------------------------------------------------------------------------
 
- bool __fastcall TTerminalThread::Finished()
 
- {
 
-   return TSimpleThread::Finished() || FAbandoned;
 
- }
 
- //---------------------------------------------------------------------------
 
- //---------------------------------------------------------------------------
 
- TQueueFileList::TQueueFileList() :
 
-   FList(new TStringList())
 
- {
 
-   Clear();
 
- }
 
- //---------------------------------------------------------------------------
 
- void TQueueFileList::Clear()
 
- {
 
-   FList->Clear();
 
-   FLastParallelOperation = NULL;
 
-   FLastParallelOperationVersion = -1;
 
- }
 
- //---------------------------------------------------------------------------
 
- void TQueueFileList::Add(const UnicodeString & FileName, int State)
 
- {
 
-   FList->AddObject(FileName, reinterpret_cast<TObject *>(State));
 
- }
 
- //---------------------------------------------------------------------------
 
- UnicodeString TQueueFileList::GetFileName(int Index) const
 
- {
 
-   return FList->Strings[Index];
 
- }
 
- //---------------------------------------------------------------------------
 
- int TQueueFileList::GetState(int Index) const
 
- {
 
-   return reinterpret_cast<int>(FList->Objects[Index]);
 
- }
 
- //---------------------------------------------------------------------------
 
- void TQueueFileList::SetState(int Index, int State)
 
- {
 
-   FList->Objects[Index] = reinterpret_cast<TObject *>(State);
 
- }
 
- //---------------------------------------------------------------------------
 
- int TQueueFileList::GetCount() const
 
- {
 
-   return FList->Count;
 
- }
 
 
  |