SynchronizeChecklist.cpp 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864
  1. //---------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include <Common.h>
  5. #include "WinInterface.h"
  6. #include "SynchronizeChecklist.h"
  7. #include <Terminal.h>
  8. #include <TextsWin.h>
  9. #include <CoreMain.h>
  10. #include <VCLCommon.h>
  11. #include <Tools.h>
  12. #include <BaseUtils.hpp>
  13. #include <Math.hpp>
  14. #include <WinConfiguration.h>
  15. #include <GUITools.h>
  16. #include <TerminalManager.h>
  17. #include <System.IOUtils.hpp>
  18. #include <System.StrUtils.hpp>
  19. #include <algorithm>
  20. //---------------------------------------------------------------------
  21. #pragma link "IEListView"
  22. #pragma link "NortonLikeListView"
  23. #pragma link "PngImageList"
  24. #pragma resource "*.dfm"
  25. //---------------------------------------------------------------------
  26. const int ImageColumnIndex = 4;
  27. //---------------------------------------------------------------------
  28. bool __fastcall DoSynchronizeChecklistDialog(TSynchronizeChecklist * Checklist,
  29. TSynchronizeMode Mode, int Params,
  30. const UnicodeString LocalDirectory, const UnicodeString RemoteDirectory,
  31. TCustomCommandMenuEvent OnCustomCommandMenu, TFullSynchronizeEvent OnSynchronize,
  32. TQueueSynchronizeEvent OnQueueSynchronize,
  33. TSynchronizeChecklistCalculateSize OnSynchronizeChecklistCalculateSize, TSynchronizeMoveEvent OnSynchronizeMove,
  34. TSynchronizeExploreEvent OnSynchronizeExplore, void * Token)
  35. {
  36. std::unique_ptr<TSynchronizeChecklistDialog> Dialog(
  37. new TSynchronizeChecklistDialog(
  38. Application, Mode, Params, LocalDirectory, RemoteDirectory, OnCustomCommandMenu, OnSynchronize,
  39. OnQueueSynchronize, OnSynchronizeChecklistCalculateSize, OnSynchronizeMove, OnSynchronizeExplore, Token));
  40. return Dialog->Execute(Checklist);
  41. }
  42. //---------------------------------------------------------------------
  43. __fastcall TSynchronizeChecklistDialog::TSynchronizeChecklistDialog(
  44. TComponent * AOwner, TSynchronizeMode Mode, int Params,
  45. const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  46. TCustomCommandMenuEvent OnCustomCommandMenu, TFullSynchronizeEvent OnSynchronize,
  47. TQueueSynchronizeEvent OnQueueSynchronize,
  48. TSynchronizeChecklistCalculateSize OnSynchronizeChecklistCalculateSize, TSynchronizeMoveEvent OnSynchronizeMove,
  49. TSynchronizeExploreEvent OnSynchronizeExplore, void * Token)
  50. : TForm(AOwner)
  51. {
  52. FFormRestored = false;
  53. FMode = Mode;
  54. FParams = Params;
  55. FLocalDirectory = ExcludeTrailingBackslash(LocalDirectory);
  56. FRemoteDirectory = UnixExcludeTrailingBackslash(RemoteDirectory);
  57. FOnCustomCommandMenu = OnCustomCommandMenu;
  58. FOnSynchronizeChecklistCalculateSize = OnSynchronizeChecklistCalculateSize;
  59. FOnSynchronizeMove = OnSynchronizeMove;
  60. FOnSynchronizeExplore = OnSynchronizeExplore;
  61. DebugAssert(OnSynchronize != NULL);
  62. FOnSynchronize = OnSynchronize;
  63. FOnQueueSynchronize = OnQueueSynchronize;
  64. FToken = Token;
  65. UseSystemSettings(this);
  66. UseDesktopFont(ListView2);
  67. UseDesktopFont(StatusBar);
  68. FChecklist = NULL;
  69. FChangingItem = NULL;
  70. FChangingItemIgnore = false;
  71. FChangingItemMass = false;
  72. FSynchronizing = false;
  73. FMoveCandidatesValidForSort = MaxInt;
  74. FDirectories = 0;
  75. SelectScaledImageList(ActionImages);
  76. FOrigListViewWindowProc = ListView2->WindowProc;
  77. ListView2->WindowProc = ListViewWindowProc;
  78. FOrigStatusBarWindowProc = StatusBar->WindowProc;
  79. StatusBar->WindowProc = StatusBarWindowProc;
  80. UpdateImages();
  81. CustomCommandsAction->Visible = (FOnCustomCommandMenu != NULL);
  82. // button visibility cannot be bound to action visibility
  83. CustomCommandsButton2->Visible = CustomCommandsAction->Visible;
  84. MenuButton(CustomCommandsButton2);
  85. MenuButton(ToolsMenuButton);
  86. // Other dialogs use !IsMainFormLike
  87. if (FOnQueueSynchronize != NULL)
  88. {
  89. OkButton->Style = TCustomButton::bsSplitButton;
  90. }
  91. }
  92. //---------------------------------------------------------------------
  93. __fastcall TSynchronizeChecklistDialog::~TSynchronizeChecklistDialog()
  94. {
  95. StatusBar->WindowProc = FOrigStatusBarWindowProc;
  96. ListView2->WindowProc = FOrigListViewWindowProc;
  97. }
  98. //---------------------------------------------------------------------
  99. bool __fastcall TSynchronizeChecklistDialog::Execute(TSynchronizeChecklist * Checklist)
  100. {
  101. FChecklist = Checklist;
  102. bool Result = (ShowModal() == DefaultResult(this));
  103. if (Result)
  104. {
  105. TSynchronizeChecklistConfiguration FormConfiguration =
  106. CustomWinConfiguration->SynchronizeChecklist;
  107. FormConfiguration.ListParams = ListView2->ColProperties->ParamsStr;
  108. UnicodeString WindowParams = FormConfiguration.WindowParams;
  109. // if there is no main window, keep previous "custom pos" indication,
  110. bool CustomPos = (StrToIntDef(CutToChar(WindowParams, L';', true), 0) != 0);
  111. if (!IsMainFormLike(this))
  112. {
  113. CustomPos = (Application->MainForm->BoundsRect != BoundsRect);
  114. }
  115. FormConfiguration.WindowParams =
  116. FORMAT(L"%d;%s", ((CustomPos ? 1 : 0), StoreForm(this)));
  117. CustomWinConfiguration->SynchronizeChecklist = FormConfiguration;
  118. }
  119. if (FException.get() != NULL)
  120. {
  121. RethrowException(FException.get());
  122. }
  123. return Result;
  124. }
  125. //---------------------------------------------------------------------
  126. void __fastcall TSynchronizeChecklistDialog::UpdateCaption()
  127. {
  128. TTerminalManager * Manager = TTerminalManager::Instance();
  129. UnicodeString Title = Manager->GetAppProgressTitle();
  130. UnicodeString StatusTitle = LoadStr(FSynchronizing ? SYNCHRONIZE_PROGRESS_SYNCHRONIZE2 : SYNCHRONIZE_CHECKLIST_CAPTION);
  131. if (Title.Pos(StatusTitle) <= 0)
  132. {
  133. AddToList(Title, StatusTitle, TitleSeparator);
  134. }
  135. Caption = Manager->FormatFormCaptionWithSession(this, Title);
  136. }
  137. //---------------------------------------------------------------------------
  138. static bool IsTransferNewAction(TSynchronizeChecklist::TAction Action)
  139. {
  140. return (Action == TSynchronizeChecklist::saUploadNew) || (Action == TSynchronizeChecklist::saDownloadNew);
  141. }
  142. //---------------------------------------------------------------------------
  143. static TSynchronizeChecklist::TAction GetOppositeMoveAction(TSynchronizeChecklist::TAction Action1)
  144. {
  145. switch (Action1)
  146. {
  147. case TSynchronizeChecklist::saUploadNew:
  148. return TSynchronizeChecklist::saDeleteRemote;
  149. case TSynchronizeChecklist::saDownloadNew:
  150. return TSynchronizeChecklist::saDeleteLocal;
  151. case TSynchronizeChecklist::saDeleteRemote:
  152. return TSynchronizeChecklist::saUploadNew;
  153. case TSynchronizeChecklist::saDeleteLocal:
  154. return TSynchronizeChecklist::saDownloadNew;
  155. default:
  156. return TSynchronizeChecklist::saNone;
  157. }
  158. }
  159. //---------------------------------------------------------------------
  160. struct TMoveActionData
  161. {
  162. TSynchronizeChecklist::TAction Action;
  163. bool AllItemsFiles;
  164. bool AllItemsDirectories;
  165. int ItemsCount;
  166. std::unique_ptr<TStrings> FileList;
  167. typedef std::vector<const TSynchronizeChecklist::TItem *> TChecklistItems;
  168. TChecklistItems ChecklistItems;
  169. TMoveActionData()
  170. {
  171. Action = TSynchronizeChecklist::saNone;
  172. AllItemsFiles = true;
  173. AllItemsDirectories = true;
  174. ItemsCount = 0;
  175. }
  176. bool Collect(const TSynchronizeChecklist::TItem * ChecklistItem, TSynchronizeChecklist::TAction AAction, bool CollectFileList)
  177. {
  178. bool Result = (ItemsCount == 0) || (Action == AAction);
  179. if (Result)
  180. {
  181. Action = AAction;
  182. if (ChecklistItem->IsDirectory)
  183. {
  184. AllItemsFiles = false;
  185. }
  186. else
  187. {
  188. AllItemsDirectories = false;
  189. }
  190. ItemsCount++;
  191. if (CollectFileList)
  192. {
  193. UnicodeString FileName;
  194. TObject * Object;
  195. if (Action == TSynchronizeChecklist::saDeleteRemote)
  196. {
  197. FileName = ChecklistItem->GetRemotePath();
  198. Object = ChecklistItem->RemoteFile;
  199. }
  200. else if (Action == TSynchronizeChecklist::saDeleteLocal)
  201. {
  202. FileName = ChecklistItem->GetLocalPath();
  203. }
  204. bool CollectFile = !FileName.IsEmpty();
  205. DebugAssert(CollectFile == IsTransferNewAction(GetOppositeMoveAction(Action)));
  206. if (CollectFile)
  207. {
  208. if (FileList.get() == NULL)
  209. {
  210. FileList.reset(new TStringList());
  211. }
  212. FileList->AddObject(FileName, Object);
  213. }
  214. ChecklistItems.push_back(ChecklistItem);
  215. }
  216. }
  217. return Result;
  218. }
  219. bool IsOnlyDirectoryTransferAction()
  220. {
  221. return
  222. IsTransferNewAction(Action) &&
  223. (ItemsCount == 1) &&
  224. AllItemsDirectories &&
  225. DebugAlwaysTrue(!AllItemsFiles);
  226. }
  227. };
  228. //---------------------------------------------------------------------
  229. struct TMoveData
  230. {
  231. TMoveActionData FirstAction;
  232. TMoveActionData SecondAction;
  233. bool ThreeActions;
  234. TMoveData(bool CollectFileList)
  235. {
  236. ThreeActions = false;
  237. FCollectFileList = CollectFileList;
  238. }
  239. void Collect(const TSynchronizeChecklist::TItem * ChecklistItem, TSynchronizeChecklist::TAction Action)
  240. {
  241. if (!FirstAction.Collect(ChecklistItem, Action, FCollectFileList) &&
  242. !SecondAction.Collect(ChecklistItem, Action, FCollectFileList))
  243. {
  244. ThreeActions = true;
  245. }
  246. }
  247. private:
  248. bool FCollectFileList;
  249. };
  250. //---------------------------------------------------------------------
  251. void __fastcall TSynchronizeChecklistDialog::UpdateControls()
  252. {
  253. UpdateCaption();
  254. StatusBar->Invalidate();
  255. bool AllChecked = true;
  256. bool AllUnchecked = true;
  257. bool AnyBoth = false;
  258. bool AnyNonBoth = false;
  259. bool AnyDirectory = false;
  260. TMoveData MoveData(false);
  261. TListItem * Item = NULL;
  262. while (IterateSelectedItems(Item))
  263. {
  264. const TSynchronizeChecklist::TItem * ChecklistItem = GetChecklistItem(Item);
  265. TSynchronizeChecklist::TAction Action = GetChecklistItemAction(ChecklistItem);
  266. if ((Action == TSynchronizeChecklist::saUploadUpdate) ||
  267. (Action == TSynchronizeChecklist::saDownloadUpdate))
  268. {
  269. AnyBoth = true;
  270. }
  271. else
  272. {
  273. AnyNonBoth = true;
  274. }
  275. MoveData.Collect(ChecklistItem, Action);
  276. if (Item->Checked)
  277. {
  278. AllUnchecked = false;
  279. }
  280. else
  281. {
  282. AllChecked = false;
  283. }
  284. if (ChecklistItem->IsDirectory)
  285. {
  286. AnyDirectory = true;
  287. }
  288. }
  289. int SelCount = ListView2->SelCount;
  290. EnableControl(OkButton, (FChecked[0] > 0) && !FSynchronizing);
  291. EnableControl(CancelButton, !FSynchronizing);
  292. EnableControl(HelpButton, !FSynchronizing);
  293. CheckAction->Enabled = !AllChecked && !FSynchronizing;
  294. UncheckAction->Enabled = !AllUnchecked && !FSynchronizing;
  295. CheckAllAction->Enabled = (FChecked[0] < FTotals[0]) && !FSynchronizing;
  296. UncheckAllAction->Enabled = (FChecked[0] > 0) && !FSynchronizing;
  297. CheckDirectoryAction->Enabled = CheckAllAction->Enabled; // sic
  298. UncheckDirectoryAction->Enabled = UncheckAllAction->Enabled; // sic
  299. CustomCommandsAction->Enabled = AnyBoth && !AnyNonBoth && DebugAlwaysTrue(!FSynchronizing);
  300. ReverseAction->Enabled = (SelCount > 0) && DebugAlwaysTrue(!FSynchronizing);
  301. MoveAction->Enabled =
  302. // All actions are of exactly two and opposite types
  303. (MoveData.SecondAction.Action != TSynchronizeChecklist::saNone) &&
  304. !MoveData.ThreeActions &&
  305. (GetOppositeMoveAction(MoveData.FirstAction.Action) == MoveData.SecondAction.Action) &&
  306. // ... and there are exactly two of them of the same file/dir type
  307. (((SelCount == 2) && (MoveData.FirstAction.AllItemsFiles == MoveData.SecondAction.AllItemsFiles)) ||
  308. // ... or the "transfer new" side is exactly one directory
  309. MoveData.FirstAction.IsOnlyDirectoryTransferAction() ||
  310. MoveData.SecondAction.IsOnlyDirectoryTransferAction());
  311. CalculateSizeAction->Enabled = (SelCount > 0) && AnyDirectory && DebugAlwaysTrue(!FSynchronizing);
  312. CalculateSizeAllAction->Enabled = (FDirectories > 0) && !FSynchronizing;
  313. TSynchronizeChecklist::TAction SelectedItemAction =
  314. (SelCount == 1) ? GetChecklistItemAction(GetChecklistItem(ListView2->Selected)) : TSynchronizeChecklist::saNone;
  315. ExploreLocalAction2->Enabled = (SelCount == 1) && (SelectedItemAction != TSynchronizeChecklist::saDeleteRemote);
  316. ExploreRemoteAction2->Enabled = (SelCount == 1) && (SelectedItemAction != TSynchronizeChecklist::saDeleteLocal);
  317. LocalPathToClipboardAction->Enabled = (SelCount > 0);
  318. RemotePathToClipboardAction->Enabled = LocalPathToClipboardAction->Enabled;
  319. int Count = ListView2->Items->Count;
  320. DebugAssert(FTotals[0] == Count);
  321. SelectAllAction->Enabled = (SelCount < Count) && !FSynchronizing;
  322. FindMoveCandidateAction->Enabled = (Count > 0) && !FSynchronizing;
  323. }
  324. //---------------------------------------------------------------------------
  325. bool __fastcall TSynchronizeChecklistDialog::GetWindowParams(UnicodeString & WindowParams)
  326. {
  327. WindowParams = CustomWinConfiguration->SynchronizeChecklist.WindowParams;
  328. bool CustomPos = (StrToIntDef(CutToChar(WindowParams, L';', true), 0) != 0);
  329. return CustomPos || IsMainFormLike(this);
  330. }
  331. //---------------------------------------------------------------------------
  332. void __fastcall TSynchronizeChecklistDialog::CreateParams(TCreateParams & Params)
  333. {
  334. UnicodeString WindowParams;
  335. if (GetWindowParams(WindowParams))
  336. {
  337. // This is only to set correct TForm::Position. Actual bounds are set later after DPI scaling
  338. RestoreForm(WindowParams, this, true);
  339. }
  340. TForm::CreateParams(Params);
  341. }
  342. //---------------------------------------------------------------------------
  343. void __fastcall TSynchronizeChecklistDialog::HelpButtonClick(TObject * /*Sender*/)
  344. {
  345. FormHelp(this);
  346. }
  347. //---------------------------------------------------------------------------
  348. void __fastcall TSynchronizeChecklistDialog::AddSubItem(TListItem * Item, int & Index, const UnicodeString & S)
  349. {
  350. if (Index < Item->SubItems->Count)
  351. {
  352. Item->SubItems->Strings[Index] = S;
  353. }
  354. else
  355. {
  356. DebugAssert(Index == Item->SubItems->Count);
  357. Item->SubItems->Add(S);
  358. }
  359. Index++;
  360. }
  361. //---------------------------------------------------------------------------
  362. void __fastcall TSynchronizeChecklistDialog::LoadItem(TListItem * Item)
  363. {
  364. UnicodeString S;
  365. const TSynchronizeChecklist::TItem * ChecklistItem = GetChecklistItem(Item);
  366. if (ChecklistItem->ImageIndex >= 0)
  367. {
  368. Item->ImageIndex = ChecklistItem->ImageIndex;
  369. }
  370. else
  371. {
  372. Item->ImageIndex = FakeFileImageIndex(ChecklistItem->GetFileName(),
  373. FLAGMASK(ChecklistItem->IsDirectory, FILE_ATTRIBUTE_DIRECTORY));
  374. }
  375. S = ChecklistItem->GetFileName();
  376. if (ChecklistItem->IsDirectory)
  377. {
  378. S = IncludeTrailingBackslash(S);
  379. }
  380. Item->Caption = S;
  381. int Index = 0;
  382. TSynchronizeChecklist::TAction Action = GetChecklistItemAction(ChecklistItem);
  383. if (Action == TSynchronizeChecklist::saDeleteRemote)
  384. {
  385. AddSubItem(Item, Index, L"");
  386. AddSubItem(Item, Index, L"");
  387. AddSubItem(Item, Index, L"");
  388. }
  389. else
  390. {
  391. S = ChecklistItem->Local.Directory;
  392. if (AnsiSameText(FLocalDirectory, S.SubString(1, FLocalDirectory.Length())))
  393. {
  394. S[1] = L'.';
  395. S.Delete(2, FLocalDirectory.Length() - 1);
  396. }
  397. else
  398. {
  399. DebugFail();
  400. }
  401. AddSubItem(Item, Index, S);
  402. if (Action == TSynchronizeChecklist::saDownloadNew)
  403. {
  404. AddSubItem(Item, Index, L"");
  405. AddSubItem(Item, Index, L"");
  406. }
  407. else
  408. {
  409. if (!ChecklistItem->HasSize())
  410. {
  411. AddSubItem(Item, Index, L"");
  412. }
  413. else
  414. {
  415. AddSubItem(Item, Index,
  416. FormatPanelBytes(ChecklistItem->Local.Size, WinConfiguration->FormatSizeBytes));
  417. }
  418. AddSubItem(Item, Index,
  419. UserModificationStr(ChecklistItem->Local.Modification,
  420. ChecklistItem->Local.ModificationFmt));
  421. }
  422. }
  423. AddSubItem(Item, Index, L"");
  424. DebugAssert(Index == ImageColumnIndex);
  425. if (Action == TSynchronizeChecklist::saDeleteLocal)
  426. {
  427. AddSubItem(Item, Index, L"");
  428. AddSubItem(Item, Index, L"");
  429. AddSubItem(Item, Index, L"");
  430. }
  431. else
  432. {
  433. S = ChecklistItem->Remote.Directory;
  434. if (AnsiSameText(FRemoteDirectory, S.SubString(1, FRemoteDirectory.Length())))
  435. {
  436. S[1] = L'.';
  437. S.Delete(2, FRemoteDirectory.Length() - 1);
  438. }
  439. else
  440. {
  441. DebugFail();
  442. }
  443. AddSubItem(Item, Index, S);
  444. if (Action == TSynchronizeChecklist::saUploadNew)
  445. {
  446. AddSubItem(Item, Index, L"");
  447. AddSubItem(Item, Index, L"");
  448. }
  449. else
  450. {
  451. if (!ChecklistItem->HasSize())
  452. {
  453. AddSubItem(Item, Index, L"");
  454. }
  455. else
  456. {
  457. AddSubItem(Item, Index,
  458. FormatPanelBytes(ChecklistItem->Remote.Size, WinConfiguration->FormatSizeBytes));
  459. }
  460. AddSubItem(Item, Index, UserModificationStr(ChecklistItem->Remote.Modification,
  461. ChecklistItem->Remote.ModificationFmt));
  462. }
  463. }
  464. }
  465. //---------------------------------------------------------------------------
  466. void __fastcall TSynchronizeChecklistDialog::CountItemSize(const TSynchronizeChecklist::TItem * ChecklistItem, int Factor)
  467. {
  468. TSynchronizeChecklist::TAction Action = GetChecklistItemAction(ChecklistItem);
  469. int ActionIndex = int(Action);
  470. __int64 ItemSize = ChecklistItem->GetSize(Action);
  471. FCheckedSize[ActionIndex] += Factor * ItemSize;
  472. FCheckedSize[0] += Factor * ItemSize;
  473. }
  474. //---------------------------------------------------------------------------
  475. void __fastcall TSynchronizeChecklistDialog::CountItem(const TSynchronizeChecklist::TItem * ChecklistItem, int Factor)
  476. {
  477. int ActionIndex = int(GetChecklistItemAction(ChecklistItem));
  478. FChecked[ActionIndex] += Factor;
  479. FChecked[0] += Factor;
  480. CountItemSize(ChecklistItem, Factor);
  481. }
  482. //---------------------------------------------------------------------------
  483. void __fastcall TSynchronizeChecklistDialog::CountItemTotal(const TSynchronizeChecklist::TItem * ChecklistItem, int Factor)
  484. {
  485. FTotals[0] += Factor;
  486. int ActionIndex = int(GetChecklistItemAction(ChecklistItem));
  487. FTotals[ActionIndex] += Factor;
  488. if (ChecklistItem->IsDirectory)
  489. {
  490. FDirectories += Factor;
  491. }
  492. }
  493. //---------------------------------------------------------------------------
  494. void __fastcall TSynchronizeChecklistDialog::LoadList()
  495. {
  496. memset(&FTotals, 0, sizeof(FTotals));
  497. memset(&FChecked, 0, sizeof(FChecked));
  498. memset(&FCheckedSize, 0, sizeof(FCheckedSize));
  499. FDirectories = 0;
  500. ListView2->Items->BeginUpdate();
  501. try
  502. {
  503. ListView2->Items->Clear();
  504. for (int Index = 0; Index < FChecklist->Count; Index++)
  505. {
  506. const TSynchronizeChecklist::TItem * ChecklistItem =
  507. FChecklist->Item[ListView2->Items->Count];
  508. FChangingItemIgnore = true;
  509. try
  510. {
  511. TListItem * Item = ListView2->Items->Add();
  512. TSynchronizeChecklist::TAction Action = ChecklistItem->Action;
  513. FActions.insert(std::make_pair(ChecklistItem, Action));
  514. FChecklistToListViewMap.insert(std::make_pair(ChecklistItem, Item));
  515. Item->Data = const_cast<TSynchronizeChecklist::TItem *>(ChecklistItem);
  516. Item->Checked = ChecklistItem->Checked;
  517. LoadItem(Item);
  518. }
  519. __finally
  520. {
  521. FChangingItemIgnore = false;
  522. }
  523. CountItemTotal(ChecklistItem, 1);
  524. if (ChecklistItem->Checked)
  525. {
  526. CountItem(ChecklistItem, 1);
  527. }
  528. }
  529. }
  530. __finally
  531. {
  532. ListView2->Items->EndUpdate();
  533. }
  534. ListView2->AlphaSort();
  535. UpdateControls();
  536. }
  537. //---------------------------------------------------------------------------
  538. void __fastcall TSynchronizeChecklistDialog::FormShow(TObject * /*Sender*/)
  539. {
  540. // Moved here from CreateParams (see also TEditorForm::CreateParams), because there it breaks per-monitor DPI.
  541. // For example BoundsRect is matched to the main form too soon, so it gets rescaled later.
  542. // Also it happens before constructor, what causes UseDesktopFont-flagged controls to rescale twice.
  543. // But Position is already set in the CreateParams, as it cannot be set here anymore.
  544. if (!FFormRestored)
  545. {
  546. FFormRestored = True;
  547. UnicodeString WindowParams;
  548. if (GetWindowParams(WindowParams))
  549. {
  550. RestoreForm(WindowParams, this);
  551. }
  552. else
  553. {
  554. BoundsRect = Application->MainForm->BoundsRect;
  555. }
  556. FlashOnBackground();
  557. }
  558. ListView2->ColProperties->ParamsStr = CustomWinConfiguration->SynchronizeChecklist.ListParams;
  559. LoadList();
  560. UpdateStatusBarSize();
  561. }
  562. //---------------------------------------------------------------------------
  563. TRect __fastcall TSynchronizeChecklistDialog::GetColumnHeaderRect(int Index)
  564. {
  565. HWND HeaderHandle = ListView_GetHeader(ListView2->Handle);
  566. TRect R;
  567. Header_GetItemRect(HeaderHandle, Index, &R);
  568. // Can be simplified using GetScrollPos
  569. TScrollInfo ScrollInfo;
  570. ZeroMemory(&ScrollInfo, sizeof(ScrollInfo));
  571. ScrollInfo.cbSize = sizeof(ScrollInfo);
  572. ScrollInfo.fMask = SIF_POS;
  573. GetScrollInfo(ListView2->Handle, SB_HORZ, &ScrollInfo);
  574. R.Left -= ScrollInfo.nPos;
  575. R.Right -= ScrollInfo.nPos;
  576. return R;
  577. }
  578. //---------------------------------------------------------------------------
  579. void __fastcall TSynchronizeChecklistDialog::ListViewWindowProc(TMessage & Message)
  580. {
  581. if (Message.Msg == CN_NOTIFY)
  582. {
  583. TWMNotify & NotifyMessage = reinterpret_cast<TWMNotify &>(Message);
  584. if (NotifyMessage.NMHdr->code == NM_CUSTOMDRAW)
  585. {
  586. // workaround
  587. // Due to a bug in VCL, OnAdvancedCustomDrawSubItem is not called for any
  588. // other stage except for cdPrePaint. So we must call it ourselves.
  589. TNMLVCustomDraw * CustomDraw =
  590. reinterpret_cast<TNMLVCustomDraw *>(NotifyMessage.NMHdr);
  591. if (FLAGSET(CustomDraw->nmcd.dwDrawStage, CDDS_ITEM) &&
  592. FLAGSET(CustomDraw->nmcd.dwDrawStage, CDDS_SUBITEM) &&
  593. FLAGSET(CustomDraw->nmcd.dwDrawStage, CDDS_ITEMPOSTPAINT) &&
  594. (CustomDraw->iSubItem == ImageColumnIndex) &&
  595. (ActionImages->Width <= ListView2->Columns->Items[CustomDraw->iSubItem]->Width))
  596. {
  597. TListItem * Item = ListView2->Items->Item[CustomDraw->nmcd.dwItemSpec];
  598. const TSynchronizeChecklist::TItem * ChecklistItem = GetChecklistItem(Item);
  599. TRect HeaderR = GetColumnHeaderRect(CustomDraw->iSubItem);
  600. TRect R = Item->DisplayRect(drBounds);
  601. R.Left = HeaderR.Left + (HeaderR.Width() - ActionImages->Width) / 2;
  602. R.Right = HeaderR.Right;
  603. // workaround
  604. // doing this from ListViewAdvancedCustomDraw corrupts list view on Windows 7
  605. ImageList_Draw(reinterpret_cast<HIMAGELIST>(ActionImages->Handle),
  606. int(GetChecklistItemAction(ChecklistItem)), CustomDraw->nmcd.hdc,
  607. R.Left, ((R.Top + R.Bottom - ActionImages->Height) / 2), ILD_TRANSPARENT);
  608. }
  609. }
  610. FOrigListViewWindowProc(Message);
  611. }
  612. else if (Message.Msg == CM_HINTSHOW)
  613. {
  614. ListViewHintShow(reinterpret_cast<TCMHintShow &>(Message));
  615. }
  616. else if (Message.Msg == WM_WANTS_SCREEN_TIPS)
  617. {
  618. Message.Result = 1;
  619. }
  620. else
  621. {
  622. FOrigListViewWindowProc(Message);
  623. }
  624. }
  625. //---------------------------------------------------------------------------
  626. void __fastcall TSynchronizeChecklistDialog::ListViewHintShow(TCMHintShow & HintShow)
  627. {
  628. TLVHitTestInfo HitTest;
  629. HitTest.pt = HintShow.HintInfo->CursorPos;
  630. int Index = ListView_SubItemHitTest(ListView2->Handle, &HitTest);
  631. if ((Index >= 0) && (HitTest.iSubItem == ImageColumnIndex))
  632. {
  633. TListItem * Item = ListView2->Items->Item[Index];
  634. const TSynchronizeChecklist::TItem * ChecklistItem = GetChecklistItem(Item);
  635. int ActionHint = 0;
  636. switch (int(GetChecklistItemAction(ChecklistItem)))
  637. {
  638. case TSynchronizeChecklist::saUploadNew:
  639. ActionHint = SYNCHRONIZE_CHECKLIST_UPLOAD_NEW;
  640. break;
  641. case TSynchronizeChecklist::saDownloadNew:
  642. ActionHint = SYNCHRONIZE_CHECKLIST_DOWNLOAD_NEW;
  643. break;
  644. case TSynchronizeChecklist::saUploadUpdate:
  645. ActionHint = SYNCHRONIZE_CHECKLIST_UPLOAD_UPDATE;
  646. break;
  647. case TSynchronizeChecklist::saDownloadUpdate:
  648. ActionHint = SYNCHRONIZE_CHECKLIST_DOWNLOAD_UPDATE;
  649. break;
  650. case TSynchronizeChecklist::saDeleteRemote:
  651. ActionHint = SYNCHRONIZE_CHECKLIST_DELETE_REMOTE;
  652. break;
  653. case TSynchronizeChecklist::saDeleteLocal:
  654. ActionHint = SYNCHRONIZE_CHECKLIST_DELETE_LOCAL;
  655. break;
  656. }
  657. if (DebugAlwaysTrue(ActionHint != 0))
  658. {
  659. HintShow.HintInfo->HintStr = FORMAT(L"%s|%s", (LoadStr(ActionHint), LoadStr(SYNCHRONIZE_CHECKLIST_REVERSE)));
  660. ListView_GetSubItemRect(ListView2->Handle, Index, ImageColumnIndex, LVIR_BOUNDS, &HintShow.HintInfo->CursorRect);
  661. HintShow.Result = 0;
  662. }
  663. }
  664. FOrigListViewWindowProc(reinterpret_cast<TMessage &>(HintShow));
  665. }
  666. //---------------------------------------------------------------------------
  667. void __fastcall TSynchronizeChecklistDialog::StatusBarHintShow(TCMHintShow & HintShow)
  668. {
  669. int IPanel = PanelAt(HintShow.HintInfo->CursorPos.x);
  670. if (IPanel >= 0)
  671. {
  672. TStatusPanel * Panel = StatusBar->Panels->Items[IPanel];
  673. HintShow.HintInfo->HintStr = FORMAT(L"%s|%s", (Panel->Text, StatusBar->Hint));
  674. HintShow.HintInfo->CursorRect.Left = 0;
  675. while (IPanel > 0)
  676. {
  677. IPanel--;
  678. HintShow.HintInfo->CursorRect.Left += StatusBar->Panels->Items[IPanel]->Width;
  679. }
  680. HintShow.HintInfo->CursorRect.Top = 0;
  681. HintShow.HintInfo->CursorRect.Bottom = StatusBar->ClientHeight;
  682. HintShow.HintInfo->CursorRect.Right = HintShow.HintInfo->CursorRect.Left + Panel->Width;
  683. HintShow.Result = 0;
  684. }
  685. FOrigListViewWindowProc(reinterpret_cast<TMessage &>(HintShow));
  686. }
  687. //---------------------------------------------------------------------------
  688. void __fastcall TSynchronizeChecklistDialog::StatusBarWindowProc(TMessage & Message)
  689. {
  690. if (Message.Msg == WM_WANTS_SCREEN_TIPS)
  691. {
  692. Message.Result = 1;
  693. }
  694. else if (Message.Msg == CM_HINTSHOW)
  695. {
  696. StatusBarHintShow(reinterpret_cast<TCMHintShow &>(Message));
  697. }
  698. else
  699. {
  700. FOrigStatusBarWindowProc(Message);
  701. }
  702. }
  703. //---------------------------------------------------------------------------
  704. void __fastcall TSynchronizeChecklistDialog::ListView2AdvancedCustomDrawSubItem(
  705. TCustomListView * /*Sender*/, TListItem * /*Item*/, int /*SubItem*/,
  706. TCustomDrawState /*State*/, TCustomDrawStage /*Stage*/, bool & /*DefaultDraw*/)
  707. {
  708. // this is just fake handler that makes the list view request custom draw notification above
  709. }
  710. //---------------------------------------------------------------------------
  711. void __fastcall TSynchronizeChecklistDialog::StatusBarDrawPanel(
  712. TStatusBar * StatusBar, TStatusPanel * Panel, const TRect & Rect)
  713. {
  714. bool Possible;
  715. int ActionIndex = Panel->Index;
  716. TSynchronizeChecklist::TAction Action = TSynchronizeChecklist::TAction(ActionIndex);
  717. if (FTotals[ActionIndex] > 0)
  718. {
  719. // if direction is overriden to an action that is otherwise not possible
  720. // in given synchronization mode, we still want to show the stats
  721. Possible = true;
  722. }
  723. else
  724. {
  725. switch (Action)
  726. {
  727. case TSynchronizeChecklist::saNone:
  728. Possible = true;
  729. break;
  730. case TSynchronizeChecklist::saUploadNew:
  731. Possible = ((FMode == smRemote) || (FMode == smBoth)) &&
  732. FLAGCLEAR(FParams, TTerminal::spTimestamp);
  733. break;
  734. case TSynchronizeChecklist::saDownloadNew:
  735. Possible = ((FMode == smLocal) || (FMode == smBoth)) &&
  736. FLAGCLEAR(FParams, TTerminal::spTimestamp);
  737. break;
  738. case TSynchronizeChecklist::saUploadUpdate:
  739. Possible =
  740. ((FMode == smRemote) || (FMode == smBoth)) &&
  741. (FLAGCLEAR(FParams, TTerminal::spNotByTime) || FLAGSET(FParams, TTerminal::spBySize) || FLAGSET(FParams, TTerminal::spByChecksum));
  742. break;
  743. case TSynchronizeChecklist::saDownloadUpdate:
  744. Possible =
  745. ((FMode == smLocal) || (FMode == smBoth)) &&
  746. (FLAGCLEAR(FParams, TTerminal::spNotByTime) || FLAGSET(FParams, TTerminal::spBySize) || FLAGSET(FParams, TTerminal::spByChecksum));
  747. break;
  748. case TSynchronizeChecklist::saDeleteRemote:
  749. Possible = (FMode == smRemote) &&
  750. FLAGCLEAR(FParams, TTerminal::spTimestamp);
  751. break;
  752. case TSynchronizeChecklist::saDeleteLocal:
  753. Possible = (FMode == smLocal) &&
  754. FLAGCLEAR(FParams, TTerminal::spTimestamp);
  755. break;
  756. default:
  757. DebugFail();
  758. Possible = false;
  759. break;
  760. }
  761. }
  762. UnicodeString PanelText;
  763. if (Possible)
  764. {
  765. PanelText = FORMAT(LoadStrPart(SYNCHRONIZE_SELECTED_ACTIONS, 1),
  766. (FormatNumber(FChecked[ActionIndex]),
  767. FormatNumber(FTotals[ActionIndex])));
  768. if ((FChecked[ActionIndex] > 0) &&
  769. ((ActionIndex == 0) || !TSynchronizeChecklist::IsItemSizeIrrelevant(Action)))
  770. {
  771. PanelText += FORMAT(L" (%s)", (FormatBytes(FCheckedSize[ActionIndex])));
  772. }
  773. }
  774. else
  775. {
  776. PanelText = LoadStrPart(SYNCHRONIZE_SELECTED_ACTIONS, 2);
  777. }
  778. int TextHeight = StatusBar->Canvas->TextHeight(PanelText);
  779. int X = Rect.Left + ActionImages->Width + 4;
  780. int Y = (Rect.Top + Rect.Bottom - TextHeight) / 2;
  781. StatusBar->Canvas->TextRect(Rect, X, Y, PanelText);
  782. X = Rect.Left + 1;
  783. Y = ((Rect.Top + Rect.Bottom - ActionImages->Height) / 2);
  784. int ImageIndex = ActionIndex;
  785. ActionImages->Draw(StatusBar->Canvas, X, Y, ImageIndex, Possible);
  786. }
  787. //---------------------------------------------------------------------------
  788. int __fastcall TSynchronizeChecklistDialog::PanelCount()
  789. {
  790. // last "panel" is technical
  791. return StatusBar->Panels->Count - 1;
  792. }
  793. //---------------------------------------------------------------------------
  794. int __fastcall TSynchronizeChecklistDialog::PanelAt(int X)
  795. {
  796. int Result = 0;
  797. while ((X > StatusBar->Panels->Items[Result]->Width) &&
  798. (Result < PanelCount()))
  799. {
  800. X -= StatusBar->Panels->Items[Result]->Width;
  801. Result++;
  802. }
  803. return ((Result < StatusBar->Panels->Count - 1) ? Result : -1);
  804. }
  805. //---------------------------------------------------------------------------
  806. void __fastcall TSynchronizeChecklistDialog::ListView2Change(
  807. TObject * /*Sender*/, TListItem * Item, TItemChange Change)
  808. {
  809. if ((Change == ctState) && (FChangingItem == Item) && (FChangingItem != NULL))
  810. {
  811. if (!FChangingItemIgnore)
  812. {
  813. DebugAssert(Item->Data != NULL);
  814. if ((FChangingItemChecked != Item->Checked) && (Item->Data != NULL))
  815. {
  816. const TSynchronizeChecklist::TItem * ChecklistItem = GetChecklistItem(Item);
  817. CountItem(ChecklistItem, Item->Checked ? 1 : -1);
  818. if (!FChangingItemMass)
  819. {
  820. UpdateControls();
  821. }
  822. }
  823. }
  824. FChangingItem = NULL;
  825. }
  826. }
  827. //---------------------------------------------------------------------------
  828. void __fastcall TSynchronizeChecklistDialog::ListView2Changing(
  829. TObject * /*Sender*/, TListItem * Item, TItemChange Change,
  830. bool & /*AllowChange*/)
  831. {
  832. if (Change == ctState)
  833. {
  834. FChangingItem = Item;
  835. FChangingItemChecked = Item->Checked;
  836. }
  837. else
  838. {
  839. DebugAssert(FChangingItem == NULL);
  840. FChangingItem = NULL;
  841. }
  842. }
  843. //---------------------------------------------------------------------------
  844. void __fastcall TSynchronizeChecklistDialog::CheckAll(bool Check)
  845. {
  846. FChangingItemMass = true;
  847. try
  848. {
  849. for (int Index = 0; Index < ListView2->Items->Count; Index++)
  850. {
  851. ListView2->Items->Item[Index]->Checked = Check;
  852. }
  853. }
  854. __finally
  855. {
  856. FChangingItemMass = false;
  857. }
  858. UpdateControls();
  859. }
  860. //---------------------------------------------------------------------------
  861. void __fastcall TSynchronizeChecklistDialog::CheckAllActionExecute(TObject * /*Sender*/)
  862. {
  863. CheckAll(true);
  864. }
  865. //---------------------------------------------------------------------------
  866. void __fastcall TSynchronizeChecklistDialog::UncheckAllActionExecute(TObject * /*Sender*/)
  867. {
  868. CheckAll(false);
  869. }
  870. //---------------------------------------------------------------------------
  871. bool TSynchronizeChecklistDialog::IterateItems(TListItem *& Item, TItemStates States)
  872. {
  873. Item = ListView2->GetNextItem(Item, sdAll, States);
  874. return (Item != NULL);
  875. }
  876. //---------------------------------------------------------------------------
  877. bool TSynchronizeChecklistDialog::IterateSelectedItems(TListItem *& Item)
  878. {
  879. return IterateItems(Item, TItemStates() << isSelected);
  880. }
  881. //---------------------------------------------------------------------------
  882. void __fastcall TSynchronizeChecklistDialog::Check(bool Check)
  883. {
  884. FChangingItemMass = true;
  885. try
  886. {
  887. TListItem * Item = NULL;
  888. while (IterateSelectedItems(Item))
  889. {
  890. Item->Checked = Check;
  891. }
  892. }
  893. __finally
  894. {
  895. FChangingItemMass = false;
  896. }
  897. UpdateControls();
  898. }
  899. //---------------------------------------------------------------------------
  900. void __fastcall TSynchronizeChecklistDialog::CheckActionExecute(TObject * /*Sender*/)
  901. {
  902. Check(true);
  903. }
  904. //---------------------------------------------------------------------------
  905. void __fastcall TSynchronizeChecklistDialog::UncheckActionExecute(TObject * /*Sender*/)
  906. {
  907. Check(false);
  908. }
  909. //---------------------------------------------------------------------------
  910. void __fastcall TSynchronizeChecklistDialog::ListView2SelectItem(
  911. TObject * /*Sender*/, TListItem * /*Item*/, bool /*Selected*/)
  912. {
  913. // Delayed update of button status in case many items are being selected at once
  914. // Also change of selection causes buttons to flash, as for short period of time,
  915. // no item is selected
  916. UpdateTimer->Enabled = true;
  917. }
  918. //---------------------------------------------------------------------------
  919. void __fastcall TSynchronizeChecklistDialog::UpdateTimerTimer(
  920. TObject * /*Sender*/)
  921. {
  922. UpdateTimer->Enabled = false;
  923. UpdateControls();
  924. }
  925. //---------------------------------------------------------------------------
  926. TListItem * __fastcall TSynchronizeChecklistDialog::SelectAll(bool Select, int Action,
  927. bool OnlyTheAction)
  928. {
  929. TListItem * Result = NULL;
  930. for (int Index = 0; Index < ListView2->Items->Count; Index++)
  931. {
  932. TListItem * Item = ListView2->Items->Item[Index];
  933. if (Action == 0)
  934. {
  935. Item->Selected = Select;
  936. if (Result == NULL)
  937. {
  938. Result = Item;
  939. }
  940. }
  941. else
  942. {
  943. const TSynchronizeChecklist::TItem * ChecklistItem = GetChecklistItem(Item);
  944. bool WantedAction = (int(GetChecklistItemAction(ChecklistItem)) == Action);
  945. if (WantedAction || !OnlyTheAction)
  946. {
  947. Item->Selected = Select && WantedAction;
  948. if (WantedAction && (Result == NULL))
  949. {
  950. Result = Item;
  951. }
  952. }
  953. }
  954. }
  955. return Result;
  956. }
  957. //---------------------------------------------------------------------------
  958. void __fastcall TSynchronizeChecklistDialog::SelectAllActionExecute(
  959. TObject * /*Sender*/)
  960. {
  961. SelectAll(true);
  962. }
  963. //---------------------------------------------------------------------------
  964. void __fastcall TSynchronizeChecklistDialog::StatusBarMouseDown(
  965. TObject * /*Sender*/, TMouseButton /*Button*/, TShiftState Shift, int X,
  966. int /*Y*/)
  967. {
  968. int IPanel = PanelAt(X);
  969. if (IPanel >= 0)
  970. {
  971. TListItem * Item = SelectAll(true, IPanel, Shift.Contains(ssCtrl));
  972. if (Item != NULL)
  973. {
  974. Item->MakeVisible(false);
  975. Item->Focused = true;
  976. ListView2->SetFocus();
  977. }
  978. }
  979. }
  980. //---------------------------------------------------------------------------
  981. TIEListViewColProperties * TSynchronizeChecklistDialog::GetColProperties()
  982. {
  983. return dynamic_cast<TIEListViewColProperties *>(ListView2->ColProperties);
  984. }
  985. //---------------------------------------------------------------------------
  986. void __fastcall TSynchronizeChecklistDialog::ListView2Compare(
  987. TObject * /*Sender*/, TListItem * Item1, TListItem * Item2, int /*Data*/,
  988. int & Compare)
  989. {
  990. const TSynchronizeChecklist::TItem * ChecklistItem1 = GetChecklistItem(Item1);
  991. const TSynchronizeChecklist::TItem * ChecklistItem2 = GetChecklistItem(Item2);
  992. TIEListViewColProperties * ColProperties = GetColProperties();
  993. switch (ColProperties->SortColumn)
  994. {
  995. case 0: // name
  996. Compare = AnsiCompareText(ChecklistItem1->GetFileName(), ChecklistItem2->GetFileName());
  997. break;
  998. // sorting by local and remote dir is the same
  999. case 1: // local dir
  1000. case 5: // remote dir
  1001. Compare = 0; // default sorting
  1002. break;
  1003. case 2: // local size
  1004. Compare = CompareNumber(ChecklistItem1->Local.Size, ChecklistItem2->Local.Size);
  1005. break;
  1006. case 3: // local changed
  1007. Compare = CompareFileTime(ChecklistItem1->Local.Modification,
  1008. ChecklistItem2->Local.Modification);
  1009. break;
  1010. case ImageColumnIndex: // action
  1011. Compare = CompareNumber(GetChecklistItemAction(ChecklistItem1), GetChecklistItemAction(ChecklistItem2));
  1012. break;
  1013. case 6: // remote size
  1014. Compare = CompareNumber(ChecklistItem1->Remote.Size, ChecklistItem2->Remote.Size);
  1015. break;
  1016. case 7: // remote changed
  1017. Compare = CompareFileTime(ChecklistItem1->Remote.Modification,
  1018. ChecklistItem2->Remote.Modification);
  1019. break;
  1020. }
  1021. if (Compare == 0)
  1022. {
  1023. Compare = TSynchronizeChecklist::Compare(ChecklistItem1, ChecklistItem2);
  1024. }
  1025. if (!ColProperties->SortAscending)
  1026. {
  1027. Compare = -Compare;
  1028. }
  1029. }
  1030. //---------------------------------------------------------------------------
  1031. void __fastcall TSynchronizeChecklistDialog::ListView2SecondaryColumnHeader(
  1032. TCustomIEListView * /*Sender*/, int Index, int & SecondaryColumn)
  1033. {
  1034. // "remote dir" column is sorting alias for "local dir" column
  1035. if (Index == 5)
  1036. {
  1037. SecondaryColumn = 1;
  1038. }
  1039. else
  1040. {
  1041. SecondaryColumn = -1;
  1042. }
  1043. }
  1044. //---------------------------------------------------------------------------
  1045. void __fastcall TSynchronizeChecklistDialog::ListView2ContextPopup(
  1046. TObject * Sender, TPoint & MousePos, bool & Handled)
  1047. {
  1048. // to update source popup menu before TBX menu is created
  1049. UpdateControls();
  1050. MenuPopup(Sender, MousePos, Handled);
  1051. }
  1052. //---------------------------------------------------------------------------
  1053. void __fastcall TSynchronizeChecklistDialog::CustomCommandsActionExecute(
  1054. TObject * /*Sender*/)
  1055. {
  1056. TStrings * LocalFileList = new TStringList();
  1057. TStrings * RemoteFileList = new TStringList();
  1058. try
  1059. {
  1060. TListItem * Item = NULL;
  1061. while (IterateSelectedItems(Item))
  1062. {
  1063. const TSynchronizeChecklist::TItem * ChecklistItem = GetChecklistItem(Item);
  1064. DebugAssert((GetChecklistItemAction(ChecklistItem) == TSynchronizeChecklist::saUploadUpdate) ||
  1065. (GetChecklistItemAction(ChecklistItem) == TSynchronizeChecklist::saDownloadUpdate));
  1066. DebugAssert(ChecklistItem->RemoteFile != NULL);
  1067. UnicodeString LocalPath = ChecklistItem->GetLocalPath();
  1068. LocalFileList->Add(LocalPath);
  1069. UnicodeString RemotePath = ChecklistItem->GetRemotePath();
  1070. RemoteFileList->AddObject(RemotePath, ChecklistItem->RemoteFile);
  1071. }
  1072. }
  1073. catch(...)
  1074. {
  1075. delete LocalFileList;
  1076. delete RemoteFileList;
  1077. throw;
  1078. }
  1079. DebugAssert(FOnCustomCommandMenu != NULL);
  1080. FOnCustomCommandMenu(CustomCommandsAction, LocalFileList, RemoteFileList);
  1081. }
  1082. //---------------------------------------------------------------------------
  1083. void __fastcall TSynchronizeChecklistDialog::UpdateStatusBarSize()
  1084. {
  1085. int PanelWidth = Min(StatusBar->Width / PanelCount(), ScaleByTextHeight(this, 160));
  1086. for (int Index = 0; Index < PanelCount(); Index++)
  1087. {
  1088. StatusBar->Panels->Items[Index]->Width = PanelWidth;
  1089. }
  1090. }
  1091. //---------------------------------------------------------------------------
  1092. void __fastcall TSynchronizeChecklistDialog::StatusBarResize(TObject * /*Sender*/)
  1093. {
  1094. UpdateStatusBarSize();
  1095. }
  1096. //---------------------------------------------------------------------------
  1097. const TSynchronizeChecklist::TItem * TSynchronizeChecklistDialog::GetChecklistItem(
  1098. TListItem * Item)
  1099. {
  1100. return static_cast<const TSynchronizeChecklist::TItem *>(Item->Data);
  1101. }
  1102. //---------------------------------------------------------------------------
  1103. TSynchronizeChecklist::TAction & TSynchronizeChecklistDialog::GetChecklistItemAction(
  1104. const TSynchronizeChecklist::TItem * ChecklistItem)
  1105. {
  1106. TActions::iterator i = FActions.find(ChecklistItem);
  1107. if (i == FActions.end())
  1108. {
  1109. throw EInvalidOperation(L"");
  1110. }
  1111. return i->second;
  1112. }
  1113. //---------------------------------------------------------------------------
  1114. void __fastcall TSynchronizeChecklistDialog::ReverseActionExecute(TObject * /*Sender*/)
  1115. {
  1116. TAutoFlag Flag(FChangingItemMass);
  1117. TListItem * Item = NULL;
  1118. while (IterateSelectedItems(Item))
  1119. {
  1120. const TSynchronizeChecklist::TItem * ChecklistItem = GetChecklistItem(Item);
  1121. TSynchronizeChecklist::TAction & Action = GetChecklistItemAction(ChecklistItem);
  1122. TSynchronizeChecklist::TAction NewAction = TSynchronizeChecklist::Reverse(Action);
  1123. if (DebugAlwaysTrue(Action != NewAction))
  1124. {
  1125. CountItemTotal(ChecklistItem, -1);
  1126. if (Item->Checked)
  1127. {
  1128. CountItem(ChecklistItem, -1);
  1129. }
  1130. Action = NewAction;
  1131. CountItemTotal(ChecklistItem, 1);
  1132. if (Item->Checked)
  1133. {
  1134. // item size may differ with action (0 for delete, but non-0 for new file transfer)
  1135. CountItem(ChecklistItem, 1);
  1136. }
  1137. LoadItem(Item);
  1138. }
  1139. }
  1140. Flag.Release();
  1141. UpdateControls();
  1142. }
  1143. //---------------------------------------------------------------------------
  1144. void __fastcall TSynchronizeChecklistDialog::ListView2Click(TObject * /*Sender*/)
  1145. {
  1146. TKeyboardState KeyState;
  1147. GetKeyboardState(KeyState);
  1148. TShiftState ShiftState = KeyboardStateToShiftState(KeyState);
  1149. // when selecting, do not reverse, even when user clicked on action column
  1150. if (!ShiftState.Contains(ssShift) && !ShiftState.Contains(ssCtrl))
  1151. {
  1152. TPoint P = ListView2->ScreenToClient(Mouse->CursorPos);
  1153. TRect R = GetColumnHeaderRect(ImageColumnIndex);
  1154. if ((R.Left <= P.x) && (P.x <= R.Right))
  1155. {
  1156. // If no item was selected before the click, the action is not enabled yet here,
  1157. // and Execute would be noop, force update
  1158. UpdateControls();
  1159. ReverseAction->Execute();
  1160. }
  1161. }
  1162. }
  1163. //---------------------------------------------------------------------------
  1164. void __fastcall TSynchronizeChecklistDialog::Dispatch(void * AMessage)
  1165. {
  1166. TMessage & Message = *reinterpret_cast<TMessage *>(AMessage);
  1167. if (Message.Msg == WM_SYSCOMMAND)
  1168. {
  1169. if (!HandleMinimizeSysCommand(Message))
  1170. {
  1171. TForm::Dispatch(AMessage);
  1172. }
  1173. }
  1174. else if (Message.Msg == WM_WANTS_MOUSEWHEEL_INACTIVE)
  1175. {
  1176. Message.Result = FSynchronizing ? 1 : 0;
  1177. }
  1178. else if (Message.Msg == WM_MANAGES_CAPTION)
  1179. {
  1180. // calling UpdateControls would cause status bar flicker
  1181. UpdateCaption();
  1182. Message.Result = 1;
  1183. }
  1184. else
  1185. {
  1186. TForm::Dispatch(AMessage);
  1187. }
  1188. }
  1189. //---------------------------------------------------------------------------
  1190. void __fastcall TSynchronizeChecklistDialog::UpdateImages()
  1191. {
  1192. ListView2->SmallImages = ShellImageListForControl(this, ilsSmall);
  1193. }
  1194. //---------------------------------------------------------------------------
  1195. void __fastcall TSynchronizeChecklistDialog::FormAfterMonitorDpiChanged(TObject *, int OldDPI, int NewDPI)
  1196. {
  1197. DebugUsedParam2(OldDPI, NewDPI);
  1198. UpdateImages();
  1199. UpdateStatusBarSize();
  1200. }
  1201. //---------------------------------------------------------------------------
  1202. void __fastcall TSynchronizeChecklistDialog::ProcessedItem(void * /*Token*/, const TSynchronizeChecklist::TItem * ChecklistItem)
  1203. {
  1204. TListItem * Item = FChecklistToListViewMap[ChecklistItem];
  1205. DebugAssert(Item->Checked);
  1206. Item->Checked = false;
  1207. Item->MakeVisible(false);
  1208. }
  1209. //---------------------------------------------------------------------------
  1210. void __fastcall TSynchronizeChecklistDialog::UpdatedSynchronizationChecklistItems(
  1211. const TSynchronizeChecklist::TItemList & Items)
  1212. {
  1213. TSynchronizeChecklist::TItemList::const_iterator Iter = Items.begin();
  1214. while (Iter != Items.end())
  1215. {
  1216. const TSynchronizeChecklist::TItem * ChecklistItem = *Iter;
  1217. TListItem * Item = FChecklistToListViewMap[ChecklistItem];
  1218. LoadItem(Item);
  1219. // When called from FOnSynchronize, we rely on a caller never to update size of an item that had size already,
  1220. // otherwise we get it counted twice here.
  1221. if (Item->Checked)
  1222. {
  1223. CountItemSize(ChecklistItem, 1);
  1224. }
  1225. Iter++;
  1226. }
  1227. UpdateControls();
  1228. }
  1229. //---------------------------------------------------------------------------
  1230. void TSynchronizeChecklistDialog::DoSynchronize(bool Queue)
  1231. {
  1232. ListView2->SelectAll(smNone);
  1233. for (int Index = 0; Index < ListView2->Items->Count; Index++)
  1234. {
  1235. TListItem * Item = ListView2->Items->Item[Index];
  1236. const TSynchronizeChecklist::TItem * ChecklistItem = GetChecklistItem(Item);
  1237. FChecklist->Update(ChecklistItem, Item->Checked, GetChecklistItemAction(ChecklistItem));
  1238. }
  1239. TAutoFlag Flag(FSynchronizing);
  1240. UpdateControls();
  1241. try
  1242. {
  1243. if (Queue)
  1244. {
  1245. FOnQueueSynchronize(FToken);
  1246. }
  1247. else
  1248. {
  1249. FOnSynchronize(FToken, ProcessedItem, UpdatedSynchronizationChecklistItems);
  1250. }
  1251. }
  1252. catch (Exception & E)
  1253. {
  1254. FException.reset(CloneException(&E));
  1255. }
  1256. // Needed when called from the drop down menu
  1257. ModalResult = OkButton->ModalResult;
  1258. }
  1259. //---------------------------------------------------------------------------
  1260. void __fastcall TSynchronizeChecklistDialog::OkButtonClick(TObject *)
  1261. {
  1262. bool Queue = OpenInNewWindow();
  1263. if (Queue && (FOnQueueSynchronize == NULL))
  1264. {
  1265. Beep();
  1266. }
  1267. else
  1268. {
  1269. DoSynchronize(Queue);
  1270. }
  1271. }
  1272. //---------------------------------------------------------------------------
  1273. void TSynchronizeChecklistDialog::CalculateSize(bool All)
  1274. {
  1275. TItemStates States;
  1276. if (!All)
  1277. {
  1278. States << isSelected;
  1279. }
  1280. TSynchronizeChecklist::TItemList Items;
  1281. TListItem * Item = NULL;
  1282. while (IterateItems(Item, States))
  1283. {
  1284. const TSynchronizeChecklist::TItem * ChecklistItem = GetChecklistItem(Item);
  1285. Items.push_back(ChecklistItem);
  1286. if (Item->Checked)
  1287. {
  1288. CountItemSize(ChecklistItem, -1);
  1289. }
  1290. }
  1291. try
  1292. {
  1293. FOnSynchronizeChecklistCalculateSize(FChecklist, Items, FToken);
  1294. }
  1295. __finally
  1296. {
  1297. UpdatedSynchronizationChecklistItems(Items);
  1298. }
  1299. }
  1300. //---------------------------------------------------------------------------
  1301. void __fastcall TSynchronizeChecklistDialog::CalculateSizeActionExecute(TObject *)
  1302. {
  1303. CalculateSize(false);
  1304. }
  1305. //---------------------------------------------------------------------------
  1306. void __fastcall TSynchronizeChecklistDialog::CalculateSizeAllActionExecute(TObject *)
  1307. {
  1308. CalculateSize(true);
  1309. }
  1310. //---------------------------------------------------------------------------
  1311. void __fastcall TSynchronizeChecklistDialog::DeleteItem(const TSynchronizeChecklist::TItem * ChecklistItem)
  1312. {
  1313. TListItem * Item = FChecklistToListViewMap[ChecklistItem];
  1314. CountItemTotal(ChecklistItem, -1);
  1315. if (Item->Checked)
  1316. {
  1317. CountItem(ChecklistItem, -1);
  1318. }
  1319. FActions.erase(ChecklistItem);
  1320. FChecklistToListViewMap.erase(ChecklistItem);
  1321. FChecklist->Delete(ChecklistItem);
  1322. int Index = Item->Index;
  1323. if (Item->Focused)
  1324. {
  1325. int FocusIndex = Index;
  1326. if (FocusIndex == ListView2->Items->Count - 1)
  1327. {
  1328. FocusIndex--;
  1329. }
  1330. else
  1331. {
  1332. FocusIndex++;
  1333. }
  1334. ListView2->ItemFocused = ListView2->Items->Item[FocusIndex];
  1335. }
  1336. ListView2->Items->Delete(Index);
  1337. FMoveCandidatesValidForSort = MaxInt; // can be optimized
  1338. }
  1339. //---------------------------------------------------------------------------
  1340. void __fastcall TSynchronizeChecklistDialog::MoveActionExecute(TObject *)
  1341. {
  1342. TMoveData MoveData(true);
  1343. TListItem * Item = NULL;
  1344. while (IterateSelectedItems(Item))
  1345. {
  1346. const TSynchronizeChecklist::TItem * ChecklistItem = GetChecklistItem(Item);
  1347. TSynchronizeChecklist::TAction Action = GetChecklistItemAction(ChecklistItem);
  1348. MoveData.Collect(ChecklistItem, Action);
  1349. }
  1350. TMoveActionData * TransferAction = &MoveData.FirstAction;
  1351. TMoveActionData * DeleteAction = &MoveData.SecondAction;
  1352. if (IsTransferNewAction(DeleteAction->Action))
  1353. {
  1354. std::swap(TransferAction, DeleteAction);
  1355. }
  1356. DebugAssert(IsTransferNewAction(TransferAction->Action));
  1357. DebugAssert(GetOppositeMoveAction(DeleteAction->Action) == TransferAction->Action);
  1358. TOperationSide Side;
  1359. UnicodeString NewFileName;
  1360. DebugAssert(TransferAction->ChecklistItems.size() == 1);
  1361. const TSynchronizeChecklist::TItem * TransferChecklistItem = TransferAction->ChecklistItems[0];
  1362. if (DeleteAction->Action == TSynchronizeChecklist::saDeleteRemote)
  1363. {
  1364. Side = osRemote;
  1365. NewFileName = UnixCombinePaths(TransferChecklistItem->Remote.Directory, TransferChecklistItem->Local.FileName);
  1366. }
  1367. else if (DebugAlwaysTrue(DeleteAction->Action == TSynchronizeChecklist::saDeleteLocal))
  1368. {
  1369. Side = osLocal;
  1370. NewFileName = CombinePaths(TransferChecklistItem->Local.Directory, TransferChecklistItem->Remote.FileName);
  1371. }
  1372. if (DebugAlwaysTrue(!NewFileName.IsEmpty()))
  1373. {
  1374. bool Move = false;
  1375. bool TargetIsDirectory;
  1376. if ((TransferAction->AllItemsDirectories == DeleteAction->AllItemsDirectories) &&
  1377. DebugAlwaysTrue(TransferAction->ItemsCount == 1) &&
  1378. DebugAlwaysTrue(DeleteAction->ItemsCount == 1))
  1379. {
  1380. Move = true;
  1381. TargetIsDirectory = false;
  1382. }
  1383. else if (DebugAlwaysTrue(TransferAction->AllItemsDirectories && (TransferAction->ItemsCount == 1)))
  1384. {
  1385. TargetIsDirectory = true;
  1386. Move = true;
  1387. }
  1388. if (DebugAlwaysTrue(Move))
  1389. {
  1390. FOnSynchronizeMove(Side, DeleteAction->FileList.get(), NewFileName, TargetIsDirectory, FToken);
  1391. TMoveActionData::TChecklistItems::const_iterator I = DeleteAction->ChecklistItems.begin();
  1392. while (I != DeleteAction->ChecklistItems.end())
  1393. {
  1394. DeleteItem(*I);
  1395. I++;
  1396. }
  1397. if (!TargetIsDirectory)
  1398. {
  1399. DeleteItem(TransferChecklistItem);
  1400. }
  1401. else
  1402. {
  1403. // The remaning "transfer" item
  1404. ListView2->Selected->MakeVisible(false);
  1405. }
  1406. UpdateControls();
  1407. }
  1408. }
  1409. }
  1410. //---------------------------------------------------------------------------
  1411. void __fastcall TSynchronizeChecklistDialog::CheckDirectory(bool Check)
  1412. {
  1413. std::unique_ptr<TStringList> Directories(new TStringList());
  1414. TListItem * Item = NULL;
  1415. while (IterateSelectedItems(Item))
  1416. {
  1417. const TSynchronizeChecklist::TItem * ChecklistItem = GetChecklistItem(Item);
  1418. // It does not matter if we use local or remote directory
  1419. Directories->Add(IncludeTrailingBackslash(ChecklistItem->Local.Directory));
  1420. }
  1421. TAutoFlag ChangingItemMassSwitch(FChangingItemMass);
  1422. for (int Index = 0; Index < ListView2->Items->Count; Index++)
  1423. {
  1424. TListItem * Item = ListView2->Items->Item[Index];
  1425. const TSynchronizeChecklist::TItem * ChecklistItem = GetChecklistItem(Item);
  1426. UnicodeString Directory = IncludeTrailingBackslash(ChecklistItem->Local.Directory);
  1427. for (int Index2 = 0; Index2 < Directories->Count; Index2++)
  1428. {
  1429. if (StartsText(Directories->Strings[Index2], Directory))
  1430. {
  1431. Item->Checked = Check;
  1432. }
  1433. }
  1434. }
  1435. ChangingItemMassSwitch.Release();
  1436. UpdateControls();
  1437. }
  1438. //---------------------------------------------------------------------------
  1439. void __fastcall TSynchronizeChecklistDialog::CheckDirectoryActionExecute(TObject *)
  1440. {
  1441. CheckDirectory(true);
  1442. }
  1443. //---------------------------------------------------------------------------
  1444. void __fastcall TSynchronizeChecklistDialog::UncheckDirectoryActionExecute(TObject *)
  1445. {
  1446. CheckDirectory(false);
  1447. }
  1448. //---------------------------------------------------------------------------
  1449. void __fastcall TSynchronizeChecklistDialog::DoExplore(TOperationSide Side)
  1450. {
  1451. const TSynchronizeChecklist::TItem * ChecklistItem = GetChecklistItem(ListView2->Selected);
  1452. TSynchronizeChecklist::TAction Action = GetChecklistItemAction(ChecklistItem);
  1453. FOnSynchronizeExplore(Side, Action, ChecklistItem);
  1454. }
  1455. //---------------------------------------------------------------------------
  1456. void __fastcall TSynchronizeChecklistDialog::ExploreLocalAction2Execute(TObject *)
  1457. {
  1458. DoExplore(osLocal);
  1459. }
  1460. //---------------------------------------------------------------------------
  1461. void __fastcall TSynchronizeChecklistDialog::ExploreRemoteAction2Execute(TObject *)
  1462. {
  1463. DoExplore(osRemote);
  1464. }
  1465. //---------------------------------------------------------------------------
  1466. void __fastcall TSynchronizeChecklistDialog::KeyDown(Word & Key, TShiftState Shift)
  1467. {
  1468. TShortCut KeyShortCut = ShortCut(Key, Shift);
  1469. TShortCut CustomShortCut = NormalizeCustomShortCut(KeyShortCut);
  1470. if (IsCustomShortCut(CustomShortCut))
  1471. {
  1472. TTBXItem * MenuItem = new TTBXItem(this);
  1473. CustomCommandsAction->ActionComponent = MenuItem;
  1474. CustomCommandsAction->Execute();
  1475. for (int Index = 0; Index < MenuItem->Count; Index++)
  1476. {
  1477. TTBCustomItem * Item = MenuItem->Items[Index];
  1478. if (Item->Enabled && (Item->ShortCut == CustomShortCut))
  1479. {
  1480. TAutoFlag DontCopyCommandToClipboardFlag(DontCopyCommandToClipboard);
  1481. Item->Click();
  1482. Key = 0;
  1483. break;
  1484. }
  1485. }
  1486. }
  1487. TForm::KeyDown(Key, Shift);
  1488. }
  1489. //---------------------------------------------------------------------------
  1490. void __fastcall TSynchronizeChecklistDialog::ListView2Recreate(TObject *)
  1491. {
  1492. FChecklistToListViewMap.clear();
  1493. for (int Index = 0; Index < ListView2->Items->Count; Index++)
  1494. {
  1495. TListItem * Item = ListView2->Items->Item[Index];
  1496. const TSynchronizeChecklist::TItem * ChecklistItem = GetChecklistItem(Item);
  1497. FChecklistToListViewMap.insert(std::make_pair(ChecklistItem, Item));
  1498. }
  1499. }
  1500. //---------------------------------------------------------------------------
  1501. void __fastcall TSynchronizeChecklistDialog::ToolsMenuButtonClick(TObject *)
  1502. {
  1503. MenuPopup(ToolsPopupMenu, ToolsMenuButton);
  1504. }
  1505. //---------------------------------------------------------------------------
  1506. void __fastcall TSynchronizeChecklistDialog::FindMoveCandidateActionExecute(TObject *)
  1507. {
  1508. TIEListViewColProperties * ColProperties = GetColProperties();
  1509. int Sort = (ColProperties->SortAscending ? 1 : -1) * ColProperties->SortColumn;
  1510. if (FMoveCandidatesValidForSort != Sort)
  1511. {
  1512. FMoveCandidatesFileName.clear();
  1513. FMoveCandidatesSize.clear();
  1514. for (int Index = 0; Index < ListView2->Items->Count; Index++)
  1515. {
  1516. const TSynchronizeChecklist::TItem * ChecklistItem = GetChecklistItem(ListView2->Items->Item[Index]);
  1517. if (ChecklistItem->IsLocalOnly() || ChecklistItem->IsRemoteOnly())
  1518. {
  1519. if (ChecklistItem->IsDirectory)
  1520. {
  1521. FMoveCandidatesFileName[ChecklistItem->GetFileName().LowerCase()].push_back(ChecklistItem);
  1522. }
  1523. else
  1524. {
  1525. FMoveCandidatesSize[ChecklistItem->GetBaseSize()].push_back(ChecklistItem);
  1526. }
  1527. }
  1528. }
  1529. FMoveCandidatesValidForSort = Sort;
  1530. }
  1531. bool Found = false;
  1532. if (!FMoveCandidatesFileName.empty() || !FMoveCandidatesSize.empty())
  1533. {
  1534. TListItem * ItemFocused = ListView2->ItemFocused;
  1535. TListItem * Item;
  1536. if (ItemFocused != NULL)
  1537. {
  1538. Item = ItemFocused;
  1539. }
  1540. // Can we have a selection without focus?
  1541. else if (DebugAlwaysFalse(ListView2->Selected != NULL))
  1542. {
  1543. Item = ListView2->Selected;
  1544. }
  1545. else if (ListView2->Items->Count > 0)
  1546. {
  1547. Item = ListView2->Items->Item[0];
  1548. }
  1549. else
  1550. {
  1551. DebugFail(); // the action is disabled when there are no items
  1552. Item = NULL;
  1553. }
  1554. TListItem * FirstItem = NULL;
  1555. while (!Found && (Item != NULL))
  1556. {
  1557. const TSynchronizeChecklist::TItem * ChecklistItem = GetChecklistItem(Item);
  1558. TSynchronizeChecklist::TAction Action = GetChecklistItemAction(ChecklistItem);
  1559. TSynchronizeChecklist::TAction OppositeAction = GetOppositeMoveAction(Action);
  1560. if ((OppositeAction != TSynchronizeChecklist::saNone) &&
  1561. // For focused item, we search pair even if the focused item is "delete" action,
  1562. // but when searching the next items, consider "transfer" actions only
  1563. ((Item == ItemFocused) || IsTransferNewAction(Action)))
  1564. {
  1565. TChecklistItems Candidates;
  1566. if (ChecklistItem->IsDirectory)
  1567. {
  1568. UnicodeString FileName = ChecklistItem->GetFileName().LowerCase();
  1569. TMoveCandidatesFileNameMap::const_iterator I = FMoveCandidatesFileName.find(FileName);
  1570. if (I != FMoveCandidatesFileName.end())
  1571. {
  1572. const TChecklistItems & NameCandidates = I->second;
  1573. for (size_t I = 0; I < NameCandidates.size(); I++)
  1574. {
  1575. const TSynchronizeChecklist::TItem * ChecklistItem2 = NameCandidates[I];
  1576. if ((GetChecklistItemAction(ChecklistItem2) == OppositeAction) &&
  1577. DebugAlwaysTrue(ChecklistItem2->IsDirectory))
  1578. {
  1579. Candidates.push_back(ChecklistItem2);
  1580. }
  1581. }
  1582. }
  1583. }
  1584. else
  1585. {
  1586. __int64 Size = ChecklistItem->GetBaseSize();
  1587. TMoveCandidatesSizeMap::const_iterator I = FMoveCandidatesSize.find(Size);
  1588. if (I != FMoveCandidatesSize.end())
  1589. {
  1590. UnicodeString FileName = ChecklistItem->GetFileName();
  1591. const TChecklistItems & SizeCandidates = I->second;
  1592. for (size_t I = 0; I < SizeCandidates.size(); I++)
  1593. {
  1594. const TSynchronizeChecklist::TItem * ChecklistItem2 = SizeCandidates[I];
  1595. if ((GetChecklistItemAction(ChecklistItem2) == OppositeAction) &&
  1596. DebugAlwaysTrue(!ChecklistItem2->IsDirectory))
  1597. {
  1598. bool IsCandidate;
  1599. // in addition to the same size, also the same file filename (although possibly different directory)
  1600. if (SameText(FileName, ChecklistItem2->GetFileName()))
  1601. {
  1602. IsCandidate = true;
  1603. }
  1604. // or different filename but the same directory (in addition to the same size)
  1605. else if ((Action == TSynchronizeChecklist::saDeleteLocal) ||
  1606. (Action == TSynchronizeChecklist::saDownloadNew))
  1607. {
  1608. IsCandidate = SamePaths(ChecklistItem->Local.Directory, ChecklistItem2->Local.Directory);
  1609. }
  1610. else if ((Action == TSynchronizeChecklist::saDeleteRemote) ||
  1611. (Action == TSynchronizeChecklist::saUploadNew))
  1612. {
  1613. IsCandidate = UnixSamePath(ChecklistItem->Remote.Directory, ChecklistItem2->Remote.Directory);
  1614. }
  1615. else
  1616. {
  1617. DebugFail();
  1618. IsCandidate = false;
  1619. }
  1620. if (IsCandidate)
  1621. {
  1622. Candidates.push_back(ChecklistItem2);
  1623. }
  1624. }
  1625. }
  1626. }
  1627. }
  1628. if (!Candidates.empty())
  1629. {
  1630. const TSynchronizeChecklist::TItem * ChecklistItem2 = Candidates[0];
  1631. if ((FirstItem == NULL) && Item->Selected && (ListView2->SelCount == 2))
  1632. {
  1633. TListItem * NextSelected = ListView2->GetNextItem(Item, sdAll, TItemStates() << isSelected);
  1634. if (NextSelected == NULL)
  1635. {
  1636. NextSelected = ListView2->Selected; // Shorthand for GetNextItem(NULL, sdAll, isSelected)
  1637. }
  1638. TChecklistItems::const_iterator I = std::find(Candidates.begin(), Candidates.end(), GetChecklistItem(NextSelected));
  1639. if (I < Candidates.end() - 1)
  1640. {
  1641. ChecklistItem2 = *(I + 1);
  1642. }
  1643. else if (I == Candidates.end() - 1)
  1644. {
  1645. ChecklistItem2 = NULL;
  1646. }
  1647. }
  1648. if (ChecklistItem2 != NULL)
  1649. {
  1650. TListItem * Item2 = FChecklistToListViewMap[ChecklistItem2];
  1651. for (int Index = 0; Index < ListView2->Items->Count; Index++)
  1652. {
  1653. TListItem * ItemI = ListView2->Items->Item[Index];
  1654. ItemI->Selected = (ItemI == Item) || (ItemI == Item2);
  1655. }
  1656. ListView2->ItemFocused = Item;
  1657. // IsItemVisible returns true even on partial visibility,
  1658. // so it is still worth trying MakeVisible to make them completelly visible
  1659. bool FlickerExpected = !ListView2->IsItemVisible(Item) || !ListView2->IsItemVisible(Item2);
  1660. if (FlickerExpected)
  1661. {
  1662. // does not seem to have any effect
  1663. ListView2->LockDrawing();
  1664. }
  1665. try
  1666. {
  1667. // even if the item turns to be invisible in the end, it at least scrolls the view towards it
  1668. Item2->MakeVisible(false);
  1669. // Should do minimal scroll needed to make the first item whole visible, but not hiding the second
  1670. Item->MakeVisible(false);
  1671. }
  1672. __finally
  1673. {
  1674. if (FlickerExpected)
  1675. {
  1676. ListView2->UnlockDrawing();
  1677. }
  1678. }
  1679. Found = true;
  1680. }
  1681. }
  1682. }
  1683. // Allow going through the first item twice, to make sure we roll over its previous pair candidates
  1684. if (Item == FirstItem)
  1685. {
  1686. Item = NULL;
  1687. }
  1688. else
  1689. {
  1690. if (FirstItem == NULL)
  1691. {
  1692. FirstItem = Item;
  1693. }
  1694. Item = ListView2->GetNextItem(Item, sdAll, TItemStates());
  1695. if (Item == NULL)
  1696. {
  1697. Item = ListView2->Items->Item[0];
  1698. }
  1699. }
  1700. }
  1701. }
  1702. if (!Found)
  1703. {
  1704. MessageBeep(MB_ICONHAND);
  1705. }
  1706. }
  1707. //---------------------------------------------------------------------------
  1708. void __fastcall TSynchronizeChecklistDialog::StartItemClick(TObject *)
  1709. {
  1710. DoSynchronize(false);
  1711. }
  1712. //---------------------------------------------------------------------------
  1713. void __fastcall TSynchronizeChecklistDialog::OkButtonDropDownClick(TObject *)
  1714. {
  1715. MenuPopup(OkPopupMenu, OkButton);
  1716. }
  1717. //---------------------------------------------------------------------------
  1718. void __fastcall TSynchronizeChecklistDialog::StartQueueItemClick(TObject *)
  1719. {
  1720. DoSynchronize(true);
  1721. }
  1722. //---------------------------------------------------------------------------
  1723. void TSynchronizeChecklistDialog::PathToClipboard(bool Local)
  1724. {
  1725. std::unique_ptr<TStrings> Paths(new TStringList());
  1726. TListItem * Item = NULL;
  1727. while (IterateSelectedItems(Item))
  1728. {
  1729. const TSynchronizeChecklist::TItem * ChecklistItem = GetChecklistItem(Item);
  1730. UnicodeString Path = Local ? ChecklistItem->ForceGetLocalPath() : ChecklistItem->ForceGetRemotePath();
  1731. Paths->Add(Path);
  1732. }
  1733. TInstantOperationVisualizer Visualizer;
  1734. CopyToClipboard(Paths.get());
  1735. }
  1736. //---------------------------------------------------------------------------
  1737. void __fastcall TSynchronizeChecklistDialog::LocalPathToClipboardActionExecute(TObject *)
  1738. {
  1739. PathToClipboard(true);
  1740. }
  1741. //---------------------------------------------------------------------------
  1742. void __fastcall TSynchronizeChecklistDialog::RemotePathToClipboardActionExecute(TObject *)
  1743. {
  1744. PathToClipboard(false);
  1745. }
  1746. //---------------------------------------------------------------------------