1
0

FileSystemInfo.cpp 15 KB

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