FileSystemInfo.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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. #ifndef NO_RESOURCES
  17. #pragma resource "*.dfm"
  18. #endif
  19. //---------------------------------------------------------------------
  20. void __fastcall DoFileSystemInfoDialog(
  21. const TSessionInfo & SessionInfo, const TFileSystemInfo & FileSystemInfo,
  22. UnicodeString SpaceAvailablePath, TGetSpaceAvailable OnGetSpaceAvailable)
  23. {
  24. TFileSystemInfoDialog * Dialog = new TFileSystemInfoDialog(Application,
  25. OnGetSpaceAvailable);
  26. try
  27. {
  28. Dialog->Execute(SessionInfo, FileSystemInfo, SpaceAvailablePath);
  29. }
  30. __finally
  31. {
  32. delete Dialog;
  33. }
  34. }
  35. //---------------------------------------------------------------------
  36. __fastcall TFileSystemInfoDialog::TFileSystemInfoDialog(TComponent * AOwner,
  37. TGetSpaceAvailable OnGetSpaceAvailable)
  38. : TForm(AOwner)
  39. {
  40. UseSystemSettings(this);
  41. FOnGetSpaceAvailable = OnGetSpaceAvailable;
  42. FSpaceAvailableLoaded = false;
  43. FLastListItem = 0;
  44. CertificateGroup->Top = HostKeyGroup->Top;
  45. ReadOnlyControl(HostKeyAlgorithmEdit);
  46. ReadOnlyControl(HostKeyFingerprintSHA256Edit);
  47. ReadOnlyControl(HostKeyFingerprintMD5Edit);
  48. ReadOnlyControl(CertificateFingerprintEdit);
  49. ReadOnlyControl(InfoMemo);
  50. }
  51. //---------------------------------------------------------------------
  52. void __fastcall TFileSystemInfoDialog::Execute(const TSessionInfo & SessionInfo,
  53. const TFileSystemInfo & FileSystemInfo, UnicodeString SpaceAvailablePath)
  54. {
  55. FSessionInfo = SessionInfo;
  56. FFileSystemInfo = FileSystemInfo;
  57. SpaceAvailablePathEdit->Text = SpaceAvailablePath;
  58. UpdateControls();
  59. ShowModal();
  60. }
  61. //---------------------------------------------------------------------
  62. UnicodeString __fastcall TFileSystemInfoDialog::CapabilityStr(TFSCapability Capability)
  63. {
  64. return BooleanToStr(FFileSystemInfo.IsCapable[Capability]);
  65. }
  66. //---------------------------------------------------------------------
  67. UnicodeString __fastcall TFileSystemInfoDialog::CapabilityStr(TFSCapability Capability1,
  68. TFSCapability Capability2)
  69. {
  70. return FORMAT(L"%s/%s", (CapabilityStr(Capability1), CapabilityStr(Capability2)));
  71. }
  72. //---------------------------------------------------------------------
  73. UnicodeString __fastcall TFileSystemInfoDialog::SpaceStr(__int64 Bytes)
  74. {
  75. UnicodeString Result;
  76. if (Bytes == 0)
  77. {
  78. Result = LoadStr(FSINFO_BYTES_UNKNOWN);
  79. }
  80. else
  81. {
  82. Result = FormatBytes(Bytes);
  83. UnicodeString SizeUnorderedStr = FormatBytes(Bytes, fbNone);
  84. if (Result != SizeUnorderedStr)
  85. {
  86. Result = FORMAT(L"%s (%s)", (Result, SizeUnorderedStr));
  87. }
  88. }
  89. return Result;
  90. }
  91. //---------------------------------------------------------------------
  92. void __fastcall TFileSystemInfoDialog::Feed(TFeedFileSystemData AddItem)
  93. {
  94. AddItem(ServerView, FSINFO_REMOTE_SYSTEM, FFileSystemInfo.RemoteSystem);
  95. AddItem(ServerView, FSINFO_FS_PROTOCOL, FFileSystemInfo.ProtocolName);
  96. AddItem(ServerView, FSINFO_CRYPTOGRAPHIC_PROTOCOL, FSessionInfo.SecurityProtocolName);
  97. AddItem(ServerView, FSINFO_SSH_IMPLEMENTATION, FSessionInfo.SshImplementation);
  98. UnicodeString Str = FSessionInfo.CSCipher;
  99. if (FSessionInfo.CSCipher != FSessionInfo.SCCipher)
  100. {
  101. Str += FORMAT(L"/%s", (FSessionInfo.SCCipher));
  102. }
  103. AddItem(ServerView, FSINFO_CIPHER, Str);
  104. Str = DefaultStr(FSessionInfo.CSCompression, LoadStr(NO_STR));
  105. if (FSessionInfo.CSCompression != FSessionInfo.SCCompression)
  106. {
  107. Str += FORMAT(L"/%s", (DefaultStr(FSessionInfo.SCCompression, LoadStr(NO_STR))));
  108. }
  109. AddItem(ServerView, FSINFO_COMPRESSION, Str);
  110. AddItem(HostKeyFingerprintSHA256Edit, 0, FSessionInfo.HostKeyFingerprintSHA256);
  111. AddItem(HostKeyFingerprintMD5Edit, 0, FSessionInfo.HostKeyFingerprintMD5);
  112. AddItem(CertificateFingerprintEdit, 0, FSessionInfo.CertificateFingerprint);
  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 == CertificateFingerprintEdit)
  160. {
  161. EnableControl(CertificateGroup, !Value.IsEmpty());
  162. CertificateGroup->Visible = !Value.IsEmpty();
  163. CertificateFingerprintEdit->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 = StripHotkey(Label->Caption);
  248. if (EndsStr(L":", S))
  249. {
  250. S.SetLength(S.Length() - 1);
  251. }
  252. FClipboard += FORMAT(L"%s = ", (S));
  253. }
  254. }
  255. if (EndsStr(L"\r\n", Value))
  256. {
  257. Value.SetLength(Value.Length() - 2);
  258. }
  259. FClipboard += FORMAT(L"%s\r\n", (Value));
  260. }
  261. else
  262. {
  263. DebugAssert(dynamic_cast<TListView *>(Control) != NULL);
  264. FClipboard += FORMAT(L"%s = %s\r\n", (LoadStr(Label), Value));
  265. }
  266. }
  267. }
  268. //---------------------------------------------------------------------------
  269. void __fastcall TFileSystemInfoDialog::ClipboardButtonClick(
  270. TObject * /*Sender*/)
  271. {
  272. TInstantOperationVisualizer Visualizer;
  273. NeedSpaceAvailable();
  274. FLastFeededControl = NULL;
  275. FClipboard = L"";
  276. Feed(ClipboardAddItem);
  277. CopyToClipboard(FClipboard);
  278. }
  279. //---------------------------------------------------------------------------
  280. void __fastcall TFileSystemInfoDialog::CopyClick(TObject * Sender)
  281. {
  282. TInstantOperationVisualizer Visualizer;
  283. TListView * ListView = dynamic_cast<TListView *>(GetPopupComponent(Sender));
  284. DebugAssert(ListView != NULL);
  285. UnicodeString Text;
  286. for (int Index = 0; Index < ListView->Items->Count; Index++)
  287. {
  288. TListItem * Item = ListView->Items->Item[Index];
  289. if (Item->Selected)
  290. {
  291. Text += FORMAT(L"%s = %s\r\n", (Item->Caption, Item->SubItems->Strings[0]));
  292. }
  293. }
  294. CopyToClipboard(Text);
  295. }
  296. //---------------------------------------------------------------------------
  297. void __fastcall TFileSystemInfoDialog::FormShow(TObject * /*Sender*/)
  298. {
  299. InstallPathWordBreakProc(SpaceAvailablePathEdit);
  300. PageControl->ActivePage = ProtocolSheet;
  301. FeedControls();
  302. }
  303. //---------------------------------------------------------------------------
  304. void __fastcall TFileSystemInfoDialog::SpaceAvailableButtonClick(
  305. TObject * /*Sender*/)
  306. {
  307. CheckSpaceAvailable();
  308. }
  309. //---------------------------------------------------------------------------
  310. void __fastcall TFileSystemInfoDialog::CheckSpaceAvailable()
  311. {
  312. DebugAssert(FOnGetSpaceAvailable != NULL);
  313. DebugAssert(!SpaceAvailablePathEdit->Text.IsEmpty());
  314. FSpaceAvailableLoaded = true;
  315. bool DoClose = false;
  316. try
  317. {
  318. FOnGetSpaceAvailable(SpaceAvailablePathEdit->Text, FSpaceAvailable, DoClose);
  319. }
  320. __finally
  321. {
  322. FeedControls();
  323. if (DoClose)
  324. {
  325. Close();
  326. }
  327. }
  328. }
  329. //---------------------------------------------------------------------------
  330. void __fastcall TFileSystemInfoDialog::NeedSpaceAvailable()
  331. {
  332. if (!FSpaceAvailableLoaded && SpaceAvailableSupported())
  333. {
  334. CheckSpaceAvailable();
  335. }
  336. }
  337. //---------------------------------------------------------------------------
  338. bool __fastcall TFileSystemInfoDialog::SpaceAvailableSupported()
  339. {
  340. return (FOnGetSpaceAvailable != NULL);
  341. }
  342. //---------------------------------------------------------------------------
  343. void __fastcall TFileSystemInfoDialog::PageControlChange(TObject * /*Sender*/)
  344. {
  345. if (PageControl->ActivePage == SpaceAvailableSheet)
  346. {
  347. NeedSpaceAvailable();
  348. }
  349. }
  350. //---------------------------------------------------------------------------
  351. void __fastcall TFileSystemInfoDialog::ControlChange(TObject * /*Sender*/)
  352. {
  353. UpdateControls();
  354. }
  355. //---------------------------------------------------------------------------
  356. void __fastcall TFileSystemInfoDialog::SpaceAvailablePathEditEnter(
  357. TObject * /*Sender*/)
  358. {
  359. SpaceAvailableButton->Default = true;
  360. CloseButton->Default = false;
  361. }
  362. //---------------------------------------------------------------------------
  363. void __fastcall TFileSystemInfoDialog::SpaceAvailablePathEditExit(
  364. TObject * /*Sender*/)
  365. {
  366. SpaceAvailableButton->Default = false;
  367. CloseButton->Default = true;
  368. }
  369. //---------------------------------------------------------------------------
  370. void __fastcall TFileSystemInfoDialog::ControlContextPopup(
  371. TObject * Sender, TPoint & MousePos, bool & Handled)
  372. {
  373. MenuPopup(Sender, MousePos, Handled);
  374. }
  375. //---------------------------------------------------------------------------
  376. void __fastcall TFileSystemInfoDialog::CertificateViewButtonClick(
  377. TObject * /*Sender*/)
  378. {
  379. MessageDialog(FSessionInfo.Certificate, qtInformation, qaOK);
  380. }
  381. //---------------------------------------------------------------------------
  382. void __fastcall TFileSystemInfoDialog::SpaceAvailableViewCustomDrawItem(
  383. TCustomListView * Sender, TListItem * Item, TCustomDrawState /*State*/, bool & /*DefaultDraw*/)
  384. {
  385. if ((Item->SubItems->Count >= 1) && (Item->SubItems->Strings[0] == LoadStr(FSINFO_BYTES_UNKNOWN)))
  386. {
  387. Sender->Canvas->Font->Color = clGrayText;
  388. }
  389. }
  390. //---------------------------------------------------------------------------
  391. void __fastcall TFileSystemInfoDialog::EditCopyActionExecute(TObject * /*Sender*/)
  392. {
  393. TEdit * Edit = HostKeyFingerprintSHA256Edit->Focused() ? HostKeyFingerprintSHA256Edit : HostKeyFingerprintMD5Edit;
  394. if ((Edit->SelLength == 0) || (Edit->SelLength == Edit->Text.Length()))
  395. {
  396. CopyToClipboard(FORMAT(L"%s %s", (HostKeyAlgorithmEdit->Text, Edit->Text)));
  397. }
  398. else
  399. {
  400. Edit->CopyToClipboard();
  401. }
  402. }
  403. //---------------------------------------------------------------------------
  404. void __fastcall TFileSystemInfoDialog::HostKeyFingerprintSHA256EditContextPopup(
  405. TObject * Sender, TPoint & MousePos, bool & Handled)
  406. {
  407. MenuPopup(Sender, MousePos, Handled);
  408. }
  409. //---------------------------------------------------------------------------
  410. void __fastcall TFileSystemInfoDialog::EditCopyActionUpdate(TObject * /*Sender*/)
  411. {
  412. // When noting is selected, we copy whole key, including algorithm, see EditCopyActionExecute
  413. EditCopyAction->Enabled = true;
  414. }
  415. //---------------------------------------------------------------------------