| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546 |
- //---------------------------------------------------------------------------
- #include <vcl.h>
- #pragma hdrstop
- #include "TerminalManager.h"
- #include "CustomScpExplorer.h"
- #include "LogMemo.h"
- #include "NonVisual.h"
- #include "WinConfiguration.h"
- #include "UserInterface.h"
- #include <Log.h>
- #include <OperationStatus.h>
- #include <Common.h>
- #include <ScpMain.h>
- #include <TextsWin.h>
- //---------------------------------------------------------------------------
- #pragma package(smart_init)
- //---------------------------------------------------------------------------
- TTerminalManager * TTerminalManager::FInstance = NULL;
- //---------------------------------------------------------------------------
- TTerminalManager * __fastcall TTerminalManager::Instance()
- {
- if (!FInstance)
- {
- FInstance = new TTerminalManager();
- }
- return FInstance;
- }
- //---------------------------------------------------------------------------
- void __fastcall TTerminalManager::DestroyInstance()
- {
- assert(FInstance);
- SAFE_DESTROY(FInstance);
- }
- //---------------------------------------------------------------------------
- __fastcall TTerminalManager::TTerminalManager() :
- TTerminalList(Configuration)
- {
- FLogMemo = NULL;
- FActiveTerminal = NULL;
- FScpExplorer = NULL;
- FDestroying = false;
- FTerminalPendingAction = tpNull;
- assert(Application && !Application->OnException);
- Application->OnException = ApplicationException;
-
- assert(Configuration && !Configuration->OnChange);
- Configuration->OnChange = ConfigurationChange;
- FOnLastTerminalClosed = NULL;
- FOnTerminalListChanged = NULL;
- FTerminalList = new TStringList();
- }
- //---------------------------------------------------------------------------
- __fastcall TTerminalManager::~TTerminalManager()
- {
- FreeAll();
- assert(!ScpExplorer);
- assert(Configuration->OnChange == ConfigurationChange);
- Configuration->OnChange = NULL;
- assert(Application && (Application->OnException == ApplicationException));
- Application->OnException = NULL;
- delete FTerminalList;
- }
- //---------------------------------------------------------------------------
- TTerminal * __fastcall TTerminalManager::NewTerminal(TSessionData * Data)
- {
- FTerminalList->Clear();
- TTerminal * Terminal = TTerminalList::NewTerminal(Data);
- try
- {
- Terminal->OnQueryUser = TerminalQueryUser;
- if (!ActiveTerminal)
- {
- ActiveTerminal = Terminal;
- }
- }
- catch(...)
- {
- FreeTerminal(Terminal);
- throw;
- }
- if (OnTerminalListChanged)
- {
- OnTerminalListChanged(this);
- }
- return Terminal;
- }
- //---------------------------------------------------------------------------
- void __fastcall TTerminalManager::FreeActiveTerminal()
- {
- if (FTerminalPendingAction == tpNull)
- {
- assert(ActiveTerminal);
- FreeTerminal(ActiveTerminal);
- }
- else
- {
- assert(FTerminalPendingAction == tpNone);
- FTerminalPendingAction = tpFree;
- }
- }
- //---------------------------------------------------------------------------
- bool __fastcall TTerminalManager::ConnectActiveTerminal()
- {
- TTerminalPendingAction Action;
- bool Result;
- do
- {
- Action = tpNull;
- Result = false;
- try
- {
- assert(ActiveTerminal);
- bool ShowLogPending = false;
- if (Configuration->Logging && (WinConfiguration->LogView == lvWindow))
- {
- if (WinConfiguration->LogWindowOnStartup)
- {
- RequireLogForm(LogMemo);
- }
- else
- {
- ShowLogPending = true;
- }
- }
- TOperationStatusForm * Form = new TOperationStatusForm(Application);
- Busy(true);
- try
- {
- Form->SecureShell = ActiveTerminal;
- Form->Show();
- ActiveTerminal->Open();
- ActiveTerminal->DoStartup();
- }
- __finally
- {
- Busy(false);
- delete Form;
- }
- if (ScpExplorer)
- {
- assert(ActiveTerminal->Status == sshReady);
- TerminalReady();
- }
- WinConfiguration->ClearTemporaryLoginData();
- if (LogForm && (WinConfiguration->LogView != lvWindow))
- {
- FreeLogForm();
- }
-
- if (ShowLogPending)
- {
- RequireLogForm(LogMemo);
- }
- Result = true;
- }
- catch(Exception & E)
- {
- assert(FTerminalPendingAction == tpNull);
- FTerminalPendingAction = tpNone;
- try
- {
- ShowExtendedException(&E, this);
- Action = FTerminalPendingAction;
- }
- __finally
- {
- FTerminalPendingAction = tpNull;
- }
- }
- }
- while (Action == tpReconnect);
- if (Action == tpFree)
- {
- FreeActiveTerminal();
- }
- return Result;
- }
- //---------------------------------------------------------------------------
- void __fastcall TTerminalManager::ReconnectActiveTerminal()
- {
- assert(ActiveTerminal);
- TTerminal * Terminal;
- if (ScpExplorer)
- {
- TSessionData * Data = new TSessionData(ActiveTerminal->SessionData->Name);
- try
- {
- Data->Assign(ActiveTerminal->SessionData);
- if (ScpExplorer->Terminal == ActiveTerminal)
- {
- ScpExplorer->UpdateSessionData(Data);
- }
- Terminal = NewTerminal(Data);
- }
- __finally
- {
- delete Data;
- }
- }
- else
- {
- // otherwise the ScpExplorer would be created already
- assert(ActiveTerminal->Status < sshReady);
- Terminal = NewTerminal(ActiveTerminal->SessionData);
- }
- try
- {
- FreeTerminal(ActiveTerminal);
- ActiveTerminal = Terminal;
- if (FTerminalPendingAction == tpNull)
- {
- ConnectActiveTerminal();
- }
- else
- {
- FTerminalPendingAction = tpReconnect;
- }
- }
- catch(...)
- {
- FreeTerminal(Terminal);
- throw;
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TTerminalManager::FreeAll()
- {
- FDestroying = true;
- try
- {
- while (Count)
- {
- FreeTerminal(Terminals[0]);
- }
- }
- __finally
- {
- FDestroying = false;
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TTerminalManager::FreeTerminal(TTerminal * Terminal)
- {
- try
- {
- Terminal->Active = false;
- }
- __finally
- {
- int Index = IndexOf(Terminal);
- FTerminalList->Clear();
- Extract(Terminal);
- if (ActiveTerminal && (Terminal == ActiveTerminal))
- {
- if ((Count > 0) && !FDestroying)
- {
- for (int i = 0; i < Count; i++)
- {
- if (Terminals[i]->Status == sshReady)
- {
- ActiveTerminal = Terminals[i];
- break;
- }
- }
- if (ActiveTerminal == Terminal)
- {
- ActiveTerminal = Terminals[Index];
- }
- }
- else
- {
- ActiveTerminal = NULL;
- }
- }
- else
- {
- SaveTerminal(Terminal);
- }
- delete Terminal;
- if (OnTerminalListChanged)
- {
- OnTerminalListChanged(this);
- }
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TTerminalManager::SetScpExplorer(TCustomScpExplorerForm * value)
- {
- if (ScpExplorer != value)
- {
- // changing explorer is not supported yet
- assert(!ScpExplorer || !value);
- FScpExplorer = value;
- if (FScpExplorer)
- {
- assert(!OnChangeTerminal);
- FScpExplorer->Terminal = ActiveTerminal;
- FOnLastTerminalClosed = FScpExplorer->LastTerminalClosed;
- FOnTerminalListChanged = FScpExplorer->TerminalListChanged;
- }
- else
- {
- FOnLastTerminalClosed = NULL;
- FOnTerminalListChanged = NULL;
- }
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TTerminalManager::SetActiveTerminal(TTerminal * value)
- {
- if (ActiveTerminal != value)
- {
- if (ActiveTerminal && ScpExplorer)
- {
- assert(!ScpExplorer->Terminal || (ScpExplorer->Terminal == ActiveTerminal));
- if (ScpExplorer->Terminal == ActiveTerminal)
- {
- ScpExplorer->UpdateSessionData();
- }
- }
- TTerminal * PActiveTerminal = ActiveTerminal;
- FActiveTerminal = NULL;
- if (OnChangeTerminal)
- {
- OnChangeTerminal(this);
- }
- FActiveTerminal = value;
- if (ActiveTerminal)
- {
- Application->Title = FMTLOAD(APP_CAPTION, (ActiveTerminalTitle, AppName));
- }
- else
- {
- // moved from else block of next if (ActiveTerminal) statement
- // so ScpExplorer can update its caption
- Application->Title = AppName;
- }
- if (ScpExplorer)
- {
- if (ActiveTerminal && (ActiveTerminal->Status == sshReady))
- {
- TerminalReady();
- }
- else
- {
- ScpExplorer->Terminal = NULL;
- }
- }
- if (PActiveTerminal && !PActiveTerminal->Active)
- {
- SaveTerminal(PActiveTerminal);
- }
- if (ActiveTerminal)
- {
- if (!PActiveTerminal)
- {
- CreateLogMemo();
- }
- assert(LogMemo);
- LogMemo->SessionLog = ActiveTerminal->Log;
- }
- else
- {
- if (LogForm)
- {
- FreeLogForm();
- }
- FreeLogMemo();
- if (OnLastTerminalClosed)
- {
- OnLastTerminalClosed(this);
- }
- }
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TTerminalManager::SaveTerminal(TTerminal * Terminal)
- {
- if (!Terminal->SessionData->Name.IsEmpty())
- {
- TSessionData * Data;
- Data = (TSessionData *)StoredSessions->FindByName(Terminal->SessionData->Name);
- if (Data)
- {
- Data->Assign(Terminal->SessionData);
- StoredSessions->Save();
- }
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TTerminalManager::CreateLogMemo()
- {
- assert(!FLogMemo);
- assert(ActiveTerminal);
- FLogMemo = new TLogMemo(Application);
- try
- {
- FLogMemo->SessionLog = ActiveTerminal->Log;
- FLogMemo->PopupMenu = NonVisualDataModule->LogMemoPopup;
- }
- catch (...)
- {
- delete FLogMemo;
- throw;
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TTerminalManager::FreeLogMemo()
- {
- assert(LogMemo);
- LogMemo->PopupMenu = NULL;
- SAFE_DESTROY(FLogMemo);
- }
- //---------------------------------------------------------------------------
- void __fastcall TTerminalManager::ApplicationException(TObject * Sender, Exception * E)
- {
- ShowExtendedException(E, Sender);
- }
- //---------------------------------------------------------------------------
- void __fastcall TTerminalManager::TerminalQueryUser(TObject * /*Sender*/,
- const AnsiString Query, TStrings * MoreMessages, int Answers,
- int Params, int & Answer, TQueryType Type)
- {
- AnsiString AQuery = Query;
- if (Params & qpFatalAbort)
- {
- AQuery = FMTLOAD(WARN_FATAL_ERROR, (AQuery));
- }
- int MessageParams = 0;
- if (Params & qpNeverAskAgainCheck)
- {
- MessageParams |= mpNeverAskAgainCheck;
- }
- if (Params & qpAllowContinueOnError)
- {
- MessageParams |= mpAllowContinueOnError;
- }
- if (ScpExplorer)
- {
- Answer = ScpExplorer->MoreMessageDialog(AQuery, MoreMessages, Type, Answers, 0, MessageParams);
- }
- else
- {
- Answer = MoreMessageDialog(AQuery, MoreMessages, Type, Answers, 0, MessageParams);
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TTerminalManager::ConfigurationChange(TObject * /*Sender*/)
- {
- assert(Configuration);
- assert(Configuration == WinConfiguration);
- if (!Application->Terminated && Configuration->Logging &&
- (WinConfiguration->LogView == lvWindow))
- {
- if (ActiveTerminal)
- {
- RequireLogForm(LogMemo);
- }
- }
- else
- {
- FreeLogForm();
- }
- if (ActiveTerminal)
- {
- assert(ActiveTerminal->Log);
- ActiveTerminal->Log->ReflectSettings();
- }
- if (ScpExplorer)
- {
- ScpExplorer->ConfigurationChanged();
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TTerminalManager::TerminalReady()
- {
- ScpExplorer->Terminal = ActiveTerminal;
- if (OnChangeTerminal)
- {
- OnChangeTerminal(this);
- }
- }
- //---------------------------------------------------------------------------
- TStrings * __fastcall TTerminalManager::GetTerminalList()
- {
- if (FTerminalList->Count != Count)
- {
- for (int i = 0; i < Count; i++)
- {
- AnsiString NameN;
- AnsiString Name = Terminals[i]->SessionData->SessionName;
- int Number = 1;
- NameN = Name;
- while (FTerminalList->IndexOf(NameN) >= 0)
- {
- Number++;
- NameN = FORMAT("%s (%d)", (Name, Number));
- }
- if (Number > 1)
- {
- Name = FORMAT("%s (%d)", (Name, Number));
- }
- FTerminalList->AddObject(Name, Terminals[i]);
- }
- }
- return FTerminalList;
- }
- //---------------------------------------------------------------------------
- int __fastcall TTerminalManager::GetActiveTerminalIndex()
- {
- return ActiveTerminal ? IndexOf(ActiveTerminal) : -1;
- }
- //---------------------------------------------------------------------------
- AnsiString __fastcall TTerminalManager::GetActiveTerminalTitle()
- {
- AnsiString Result = ActiveTerminal ?
- TerminalList->Strings[IndexOf(ActiveTerminal)] : AnsiString("");
- return Result;
- }
|