ImportSessions.cpp 11 KB

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