FileSystemInfo.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. //---------------------------------------------------------------------
  13. #pragma link "HistoryComboBox"
  14. #pragma resource "*.dfm"
  15. //---------------------------------------------------------------------
  16. void __fastcall DoFileSystemInfoDialog(
  17. const TSessionInfo & SessionInfo, const TFileSystemInfo & FileSystemInfo,
  18. AnsiString SpaceAvailablePath, TGetSpaceAvailable OnGetSpaceAvailable)
  19. {
  20. TFileSystemInfoDialog * Dialog = new TFileSystemInfoDialog(Application,
  21. OnGetSpaceAvailable);
  22. try
  23. {
  24. Dialog->Execute(SessionInfo, FileSystemInfo, SpaceAvailablePath);
  25. }
  26. __finally
  27. {
  28. delete Dialog;
  29. }
  30. }
  31. //---------------------------------------------------------------------
  32. __fastcall TFileSystemInfoDialog::TFileSystemInfoDialog(TComponent * AOwner,
  33. TGetSpaceAvailable OnGetSpaceAvailable)
  34. : TForm(AOwner)
  35. {
  36. UseSystemSettings(this);
  37. FOnGetSpaceAvailable = OnGetSpaceAvailable;
  38. FSpaceAvailableLoaded = false;
  39. FLastListItem = 0;
  40. InstallPathWordBreakProc(SpaceAvailablePathEdit);
  41. ReadOnlyControl(HostKeyFingerprintEdit);
  42. ReadOnlyControl(InfoMemo);
  43. }
  44. //---------------------------------------------------------------------
  45. void __fastcall TFileSystemInfoDialog::Execute(const TSessionInfo & SessionInfo,
  46. const TFileSystemInfo & FileSystemInfo, AnsiString SpaceAvailablePath)
  47. {
  48. FSessionInfo = SessionInfo;
  49. FFileSystemInfo = FileSystemInfo;
  50. SpaceAvailablePathEdit->Text = SpaceAvailablePath;
  51. UpdateControls();
  52. ShowModal();
  53. }
  54. //---------------------------------------------------------------------
  55. AnsiString __fastcall TFileSystemInfoDialog::CapabilityStr(TFSCapability Capability)
  56. {
  57. return BooleanToStr(FFileSystemInfo.IsCapable[Capability]);
  58. }
  59. //---------------------------------------------------------------------
  60. AnsiString __fastcall TFileSystemInfoDialog::CapabilityStr(TFSCapability Capability1,
  61. TFSCapability Capability2)
  62. {
  63. return FORMAT("%s/%s", (CapabilityStr(Capability1), CapabilityStr(Capability2)));
  64. }
  65. //---------------------------------------------------------------------
  66. AnsiString __fastcall TFileSystemInfoDialog::SpaceStr(__int64 Bytes)
  67. {
  68. AnsiString Result;
  69. if (Bytes == 0)
  70. {
  71. Result = LoadStr(FSINFO_BYTES_UNKNOWN);
  72. }
  73. else
  74. {
  75. Result = FormatBytes(Bytes);
  76. AnsiString SizeUnorderedStr = FormatBytes(Bytes, false);
  77. if (Result != SizeUnorderedStr)
  78. {
  79. Result = FORMAT("%s (%s)", (Result, SizeUnorderedStr));
  80. }
  81. }
  82. return Result;
  83. }
  84. //---------------------------------------------------------------------
  85. void __fastcall TFileSystemInfoDialog::Feed(TFeedFileSystemData AddItem)
  86. {
  87. AddItem(ServerView, FSINFO_REMOTE_SYSTEM, FFileSystemInfo.RemoteSystem);
  88. AddItem(ServerView, FSINFO_SESSION_PROTOCOL, FSessionInfo.ProtocolName);
  89. AddItem(ServerView, FSINFO_SSH_IMPLEMENTATION, FSessionInfo.SshImplementation);
  90. AnsiString Str = FSessionInfo.CSCipher;
  91. if (FSessionInfo.CSCipher != FSessionInfo.SCCipher)
  92. {
  93. Str += FORMAT("/%s", (FSessionInfo.SCCipher));
  94. }
  95. AddItem(ServerView, FSINFO_CIPHER, Str);
  96. Str = DefaultStr(FSessionInfo.CSCompression, LoadStr(NO_STR));
  97. if (FSessionInfo.CSCompression != FSessionInfo.SCCompression)
  98. {
  99. Str += FORMAT("/%s", (DefaultStr(FSessionInfo.SCCompression, LoadStr(NO_STR))));
  100. }
  101. AddItem(ServerView, FSINFO_COMPRESSION, Str);
  102. if (FSessionInfo.ProtocolName != FFileSystemInfo.ProtocolName)
  103. {
  104. AddItem(ServerView, FSINFO_FS_PROTOCOL, FFileSystemInfo.ProtocolName);
  105. }
  106. AddItem(HostKeyFingerprintEdit, 0, FSessionInfo.HostKeyFingerprint);
  107. AddItem(ProtocolView, FSINFO_MODE_CHANGING, CapabilityStr(fcModeChanging));
  108. AddItem(ProtocolView, FSINFO_OWNER_GROUP_CHANGING, CapabilityStr(fcGroupChanging));
  109. AnsiString AnyCommand;
  110. if (!FFileSystemInfo.IsCapable[fcShellAnyCommand] &&
  111. FFileSystemInfo.IsCapable[fcAnyCommand])
  112. {
  113. AnyCommand = LoadStr(FSINFO_PROTOCOL_ANY_COMMAND);
  114. }
  115. else
  116. {
  117. AnyCommand = CapabilityStr(fcAnyCommand);
  118. }
  119. AddItem(ProtocolView, FSINFO_ANY_COMMAND, AnyCommand);
  120. AddItem(ProtocolView, FSINFO_SYMBOLIC_HARD_LINK, CapabilityStr(fcSymbolicLink, fcHardLink));
  121. AddItem(ProtocolView, FSINFO_USER_GROUP_LISTING, CapabilityStr(fcUserGroupListing));
  122. AddItem(ProtocolView, FSINFO_REMOTE_COPY, CapabilityStr(fcRemoteCopy));
  123. AddItem(ProtocolView, FSINFO_CHECKING_SPACE_AVAILABLE, CapabilityStr(fcCheckingSpaceAvailable));
  124. AddItem(ProtocolView, FSINFO_CALCULATING_CHECKSUM, CapabilityStr(fcCalculatingChecksum));
  125. AddItem(ProtocolView, FSINFO_NATIVE_TEXT_MODE, CapabilityStr(fcNativeTextMode));
  126. AddItem(InfoMemo, 0, FFileSystemInfo.AdditionalInfo);
  127. AddItem(SpaceAvailableView, FSINFO_BYTES_ON_DEVICE, SpaceStr(FSpaceAvailable.BytesOnDevice));
  128. AddItem(SpaceAvailableView, FSINFO_UNUSED_BYTES_ON_DEVICE, SpaceStr(FSpaceAvailable.UnusedBytesOnDevice));
  129. AddItem(SpaceAvailableView, FSINFO_BYTES_AVAILABLE_TO_USER, SpaceStr(FSpaceAvailable.BytesAvailableToUser));
  130. AddItem(SpaceAvailableView, FSINFO_UNUSED_BYTES_AVAILABLE_TO_USER, SpaceStr(FSpaceAvailable.UnusedBytesAvailableToUser));
  131. AddItem(SpaceAvailableView, FSINFO_BYTES_PER_ALLOCATION_UNIT, SpaceStr(FSpaceAvailable.BytesPerAllocationUnit));
  132. }
  133. //---------------------------------------------------------------------
  134. void __fastcall TFileSystemInfoDialog::ControlsAddItem(TControl * Control,
  135. int Label, AnsiString Value)
  136. {
  137. if (FLastFeededControl != Control)
  138. {
  139. // TODO, we should clear excess list view items here, but it
  140. // actually should not happen as of now
  141. FLastFeededControl = Control;
  142. FLastListItem = 0;
  143. }
  144. if (Control == HostKeyFingerprintEdit)
  145. {
  146. EnableControl(HostKeyGroup, !Value.IsEmpty());
  147. HostKeyGroup->Visible = !Value.IsEmpty();
  148. HostKeyFingerprintEdit->Text = Value;
  149. }
  150. else if (Control == InfoMemo)
  151. {
  152. EnableControl(InfoGroup, !Value.IsEmpty());
  153. InfoGroup->Visible = !Value.IsEmpty();
  154. InfoMemo->Lines->Text = Value;
  155. }
  156. else
  157. {
  158. TListView * ListView = dynamic_cast<TListView *>(Control);
  159. assert(ListView != NULL);
  160. if (!Value.IsEmpty())
  161. {
  162. TListItem * Item;
  163. if (ListView->Items->Count > FLastListItem)
  164. {
  165. Item = ListView->Items->Item[FLastListItem];
  166. }
  167. else
  168. {
  169. Item = ListView->Items->Add();
  170. }
  171. FLastListItem++;
  172. Item->Caption = LoadStr(Label);
  173. if (Item->SubItems->Count > 0)
  174. {
  175. Item->SubItems->Strings[0] = Value;
  176. }
  177. else
  178. {
  179. Item->SubItems->Add(Value);
  180. }
  181. }
  182. }
  183. }
  184. //---------------------------------------------------------------------
  185. void __fastcall TFileSystemInfoDialog::FeedControls()
  186. {
  187. FLastFeededControl = NULL;
  188. Feed(ControlsAddItem);
  189. }
  190. //---------------------------------------------------------------------
  191. void __fastcall TFileSystemInfoDialog::UpdateControls()
  192. {
  193. EnableControl(SpaceAvailableSheet, SpaceAvailableSupported());
  194. EnableControl(SpaceAvailableButton, SpaceAvailableSheet->Enabled &&
  195. !SpaceAvailablePathEdit->Text.IsEmpty());
  196. }
  197. //---------------------------------------------------------------------
  198. void __fastcall TFileSystemInfoDialog::HelpButtonClick(TObject * /*Sender*/)
  199. {
  200. FormHelp(this);
  201. }
  202. //---------------------------------------------------------------------
  203. void __fastcall TFileSystemInfoDialog::ClipboardAddItem(TControl * Control,
  204. int Label, AnsiString Value)
  205. {
  206. if (Control->Enabled && !Value.IsEmpty())
  207. {
  208. if (FLastFeededControl != Control)
  209. {
  210. if (FLastFeededControl != NULL)
  211. {
  212. FClipboard += AnsiString::StringOfChar('-', 60) + "\r\n";
  213. }
  214. FLastFeededControl = Control;
  215. }
  216. if (dynamic_cast<TListView *>(Control) == NULL)
  217. {
  218. TGroupBox * Group = dynamic_cast<TGroupBox *>(Control->Parent);
  219. assert(Group != NULL);
  220. if ((Value.Length() >= 2) && (Value.SubString(Value.Length() - 1, 2) == "\r\n"))
  221. {
  222. Value.SetLength(Value.Length() - 2);
  223. }
  224. FClipboard += FORMAT("%s\r\n%s\r\n", (Group->Caption, Value));
  225. }
  226. else
  227. {
  228. assert(dynamic_cast<TListView *>(Control) != NULL);
  229. FClipboard += FORMAT("%s = %s\r\n", (LoadStr(Label), Value));
  230. }
  231. }
  232. }
  233. //---------------------------------------------------------------------------
  234. void __fastcall TFileSystemInfoDialog::ClipboardButtonClick(
  235. TObject * /*Sender*/)
  236. {
  237. NeedSpaceAvailable();
  238. FLastFeededControl = NULL;
  239. FClipboard = "";
  240. Feed(ClipboardAddItem);
  241. CopyToClipboard(FClipboard);
  242. }
  243. //---------------------------------------------------------------------------
  244. void __fastcall TFileSystemInfoDialog::CopyClick(TObject * Sender)
  245. {
  246. TComponent * Item = dynamic_cast<TComponent *>(Sender);
  247. assert(Item != NULL);
  248. TPopupMenu * PopupMenu = dynamic_cast<TPopupMenu *>(Item->Owner);
  249. assert(PopupMenu != NULL);
  250. TListView * ListView = dynamic_cast<TListView *>(PopupMenu->PopupComponent);
  251. assert(ListView != NULL);
  252. AnsiString Text;
  253. for (int Index = 0; Index < ListView->Items->Count; Index++)
  254. {
  255. TListItem * Item = ListView->Items->Item[Index];
  256. if (Item->Selected)
  257. {
  258. Text += FORMAT("%s = %s\r\n", (Item->Caption, Item->SubItems->Strings[0]));
  259. }
  260. }
  261. CopyToClipboard(Text);
  262. }
  263. //---------------------------------------------------------------------------
  264. void __fastcall TFileSystemInfoDialog::FormShow(TObject * /*Sender*/)
  265. {
  266. PageControl->ActivePage = ProtocolSheet;
  267. FeedControls();
  268. }
  269. //---------------------------------------------------------------------------
  270. void __fastcall TFileSystemInfoDialog::SpaceAvailableButtonClick(
  271. TObject * /*Sender*/)
  272. {
  273. CheckSpaceAvailable();
  274. }
  275. //---------------------------------------------------------------------------
  276. void __fastcall TFileSystemInfoDialog::CheckSpaceAvailable()
  277. {
  278. assert(FOnGetSpaceAvailable != NULL);
  279. assert(!SpaceAvailablePathEdit->Text.IsEmpty());
  280. FSpaceAvailableLoaded = true;
  281. bool DoClose = false;
  282. try
  283. {
  284. FOnGetSpaceAvailable(SpaceAvailablePathEdit->Text, FSpaceAvailable, DoClose);
  285. }
  286. __finally
  287. {
  288. FeedControls();
  289. if (DoClose)
  290. {
  291. Close();
  292. }
  293. }
  294. }
  295. //---------------------------------------------------------------------------
  296. void __fastcall TFileSystemInfoDialog::NeedSpaceAvailable()
  297. {
  298. if (!FSpaceAvailableLoaded && SpaceAvailableSupported())
  299. {
  300. CheckSpaceAvailable();
  301. }
  302. }
  303. //---------------------------------------------------------------------------
  304. bool __fastcall TFileSystemInfoDialog::SpaceAvailableSupported()
  305. {
  306. return (FOnGetSpaceAvailable != NULL);
  307. }
  308. //---------------------------------------------------------------------------
  309. void __fastcall TFileSystemInfoDialog::PageControlChange(TObject * /*Sender*/)
  310. {
  311. if (PageControl->ActivePage == SpaceAvailableSheet)
  312. {
  313. NeedSpaceAvailable();
  314. }
  315. }
  316. //---------------------------------------------------------------------------
  317. void __fastcall TFileSystemInfoDialog::ControlChange(TObject * /*Sender*/)
  318. {
  319. UpdateControls();
  320. }
  321. //---------------------------------------------------------------------------
  322. void __fastcall TFileSystemInfoDialog::SpaceAvailablePathEditEnter(
  323. TObject * /*Sender*/)
  324. {
  325. SpaceAvailableButton->Default = true;
  326. CloseButton->Default = false;
  327. }
  328. //---------------------------------------------------------------------------
  329. void __fastcall TFileSystemInfoDialog::SpaceAvailablePathEditExit(
  330. TObject * /*Sender*/)
  331. {
  332. SpaceAvailableButton->Default = false;
  333. CloseButton->Default = true;
  334. }
  335. //---------------------------------------------------------------------------
  336. void __fastcall TFileSystemInfoDialog::ControlContextPopup(
  337. TObject * Sender, TPoint & MousePos, bool & Handled)
  338. {
  339. MenuPopup(Sender, MousePos, Handled);
  340. }
  341. //---------------------------------------------------------------------------