FileSystemInfo.cpp 15 KB

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