ImportSessions.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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 <Tools.h>
  13. #include <WinApi.h>
  14. #include <PasTools.hpp>
  15. //---------------------------------------------------------------------
  16. #pragma resource "*.dfm"
  17. //---------------------------------------------------------------------
  18. const int KnownHostsIndex = 2;
  19. //---------------------------------------------------------------------
  20. bool __fastcall DoImportSessionsDialog(TList * Imported)
  21. {
  22. std::unique_ptr<TStrings> Errors(new TStringList());
  23. UnicodeString Error;
  24. std::unique_ptr<TStoredSessionList> PuttyImportSessionList(
  25. GUIConfiguration->SelectPuttySessionsForImport(StoredSessions, Error));
  26. Errors->Add(Error);
  27. std::unique_ptr<TStoredSessionList> FilezillaImportSessionList(
  28. Configuration->SelectFilezillaSessionsForImport(StoredSessions, Error));
  29. Errors->Add(Error);
  30. std::unique_ptr<TStoredSessionList> KnownHostsImportSessionList(
  31. Configuration->SelectKnownHostsSessionsForImport(StoredSessions, Error));
  32. Errors->Add(Error);
  33. std::unique_ptr<TList> SessionListsList(new TList());
  34. SessionListsList->Add(PuttyImportSessionList.get());
  35. SessionListsList->Add(FilezillaImportSessionList.get());
  36. SessionListsList->Add(KnownHostsImportSessionList.get());
  37. std::unique_ptr<TImportSessionsDialog> ImportSessionsDialog(
  38. SafeFormCreate<TImportSessionsDialog>(Application));
  39. ImportSessionsDialog->Init(SessionListsList.get(), Errors.get());
  40. bool Result = ImportSessionsDialog->Execute();
  41. if (Result)
  42. {
  43. // Particularly when importing known_hosts, there is no feedback.
  44. TInstantOperationVisualizer Visualizer;
  45. StoredSessions->Import(PuttyImportSessionList.get(), true, Imported);
  46. StoredSessions->Import(FilezillaImportSessionList.get(), true, Imported);
  47. UnicodeString SourceKey = Configuration->PuttyRegistryStorageKey;
  48. TStoredSessionList::ImportHostKeys(SourceKey, PuttyImportSessionList.get(), true);
  49. // Filezilla uses PuTTY's host key store
  50. TStoredSessionList::ImportHostKeys(SourceKey, FilezillaImportSessionList.get(), true);
  51. TStoredSessionList * AKnownHostsImportSessionList =
  52. static_cast<TStoredSessionList *>(SessionListsList->Items[KnownHostsIndex]);
  53. TStoredSessionList::ImportSelectedKnownHosts(AKnownHostsImportSessionList);
  54. }
  55. return Result;
  56. }
  57. //---------------------------------------------------------------------
  58. __fastcall TImportSessionsDialog::TImportSessionsDialog(TComponent * AOwner) :
  59. TForm(AOwner)
  60. {
  61. UseSystemSettings(this);
  62. // this is loaded from res string to force translation
  63. Caption = LoadStr(IMPORT_CAPTION);
  64. }
  65. //---------------------------------------------------------------------
  66. void __fastcall TImportSessionsDialog::Init(TList * SessionListsList, TStrings * Errors)
  67. {
  68. FSessionListsList = SessionListsList;
  69. FErrors = Errors;
  70. for (int Index = 0; Index < SessionListsList->Count; Index++)
  71. {
  72. if ((SourceComboBox->ItemIndex < 0) && (GetSessionList(Index)->Count > 0))
  73. {
  74. SourceComboBox->ItemIndex = Index;
  75. }
  76. }
  77. if (SourceComboBox->ItemIndex < 0)
  78. {
  79. SourceComboBox->ItemIndex = 0;
  80. }
  81. int Offset = ScaleByTextHeight(this, 8);
  82. ErrorPanel->BoundsRect =
  83. TRect(
  84. SessionListView2->BoundsRect.Left + Offset, SessionListView2->BoundsRect.Top + Offset,
  85. SessionListView2->BoundsRect.Right - Offset, SessionListView2->BoundsRect.Bottom - Offset);
  86. }
  87. //---------------------------------------------------------------------
  88. TStoredSessionList * __fastcall TImportSessionsDialog::GetSessionList(int Index)
  89. {
  90. return reinterpret_cast<TStoredSessionList *>(FSessionListsList->Items[Index]);
  91. }
  92. //---------------------------------------------------------------------
  93. void __fastcall TImportSessionsDialog::UpdateControls()
  94. {
  95. PasteButton->Visible = (SourceComboBox->ItemIndex == KnownHostsIndex);
  96. EnableControl(PasteButton, IsFormatInClipboard(CF_TEXT));
  97. EnableControl(OKButton, ListViewAnyChecked(SessionListView2));
  98. EnableControl(CheckAllButton, SessionListView2->Items->Count > 0);
  99. AutoSizeListColumnsWidth(SessionListView2);
  100. }
  101. //---------------------------------------------------------------------
  102. void __fastcall TImportSessionsDialog::ClearSelections()
  103. {
  104. for (int Index = 0; Index < SourceComboBox->Items->Count; Index++)
  105. {
  106. TStoredSessionList * SessionList = GetSessionList(Index);
  107. for (int Index = 0; Index < SessionList->Count; Index++)
  108. {
  109. SessionList->Sessions[Index]->Selected = false;
  110. }
  111. }
  112. }
  113. //---------------------------------------------------------------------
  114. void __fastcall TImportSessionsDialog::SaveSelection()
  115. {
  116. for (int Index = 0; Index < SessionListView2->Items->Count; Index++)
  117. {
  118. ((TSessionData*)SessionListView2->Items->Item[Index]->Data)->Selected =
  119. SessionListView2->Items->Item[Index]->Checked;
  120. }
  121. }
  122. //---------------------------------------------------------------------
  123. void __fastcall TImportSessionsDialog::LoadSessions()
  124. {
  125. TStoredSessionList * SessionList = GetSessionList(SourceComboBox->ItemIndex);
  126. SessionListView2->Items->BeginUpdate();
  127. try
  128. {
  129. SessionListView2->Items->Clear();
  130. for (int Index = 0; Index < SessionList->Count; Index++)
  131. {
  132. TSessionData * Session = SessionList->Sessions[Index];
  133. TListItem * Item = SessionListView2->Items->Add();
  134. Item->Data = Session;
  135. Item->Caption = Session->Name;
  136. Item->Checked = Session->Selected;
  137. }
  138. }
  139. __finally
  140. {
  141. SessionListView2->Items->EndUpdate();
  142. }
  143. UnicodeString Error = FErrors->Strings[SourceComboBox->ItemIndex];
  144. if ((SessionList->Count > 0) || Error.IsEmpty())
  145. {
  146. ErrorPanel->Visible = false;
  147. }
  148. else
  149. {
  150. ErrorLabel->Caption = Error;
  151. ErrorPanel->Visible = true;
  152. }
  153. UpdateControls();
  154. }
  155. //---------------------------------------------------------------------------
  156. void __fastcall TImportSessionsDialog::SessionListView2InfoTip(
  157. TObject * /*Sender*/, TListItem * Item, UnicodeString & InfoTip)
  158. {
  159. TSessionData * Data = DebugNotNull(reinterpret_cast<TSessionData *>(Item->Data));
  160. if (SourceComboBox->ItemIndex == KnownHostsIndex)
  161. {
  162. UnicodeString Algs;
  163. UnicodeString HostKeys = Data->HostKey;
  164. while (!HostKeys.IsEmpty())
  165. {
  166. UnicodeString HostKey = CutToChar(HostKeys, L';', true);
  167. UnicodeString Alg = CutToChar(HostKey, L':', true);
  168. AddToList(Algs, Alg, L", ");
  169. }
  170. InfoTip = FMTLOAD(IMPORT_KNOWNHOSTS_INFO_TIP, (Data->HostName, Algs));
  171. }
  172. else
  173. {
  174. InfoTip = Data->InfoTip;
  175. }
  176. }
  177. //---------------------------------------------------------------------------
  178. void __fastcall TImportSessionsDialog::SessionListView2MouseDown(
  179. TObject * /*Sender*/, TMouseButton /*Button*/, TShiftState /*Shift*/,
  180. int /*X*/, int /*Y*/)
  181. {
  182. UpdateControls();
  183. }
  184. //---------------------------------------------------------------------------
  185. void __fastcall TImportSessionsDialog::SessionListView2KeyUp(
  186. TObject * /*Sender*/, WORD & /*Key*/, TShiftState /*Shift*/)
  187. {
  188. UpdateControls();
  189. }
  190. //---------------------------------------------------------------------------
  191. void __fastcall TImportSessionsDialog::FormShow(TObject * /*Sender*/)
  192. {
  193. // Load only now, as earlier loading somehow breaks SessionListView2 layout on initial per-monitor DPI scaling
  194. LoadSessions();
  195. }
  196. //---------------------------------------------------------------------------
  197. void __fastcall TImportSessionsDialog::CheckAllButtonClick(TObject * /*Sender*/)
  198. {
  199. ListViewCheckAll(SessionListView2, caToggle);
  200. UpdateControls();
  201. }
  202. //---------------------------------------------------------------------------
  203. void __fastcall TImportSessionsDialog::HelpButtonClick(TObject * /*Sender*/)
  204. {
  205. FormHelp(this);
  206. }
  207. //---------------------------------------------------------------------------
  208. bool __fastcall TImportSessionsDialog::Execute()
  209. {
  210. bool Result = (ShowModal() == DefaultResult(this));
  211. if (Result)
  212. {
  213. ClearSelections();
  214. SaveSelection();
  215. }
  216. return Result;
  217. }
  218. //---------------------------------------------------------------------------
  219. void __fastcall TImportSessionsDialog::SourceComboBoxSelect(TObject * /*Sender*/)
  220. {
  221. SaveSelection();
  222. LoadSessions();
  223. }
  224. //---------------------------------------------------------------------------
  225. void __fastcall TImportSessionsDialog::CreateHandle()
  226. {
  227. TForm::CreateHandle();
  228. if (DebugAlwaysTrue(HandleAllocated()))
  229. {
  230. HINSTANCE User32Library = LoadLibrary(L"user32.dll");
  231. AddClipboardFormatListenerProc AddClipboardFormatListener =
  232. (AddClipboardFormatListenerProc)GetProcAddress(User32Library, "AddClipboardFormatListener");
  233. if (AddClipboardFormatListener != NULL)
  234. {
  235. AddClipboardFormatListener(Handle);
  236. }
  237. }
  238. }
  239. //---------------------------------------------------------------------------
  240. void __fastcall TImportSessionsDialog::DestroyHandle()
  241. {
  242. if (DebugAlwaysTrue(HandleAllocated()))
  243. {
  244. HINSTANCE User32Library = LoadLibrary(L"user32.dll");
  245. RemoveClipboardFormatListenerProc RemoveClipboardFormatListener =
  246. (RemoveClipboardFormatListenerProc)GetProcAddress(User32Library, "RemoveClipboardFormatListener");
  247. if (RemoveClipboardFormatListener != NULL)
  248. {
  249. RemoveClipboardFormatListener(Handle);
  250. }
  251. }
  252. TForm::DestroyHandle();
  253. }
  254. //---------------------------------------------------------------------------
  255. void __fastcall TImportSessionsDialog::Dispatch(void * Message)
  256. {
  257. TMessage * M = static_cast<TMessage*>(Message);
  258. switch (M->Msg)
  259. {
  260. case WM_CLIPBOARDUPDATE:
  261. UpdateControls();
  262. TForm::Dispatch(Message);
  263. break;
  264. default:
  265. TForm::Dispatch(Message);
  266. break;
  267. }
  268. }
  269. //---------------------------------------------------------------------------
  270. void __fastcall TImportSessionsDialog::PasteButtonClick(TObject * /*Sender*/)
  271. {
  272. UnicodeString Text;
  273. // Proceed even when retrieving from clipboard fails, "no host keys" error will show.
  274. TextFromClipboard(Text, false);
  275. std::unique_ptr<TStrings> Lines(new TStringList());
  276. Lines->Text = Text;
  277. SessionListView2->Items->Clear();
  278. int Index = SourceComboBox->ItemIndex;
  279. UnicodeString Error;
  280. FPastedKnownHosts.reset(Configuration->SelectKnownHostsSessionsForImport(Lines.get(), StoredSessions, Error));
  281. FSessionListsList->Items[Index] = FPastedKnownHosts.get();
  282. FErrors->Strings[Index] = Error;
  283. LoadSessions();
  284. }
  285. //---------------------------------------------------------------------------