SynchronizeChecklist.cpp 61 KB

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