FileSystemInfo.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. //---------------------------------------------------------------------
  14. #pragma link "HistoryComboBox"
  15. #ifndef NO_RESOURCES
  16. #pragma resource "*.dfm"
  17. #endif
  18. //---------------------------------------------------------------------
  19. void __fastcall DoFileSystemInfoDialog(
  20. const TSessionInfo & SessionInfo, const TFileSystemInfo & FileSystemInfo,
  21. UnicodeString SpaceAvailablePath, TGetSpaceAvailable OnGetSpaceAvailable)
  22. {
  23. TFileSystemInfoDialog * Dialog = new TFileSystemInfoDialog(Application,
  24. OnGetSpaceAvailable);
  25. try
  26. {
  27. Dialog->Execute(SessionInfo, FileSystemInfo, SpaceAvailablePath);
  28. }
  29. __finally
  30. {
  31. delete Dialog;
  32. }
  33. }
  34. //---------------------------------------------------------------------
  35. __fastcall TFileSystemInfoDialog::TFileSystemInfoDialog(TComponent * AOwner,
  36. TGetSpaceAvailable OnGetSpaceAvailable)
  37. : TForm(AOwner)
  38. {
  39. UseSystemSettings(this);
  40. FOnGetSpaceAvailable = OnGetSpaceAvailable;
  41. FSpaceAvailableLoaded = false;
  42. FLastListItem = 0;
  43. CertificateGroup->Top = HostKeyGroup->Top;
  44. ReadOnlyControl(HostKeyFingerprintEdit);
  45. ReadOnlyControl(CertificateFingerprintEdit);
  46. ReadOnlyControl(InfoMemo);
  47. }
  48. //---------------------------------------------------------------------
  49. void __fastcall TFileSystemInfoDialog::Execute(const TSessionInfo & SessionInfo,
  50. const TFileSystemInfo & FileSystemInfo, UnicodeString SpaceAvailablePath)
  51. {
  52. FSessionInfo = SessionInfo;
  53. FFileSystemInfo = FileSystemInfo;
  54. SpaceAvailablePathEdit->Text = SpaceAvailablePath;
  55. UpdateControls();
  56. ShowModal();
  57. }
  58. //---------------------------------------------------------------------
  59. UnicodeString __fastcall TFileSystemInfoDialog::CapabilityStr(TFSCapability Capability)
  60. {
  61. return BooleanToStr(FFileSystemInfo.IsCapable[Capability]);
  62. }
  63. //---------------------------------------------------------------------
  64. UnicodeString __fastcall TFileSystemInfoDialog::CapabilityStr(TFSCapability Capability1,
  65. TFSCapability Capability2)
  66. {
  67. return FORMAT(L"%s/%s", (CapabilityStr(Capability1), CapabilityStr(Capability2)));
  68. }
  69. //---------------------------------------------------------------------
  70. UnicodeString __fastcall TFileSystemInfoDialog::SpaceStr(__int64 Bytes)
  71. {
  72. UnicodeString Result;
  73. if (Bytes == 0)
  74. {
  75. Result = LoadStr(FSINFO_BYTES_UNKNOWN);
  76. }
  77. else
  78. {
  79. Result = FormatBytes(Bytes);
  80. UnicodeString SizeUnorderedStr = FormatBytes(Bytes, fbNone);
  81. if (Result != SizeUnorderedStr)
  82. {
  83. Result = FORMAT(L"%s (%s)", (Result, SizeUnorderedStr));
  84. }
  85. }
  86. return Result;
  87. }
  88. //---------------------------------------------------------------------
  89. void __fastcall TFileSystemInfoDialog::Feed(TFeedFileSystemData AddItem)
  90. {
  91. AddItem(ServerView, FSINFO_REMOTE_SYSTEM, FFileSystemInfo.RemoteSystem);
  92. AddItem(ServerView, FSINFO_FS_PROTOCOL, FFileSystemInfo.ProtocolName);
  93. AddItem(ServerView, FSINFO_CRYPTOGRAPHIC_PROTOCOL, FSessionInfo.SecurityProtocolName);
  94. AddItem(ServerView, FSINFO_SSH_IMPLEMENTATION, FSessionInfo.SshImplementation);
  95. UnicodeString Str = FSessionInfo.CSCipher;
  96. if (FSessionInfo.CSCipher != FSessionInfo.SCCipher)
  97. {
  98. Str += FORMAT(L"/%s", (FSessionInfo.SCCipher));
  99. }
  100. AddItem(ServerView, FSINFO_CIPHER, Str);
  101. Str = DefaultStr(FSessionInfo.CSCompression, LoadStr(NO_STR));
  102. if (FSessionInfo.CSCompression != FSessionInfo.SCCompression)
  103. {
  104. Str += FORMAT(L"/%s", (DefaultStr(FSessionInfo.SCCompression, LoadStr(NO_STR))));
  105. }
  106. AddItem(ServerView, FSINFO_COMPRESSION, Str);
  107. AddItem(HostKeyFingerprintEdit, 0, FSessionInfo.HostKeyFingerprint);
  108. AddItem(CertificateFingerprintEdit, 0, FSessionInfo.CertificateFingerprint);
  109. AddItem(ProtocolView, FSINFO_MODE_CHANGING, CapabilityStr(fcModeChanging));
  110. AddItem(ProtocolView, FSINFO_OWNER_GROUP_CHANGING, CapabilityStr(fcGroupChanging));
  111. UnicodeString AnyCommand;
  112. if (!FFileSystemInfo.IsCapable[fcShellAnyCommand] &&
  113. FFileSystemInfo.IsCapable[fcAnyCommand])
  114. {
  115. AnyCommand = LoadStr(FSINFO_PROTOCOL_ANY_COMMAND);
  116. }
  117. else
  118. {
  119. AnyCommand = CapabilityStr(fcAnyCommand);
  120. }
  121. AddItem(ProtocolView, FSINFO_ANY_COMMAND, AnyCommand);
  122. AddItem(ProtocolView, FSINFO_SYMBOLIC_HARD_LINK, CapabilityStr(fcSymbolicLink, fcHardLink));
  123. AddItem(ProtocolView, FSINFO_USER_GROUP_LISTING, CapabilityStr(fcUserGroupListing));
  124. AddItem(ProtocolView, FSINFO_REMOTE_COPY, CapabilityStr(fcRemoteCopy));
  125. AddItem(ProtocolView, FSINFO_CHECKING_SPACE_AVAILABLE, CapabilityStr(fcCheckingSpaceAvailable));
  126. AddItem(ProtocolView, FSINFO_CALCULATING_CHECKSUM, CapabilityStr(fcCalculatingChecksum));
  127. AddItem(ProtocolView, FSINFO_NATIVE_TEXT_MODE, CapabilityStr(fcNativeTextMode));
  128. AddItem(InfoMemo, 0, FFileSystemInfo.AdditionalInfo);
  129. AddItem(SpaceAvailableView, FSINFO_BYTES_ON_DEVICE, SpaceStr(FSpaceAvailable.BytesOnDevice));
  130. AddItem(SpaceAvailableView, FSINFO_UNUSED_BYTES_ON_DEVICE, SpaceStr(FSpaceAvailable.UnusedBytesOnDevice));
  131. AddItem(SpaceAvailableView, FSINFO_BYTES_AVAILABLE_TO_USER, SpaceStr(FSpaceAvailable.BytesAvailableToUser));
  132. AddItem(SpaceAvailableView, FSINFO_UNUSED_BYTES_AVAILABLE_TO_USER, SpaceStr(FSpaceAvailable.UnusedBytesAvailableToUser));
  133. AddItem(SpaceAvailableView, FSINFO_BYTES_PER_ALLOCATION_UNIT, SpaceStr(FSpaceAvailable.BytesPerAllocationUnit));
  134. }
  135. //---------------------------------------------------------------------
  136. void __fastcall TFileSystemInfoDialog::ControlsAddItem(TControl * Control,
  137. int Label, UnicodeString Value)
  138. {
  139. if (FLastFeededControl != Control)
  140. {
  141. // TODO, we should clear excess list view items here, but it
  142. // actually should not happen as of now
  143. FLastFeededControl = Control;
  144. FLastListItem = 0;
  145. }
  146. if (Control == HostKeyFingerprintEdit)
  147. {
  148. EnableControl(HostKeyGroup, !Value.IsEmpty());
  149. HostKeyGroup->Visible = !Value.IsEmpty();
  150. HostKeyFingerprintEdit->Text = Value;
  151. }
  152. else if (Control == CertificateFingerprintEdit)
  153. {
  154. EnableControl(CertificateGroup, !Value.IsEmpty());
  155. CertificateGroup->Visible = !Value.IsEmpty();
  156. CertificateFingerprintEdit->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. assert(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. if (FLastFeededControl != Control)
  220. {
  221. if (FLastFeededControl != NULL)
  222. {
  223. FClipboard += UnicodeString::StringOfChar(L'-', 60) + L"\r\n";
  224. }
  225. FLastFeededControl = Control;
  226. }
  227. if (dynamic_cast<TListView *>(Control) == NULL)
  228. {
  229. TGroupBox * Group = dynamic_cast<TGroupBox *>(Control->Parent);
  230. assert(Group != NULL);
  231. if ((Value.Length() >= 2) && (Value.SubString(Value.Length() - 1, 2) == L"\r\n"))
  232. {
  233. Value.SetLength(Value.Length() - 2);
  234. }
  235. FClipboard += FORMAT(L"%s\r\n%s\r\n", (Group->Caption, Value));
  236. }
  237. else
  238. {
  239. assert(dynamic_cast<TListView *>(Control) != NULL);
  240. FClipboard += FORMAT(L"%s = %s\r\n", (LoadStr(Label), Value));
  241. }
  242. }
  243. }
  244. //---------------------------------------------------------------------------
  245. void __fastcall TFileSystemInfoDialog::ClipboardButtonClick(
  246. TObject * /*Sender*/)
  247. {
  248. TInstantOperationVisualizer Visualizer;
  249. NeedSpaceAvailable();
  250. FLastFeededControl = NULL;
  251. FClipboard = L"";
  252. Feed(ClipboardAddItem);
  253. CopyToClipboard(FClipboard);
  254. }
  255. //---------------------------------------------------------------------------
  256. void __fastcall TFileSystemInfoDialog::CopyClick(TObject * Sender)
  257. {
  258. TInstantOperationVisualizer Visualizer;
  259. TListView * ListView = dynamic_cast<TListView *>(GetPopupComponent(Sender));
  260. assert(ListView != NULL);
  261. UnicodeString Text;
  262. for (int Index = 0; Index < ListView->Items->Count; Index++)
  263. {
  264. TListItem * Item = ListView->Items->Item[Index];
  265. if (Item->Selected)
  266. {
  267. Text += FORMAT(L"%s = %s\r\n", (Item->Caption, Item->SubItems->Strings[0]));
  268. }
  269. }
  270. CopyToClipboard(Text);
  271. }
  272. //---------------------------------------------------------------------------
  273. void __fastcall TFileSystemInfoDialog::FormShow(TObject * /*Sender*/)
  274. {
  275. InstallPathWordBreakProc(SpaceAvailablePathEdit);
  276. PageControl->ActivePage = ProtocolSheet;
  277. FeedControls();
  278. }
  279. //---------------------------------------------------------------------------
  280. void __fastcall TFileSystemInfoDialog::SpaceAvailableButtonClick(
  281. TObject * /*Sender*/)
  282. {
  283. CheckSpaceAvailable();
  284. }
  285. //---------------------------------------------------------------------------
  286. void __fastcall TFileSystemInfoDialog::CheckSpaceAvailable()
  287. {
  288. assert(FOnGetSpaceAvailable != NULL);
  289. assert(!SpaceAvailablePathEdit->Text.IsEmpty());
  290. FSpaceAvailableLoaded = true;
  291. bool DoClose = false;
  292. try
  293. {
  294. FOnGetSpaceAvailable(SpaceAvailablePathEdit->Text, FSpaceAvailable, DoClose);
  295. }
  296. __finally
  297. {
  298. FeedControls();
  299. if (DoClose)
  300. {
  301. Close();
  302. }
  303. }
  304. }
  305. //---------------------------------------------------------------------------
  306. void __fastcall TFileSystemInfoDialog::NeedSpaceAvailable()
  307. {
  308. if (!FSpaceAvailableLoaded && SpaceAvailableSupported())
  309. {
  310. CheckSpaceAvailable();
  311. }
  312. }
  313. //---------------------------------------------------------------------------
  314. bool __fastcall TFileSystemInfoDialog::SpaceAvailableSupported()
  315. {
  316. return (FOnGetSpaceAvailable != NULL);
  317. }
  318. //---------------------------------------------------------------------------
  319. void __fastcall TFileSystemInfoDialog::PageControlChange(TObject * /*Sender*/)
  320. {
  321. if (PageControl->ActivePage == SpaceAvailableSheet)
  322. {
  323. NeedSpaceAvailable();
  324. }
  325. }
  326. //---------------------------------------------------------------------------
  327. void __fastcall TFileSystemInfoDialog::ControlChange(TObject * /*Sender*/)
  328. {
  329. UpdateControls();
  330. }
  331. //---------------------------------------------------------------------------
  332. void __fastcall TFileSystemInfoDialog::SpaceAvailablePathEditEnter(
  333. TObject * /*Sender*/)
  334. {
  335. SpaceAvailableButton->Default = true;
  336. CloseButton->Default = false;
  337. }
  338. //---------------------------------------------------------------------------
  339. void __fastcall TFileSystemInfoDialog::SpaceAvailablePathEditExit(
  340. TObject * /*Sender*/)
  341. {
  342. SpaceAvailableButton->Default = false;
  343. CloseButton->Default = true;
  344. }
  345. //---------------------------------------------------------------------------
  346. void __fastcall TFileSystemInfoDialog::ControlContextPopup(
  347. TObject * Sender, TPoint & MousePos, bool & Handled)
  348. {
  349. MenuPopup(Sender, MousePos, Handled);
  350. }
  351. //---------------------------------------------------------------------------
  352. void __fastcall TFileSystemInfoDialog::CertificateViewButtonClick(
  353. TObject * /*Sender*/)
  354. {
  355. MessageDialog(FSessionInfo.Certificate, qtInformation, qaOK);
  356. }
  357. //---------------------------------------------------------------------------
  358. void __fastcall TFileSystemInfoDialog::SpaceAvailableViewCustomDrawItem(
  359. TCustomListView * Sender, TListItem * Item, TCustomDrawState /*State*/, bool & /*DefaultDraw*/)
  360. {
  361. if ((Item->SubItems->Count >= 1) && (Item->SubItems->Strings[0] == LoadStr(FSINFO_BYTES_UNKNOWN)))
  362. {
  363. Sender->Canvas->Font->Color = clGrayText;
  364. }
  365. }
  366. //---------------------------------------------------------------------------