1
0

Properties.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989
  1. //---------------------------------------------------------------------
  2. #include <FormsPCH.h>
  3. #pragma hdrstop
  4. #include "Properties.h"
  5. #include <Terminal.h>
  6. //---------------------------------------------------------------------
  7. #pragma link "PathLabel"
  8. #pragma link "Rights"
  9. #pragma resource "*.dfm"
  10. //---------------------------------------------------------------------
  11. bool __fastcall DoPropertiesDialog(TStrings * FileList,
  12. const UnicodeString Directory, const TRemoteTokenList * GroupList,
  13. const TRemoteTokenList * UserList, TStrings * ChecksumAlgs,
  14. TRemoteProperties * Properties,
  15. int AllowedChanges, int Options, TCalculateSizeEvent OnCalculateSize,
  16. TCalculateChecksumEvent OnCalculateChecksum)
  17. {
  18. bool Result;
  19. TPropertiesDialog * PropertiesDialog = new TPropertiesDialog(Application,
  20. FileList, Directory, GroupList, UserList, ChecksumAlgs, AllowedChanges, Options,
  21. OnCalculateSize, OnCalculateChecksum);
  22. try
  23. {
  24. Result = PropertiesDialog->Execute(*Properties);
  25. }
  26. __finally
  27. {
  28. delete PropertiesDialog;
  29. }
  30. return Result;
  31. }
  32. //---------------------------------------------------------------------
  33. __fastcall TPropertiesDialog::TPropertiesDialog(TComponent* AOwner,
  34. TStrings * FileList, const UnicodeString Directory,
  35. const TRemoteTokenList * GroupList, const TRemoteTokenList * UserList,
  36. TStrings * ChecksumAlgs,
  37. int AllowedChanges, int Options, TCalculateSizeEvent OnCalculateSize,
  38. TCalculateChecksumEvent OnCalculateChecksum)
  39. : TForm(AOwner)
  40. {
  41. FOnCalculateSize = OnCalculateSize;
  42. FOnCalculateChecksum = OnCalculateChecksum;
  43. RightsFrame->OnChange = ControlChange;
  44. FFileList = new TStringList();
  45. FFileList->Assign(FileList);
  46. FAllowedChanges = AllowedChanges;
  47. if (FLAGSET(FAllowedChanges, cpAcl))
  48. {
  49. DebugAssert(FLAGCLEAR(FAllowedChanges, cpMode));
  50. RightsLabel->Caption = LoadStr(PROPERTIES_ACL);
  51. RightsFrame->DisplayAsAcl();
  52. }
  53. FOptions = Options;
  54. FAllowCalculateStats = false;
  55. FStatsNotCalculated = false;
  56. FChecksumLoaded = false;
  57. FMultipleChecksum = false;
  58. LocationLabel->Text = Directory;
  59. FGroupList = GroupList;
  60. FUserList = UserList;
  61. FChecksumAlgs = ChecksumAlgs;
  62. ReadOnlyControl(ChecksumEdit);
  63. ReadOnlyControl(OwnerView);
  64. ReadOnlyControl(GroupView);
  65. ReadOnlyControl(FileLabel);
  66. ReadOnlyControl(LocationLabel);
  67. ReadOnlyControl(SizeLabel);
  68. ReadOnlyControl(LinksToLabel);
  69. ChecksumUnknownLabel->Caption = LoadStr(PROPERTIES_CHECKSUM_UNKNOWN);
  70. UseSystemSettings(this);
  71. LoadInfo();
  72. }
  73. //---------------------------------------------------------------------------
  74. __fastcall TPropertiesDialog::~TPropertiesDialog()
  75. {
  76. delete FFileList;
  77. FFileList = NULL;
  78. }
  79. //---------------------------------------------------------------------
  80. bool __fastcall TPropertiesDialog::Execute(TRemoteProperties & Properties)
  81. {
  82. SetFileProperties(Properties);
  83. PageControl->ActivePage = CommonSheet;
  84. if (OwnerComboBox->Visible && OwnerComboBox->Enabled)
  85. {
  86. ActiveControl = OwnerComboBox;
  87. }
  88. else if (GroupComboBox->Visible && GroupComboBox->Enabled)
  89. {
  90. ActiveControl = GroupComboBox;
  91. }
  92. else if (RightsFrame->Visible && RightsFrame->Enabled)
  93. {
  94. ActiveControl = RightsFrame;
  95. }
  96. else if (DebugAlwaysTrue(CancelButton->Visible && CancelButton->Enabled))
  97. {
  98. ActiveControl = CancelButton;
  99. }
  100. if (DebugAlwaysTrue(FChecksumAlgs != NULL))
  101. {
  102. ChecksumAlgEdit->Items->Assign(FChecksumAlgs);
  103. int ChecksumIndex = FChecksumAlgs->IndexOf(GUIConfiguration->ChecksumAlg);
  104. if (ChecksumIndex < 0)
  105. {
  106. ChecksumIndex = 0;
  107. }
  108. ChecksumAlgEdit->ItemIndex = ChecksumIndex;
  109. }
  110. ResetChecksum();
  111. UpdateControls();
  112. bool Result = (ShowModal() == DefaultResult());
  113. if (Result)
  114. {
  115. Properties = GetFileProperties();
  116. }
  117. return Result;
  118. }
  119. //---------------------------------------------------------------------------
  120. TModalResult __fastcall TPropertiesDialog::DefaultResult()
  121. {
  122. // Fallback when ChecksumButton is default
  123. return ::DefaultResult(this, OkButton);
  124. }
  125. //---------------------------------------------------------------------------
  126. UnicodeString __fastcall TPropertiesDialog::LoadRemoteToken(
  127. const TRemoteToken & Token)
  128. {
  129. UnicodeString Result;
  130. if (FLAGSET(FOptions, poUserGroupByID))
  131. {
  132. if (Token.IDValid)
  133. {
  134. if (Token.NameValid)
  135. {
  136. Result = FORMAT(L"%s [%d]", (Token.Name, int(Token.ID)));
  137. }
  138. else
  139. {
  140. Result = FORMAT(L"[%d]", (int(Token.ID)));
  141. }
  142. }
  143. else
  144. {
  145. // be it valid or not
  146. Result = Token.Name;
  147. }
  148. }
  149. else
  150. {
  151. // what if name is not filled in?
  152. Result = Token.Name;
  153. }
  154. return Result;
  155. }
  156. //---------------------------------------------------------------------------
  157. void __fastcall TPropertiesDialog::LoadRemoteToken(
  158. TComboBox * ComboBox, TEdit * View, TLabel * Label, bool Valid, const TRemoteToken & Token, int Change)
  159. {
  160. UnicodeString Value = Valid ? LoadRemoteToken(Token) : EmptyStr;
  161. ComboBox->Text = Value;
  162. View->Text = Value;
  163. bool AllowedChange = FLAGSET(FAllowedChanges, Change);
  164. ComboBox->Visible = AllowedChange;
  165. View->Visible = Valid && !AllowedChange;
  166. Label->FocusControl = AllowedChange ? static_cast<TWinControl *>(ComboBox) : static_cast<TWinControl *>(View);
  167. Label->Visible = Label->FocusControl->Visible;
  168. }
  169. //---------------------------------------------------------------------------
  170. void __fastcall TPropertiesDialog::LoadRemoteTokens(TComboBox * ComboBox,
  171. const TRemoteTokenList * List)
  172. {
  173. TStrings * Items = ComboBox->Items;
  174. Items->BeginUpdate();
  175. try
  176. {
  177. Items->Clear();
  178. if (List != NULL)
  179. {
  180. int Count = List->Count();
  181. for (int Index = 0; Index < Count; Index++)
  182. {
  183. Items->Add(LoadRemoteToken(*List->Token(Index)));
  184. }
  185. }
  186. }
  187. __finally
  188. {
  189. Items->EndUpdate();
  190. }
  191. }
  192. //---------------------------------------------------------------------------
  193. void __fastcall TPropertiesDialog::LoadInfo()
  194. {
  195. DebugAssert(FFileList->Count > 0);
  196. FMultiple = FFileList->Count > 1;
  197. FMultipleChecksum = FMultiple;
  198. FAllowCalculateStats = false;
  199. FStatsNotCalculated = false;
  200. __int64 FilesSize = 0;
  201. TCalculateSizeStats Stats;
  202. for (int Index = 0; Index < FFileList->Count; Index++)
  203. {
  204. TRemoteFile * File = (TRemoteFile *)(FFileList->Objects[Index]);
  205. if (File->IsDirectory)
  206. {
  207. Stats.Directories++;
  208. // we should use TTerminal::CanRecurseToDirectory instead
  209. if (!File->IsSymLink)
  210. {
  211. FAllowCalculateStats = true;
  212. FStatsNotCalculated = true;
  213. FMultipleChecksum = true;
  214. }
  215. }
  216. else
  217. {
  218. Stats.Files++;
  219. }
  220. if (File->IsSymLink)
  221. {
  222. Stats.SymLinks++;
  223. }
  224. FilesSize += File->Size;
  225. }
  226. // before it gets eventualy cleared if !FMultiple
  227. bool ShowTags = FLAGSET(FOptions, poTags) && (Stats.Files == 1) && (Stats.Directories == 0);
  228. LoadRemoteTokens(GroupComboBox, FGroupList);
  229. LoadRemoteTokens(OwnerComboBox, FUserList);
  230. FAnyDirectories = (Stats.Directories > 0);
  231. RightsFrame->AllowAddXToDirectories = FAnyDirectories;
  232. if (!FMultiple)
  233. {
  234. // Show only file name, if we have only single file/directory.
  235. // For directory, this changes, once "Calculate" button is pressed
  236. Stats = TCalculateSizeStats();
  237. }
  238. LoadStats(FilesSize, Stats);
  239. RightsFrame->AllowUndef = FMultiple;
  240. if (!FMultiple)
  241. {
  242. TRemoteFile * File = (TRemoteFile *)(FFileList->Objects[0]);
  243. DebugAssert(File);
  244. UpdateFileImage();
  245. LinksToLabelLabel->Visible = File->IsSymLink;
  246. LinksToLabel->Visible = File->IsSymLink;
  247. if (File->IsSymLink)
  248. {
  249. LinksToLabel->Text = File->LinkTo;
  250. }
  251. Caption = FMTLOAD(PROPERTIES_FILE_CAPTION, (File->FileName));
  252. }
  253. else
  254. {
  255. Caption = FMTLOAD(PROPERTIES_FILES_CAPTION, (FFileList->Strings[0]));
  256. LinksToLabelLabel->Hide();
  257. LinksToLabel->Hide();
  258. LoadDialogImage(FileIconImage, L"Multiple Files");
  259. }
  260. ChecksumGroup->Visible = !FMultipleChecksum;
  261. ChecksumView->Visible = FMultipleChecksum;
  262. TagsSheet->TabVisible = ShowTags;
  263. }
  264. //---------------------------------------------------------------------------
  265. void __fastcall TPropertiesDialog::UpdateFileImage()
  266. {
  267. TImageList * ImageList = ShellImageListForControl(this, ilsLarge);
  268. FileIconImage->Picture->Bitmap = NULL;
  269. TRemoteFile * File = (TRemoteFile *)(FFileList->Objects[0]);
  270. // shell image list does not have fixed large icon size
  271. // (it is probably 32x32 min, but can be larger, on xp it is 48x48 if
  272. // large icons are enabled, on vista can be even larger).
  273. // so we stretch (shrink) the icon to 32x32 here to be sure.
  274. Graphics::TBitmap * Bitmap = new Graphics::TBitmap;
  275. try
  276. {
  277. ImageList->GetBitmap(File->IconIndex, Bitmap);
  278. int Size = DialogImageSize(this);
  279. // Use exact DPI-scaled size, not approximate scaling by font size.
  280. // Otherwise we stretch icons unnecessarily because the canvas
  281. // is one or two pixels off the icon size
  282. FileIconImage->Width = Size;
  283. FileIconImage->Height = Size;
  284. FileIconImage->Picture->Bitmap->Width = Size;
  285. FileIconImage->Picture->Bitmap->Height = Size;
  286. FileIconImage->Picture->Bitmap->Canvas->StretchDraw(
  287. TRect(0, 0, Size, Size),
  288. Bitmap);
  289. }
  290. __finally
  291. {
  292. delete Bitmap;
  293. }
  294. }
  295. //---------------------------------------------------------------------------
  296. void __fastcall TPropertiesDialog::LoadStats(__int64 FilesSize,
  297. const TCalculateSizeStats & Stats)
  298. {
  299. UnicodeString SizeStr;
  300. UnicodeString FilesStr;
  301. if (FStatsNotCalculated)
  302. {
  303. SizeStr = LoadStr(PROPERTIES_UNKNOWN_SIZE);
  304. }
  305. else
  306. {
  307. SizeStr = FormatBytes(FilesSize);
  308. UnicodeString SizeUnorderedStr = FormatBytes(FilesSize, fbNone);
  309. if (SizeStr != SizeUnorderedStr)
  310. {
  311. SizeStr = FORMAT(L"%s (%s)", (SizeStr, SizeUnorderedStr));
  312. }
  313. }
  314. if (((Stats.Files + Stats.Directories) == 0) && !FMultiple)
  315. {
  316. TRemoteFile * File = (TRemoteFile *)(FFileList->Objects[0]);
  317. DebugAssert(File != NULL);
  318. FilesStr = File->FileName;
  319. }
  320. else
  321. {
  322. if (Stats.Files > 0)
  323. {
  324. FilesStr = (Stats.Files == 1) ? FMTLOAD(PROPERTIES_FILE, (Stats.Files)) :
  325. FMTLOAD(PROPERTIES_FILES, (Stats.Files));
  326. if (Stats.Directories > 0)
  327. {
  328. FilesStr = FORMAT(L"%s, ", (FilesStr));
  329. }
  330. }
  331. if (Stats.Directories > 0)
  332. {
  333. FilesStr += (Stats.Directories == 1) ? FMTLOAD(PROPERTIES_DIRECTORY, (Stats.Directories)) :
  334. FMTLOAD(PROPERTIES_DIRECTORIES, (Stats.Directories));
  335. }
  336. if (Stats.SymLinks > 0)
  337. {
  338. UnicodeString SymLinksStr;
  339. SymLinksStr = (Stats.SymLinks == 1) ? FMTLOAD(PROPERTIES_SYMLINK, (Stats.SymLinks)) :
  340. FMTLOAD(PROPERTIES_SYMLINKS, (Stats.SymLinks));
  341. FilesStr = FORMAT(L"%s (%s)", (FilesStr, SymLinksStr));
  342. }
  343. }
  344. SizeLabel->Text = SizeStr;
  345. FileLabel->Text = FilesStr;
  346. }
  347. //---------------------------------------------------------------------------
  348. void __fastcall TPropertiesDialog::SetFileProperties(const TRemoteProperties & value)
  349. {
  350. TValidProperties Valid;
  351. if (value.Valid.Contains(vpRights) && (FLAGSET(FAllowedChanges, cpMode) || FLAGSET(FAllowedChanges, cpAcl)))
  352. {
  353. Valid << vpRights;
  354. }
  355. if (value.Valid.Contains(vpOwner) && FLAGSET(FAllowedChanges, cpOwner))
  356. {
  357. Valid << vpOwner;
  358. }
  359. if (value.Valid.Contains(vpGroup) && FLAGSET(FAllowedChanges, cpGroup))
  360. {
  361. Valid << vpGroup;
  362. }
  363. FOrigProperties = value;
  364. FOrigProperties.Valid = Valid;
  365. FOrigProperties.Recursive = false;
  366. bool HasRights = value.Valid.Contains(vpRights);
  367. RightsFrame->Visible = HasRights;
  368. RightsLabel->Visible = RightsFrame->Visible;
  369. if (HasRights)
  370. {
  371. RightsFrame->Rights = value.Rights;
  372. RightsFrame->AddXToDirectories = value.AddXToDirectories;
  373. }
  374. else
  375. {
  376. RightsFrame->Rights = TRights();
  377. RightsFrame->AddXToDirectories = false;
  378. }
  379. // Not necesarily true, let's find the scenario when it is not and then decide how to render that (shift group up?)
  380. DebugAssert(value.Valid.Contains(vpOwner) || !value.Valid.Contains(vpGroup));
  381. LoadRemoteToken(GroupComboBox, GroupView, GroupLabel, value.Valid.Contains(vpGroup), value.Group, cpGroup);
  382. LoadRemoteToken(OwnerComboBox, OwnerView, OwnerLabel, value.Valid.Contains(vpOwner), value.Owner, cpOwner);
  383. bool HasAnything = GroupLabel->Visible || OwnerLabel->Visible || HasRights;
  384. // Not necesarily true, let's find the scenario when it is not and then decide how to render that (shift rights up?)
  385. DebugAssert((GroupLabel->Visible || OwnerLabel->Visible) || !HasRights);
  386. GroupOwnerRightsBevel->Visible = HasAnything;
  387. RecursiveCheck2->Checked = value.Recursive;
  388. RecursiveCheck2->Visible =
  389. (GroupComboBox->Visible ||
  390. OwnerComboBox->Visible ||
  391. // Recursion is always supported for permissions and never for ACL.
  392. // If this ever changes we will have to introduce respective capability check.
  393. (HasRights && !FLAGSET(FAllowedChanges, cpAcl))) &&
  394. FAnyDirectories;
  395. RecursiveBevel->Visible = RecursiveCheck2->Visible || HasRights;
  396. if (TagsSheet->TabVisible)
  397. {
  398. TagsView->Clear();
  399. if (value.Valid.Contains(vpTags))
  400. {
  401. std::unique_ptr<TStrings> Tags(TextToStringList(value.Tags));
  402. for (int Index = 0; Index < Tags->Count; Index += 2)
  403. {
  404. AddTag(Tags->Strings[Index], Tags->Strings[Index + 1]);
  405. }
  406. }
  407. AutoSizeTagsView();
  408. }
  409. UpdateControls();
  410. }
  411. //---------------------------------------------------------------------------
  412. void __fastcall TPropertiesDialog::StoreRemoteToken(unsigned int ID,
  413. const UnicodeString & Text, const TRemoteTokenList * List, TRemoteToken & Result)
  414. {
  415. DebugAssert(List != NULL);
  416. const TRemoteToken * Token = List->Find(ID);
  417. if (Token == NULL)
  418. {
  419. Result.ID = ID;
  420. Result.Name = Text;
  421. }
  422. else
  423. {
  424. Result = *Token;
  425. }
  426. }
  427. //---------------------------------------------------------------------------
  428. TRemoteToken __fastcall TPropertiesDialog::StoreRemoteToken(const TRemoteToken & Orig,
  429. UnicodeString Text, int Message, const TRemoteTokenList * List)
  430. {
  431. TRemoteToken Result;
  432. Text = Text.Trim();
  433. if (!Text.IsEmpty())
  434. {
  435. if (FLAGSET(FOptions, poUserGroupByID))
  436. {
  437. DebugAssert(List != NULL);
  438. int IDStart = Text.LastDelimiter(L"[");
  439. if (!Text.IsEmpty() && (IDStart >= 0) && (Text[Text.Length()] == L']'))
  440. {
  441. int ID;
  442. UnicodeString IDStr = Text.SubString(IDStart + 1, Text.Length() - IDStart - 1);
  443. if (!TryStrToInt(IDStr, ID))
  444. {
  445. throw Exception(Message);
  446. }
  447. else
  448. {
  449. StoreRemoteToken(ID, Text.SubString(1, IDStart - 1).Trim(), List, Result);
  450. }
  451. }
  452. else
  453. {
  454. const TRemoteToken * Token = List->Find(Text);
  455. if (Token == NULL)
  456. {
  457. int ID;
  458. if (TryStrToInt(Text, ID))
  459. {
  460. StoreRemoteToken(ID, Text, List, Result);
  461. }
  462. else
  463. {
  464. throw Exception(MainInstructions(FMTLOAD(PROPERTIES_UNKNOWN_TOKEN, (Text))));
  465. }
  466. }
  467. else
  468. {
  469. Result = *Token;
  470. }
  471. }
  472. }
  473. else
  474. {
  475. Result.Name = Text;
  476. }
  477. }
  478. if (LoadRemoteToken(Result) == LoadRemoteToken(Orig))
  479. {
  480. Result = Orig;
  481. }
  482. return Result;
  483. }
  484. //---------------------------------------------------------------------------
  485. void __fastcall TPropertiesDialog::StoreRemoteToken(TComboBox * ComboBox,
  486. int ChangeFlag, TValidProperty PropertyFlag, const TRemoteToken & Orig,
  487. TRemoteToken & Token, int Message, const TRemoteTokenList * List,
  488. TRemoteProperties & Properties)
  489. {
  490. UnicodeString Text = ComboBox->Text.Trim();
  491. if (FLAGSET(FAllowedChanges, ChangeFlag))
  492. {
  493. Token = StoreRemoteToken(Orig, Text, Message, List);
  494. if (Token.IsSet)
  495. {
  496. Properties.Valid << PropertyFlag;
  497. }
  498. }
  499. }
  500. //---------------------------------------------------------------------------
  501. TRemoteProperties __fastcall TPropertiesDialog::GetFileProperties()
  502. {
  503. TRemoteProperties Result;
  504. if (FLAGSET(FAllowedChanges, cpMode) || FLAGSET(FAllowedChanges, cpAcl))
  505. {
  506. Result.Valid << vpRights;
  507. Result.Rights = RightsFrame->Rights;
  508. Result.AddXToDirectories = RightsFrame->AddXToDirectories;
  509. }
  510. StoreRemoteToken(GroupComboBox, cpGroup, vpGroup, FOrigProperties.Group,
  511. Result.Group, PROPERTIES_INVALID_GROUP, FGroupList, Result);
  512. StoreRemoteToken(OwnerComboBox, cpOwner, vpOwner, FOrigProperties.Owner,
  513. Result.Owner, PROPERTIES_INVALID_OWNER, FUserList, Result);
  514. Result.Recursive = RecursiveCheck2->Checked;
  515. if (TagsSheet->TabVisible)
  516. {
  517. std::unique_ptr<TStrings> Tags(new TStringList());
  518. TagsView->HandleNeeded(); // Count does not work otherwise
  519. for (int Index = 0; Index < TagsView->Items->Count; Index++)
  520. {
  521. TListItem * Item = TagsView->Items->Item[Index];
  522. Tags->Add(Item->Caption);
  523. Tags->Add(Item->SubItems->Strings[0]);
  524. }
  525. Result.Tags = Tags->Text;
  526. Result.Valid << vpTags;
  527. }
  528. return Result;
  529. }
  530. //---------------------------------------------------------------------------
  531. void __fastcall TPropertiesDialog::ControlChange(TObject * /*Sender*/)
  532. {
  533. if (Visible)
  534. {
  535. UpdateControls();
  536. }
  537. }
  538. //---------------------------------------------------------------------------
  539. void __fastcall TPropertiesDialog::UpdateControls()
  540. {
  541. // No point enabling recursive check if there's no change allowed (supported),
  542. // i.e. with WebDAV.
  543. bool AnyAllowedChanges =
  544. FLAGSET(FAllowedChanges, cpGroup) || FLAGSET(FAllowedChanges, cpOwner) ||
  545. FLAGSET(FAllowedChanges, cpMode) || FLAGSET(FAllowedChanges, cpAcl);
  546. EnableControl(RecursiveCheck2, AnyAllowedChanges);
  547. bool Allow;
  548. try
  549. {
  550. Allow =
  551. !TRemoteProperties::ChangedProperties(FOrigProperties, GetFileProperties()).Valid.Empty() ||
  552. (RecursiveCheck2->Enabled && RecursiveCheck2->Checked);
  553. }
  554. catch(...)
  555. {
  556. // when properties are invalid allow submitting the form,
  557. // because that reveals the cause to the user, otherwise he/she
  558. // may not be able to tell what is wrong
  559. Allow = true;
  560. }
  561. EnableControl(OkButton, Allow);
  562. EnableControl(GroupComboBox, FLAGSET(FAllowedChanges, cpGroup));
  563. EnableControl(OwnerComboBox, FLAGSET(FAllowedChanges, cpOwner));
  564. EnableControl(RightsFrame, FLAGSET(FAllowedChanges, cpMode) || FLAGSET(FAllowedChanges, cpAcl));
  565. CalculateSizeButton->Visible = FAllowCalculateStats;
  566. if (!FMultiple)
  567. {
  568. // when setting properties for one file only, allow undef state
  569. // only when the input right explicitly requires it or
  570. // when "recursive" is on (possible for directory only).
  571. bool AllowUndef =
  572. (FOrigProperties.Valid.Contains(vpRights) &&
  573. FOrigProperties.Rights.AllowUndef) ||
  574. (RecursiveCheck2->Checked);
  575. if (!AllowUndef)
  576. {
  577. // when disallowing undef state, make sure, all undef are turned into unset
  578. RightsFrame->Rights = TRights(RightsFrame->Rights.NumberSet);
  579. }
  580. RightsFrame->AllowUndef = AllowUndef;
  581. }
  582. EnableControl(ChecksumSheet, ChecksumSupported());
  583. EnableControl(ChecksumButton, ChecksumSheet->Enabled &&
  584. !ChecksumAlgEdit->Text.IsEmpty());
  585. ChecksumEdit->Visible = !ChecksumEdit->Text.IsEmpty();
  586. ChecksumUnknownLabel->Visible = !ChecksumEdit->Visible;
  587. EnableControl(EditTagButton, (TagsView->ItemIndex >= 0));
  588. EnableControl(RemoveTagButton, (TagsView->ItemIndex >= 0));
  589. DefaultButton(ChecksumButton, ChecksumAlgEdit->Focused());
  590. DefaultButton(OkButton, !ChecksumAlgEdit->Focused());
  591. }
  592. //---------------------------------------------------------------------------
  593. void __fastcall TPropertiesDialog::FormCloseQuery(TObject * /*Sender*/,
  594. bool & /*CanClose*/)
  595. {
  596. if (ModalResult == DefaultResult())
  597. {
  598. ExitActiveControl(this);
  599. }
  600. }
  601. //---------------------------------------------------------------------------
  602. void __fastcall TPropertiesDialog::CalculateSizeButtonClick(
  603. TObject * /*Sender*/)
  604. {
  605. DebugAssert(FOnCalculateSize != NULL);
  606. bool DoClose = false;
  607. Enabled = false;
  608. try
  609. {
  610. __int64 Size;
  611. TCalculateSizeStats Stats;
  612. FOnCalculateSize(FFileList, Size, Stats, DoClose);
  613. FStatsNotCalculated = false;
  614. LoadStats(Size, Stats);
  615. }
  616. __finally
  617. {
  618. Enabled = true;
  619. if (DoClose)
  620. {
  621. Close();
  622. }
  623. }
  624. }
  625. //---------------------------------------------------------------------------
  626. void __fastcall TPropertiesDialog::HelpButtonClick(TObject * /*Sender*/)
  627. {
  628. FormHelp(this);
  629. }
  630. //---------------------------------------------------------------------------
  631. void __fastcall TPropertiesDialog::ResetChecksum()
  632. {
  633. ChecksumView->Items->Clear();
  634. ChecksumEdit->Text = UnicodeString();
  635. AutoSizeListColumnsWidth(ChecksumView);
  636. }
  637. //---------------------------------------------------------------------------
  638. void __fastcall TPropertiesDialog::CalculateChecksum()
  639. {
  640. DebugAssert(FOnCalculateChecksum != NULL);
  641. ResetChecksum();
  642. FChecksumLoaded = true;
  643. FAlgUsed = UnicodeString();
  644. bool DoClose = false;
  645. try
  646. {
  647. FOnCalculateChecksum(ChecksumAlgEdit->Text, FFileList, CalculatedChecksum, DoClose);
  648. }
  649. __finally
  650. {
  651. if (DoClose)
  652. {
  653. Close();
  654. }
  655. }
  656. // If we successfully used the selected checksum, remember it (in normalized form)
  657. if (!FAlgUsed.IsEmpty())
  658. {
  659. GUIConfiguration->ChecksumAlg = FAlgUsed;
  660. }
  661. AutoSizeListColumnsWidth(ChecksumView);
  662. }
  663. //---------------------------------------------------------------------------
  664. void __fastcall TPropertiesDialog::CalculatedChecksum(
  665. const UnicodeString & FileName, const UnicodeString & Alg,
  666. const UnicodeString & Hash)
  667. {
  668. if (FMultipleChecksum)
  669. {
  670. TListItem * Item = ChecksumView->Items->Add();
  671. Item->Caption = FileName;
  672. Item->SubItems->Add(Hash);
  673. // optimization
  674. int TopIndex = ListView_GetTopIndex(ChecksumView->Handle);
  675. int Index = Item->Index;
  676. if ((TopIndex <= Index) &&
  677. (Index <= TopIndex + ChecksumView->VisibleRowCount))
  678. {
  679. AutoSizeListColumnsWidth(ChecksumView);
  680. }
  681. }
  682. else
  683. {
  684. ChecksumEdit->Text = Hash;
  685. }
  686. FAlgUsed = Alg;
  687. UpdateControls();
  688. }
  689. //---------------------------------------------------------------------------
  690. void __fastcall TPropertiesDialog::NeedChecksum()
  691. {
  692. if (!FChecksumLoaded && ChecksumSupported())
  693. {
  694. CalculateChecksum();
  695. }
  696. }
  697. //---------------------------------------------------------------------------
  698. bool __fastcall TPropertiesDialog::ChecksumSupported()
  699. {
  700. return (FOnCalculateChecksum != NULL);
  701. }
  702. //---------------------------------------------------------------------------
  703. void __fastcall TPropertiesDialog::ChecksumButtonClick(TObject * /*Sender*/)
  704. {
  705. CalculateChecksum();
  706. }
  707. //---------------------------------------------------------------------------
  708. void __fastcall TPropertiesDialog::PageControlChange(TObject * /*Sender*/)
  709. {
  710. if (PageControl->ActivePage == ChecksumSheet)
  711. {
  712. NeedChecksum();
  713. }
  714. }
  715. //---------------------------------------------------------------------------
  716. void __fastcall TPropertiesDialog::ChecksumAlgEditChange(TObject * /*Sender*/)
  717. {
  718. ResetChecksum();
  719. UpdateControls();
  720. }
  721. //---------------------------------------------------------------------------
  722. void __fastcall TPropertiesDialog::CopyClick(TObject * Sender)
  723. {
  724. TInstantOperationVisualizer Visualizer;
  725. TListView * ListView = dynamic_cast<TListView *>(GetPopupComponent(Sender));
  726. DebugAssert(ListView != NULL);
  727. int Count = 0;
  728. UnicodeString SingleText;
  729. std::unique_ptr<TStrings> Lines(new TStringList());
  730. TListItem * Item = ListView->GetNextItem(NULL, sdAll, TItemStates() << isSelected);
  731. while (Item != NULL)
  732. {
  733. DebugAssert(Item->Selected);
  734. SingleText = Item->SubItems->Strings[0];
  735. UnicodeString Value = Item->SubItems->Strings[0];
  736. UnicodeString Entry = Item->Caption;
  737. if (!Value.IsEmpty())
  738. {
  739. Entry += FORMAT(L" = %s", (Value));
  740. }
  741. Lines->Add(Entry);
  742. Count++;
  743. Item = ListView->GetNextItem(Item, sdAll, TItemStates() << isSelected);
  744. }
  745. if ((ListView == ChecksumView) && (Count == 1))
  746. {
  747. CopyToClipboard(SingleText);
  748. }
  749. else
  750. {
  751. CopyToClipboard(Lines.get());
  752. }
  753. }
  754. //---------------------------------------------------------------------------
  755. void __fastcall TPropertiesDialog::ListViewContextPopup(
  756. TObject * Sender, TPoint & MousePos, bool & Handled)
  757. {
  758. MenuPopup(Sender, MousePos, Handled);
  759. }
  760. //---------------------------------------------------------------------------
  761. void __fastcall TPropertiesDialog::ValidateRemoteToken(
  762. const TRemoteToken & Orig, int Message, TComboBox * ComboBox,
  763. const TRemoteTokenList * List)
  764. {
  765. if (!IsCancelButtonBeingClicked(this))
  766. {
  767. try
  768. {
  769. ComboBox->Text =
  770. LoadRemoteToken(StoreRemoteToken(Orig, ComboBox->Text, Message, List));
  771. }
  772. catch(...)
  773. {
  774. ComboBox->SetFocus();
  775. throw;
  776. }
  777. }
  778. }
  779. //---------------------------------------------------------------------------
  780. void __fastcall TPropertiesDialog::GroupComboBoxExit(TObject * Sender)
  781. {
  782. ValidateRemoteToken(FOrigProperties.Group, PROPERTIES_INVALID_GROUP,
  783. dynamic_cast<TComboBox *>(Sender), FGroupList);
  784. }
  785. //---------------------------------------------------------------------------
  786. void __fastcall TPropertiesDialog::OwnerComboBoxExit(TObject * Sender)
  787. {
  788. ValidateRemoteToken(FOrigProperties.Owner, PROPERTIES_INVALID_OWNER,
  789. dynamic_cast<TComboBox *>(Sender), FUserList);
  790. }
  791. //---------------------------------------------------------------------------
  792. void __fastcall TPropertiesDialog::FormShow(TObject * /*Sender*/)
  793. {
  794. UpdateControls();
  795. }
  796. //---------------------------------------------------------------------------
  797. void __fastcall TPropertiesDialog::CMDpiChanged(TMessage & Message)
  798. {
  799. TForm::Dispatch(&Message);
  800. if (!FMultiple)
  801. {
  802. UpdateFileImage();
  803. }
  804. // WORKAROUND: Mere presence of the RightsFrame breaks automatic layout on DPI change in some situation, fixing it manually.
  805. // (opening on a secondary display with 100%, while primary display has 150%)
  806. SizeLabel->Width = CalculateSizeButton->Left - ScaleByTextHeight(this, 8) - SizeLabel->Left;
  807. Bevel1->Width = CommonSheet->ClientWidth - (Bevel1->Left * 2);
  808. Bevel2->Width = Bevel1->Width;
  809. GroupOwnerRightsBevel->Width = Bevel1->Width;
  810. RecursiveBevel->Width = Bevel1->Width;
  811. }
  812. //---------------------------------------------------------------------------
  813. void __fastcall TPropertiesDialog::Dispatch(void * Message)
  814. {
  815. TMessage * M = reinterpret_cast<TMessage*>(Message);
  816. if (M->Msg == CM_DPICHANGED)
  817. {
  818. CMDpiChanged(*M);
  819. }
  820. else
  821. {
  822. TForm::Dispatch(Message);
  823. }
  824. }
  825. //---------------------------------------------------------------------------
  826. void __fastcall TPropertiesDialog::TagsViewKeyDown(TObject *, WORD & Key, TShiftState)
  827. {
  828. if (RemoveTagButton->Enabled && (Key == VK_DELETE))
  829. {
  830. RemoveTagButton->OnClick(NULL);
  831. }
  832. if (AddTagButton->Enabled && (Key == VK_INSERT))
  833. {
  834. AddTagButton->OnClick(NULL);
  835. }
  836. }
  837. //---------------------------------------------------------------------------
  838. TListItem * TPropertiesDialog::AddTag(const UnicodeString & Key, const UnicodeString & Value)
  839. {
  840. TListItem * Item = TagsView->Items->Add();
  841. Item->Caption = Key;
  842. Item->SubItems->Add(Value);
  843. return Item;
  844. }
  845. //---------------------------------------------------------------------------
  846. void TPropertiesDialog::AutoSizeTagsView()
  847. {
  848. AutoSizeListColumnsWidth(TagsView, 1);
  849. }
  850. //---------------------------------------------------------------------------
  851. void TPropertiesDialog::AddEditTag(bool Add)
  852. {
  853. std::unique_ptr<TStrings> Tags(CreateSortedStringList(true));
  854. TListItem * ItemFocused = TagsView->ItemFocused;
  855. for (int Index = 0; Index < TagsView->Items->Count; Index++)
  856. {
  857. TListItem * Item = TagsView->Items->Item[Index];
  858. if (Add || (Item != ItemFocused))
  859. {
  860. Tags->Add(Item->Caption);
  861. }
  862. }
  863. UnicodeString Key, Value;
  864. if (!Add)
  865. {
  866. Key = ItemFocused->Caption;
  867. Value = ItemFocused->SubItems->Strings[0];
  868. }
  869. if (DoTagDialog(Add, Tags.get(), Key, Value))
  870. {
  871. if (Add)
  872. {
  873. TagsView->ItemFocused = AddTag(Key, Value);
  874. TagsView->ItemFocused->MakeVisible(false);
  875. }
  876. else
  877. {
  878. ItemFocused->Caption = Key;
  879. ItemFocused->SubItems->Strings[0] = Value;
  880. }
  881. AutoSizeTagsView();
  882. UpdateControls();
  883. }
  884. }
  885. //---------------------------------------------------------------------------
  886. void __fastcall TPropertiesDialog::AddTagButtonClick(TObject *)
  887. {
  888. AddEditTag(true);
  889. }
  890. //---------------------------------------------------------------------------
  891. void __fastcall TPropertiesDialog::TagsViewSelectItem(TObject *, TListItem *, bool Selected)
  892. {
  893. DebugUsedParam(Selected);
  894. UpdateControls();
  895. }
  896. //---------------------------------------------------------------------------
  897. void __fastcall TPropertiesDialog::EditTagButtonClick(TObject *)
  898. {
  899. AddEditTag(false);
  900. }
  901. //---------------------------------------------------------------------------
  902. void __fastcall TPropertiesDialog::RemoveTagButtonClick(TObject *)
  903. {
  904. int Index = TagsView->ItemIndex;
  905. TagsView->ItemFocused->Delete();
  906. int Count = TagsView->Items->Count;
  907. TagsView->ItemIndex = (Index < Count ? Index : Count - 1);
  908. AutoSizeTagsView();
  909. UpdateControls();
  910. }
  911. //---------------------------------------------------------------------------
  912. void __fastcall TPropertiesDialog::TagsViewDblClick(TObject *)
  913. {
  914. if (EditTagButton->Enabled)
  915. {
  916. EditTagButton->OnClick(NULL);
  917. }
  918. }
  919. //---------------------------------------------------------------------------