1
0

SynchronizeChecklist.cpp 63 KB

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