ImportSessions.cpp 7.9 KB

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