| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725 |
- //---------------------------------------------------------------------------
- #include <vcl.h>
- #pragma hdrstop
- #include "Common.h"
- #include "Terminal.h"
- #include "Queue.h"
- //---------------------------------------------------------------------------
- #pragma package(smart_init)
- //---------------------------------------------------------------------------
- class TBackgroundTerminal;
- //---------------------------------------------------------------------------
- class TUserAction
- {
- public:
- virtual __fastcall ~TUserAction() {}
- virtual void __fastcall Execute(TTerminalQueue * Queue, void * Arg) = 0;
- };
- //---------------------------------------------------------------------------
- class TQueryUserAction : public TUserAction
- {
- public:
- virtual void __fastcall Execute(TTerminalQueue * Queue, void * Arg)
- {
- Queue->DoQueryUser(Sender, Query, MoreMessages, Answers, Params, Answer, Type, Arg);
- }
- TObject * Sender;
- AnsiString Query;
- TStrings * MoreMessages;
- int Answers;
- const TQueryParams * Params;
- int Answer;
- TQueryType Type;
- };
- //---------------------------------------------------------------------------
- class TPromptUserAction : public TUserAction
- {
- public:
- __fastcall TPromptUserAction() :
- Results(new TStringList())
- {
- }
- virtual __fastcall ~TPromptUserAction()
- {
- delete Results;
- }
- virtual void __fastcall Execute(TTerminalQueue * Queue, void * Arg)
- {
- Queue->DoPromptUser(Terminal, Kind, Name, Instructions, Prompts, Results, Result, Arg);
- }
- TTerminal * Terminal;
- TPromptKind Kind;
- AnsiString Name;
- AnsiString Instructions;
- TStrings * Prompts;
- TStrings * Results;
- bool Result;
- };
- //---------------------------------------------------------------------------
- class TShowExtendedExceptionAction : public TUserAction
- {
- public:
- virtual void __fastcall Execute(TTerminalQueue * Queue, void * Arg)
- {
- Queue->DoShowExtendedException(Terminal, E, Arg);
- }
- TTerminal * Terminal;
- Exception * E;
- };
- //---------------------------------------------------------------------------
- class TTerminalItem : public TSignalThread
- {
- friend class TQueueItem;
- friend class TBackgroundTerminal;
- public:
- __fastcall TTerminalItem(TTerminalQueue * Queue, int Index);
- __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 void __fastcall Finished();
- bool __fastcall WaitForUserAction(TQueueItem::TStatus ItemStatus, TUserAction * UserAction);
- bool __fastcall OverrideItemStatus(TQueueItem::TStatus & ItemStatus);
- void __fastcall TerminalQueryUser(TObject * Sender,
- const AnsiString Query, TStrings * MoreMessages, int Answers,
- const TQueryParams * Params, int & Answer, TQueryType Type, void * Arg);
- void __fastcall TerminalPromptUser(TTerminal * Terminal, TPromptKind Kind,
- AnsiString Name, AnsiString 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 AnsiString & FileName, bool Success,
- bool & DisconnectWhenFinished);
- void __fastcall OperationProgress(TFileOperationProgressType & ProgressData,
- TCancelStatus & Cancel);
- };
- //---------------------------------------------------------------------------
- // TSignalThread
- //---------------------------------------------------------------------------
- int __fastcall TSimpleThread::ThreadProc(void * Thread)
- {
- TSimpleThread * SimpleThread = reinterpret_cast<TSimpleThread*>(Thread);
- assert(SimpleThread != NULL);
- try
- {
- SimpleThread->Execute();
- }
- catch(...)
- {
- // we do not expect thread to be terminated with exception
- assert(false);
- }
- SimpleThread->FFinished = true;
- SimpleThread->Finished();
- return 0;
- }
- //---------------------------------------------------------------------------
- __fastcall TSimpleThread::TSimpleThread() :
- FThread(NULL), FFinished(true)
- {
- unsigned ThreadID;
- FThread = reinterpret_cast<HANDLE>(
- StartThread(NULL, 0, ThreadProc, this, CREATE_SUSPENDED, ThreadID));
- }
- //---------------------------------------------------------------------------
- __fastcall TSimpleThread::~TSimpleThread()
- {
- Close();
- if (FThread != NULL)
- {
- CloseHandle(FThread);
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TSimpleThread::Start()
- {
- if (ResumeThread(FThread) == 1)
- {
- FFinished = false;
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TSimpleThread::Finished()
- {
- }
- //---------------------------------------------------------------------------
- void __fastcall TSimpleThread::Close()
- {
- if (!FFinished)
- {
- Terminate();
- WaitFor();
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TSimpleThread::WaitFor()
- {
- WaitForSingleObject(FThread, INFINITE);
- }
- //---------------------------------------------------------------------------
- // TSignalThread
- //---------------------------------------------------------------------------
- __fastcall TSignalThread::TSignalThread() :
- TSimpleThread(),
- FTerminated(true), FEvent(NULL)
- {
- FEvent = CreateEvent(NULL, false, false, NULL);
- assert(FEvent != NULL);
- ::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()
- {
- return (WaitForSingleObject(FEvent, INFINITE) == WAIT_OBJECT_0) &&
- !FTerminated;
- }
- //---------------------------------------------------------------------------
- void __fastcall TSignalThread::Execute()
- {
- while (!FTerminated)
- {
- if (WaitForEvent())
- {
- ProcessEvent();
- }
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TSignalThread::Terminate()
- {
- FTerminated = true;
- TriggerEvent();
- }
- //---------------------------------------------------------------------------
- // TTerminalQueue
- //---------------------------------------------------------------------------
- __fastcall TTerminalQueue::TTerminalQueue(TTerminal * Terminal,
- TConfiguration * Configuration) :
- FTerminal(Terminal), FTransfersLimit(2),
- FConfiguration(Configuration), FSessionData(NULL), FItems(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 = EncodeTime(0, 0, 2, 0);
- assert(Terminal != NULL);
- FSessionData = new TSessionData("");
- FSessionData->Assign(Terminal->SessionData);
- FItems = new TList();
- FTerminals = 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;
- for (int Index = 0; Index < FItems->Count; Index++)
- {
- delete GetItem(Index);
- }
- delete FItems;
- }
- delete FItemsSection;
- delete FSessionData;
- }
- //---------------------------------------------------------------------------
- void __fastcall TTerminalQueue::TerminalFinished(TTerminalItem * TerminalItem)
- {
- if (!FTerminated)
- {
- {
- TGuard Guard(FItemsSection);
- int Index = FTerminals->IndexOf(TerminalItem);
- assert(Index >= 0);
- if (Index < FFreeTerminals)
- {
- FFreeTerminals--;
- }
- // Index may be >= FTransfersLimit also when the transfer limit was
- // recently decresed, 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);
- assert(Index >= 0);
- assert(Index >= FFreeTerminals);
- Result = (FTransfersLimit <= 0) || (Index < FTransfersLimit);
- if (Result)
- {
- FTerminals->Move(Index, 0);
- FFreeTerminals++;
- }
- }
- TriggerEvent();
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- void __fastcall TTerminalQueue::AddItem(TQueueItem * Item)
- {
- assert(!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);
- assert(Index < FItemsInProcess);
- USEDPARAM(Index);
- FItemsInProcess--;
- FItems->Add(Item);
- }
- DoListUpdate();
- TriggerEvent();
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TTerminalQueue::DeleteItem(TQueueItem * Item)
- {
- if (!FTerminated)
- {
- bool Empty;
- bool Monitored;
- {
- TGuard Guard(FItemsSection);
- // does this need to be within guard?
- Monitored = (Item->CompleteEvent != INVALID_HANDLE_VALUE);
- int Index = FItems->Remove(Item);
- assert(Index < FItemsInProcess);
- USEDPARAM(Index);
- FItemsInProcess--;
- delete Item;
- Empty = true;
- Index = 0;
- while (Empty && (Index < FItems->Count))
- {
- Empty = (GetItem(Index) != INVALID_HANDLE_VALUE);
- Index++;
- }
- }
- DoListUpdate();
- // report empty, if queue is empty or only monitored items are pending.
- // do not report if current item was the last, but was monitored.
- if (!Monitored && Empty)
- {
- DoEvent(qeEmpty);
- }
- }
- }
- //---------------------------------------------------------------------------
- TQueueItem * __fastcall TTerminalQueue::GetItem(int Index)
- {
- return reinterpret_cast<TQueueItem*>(FItems->Items[Index]);
- }
- //---------------------------------------------------------------------------
- TTerminalQueueStatus * __fastcall TTerminalQueue::CreateStatus(TTerminalQueueStatus * Current)
- {
- TTerminalQueueStatus * Status = new TTerminalQueueStatus();
- try
- {
- try
- {
- TGuard Guard(FItemsSection);
- TQueueItem * Item;
- TQueueItemProxy * ItemProxy;
- for (int Index = 0; Index < FItems->Count; Index++)
- {
- Item = GetItem(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));
- }
- }
- }
- __finally
- {
- if (Current != NULL)
- {
- delete Current;
- }
- }
- }
- catch(...)
- {
- delete Status;
- throw;
- }
- return Status;
- }
- //---------------------------------------------------------------------------
- bool __fastcall TTerminalQueue::ItemGetData(TQueueItem * Item,
- TQueueItemProxy * Proxy)
- {
- // 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->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))
- {
- FTemporaryTerminals++;
- }
- }
- }
- 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);
- UpdateList = true;
- }
- else
- {
- Item->FTerminalItem->Cancel();
- }
- }
- }
- 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;
- }
- //---------------------------------------------------------------------------
- void __fastcall TTerminalQueue::Idle()
- {
- if (Now() - FLastIdle > FIdleInterval)
- {
- FLastIdle = FIdleInterval;
- 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();
- }
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TTerminalQueue::ProcessEvent()
- {
- TTerminalItem * TerminalItem;
- TQueueItem * Item;
- do
- {
- TerminalItem = NULL;
- Item = NULL;
- if (FItems->Count > FItemsInProcess)
- {
- TGuard Guard(FItemsSection);
- 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)
- {
- Item = GetItem(FItemsInProcess);
- 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::DoQueryUser(TObject * Sender,
- const AnsiString Query, TStrings * MoreMessages, int Answers,
- const TQueryParams * Params, int & Answer, TQueryType Type, void * Arg)
- {
- if (OnQueryUser != NULL)
- {
- OnQueryUser(Sender, Query, MoreMessages, Answers, Params, Answer, Type, Arg);
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TTerminalQueue::DoPromptUser(TTerminal * Terminal,
- TPromptKind Kind, AnsiString Name, AnsiString Instructions,
- TStrings * Prompts, TStrings * Results, bool & Result, void * Arg)
- {
- if (OnPromptUser != NULL)
- {
- OnPromptUser(Terminal, Kind, Name, Instructions, Prompts, Results, Result, Arg);
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TTerminalQueue::DoShowExtendedException(
- TTerminal * Terminal, Exception * E, void * Arg)
- {
- if (OnShowExtendedException != NULL)
- {
- OnShowExtendedException(Terminal, E, Arg);
- }
- }
- //---------------------------------------------------------------------------
- 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();
- }
- }
- //---------------------------------------------------------------------------
- bool __fastcall TTerminalQueue::GetIsEmpty()
- {
- TGuard Guard(FItemsSection);
- return (FItems->Count == 0);
- }
- //---------------------------------------------------------------------------
- // TBackgroundItem
- //---------------------------------------------------------------------------
- class TBackgroundTerminal : public TSecondaryTerminal
- {
- friend class TTerminalItem;
- public:
- __fastcall TBackgroundTerminal(TTerminal * MainTerminal,
- TSessionData * SessionData, TConfiguration * Configuration,
- TTerminalItem * Item, const AnsiString & Name);
- protected:
- virtual bool __fastcall DoQueryReopen(Exception * E);
- private:
- TTerminalItem * FItem;
- };
- //---------------------------------------------------------------------------
- __fastcall TBackgroundTerminal::TBackgroundTerminal(TTerminal * MainTerminal,
- TSessionData * SessionData, TConfiguration * Configuration, TTerminalItem * Item,
- const AnsiString & Name) :
- TSecondaryTerminal(MainTerminal, SessionData, Configuration, Name), FItem(Item)
- {
- }
- //---------------------------------------------------------------------------
- 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(), FQueue(Queue), FTerminal(NULL), FItem(NULL),
- FCriticalSection(NULL), FUserAction(NULL)
- {
- FCriticalSection = new TCriticalSection();
- FTerminal = new TBackgroundTerminal(FQueue->FTerminal, Queue->FSessionData,
- FQueue->FConfiguration, this, FORMAT("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();
- assert(FItem == NULL);
- delete FTerminal;
- delete FCriticalSection;
- }
- //---------------------------------------------------------------------------
- void __fastcall TTerminalItem::Process(TQueueItem * Item)
- {
- {
- TGuard Guard(FCriticalSection);
- assert(FItem == NULL);
- FItem = Item;
- }
- TriggerEvent();
- }
- //---------------------------------------------------------------------------
- void __fastcall TTerminalItem::ProcessEvent()
- {
- TGuard Guard(FCriticalSection);
- bool Retry = true;
- FCancel = false;
- FPause = false;
- FItem->FTerminalItem = this;
- try
- {
- assert(FItem != NULL);
- if (!FTerminal->Active)
- {
- FItem->SetStatus(TQueueItem::qsConnecting);
- FTerminal->SessionData->RemoteDirectory = FItem->StartupDirectory();
- FTerminal->Open();
- }
- Retry = false;
- if (!FCancel)
- {
- FItem->SetStatus(TQueueItem::qsProcessing);
- FItem->Execute(this);
- }
- }
- catch(Exception & E)
- {
- // do not show error messages, if task was canceled anyway
- // (for example if transfer is cancelled during reconnection attempts)
- if (!FCancel &&
- (FTerminal->QueryUserException("", &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);
- }
- if (!FTerminal->Active ||
- !FQueue->TerminalFree(this))
- {
- Terminate();
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TTerminalItem::Idle()
- {
- TGuard Guard(FCriticalSection);
- assert(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()
- {
- assert(FItem != NULL);
- bool Result = (FItem->GetStatus() == TQueueItem::qsProcessing) && !FPause;
- if (Result)
- {
- FPause = true;
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- bool __fastcall TTerminalItem::Resume()
- {
- assert(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)
- {
- assert(FItem != NULL);
- FUserAction->Execute(FQueue, Arg);
- FUserAction = NULL;
- TriggerEvent();
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- bool __fastcall TTerminalItem::WaitForUserAction(
- TQueueItem::TStatus ItemStatus, TUserAction * UserAction)
- {
- assert(FItem != NULL);
- assert((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;
- }
- //---------------------------------------------------------------------------
- void __fastcall TTerminalItem::Finished()
- {
- TSignalThread::Finished();
- FQueue->TerminalFinished(this);
- }
- //---------------------------------------------------------------------------
- void __fastcall TTerminalItem::TerminalQueryUser(TObject * Sender,
- const AnsiString Query, TStrings * MoreMessages, int Answers,
- const TQueryParams * Params, int & Answer, TQueryType Type, void * Arg)
- {
- // so far query without queue item can occur only for key cofirmation
- // on re-key with non-cached host key. make it fail.
- if (FItem != NULL)
- {
- USEDPARAM(Arg);
- assert(Arg == NULL);
- TQueryUserAction Action;
- 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, AnsiString Name, AnsiString Instructions, TStrings * Prompts,
- TStrings * Results, bool & Result, void * Arg)
- {
- if (FItem == NULL)
- {
- // sanity, should not occur
- assert(false);
- Result = false;
- }
- else
- {
- USEDPARAM(Arg);
- assert(Arg == NULL);
- TPromptUserAction Action;
- 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)
- {
- USEDPARAM(Arg);
- assert(Arg == NULL);
- if ((FItem != NULL) &&
- !E->Message.IsEmpty() &&
- (dynamic_cast<EAbort*>(E) == NULL))
- {
- TShowExtendedExceptionAction Action;
- Action.Terminal = Terminal;
- Action.E = E;
- WaitForUserAction(TQueueItem::qsError, &Action);
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TTerminalItem::OperationFinished(TFileOperation /*Operation*/,
- TOperationSide /*Side*/, bool /*Temp*/, const AnsiString & /*FileName*/,
- bool /*Success*/, bool & /*DisconnectWhenFinished*/)
- {
- // nothing
- }
- //---------------------------------------------------------------------------
- void __fastcall TTerminalItem::OperationProgress(
- TFileOperationProgressType & ProgressData, TCancelStatus & Cancel)
- {
- if (FPause && !FTerminated && !FCancel)
- {
- TQueueItem::TStatus PrevStatus = FItem->GetStatus();
- assert(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.TransferingFile)
- {
- Cancel = csCancelTransfer;
- }
- else
- {
- Cancel = csCancel;
- }
- }
- assert(FItem != NULL);
- FItem->SetProgress(ProgressData);
- }
- //---------------------------------------------------------------------------
- bool __fastcall TTerminalItem::OverrideItemStatus(TQueueItem::TStatus & ItemStatus)
- {
- assert(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();
- }
- //---------------------------------------------------------------------------
- __fastcall TQueueItem::~TQueueItem()
- {
- if (FCompleteEvent != INVALID_HANDLE_VALUE)
- {
- SetEvent(FCompleteEvent);
- }
- delete FSection;
- delete FInfo;
- }
- //---------------------------------------------------------------------------
- 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;
- }
- assert((FQueue != NULL) || (Status == qsPending));
- if (FQueue != NULL)
- {
- FQueue->DoQueueItemUpdate(this);
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TQueueItem::SetProgress(
- TFileOperationProgressType & ProgressData)
- {
- {
- TGuard Guard(FSection);
- assert(FProgressData != NULL);
- *FProgressData = ProgressData;
- FProgressData->Reset();
- if (FCPSLimit >= 0)
- {
- ProgressData.CPSLimit = static_cast<unsigned long>(FCPSLimit);
- FCPSLimit = -1;
- }
- }
- FQueue->DoQueueItemUpdate(this);
- }
- //---------------------------------------------------------------------------
- void __fastcall TQueueItem::GetData(TQueueItemProxy * Proxy)
- {
- TGuard Guard(FSection);
- assert(Proxy->FProgressData != NULL);
- if (FProgressData != NULL)
- {
- *Proxy->FProgressData = *FProgressData;
- }
- else
- {
- Proxy->FProgressData->Clear();
- }
- *Proxy->FInfo = *FInfo;
- Proxy->FStatus = FStatus;
- if (FTerminalItem != NULL)
- {
- FTerminalItem->OverrideItemStatus(Proxy->FStatus);
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TQueueItem::Execute(TTerminalItem * TerminalItem)
- {
- try
- {
- {
- assert(FProgressData == NULL);
- TGuard Guard(FSection);
- FProgressData = new TFileOperationProgressType();
- }
- DoExecute(TerminalItem->FTerminal);
- }
- __finally
- {
- {
- TGuard Guard(FSection);
- delete FProgressData;
- FProgressData = NULL;
- }
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TQueueItem::SetCPSLimit(unsigned long CPSLimit)
- {
- FCPSLimit = static_cast<long>(CPSLimit);
- }
- //---------------------------------------------------------------------------
- // 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;
- }
- //---------------------------------------------------------------------------
- bool __fastcall TQueueItemProxy::Update()
- {
- assert(FQueueItem != NULL);
- TQueueItem::TStatus PrevStatus = Status;
- bool Result = FQueue->ItemGetData(FQueueItem, this);
- if ((FQueueStatus != NULL) && (PrevStatus != Status))
- {
- FQueueStatus->ResetStats();
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- 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(void * Arg)
- {
- assert(FQueueItem != NULL);
- bool Result;
- FProcessingUserAction = true;
- try
- {
- Result = FQueue->ItemProcessUserAction(FQueueItem, Arg);
- }
- __finally
- {
- FProcessingUserAction = false;
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- bool __fastcall TQueueItemProxy::SetCPSLimit(unsigned long CPSLimit)
- {
- return FQueue->ItemSetCPSLimit(FQueueItem, CPSLimit);
- }
- //---------------------------------------------------------------------------
- int __fastcall TQueueItemProxy::GetIndex()
- {
- assert(FQueueStatus != NULL);
- int Index = FQueueStatus->FList->IndexOf(this);
- assert(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;
- }
- //---------------------------------------------------------------------------
- int __fastcall TTerminalQueueStatus::GetActiveCount()
- {
- if (FActiveCount < 0)
- {
- FActiveCount = 0;
- while ((FActiveCount < FList->Count) &&
- (GetItem(FActiveCount)->Status != TQueueItem::qsPending))
- {
- FActiveCount++;
- }
- }
- return FActiveCount;
- }
- //---------------------------------------------------------------------------
- void __fastcall TTerminalQueueStatus::Add(TQueueItemProxy * ItemProxy)
- {
- ItemProxy->FQueueStatus = this;
- FList->Add(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;
- }
- //---------------------------------------------------------------------------
- // TLocatedQueueItem
- //---------------------------------------------------------------------------
- __fastcall TLocatedQueueItem::TLocatedQueueItem(TTerminal * Terminal) :
- TQueueItem()
- {
- assert(Terminal != NULL);
- FCurrentDir = Terminal->CurrentDirectory;
- }
- //---------------------------------------------------------------------------
- AnsiString __fastcall TLocatedQueueItem::StartupDirectory()
- {
- return FCurrentDir;
- }
- //---------------------------------------------------------------------------
- void __fastcall TLocatedQueueItem::DoExecute(TTerminal * Terminal)
- {
- assert(Terminal != NULL);
- Terminal->CurrentDirectory = FCurrentDir;
- }
- //---------------------------------------------------------------------------
- // TTransferQueueItem
- //---------------------------------------------------------------------------
- __fastcall TTransferQueueItem::TTransferQueueItem(TTerminal * Terminal,
- TStrings * FilesToCopy, const AnsiString & TargetDir,
- const TCopyParamType * CopyParam, int Params, TOperationSide Side) :
- TLocatedQueueItem(Terminal), FFilesToCopy(NULL), FCopyParam(NULL)
- {
- FInfo->Operation = (Params & cpDelete ? foMove : foCopy);
- FInfo->Side = Side;
- assert(FilesToCopy != NULL);
- FFilesToCopy = new TStringList();
- for (int Index = 0; Index < FilesToCopy->Count; Index++)
- {
- FFilesToCopy->AddObject(FilesToCopy->Strings[Index],
- ((FilesToCopy->Objects[Index] == NULL) || (Side == osLocal)) ? NULL :
- dynamic_cast<TRemoteFile*>(FilesToCopy->Objects[Index])->Duplicate());
- }
- FTargetDir = TargetDir;
- assert(CopyParam != NULL);
- FCopyParam = new TCopyParamType(*CopyParam);
- FParams = Params;
- }
- //---------------------------------------------------------------------------
- __fastcall TTransferQueueItem::~TTransferQueueItem()
- {
- delete FFilesToCopy;
- delete FCopyParam;
- }
- //---------------------------------------------------------------------------
- // TUploadQueueItem
- //---------------------------------------------------------------------------
- __fastcall TUploadQueueItem::TUploadQueueItem(TTerminal * Terminal,
- TStrings * FilesToCopy, const AnsiString & TargetDir,
- const TCopyParamType * CopyParam, int Params) :
- TTransferQueueItem(Terminal, FilesToCopy, TargetDir, CopyParam, Params, osLocal)
- {
- if (FilesToCopy->Count > 1)
- {
- if (FLAGSET(Params, cpTemporary))
- {
- FInfo->Source = "";
- FInfo->ModifiedLocal = "";
- }
- 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) ? AnsiString() :
- IncludeTrailingBackslash(FInfo->Source);
- }
- }
- else
- {
- if (FLAGSET(Params, cpTemporary))
- {
- FInfo->Source = ExtractFileName(FilesToCopy->Strings[0]);
- FInfo->ModifiedLocal = "";
- }
- else
- {
- assert(FilesToCopy->Count > 0);
- FInfo->Source = FilesToCopy->Strings[0];
- FInfo->ModifiedLocal = FLAGCLEAR(Params, cpDelete) ? AnsiString() :
- IncludeTrailingBackslash(ExtractFilePath(FInfo->Source));
- }
- }
- FInfo->Destination =
- UnixIncludeTrailingBackslash(TargetDir) + CopyParam->FileMask;
- FInfo->ModifiedRemote = UnixIncludeTrailingBackslash(TargetDir);
- }
- //---------------------------------------------------------------------------
- void __fastcall TUploadQueueItem::DoExecute(TTerminal * Terminal)
- {
- TTransferQueueItem::DoExecute(Terminal);
- assert(Terminal != NULL);
- Terminal->CopyToRemote(FFilesToCopy, FTargetDir, FCopyParam, FParams);
- }
- //---------------------------------------------------------------------------
- // TDownloadQueueItem
- //---------------------------------------------------------------------------
- __fastcall TDownloadQueueItem::TDownloadQueueItem(TTerminal * Terminal,
- TStrings * FilesToCopy, const AnsiString & TargetDir,
- const TCopyParamType * CopyParam, int Params) :
- TTransferQueueItem(Terminal, FilesToCopy, TargetDir, CopyParam, Params, osRemote)
- {
- if (FilesToCopy->Count > 1)
- {
- if (!UnixExtractCommonPath(FilesToCopy, FInfo->Source))
- {
- FInfo->Source = Terminal->CurrentDirectory;
- }
- FInfo->Source = UnixExcludeTrailingBackslash(FInfo->Source);
- FInfo->ModifiedRemote = FLAGCLEAR(Params, cpDelete) ? AnsiString() :
- UnixIncludeTrailingBackslash(FInfo->Source);
- }
- else
- {
- assert(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) ? AnsiString() :
- UnixIncludeTrailingBackslash(Terminal->CurrentDirectory);
- }
- else
- {
- FInfo->ModifiedRemote = FLAGCLEAR(Params, cpDelete) ? AnsiString() :
- UnixExtractFilePath(FInfo->Source);
- }
- }
- if (FLAGSET(Params, cpTemporary))
- {
- FInfo->Destination = "";
- }
- else
- {
- FInfo->Destination =
- IncludeTrailingBackslash(TargetDir) + CopyParam->FileMask;
- }
- FInfo->ModifiedLocal = IncludeTrailingBackslash(TargetDir);
- }
- //---------------------------------------------------------------------------
- void __fastcall TDownloadQueueItem::DoExecute(TTerminal * Terminal)
- {
- TTransferQueueItem::DoExecute(Terminal);
- assert(Terminal != NULL);
- Terminal->CopyToLocal(FFilesToCopy, FTargetDir, FCopyParam, FParams);
- }
|