Queue.cpp 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "Common.h"
  5. #include "Terminal.h"
  6. #include "Queue.h"
  7. //---------------------------------------------------------------------------
  8. #pragma package(smart_init)
  9. //---------------------------------------------------------------------------
  10. class TTerminalItem : public TSignalThread
  11. {
  12. friend class TQueueItem;
  13. public:
  14. __fastcall TTerminalItem(TTerminalQueue * Queue);
  15. __fastcall ~TTerminalItem();
  16. void __fastcall Process(TQueueItem * Item);
  17. bool __fastcall ProcessUserAction(void * Arg);
  18. void __fastcall Cancel();
  19. void __fastcall Idle();
  20. bool __fastcall Pause();
  21. bool __fastcall Resume();
  22. protected:
  23. struct TQueryUserRec
  24. {
  25. TObject * Sender;
  26. AnsiString Query;
  27. TStrings * MoreMessages;
  28. int Answers;
  29. const TQueryParams * Params;
  30. int Answer;
  31. TQueryType Type;
  32. };
  33. struct TPromptUserRec
  34. {
  35. TSecureShell * SecureShell;
  36. AnsiString Prompt;
  37. TPromptKind Kind;
  38. AnsiString Response;
  39. bool Result;
  40. };
  41. struct TShowExtendedExceptionRec
  42. {
  43. TSecureShell * SecureShell;
  44. Exception * E;
  45. };
  46. TTerminalQueue * FQueue;
  47. TTerminal * FTerminal;
  48. TQueueItem * FItem;
  49. TCriticalSection * FCriticalSection;
  50. void * FUserActionParams;
  51. bool FCancel;
  52. bool FPause;
  53. virtual void __fastcall ProcessEvent();
  54. virtual void __fastcall Finished();
  55. bool __fastcall WaitForUserAction(TQueueItem::TStatus ItemStatus, void * Params);
  56. void __fastcall TerminalClose(TObject * Sender);
  57. void __fastcall TerminalQueryUser(TObject * Sender,
  58. const AnsiString Query, TStrings * MoreMessages, int Answers,
  59. const TQueryParams * Params, int & Answer, TQueryType Type, void * Arg);
  60. void __fastcall TerminalPromptUser(TSecureShell * SecureShell,
  61. AnsiString Prompt, TPromptKind Kind, AnsiString & Response, bool & Result, void * Arg);
  62. void __fastcall TerminalShowExtendedException(TSecureShell * SecureShell,
  63. Exception * E, void * Arg);
  64. void __fastcall OperationFinished(TFileOperation Operation, TOperationSide Side,
  65. bool Temp, const AnsiString FileName, bool Success,
  66. bool & DisconnectWhenFinished);
  67. void __fastcall OperationProgress(TFileOperationProgressType & ProgressData,
  68. TCancelStatus & Cancel);
  69. };
  70. //---------------------------------------------------------------------------
  71. // TSignalThread
  72. //---------------------------------------------------------------------------
  73. int __fastcall ThreadProc(void * Thread)
  74. {
  75. TSimpleThread * SimpleThread = reinterpret_cast<TSimpleThread*>(Thread);
  76. assert(SimpleThread != NULL);
  77. try
  78. {
  79. SimpleThread->Execute();
  80. }
  81. __finally
  82. {
  83. SimpleThread->FFinished = true;
  84. SimpleThread->Finished();
  85. EndThread(0);
  86. }
  87. return 0;
  88. }
  89. //---------------------------------------------------------------------------
  90. __fastcall TSimpleThread::TSimpleThread() :
  91. FThread(NULL), FFinished(true)
  92. {
  93. unsigned ThreadID;
  94. FThread = reinterpret_cast<HANDLE>(
  95. BeginThread(NULL, 0, ThreadProc, this, CREATE_SUSPENDED, ThreadID));
  96. }
  97. //---------------------------------------------------------------------------
  98. __fastcall TSimpleThread::~TSimpleThread()
  99. {
  100. Close();
  101. if (FThread != NULL)
  102. {
  103. CloseHandle(FThread);
  104. }
  105. }
  106. //---------------------------------------------------------------------------
  107. void __fastcall TSimpleThread::Start()
  108. {
  109. if (ResumeThread(FThread) == 1)
  110. {
  111. FFinished = false;
  112. }
  113. }
  114. //---------------------------------------------------------------------------
  115. void __fastcall TSimpleThread::Finished()
  116. {
  117. }
  118. //---------------------------------------------------------------------------
  119. void __fastcall TSimpleThread::Close()
  120. {
  121. if (!FFinished)
  122. {
  123. Terminate();
  124. WaitFor();
  125. }
  126. }
  127. //---------------------------------------------------------------------------
  128. void __fastcall TSimpleThread::WaitFor()
  129. {
  130. WaitForSingleObject(FThread, INFINITE);
  131. }
  132. //---------------------------------------------------------------------------
  133. // TSignalThread
  134. //---------------------------------------------------------------------------
  135. __fastcall TSignalThread::TSignalThread() :
  136. TSimpleThread(),
  137. FTerminated(true), FEvent(NULL)
  138. {
  139. FEvent = CreateEvent(NULL, false, false, NULL);
  140. assert(FEvent != NULL);
  141. ::SetThreadPriority(FThread, THREAD_PRIORITY_BELOW_NORMAL);
  142. }
  143. //---------------------------------------------------------------------------
  144. __fastcall TSignalThread::~TSignalThread()
  145. {
  146. // cannot leave closing to TSimpleThread as we need to close it before
  147. // destroying the event
  148. Close();
  149. if (FEvent)
  150. {
  151. CloseHandle(FEvent);
  152. }
  153. }
  154. //---------------------------------------------------------------------------
  155. void __fastcall TSignalThread::Start()
  156. {
  157. FTerminated = false;
  158. TSimpleThread::Start();
  159. }
  160. //---------------------------------------------------------------------------
  161. void __fastcall TSignalThread::TriggerEvent()
  162. {
  163. SetEvent(FEvent);
  164. }
  165. //---------------------------------------------------------------------------
  166. bool __fastcall TSignalThread::WaitForEvent()
  167. {
  168. return (WaitForSingleObject(FEvent, INFINITE) == WAIT_OBJECT_0) &&
  169. !FTerminated;
  170. }
  171. //---------------------------------------------------------------------------
  172. void __fastcall TSignalThread::Execute()
  173. {
  174. while (!FTerminated)
  175. {
  176. if (WaitForEvent())
  177. {
  178. ProcessEvent();
  179. }
  180. }
  181. }
  182. //---------------------------------------------------------------------------
  183. void __fastcall TSignalThread::Terminate()
  184. {
  185. FTerminated = true;
  186. TriggerEvent();
  187. }
  188. //---------------------------------------------------------------------------
  189. // TTerminalQueue
  190. //---------------------------------------------------------------------------
  191. __fastcall TTerminalQueue::TTerminalQueue(TTerminal * Terminal,
  192. TConfiguration * Configuration) :
  193. FTerminal(Terminal), FTransfersLimit(2),
  194. FConfiguration(Configuration), FSessionData(NULL), FItems(NULL),
  195. FTerminals(NULL), FItemsSection(NULL), FFreeTerminals(0),
  196. FItemsInProcess(0), FTemporaryTerminals(0)
  197. {
  198. FOnQueryUser = NULL;
  199. FOnPromptUser = NULL;
  200. FOnShowExtendedException = NULL;
  201. FOnQueueItemUpdate = NULL;
  202. FOnListUpdate = NULL;
  203. FLastIdle = Now();
  204. FIdleInterval = EncodeTime(0, 0, 2, 0);
  205. assert(Terminal != NULL);
  206. FSessionData = new TSessionData("");
  207. FSessionData->Assign(Terminal->SessionData);
  208. FItems = new TList();
  209. FTerminals = new TList();
  210. FItemsSection = new TCriticalSection();
  211. Start();
  212. }
  213. //---------------------------------------------------------------------------
  214. __fastcall TTerminalQueue::~TTerminalQueue()
  215. {
  216. Close();
  217. {
  218. TGuard Guard(FItemsSection);
  219. TTerminalItem * TerminalItem;
  220. while (FTerminals->Count > 0)
  221. {
  222. TerminalItem = reinterpret_cast<TTerminalItem*>(FTerminals->Items[0]);
  223. FTerminals->Delete(0);
  224. TerminalItem->Terminate();
  225. TerminalItem->WaitFor();
  226. delete TerminalItem;
  227. }
  228. delete FTerminals;
  229. for (int Index = 0; Index < FItems->Count; Index++)
  230. {
  231. delete GetItem(Index);
  232. }
  233. }
  234. delete FItemsSection;
  235. }
  236. //---------------------------------------------------------------------------
  237. void __fastcall TTerminalQueue::TerminalFinished(TTerminalItem * TerminalItem)
  238. {
  239. if (!FTerminated)
  240. {
  241. {
  242. TGuard Guard(FItemsSection);
  243. int Index = FTerminals->IndexOf(TerminalItem);
  244. assert(Index >= 0);
  245. if (Index < FFreeTerminals)
  246. {
  247. FFreeTerminals--;
  248. }
  249. // Index may be >= FTransfersLimit also when the transfer limit was
  250. // recently decresed, then
  251. // FTemporaryTerminals < FTerminals->Count - FTransfersLimit
  252. if ((FTransfersLimit > 0) && (Index >= FTransfersLimit) && (FTemporaryTerminals > 0))
  253. {
  254. FTemporaryTerminals--;
  255. }
  256. FTerminals->Extract(TerminalItem);
  257. delete TerminalItem;
  258. }
  259. TriggerEvent();
  260. }
  261. }
  262. //---------------------------------------------------------------------------
  263. bool __fastcall TTerminalQueue::TerminalFree(TTerminalItem * TerminalItem)
  264. {
  265. bool Result = true;
  266. if (!FTerminated)
  267. {
  268. {
  269. TGuard Guard(FItemsSection);
  270. int Index = FTerminals->IndexOf(TerminalItem);
  271. assert(Index >= 0);
  272. assert(Index >= FFreeTerminals);
  273. Result = (FTransfersLimit <= 0) || (Index < FTransfersLimit);
  274. if (Result)
  275. {
  276. FTerminals->Move(Index, 0);
  277. FFreeTerminals++;
  278. }
  279. }
  280. TriggerEvent();
  281. }
  282. return Result;
  283. }
  284. //---------------------------------------------------------------------------
  285. void __fastcall TTerminalQueue::AddItem(TQueueItem * Item)
  286. {
  287. assert(!FTerminated);
  288. Item->SetStatus(TQueueItem::qsPending);
  289. {
  290. TGuard Guard(FItemsSection);
  291. FItems->Add(Item);
  292. Item->FQueue = this;
  293. }
  294. DoListUpdate();
  295. TriggerEvent();
  296. }
  297. //---------------------------------------------------------------------------
  298. void __fastcall TTerminalQueue::RetryItem(TQueueItem * Item)
  299. {
  300. if (!FTerminated)
  301. {
  302. {
  303. TGuard Guard(FItemsSection);
  304. int Index = FItems->Remove(Item);
  305. assert(Index < FItemsInProcess);
  306. USEDPARAM(Index);
  307. FItemsInProcess--;
  308. FItems->Add(Item);
  309. }
  310. DoListUpdate();
  311. TriggerEvent();
  312. }
  313. }
  314. //---------------------------------------------------------------------------
  315. void __fastcall TTerminalQueue::DeleteItem(TQueueItem * Item)
  316. {
  317. if (!FTerminated)
  318. {
  319. {
  320. TGuard Guard(FItemsSection);
  321. int Index = FItems->Remove(Item);
  322. assert(Index < FItemsInProcess);
  323. USEDPARAM(Index);
  324. FItemsInProcess--;
  325. delete Item;
  326. }
  327. DoListUpdate();
  328. }
  329. }
  330. //---------------------------------------------------------------------------
  331. TQueueItem * __fastcall TTerminalQueue::GetItem(int Index)
  332. {
  333. return reinterpret_cast<TQueueItem*>(FItems->Items[Index]);
  334. }
  335. //---------------------------------------------------------------------------
  336. TTerminalQueueStatus * __fastcall TTerminalQueue::CreateStatus(TTerminalQueueStatus * Current)
  337. {
  338. TTerminalQueueStatus * Status = new TTerminalQueueStatus();
  339. try
  340. {
  341. try
  342. {
  343. TGuard Guard(FItemsSection);
  344. TQueueItem * Item;
  345. TQueueItemProxy * ItemProxy;
  346. for (int Index = 0; Index < FItems->Count; Index++)
  347. {
  348. Item = GetItem(Index);
  349. if (Current != NULL)
  350. {
  351. ItemProxy = Current->FindByQueueItem(Item);
  352. }
  353. else
  354. {
  355. ItemProxy = NULL;
  356. }
  357. if (ItemProxy != NULL)
  358. {
  359. Current->Delete(ItemProxy);
  360. Status->Add(ItemProxy);
  361. ItemProxy->Update();
  362. }
  363. else
  364. {
  365. Status->Add(new TQueueItemProxy(this, Item));
  366. }
  367. }
  368. }
  369. __finally
  370. {
  371. if (Current != NULL)
  372. {
  373. delete Current;
  374. }
  375. }
  376. }
  377. catch(...)
  378. {
  379. delete Status;
  380. throw;
  381. }
  382. return Status;
  383. }
  384. //---------------------------------------------------------------------------
  385. bool __fastcall TTerminalQueue::ItemGetData(TQueueItem * Item,
  386. TQueueItemProxy * Proxy)
  387. {
  388. // to prevent deadlocks when closing queue from other thread
  389. bool Result = !FFinished;
  390. if (Result)
  391. {
  392. TGuard Guard(FItemsSection);
  393. Result = (FItems->IndexOf(Item) >= 0);
  394. if (Result)
  395. {
  396. Item->GetData(Proxy);
  397. }
  398. }
  399. return Result;
  400. }
  401. //---------------------------------------------------------------------------
  402. bool __fastcall TTerminalQueue::ItemProcessUserAction(TQueueItem * Item, void * Arg)
  403. {
  404. // to prevent deadlocks when closing queue from other thread
  405. bool Result = !FFinished;
  406. if (Result)
  407. {
  408. TTerminalItem * TerminalItem;
  409. {
  410. TGuard Guard(FItemsSection);
  411. Result = (FItems->IndexOf(Item) >= 0) &&
  412. TQueueItem::IsUserActionStatus(Item->Status);
  413. if (Result)
  414. {
  415. TerminalItem = Item->FTerminalItem;
  416. }
  417. }
  418. if (Result)
  419. {
  420. Result = TerminalItem->ProcessUserAction(Arg);
  421. }
  422. }
  423. return Result;
  424. }
  425. //---------------------------------------------------------------------------
  426. bool __fastcall TTerminalQueue::ItemMove(TQueueItem * Item, TQueueItem * BeforeItem)
  427. {
  428. // to prevent deadlocks when closing queue from other thread
  429. bool Result = !FFinished;
  430. if (Result)
  431. {
  432. {
  433. TGuard Guard(FItemsSection);
  434. int Index = FItems->IndexOf(Item);
  435. int IndexDest = FItems->IndexOf(BeforeItem);
  436. Result = (Index >= 0) && (IndexDest >= 0) &&
  437. (Item->GetStatus() == TQueueItem::qsPending) &&
  438. (BeforeItem->GetStatus() == TQueueItem::qsPending);
  439. if (Result)
  440. {
  441. FItems->Move(Index, IndexDest);
  442. }
  443. }
  444. if (Result)
  445. {
  446. DoListUpdate();
  447. TriggerEvent();
  448. }
  449. }
  450. return Result;
  451. }
  452. //---------------------------------------------------------------------------
  453. bool __fastcall TTerminalQueue::ItemExecuteNow(TQueueItem * Item)
  454. {
  455. // to prevent deadlocks when closing queue from other thread
  456. bool Result = !FFinished;
  457. if (Result)
  458. {
  459. {
  460. TGuard Guard(FItemsSection);
  461. int Index = FItems->IndexOf(Item);
  462. Result = (Index >= 0) && (Item->GetStatus() == TQueueItem::qsPending) &&
  463. // prevent double-initiation when "execute" is clicked twice too fast
  464. (Index >= FItemsInProcess);
  465. if (Result)
  466. {
  467. if (Index > FItemsInProcess)
  468. {
  469. FItems->Move(Index, FItemsInProcess);
  470. }
  471. if ((FTransfersLimit > 0) && (FTerminals->Count >= FTransfersLimit))
  472. {
  473. FTemporaryTerminals++;
  474. }
  475. }
  476. }
  477. if (Result)
  478. {
  479. DoListUpdate();
  480. TriggerEvent();
  481. }
  482. }
  483. return Result;
  484. }
  485. //---------------------------------------------------------------------------
  486. bool __fastcall TTerminalQueue::ItemDelete(TQueueItem * Item)
  487. {
  488. // to prevent deadlocks when closing queue from other thread
  489. bool Result = !FFinished;
  490. if (Result)
  491. {
  492. bool UpdateList = false;
  493. {
  494. TGuard Guard(FItemsSection);
  495. int Index = FItems->IndexOf(Item);
  496. Result = (Index >= 0);
  497. if (Result)
  498. {
  499. if (Item->Status == TQueueItem::qsPending)
  500. {
  501. FItems->Delete(Index);
  502. UpdateList = true;
  503. }
  504. else
  505. {
  506. Item->FTerminalItem->Cancel();
  507. }
  508. }
  509. }
  510. if (UpdateList)
  511. {
  512. DoListUpdate();
  513. TriggerEvent();
  514. }
  515. }
  516. return Result;
  517. }
  518. //---------------------------------------------------------------------------
  519. bool __fastcall TTerminalQueue::ItemPause(TQueueItem * Item, bool Pause)
  520. {
  521. // to prevent deadlocks when closing queue from other thread
  522. bool Result = !FFinished;
  523. if (Result)
  524. {
  525. TTerminalItem * TerminalItem;
  526. {
  527. TGuard Guard(FItemsSection);
  528. Result = (FItems->IndexOf(Item) >= 0) &&
  529. ((Pause && (Item->Status == TQueueItem::qsProcessing)) ||
  530. (!Pause && (Item->Status == TQueueItem::qsPaused)));
  531. if (Result)
  532. {
  533. TerminalItem = Item->FTerminalItem;
  534. }
  535. }
  536. if (Result)
  537. {
  538. if (Pause)
  539. {
  540. Result = TerminalItem->Pause();
  541. }
  542. else
  543. {
  544. Result = TerminalItem->Resume();
  545. }
  546. }
  547. }
  548. return Result;
  549. }
  550. //---------------------------------------------------------------------------
  551. void __fastcall TTerminalQueue::Idle()
  552. {
  553. if (Now() - FLastIdle > FIdleInterval)
  554. {
  555. FLastIdle = FIdleInterval;
  556. TTerminalItem * TerminalItem = NULL;
  557. if (FFreeTerminals > 0)
  558. {
  559. TGuard Guard(FItemsSection);
  560. if (FFreeTerminals > 0)
  561. {
  562. // take the last free terminal, because TerminalFree() puts it to the
  563. // front, this ensures we cycle thru all free terminals
  564. TerminalItem = reinterpret_cast<TTerminalItem*>(FTerminals->Items[FFreeTerminals - 1]);
  565. FTerminals->Move(FFreeTerminals - 1, FTerminals->Count - 1);
  566. FFreeTerminals--;
  567. }
  568. }
  569. if (TerminalItem != NULL)
  570. {
  571. TerminalItem->Idle();
  572. }
  573. }
  574. }
  575. //---------------------------------------------------------------------------
  576. void __fastcall TTerminalQueue::ProcessEvent()
  577. {
  578. TTerminalItem * TerminalItem;
  579. TQueueItem * Item;
  580. do
  581. {
  582. TerminalItem = NULL;
  583. Item = NULL;
  584. if (FItems->Count > FItemsInProcess)
  585. {
  586. TGuard Guard(FItemsSection);
  587. if ((FFreeTerminals == 0) &&
  588. ((FTransfersLimit <= 0) ||
  589. (FTerminals->Count < FTransfersLimit + FTemporaryTerminals)))
  590. {
  591. TerminalItem = new TTerminalItem(this);
  592. FTerminals->Add(TerminalItem);
  593. }
  594. else if (FFreeTerminals > 0)
  595. {
  596. TerminalItem = reinterpret_cast<TTerminalItem*>(FTerminals->Items[0]);
  597. FTerminals->Move(0, FTerminals->Count - 1);
  598. FFreeTerminals--;
  599. }
  600. if (TerminalItem != NULL)
  601. {
  602. Item = GetItem(FItemsInProcess);
  603. FItemsInProcess++;
  604. }
  605. }
  606. if (TerminalItem != NULL)
  607. {
  608. TerminalItem->Process(Item);
  609. }
  610. }
  611. while (!FTerminated && (TerminalItem != NULL));
  612. }
  613. //---------------------------------------------------------------------------
  614. void __fastcall TTerminalQueue::DoQueueItemUpdate(TQueueItem * Item)
  615. {
  616. if (OnQueueItemUpdate != NULL)
  617. {
  618. OnQueueItemUpdate(this, Item);
  619. }
  620. }
  621. //---------------------------------------------------------------------------
  622. void __fastcall TTerminalQueue::DoListUpdate()
  623. {
  624. if (OnListUpdate != NULL)
  625. {
  626. OnListUpdate(this);
  627. }
  628. }
  629. //---------------------------------------------------------------------------
  630. void __fastcall TTerminalQueue::DoQueryUser(TObject * Sender,
  631. const AnsiString Query, TStrings * MoreMessages, int Answers,
  632. const TQueryParams * Params, int & Answer, TQueryType Type, void * Arg)
  633. {
  634. if (OnQueryUser != NULL)
  635. {
  636. OnQueryUser(Sender, Query, MoreMessages, Answers, Params, Answer, Type, Arg);
  637. }
  638. }
  639. //---------------------------------------------------------------------------
  640. void __fastcall TTerminalQueue::DoPromptUser(TSecureShell * SecureShell,
  641. AnsiString Prompt, TPromptKind Kind, AnsiString & Response, bool & Result, void * Arg)
  642. {
  643. if (OnPromptUser != NULL)
  644. {
  645. OnPromptUser(SecureShell, Prompt, Kind, Response, Result, Arg);
  646. }
  647. }
  648. //---------------------------------------------------------------------------
  649. void __fastcall TTerminalQueue::DoShowExtendedException(
  650. TSecureShell * SecureShell, Exception * E, void * Arg)
  651. {
  652. if (OnShowExtendedException != NULL)
  653. {
  654. OnShowExtendedException(SecureShell, E, Arg);
  655. }
  656. }
  657. //---------------------------------------------------------------------------
  658. void __fastcall TTerminalQueue::SetTransfersLimit(int value)
  659. {
  660. if (FTransfersLimit != value)
  661. {
  662. {
  663. TGuard Guard(FItemsSection);
  664. if ((value > 0) && (value < FItemsInProcess))
  665. {
  666. FTemporaryTerminals = (FItemsInProcess - value);
  667. }
  668. else
  669. {
  670. FTemporaryTerminals = 0;
  671. }
  672. FTransfersLimit = value;
  673. }
  674. TriggerEvent();
  675. }
  676. }
  677. //---------------------------------------------------------------------------
  678. bool __fastcall TTerminalQueue::GetIsEmpty()
  679. {
  680. TGuard Guard(FItemsSection);
  681. return (FItems->Count == 0);
  682. }
  683. //---------------------------------------------------------------------------
  684. // TTerminalItem
  685. //---------------------------------------------------------------------------
  686. __fastcall TTerminalItem::TTerminalItem(TTerminalQueue * Queue) :
  687. TSignalThread(), FQueue(Queue), FTerminal(NULL), FItem(NULL),
  688. FCriticalSection(NULL), FUserActionParams(NULL)
  689. {
  690. FCriticalSection = new TCriticalSection();
  691. FTerminal = new TSecondaryTerminal(FQueue->FTerminal);
  692. try
  693. {
  694. FTerminal->UseBusyCursor = false;
  695. FTerminal->Configuration = Queue->FConfiguration;
  696. FTerminal->SessionData = Queue->FSessionData;
  697. FTerminal->OnClose = TerminalClose;
  698. FTerminal->OnQueryUser = TerminalQueryUser;
  699. FTerminal->OnPromptUser = TerminalPromptUser;
  700. FTerminal->OnShowExtendedException = TerminalShowExtendedException;
  701. FTerminal->OnProgress = OperationProgress;
  702. FTerminal->OnFinished = OperationFinished;
  703. }
  704. catch(...)
  705. {
  706. delete FTerminal;
  707. throw;
  708. }
  709. Start();
  710. }
  711. //---------------------------------------------------------------------------
  712. __fastcall TTerminalItem::~TTerminalItem()
  713. {
  714. Close();
  715. assert(FItem == NULL);
  716. delete FTerminal;
  717. delete FCriticalSection;
  718. }
  719. //---------------------------------------------------------------------------
  720. void __fastcall TTerminalItem::Process(TQueueItem * Item)
  721. {
  722. {
  723. TGuard Guard(FCriticalSection);
  724. assert(FItem == NULL);
  725. FItem = Item;
  726. }
  727. TriggerEvent();
  728. }
  729. //---------------------------------------------------------------------------
  730. void __fastcall TTerminalItem::ProcessEvent()
  731. {
  732. TGuard Guard(FCriticalSection);
  733. bool Retry = true;
  734. FCancel = false;
  735. FPause = false;
  736. FItem->FTerminalItem = this;
  737. try
  738. {
  739. assert(FItem != NULL);
  740. if (!FTerminal->Active)
  741. {
  742. FItem->SetStatus(TQueueItem::qsConnecting);
  743. FTerminal->SessionData->RemoteDirectory = FItem->StartupDirectory();
  744. FTerminal->Open();
  745. FTerminal->DoStartup();
  746. }
  747. Retry = false;
  748. if (!FCancel)
  749. {
  750. FItem->SetStatus(TQueueItem::qsProcessing);
  751. FItem->Execute(this);
  752. }
  753. }
  754. catch(Exception & E)
  755. {
  756. FTerminal->DoShowExtendedException(&E);
  757. }
  758. FItem->SetStatus(TQueueItem::qsDone);
  759. FItem->FTerminalItem = NULL;
  760. TQueueItem * Item = FItem;
  761. FItem = NULL;
  762. if (Retry && !FCancel)
  763. {
  764. FQueue->RetryItem(Item);
  765. }
  766. else
  767. {
  768. FQueue->DeleteItem(Item);
  769. }
  770. if (!FTerminal->Active ||
  771. !FQueue->TerminalFree(this))
  772. {
  773. Terminate();
  774. }
  775. }
  776. //---------------------------------------------------------------------------
  777. void __fastcall TTerminalItem::Idle()
  778. {
  779. TGuard Guard(FCriticalSection);
  780. assert(FTerminal->Active);
  781. try
  782. {
  783. FTerminal->Idle();
  784. }
  785. catch(...)
  786. {
  787. }
  788. if (!FTerminal->Active ||
  789. !FQueue->TerminalFree(this))
  790. {
  791. Terminate();
  792. }
  793. }
  794. //---------------------------------------------------------------------------
  795. void __fastcall TTerminalItem::Cancel()
  796. {
  797. FCancel = true;
  798. if (FItem->GetStatus() == TQueueItem::qsPaused)
  799. {
  800. TriggerEvent();
  801. }
  802. }
  803. //---------------------------------------------------------------------------
  804. bool __fastcall TTerminalItem::Pause()
  805. {
  806. assert(FItem != NULL);
  807. bool Result = (FItem->GetStatus() == TQueueItem::qsProcessing) && !FPause;
  808. if (Result)
  809. {
  810. FPause = true;
  811. }
  812. return Result;
  813. }
  814. //---------------------------------------------------------------------------
  815. bool __fastcall TTerminalItem::Resume()
  816. {
  817. assert(FItem != NULL);
  818. bool Result = (FItem->GetStatus() == TQueueItem::qsPaused);
  819. if (Result)
  820. {
  821. TriggerEvent();
  822. }
  823. return Result;
  824. }
  825. //---------------------------------------------------------------------------
  826. bool __fastcall TTerminalItem::ProcessUserAction(void * Arg)
  827. {
  828. // When status is changed twice quickly, the controller when responding
  829. // to the first change (non-user-action) can be so slow to check only after
  830. // the second (user-action) change occurs. Thus it responds it.
  831. // Then as reaction to the second (user-action) change there will not be
  832. // any outstanding user-action.
  833. bool Result = (FUserActionParams != NULL);
  834. if (Result)
  835. {
  836. assert(FItem != NULL);
  837. if (FItem->GetStatus() == TQueueItem::qsQuery)
  838. {
  839. TQueryUserRec * Params;
  840. Params = reinterpret_cast<TQueryUserRec *>(FUserActionParams);
  841. FQueue->DoQueryUser(Params->Sender, Params->Query, Params->MoreMessages,
  842. Params->Answers, Params->Params, Params->Answer, Params->Type, Arg);
  843. }
  844. else if (FItem->GetStatus() == TQueueItem::qsPrompt)
  845. {
  846. TPromptUserRec * Params;
  847. Params = reinterpret_cast<TPromptUserRec *>(FUserActionParams);
  848. FQueue->DoPromptUser(Params->SecureShell, Params->Prompt,
  849. Params->Kind, Params->Response, Params->Result, Arg);
  850. }
  851. else if (FItem->GetStatus() == TQueueItem::qsError)
  852. {
  853. TShowExtendedExceptionRec * Params;
  854. Params = reinterpret_cast<TShowExtendedExceptionRec *>(FUserActionParams);
  855. FQueue->DoShowExtendedException(Params->SecureShell, Params->E, Arg);
  856. }
  857. else
  858. {
  859. assert(false);
  860. }
  861. FUserActionParams = NULL;
  862. TriggerEvent();
  863. }
  864. return Result;
  865. }
  866. //---------------------------------------------------------------------------
  867. bool __fastcall TTerminalItem::WaitForUserAction(
  868. TQueueItem::TStatus ItemStatus, void * Params)
  869. {
  870. assert(FItem != NULL);
  871. assert((FItem->GetStatus() == TQueueItem::qsProcessing) ||
  872. (FItem->GetStatus() == TQueueItem::qsConnecting));
  873. bool Result;
  874. TQueueItem::TStatus PrevStatus = FItem->GetStatus();
  875. try
  876. {
  877. FUserActionParams = Params;
  878. FItem->SetStatus(ItemStatus);
  879. Result = !FTerminated && WaitForEvent() && !FCancel;
  880. }
  881. __finally
  882. {
  883. FUserActionParams = NULL;
  884. FItem->SetStatus(PrevStatus);
  885. }
  886. return Result;
  887. }
  888. //---------------------------------------------------------------------------
  889. void __fastcall TTerminalItem::Finished()
  890. {
  891. TSignalThread::Finished();
  892. FQueue->TerminalFinished(this);
  893. }
  894. //---------------------------------------------------------------------------
  895. void __fastcall TTerminalItem::TerminalClose(TObject * /*Sender*/)
  896. {
  897. // nothing
  898. }
  899. //---------------------------------------------------------------------------
  900. void __fastcall TTerminalItem::TerminalQueryUser(TObject * Sender,
  901. const AnsiString Query, TStrings * MoreMessages, int Answers,
  902. const TQueryParams * Params, int & Answer, TQueryType Type, void * Arg)
  903. {
  904. USEDPARAM(Arg);
  905. assert(Arg == NULL);
  906. TQueryUserRec QueryUserRec;
  907. QueryUserRec.Sender = Sender;
  908. QueryUserRec.Query = Query;
  909. QueryUserRec.MoreMessages = MoreMessages;
  910. QueryUserRec.Answers = Answers;
  911. QueryUserRec.Params = Params;
  912. QueryUserRec.Answer = Answer;
  913. QueryUserRec.Type = Type;
  914. if (WaitForUserAction(TQueueItem::qsQuery, &QueryUserRec))
  915. {
  916. Answer = QueryUserRec.Answer;
  917. }
  918. }
  919. //---------------------------------------------------------------------------
  920. void __fastcall TTerminalItem::TerminalPromptUser(TSecureShell * SecureShell,
  921. AnsiString Prompt, TPromptKind Kind, AnsiString & Response, bool & Result,
  922. void * Arg)
  923. {
  924. USEDPARAM(Arg);
  925. assert(Arg == NULL);
  926. TPromptUserRec PromptUserRec;
  927. PromptUserRec.SecureShell = SecureShell;
  928. PromptUserRec.Prompt = Prompt;
  929. PromptUserRec.Kind = Kind;
  930. PromptUserRec.Response = Response.c_str();
  931. PromptUserRec.Result = Result;
  932. if (WaitForUserAction(TQueueItem::qsPrompt, &PromptUserRec))
  933. {
  934. Response = PromptUserRec.Response.c_str();
  935. Result = PromptUserRec.Result;
  936. }
  937. }
  938. //---------------------------------------------------------------------------
  939. void __fastcall TTerminalItem::TerminalShowExtendedException(
  940. TSecureShell * SecureShell, Exception * E, void * Arg)
  941. {
  942. USEDPARAM(Arg);
  943. assert(Arg == NULL);
  944. if (!E->Message.IsEmpty() &&
  945. (dynamic_cast<EAbort*>(E) == NULL))
  946. {
  947. TShowExtendedExceptionRec ShowExtendedExceptionRec;
  948. ShowExtendedExceptionRec.SecureShell = SecureShell;
  949. ShowExtendedExceptionRec.E = E;
  950. WaitForUserAction(TQueueItem::qsError, &ShowExtendedExceptionRec);
  951. }
  952. }
  953. //---------------------------------------------------------------------------
  954. void __fastcall TTerminalItem::OperationFinished(TFileOperation /*Operation*/,
  955. TOperationSide /*Side*/, bool /*Temp*/, const AnsiString /*FileName*/,
  956. bool /*Success*/, bool & /*DisconnectWhenFinished*/)
  957. {
  958. // nothing
  959. }
  960. //---------------------------------------------------------------------------
  961. void __fastcall TTerminalItem::OperationProgress(
  962. TFileOperationProgressType & ProgressData, TCancelStatus & Cancel)
  963. {
  964. if (FPause && !FTerminated && !FCancel)
  965. {
  966. TQueueItem::TStatus PrevStatus = FItem->GetStatus();
  967. assert(PrevStatus == TQueueItem::qsProcessing);
  968. // must be set before TFileOperationProgressType::Suspend(), because
  969. // it invokes this method back
  970. FPause = false;
  971. ProgressData.Suspend();
  972. try
  973. {
  974. FItem->SetStatus(TQueueItem::qsPaused);
  975. WaitForEvent();
  976. }
  977. __finally
  978. {
  979. FItem->SetStatus(PrevStatus);
  980. ProgressData.Resume();
  981. }
  982. }
  983. if (FTerminated || FCancel)
  984. {
  985. if (ProgressData.TransferingFile)
  986. {
  987. Cancel = csCancelTransfer;
  988. }
  989. else
  990. {
  991. Cancel = csCancel;
  992. }
  993. }
  994. assert(FItem != NULL);
  995. FItem->SetProgress(ProgressData);
  996. }
  997. //---------------------------------------------------------------------------
  998. // TQueueItem
  999. //---------------------------------------------------------------------------
  1000. __fastcall TQueueItem::TQueueItem() :
  1001. FStatus(qsPending), FTerminalItem(NULL), FSection(NULL), FProgressData(NULL),
  1002. FQueue(NULL), FInfo(NULL), FCompleteEvent(INVALID_HANDLE_VALUE)
  1003. {
  1004. FSection = new TCriticalSection();
  1005. FInfo = new TInfo();
  1006. }
  1007. //---------------------------------------------------------------------------
  1008. __fastcall TQueueItem::~TQueueItem()
  1009. {
  1010. if (FCompleteEvent != INVALID_HANDLE_VALUE)
  1011. {
  1012. SetEvent(FCompleteEvent);
  1013. }
  1014. delete FSection;
  1015. delete FInfo;
  1016. }
  1017. //---------------------------------------------------------------------------
  1018. bool __fastcall TQueueItem::IsUserActionStatus(TStatus Status)
  1019. {
  1020. return (Status == qsQuery) || (Status == qsError) || (Status == qsPrompt);
  1021. }
  1022. //---------------------------------------------------------------------------
  1023. TQueueItem::TStatus __fastcall TQueueItem::GetStatus()
  1024. {
  1025. TGuard Guard(FSection);
  1026. return FStatus;
  1027. }
  1028. //---------------------------------------------------------------------------
  1029. void __fastcall TQueueItem::SetStatus(TStatus Status)
  1030. {
  1031. {
  1032. TGuard Guard(FSection);
  1033. FStatus = Status;
  1034. }
  1035. assert((FQueue != NULL) || (Status == qsPending));
  1036. if (FQueue != NULL)
  1037. {
  1038. FQueue->DoQueueItemUpdate(this);
  1039. }
  1040. }
  1041. //---------------------------------------------------------------------------
  1042. void __fastcall TQueueItem::SetProgress(
  1043. const TFileOperationProgressType & ProgressData)
  1044. {
  1045. {
  1046. TGuard Guard(FSection);
  1047. assert(FProgressData != NULL);
  1048. *FProgressData = ProgressData;
  1049. FProgressData->Reset();
  1050. }
  1051. FQueue->DoQueueItemUpdate(this);
  1052. }
  1053. //---------------------------------------------------------------------------
  1054. void __fastcall TQueueItem::GetData(TQueueItemProxy * Proxy)
  1055. {
  1056. TGuard Guard(FSection);
  1057. assert(Proxy->FProgressData != NULL);
  1058. if (FProgressData != NULL)
  1059. {
  1060. *Proxy->FProgressData = *FProgressData;
  1061. }
  1062. else
  1063. {
  1064. Proxy->FProgressData->Clear();
  1065. }
  1066. *Proxy->FInfo = *FInfo;
  1067. Proxy->FStatus = FStatus;
  1068. }
  1069. //---------------------------------------------------------------------------
  1070. void __fastcall TQueueItem::Execute(TTerminalItem * TerminalItem)
  1071. {
  1072. try
  1073. {
  1074. {
  1075. assert(FProgressData == NULL);
  1076. TGuard Guard(FSection);
  1077. FProgressData = new TFileOperationProgressType();
  1078. }
  1079. DoExecute(TerminalItem->FTerminal);
  1080. }
  1081. __finally
  1082. {
  1083. {
  1084. TGuard Guard(FSection);
  1085. delete FProgressData;
  1086. FProgressData = NULL;
  1087. }
  1088. }
  1089. }
  1090. //---------------------------------------------------------------------------
  1091. // TQueueItemProxy
  1092. //---------------------------------------------------------------------------
  1093. __fastcall TQueueItemProxy::TQueueItemProxy(TTerminalQueue * Queue,
  1094. TQueueItem * QueueItem) :
  1095. FQueue(Queue), FQueueItem(QueueItem), FProgressData(NULL),
  1096. FQueueStatus(NULL), FInfo(NULL),
  1097. FProcessingUserAction(false), FUserData(NULL)
  1098. {
  1099. FProgressData = new TFileOperationProgressType();
  1100. FInfo = new TQueueItem::TInfo();
  1101. Update();
  1102. }
  1103. //---------------------------------------------------------------------------
  1104. __fastcall TQueueItemProxy::~TQueueItemProxy()
  1105. {
  1106. delete FProgressData;
  1107. delete FInfo;
  1108. }
  1109. //---------------------------------------------------------------------------
  1110. TFileOperationProgressType * __fastcall TQueueItemProxy::GetProgressData()
  1111. {
  1112. return (FProgressData->Operation == foNone) ? NULL : FProgressData;
  1113. }
  1114. //---------------------------------------------------------------------------
  1115. bool __fastcall TQueueItemProxy::Update()
  1116. {
  1117. assert(FQueueItem != NULL);
  1118. TQueueItem::TStatus PrevStatus = Status;
  1119. bool Result = FQueue->ItemGetData(FQueueItem, this);
  1120. if ((FQueueStatus != NULL) && (PrevStatus != Status))
  1121. {
  1122. FQueueStatus->ResetStats();
  1123. }
  1124. return Result;
  1125. }
  1126. //---------------------------------------------------------------------------
  1127. bool __fastcall TQueueItemProxy::ExecuteNow()
  1128. {
  1129. return FQueue->ItemExecuteNow(FQueueItem);
  1130. }
  1131. //---------------------------------------------------------------------------
  1132. bool __fastcall TQueueItemProxy::Move(bool Sooner)
  1133. {
  1134. bool Result = false;
  1135. int I = Index;
  1136. if (Sooner)
  1137. {
  1138. if (I > 0)
  1139. {
  1140. Result = Move(FQueueStatus->Items[I - 1]);
  1141. }
  1142. }
  1143. else
  1144. {
  1145. if (I < FQueueStatus->Count - 1)
  1146. {
  1147. Result = FQueueStatus->Items[I + 1]->Move(this);
  1148. }
  1149. }
  1150. return Result;
  1151. }
  1152. //---------------------------------------------------------------------------
  1153. bool __fastcall TQueueItemProxy::Move(TQueueItemProxy * BeforeItem)
  1154. {
  1155. return FQueue->ItemMove(FQueueItem, BeforeItem->FQueueItem);
  1156. }
  1157. //---------------------------------------------------------------------------
  1158. bool __fastcall TQueueItemProxy::Delete()
  1159. {
  1160. return FQueue->ItemDelete(FQueueItem);
  1161. }
  1162. //---------------------------------------------------------------------------
  1163. bool __fastcall TQueueItemProxy::Pause()
  1164. {
  1165. return FQueue->ItemPause(FQueueItem, true);
  1166. }
  1167. //---------------------------------------------------------------------------
  1168. bool __fastcall TQueueItemProxy::Resume()
  1169. {
  1170. return FQueue->ItemPause(FQueueItem, false);
  1171. }
  1172. //---------------------------------------------------------------------------
  1173. bool __fastcall TQueueItemProxy::ProcessUserAction(void * Arg)
  1174. {
  1175. assert(FQueueItem != NULL);
  1176. bool Result;
  1177. FProcessingUserAction = true;
  1178. try
  1179. {
  1180. Result = FQueue->ItemProcessUserAction(FQueueItem, Arg);
  1181. }
  1182. __finally
  1183. {
  1184. FProcessingUserAction = false;
  1185. }
  1186. return Result;
  1187. }
  1188. //---------------------------------------------------------------------------
  1189. int __fastcall TQueueItemProxy::GetIndex()
  1190. {
  1191. assert(FQueueStatus != NULL);
  1192. int Index = FQueueStatus->FList->IndexOf(this);
  1193. assert(Index >= 0);
  1194. return Index;
  1195. }
  1196. //---------------------------------------------------------------------------
  1197. // TTerminalQueueStatus
  1198. //---------------------------------------------------------------------------
  1199. __fastcall TTerminalQueueStatus::TTerminalQueueStatus() :
  1200. FList(NULL)
  1201. {
  1202. FList = new TList();
  1203. ResetStats();
  1204. }
  1205. //---------------------------------------------------------------------------
  1206. __fastcall TTerminalQueueStatus::~TTerminalQueueStatus()
  1207. {
  1208. for (int Index = 0; Index < FList->Count; Index++)
  1209. {
  1210. delete GetItem(Index);
  1211. }
  1212. delete FList;
  1213. FList = NULL;
  1214. }
  1215. //---------------------------------------------------------------------------
  1216. void __fastcall TTerminalQueueStatus::ResetStats()
  1217. {
  1218. FActiveCount = -1;
  1219. }
  1220. //---------------------------------------------------------------------------
  1221. int __fastcall TTerminalQueueStatus::GetActiveCount()
  1222. {
  1223. if (FActiveCount < 0)
  1224. {
  1225. FActiveCount = 0;
  1226. while ((FActiveCount < FList->Count) &&
  1227. (GetItem(FActiveCount)->Status != TQueueItem::qsPending))
  1228. {
  1229. FActiveCount++;
  1230. }
  1231. }
  1232. return FActiveCount;
  1233. }
  1234. //---------------------------------------------------------------------------
  1235. void __fastcall TTerminalQueueStatus::Add(TQueueItemProxy * ItemProxy)
  1236. {
  1237. ItemProxy->FQueueStatus = this;
  1238. FList->Add(ItemProxy);
  1239. ResetStats();
  1240. }
  1241. //---------------------------------------------------------------------------
  1242. void __fastcall TTerminalQueueStatus::Delete(TQueueItemProxy * ItemProxy)
  1243. {
  1244. FList->Extract(ItemProxy);
  1245. ItemProxy->FQueueStatus = NULL;
  1246. ResetStats();
  1247. }
  1248. //---------------------------------------------------------------------------
  1249. int __fastcall TTerminalQueueStatus::GetCount()
  1250. {
  1251. return FList->Count;
  1252. }
  1253. //---------------------------------------------------------------------------
  1254. TQueueItemProxy * __fastcall TTerminalQueueStatus::GetItem(int Index)
  1255. {
  1256. return reinterpret_cast<TQueueItemProxy *>(FList->Items[Index]);
  1257. }
  1258. //---------------------------------------------------------------------------
  1259. TQueueItemProxy * __fastcall TTerminalQueueStatus::FindByQueueItem(
  1260. TQueueItem * QueueItem)
  1261. {
  1262. TQueueItemProxy * Item;
  1263. for (int Index = 0; Index < FList->Count; Index++)
  1264. {
  1265. Item = GetItem(Index);
  1266. if (Item->FQueueItem == QueueItem)
  1267. {
  1268. return Item;
  1269. }
  1270. }
  1271. return NULL;
  1272. }
  1273. //---------------------------------------------------------------------------
  1274. // TLocatedQueueItem
  1275. //---------------------------------------------------------------------------
  1276. __fastcall TLocatedQueueItem::TLocatedQueueItem(TTerminal * Terminal) :
  1277. TQueueItem()
  1278. {
  1279. assert(Terminal != NULL);
  1280. FCurrentDir = Terminal->CurrentDirectory;
  1281. }
  1282. //---------------------------------------------------------------------------
  1283. AnsiString __fastcall TLocatedQueueItem::StartupDirectory()
  1284. {
  1285. return FCurrentDir;
  1286. }
  1287. //---------------------------------------------------------------------------
  1288. void __fastcall TLocatedQueueItem::DoExecute(TTerminal * Terminal)
  1289. {
  1290. assert(Terminal != NULL);
  1291. Terminal->CurrentDirectory = FCurrentDir;
  1292. }
  1293. //---------------------------------------------------------------------------
  1294. // TTransferQueueItem
  1295. //---------------------------------------------------------------------------
  1296. __fastcall TTransferQueueItem::TTransferQueueItem(TTerminal * Terminal,
  1297. TStrings * FilesToCopy, const AnsiString & TargetDir,
  1298. const TCopyParamType * CopyParam, int Params, TOperationSide Side) :
  1299. TLocatedQueueItem(Terminal), FFilesToCopy(NULL), FCopyParam(NULL)
  1300. {
  1301. FInfo->Operation = (Params & cpDelete ? foMove : foCopy);
  1302. FInfo->Side = Side;
  1303. assert(FilesToCopy != NULL);
  1304. FFilesToCopy = new TStringList();
  1305. for (int Index = 0; Index < FilesToCopy->Count; Index++)
  1306. {
  1307. FFilesToCopy->AddObject(FilesToCopy->Strings[Index],
  1308. ((FilesToCopy->Objects[Index] == NULL) || (Side == osLocal)) ? NULL :
  1309. dynamic_cast<TRemoteFile*>(FilesToCopy->Objects[Index])->Duplicate());
  1310. }
  1311. FTargetDir = TargetDir;
  1312. assert(CopyParam != NULL);
  1313. FCopyParam = new TCopyParamType(*CopyParam);
  1314. FParams = Params;
  1315. }
  1316. //---------------------------------------------------------------------------
  1317. __fastcall TTransferQueueItem::~TTransferQueueItem()
  1318. {
  1319. delete FFilesToCopy;
  1320. delete FCopyParam;
  1321. }
  1322. //---------------------------------------------------------------------------
  1323. // TUploadQueueItem
  1324. //---------------------------------------------------------------------------
  1325. __fastcall TUploadQueueItem::TUploadQueueItem(TTerminal * Terminal,
  1326. TStrings * FilesToCopy, const AnsiString & TargetDir,
  1327. const TCopyParamType * CopyParam, int Params) :
  1328. TTransferQueueItem(Terminal, FilesToCopy, TargetDir, CopyParam, Params, osLocal)
  1329. {
  1330. if (FilesToCopy->Count > 1)
  1331. {
  1332. if (FLAGSET(Params, cpTemporary))
  1333. {
  1334. FInfo->Source = "";
  1335. FInfo->ModifiedLocal = "";
  1336. }
  1337. else
  1338. {
  1339. ExtractCommonPath(FilesToCopy, FInfo->Source);
  1340. // this way the trailing backslash is preserved for root directories like D:\\
  1341. FInfo->Source = ExtractFileDir(IncludeTrailingBackslash(FInfo->Source));
  1342. FInfo->ModifiedLocal = FLAGCLEAR(Params, cpDelete) ? AnsiString() :
  1343. IncludeTrailingBackslash(FInfo->Source);
  1344. }
  1345. }
  1346. else
  1347. {
  1348. if (FLAGSET(Params, cpTemporary))
  1349. {
  1350. FInfo->Source = ExtractFileName(FilesToCopy->Strings[0]);
  1351. FInfo->ModifiedLocal = "";
  1352. }
  1353. else
  1354. {
  1355. assert(FilesToCopy->Count > 0);
  1356. FInfo->Source = FilesToCopy->Strings[0];
  1357. FInfo->ModifiedLocal = FLAGCLEAR(Params, cpDelete) ? AnsiString() :
  1358. IncludeTrailingBackslash(ExtractFilePath(FInfo->Source));
  1359. }
  1360. }
  1361. FInfo->Destination =
  1362. UnixIncludeTrailingBackslash(TargetDir) + CopyParam->FileMask;
  1363. FInfo->ModifiedRemote = UnixIncludeTrailingBackslash(TargetDir);
  1364. }
  1365. //---------------------------------------------------------------------------
  1366. void __fastcall TUploadQueueItem::DoExecute(TTerminal * Terminal)
  1367. {
  1368. TTransferQueueItem::DoExecute(Terminal);
  1369. assert(Terminal != NULL);
  1370. Terminal->CopyToRemote(FFilesToCopy, FTargetDir, FCopyParam, FParams);
  1371. }
  1372. //---------------------------------------------------------------------------
  1373. // TDownloadQueueItem
  1374. //---------------------------------------------------------------------------
  1375. __fastcall TDownloadQueueItem::TDownloadQueueItem(TTerminal * Terminal,
  1376. TStrings * FilesToCopy, const AnsiString & TargetDir,
  1377. const TCopyParamType * CopyParam, int Params) :
  1378. TTransferQueueItem(Terminal, FilesToCopy, TargetDir, CopyParam, Params, osRemote)
  1379. {
  1380. if (FilesToCopy->Count > 1)
  1381. {
  1382. if (!UnixExtractCommonPath(FilesToCopy, FInfo->Source))
  1383. {
  1384. FInfo->Source = Terminal->CurrentDirectory;
  1385. }
  1386. FInfo->Source = UnixExcludeTrailingBackslash(FInfo->Source);
  1387. FInfo->ModifiedRemote = FLAGCLEAR(Params, cpDelete) ? AnsiString() :
  1388. UnixIncludeTrailingBackslash(FInfo->Source);
  1389. }
  1390. else
  1391. {
  1392. assert(FilesToCopy->Count > 0);
  1393. FInfo->Source = FilesToCopy->Strings[0];
  1394. if (UnixExtractFilePath(FInfo->Source).IsEmpty())
  1395. {
  1396. FInfo->Source = UnixIncludeTrailingBackslash(Terminal->CurrentDirectory) +
  1397. FInfo->Source;
  1398. FInfo->ModifiedRemote = FLAGCLEAR(Params, cpDelete) ? AnsiString() :
  1399. UnixIncludeTrailingBackslash(Terminal->CurrentDirectory);
  1400. }
  1401. else
  1402. {
  1403. FInfo->ModifiedRemote = FLAGCLEAR(Params, cpDelete) ? AnsiString() :
  1404. UnixExtractFilePath(FInfo->Source);
  1405. }
  1406. }
  1407. if (FLAGSET(Params, cpTemporary))
  1408. {
  1409. FInfo->Destination = "";
  1410. }
  1411. else
  1412. {
  1413. FInfo->Destination =
  1414. IncludeTrailingBackslash(TargetDir) + CopyParam->FileMask;
  1415. }
  1416. FInfo->ModifiedLocal = IncludeTrailingBackslash(TargetDir);
  1417. }
  1418. //---------------------------------------------------------------------------
  1419. void __fastcall TDownloadQueueItem::DoExecute(TTerminal * Terminal)
  1420. {
  1421. TTransferQueueItem::DoExecute(Terminal);
  1422. assert(Terminal != NULL);
  1423. Terminal->CopyToLocal(FFilesToCopy, FTargetDir, FCopyParam, FParams);
  1424. }