SynchronizeChecklist.cpp 63 KB

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