FileSystemInfo.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. InstallPathWordBreakProc(SpaceAvailablePathEdit);
  45. ReadOnlyControl(HostKeyFingerprintEdit);
  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, false);
  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_SESSION_PROTOCOL, FSessionInfo.ProtocolName);
  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. if (FSessionInfo.ProtocolName != FFileSystemInfo.ProtocolName)
  108. {
  109. AddItem(ServerView, FSINFO_FS_PROTOCOL, FFileSystemInfo.ProtocolName);
  110. }
  111. AddItem(HostKeyFingerprintEdit, 0, FSessionInfo.HostKeyFingerprint);
  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 == HostKeyFingerprintEdit)
  151. {
  152. EnableControl(HostKeyGroup, !Value.IsEmpty());
  153. HostKeyGroup->Visible = !Value.IsEmpty();
  154. HostKeyFingerprintEdit->Text = Value;
  155. }
  156. else if (Control == CertificateFingerprintEdit)
  157. {
  158. EnableControl(CertificateGroup, !Value.IsEmpty());
  159. CertificateGroup->Visible = !Value.IsEmpty();
  160. CertificateFingerprintEdit->Text = Value;
  161. }
  162. else if (Control == InfoMemo)
  163. {
  164. EnableControl(InfoGroup, !Value.IsEmpty());
  165. InfoGroup->Visible = !Value.IsEmpty();
  166. InfoMemo->Lines->Text = Value;
  167. }
  168. else
  169. {
  170. TListView * ListView = dynamic_cast<TListView *>(Control);
  171. assert(ListView != NULL);
  172. if (!Value.IsEmpty())
  173. {
  174. TListItem * Item;
  175. if (ListView->Items->Count > FLastListItem)
  176. {
  177. Item = ListView->Items->Item[FLastListItem];
  178. }
  179. else
  180. {
  181. Item = ListView->Items->Add();
  182. }
  183. FLastListItem++;
  184. Item->Caption = LoadStr(Label);
  185. if (Item->SubItems->Count > 0)
  186. {
  187. Item->SubItems->Strings[0] = Value;
  188. }
  189. else
  190. {
  191. Item->SubItems->Add(Value);
  192. }
  193. }
  194. }
  195. }
  196. //---------------------------------------------------------------------
  197. void __fastcall TFileSystemInfoDialog::FeedControls()
  198. {
  199. FLastFeededControl = NULL;
  200. Feed(ControlsAddItem);
  201. }
  202. //---------------------------------------------------------------------
  203. void __fastcall TFileSystemInfoDialog::UpdateControls()
  204. {
  205. EnableControl(SpaceAvailableSheet, SpaceAvailableSupported());
  206. EnableControl(SpaceAvailableButton, SpaceAvailableSheet->Enabled &&
  207. !SpaceAvailablePathEdit->Text.IsEmpty());
  208. }
  209. //---------------------------------------------------------------------
  210. void __fastcall TFileSystemInfoDialog::HelpButtonClick(TObject * /*Sender*/)
  211. {
  212. FormHelp(this);
  213. }
  214. //---------------------------------------------------------------------
  215. void __fastcall TFileSystemInfoDialog::ClipboardAddItem(TControl * Control,
  216. int Label, UnicodeString Value)
  217. {
  218. if (Control->Enabled && !Value.IsEmpty())
  219. {
  220. if (FLastFeededControl != Control)
  221. {
  222. if (FLastFeededControl != NULL)
  223. {
  224. FClipboard += UnicodeString::StringOfChar(L'-', 60) + L"\r\n";
  225. }
  226. FLastFeededControl = Control;
  227. }
  228. if (dynamic_cast<TListView *>(Control) == NULL)
  229. {
  230. TGroupBox * Group = dynamic_cast<TGroupBox *>(Control->Parent);
  231. assert(Group != NULL);
  232. if ((Value.Length() >= 2) && (Value.SubString(Value.Length() - 1, 2) == L"\r\n"))
  233. {
  234. Value.SetLength(Value.Length() - 2);
  235. }
  236. FClipboard += FORMAT(L"%s\r\n%s\r\n", (Group->Caption, Value));
  237. }
  238. else
  239. {
  240. assert(dynamic_cast<TListView *>(Control) != NULL);
  241. FClipboard += FORMAT(L"%s = %s\r\n", (LoadStr(Label), Value));
  242. }
  243. }
  244. }
  245. //---------------------------------------------------------------------------
  246. void __fastcall TFileSystemInfoDialog::ClipboardButtonClick(
  247. TObject * /*Sender*/)
  248. {
  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. TComponent * Item = dynamic_cast<TComponent *>(Sender);
  259. assert(Item != NULL);
  260. TPopupMenu * PopupMenu = dynamic_cast<TPopupMenu *>(Item->Owner);
  261. assert(PopupMenu != NULL);
  262. TListView * ListView = dynamic_cast<TListView *>(PopupMenu->PopupComponent);
  263. assert(ListView != NULL);
  264. UnicodeString Text;
  265. for (int Index = 0; Index < ListView->Items->Count; Index++)
  266. {
  267. TListItem * Item = ListView->Items->Item[Index];
  268. if (Item->Selected)
  269. {
  270. Text += FORMAT(L"%s = %s\r\n", (Item->Caption, Item->SubItems->Strings[0]));
  271. }
  272. }
  273. CopyToClipboard(Text);
  274. }
  275. //---------------------------------------------------------------------------
  276. void __fastcall TFileSystemInfoDialog::FormShow(TObject * /*Sender*/)
  277. {
  278. PageControl->ActivePage = ProtocolSheet;
  279. FeedControls();
  280. }
  281. //---------------------------------------------------------------------------
  282. void __fastcall TFileSystemInfoDialog::SpaceAvailableButtonClick(
  283. TObject * /*Sender*/)
  284. {
  285. CheckSpaceAvailable();
  286. }
  287. //---------------------------------------------------------------------------
  288. void __fastcall TFileSystemInfoDialog::CheckSpaceAvailable()
  289. {
  290. assert(FOnGetSpaceAvailable != NULL);
  291. assert(!SpaceAvailablePathEdit->Text.IsEmpty());
  292. FSpaceAvailableLoaded = true;
  293. bool DoClose = false;
  294. try
  295. {
  296. FOnGetSpaceAvailable(SpaceAvailablePathEdit->Text, FSpaceAvailable, DoClose);
  297. }
  298. __finally
  299. {
  300. FeedControls();
  301. if (DoClose)
  302. {
  303. Close();
  304. }
  305. }
  306. }
  307. //---------------------------------------------------------------------------
  308. void __fastcall TFileSystemInfoDialog::NeedSpaceAvailable()
  309. {
  310. if (!FSpaceAvailableLoaded && SpaceAvailableSupported())
  311. {
  312. CheckSpaceAvailable();
  313. }
  314. }
  315. //---------------------------------------------------------------------------
  316. bool __fastcall TFileSystemInfoDialog::SpaceAvailableSupported()
  317. {
  318. return (FOnGetSpaceAvailable != NULL);
  319. }
  320. //---------------------------------------------------------------------------
  321. void __fastcall TFileSystemInfoDialog::PageControlChange(TObject * /*Sender*/)
  322. {
  323. if (PageControl->ActivePage == SpaceAvailableSheet)
  324. {
  325. NeedSpaceAvailable();
  326. }
  327. }
  328. //---------------------------------------------------------------------------
  329. void __fastcall TFileSystemInfoDialog::ControlChange(TObject * /*Sender*/)
  330. {
  331. UpdateControls();
  332. }
  333. //---------------------------------------------------------------------------
  334. void __fastcall TFileSystemInfoDialog::SpaceAvailablePathEditEnter(
  335. TObject * /*Sender*/)
  336. {
  337. SpaceAvailableButton->Default = true;
  338. CloseButton->Default = false;
  339. }
  340. //---------------------------------------------------------------------------
  341. void __fastcall TFileSystemInfoDialog::SpaceAvailablePathEditExit(
  342. TObject * /*Sender*/)
  343. {
  344. SpaceAvailableButton->Default = false;
  345. CloseButton->Default = true;
  346. }
  347. //---------------------------------------------------------------------------
  348. void __fastcall TFileSystemInfoDialog::ControlContextPopup(
  349. TObject * Sender, TPoint & MousePos, bool & Handled)
  350. {
  351. MenuPopup(Sender, MousePos, Handled);
  352. }
  353. //---------------------------------------------------------------------------
  354. void __fastcall TFileSystemInfoDialog::CertificateViewButtonClick(
  355. TObject * /*Sender*/)
  356. {
  357. MessageDialog(FSessionInfo.Certificate, qtInformation, qaOK);
  358. }
  359. //---------------------------------------------------------------------------