ImportSessions.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. #include <PasTools.hpp>
  13. //---------------------------------------------------------------------
  14. #ifndef NO_RESOURCES
  15. #pragma resource "*.dfm"
  16. #endif
  17. //---------------------------------------------------------------------
  18. bool __fastcall DoImportSessionsDialog(TList * Imported)
  19. {
  20. std::unique_ptr<TStrings> Errors(new TStringList());
  21. UnicodeString Error;
  22. std::unique_ptr<TStoredSessionList> PuttyImportSessionList(
  23. GUIConfiguration->SelectPuttySessionsForImport(StoredSessions, Error));
  24. Errors->Add(Error);
  25. std::unique_ptr<TStoredSessionList> FilezillaImportSessionList(
  26. Configuration->SelectFilezillaSessionsForImport(StoredSessions, Error));
  27. Errors->Add(Error);
  28. std::unique_ptr<TList> SessionListsList(new TList());
  29. SessionListsList->Add(PuttyImportSessionList.get());
  30. SessionListsList->Add(FilezillaImportSessionList.get());
  31. bool ImportKeys = true;
  32. std::unique_ptr<TImportSessionsDialog> ImportSessionsDialog(
  33. SafeFormCreate<TImportSessionsDialog>(Application));
  34. ImportSessionsDialog->Init(SessionListsList.get(), Errors.get());
  35. bool Result = ImportSessionsDialog->Execute(ImportKeys);
  36. if (Result)
  37. {
  38. StoredSessions->Import(PuttyImportSessionList.get(), true, Imported);
  39. StoredSessions->Import(FilezillaImportSessionList.get(), true, Imported);
  40. if (ImportKeys)
  41. {
  42. UnicodeString TargetKey = Configuration->RegistryStorageKey + L"\\" + Configuration->SshHostKeysSubKey;
  43. UnicodeString SourceKey = Configuration->PuttyRegistryStorageKey + L"\\" + Configuration->SshHostKeysSubKey;
  44. TStoredSessionList::ImportHostKeys(TargetKey, SourceKey, PuttyImportSessionList.get(), true);
  45. // Filezilla uses PuTTY's host key store
  46. TStoredSessionList::ImportHostKeys(TargetKey, SourceKey, FilezillaImportSessionList.get(), true);
  47. }
  48. }
  49. return Result;
  50. }
  51. //---------------------------------------------------------------------
  52. __fastcall TImportSessionsDialog::TImportSessionsDialog(TComponent * AOwner) :
  53. TForm(AOwner)
  54. {
  55. UseSystemSettings(this);
  56. // this is loaded from res string to force translation
  57. Caption = LoadStr(IMPORT_CAPTION);
  58. }
  59. //---------------------------------------------------------------------
  60. void __fastcall TImportSessionsDialog::Init(TList * SessionListsList, TStrings * Errors)
  61. {
  62. FErrors = Errors;
  63. for (int Index = 0; Index < SessionListsList->Count; Index++)
  64. {
  65. SourceComboBox->Items->Objects[Index] = static_cast<TObject *>(SessionListsList->Items[Index]);
  66. if ((SourceComboBox->ItemIndex < 0) && (GetSessionList(Index)->Count > 0))
  67. {
  68. SourceComboBox->ItemIndex = Index;
  69. }
  70. }
  71. if (SourceComboBox->ItemIndex < 0)
  72. {
  73. SourceComboBox->ItemIndex = 0;
  74. }
  75. int Offset = ScaleByTextHeight(this, 8);
  76. ErrorPanel->BoundsRect =
  77. TRect(
  78. SessionListView2->BoundsRect.Left + Offset, SessionListView2->BoundsRect.Top + Offset,
  79. SessionListView2->BoundsRect.Right - Offset, SessionListView2->BoundsRect.Bottom - Offset);
  80. }
  81. //---------------------------------------------------------------------
  82. TStoredSessionList * __fastcall TImportSessionsDialog::GetSessionList(int Index)
  83. {
  84. return dynamic_cast<TStoredSessionList *>(SourceComboBox->Items->Objects[Index]);
  85. }
  86. //---------------------------------------------------------------------
  87. void __fastcall TImportSessionsDialog::UpdateControls()
  88. {
  89. EnableControl(OKButton, ListViewAnyChecked(SessionListView2));
  90. bool AnySshChecked = false;
  91. for (int Index = 0; Index < SessionListView2->Items->Count; Index++)
  92. {
  93. TListItem * Item = SessionListView2->Items->Item[Index];
  94. TSessionData * Data = (TSessionData*)Item->Data;
  95. if (Item->Checked && Data->UsesSsh)
  96. {
  97. AnySshChecked = true;
  98. break;
  99. }
  100. }
  101. EnableControl(ImportKeysCheck, AnySshChecked);
  102. EnableControl(CheckAllButton, SessionListView2->Items->Count > 0);
  103. AutoSizeListColumnsWidth(SessionListView2);
  104. }
  105. //---------------------------------------------------------------------
  106. void __fastcall TImportSessionsDialog::ClearSelections()
  107. {
  108. for (int Index = 0; Index < SourceComboBox->Items->Count; Index++)
  109. {
  110. TStoredSessionList * SessionList = GetSessionList(Index);
  111. for (int Index = 0; Index < SessionList->Count; Index++)
  112. {
  113. SessionList->Sessions[Index]->Selected = false;
  114. }
  115. }
  116. }
  117. //---------------------------------------------------------------------
  118. void __fastcall TImportSessionsDialog::SaveSelection()
  119. {
  120. for (int Index = 0; Index < SessionListView2->Items->Count; Index++)
  121. {
  122. ((TSessionData*)SessionListView2->Items->Item[Index]->Data)->Selected =
  123. SessionListView2->Items->Item[Index]->Checked;
  124. }
  125. }
  126. //---------------------------------------------------------------------
  127. void __fastcall TImportSessionsDialog::LoadSessions()
  128. {
  129. TStoredSessionList * SessionList = GetSessionList(SourceComboBox->ItemIndex);
  130. SessionListView2->Items->BeginUpdate();
  131. try
  132. {
  133. SessionListView2->Items->Clear();
  134. for (int Index = 0; Index < SessionList->Count; Index++)
  135. {
  136. TSessionData * Session = SessionList->Sessions[Index];
  137. TListItem * Item = SessionListView2->Items->Add();
  138. Item->Data = Session;
  139. Item->Caption = Session->Name;
  140. Item->Checked = Session->Selected;
  141. }
  142. }
  143. __finally
  144. {
  145. SessionListView2->Items->EndUpdate();
  146. }
  147. UnicodeString Error = FErrors->Strings[SourceComboBox->ItemIndex];
  148. if ((SessionList->Count > 0) || Error.IsEmpty())
  149. {
  150. ErrorPanel->Visible = false;
  151. }
  152. else
  153. {
  154. ErrorLabel->Caption = Error;
  155. ErrorPanel->Visible = true;
  156. }
  157. UpdateControls();
  158. }
  159. //---------------------------------------------------------------------------
  160. void __fastcall TImportSessionsDialog::SessionListView2InfoTip(
  161. TObject * /*Sender*/, TListItem * Item, UnicodeString & InfoTip)
  162. {
  163. InfoTip = ((TSessionData*)Item->Data)->InfoTip;
  164. }
  165. //---------------------------------------------------------------------------
  166. void __fastcall TImportSessionsDialog::SessionListView2MouseDown(
  167. TObject * /*Sender*/, TMouseButton /*Button*/, TShiftState /*Shift*/,
  168. int /*X*/, int /*Y*/)
  169. {
  170. UpdateControls();
  171. }
  172. //---------------------------------------------------------------------------
  173. void __fastcall TImportSessionsDialog::SessionListView2KeyUp(
  174. TObject * /*Sender*/, WORD & /*Key*/, TShiftState /*Shift*/)
  175. {
  176. UpdateControls();
  177. }
  178. //---------------------------------------------------------------------------
  179. void __fastcall TImportSessionsDialog::FormShow(TObject * /*Sender*/)
  180. {
  181. // Load only now, as earlier loading somehow breaks SessionListView2 layout on initial per-monitor DPI scaling
  182. LoadSessions();
  183. }
  184. //---------------------------------------------------------------------------
  185. void __fastcall TImportSessionsDialog::CheckAllButtonClick(TObject * /*Sender*/)
  186. {
  187. ListViewCheckAll(SessionListView2, caToggle);
  188. UpdateControls();
  189. }
  190. //---------------------------------------------------------------------------
  191. void __fastcall TImportSessionsDialog::HelpButtonClick(TObject * /*Sender*/)
  192. {
  193. FormHelp(this);
  194. }
  195. //---------------------------------------------------------------------------
  196. bool __fastcall TImportSessionsDialog::Execute(bool & ImportKeys)
  197. {
  198. ImportKeysCheck->Checked = ImportKeys;
  199. bool Result = (ShowModal() == DefaultResult(this));
  200. if (Result)
  201. {
  202. ClearSelections();
  203. SaveSelection();
  204. ImportKeys = ImportKeysCheck->Enabled && ImportKeysCheck->Checked;
  205. }
  206. return Result;
  207. }
  208. //---------------------------------------------------------------------------
  209. void __fastcall TImportSessionsDialog::SourceComboBoxSelect(TObject * /*Sender*/)
  210. {
  211. SaveSelection();
  212. LoadSessions();
  213. }
  214. //---------------------------------------------------------------------------