Login.cpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050
  1. //---------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include <ScpMain.h>
  5. #include <Common.h>
  6. #include <TextsWin.h>
  7. #include <VCLCommon.h>
  8. #include "Login.h"
  9. #include "WinInterface.h"
  10. #include "UserInterface.h"
  11. #include "Tools.h"
  12. #include "WinConfiguration.h"
  13. //---------------------------------------------------------------------
  14. #pragma link "ComboEdit"
  15. #pragma link "LogSettings"
  16. #pragma link "GeneralSettings"
  17. #pragma link "UpDownEdit"
  18. #pragma link "XPGroupBox"
  19. #pragma link "PasswordEdit"
  20. #pragma resource "*.dfm"
  21. //---------------------------------------------------------------------------
  22. bool __fastcall DoLoginDialog(TStoredSessionList *SessionList,
  23. TSessionData * Data, bool Initial)
  24. {
  25. assert(Data);
  26. TLoginDialog *LoginDialog = new TLoginDialog(Application);
  27. bool Result;
  28. try
  29. {
  30. LoginDialog->StoredSessions = SessionList;
  31. LoginDialog->SessionData = Data;
  32. LoginDialog->Initial = Initial;
  33. Result = LoginDialog->Execute();
  34. if (Result)
  35. {
  36. Data->Assign(LoginDialog->SessionData);
  37. };
  38. }
  39. __finally
  40. {
  41. delete LoginDialog;
  42. }
  43. return Result;
  44. }
  45. //---------------------------------------------------------------------
  46. __fastcall TLoginDialog::TLoginDialog(TComponent* AOwner)
  47. : TForm(AOwner)
  48. {
  49. FSessionData = new TSessionData("");
  50. NoUpdate = 0;
  51. FLanguagesPopupMenu = NULL;
  52. FInitialized = false;
  53. FSavedTab = NULL;
  54. FSavedSession = -1;
  55. FInitial = true;
  56. FLocaleChanging = false;
  57. }
  58. //---------------------------------------------------------------------
  59. __fastcall TLoginDialog::~TLoginDialog()
  60. {
  61. LoggingFrame->OnGetDefaultLogFileName = NULL;
  62. // SelectItem event is called after destructor! Why?
  63. SessionListView->Selected = NULL;
  64. delete FSessionData;
  65. delete FLanguagesPopupMenu;
  66. }
  67. //---------------------------------------------------------------------
  68. void __fastcall TLoginDialog::ShowTabs(bool Show)
  69. {
  70. for (int Index = 0; Index < PageControl->PageCount; Index++)
  71. {
  72. PageControl->Pages[Index]->TabVisible = Show;
  73. }
  74. // change form height by height of hidden tabs
  75. ClientHeight += (Show ? 1 : -1) * 50;
  76. }
  77. //---------------------------------------------------------------------
  78. void __fastcall TLoginDialog::Init()
  79. {
  80. LoggingFrame->OnGetDefaultLogFileName = LoggingGetDefaultLogFileName;
  81. UseSystemSettings(this, &FSystemSettings);
  82. Caption = FORMAT("%s %s", (AppName, Caption));
  83. InitializeBugsCombo(BugIgnore1Combo);
  84. InitializeBugsCombo(BugPlainPW1Combo);
  85. InitializeBugsCombo(BugRSA1Combo);
  86. InitializeBugsCombo(BugHMAC2Combo);
  87. InitializeBugsCombo(BugDeriveKey2Combo);
  88. InitializeBugsCombo(BugRSAPad2Combo);
  89. InitializeBugsCombo(BugDHGEx2Combo);
  90. InitializeBugsCombo(BugPKSessID2Combo);
  91. PrepareNavigationTree(SimpleNavigationTree);
  92. PrepareNavigationTree(AdvancedNavigationTree);
  93. if (!WinConfiguration->ExpertMode)
  94. {
  95. Label9->Visible = false;
  96. LocalDirectoryEdit->Visible = false;
  97. Label16->Visible = false;
  98. int PrevHeight = DirectoriesGroup->Height;
  99. DirectoriesGroup->Height = RemoteDirectoryEdit->Top + RemoteDirectoryEdit->Height + 12;
  100. EOLTypeGroup->Top = EOLTypeGroup->Top - PrevHeight + RemoteDirectoryEdit->Height;
  101. }
  102. ShowTabs(false);
  103. if (StoredSessions->Count && StoredSessions &&
  104. (FSessionData->Name == StoredSessions->DefaultSettings->Name))
  105. {
  106. ChangePage(SessionListSheet);
  107. SessionListView->SetFocus();
  108. }
  109. else
  110. {
  111. ChangePage(BasicSheet);
  112. HostNameEdit->SetFocus();
  113. }
  114. UpdateControls();
  115. }
  116. //---------------------------------------------------------------------
  117. void __fastcall TLoginDialog::InitializeBugsCombo(TComboBox * BugsCombo)
  118. {
  119. BugsCombo->Clear();
  120. BugsCombo->Items->Add(LoadStr(LOGIN_BUG_AUTO));
  121. BugsCombo->Items->Add(LoadStr(LOGIN_BUG_OFF));
  122. BugsCombo->Items->Add(LoadStr(LOGIN_BUG_ON));
  123. }
  124. //---------------------------------------------------------------------
  125. void __fastcall TLoginDialog::LoadSessions()
  126. {
  127. SessionListView->Items->BeginUpdate();
  128. try
  129. {
  130. SessionListView->Items->Clear();
  131. if (StoredSessions)
  132. {
  133. for (int Index = 0; Index < StoredSessions->Count; Index++)
  134. {
  135. TListItem *Item;
  136. Item = SessionListView->Items->Add();
  137. LoadSessionItem(Item);
  138. }
  139. }
  140. }
  141. __finally
  142. {
  143. SessionListView->Items->EndUpdate();
  144. }
  145. SelectedSession = NULL;
  146. UpdateControls();
  147. }
  148. //---------------------------------------------------------------------------
  149. void __fastcall TLoginDialog::Default()
  150. {
  151. if (StoredSessions)
  152. {
  153. FSessionData->Assign(StoredSessions->DefaultSettings);
  154. }
  155. else
  156. {
  157. FSessionData->Default();
  158. }
  159. LoadSession(FSessionData);
  160. }
  161. //---------------------------------------------------------------------
  162. void __fastcall TLoginDialog::LoadSession(TSessionData * aSessionData)
  163. {
  164. NoUpdate++;
  165. try
  166. {
  167. // Basic tab
  168. UserNameEdit->Text = aSessionData->UserName;
  169. PortNumberEdit->AsInteger = aSessionData->PortNumber;
  170. HostNameEdit->Text = aSessionData->HostName;
  171. PasswordEdit->Text = aSessionData->Password;
  172. PrivateKeyEdit->Text = aSessionData->PublicKeyFile;
  173. switch (aSessionData->FSProtocol) {
  174. case fsSCPonly: SCPonlyButton->Checked = true; break;
  175. case fsSFTP: SFTPButton->Checked = true; break;
  176. case fsSFTPonly:
  177. default: SFTPonlyButton->Checked = true; break;
  178. }
  179. // Environment tab
  180. LocalDirectoryEdit->Text = aSessionData->LocalDirectory;
  181. RemoteDirectoryEdit->Text = aSessionData->RemoteDirectory;
  182. UpdateDirectoriesCheck->Checked = aSessionData->UpdateDirectories;
  183. CacheDirectoriesCheck->Checked = aSessionData->CacheDirectories;
  184. ResolveSymlinksCheck->Checked = aSessionData->ResolveSymlinks;
  185. if (aSessionData->EOLType == eolLF)
  186. {
  187. EOLTypeLFButton->Checked = true;
  188. }
  189. else
  190. {
  191. EOLTypeCRLFButton->Checked = true;
  192. }
  193. AuthTISCheck->Checked = aSessionData->AuthTIS;
  194. AuthKICheck->Checked = aSessionData->AuthKI;
  195. AgentFwdCheck->Checked = aSessionData->AgentFwd;
  196. Ssh2LegacyDESCheck->Checked = aSessionData->Ssh2DES;
  197. // SSH tab
  198. CompressionCheck->Checked = aSessionData->Compression;
  199. switch (aSessionData->SshProt) {
  200. case ssh1only: SshProt1onlyButton->Checked = true; break;
  201. case ssh1: SshProt1Button->Checked = true; break;
  202. case ssh2: SshProt2Button->Checked = true; break;
  203. case ssh2only: SshProt2onlyButton->Checked = true; break;
  204. }
  205. CipherListBox->Items->Clear();
  206. assert(CIPHER_NAME_WARN+CIPHER_COUNT-1 == CIPHER_NAME_DES);
  207. for (int Index = 0; Index < CIPHER_COUNT; Index++)
  208. {
  209. CipherListBox->Items->AddObject(
  210. LoadStr(CIPHER_NAME_WARN+aSessionData->Cipher[Index]),
  211. (TObject*)aSessionData->Cipher[Index]);
  212. }
  213. // Connection tab
  214. PingIntervalCheck->Checked = (aSessionData->PingInterval > 0);
  215. if (aSessionData->PingEnabled)
  216. PingIntervalSecEdit->AsInteger = aSessionData->PingInterval;
  217. else
  218. PingIntervalSecEdit->AsInteger = 60;
  219. TimeoutEdit->AsInteger = aSessionData->Timeout;
  220. // Shell tab
  221. if (aSessionData->DefaultShell)
  222. DefaultShellButton->Checked = true;
  223. else
  224. ShellEnterButton->Checked = true;
  225. ShellEdit->Text = aSessionData->Shell;
  226. if (aSessionData->DetectReturnVar)
  227. ReturnVarAutodetectButton->Checked = true;
  228. else
  229. ReturnVarEnterButton->Checked = true;
  230. ReturnVarEdit->Text = aSessionData->ReturnVar;
  231. LookupUserGroupsCheck->Checked = aSessionData->LookupUserGroups;
  232. ClearAliasesCheck->Checked = aSessionData->ClearAliases;
  233. IgnoreLsWarningsCheck->Checked = aSessionData->IgnoreLsWarnings;
  234. Scp1CompatibilityCheck->Checked = aSessionData->Scp1Compatibility;
  235. UnsetNationalVarsCheck->Checked = aSessionData->UnsetNationalVars;
  236. AliasGroupListCheck->Checked = aSessionData->AliasGroupList;
  237. TimeDifferenceEdit->AsInteger = double(aSessionData->TimeDifference) * 24;
  238. // Proxy tab
  239. switch (aSessionData->ProxyMethod) {
  240. case pmHTTP: ProxyHTTPButton->Checked = true; break;
  241. case pmSocks4: ProxySocks4Button->Checked = true; break;
  242. case pmSocks5: ProxySocks5Button->Checked = true; break;
  243. case pmTelnet: ProxyTelnetButton->Checked = true; break;
  244. default: ProxyNoneButton->Checked = true; break;
  245. }
  246. ProxyHostEdit->Text = aSessionData->ProxyHost;
  247. ProxyPortEdit->AsInteger = aSessionData->ProxyPort;
  248. ProxyUsernameEdit->Text = aSessionData->ProxyUsername;
  249. ProxyPasswordEdit->Text = aSessionData->ProxyPassword;
  250. ProxyTelnetCommandEdit->Text = aSessionData->ProxyTelnetCommand;
  251. ProxyLocalhostCheck->Checked = aSessionData->ProxyLocalhost;
  252. switch (aSessionData->ProxyDNS) {
  253. case asOn: ProxyDNSOnButton->Checked = true; break;
  254. case asOff: ProxyDNSOffButton->Checked = true; break;
  255. default: ProxyDNSAutoButton->Checked = true; break;
  256. }
  257. // Bugs tab
  258. #define LOAD_BUG_COMBO(BUG) Bug ## BUG ## Combo->ItemIndex = 2 - aSessionData->Bug[sb ## BUG]
  259. LOAD_BUG_COMBO(Ignore1);
  260. LOAD_BUG_COMBO(PlainPW1);
  261. LOAD_BUG_COMBO(RSA1);
  262. LOAD_BUG_COMBO(HMAC2);
  263. LOAD_BUG_COMBO(DeriveKey2);
  264. LOAD_BUG_COMBO(RSAPad2);
  265. LOAD_BUG_COMBO(DHGEx2);
  266. LOAD_BUG_COMBO(PKSessID2);
  267. #undef LOAD_BUG_COMBO
  268. }
  269. __finally
  270. {
  271. NoUpdate--;
  272. UpdateControls();
  273. }
  274. }
  275. //---------------------------------------------------------------------
  276. void __fastcall TLoginDialog::SaveSession(TSessionData * aSessionData)
  277. {
  278. aSessionData->Name = "";
  279. // Basic tab
  280. aSessionData->UserName = UserNameEdit->Text;
  281. aSessionData->PortNumber = PortNumberEdit->AsInteger;
  282. // must be loaded after UserName, because HostName may be in format user@host
  283. aSessionData->HostName = HostNameEdit->Text;
  284. aSessionData->Password = PasswordEdit->Text;
  285. aSessionData->PublicKeyFile = PrivateKeyEdit->Text;
  286. if (SCPonlyButton->Checked) aSessionData->FSProtocol = fsSCPonly;
  287. else
  288. if (SFTPButton->Checked) aSessionData->FSProtocol = fsSFTP;
  289. else aSessionData->FSProtocol = fsSFTPonly;
  290. // SSH tab
  291. aSessionData->Compression = CompressionCheck->Checked;
  292. aSessionData->Ssh2DES = Ssh2LegacyDESCheck->Checked;
  293. if (SshProt1onlyButton->Checked) aSessionData->SshProt = ssh1only;
  294. else
  295. if (SshProt1Button->Checked) aSessionData->SshProt = ssh1;
  296. else
  297. if (SshProt2Button->Checked) aSessionData->SshProt = ssh2;
  298. else aSessionData->SshProt = ssh2only;
  299. for (int Index = 0; Index < CIPHER_COUNT; Index++)
  300. {
  301. aSessionData->Cipher[Index] = (TCipher)CipherListBox->Items->Objects[Index];
  302. }
  303. // Authentication tab
  304. aSessionData->AuthTIS = AuthTISCheck->Checked;
  305. aSessionData->AuthKI = AuthKICheck->Checked;
  306. aSessionData->AgentFwd = AgentFwdCheck->Checked;
  307. // Connection tab
  308. aSessionData->PingEnabled = PingIntervalCheck->Checked;
  309. if (PingIntervalCheck->Checked)
  310. aSessionData->PingInterval = PingIntervalSecEdit->AsInteger;
  311. aSessionData->Timeout = TimeoutEdit->AsInteger;
  312. // Environment tab
  313. aSessionData->LocalDirectory = LocalDirectoryEdit->Text;
  314. aSessionData->RemoteDirectory = RemoteDirectoryEdit->Text;
  315. aSessionData->UpdateDirectories = UpdateDirectoriesCheck->Checked;
  316. aSessionData->CacheDirectories = CacheDirectoriesCheck->Checked;
  317. aSessionData->ResolveSymlinks = ResolveSymlinksCheck->Checked;
  318. // Shell tab
  319. aSessionData->DefaultShell = DefaultShellButton->Checked;
  320. if (ShellEnterButton->Checked)
  321. aSessionData->Shell = ShellEdit->Text;
  322. aSessionData->DetectReturnVar = ReturnVarAutodetectButton->Checked;
  323. if (ReturnVarEnterButton->Checked)
  324. aSessionData->ReturnVar = ReturnVarEdit->Text;
  325. aSessionData->LookupUserGroups = LookupUserGroupsCheck->Checked;
  326. aSessionData->ClearAliases = ClearAliasesCheck->Checked;
  327. aSessionData->IgnoreLsWarnings = IgnoreLsWarningsCheck->Checked;
  328. aSessionData->Scp1Compatibility = Scp1CompatibilityCheck->Checked;
  329. if (EOLTypeLFButton->Checked) aSessionData->EOLType = eolLF;
  330. else aSessionData->EOLType = eolCRLF;
  331. aSessionData->UnsetNationalVars = UnsetNationalVarsCheck->Checked;
  332. aSessionData->AliasGroupList = AliasGroupListCheck->Checked;
  333. aSessionData->TimeDifference = double(TimeDifferenceEdit->AsInteger) / 24;
  334. // Proxy tab
  335. if (ProxyHTTPButton->Checked) aSessionData->ProxyMethod = pmHTTP;
  336. else
  337. if (ProxySocks4Button->Checked) aSessionData->ProxyMethod = pmSocks4;
  338. else
  339. if (ProxySocks5Button->Checked) aSessionData->ProxyMethod = pmSocks5;
  340. else
  341. if (ProxyTelnetButton->Checked) aSessionData->ProxyMethod = pmTelnet;
  342. else aSessionData->ProxyMethod = pmNone;
  343. aSessionData->ProxyHost = ProxyHostEdit->Text;
  344. aSessionData->ProxyPort = ProxyPortEdit->AsInteger;
  345. aSessionData->ProxyUsername = ProxyUsernameEdit->Text;
  346. aSessionData->ProxyPassword = ProxyPasswordEdit->Text;
  347. aSessionData->ProxyTelnetCommand = ProxyTelnetCommandEdit->Text;
  348. aSessionData->ProxyLocalhost = ProxyLocalhostCheck->Checked;
  349. if (ProxyDNSOnButton->Checked) aSessionData->ProxyDNS = asOn;
  350. else
  351. if (ProxyDNSOffButton->Checked) aSessionData->ProxyDNS = asOff;
  352. else aSessionData->ProxyDNS = asAuto;
  353. // Bugs tab
  354. #define SAVE_BUG_COMBO(BUG) aSessionData->Bug[sb ## BUG] = (TAutoSwitch)(2 - Bug ## BUG ## Combo->ItemIndex);
  355. SAVE_BUG_COMBO(Ignore1);
  356. SAVE_BUG_COMBO(PlainPW1);
  357. SAVE_BUG_COMBO(RSA1);
  358. SAVE_BUG_COMBO(HMAC2);
  359. SAVE_BUG_COMBO(DeriveKey2);
  360. SAVE_BUG_COMBO(RSAPad2);
  361. SAVE_BUG_COMBO(DHGEx2);
  362. SAVE_BUG_COMBO(PKSessID2);
  363. #undef SAVE_BUG_COMBO
  364. }
  365. //---------------------------------------------------------------------
  366. void __fastcall TLoginDialog::UpdateControls()
  367. {
  368. if (Visible)
  369. {
  370. NoUpdate++;
  371. try
  372. {
  373. #define SHOW_NAVIGATION(TREE, SHOW) if ((TREE)->Visible != (SHOW)) { \
  374. (TREE)->Visible = (SHOW); PageControlChange(PageControl); }
  375. SHOW_NAVIGATION(SimpleNavigationTree, !ShowAdvancedLoginOptionsCheck->Checked);
  376. SHOW_NAVIGATION(AdvancedNavigationTree, ShowAdvancedLoginOptionsCheck->Checked);
  377. #undef SHOW_NAVIGATION
  378. EnableControl(ShellIconsButton, SessionListView->Selected);
  379. if (!PrivateKeyEdit->Text.IsEmpty())
  380. {
  381. PasswordEdit->Clear();
  382. }
  383. EnableControl(PasswordEdit, PrivateKeyEdit->Text.IsEmpty());
  384. if (!PasswordEdit->Text.IsEmpty()) PrivateKeyEdit->Clear();
  385. EnableControl(PrivateKeyEdit, PasswordEdit->Text.IsEmpty());
  386. EnableControl(PingIntervalSecEdit, PingIntervalCheck->Checked);
  387. EnableControl(SessionListView, SessionListView->Items->Count);
  388. AdjustListColumnsWidth(SessionListView);
  389. SessionListView->Columns->Items[0]->Width -= 2;
  390. EnableControl(AuthTISCheck, !SshProt2onlyButton->Checked);
  391. EnableControl(AuthKICheck, !SshProt1onlyButton->Checked);
  392. EnableControl(CipherUpButton, CipherListBox->ItemIndex > 0);
  393. EnableControl(CipherDownButton, CipherListBox->ItemIndex >= 0 &&
  394. CipherListBox->ItemIndex < CipherListBox->Items->Count-1);
  395. EnableControl(Ssh2LegacyDESCheck, !SshProt1onlyButton->Checked);
  396. EnableControl(BugIgnore1Combo, !SshProt2onlyButton->Checked);
  397. EnableControl(BugPlainPW1Combo, !SshProt2onlyButton->Checked);
  398. EnableControl(BugRSA1Combo, !SshProt2onlyButton->Checked);
  399. EnableControl(BugHMAC2Combo, !SshProt1onlyButton->Checked);
  400. EnableControl(BugDeriveKey2Combo, !SshProt1onlyButton->Checked);
  401. EnableControl(BugRSAPad2Combo, !SshProt1onlyButton->Checked);
  402. EnableControl(BugDHGEx2Combo, !SshProt1onlyButton->Checked);
  403. EnableControl(ShellEdit, ShellEnterButton->Checked);
  404. EnableControl(ReturnVarEdit, ReturnVarEnterButton->Checked);
  405. EnableControl(ProxyHostEdit, !ProxyNoneButton->Checked);
  406. EnableControl(ProxyPortEdit, !ProxyNoneButton->Checked);
  407. EnableControl(ProxyUsernameEdit, !ProxyNoneButton->Checked);
  408. EnableControl(ProxyPasswordEdit, !ProxyNoneButton->Checked);
  409. EnableControl(ProxySettingsGroup, !ProxyNoneButton->Checked);
  410. EnableControl(ProxyTelnetCommandEdit, ProxyTelnetButton->Checked);
  411. }
  412. __finally
  413. {
  414. NoUpdate--;
  415. }
  416. }
  417. }
  418. //---------------------------------------------------------------------------
  419. void __fastcall TLoginDialog::DataChange(TObject * /*Sender*/)
  420. {
  421. if (!NoUpdate) UpdateControls();
  422. }
  423. //---------------------------------------------------------------------------
  424. void __fastcall TLoginDialog::PrepareNavigationTree(TTreeView * Tree)
  425. {
  426. Tree->FullExpand();
  427. int i = 0;
  428. while (i < Tree->Items->Count)
  429. {
  430. if ((!WinConfiguration->ExpertMode &&
  431. Tree->Items->Item[i]->SelectedIndex & 128) ||
  432. (!Initial &&
  433. Tree->Items->Item[i]->SelectedIndex & 256))
  434. {
  435. Tree->Items->Delete(Tree->Items->Item[i]);
  436. }
  437. else
  438. {
  439. for (int pi = 0; pi < PageControl->PageCount; pi++)
  440. {
  441. if (PageControl->Pages[pi]->Tag == (Tree->Items->Item[i]->SelectedIndex & 127))
  442. {
  443. Tree->Items->Item[i]->Text = PageControl->Pages[pi]->Hint;
  444. break;
  445. }
  446. }
  447. i++;
  448. }
  449. }
  450. ToolsMenuButton->Visible = WinConfiguration->ExpertMode;
  451. }
  452. //---------------------------------------------------------------------------
  453. void __fastcall TLoginDialog::FormShow(TObject * /*Sender*/)
  454. {
  455. if (!FInitialized)
  456. {
  457. FInitialized = true;
  458. Init();
  459. Default();
  460. }
  461. if (FLocaleChanging)
  462. {
  463. Init();
  464. LoadSession(FSessionData);
  465. ChangePage(FSavedTab);
  466. SessionListView->ItemIndex = FSavedSession;
  467. LoadConfiguration();
  468. }
  469. }
  470. //---------------------------------------------------------------------------
  471. void __fastcall TLoginDialog::SessionListViewSelectItem(TObject * /*Sender*/,
  472. TListItem * /*Item*/, bool /*Selected*/)
  473. {
  474. UpdateControls();
  475. }
  476. //---------------------------------------------------------------------------
  477. void __fastcall TLoginDialog::StoreSessions()
  478. {
  479. StoredSessions->Save();
  480. }
  481. //---------------------------------------------------------------------------
  482. void __fastcall TLoginDialog::SetSessionData(TSessionData * value)
  483. {
  484. FSessionData->Assign(value);
  485. FSessionData->Special = false;
  486. LoadSession(FSessionData);
  487. }
  488. //---------------------------------------------------------------------------
  489. TSessionData * __fastcall TLoginDialog::GetSessionData()
  490. {
  491. if (PageControl->ActivePage == SessionListSheet)
  492. {
  493. return SelectedSession;
  494. }
  495. else
  496. {
  497. SaveSession(FSessionData);
  498. return FSessionData;
  499. }
  500. }
  501. //---------------------------------------------------------------------------
  502. void __fastcall TLoginDialog::SetStoredSessions(TStoredSessionList * value)
  503. {
  504. if (FStoredSessions != value)
  505. {
  506. FStoredSessions = value;
  507. LoadSessions();
  508. }
  509. }
  510. //---------------------------------------------------------------------------
  511. void __fastcall TLoginDialog::LoadSessionItem(TListItem * Item)
  512. {
  513. Item->Data = StoredSessions->AtObject(Item->Index);
  514. Item->Caption = ((TSessionData*)Item->Data)->Name;
  515. }
  516. //---------------------------------------------------------------------------
  517. void __fastcall TLoginDialog::SessionListViewDblClick(TObject * /*Sender*/)
  518. {
  519. if (SelectedSession)
  520. {
  521. if (SelectedSession->CanLogin) ModalResult = mrOk;
  522. else
  523. {
  524. SessionData = SelectedSession;
  525. ChangePage(BasicSheet);
  526. if (HostNameEdit->Text.IsEmpty()) HostNameEdit->SetFocus();
  527. else
  528. if (UserNameEdit->Text.IsEmpty()) UserNameEdit->SetFocus();
  529. }
  530. }
  531. }
  532. //---------------------------------------------------------------------------
  533. void __fastcall TLoginDialog::SetSelectedSession(TSessionData * value)
  534. {
  535. if (value)
  536. {
  537. int Index = StoredSessions->IndexOf(value);
  538. if (Index >= 0)
  539. {
  540. TListItem *Item = SessionListView->Items->Item[Index];
  541. Item->Focused = true;
  542. Item->Selected = true;
  543. }
  544. }
  545. else
  546. {
  547. SessionListView->Selected = NULL;
  548. }
  549. }
  550. //---------------------------------------------------------------------------
  551. TSessionData * __fastcall TLoginDialog::GetSelectedSession()
  552. {
  553. if (SessionListView->Selected)
  554. return (TSessionData *)SessionListView->Selected->Data;
  555. else
  556. return NULL;
  557. }
  558. //---------------------------------------------------------------------------
  559. void __fastcall TLoginDialog::SessionListViewInfoTip(TObject * /*Sender*/,
  560. TListItem * Item, AnsiString & InfoTip)
  561. {
  562. InfoTip = ((TSessionData*)Item->Data)->InfoTip;
  563. }
  564. //---------------------------------------------------------------------------
  565. void __fastcall TLoginDialog::SessionListViewKeyDown(TObject * /*Sender*/,
  566. WORD &Key, TShiftState /*Shift*/)
  567. {
  568. if (Key == VK_DELETE) DeleteSessionAction->Execute();
  569. }
  570. //---------------------------------------------------------------------------
  571. void __fastcall TLoginDialog::LoadSessionActionExecute(TObject * /*Sender*/)
  572. {
  573. if (SelectedSession)
  574. {
  575. SessionData = SelectedSession;
  576. ChangePage(BasicSheet);
  577. }
  578. }
  579. //---------------------------------------------------------------------------
  580. void __fastcall TLoginDialog::SaveSessionActionExecute(TObject * /*Sender*/)
  581. {
  582. AnsiString SessionName;
  583. SaveSession(FSessionData);
  584. SessionName = DoSaveSessionDialog(StoredSessions, FSessionData->SessionName);
  585. if (!SessionName.IsEmpty())
  586. {
  587. TListItem * Item;
  588. TSessionData *NewSession =
  589. StoredSessions->NewSession(SessionName, FSessionData);
  590. StoredSessions->Save();
  591. // by now list must contais same number of items or one less
  592. assert(StoredSessions->Count == SessionListView->Items->Count ||
  593. StoredSessions->Count == SessionListView->Items->Count+1);
  594. if (StoredSessions->Count > SessionListView->Items->Count)
  595. Item = SessionListView->Items->Insert(StoredSessions->IndexOf(NewSession));
  596. else
  597. Item = SessionListView->Items->Item[StoredSessions->IndexOf(NewSession)];
  598. LoadSessionItem(Item);
  599. SelectedSession = NewSession;
  600. SessionData = NewSession;
  601. PageControl->ActivePage = SessionListSheet;
  602. }
  603. }
  604. //---------------------------------------------------------------------------
  605. void __fastcall TLoginDialog::DeleteSessionActionExecute(TObject * /*Sender*/)
  606. {
  607. if (SelectedSession)
  608. {
  609. Integer PrevSelectedIndex = SessionListView->Selected->Index;
  610. SelectedSession->Remove();
  611. StoredSessions->Remove(SelectedSession);
  612. SessionListView->Selected->Delete();
  613. if (SessionListView->Items->Count)
  614. {
  615. if (PrevSelectedIndex >= SessionListView->Items->Count)
  616. PrevSelectedIndex = SessionListView->Items->Count - 1;
  617. SelectedSession =
  618. (TSessionData *)StoredSessions->AtObject(PrevSelectedIndex);
  619. }
  620. }
  621. }
  622. //---------------------------------------------------------------------------
  623. void __fastcall TLoginDialog::ImportSessionsActionExecute(TObject * /*Sender*/)
  624. {
  625. if (DoImportSessionsDialog(StoredSessions))
  626. {
  627. LoadSessions();
  628. if (SessionListView->Items->Count)
  629. SessionListView->Items->Item[0]->MakeVisible(False);
  630. }
  631. }
  632. //---------------------------------------------------------------------------
  633. void __fastcall TLoginDialog::CleanUpActionExecute(TObject * /*Sender*/)
  634. {
  635. if (DoCleanupDialog(StoredSessions, Configuration))
  636. LoadSessions();
  637. }
  638. //---------------------------------------------------------------------------
  639. void __fastcall TLoginDialog::AboutActionExecute(TObject * /*Sender*/)
  640. {
  641. DoAboutDialog(Configuration);
  642. }
  643. //---------------------------------------------------------------------------
  644. void __fastcall TLoginDialog::ActionListUpdate(TBasicAction *Action,
  645. bool &Handled)
  646. {
  647. if (Action == LoadSessionAction)
  648. {
  649. LoadSessionAction->Enabled = SessionListView->Selected;
  650. }
  651. else if (Action == DeleteSessionAction)
  652. {
  653. TSessionData * Data = SessionData;
  654. DeleteSessionAction->Enabled =
  655. SessionListView->Selected && Data && !Data->Special;
  656. }
  657. else if (Action == DesktopIconAction)
  658. {
  659. DesktopIconAction->Enabled = SessionListView->Selected;
  660. }
  661. else if (Action == SendToHookAction)
  662. {
  663. SendToHookAction->Enabled = SessionListView->Selected;
  664. }
  665. else if (Action == LoginAction)
  666. {
  667. TSessionData * Data = SessionData;
  668. LoginAction->Enabled = Data && Data->CanLogin;
  669. }
  670. else if (Action == SaveSessionAction)
  671. {
  672. SaveSessionAction->Enabled = (PageControl->ActivePage != SessionListSheet);
  673. }
  674. Handled = true;
  675. }
  676. //---------------------------------------------------------------------------
  677. bool __fastcall TLoginDialog::Execute()
  678. {
  679. LoadConfiguration();
  680. bool Result = (ShowModal() == mrOk);
  681. if (Result)
  682. {
  683. SaveConfiguration();
  684. }
  685. return Result;
  686. }
  687. //---------------------------------------------------------------------------
  688. void __fastcall TLoginDialog::SaveConfiguration()
  689. {
  690. WinConfiguration->BeginUpdate();
  691. try
  692. {
  693. LoggingFrame->SaveConfiguration();
  694. GeneralSettingsFrame->SaveConfiguration();
  695. WinConfiguration->ShowAdvancedLoginOptions = ShowAdvancedLoginOptionsCheck->Checked;
  696. }
  697. __finally
  698. {
  699. WinConfiguration->EndUpdate();
  700. }
  701. }
  702. //---------------------------------------------------------------------------
  703. void __fastcall TLoginDialog::LoadConfiguration()
  704. {
  705. LoggingFrame->LoadConfiguration();
  706. GeneralSettingsFrame->LoadConfiguration();
  707. ShowAdvancedLoginOptionsCheck->Checked = WinConfiguration->ShowAdvancedLoginOptions;
  708. UpdateControls();
  709. }
  710. //---------------------------------------------------------------------------
  711. void __fastcall TLoginDialog::LoggingGetDefaultLogFileName(
  712. TObject* /*Sender*/, AnsiString & DefaultLogFileName)
  713. {
  714. assert(FSessionData);
  715. DefaultLogFileName = FSessionData->DefaultLogFileName;
  716. }
  717. //---------------------------------------------------------------------------
  718. void __fastcall TLoginDialog::PreferencesButtonClick(TObject * /*Sender*/)
  719. {
  720. ShowPreferencesDialog();
  721. }
  722. //---------------------------------------------------------------------------
  723. void __fastcall TLoginDialog::ShowPreferencesDialog()
  724. {
  725. DoPreferencesDialog(pmLogin);
  726. }
  727. //---------------------------------------------------------------------------
  728. void __fastcall TLoginDialog::NewSessionActionExecute(TObject * /*Sender*/)
  729. {
  730. Default();
  731. ChangePage(BasicSheet);
  732. }
  733. //---------------------------------------------------------------------------
  734. void __fastcall TLoginDialog::NavigationTreeChange(TObject * /*Sender*/,
  735. TTreeNode *Node)
  736. {
  737. if (Node->SelectedIndex)
  738. {
  739. for (Integer Index = 0; Index < PageControl->PageCount; Index++)
  740. {
  741. if (PageControl->Pages[Index]->Tag == (Node->SelectedIndex & 127))
  742. {
  743. PageControl->ActivePage = PageControl->Pages[Index];
  744. return;
  745. }
  746. }
  747. }
  748. assert(false);
  749. }
  750. //---------------------------------------------------------------------------
  751. void __fastcall TLoginDialog::ChangePage(TTabSheet * Tab)
  752. {
  753. PageControl->ActivePage = Tab;
  754. PageControlChange(PageControl);
  755. }
  756. //---------------------------------------------------------------------------
  757. void __fastcall TLoginDialog::PageControlChange(TObject *Sender)
  758. {
  759. bool Found = false;
  760. if (PageControl->ActivePage->Tag)
  761. {
  762. for (int Index = 0; Index < NavigationTree->Items->Count; Index++)
  763. {
  764. if ((NavigationTree->Items->Item[Index]->SelectedIndex & 127) ==
  765. PageControl->ActivePage->Tag)
  766. {
  767. NavigationTree->Items->Item[Index]->Selected = true;
  768. Found = true;
  769. }
  770. }
  771. }
  772. if (!Found)
  773. {
  774. ChangePage(BasicSheet);
  775. }
  776. else
  777. {
  778. DataChange(Sender);
  779. }
  780. }
  781. //---------------------------------------------------------------------------
  782. TTreeView * __fastcall TLoginDialog::GetNavigationTree()
  783. {
  784. return (ShowAdvancedLoginOptionsCheck->Checked ?
  785. AdvancedNavigationTree : SimpleNavigationTree);
  786. }
  787. //---------------------------------------------------------------------------
  788. void __fastcall TLoginDialog::CMDialogKey(TWMKeyDown & Message)
  789. {
  790. if (Message.CharCode == VK_TAB)
  791. {
  792. TShiftState Shift = KeyDataToShiftState(Message.KeyData);
  793. if (Shift.Contains(ssCtrl))
  794. {
  795. TTreeNode * Node = NavigationTree->Selected;
  796. if (!Shift.Contains(ssShift))
  797. {
  798. Node = Node->GetNext();
  799. if (!Node) Node = NavigationTree->Items->GetFirstNode();
  800. }
  801. else
  802. {
  803. if (Node->GetPrev()) Node = Node->GetPrev();
  804. else
  805. while (Node->GetNext()) Node = Node->GetNext();
  806. }
  807. Node->Selected = True;
  808. Message.Result = 1;
  809. return;
  810. }
  811. }
  812. TForm::Dispatch(&Message);
  813. }
  814. //---------------------------------------------------------------------------
  815. void __fastcall TLoginDialog::Dispatch(void *Message)
  816. {
  817. TMessage * M = reinterpret_cast<TMessage*>(Message);
  818. assert(M);
  819. if (M->Msg == CM_DIALOGKEY)
  820. {
  821. CMDialogKey(*((TWMKeyDown *)Message));
  822. }
  823. else if (M->Msg == WM_LOCALE_CHANGE)
  824. {
  825. if (M->WParam == 0)
  826. {
  827. SaveConfiguration();
  828. SaveSession(FSessionData);
  829. FSavedTab = PageControl->ActivePage;
  830. FSavedSession = SessionListView->ItemIndex;
  831. RevokeSystemSettings(this, FSystemSettings);
  832. ShowTabs(true);
  833. Hide();
  834. }
  835. else
  836. {
  837. FLocaleChanging = true;
  838. try
  839. {
  840. Show();
  841. }
  842. __finally
  843. {
  844. FLocaleChanging = false;
  845. }
  846. }
  847. }
  848. else
  849. {
  850. TForm::Dispatch(Message);
  851. }
  852. }
  853. //---------------------------------------------------------------------------
  854. void __fastcall TLoginDialog::CipherListBoxStartDrag(TObject * /*Sender*/,
  855. TDragObject *& /*DragObject*/)
  856. {
  857. FCipherDragSource = CipherListBox->ItemIndex;
  858. FCipherDragDest = -1;
  859. }
  860. //---------------------------------------------------------------------------
  861. void __fastcall TLoginDialog::CipherListBoxDragOver(TObject * /*Sender*/,
  862. TObject *Source, int X, int Y, TDragState /*State*/, bool &Accept)
  863. {
  864. if (Source == CipherListBox) Accept = AllowCipherDrag(X, Y);
  865. }
  866. //---------------------------------------------------------------------------
  867. void __fastcall TLoginDialog::CipherListBoxDragDrop(TObject * /*Sender*/,
  868. TObject *Source, int X, int Y)
  869. {
  870. if (Source == CipherListBox)
  871. {
  872. if (AllowCipherDrag(X, Y)) CipherMove(FCipherDragSource, FCipherDragDest);
  873. }
  874. }
  875. //---------------------------------------------------------------------------
  876. void __fastcall TLoginDialog::CipherButtonClick(TObject *Sender)
  877. {
  878. CipherMove(CipherListBox->ItemIndex,
  879. CipherListBox->ItemIndex + (Sender == CipherUpButton ? -1 : 1));
  880. UpdateControls();
  881. }
  882. //---------------------------------------------------------------------------
  883. bool __fastcall TLoginDialog::AllowCipherDrag(int X, int Y)
  884. {
  885. FCipherDragDest = CipherListBox->ItemAtPos(TPoint(X, Y), true);
  886. return (FCipherDragDest >= 0) && (FCipherDragDest != FCipherDragSource);
  887. }
  888. //---------------------------------------------------------------------------
  889. void __fastcall TLoginDialog::CipherMove(int Source, int Dest)
  890. {
  891. if (Source >= 0 && Source < CipherListBox->Items->Count &&
  892. Dest >= 0 && Dest < CipherListBox->Items->Count)
  893. {
  894. CipherListBox->Items->Move(Source, Dest);
  895. CipherListBox->ItemIndex = Dest;
  896. CipherListBox->SetFocus();
  897. }
  898. }
  899. //---------------------------------------------------------------------------
  900. void __fastcall TLoginDialog::SetDefaultSessionActionExecute(
  901. TObject * /*Sender*/)
  902. {
  903. if (MessageDialog(LoadStr(SET_DEFAULT_SESSION_SETTINGS), qtConfirmation,
  904. qaOK | qaCancel, 0) == qaOK)
  905. {
  906. SaveSession(FSessionData);
  907. StoredSessions->DefaultSettings = FSessionData;
  908. }
  909. }
  910. //---------------------------------------------------------------------------
  911. void __fastcall TLoginDialog::ToolsMenuButtonClick(TObject * /*Sender*/)
  912. {
  913. TPoint PopupPoint = ToolsMenuButton->ClientToScreen(TPoint(0, ToolsMenuButton->Height));
  914. ToolsPopupMenu->Popup(PopupPoint.x, PopupPoint.y);
  915. }
  916. //---------------------------------------------------------------------------
  917. void __fastcall TLoginDialog::ShellIconsButtonClick(TObject * /*Sender*/)
  918. {
  919. TPoint PopupPoint = ShellIconsButton->ClientToScreen(TPoint(0, ShellIconsButton->Height));
  920. IconsPopupMenu->Popup(PopupPoint.x, PopupPoint.y);
  921. }
  922. //---------------------------------------------------------------------------
  923. void __fastcall TLoginDialog::DesktopIconActionExecute(TObject * /*Sender*/)
  924. {
  925. if (MessageDialog(FMTLOAD(CONFIRM_CREATE_SHORTCUT, (SelectedSession->Name)),
  926. qtConfirmation, qaYes | qaNo, 0) == qaYes)
  927. {
  928. assert(SelectedSession);
  929. CreateDesktopShortCut(SelectedSession->Name, Application->ExeName,
  930. FORMAT("\"%s\"", (SelectedSession->Name)),
  931. FMTLOAD(SHORTCUT_INFO_TIP, (SelectedSession->Name, SelectedSession->InfoTip)));
  932. }
  933. }
  934. //---------------------------------------------------------------------------
  935. void __fastcall TLoginDialog::SendToHookActionExecute(TObject * /*Sender*/)
  936. {
  937. if (MessageDialog(FMTLOAD(CONFIRM_CREATE_SENDTO, (SelectedSession->Name)),
  938. qtConfirmation, qaYes | qaNo, 0) == qaYes)
  939. {
  940. assert(SelectedSession);
  941. CreateDesktopShortCut(FMTLOAD(SESSION_SENDTO_HOOK_NAME, (SelectedSession->Name)),
  942. Application->ExeName,
  943. FORMAT("\"%s\" /upload", (SelectedSession->Name)), "",
  944. CSIDL_SENDTO);
  945. }
  946. }
  947. //---------------------------------------------------------------------------
  948. void __fastcall TLoginDialog::SessionListViewCustomDrawItem(
  949. TCustomListView *Sender, TListItem *Item, TCustomDrawState /*State*/,
  950. bool &DefaultDraw)
  951. {
  952. TSessionData * Data = (TSessionData *)Item->Data;
  953. TFontStyles Styles = Sender->Canvas->Font->Style;
  954. if (Data->Special) Styles = Styles /*< fsItalic*/ << fsBold << fsUnderline;
  955. else Styles = Styles /*>> fsItalic*/ >> fsBold >> fsUnderline;
  956. Sender->Canvas->Font->Style = Styles;
  957. DefaultDraw = true;
  958. }
  959. //---------------------------------------------------------------------------
  960. void __fastcall TLoginDialog::CheckForUpdatesActionExecute(TObject * /*Sender*/)
  961. {
  962. CheckForUpdates();
  963. }
  964. //---------------------------------------------------------------------------
  965. void __fastcall TLoginDialog::SetInitial(bool value)
  966. {
  967. if (Initial != value)
  968. {
  969. FInitial = value;
  970. LanguagesButton->Visible = Initial;
  971. }
  972. }
  973. //---------------------------------------------------------------------------
  974. void __fastcall TLoginDialog::LanguagesButtonClick(TObject * /*Sender*/)
  975. {
  976. TPoint PopupPoint = LanguagesButton->ClientToScreen(TPoint(0, LanguagesButton->Height));
  977. delete FLanguagesPopupMenu;
  978. FLanguagesPopupMenu = new TPopupMenu(this);
  979. TStrings * Locales = WinConfiguration->Locales;
  980. for (int Index = 0; Index < Locales->Count; Index++)
  981. {
  982. TMenuItem * Item = new TMenuItem(FLanguagesPopupMenu);
  983. FLanguagesPopupMenu->Items->Add(Item);
  984. Item->Caption = Locales->Strings[Index];
  985. Item->Tag = reinterpret_cast<int>(Locales->Objects[Index]);
  986. Item->OnClick = LocaleClick;
  987. Item->Checked = (reinterpret_cast<LCID>(Locales->Objects[Index]) ==
  988. WinConfiguration->Locale);
  989. }
  990. FLanguagesPopupMenu->Popup(PopupPoint.x, PopupPoint.y);
  991. }
  992. //---------------------------------------------------------------------------
  993. void __fastcall TLoginDialog::LocaleClick(TObject * Sender)
  994. {
  995. assert(Sender);
  996. WinConfiguration->Locale =
  997. static_cast<LCID>(dynamic_cast<TMenuItem*>(Sender)->Tag);
  998. LanguagesButton->SetFocus();
  999. }
  1000. //---------------------------------------------------------------------------