FileSystemInfo.cpp 15 KB

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