FileSystemInfo.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. //---------------------------------------------------------------------
  2. #include <FormsPCH.h>
  3. #pragma hdrstop
  4. #include <Terminal.h>
  5. #include "FileSystemInfo.h"
  6. //---------------------------------------------------------------------
  7. #pragma link "HistoryComboBox"
  8. #pragma resource "*.dfm"
  9. //---------------------------------------------------------------------
  10. void __fastcall DoFileSystemInfoDialog(
  11. const TSessionInfo & SessionInfo, const TFileSystemInfo & FileSystemInfo,
  12. UnicodeString SpaceAvailablePath, TGetSpaceAvailable OnGetSpaceAvailable)
  13. {
  14. TFileSystemInfoDialog * Dialog = new TFileSystemInfoDialog(Application,
  15. OnGetSpaceAvailable);
  16. try
  17. {
  18. Dialog->Execute(SessionInfo, FileSystemInfo, SpaceAvailablePath);
  19. }
  20. __finally
  21. {
  22. delete Dialog;
  23. }
  24. }
  25. //---------------------------------------------------------------------
  26. __fastcall TFileSystemInfoDialog::TFileSystemInfoDialog(TComponent * AOwner,
  27. TGetSpaceAvailable OnGetSpaceAvailable)
  28. : TForm(AOwner)
  29. {
  30. UseSystemSettings(this);
  31. FOnGetSpaceAvailable = OnGetSpaceAvailable;
  32. FSpaceAvailableLoaded = false;
  33. FLastListItem = 0;
  34. CertificateGroup->Top = HostKeyGroup->Top;
  35. ReadOnlyControl(HostKeyAlgorithmEdit);
  36. ReadOnlyControl(HostKeyFingerprintSHA256Edit);
  37. ReadOnlyControl(HostKeyFingerprintMD5Edit);
  38. ReadOnlyControl(CertificateFingerprintSha256Edit);
  39. ReadOnlyControl(CertificateFingerprintSha1Edit);
  40. ReadOnlyControl(InfoMemo);
  41. }
  42. //---------------------------------------------------------------------
  43. void __fastcall TFileSystemInfoDialog::Execute(const TSessionInfo & SessionInfo,
  44. const TFileSystemInfo & FileSystemInfo, UnicodeString SpaceAvailablePath)
  45. {
  46. FSessionInfo = SessionInfo;
  47. FFileSystemInfo = FileSystemInfo;
  48. SpaceAvailablePathEdit->Text = SpaceAvailablePath;
  49. UpdateControls();
  50. ShowModal();
  51. }
  52. //---------------------------------------------------------------------
  53. UnicodeString __fastcall TFileSystemInfoDialog::CapabilityStr(TFSCapability Capability)
  54. {
  55. return BooleanToStr(FFileSystemInfo.IsCapable[Capability]);
  56. }
  57. //---------------------------------------------------------------------
  58. UnicodeString __fastcall TFileSystemInfoDialog::CapabilityStr(TFSCapability Capability1,
  59. TFSCapability Capability2)
  60. {
  61. return FORMAT(L"%s/%s", (CapabilityStr(Capability1), CapabilityStr(Capability2)));
  62. }
  63. //---------------------------------------------------------------------
  64. UnicodeString __fastcall TFileSystemInfoDialog::SpaceStr(__int64 Bytes)
  65. {
  66. UnicodeString Result;
  67. if (Bytes == 0)
  68. {
  69. Result = LoadStr(FSINFO_BYTES_UNKNOWN);
  70. }
  71. else
  72. {
  73. Result = FormatBytes(Bytes);
  74. UnicodeString SizeUnorderedStr = FormatBytes(Bytes, fbNone);
  75. if (Result != SizeUnorderedStr)
  76. {
  77. Result = FORMAT(L"%s (%s)", (Result, SizeUnorderedStr));
  78. }
  79. }
  80. return Result;
  81. }
  82. //---------------------------------------------------------------------
  83. void __fastcall TFileSystemInfoDialog::Feed(TFeedFileSystemData AddItem)
  84. {
  85. AddItem(ServerView, FSINFO_REMOTE_SYSTEM, FFileSystemInfo.RemoteSystem);
  86. AddItem(ServerView, FSINFO_FS_PROTOCOL, FFileSystemInfo.ProtocolName);
  87. AddItem(ServerView, FSINFO_CRYPTOGRAPHIC_PROTOCOL, FSessionInfo.SecurityProtocolName);
  88. AddItem(ServerView, FSINFO_SSH_IMPLEMENTATION, FSessionInfo.SshImplementation);
  89. UnicodeString Str = FSessionInfo.CSCipher;
  90. if (FSessionInfo.CSCipher != FSessionInfo.SCCipher)
  91. {
  92. Str += FORMAT(L"/%s", (FSessionInfo.SCCipher));
  93. }
  94. AddItem(ServerView, FSINFO_CIPHER, Str);
  95. Str = DefaultStr(FSessionInfo.CSCompression, LoadStr(NO_STR));
  96. if (FSessionInfo.CSCompression != FSessionInfo.SCCompression)
  97. {
  98. Str += FORMAT(L"/%s", (DefaultStr(FSessionInfo.SCCompression, LoadStr(NO_STR))));
  99. }
  100. AddItem(ServerView, FSINFO_COMPRESSION, Str);
  101. AddItem(HostKeyFingerprintSHA256Edit, 0, FSessionInfo.HostKeyFingerprintSHA256);
  102. AddItem(HostKeyFingerprintMD5Edit, 0, FSessionInfo.HostKeyFingerprintMD5);
  103. AddItem(CertificateFingerprintSha256Edit, 0, FSessionInfo.CertificateFingerprintSHA256);
  104. AddItem(CertificateFingerprintSha1Edit, 0, FSessionInfo.CertificateFingerprintSHA1);
  105. AddItem(ProtocolView, FSINFO_MODE_CHANGING, CapabilityStr(fcModeChanging));
  106. AddItem(ProtocolView, FSINFO_ACL_CHANGING, CapabilityStr(fcAclChangingFiles));
  107. AddItem(ProtocolView, FSINFO_OWNER_GROUP_CHANGING, CapabilityStr(fcGroupChanging));
  108. UnicodeString AnyCommand;
  109. if (!FFileSystemInfo.IsCapable[fcShellAnyCommand] &&
  110. FFileSystemInfo.IsCapable[fcAnyCommand])
  111. {
  112. AnyCommand = LoadStr(FSINFO_PROTOCOL_ANY_COMMAND);
  113. }
  114. else
  115. {
  116. AnyCommand = CapabilityStr(fcAnyCommand);
  117. }
  118. AddItem(ProtocolView, FSINFO_ANY_COMMAND, AnyCommand);
  119. AddItem(ProtocolView, FSINFO_SYMBOLIC_HARD_LINK, CapabilityStr(fcSymbolicLink, fcHardLink));
  120. AddItem(ProtocolView, FSINFO_USER_GROUP_LISTING, CapabilityStr(fcUserGroupListing));
  121. AddItem(ProtocolView, FSINFO_REMOTE_COPY, CapabilityStr(fcRemoteCopy));
  122. AddItem(ProtocolView, FSINFO_CHECKING_SPACE_AVAILABLE, CapabilityStr(fcCheckingSpaceAvailable));
  123. AddItem(ProtocolView, FSINFO_CALCULATING_CHECKSUM, CapabilityStr(fcCalculatingChecksum));
  124. AddItem(ProtocolView, FSINFO_NATIVE_TEXT_MODE, CapabilityStr(fcNativeTextMode));
  125. AddItem(InfoMemo, 0, FFileSystemInfo.AdditionalInfo);
  126. AddItem(SpaceAvailableView, FSINFO_BYTES_ON_DEVICE, SpaceStr(FSpaceAvailable.BytesOnDevice));
  127. AddItem(SpaceAvailableView, FSINFO_UNUSED_BYTES_ON_DEVICE, SpaceStr(FSpaceAvailable.UnusedBytesOnDevice));
  128. AddItem(SpaceAvailableView, FSINFO_BYTES_AVAILABLE_TO_USER, SpaceStr(FSpaceAvailable.BytesAvailableToUser));
  129. AddItem(SpaceAvailableView, FSINFO_UNUSED_BYTES_AVAILABLE_TO_USER, SpaceStr(FSpaceAvailable.UnusedBytesAvailableToUser));
  130. AddItem(SpaceAvailableView, FSINFO_BYTES_PER_ALLOCATION_UNIT, SpaceStr(FSpaceAvailable.BytesPerAllocationUnit));
  131. }
  132. //---------------------------------------------------------------------
  133. void __fastcall TFileSystemInfoDialog::ControlsAddItem(TControl * Control,
  134. int Label, UnicodeString Value)
  135. {
  136. if (FLastFeededControl != Control)
  137. {
  138. // TODO, we should clear excess list view items here, but it
  139. // actually should not happen as of now
  140. FLastFeededControl = Control;
  141. FLastListItem = 0;
  142. }
  143. if ((Control == HostKeyFingerprintSHA256Edit) || (Control == HostKeyFingerprintMD5Edit))
  144. {
  145. EnableControl(HostKeyGroup, !Value.IsEmpty());
  146. HostKeyGroup->Visible = !Value.IsEmpty();
  147. UnicodeString Alg1 = CutToChar(Value, L' ', true);
  148. UnicodeString Alg2 = CutToChar(Value, L' ', true);
  149. HostKeyAlgorithmEdit->Text = FORMAT(L"%s %s", (Alg1, Alg2));
  150. DebugNotNull(dynamic_cast<TEdit *>(Control))->Text = Value;
  151. }
  152. else if ((Control == CertificateFingerprintSha256Edit) || (Control == CertificateFingerprintSha1Edit))
  153. {
  154. EnableControl(CertificateGroup, !Value.IsEmpty());
  155. CertificateGroup->Visible = !Value.IsEmpty();
  156. DebugNotNull(dynamic_cast<TEdit *>(Control))->Text = Value;
  157. }
  158. else if (Control == InfoMemo)
  159. {
  160. EnableControl(InfoGroup, !Value.IsEmpty());
  161. InfoGroup->Visible = !Value.IsEmpty();
  162. InfoMemo->Lines->Text = Value;
  163. }
  164. else
  165. {
  166. TListView * ListView = dynamic_cast<TListView *>(Control);
  167. DebugAssert(ListView != NULL);
  168. if (!Value.IsEmpty())
  169. {
  170. TListItem * Item;
  171. if (ListView->Items->Count > FLastListItem)
  172. {
  173. Item = ListView->Items->Item[FLastListItem];
  174. }
  175. else
  176. {
  177. Item = ListView->Items->Add();
  178. }
  179. FLastListItem++;
  180. Item->Caption = LoadStr(Label);
  181. if (Item->SubItems->Count > 0)
  182. {
  183. Item->SubItems->Strings[0] = Value;
  184. }
  185. else
  186. {
  187. Item->SubItems->Add(Value);
  188. }
  189. }
  190. }
  191. }
  192. //---------------------------------------------------------------------
  193. void __fastcall TFileSystemInfoDialog::FeedControls()
  194. {
  195. FLastFeededControl = NULL;
  196. Feed(ControlsAddItem);
  197. AutoSizeListColumnsWidth(ServerView, 1);
  198. AutoSizeListColumnsWidth(ProtocolView, 1);
  199. AutoSizeListColumnsWidth(SpaceAvailableView, 1);
  200. }
  201. //---------------------------------------------------------------------
  202. void __fastcall TFileSystemInfoDialog::UpdateControls()
  203. {
  204. EnableControl(SpaceAvailableSheet, SpaceAvailableSupported());
  205. EnableControl(SpaceAvailableButton, SpaceAvailableSheet->Enabled &&
  206. !SpaceAvailablePathEdit->Text.IsEmpty());
  207. }
  208. //---------------------------------------------------------------------
  209. void __fastcall TFileSystemInfoDialog::HelpButtonClick(TObject * /*Sender*/)
  210. {
  211. FormHelp(this);
  212. }
  213. //---------------------------------------------------------------------
  214. void __fastcall TFileSystemInfoDialog::ClipboardAddItem(TControl * Control,
  215. int Label, UnicodeString Value)
  216. {
  217. if (Control->Enabled && !Value.IsEmpty())
  218. {
  219. TGroupBox * Group = dynamic_cast<TGroupBox *>(Control->Parent);
  220. TControl * ControlGroup = (Group != NULL) ? Group : Control;
  221. if (FLastFeededControl != ControlGroup)
  222. {
  223. if (FLastFeededControl != NULL)
  224. {
  225. FClipboard += UnicodeString::StringOfChar(L'-', 60) + L"\r\n";
  226. }
  227. if (Group != NULL)
  228. {
  229. FClipboard += FORMAT(L"%s\r\n", (Group->Caption));
  230. }
  231. FLastFeededControl = ControlGroup;
  232. }
  233. if (dynamic_cast<TListView *>(Control) == NULL)
  234. {
  235. for (int Index = 0; Index < Control->Parent->ControlCount; Index++)
  236. {
  237. TLabel * Label = dynamic_cast<TLabel *>(Control->Parent->Controls[Index]);
  238. if ((Label != NULL) && (Label->FocusControl == Control))
  239. {
  240. UnicodeString S = RemoveSuffix(StripHotkey(Label->Caption), L":");
  241. FClipboard += FORMAT(L"%s = ", (S));
  242. }
  243. }
  244. Value = RemoveSuffix(Value, L"\r\n");
  245. FClipboard += FORMAT(L"%s\r\n", (Value));
  246. }
  247. else
  248. {
  249. DebugAssert(dynamic_cast<TListView *>(Control) != NULL);
  250. FClipboard += FORMAT(L"%s = %s\r\n", (LoadStr(Label), Value));
  251. }
  252. }
  253. }
  254. //---------------------------------------------------------------------------
  255. void __fastcall TFileSystemInfoDialog::ClipboardButtonClick(
  256. TObject * /*Sender*/)
  257. {
  258. TInstantOperationVisualizer Visualizer;
  259. NeedSpaceAvailable();
  260. FLastFeededControl = NULL;
  261. FClipboard = L"";
  262. Feed(ClipboardAddItem);
  263. CopyToClipboard(FClipboard);
  264. }
  265. //---------------------------------------------------------------------------
  266. void __fastcall TFileSystemInfoDialog::CopyClick(TObject * Sender)
  267. {
  268. TInstantOperationVisualizer Visualizer;
  269. TListView * ListView = dynamic_cast<TListView *>(GetPopupComponent(Sender));
  270. DebugAssert(ListView != NULL);
  271. UnicodeString Text;
  272. for (int Index = 0; Index < ListView->Items->Count; Index++)
  273. {
  274. TListItem * Item = ListView->Items->Item[Index];
  275. if (Item->Selected)
  276. {
  277. Text += FORMAT(L"%s = %s\r\n", (Item->Caption, Item->SubItems->Strings[0]));
  278. }
  279. }
  280. CopyToClipboard(Text);
  281. }
  282. //---------------------------------------------------------------------------
  283. void __fastcall TFileSystemInfoDialog::FormShow(TObject * /*Sender*/)
  284. {
  285. InstallPathWordBreakProc(SpaceAvailablePathEdit);
  286. PageControl->ActivePage = ProtocolSheet;
  287. FeedControls();
  288. }
  289. //---------------------------------------------------------------------------
  290. void __fastcall TFileSystemInfoDialog::SpaceAvailableButtonClick(
  291. TObject * /*Sender*/)
  292. {
  293. CheckSpaceAvailable();
  294. }
  295. //---------------------------------------------------------------------------
  296. void __fastcall TFileSystemInfoDialog::CheckSpaceAvailable()
  297. {
  298. DebugAssert(FOnGetSpaceAvailable != NULL);
  299. DebugAssert(!SpaceAvailablePathEdit->Text.IsEmpty());
  300. FSpaceAvailableLoaded = true;
  301. bool DoClose = false;
  302. try
  303. {
  304. FOnGetSpaceAvailable(SpaceAvailablePathEdit->Text, FSpaceAvailable, DoClose);
  305. }
  306. __finally
  307. {
  308. FeedControls();
  309. if (DoClose)
  310. {
  311. Close();
  312. }
  313. }
  314. }
  315. //---------------------------------------------------------------------------
  316. void __fastcall TFileSystemInfoDialog::NeedSpaceAvailable()
  317. {
  318. if (!FSpaceAvailableLoaded && SpaceAvailableSupported())
  319. {
  320. CheckSpaceAvailable();
  321. }
  322. }
  323. //---------------------------------------------------------------------------
  324. bool __fastcall TFileSystemInfoDialog::SpaceAvailableSupported()
  325. {
  326. return (FOnGetSpaceAvailable != NULL);
  327. }
  328. //---------------------------------------------------------------------------
  329. void __fastcall TFileSystemInfoDialog::PageControlChange(TObject * /*Sender*/)
  330. {
  331. if (PageControl->ActivePage == SpaceAvailableSheet)
  332. {
  333. NeedSpaceAvailable();
  334. }
  335. }
  336. //---------------------------------------------------------------------------
  337. void __fastcall TFileSystemInfoDialog::ControlChange(TObject * /*Sender*/)
  338. {
  339. UpdateControls();
  340. }
  341. //---------------------------------------------------------------------------
  342. void __fastcall TFileSystemInfoDialog::SpaceAvailablePathEditEnter(
  343. TObject * /*Sender*/)
  344. {
  345. SpaceAvailableButton->Default = true;
  346. CloseButton->Default = false;
  347. }
  348. //---------------------------------------------------------------------------
  349. void __fastcall TFileSystemInfoDialog::SpaceAvailablePathEditExit(
  350. TObject * /*Sender*/)
  351. {
  352. SpaceAvailableButton->Default = false;
  353. CloseButton->Default = true;
  354. }
  355. //---------------------------------------------------------------------------
  356. void __fastcall TFileSystemInfoDialog::ControlContextPopup(
  357. TObject * Sender, TPoint & MousePos, bool & Handled)
  358. {
  359. MenuPopup(Sender, MousePos, Handled);
  360. }
  361. //---------------------------------------------------------------------------
  362. void __fastcall TFileSystemInfoDialog::CertificateViewButtonClick(
  363. TObject * /*Sender*/)
  364. {
  365. MessageDialog(FSessionInfo.Certificate, qtInformation, qaOK);
  366. }
  367. //---------------------------------------------------------------------------
  368. void __fastcall TFileSystemInfoDialog::SpaceAvailableViewCustomDrawItem(
  369. TCustomListView * Sender, TListItem * Item, TCustomDrawState /*State*/, bool & /*DefaultDraw*/)
  370. {
  371. if ((Item->SubItems->Count >= 1) && (Item->SubItems->Strings[0] == LoadStr(FSINFO_BYTES_UNKNOWN)))
  372. {
  373. Sender->Canvas->Font->Color = clGrayText;
  374. }
  375. }
  376. //---------------------------------------------------------------------------
  377. void __fastcall TFileSystemInfoDialog::EditCopyActionExecute(TObject * /*Sender*/)
  378. {
  379. TEdit * Edit = HostKeyFingerprintSHA256Edit->Focused() ? HostKeyFingerprintSHA256Edit : HostKeyFingerprintMD5Edit;
  380. if ((Edit->SelLength == 0) || (Edit->SelLength == Edit->Text.Length()))
  381. {
  382. CopyToClipboard(FORMAT(L"%s %s", (HostKeyAlgorithmEdit->Text, Edit->Text)));
  383. }
  384. else
  385. {
  386. Edit->CopyToClipboard();
  387. }
  388. }
  389. //---------------------------------------------------------------------------
  390. void __fastcall TFileSystemInfoDialog::HostKeyFingerprintSHA256EditContextPopup(
  391. TObject * Sender, TPoint & MousePos, bool & Handled)
  392. {
  393. MenuPopup(Sender, MousePos, Handled);
  394. }
  395. //---------------------------------------------------------------------------
  396. void __fastcall TFileSystemInfoDialog::EditCopyActionUpdate(TObject * /*Sender*/)
  397. {
  398. // When noting is selected, we copy whole key, including algorithm, see EditCopyActionExecute
  399. EditCopyAction->Enabled = true;
  400. }
  401. //---------------------------------------------------------------------------