SynchronizeChecklist.cpp 62 KB

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