FileSystemInfo.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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, false);
  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. }
  198. //---------------------------------------------------------------------
  199. void __fastcall TFileSystemInfoDialog::UpdateControls()
  200. {
  201. EnableControl(SpaceAvailableSheet, SpaceAvailableSupported());
  202. EnableControl(SpaceAvailableButton, SpaceAvailableSheet->Enabled &&
  203. !SpaceAvailablePathEdit->Text.IsEmpty());
  204. }
  205. //---------------------------------------------------------------------
  206. void __fastcall TFileSystemInfoDialog::HelpButtonClick(TObject * /*Sender*/)
  207. {
  208. FormHelp(this);
  209. }
  210. //---------------------------------------------------------------------
  211. void __fastcall TFileSystemInfoDialog::ClipboardAddItem(TControl * Control,
  212. int Label, UnicodeString Value)
  213. {
  214. if (Control->Enabled && !Value.IsEmpty())
  215. {
  216. if (FLastFeededControl != Control)
  217. {
  218. if (FLastFeededControl != NULL)
  219. {
  220. FClipboard += UnicodeString::StringOfChar(L'-', 60) + L"\r\n";
  221. }
  222. FLastFeededControl = Control;
  223. }
  224. if (dynamic_cast<TListView *>(Control) == NULL)
  225. {
  226. TGroupBox * Group = dynamic_cast<TGroupBox *>(Control->Parent);
  227. assert(Group != NULL);
  228. if ((Value.Length() >= 2) && (Value.SubString(Value.Length() - 1, 2) == L"\r\n"))
  229. {
  230. Value.SetLength(Value.Length() - 2);
  231. }
  232. FClipboard += FORMAT(L"%s\r\n%s\r\n", (Group->Caption, Value));
  233. }
  234. else
  235. {
  236. assert(dynamic_cast<TListView *>(Control) != NULL);
  237. FClipboard += FORMAT(L"%s = %s\r\n", (LoadStr(Label), Value));
  238. }
  239. }
  240. }
  241. //---------------------------------------------------------------------------
  242. void __fastcall TFileSystemInfoDialog::ClipboardButtonClick(
  243. TObject * /*Sender*/)
  244. {
  245. NeedSpaceAvailable();
  246. FLastFeededControl = NULL;
  247. FClipboard = L"";
  248. Feed(ClipboardAddItem);
  249. CopyToClipboard(FClipboard);
  250. }
  251. //---------------------------------------------------------------------------
  252. void __fastcall TFileSystemInfoDialog::CopyClick(TObject * Sender)
  253. {
  254. TComponent * Item = dynamic_cast<TComponent *>(Sender);
  255. assert(Item != NULL);
  256. TPopupMenu * PopupMenu = dynamic_cast<TPopupMenu *>(Item->Owner);
  257. assert(PopupMenu != NULL);
  258. TListView * ListView = dynamic_cast<TListView *>(PopupMenu->PopupComponent);
  259. assert(ListView != NULL);
  260. UnicodeString Text;
  261. for (int Index = 0; Index < ListView->Items->Count; Index++)
  262. {
  263. TListItem * Item = ListView->Items->Item[Index];
  264. if (Item->Selected)
  265. {
  266. Text += FORMAT(L"%s = %s\r\n", (Item->Caption, Item->SubItems->Strings[0]));
  267. }
  268. }
  269. CopyToClipboard(Text);
  270. }
  271. //---------------------------------------------------------------------------
  272. void __fastcall TFileSystemInfoDialog::FormShow(TObject * /*Sender*/)
  273. {
  274. InstallPathWordBreakProc(SpaceAvailablePathEdit);
  275. PageControl->ActivePage = ProtocolSheet;
  276. FeedControls();
  277. }
  278. //---------------------------------------------------------------------------
  279. void __fastcall TFileSystemInfoDialog::SpaceAvailableButtonClick(
  280. TObject * /*Sender*/)
  281. {
  282. CheckSpaceAvailable();
  283. }
  284. //---------------------------------------------------------------------------
  285. void __fastcall TFileSystemInfoDialog::CheckSpaceAvailable()
  286. {
  287. assert(FOnGetSpaceAvailable != NULL);
  288. assert(!SpaceAvailablePathEdit->Text.IsEmpty());
  289. FSpaceAvailableLoaded = true;
  290. bool DoClose = false;
  291. try
  292. {
  293. FOnGetSpaceAvailable(SpaceAvailablePathEdit->Text, FSpaceAvailable, DoClose);
  294. }
  295. __finally
  296. {
  297. FeedControls();
  298. if (DoClose)
  299. {
  300. Close();
  301. }
  302. }
  303. }
  304. //---------------------------------------------------------------------------
  305. void __fastcall TFileSystemInfoDialog::NeedSpaceAvailable()
  306. {
  307. if (!FSpaceAvailableLoaded && SpaceAvailableSupported())
  308. {
  309. CheckSpaceAvailable();
  310. }
  311. }
  312. //---------------------------------------------------------------------------
  313. bool __fastcall TFileSystemInfoDialog::SpaceAvailableSupported()
  314. {
  315. return (FOnGetSpaceAvailable != NULL);
  316. }
  317. //---------------------------------------------------------------------------
  318. void __fastcall TFileSystemInfoDialog::PageControlChange(TObject * /*Sender*/)
  319. {
  320. if (PageControl->ActivePage == SpaceAvailableSheet)
  321. {
  322. NeedSpaceAvailable();
  323. }
  324. }
  325. //---------------------------------------------------------------------------
  326. void __fastcall TFileSystemInfoDialog::ControlChange(TObject * /*Sender*/)
  327. {
  328. UpdateControls();
  329. }
  330. //---------------------------------------------------------------------------
  331. void __fastcall TFileSystemInfoDialog::SpaceAvailablePathEditEnter(
  332. TObject * /*Sender*/)
  333. {
  334. SpaceAvailableButton->Default = true;
  335. CloseButton->Default = false;
  336. }
  337. //---------------------------------------------------------------------------
  338. void __fastcall TFileSystemInfoDialog::SpaceAvailablePathEditExit(
  339. TObject * /*Sender*/)
  340. {
  341. SpaceAvailableButton->Default = false;
  342. CloseButton->Default = true;
  343. }
  344. //---------------------------------------------------------------------------
  345. void __fastcall TFileSystemInfoDialog::ControlContextPopup(
  346. TObject * Sender, TPoint & MousePos, bool & Handled)
  347. {
  348. MenuPopup(Sender, MousePos, Handled);
  349. }
  350. //---------------------------------------------------------------------------
  351. void __fastcall TFileSystemInfoDialog::CertificateViewButtonClick(
  352. TObject * /*Sender*/)
  353. {
  354. MessageDialog(FSessionInfo.Certificate, qtInformation, qaOK);
  355. }
  356. //---------------------------------------------------------------------------