ImportSessions.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. //---------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include <Common.h>
  5. #include "ImportSessions.h"
  6. #include <Configuration.h>
  7. #include <ScpMain.h>
  8. #include <VCLCommon.h>
  9. //---------------------------------------------------------------------
  10. #pragma resource "*.dfm"
  11. //---------------------------------------------------------------------
  12. Boolean __fastcall DoImportSessionsDialog(TStoredSessionList *SessionList)
  13. {
  14. Boolean Result;
  15. TImportSessionsDialog *ImportSessionsDialog;
  16. TStoredSessionList *ImportSessionList;
  17. try {
  18. ImportSessionsDialog = new TImportSessionsDialog(Application);
  19. ImportSessionList = new TStoredSessionList(true);
  20. ImportSessionList->Load(Configuration->PuttySessionsKey);
  21. ImportSessionList->SelectSessionsToImport(SessionList, True);
  22. ImportSessionsDialog->SessionList = ImportSessionList;
  23. Result = (ImportSessionsDialog->ShowModal() == mrOk);
  24. if (Result)
  25. {
  26. SessionList->Import(ImportSessionsDialog->SessionList, True);
  27. if (ImportSessionsDialog->ImportKeys)
  28. {
  29. TStoredSessionList::ImportHostKeys(
  30. Configuration->RegistryStorageKey + "\\" + Configuration->SshHostKeysSubKey,
  31. Configuration->PuttyRegistryStorageKey + "\\" + Configuration->SshHostKeysSubKey,
  32. ImportSessionsDialog->SessionList, true);
  33. }
  34. }
  35. } __finally {
  36. delete ImportSessionsDialog;
  37. delete ImportSessionList;
  38. }
  39. return Result;
  40. }
  41. //---------------------------------------------------------------------
  42. __fastcall TImportSessionsDialog::TImportSessionsDialog(TComponent* AOwner)
  43. : TForm(AOwner)
  44. {
  45. UseSystemSettings(this);
  46. }
  47. //---------------------------------------------------------------------
  48. void __fastcall TImportSessionsDialog::UpdateControls()
  49. {
  50. bool AnyChecked = ListViewAnyChecked(SessionListView);
  51. EnableControl(OKButton, AnyChecked);
  52. EnableControl(ImportKeysCheck, AnyChecked);
  53. EnableControl(CheckAllButton, SessionListView->Items->Count > 0);
  54. AdjustListColumnsWidth(SessionListView);
  55. }
  56. //---------------------------------------------------------------------
  57. void __fastcall TImportSessionsDialog::SetSessionList(TStoredSessionList *value)
  58. {
  59. if (FSessionList != value)
  60. {
  61. FSessionList = value;
  62. LoadSessions();
  63. }
  64. }
  65. //---------------------------------------------------------------------
  66. void __fastcall TImportSessionsDialog::LoadSessions()
  67. {
  68. SessionListView->Items->BeginUpdate();
  69. try {
  70. SessionListView->Items->Clear();
  71. if (FSessionList)
  72. for (int Index = 0; Index < FSessionList->Count; Index++)
  73. {
  74. TListItem *Item;
  75. TSessionData *Session =
  76. (TSessionData*)FSessionList->AtObject(Index);
  77. Item = SessionListView->Items->Add();
  78. Item->Data = Session;
  79. Item->Caption = Session->Name;
  80. Item->SubItems->Add(Session->ProtocolStr);
  81. Item->Checked = Session->Selected;
  82. }
  83. } __finally {
  84. SessionListView->Items->EndUpdate();
  85. }
  86. UpdateControls();
  87. }
  88. //---------------------------------------------------------------------------
  89. void __fastcall TImportSessionsDialog::FormClose(TObject * /*Sender*/,
  90. TCloseAction & /*Action*/)
  91. {
  92. for (Integer Index = 0; Index < SessionListView->Items->Count; Index++)
  93. ((TSessionData*)SessionListView->Items->Item[Index]->Data)->Selected =
  94. SessionListView->Items->Item[Index]->Checked;
  95. }
  96. //---------------------------------------------------------------------------
  97. void __fastcall TImportSessionsDialog::SessionListViewInfoTip(
  98. TObject * /*Sender*/, TListItem * Item, AnsiString & InfoTip)
  99. {
  100. InfoTip = ((TSessionData*)Item->Data)->InfoTip;
  101. }
  102. //---------------------------------------------------------------------------
  103. void __fastcall TImportSessionsDialog::SessionListViewMouseDown(
  104. TObject * /*Sender*/, TMouseButton /*Button*/, TShiftState /*Shift*/,
  105. int /*X*/, int /*Y*/)
  106. {
  107. UpdateControls();
  108. }
  109. //---------------------------------------------------------------------------
  110. void __fastcall TImportSessionsDialog::SessionListViewKeyUp(
  111. TObject * /*Sender*/, WORD & /*Key*/, TShiftState /*Shift*/)
  112. {
  113. UpdateControls();
  114. }
  115. //---------------------------------------------------------------------------
  116. void __fastcall TImportSessionsDialog::FormShow(TObject * /*Sender*/)
  117. {
  118. UpdateControls();
  119. }
  120. //---------------------------------------------------------------------------
  121. void __fastcall TImportSessionsDialog::CheckAllButtonClick(TObject * /*Sender*/)
  122. {
  123. ListViewCheckAll(SessionListView, caToggle);
  124. UpdateControls();
  125. }
  126. //---------------------------------------------------------------------------
  127. bool __fastcall TImportSessionsDialog::GetImportKeys()
  128. {
  129. return ImportKeysCheck->Checked;
  130. }