ImportSessions.cpp 5.3 KB

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