ImportSessions.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. #include <TextsWin.h>
  11. #include <CoreMain.h>
  12. //---------------------------------------------------------------------
  13. #ifndef NO_RESOURCES
  14. #pragma resource "*.dfm"
  15. #endif
  16. //---------------------------------------------------------------------
  17. bool __fastcall DoImportSessionsDialog()
  18. {
  19. std::auto_ptr<TStoredSessionList> PuttyImportSessionList(
  20. GUIConfiguration->SelectPuttySessionsForImport(StoredSessions));
  21. std::auto_ptr<TStoredSessionList> FilezillaImportSessionList(
  22. GUIConfiguration->SelectFilezillaSessionsForImport(StoredSessions));
  23. std::auto_ptr<TList> SessionListsList(new TList());
  24. SessionListsList->Add(PuttyImportSessionList.get());
  25. SessionListsList->Add(FilezillaImportSessionList.get());
  26. bool ImportKeys = true;
  27. std::auto_ptr<TImportSessionsDialog> ImportSessionsDialog(
  28. new TImportSessionsDialog(Application, SessionListsList.get()));
  29. bool Result = ImportSessionsDialog->Execute(ImportKeys);
  30. if (Result)
  31. {
  32. StoredSessions->Import(PuttyImportSessionList.get(), true);
  33. StoredSessions->Import(FilezillaImportSessionList.get(), true);
  34. if (ImportKeys)
  35. {
  36. UnicodeString TargetKey = Configuration->RegistryStorageKey + L"\\" + Configuration->SshHostKeysSubKey;
  37. UnicodeString SourceKey = Configuration->PuttyRegistryStorageKey + L"\\" + Configuration->SshHostKeysSubKey;
  38. TStoredSessionList::ImportHostKeys(TargetKey, SourceKey, PuttyImportSessionList.get(), true);
  39. // Filezilla uses PuTTY's host key store
  40. TStoredSessionList::ImportHostKeys(TargetKey, SourceKey, FilezillaImportSessionList.get(), true);
  41. }
  42. }
  43. return Result;
  44. }
  45. //---------------------------------------------------------------------
  46. __fastcall TImportSessionsDialog::TImportSessionsDialog(TComponent * AOwner,
  47. TList * SessionListsList)
  48. : TForm(AOwner)
  49. {
  50. UseSystemSettings(this);
  51. // this is loaded from res string to force translation
  52. Caption = LoadStr(IMPORT_CAPTION);
  53. for (int Index = 0; Index < SessionListsList->Count; Index++)
  54. {
  55. SourceComboBox->Items->Objects[Index] = static_cast<TObject *>(SessionListsList->Items[Index]);
  56. if ((SourceComboBox->ItemIndex < 0) && (GetSessionList(Index)->Count > 0))
  57. {
  58. SourceComboBox->ItemIndex = Index;
  59. }
  60. }
  61. // should not happen as we never get here when there are no session to import
  62. if (SourceComboBox->ItemIndex < 0)
  63. {
  64. SourceComboBox->ItemIndex = 0;
  65. }
  66. LoadSessions();
  67. }
  68. //---------------------------------------------------------------------
  69. TStoredSessionList * __fastcall TImportSessionsDialog::GetSessionList(int Index)
  70. {
  71. return dynamic_cast<TStoredSessionList *>(SourceComboBox->Items->Objects[Index]);
  72. }
  73. //---------------------------------------------------------------------
  74. void __fastcall TImportSessionsDialog::UpdateControls()
  75. {
  76. EnableControl(OKButton, ListViewAnyChecked(SessionListView2));
  77. bool AnySshChecked = false;
  78. for (int Index = 0; Index < SessionListView2->Items->Count; Index++)
  79. {
  80. TListItem * Item = SessionListView2->Items->Item[Index];
  81. TSessionData * Data = (TSessionData*)Item->Data;
  82. if (Item->Checked && Data->UsesSsh)
  83. {
  84. AnySshChecked = true;
  85. break;
  86. }
  87. }
  88. EnableControl(ImportKeysCheck, AnySshChecked);
  89. EnableControl(CheckAllButton, SessionListView2->Items->Count > 0);
  90. AdjustListColumnsWidth(SessionListView2);
  91. }
  92. //---------------------------------------------------------------------
  93. void __fastcall TImportSessionsDialog::ClearSelections()
  94. {
  95. for (int Index = 0; Index < SourceComboBox->Items->Count; Index++)
  96. {
  97. TStoredSessionList * SessionList = GetSessionList(Index);
  98. for (int Index = 0; Index < SessionList->Count; Index++)
  99. {
  100. SessionList->Sessions[Index]->Selected = false;
  101. }
  102. }
  103. }
  104. //---------------------------------------------------------------------
  105. void __fastcall TImportSessionsDialog::SaveSelection()
  106. {
  107. for (int Index = 0; Index < SessionListView2->Items->Count; Index++)
  108. {
  109. ((TSessionData*)SessionListView2->Items->Item[Index]->Data)->Selected =
  110. SessionListView2->Items->Item[Index]->Checked;
  111. }
  112. }
  113. //---------------------------------------------------------------------
  114. void __fastcall TImportSessionsDialog::LoadSessions()
  115. {
  116. SessionListView2->Items->BeginUpdate();
  117. try
  118. {
  119. SessionListView2->Items->Clear();
  120. TStoredSessionList * SessionList = GetSessionList(SourceComboBox->ItemIndex);
  121. for (int Index = 0; Index < SessionList->Count; Index++)
  122. {
  123. TSessionData * Session = SessionList->Sessions[Index];
  124. TListItem * Item = SessionListView2->Items->Add();
  125. Item->Data = Session;
  126. Item->Caption = Session->Name;
  127. Item->Checked = Session->Selected;
  128. }
  129. }
  130. __finally
  131. {
  132. SessionListView2->Items->EndUpdate();
  133. }
  134. UpdateControls();
  135. }
  136. //---------------------------------------------------------------------------
  137. void __fastcall TImportSessionsDialog::SessionListView2InfoTip(
  138. TObject * /*Sender*/, TListItem * Item, UnicodeString & InfoTip)
  139. {
  140. InfoTip = ((TSessionData*)Item->Data)->InfoTip;
  141. }
  142. //---------------------------------------------------------------------------
  143. void __fastcall TImportSessionsDialog::SessionListView2MouseDown(
  144. TObject * /*Sender*/, TMouseButton /*Button*/, TShiftState /*Shift*/,
  145. int /*X*/, int /*Y*/)
  146. {
  147. UpdateControls();
  148. }
  149. //---------------------------------------------------------------------------
  150. void __fastcall TImportSessionsDialog::SessionListView2KeyUp(
  151. TObject * /*Sender*/, WORD & /*Key*/, TShiftState /*Shift*/)
  152. {
  153. UpdateControls();
  154. }
  155. //---------------------------------------------------------------------------
  156. void __fastcall TImportSessionsDialog::FormShow(TObject * /*Sender*/)
  157. {
  158. UpdateControls();
  159. }
  160. //---------------------------------------------------------------------------
  161. void __fastcall TImportSessionsDialog::CheckAllButtonClick(TObject * /*Sender*/)
  162. {
  163. ListViewCheckAll(SessionListView2, caToggle);
  164. UpdateControls();
  165. }
  166. //---------------------------------------------------------------------------
  167. void __fastcall TImportSessionsDialog::HelpButtonClick(TObject * /*Sender*/)
  168. {
  169. FormHelp(this);
  170. }
  171. //---------------------------------------------------------------------------
  172. bool __fastcall TImportSessionsDialog::Execute(bool & ImportKeys)
  173. {
  174. ImportKeysCheck->Checked = ImportKeys;
  175. bool Result = (ShowModal() == mrOk);
  176. if (Result)
  177. {
  178. ClearSelections();
  179. SaveSelection();
  180. ImportKeys = ImportKeysCheck->Enabled && ImportKeysCheck->Checked;
  181. }
  182. return Result;
  183. }
  184. //---------------------------------------------------------------------------
  185. void __fastcall TImportSessionsDialog::SourceComboBoxSelect(TObject * /*Sender*/)
  186. {
  187. SaveSelection();
  188. LoadSessions();
  189. }
  190. //---------------------------------------------------------------------------