ImportSessions.cpp 5.1 KB

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