SynchronizeChecklist.cpp 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838
  1. //---------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include <Common.h>
  5. #include "WinInterface.h"
  6. #include "SynchronizeChecklist.h"
  7. #include <Terminal.h>
  8. #include <TextsWin.h>
  9. #include <CoreMain.h>
  10. #include <VCLCommon.h>
  11. #include <Tools.h>
  12. #include <BaseUtils.hpp>
  13. #include <Math.hpp>
  14. #include <WinConfiguration.h>
  15. #include <GUITools.h>
  16. #include <TerminalManager.h>
  17. #include <System.IOUtils.hpp>
  18. #include <System.StrUtils.hpp>
  19. #include <algorithm>
  20. //---------------------------------------------------------------------
  21. #pragma link "IEListView"
  22. #pragma link "NortonLikeListView"
  23. #pragma link "PngImageList"
  24. #pragma resource "*.dfm"
  25. //---------------------------------------------------------------------
  26. const int ImageColumnIndex = 4;
  27. //---------------------------------------------------------------------
  28. bool __fastcall DoSynchronizeChecklistDialog(TSynchronizeChecklist * Checklist,
  29. TSynchronizeMode Mode, int Params,
  30. const UnicodeString LocalDirectory, const UnicodeString RemoteDirectory,
  31. TCustomCommandMenuEvent OnCustomCommandMenu, TFullSynchronizeEvent OnSynchronize,
  32. TQueueSynchronizeEvent OnQueueSynchronize,
  33. TSynchronizeChecklistCalculateSize OnSynchronizeChecklistCalculateSize, TSynchronizeMoveEvent OnSynchronizeMove,
  34. TSynchronizeBrowseEvent OnSynchronizeBrowse, void * Token)
  35. {
  36. std::unique_ptr<TSynchronizeChecklistDialog> Dialog(
  37. new TSynchronizeChecklistDialog(
  38. Application, Mode, Params, LocalDirectory, RemoteDirectory, OnCustomCommandMenu, OnSynchronize,
  39. OnQueueSynchronize, OnSynchronizeChecklistCalculateSize, OnSynchronizeMove, OnSynchronizeBrowse, Token));
  40. return Dialog->Execute(Checklist);
  41. }
  42. //---------------------------------------------------------------------
  43. __fastcall TSynchronizeChecklistDialog::TSynchronizeChecklistDialog(
  44. TComponent * AOwner, TSynchronizeMode Mode, int Params,
  45. const UnicodeString & LocalDirectory, const UnicodeString & RemoteDirectory,
  46. TCustomCommandMenuEvent OnCustomCommandMenu, TFullSynchronizeEvent OnSynchronize,
  47. TQueueSynchronizeEvent OnQueueSynchronize,
  48. TSynchronizeChecklistCalculateSize OnSynchronizeChecklistCalculateSize, TSynchronizeMoveEvent OnSynchronizeMove,
  49. TSynchronizeBrowseEvent OnSynchronizeBrowse, void * Token)
  50. : TForm(AOwner)
  51. {
  52. FFormRestored = false;
  53. FMode = Mode;
  54. FParams = Params;
  55. FLocalDirectory = ExcludeTrailingBackslash(LocalDirectory);
  56. FRemoteDirectory = UnixExcludeTrailingBackslash(RemoteDirectory);
  57. FOnCustomCommandMenu = OnCustomCommandMenu;
  58. FOnSynchronizeChecklistCalculateSize = OnSynchronizeChecklistCalculateSize;
  59. FOnSynchronizeMove = OnSynchronizeMove;
  60. FOnSynchronizeBrowse = OnSynchronizeBrowse;
  61. DebugAssert(OnSynchronize != NULL);
  62. FOnSynchronize = OnSynchronize;
  63. FOnQueueSynchronize = OnQueueSynchronize;
  64. FToken = Token;
  65. UseSystemSettings(this);
  66. UseDesktopFont(ListView);
  67. UseDesktopFont(StatusBar);
  68. FChecklist = NULL;
  69. FChangingItem = NULL;
  70. FChangingItemIgnore = false;
  71. FChangingItemMass = false;
  72. FSynchronizing = false;
  73. FMoveCandidatesValidForSort = MaxInt;
  74. FDirectories = 0;
  75. SelectScaledImageList(ActionImages);
  76. FOrigListViewWindowProc = ListView->WindowProc;
  77. ListView->WindowProc = ListViewWindowProc;
  78. FOrigStatusBarWindowProc = StatusBar->WindowProc;
  79. StatusBar->WindowProc = StatusBarWindowProc;
  80. UpdateImages();
  81. CustomCommandsAction->Visible = (FOnCustomCommandMenu != NULL);
  82. // button visibility cannot be bound to action visibility
  83. CustomCommandsButton2->Visible = CustomCommandsAction->Visible;
  84. MenuButton(CustomCommandsButton2);
  85. MenuButton(ToolsMenuButton);
  86. // Other dialogs use !IsMainFormLike
  87. if (FOnQueueSynchronize != NULL)
  88. {
  89. OkButton->Style = TCustomButton::bsSplitButton;
  90. }
  91. }
  92. //---------------------------------------------------------------------
  93. __fastcall TSynchronizeChecklistDialog::~TSynchronizeChecklistDialog()
  94. {
  95. StatusBar->WindowProc = FOrigStatusBarWindowProc;
  96. ListView->WindowProc = FOrigListViewWindowProc;
  97. }
  98. //---------------------------------------------------------------------
  99. bool __fastcall TSynchronizeChecklistDialog::Execute(TSynchronizeChecklist * Checklist)
  100. {
  101. FChecklist = Checklist;
  102. bool Result = (ShowModal() == DefaultResult(this));
  103. if (Result)
  104. {
  105. TSynchronizeChecklistConfiguration FormConfiguration =
  106. CustomWinConfiguration->SynchronizeChecklist;
  107. FormConfiguration.ListParams = ListView->ColProperties->ParamsStr;
  108. UnicodeString WindowParams = FormConfiguration.WindowParams;
  109. // if there is no main window, keep previous "custom pos" indication,
  110. bool CustomPos = (StrToIntDef(CutToChar(WindowParams, L';', true), 0) != 0);
  111. if (!IsMainFormLike(this))
  112. {
  113. CustomPos = (Application->MainForm->BoundsRect != BoundsRect);
  114. }
  115. FormConfiguration.WindowParams =
  116. FORMAT(L"%d;%s", ((CustomPos ? 1 : 0), StoreForm(this)));
  117. CustomWinConfiguration->SynchronizeChecklist = FormConfiguration;
  118. }
  119. if (FException.get() != NULL)
  120. {
  121. RethrowException(FException.get());
  122. }
  123. return Result;
  124. }
  125. //---------------------------------------------------------------------
  126. void __fastcall TSynchronizeChecklistDialog::UpdateCaption()
  127. {
  128. TTerminalManager * Manager = TTerminalManager::Instance();
  129. UnicodeString Title = Manager->GetAppProgressTitle();
  130. UnicodeString StatusTitle = LoadStr(FSynchronizing ? SYNCHRONIZE_PROGRESS_SYNCHRONIZE2 : SYNCHRONIZE_CHECKLIST_CAPTION);
  131. if (Title.Pos(StatusTitle) <= 0)
  132. {
  133. AddToList(Title, StatusTitle, TitleSeparator);
  134. }
  135. Caption = Manager->FormatFormCaptionWithSession(this, Title);
  136. }
  137. //---------------------------------------------------------------------------
  138. static bool IsTransferNewAction(TSynchronizeChecklist::TAction Action)
  139. {
  140. return (Action == TSynchronizeChecklist::saUploadNew) || (Action == TSynchronizeChecklist::saDownloadNew);
  141. }
  142. //---------------------------------------------------------------------------
  143. static TSynchronizeChecklist::TAction GetOppositeMoveAction(TSynchronizeChecklist::TAction Action1)
  144. {
  145. switch (Action1)
  146. {
  147. case TSynchronizeChecklist::saUploadNew:
  148. return TSynchronizeChecklist::saDeleteRemote;
  149. case TSynchronizeChecklist::saDownloadNew:
  150. return TSynchronizeChecklist::saDeleteLocal;
  151. case TSynchronizeChecklist::saDeleteRemote:
  152. return TSynchronizeChecklist::saUploadNew;
  153. case TSynchronizeChecklist::saDeleteLocal:
  154. return TSynchronizeChecklist::saDownloadNew;
  155. default:
  156. return TSynchronizeChecklist::saNone;
  157. }
  158. }
  159. //---------------------------------------------------------------------
  160. struct TMoveActionData
  161. {
  162. TSynchronizeChecklist::TAction Action;
  163. bool AllItemsFiles;
  164. bool AllItemsDirectories;
  165. int ItemsCount;
  166. std::unique_ptr<TStrings> FileList;
  167. typedef std::vector<const TSynchronizeChecklist::TItem *> TChecklistItems;
  168. TChecklistItems ChecklistItems;
  169. TMoveActionData()
  170. {
  171. Action = TSynchronizeChecklist::saNone;
  172. AllItemsFiles = true;
  173. AllItemsDirectories = true;
  174. ItemsCount = 0;
  175. }
  176. bool Collect(const TSynchronizeChecklist::TItem * ChecklistItem, TSynchronizeChecklist::TAction AAction, bool CollectFileList)
  177. {
  178. bool Result = (ItemsCount == 0) || (Action == AAction);
  179. if (Result)
  180. {
  181. Action = AAction;
  182. if (ChecklistItem->IsDirectory)
  183. {
  184. AllItemsFiles = false;
  185. }
  186. else
  187. {
  188. AllItemsDirectories = false;
  189. }
  190. ItemsCount++;
  191. if (CollectFileList)
  192. {
  193. UnicodeString FileName;
  194. TObject * Object;
  195. if (Action == TSynchronizeChecklist::saDeleteRemote)
  196. {
  197. FileName = ChecklistItem->GetRemotePath();
  198. Object = ChecklistItem->RemoteFile;
  199. }
  200. else if (Action == TSynchronizeChecklist::saDeleteLocal)
  201. {
  202. FileName = ChecklistItem->GetLocalPath();
  203. }
  204. bool CollectFile = !FileName.IsEmpty();
  205. DebugAssert(CollectFile == IsTransferNewAction(GetOppositeMoveAction(Action)));
  206. if (CollectFile)
  207. {
  208. if (FileList.get() == NULL)
  209. {
  210. FileList.reset(new TStringList());
  211. }
  212. FileList->AddObject(FileName, Object);
  213. }
  214. ChecklistItems.push_back(ChecklistItem);
  215. }
  216. }
  217. return Result;
  218. }
  219. bool IsOnlyDirectoryTransferAction()
  220. {
  221. return
  222. IsTransferNewAction(Action) &&
  223. (ItemsCount == 1) &&
  224. AllItemsDirectories &&
  225. DebugAlwaysTrue(!AllItemsFiles);
  226. }
  227. };
  228. //---------------------------------------------------------------------
  229. struct TMoveData
  230. {
  231. TMoveActionData FirstAction;
  232. TMoveActionData SecondAction;
  233. bool ThreeActions;
  234. TMoveData(bool CollectFileList)
  235. {
  236. ThreeActions = false;
  237. FCollectFileList = CollectFileList;
  238. }
  239. void Collect(const TSynchronizeChecklist::TItem * ChecklistItem, TSynchronizeChecklist::TAction Action)
  240. {
  241. if (!FirstAction.Collect(ChecklistItem, Action, FCollectFileList) &&
  242. !SecondAction.Collect(ChecklistItem, Action, FCollectFileList))
  243. {
  244. ThreeActions = true;
  245. }
  246. }
  247. private:
  248. bool FCollectFileList;
  249. };
  250. //---------------------------------------------------------------------
  251. void __fastcall TSynchronizeChecklistDialog::UpdateControls()
  252. {
  253. UpdateCaption();
  254. StatusBar->Invalidate();
  255. bool AllChecked = true;
  256. bool AllUnchecked = true;
  257. bool AnyBoth = false;
  258. bool AnyNonBoth = false;
  259. bool AnyDirectory = false;
  260. TMoveData MoveData(false);
  261. TListItem * Item = NULL;
  262. while (IterateSelectedItems(Item))
  263. {
  264. const TSynchronizeChecklist::TItem * ChecklistItem = GetChecklistItem(Item);
  265. TSynchronizeChecklist::TAction Action = GetChecklistItemAction(ChecklistItem);
  266. if ((Action == TSynchronizeChecklist::saUploadUpdate) ||
  267. (Action == TSynchronizeChecklist::saDownloadUpdate))
  268. {
  269. AnyBoth = true;
  270. }
  271. else
  272. {
  273. AnyNonBoth = true;
  274. }
  275. MoveData.Collect(ChecklistItem, Action);
  276. if (Item->Checked)
  277. {
  278. AllUnchecked = false;
  279. }
  280. else
  281. {
  282. AllChecked = false;
  283. }
  284. if (ChecklistItem->IsDirectory)
  285. {
  286. AnyDirectory = true;
  287. }
  288. }
  289. int SelCount = ListView->SelCount;
  290. EnableControl(OkButton, (FChecked[0] > 0) && !FSynchronizing);
  291. EnableControl(CancelButton, !FSynchronizing);
  292. EnableControl(HelpButton, !FSynchronizing);
  293. CheckAction->Enabled = !AllChecked && !FSynchronizing;
  294. UncheckAction->Enabled = !AllUnchecked && !FSynchronizing;
  295. CheckAllAction->Enabled = (FChecked[0] < FTotals[0]) && !FSynchronizing;
  296. UncheckAllAction->Enabled = (FChecked[0] > 0) && !FSynchronizing;
  297. CheckDirectoryAction->Enabled = CheckAllAction->Enabled; // sic
  298. UncheckDirectoryAction->Enabled = UncheckAllAction->Enabled; // sic
  299. CustomCommandsAction->Enabled = AnyBoth && !AnyNonBoth && DebugAlwaysTrue(!FSynchronizing);
  300. ReverseAction->Enabled = (SelCount > 0) && DebugAlwaysTrue(!FSynchronizing);
  301. MoveAction->Enabled =
  302. // All actions are of exactly two and opposite types
  303. (MoveData.SecondAction.Action != TSynchronizeChecklist::saNone) &&
  304. !MoveData.ThreeActions &&
  305. (GetOppositeMoveAction(MoveData.FirstAction.Action) == MoveData.SecondAction.Action) &&
  306. // ... and there are exactly two of them of the same file/dir type
  307. (((SelCount == 2) && (MoveData.FirstAction.AllItemsFiles == MoveData.SecondAction.AllItemsFiles)) ||
  308. // ... or the "transfer new" side is exactly one directory
  309. MoveData.FirstAction.IsOnlyDirectoryTransferAction() ||
  310. MoveData.SecondAction.IsOnlyDirectoryTransferAction());
  311. CalculateSizeAction->Enabled = (SelCount > 0) && AnyDirectory && DebugAlwaysTrue(!FSynchronizing);
  312. CalculateSizeAllAction->Enabled = (FDirectories > 0) && !FSynchronizing;
  313. TSynchronizeChecklist::TAction SelectedItemAction =
  314. (SelCount == 1) ? GetChecklistItemAction(GetChecklistItem(ListView->Selected)) : TSynchronizeChecklist::saNone;
  315. BrowseLocalAction->Enabled = (SelCount == 1) && (SelectedItemAction != TSynchronizeChecklist::saDeleteRemote);
  316. BrowseRemoteAction->Enabled = (SelCount == 1) && (SelectedItemAction != TSynchronizeChecklist::saDeleteLocal);
  317. int Count = ListView->Items->Count;
  318. DebugAssert(FTotals[0] == Count);
  319. SelectAllAction->Enabled = (SelCount < Count) && !FSynchronizing;
  320. FindMoveCandidateAction->Enabled = (Count > 0) && !FSynchronizing;
  321. }
  322. //---------------------------------------------------------------------------
  323. bool __fastcall TSynchronizeChecklistDialog::GetWindowParams(UnicodeString & WindowParams)
  324. {
  325. WindowParams = CustomWinConfiguration->SynchronizeChecklist.WindowParams;
  326. bool CustomPos = (StrToIntDef(CutToChar(WindowParams, L';', true), 0) != 0);
  327. return CustomPos || IsMainFormLike(this);
  328. }
  329. //---------------------------------------------------------------------------
  330. void __fastcall TSynchronizeChecklistDialog::CreateParams(TCreateParams & Params)
  331. {
  332. UnicodeString WindowParams;
  333. if (GetWindowParams(WindowParams))
  334. {
  335. // This is only to set correct TForm::Position. Actual bounds are set later after DPI scaling
  336. RestoreForm(WindowParams, this, true);
  337. }
  338. TForm::CreateParams(Params);
  339. }
  340. //---------------------------------------------------------------------------
  341. void __fastcall TSynchronizeChecklistDialog::HelpButtonClick(TObject * /*Sender*/)
  342. {
  343. FormHelp(this);
  344. }
  345. //---------------------------------------------------------------------------
  346. void __fastcall TSynchronizeChecklistDialog::AddSubItem(TListItem * Item, int & Index, const UnicodeString & S)
  347. {
  348. if (Index < Item->SubItems->Count)
  349. {
  350. Item->SubItems->Strings[Index] = S;
  351. }
  352. else
  353. {
  354. DebugAssert(Index == Item->SubItems->Count);
  355. Item->SubItems->Add(S);
  356. }
  357. Index++;
  358. }
  359. //---------------------------------------------------------------------------
  360. void __fastcall TSynchronizeChecklistDialog::LoadItem(TListItem * Item)
  361. {
  362. UnicodeString S;
  363. const TSynchronizeChecklist::TItem * ChecklistItem = GetChecklistItem(Item);
  364. if (ChecklistItem->ImageIndex >= 0)
  365. {
  366. Item->ImageIndex = ChecklistItem->ImageIndex;
  367. }
  368. else
  369. {
  370. Item->ImageIndex = FakeFileImageIndex(ChecklistItem->GetFileName(),
  371. FLAGMASK(ChecklistItem->IsDirectory, FILE_ATTRIBUTE_DIRECTORY));
  372. }
  373. S = ChecklistItem->GetFileName();
  374. if (ChecklistItem->IsDirectory)
  375. {
  376. S = IncludeTrailingBackslash(S);
  377. }
  378. Item->Caption = S;
  379. int Index = 0;
  380. TSynchronizeChecklist::TAction Action = GetChecklistItemAction(ChecklistItem);
  381. if (Action == TSynchronizeChecklist::saDeleteRemote)
  382. {
  383. AddSubItem(Item, Index, L"");
  384. AddSubItem(Item, Index, L"");
  385. AddSubItem(Item, Index, L"");
  386. }
  387. else
  388. {
  389. S = ChecklistItem->Local.Directory;
  390. if (AnsiSameText(FLocalDirectory, S.SubString(1, FLocalDirectory.Length())))
  391. {
  392. S[1] = L'.';
  393. S.Delete(2, FLocalDirectory.Length() - 1);
  394. }
  395. else
  396. {
  397. DebugFail();
  398. }
  399. AddSubItem(Item, Index, S);
  400. if (Action == TSynchronizeChecklist::saDownloadNew)
  401. {
  402. AddSubItem(Item, Index, L"");
  403. AddSubItem(Item, Index, L"");
  404. }
  405. else
  406. {
  407. if (!ChecklistItem->HasSize())
  408. {
  409. AddSubItem(Item, Index, L"");
  410. }
  411. else
  412. {
  413. AddSubItem(Item, Index,
  414. FormatPanelBytes(ChecklistItem->Local.Size, WinConfiguration->FormatSizeBytes));
  415. }
  416. AddSubItem(Item, Index,
  417. UserModificationStr(ChecklistItem->Local.Modification,
  418. ChecklistItem->Local.ModificationFmt));
  419. }
  420. }
  421. AddSubItem(Item, Index, L"");
  422. DebugAssert(Index == ImageColumnIndex);
  423. if (Action == TSynchronizeChecklist::saDeleteLocal)
  424. {
  425. AddSubItem(Item, Index, L"");
  426. AddSubItem(Item, Index, L"");
  427. AddSubItem(Item, Index, L"");
  428. }
  429. else
  430. {
  431. S = ChecklistItem->Remote.Directory;
  432. if (AnsiSameText(FRemoteDirectory, S.SubString(1, FRemoteDirectory.Length())))
  433. {
  434. S[1] = L'.';
  435. S.Delete(2, FRemoteDirectory.Length() - 1);
  436. }
  437. else
  438. {
  439. DebugFail();
  440. }
  441. AddSubItem(Item, Index, S);
  442. if (Action == TSynchronizeChecklist::saUploadNew)
  443. {
  444. AddSubItem(Item, Index, L"");
  445. AddSubItem(Item, Index, L"");
  446. }
  447. else
  448. {
  449. if (!ChecklistItem->HasSize())
  450. {
  451. AddSubItem(Item, Index, L"");
  452. }
  453. else
  454. {
  455. AddSubItem(Item, Index,
  456. FormatPanelBytes(ChecklistItem->Remote.Size, WinConfiguration->FormatSizeBytes));
  457. }
  458. AddSubItem(Item, Index, UserModificationStr(ChecklistItem->Remote.Modification,
  459. ChecklistItem->Remote.ModificationFmt));
  460. }
  461. }
  462. }
  463. //---------------------------------------------------------------------------
  464. void __fastcall TSynchronizeChecklistDialog::CountItemSize(const TSynchronizeChecklist::TItem * ChecklistItem, int Factor)
  465. {
  466. TSynchronizeChecklist::TAction Action = GetChecklistItemAction(ChecklistItem);
  467. int ActionIndex = int(Action);
  468. __int64 ItemSize = ChecklistItem->GetSize(Action);
  469. FCheckedSize[ActionIndex] += Factor * ItemSize;
  470. FCheckedSize[0] += Factor * ItemSize;
  471. }
  472. //---------------------------------------------------------------------------
  473. void __fastcall TSynchronizeChecklistDialog::CountItem(const TSynchronizeChecklist::TItem * ChecklistItem, int Factor)
  474. {
  475. int ActionIndex = int(GetChecklistItemAction(ChecklistItem));
  476. FChecked[ActionIndex] += Factor;
  477. FChecked[0] += Factor;
  478. CountItemSize(ChecklistItem, Factor);
  479. }
  480. //---------------------------------------------------------------------------
  481. void __fastcall TSynchronizeChecklistDialog::CountItemTotal(const TSynchronizeChecklist::TItem * ChecklistItem, int Factor)
  482. {
  483. FTotals[0] += Factor;
  484. int ActionIndex = int(GetChecklistItemAction(ChecklistItem));
  485. FTotals[ActionIndex] += Factor;
  486. if (ChecklistItem->IsDirectory)
  487. {
  488. FDirectories += Factor;
  489. }
  490. }
  491. //---------------------------------------------------------------------------
  492. void __fastcall TSynchronizeChecklistDialog::LoadList()
  493. {
  494. memset(&FTotals, 0, sizeof(FTotals));
  495. memset(&FChecked, 0, sizeof(FChecked));
  496. memset(&FCheckedSize, 0, sizeof(FCheckedSize));
  497. FDirectories = 0;
  498. ListView->Items->BeginUpdate();
  499. try
  500. {
  501. ListView->Items->Clear();
  502. for (int Index = 0; Index < FChecklist->Count; Index++)
  503. {
  504. const TSynchronizeChecklist::TItem * ChecklistItem =
  505. FChecklist->Item[ListView->Items->Count];
  506. FChangingItemIgnore = true;
  507. try
  508. {
  509. TListItem * Item = ListView->Items->Add();
  510. TSynchronizeChecklist::TAction Action = ChecklistItem->Action;
  511. FActions.insert(std::make_pair(ChecklistItem, Action));
  512. FChecklistToListViewMap.insert(std::make_pair(ChecklistItem, Item));
  513. Item->Data = const_cast<TSynchronizeChecklist::TItem *>(ChecklistItem);
  514. Item->Checked = ChecklistItem->Checked;
  515. LoadItem(Item);
  516. }
  517. __finally
  518. {
  519. FChangingItemIgnore = false;
  520. }
  521. CountItemTotal(ChecklistItem, 1);
  522. if (ChecklistItem->Checked)
  523. {
  524. CountItem(ChecklistItem, 1);
  525. }
  526. }
  527. }
  528. __finally
  529. {
  530. ListView->Items->EndUpdate();
  531. }
  532. ListView->AlphaSort();
  533. UpdateControls();
  534. }
  535. //---------------------------------------------------------------------------
  536. void __fastcall TSynchronizeChecklistDialog::FormShow(TObject * /*Sender*/)
  537. {
  538. // Moved here from CreateParams (see also TEditorForm::CreateParams), because there it breaks per-monitor DPI.
  539. // For example BoundsRect is matched to the main form too soon, so it gets rescaled later.
  540. // Also it happens before constructor, what causes UseDesktopFont-flagged controls to rescale twice.
  541. // But Position is already set in the CreateParams, as it cannot be set here anymore.
  542. if (!FFormRestored)
  543. {
  544. FFormRestored = True;
  545. UnicodeString WindowParams;
  546. if (GetWindowParams(WindowParams))
  547. {
  548. RestoreForm(WindowParams, this);
  549. }
  550. else
  551. {
  552. BoundsRect = Application->MainForm->BoundsRect;
  553. }
  554. FlashOnBackground();
  555. }
  556. ListView->ColProperties->ParamsStr = CustomWinConfiguration->SynchronizeChecklist.ListParams;
  557. LoadList();
  558. UpdateStatusBarSize();
  559. }
  560. //---------------------------------------------------------------------------
  561. TRect __fastcall TSynchronizeChecklistDialog::GetColumnHeaderRect(int Index)
  562. {
  563. HWND HeaderHandle = ListView_GetHeader(ListView->Handle);
  564. TRect R;
  565. Header_GetItemRect(HeaderHandle, Index, &R);
  566. // Can be simplified using GetScrollPos
  567. TScrollInfo ScrollInfo;
  568. ZeroMemory(&ScrollInfo, sizeof(ScrollInfo));
  569. ScrollInfo.cbSize = sizeof(ScrollInfo);
  570. ScrollInfo.fMask = SIF_POS;
  571. GetScrollInfo(ListView->Handle, SB_HORZ, &ScrollInfo);
  572. R.Left -= ScrollInfo.nPos;
  573. R.Right -= ScrollInfo.nPos;
  574. return R;
  575. }
  576. //---------------------------------------------------------------------------
  577. void __fastcall TSynchronizeChecklistDialog::ListViewWindowProc(TMessage & Message)
  578. {
  579. if (Message.Msg == CN_NOTIFY)
  580. {
  581. TWMNotify & NotifyMessage = reinterpret_cast<TWMNotify &>(Message);
  582. if (NotifyMessage.NMHdr->code == NM_CUSTOMDRAW)
  583. {
  584. // workaround
  585. // Due to a bug in VCL, OnAdvancedCustomDrawSubItem is not called for any
  586. // other stage except for cdPrePaint. So we must call it ourselves.
  587. TNMLVCustomDraw * CustomDraw =
  588. reinterpret_cast<TNMLVCustomDraw *>(NotifyMessage.NMHdr);
  589. if (FLAGSET(CustomDraw->nmcd.dwDrawStage, CDDS_ITEM) &&
  590. FLAGSET(CustomDraw->nmcd.dwDrawStage, CDDS_SUBITEM) &&
  591. FLAGSET(CustomDraw->nmcd.dwDrawStage, CDDS_ITEMPOSTPAINT) &&
  592. (CustomDraw->iSubItem == ImageColumnIndex) &&
  593. (ActionImages->Width <= ListView->Columns->Items[CustomDraw->iSubItem]->Width))
  594. {
  595. TListItem * Item = ListView->Items->Item[CustomDraw->nmcd.dwItemSpec];
  596. const TSynchronizeChecklist::TItem * ChecklistItem = GetChecklistItem(Item);
  597. TRect HeaderR = GetColumnHeaderRect(CustomDraw->iSubItem);
  598. TRect R = Item->DisplayRect(drBounds);
  599. R.Left = HeaderR.Left + (HeaderR.Width() - ActionImages->Width) / 2;
  600. R.Right = HeaderR.Right;
  601. // workaround
  602. // doing this from ListViewAdvancedCustomDraw corrupts list view on Windows 7
  603. ImageList_Draw(reinterpret_cast<HIMAGELIST>(ActionImages->Handle),
  604. int(GetChecklistItemAction(ChecklistItem)), CustomDraw->nmcd.hdc,
  605. R.Left, ((R.Top + R.Bottom - ActionImages->Height) / 2), ILD_TRANSPARENT);
  606. }
  607. }
  608. FOrigListViewWindowProc(Message);
  609. }
  610. else if (Message.Msg == CM_HINTSHOW)
  611. {
  612. ListViewHintShow(reinterpret_cast<TCMHintShow &>(Message));
  613. }
  614. else if (Message.Msg == WM_WANTS_SCREEN_TIPS)
  615. {
  616. Message.Result = 1;
  617. }
  618. else
  619. {
  620. FOrigListViewWindowProc(Message);
  621. }
  622. }
  623. //---------------------------------------------------------------------------
  624. void __fastcall TSynchronizeChecklistDialog::ListViewHintShow(TCMHintShow & HintShow)
  625. {
  626. TLVHitTestInfo HitTest;
  627. HitTest.pt = HintShow.HintInfo->CursorPos;
  628. int Index = ListView_SubItemHitTest(ListView->Handle, &HitTest);
  629. if ((Index >= 0) && (HitTest.iSubItem == ImageColumnIndex))
  630. {
  631. TListItem * Item = ListView->Items->Item[Index];
  632. const TSynchronizeChecklist::TItem * ChecklistItem = GetChecklistItem(Item);
  633. int ActionHint = 0;
  634. switch (int(GetChecklistItemAction(ChecklistItem)))
  635. {
  636. case TSynchronizeChecklist::saUploadNew:
  637. ActionHint = SYNCHRONIZE_CHECKLIST_UPLOAD_NEW;
  638. break;
  639. case TSynchronizeChecklist::saDownloadNew:
  640. ActionHint = SYNCHRONIZE_CHECKLIST_DOWNLOAD_NEW;
  641. break;
  642. case TSynchronizeChecklist::saUploadUpdate:
  643. ActionHint = SYNCHRONIZE_CHECKLIST_UPLOAD_UPDATE;
  644. break;
  645. case TSynchronizeChecklist::saDownloadUpdate:
  646. ActionHint = SYNCHRONIZE_CHECKLIST_DOWNLOAD_UPDATE;
  647. break;
  648. case TSynchronizeChecklist::saDeleteRemote:
  649. ActionHint = SYNCHRONIZE_CHECKLIST_DELETE_REMOTE;
  650. break;
  651. case TSynchronizeChecklist::saDeleteLocal:
  652. ActionHint = SYNCHRONIZE_CHECKLIST_DELETE_LOCAL;
  653. break;
  654. }
  655. if (DebugAlwaysTrue(ActionHint != 0))
  656. {
  657. HintShow.HintInfo->HintStr = FORMAT(L"%s|%s", (LoadStr(ActionHint), LoadStr(SYNCHRONIZE_CHECKLIST_REVERSE)));
  658. ListView_GetSubItemRect(ListView->Handle, Index, ImageColumnIndex, LVIR_BOUNDS, &HintShow.HintInfo->CursorRect);
  659. HintShow.Result = 0;
  660. }
  661. }
  662. FOrigListViewWindowProc(reinterpret_cast<TMessage &>(HintShow));
  663. }
  664. //---------------------------------------------------------------------------
  665. void __fastcall TSynchronizeChecklistDialog::StatusBarHintShow(TCMHintShow & HintShow)
  666. {
  667. int IPanel = PanelAt(HintShow.HintInfo->CursorPos.x);
  668. if (IPanel >= 0)
  669. {
  670. TStatusPanel * Panel = StatusBar->Panels->Items[IPanel];
  671. HintShow.HintInfo->HintStr = FORMAT(L"%s|%s", (Panel->Text, StatusBar->Hint));
  672. HintShow.HintInfo->CursorRect.Left = 0;
  673. while (IPanel > 0)
  674. {
  675. IPanel--;
  676. HintShow.HintInfo->CursorRect.Left += StatusBar->Panels->Items[IPanel]->Width;
  677. }
  678. HintShow.HintInfo->CursorRect.Top = 0;
  679. HintShow.HintInfo->CursorRect.Bottom = StatusBar->ClientHeight;
  680. HintShow.HintInfo->CursorRect.Right = HintShow.HintInfo->CursorRect.Left + Panel->Width;
  681. HintShow.Result = 0;
  682. }
  683. FOrigListViewWindowProc(reinterpret_cast<TMessage &>(HintShow));
  684. }
  685. //---------------------------------------------------------------------------
  686. void __fastcall TSynchronizeChecklistDialog::StatusBarWindowProc(TMessage & Message)
  687. {
  688. if (Message.Msg == WM_WANTS_SCREEN_TIPS)
  689. {
  690. Message.Result = 1;
  691. }
  692. else if (Message.Msg == CM_HINTSHOW)
  693. {
  694. StatusBarHintShow(reinterpret_cast<TCMHintShow &>(Message));
  695. }
  696. else
  697. {
  698. FOrigStatusBarWindowProc(Message);
  699. }
  700. }
  701. //---------------------------------------------------------------------------
  702. void __fastcall TSynchronizeChecklistDialog::ListViewAdvancedCustomDrawSubItem(
  703. TCustomListView * /*Sender*/, TListItem * /*Item*/, int /*SubItem*/,
  704. TCustomDrawState /*State*/, TCustomDrawStage /*Stage*/, bool & /*DefaultDraw*/)
  705. {
  706. // this is just fake handler that makes the list view request custom draw notification above
  707. }
  708. //---------------------------------------------------------------------------
  709. void __fastcall TSynchronizeChecklistDialog::StatusBarDrawPanel(
  710. TStatusBar * StatusBar, TStatusPanel * Panel, const TRect & Rect)
  711. {
  712. bool Possible;
  713. int ActionIndex = Panel->Index;
  714. TSynchronizeChecklist::TAction Action = TSynchronizeChecklist::TAction(ActionIndex);
  715. if (FTotals[ActionIndex] > 0)
  716. {
  717. // if direction is overriden to an action that is otherwise not possible
  718. // in given synchronization mode, we still want to show the stats
  719. Possible = true;
  720. }
  721. else
  722. {
  723. switch (Action)
  724. {
  725. case TSynchronizeChecklist::saNone:
  726. Possible = true;
  727. break;
  728. case TSynchronizeChecklist::saUploadNew:
  729. Possible = ((FMode == smRemote) || (FMode == smBoth)) &&
  730. FLAGCLEAR(FParams, TTerminal::spTimestamp);
  731. break;
  732. case TSynchronizeChecklist::saDownloadNew:
  733. Possible = ((FMode == smLocal) || (FMode == smBoth)) &&
  734. FLAGCLEAR(FParams, TTerminal::spTimestamp);
  735. break;
  736. case TSynchronizeChecklist::saUploadUpdate:
  737. Possible =
  738. ((FMode == smRemote) || (FMode == smBoth)) &&
  739. (FLAGCLEAR(FParams, TTerminal::spNotByTime) || FLAGSET(FParams, TTerminal::spBySize) || FLAGSET(FParams, TTerminal::spByChecksum));
  740. break;
  741. case TSynchronizeChecklist::saDownloadUpdate:
  742. Possible =
  743. ((FMode == smLocal) || (FMode == smBoth)) &&
  744. (FLAGCLEAR(FParams, TTerminal::spNotByTime) || FLAGSET(FParams, TTerminal::spBySize) || FLAGSET(FParams, TTerminal::spByChecksum));
  745. break;
  746. case TSynchronizeChecklist::saDeleteRemote:
  747. Possible = (FMode == smRemote) &&
  748. FLAGCLEAR(FParams, TTerminal::spTimestamp);
  749. break;
  750. case TSynchronizeChecklist::saDeleteLocal:
  751. Possible = (FMode == smLocal) &&
  752. FLAGCLEAR(FParams, TTerminal::spTimestamp);
  753. break;
  754. default:
  755. DebugFail();
  756. Possible = false;
  757. break;
  758. }
  759. }
  760. UnicodeString PanelText;
  761. if (Possible)
  762. {
  763. PanelText = FORMAT(LoadStrPart(SYNCHRONIZE_SELECTED_ACTIONS, 1),
  764. (FormatNumber(FChecked[ActionIndex]),
  765. FormatNumber(FTotals[ActionIndex])));
  766. if ((FChecked[ActionIndex] > 0) &&
  767. ((ActionIndex == 0) || !TSynchronizeChecklist::IsItemSizeIrrelevant(Action)))
  768. {
  769. PanelText += FORMAT(L" (%s)", (FormatBytes(FCheckedSize[ActionIndex])));
  770. }
  771. }
  772. else
  773. {
  774. PanelText = LoadStrPart(SYNCHRONIZE_SELECTED_ACTIONS, 2);
  775. }
  776. int TextHeight = StatusBar->Canvas->TextHeight(PanelText);
  777. int X = Rect.Left + ActionImages->Width + 4;
  778. int Y = (Rect.Top + Rect.Bottom - TextHeight) / 2;
  779. StatusBar->Canvas->TextRect(Rect, X, Y, PanelText);
  780. X = Rect.Left + 1;
  781. Y = ((Rect.Top + Rect.Bottom - ActionImages->Height) / 2);
  782. int ImageIndex = ActionIndex;
  783. ActionImages->Draw(StatusBar->Canvas, X, Y, ImageIndex, Possible);
  784. }
  785. //---------------------------------------------------------------------------
  786. int __fastcall TSynchronizeChecklistDialog::PanelCount()
  787. {
  788. // last "panel" is technical
  789. return StatusBar->Panels->Count - 1;
  790. }
  791. //---------------------------------------------------------------------------
  792. int __fastcall TSynchronizeChecklistDialog::PanelAt(int X)
  793. {
  794. int Result = 0;
  795. while ((X > StatusBar->Panels->Items[Result]->Width) &&
  796. (Result < PanelCount()))
  797. {
  798. X -= StatusBar->Panels->Items[Result]->Width;
  799. Result++;
  800. }
  801. return ((Result < StatusBar->Panels->Count - 1) ? Result : -1);
  802. }
  803. //---------------------------------------------------------------------------
  804. void __fastcall TSynchronizeChecklistDialog::ListViewChange(
  805. TObject * /*Sender*/, TListItem * Item, TItemChange Change)
  806. {
  807. if ((Change == ctState) && (FChangingItem == Item) && (FChangingItem != NULL))
  808. {
  809. if (!FChangingItemIgnore)
  810. {
  811. DebugAssert(Item->Data != NULL);
  812. if ((FChangingItemChecked != Item->Checked) && (Item->Data != NULL))
  813. {
  814. const TSynchronizeChecklist::TItem * ChecklistItem = GetChecklistItem(Item);
  815. CountItem(ChecklistItem, Item->Checked ? 1 : -1);
  816. if (!FChangingItemMass)
  817. {
  818. UpdateControls();
  819. }
  820. }
  821. }
  822. FChangingItem = NULL;
  823. }
  824. }
  825. //---------------------------------------------------------------------------
  826. void __fastcall TSynchronizeChecklistDialog::ListViewChanging(
  827. TObject * /*Sender*/, TListItem * Item, TItemChange Change,
  828. bool & /*AllowChange*/)
  829. {
  830. if (Change == ctState)
  831. {
  832. FChangingItem = Item;
  833. FChangingItemChecked = Item->Checked;
  834. }
  835. else
  836. {
  837. DebugAssert(FChangingItem == NULL);
  838. FChangingItem = NULL;
  839. }
  840. }
  841. //---------------------------------------------------------------------------
  842. void __fastcall TSynchronizeChecklistDialog::CheckAll(bool Check)
  843. {
  844. FChangingItemMass = true;
  845. try
  846. {
  847. for (int Index = 0; Index < ListView->Items->Count; Index++)
  848. {
  849. ListView->Items->Item[Index]->Checked = Check;
  850. }
  851. }
  852. __finally
  853. {
  854. FChangingItemMass = false;
  855. }
  856. UpdateControls();
  857. }
  858. //---------------------------------------------------------------------------
  859. void __fastcall TSynchronizeChecklistDialog::CheckAllActionExecute(TObject * /*Sender*/)
  860. {
  861. CheckAll(true);
  862. }
  863. //---------------------------------------------------------------------------
  864. void __fastcall TSynchronizeChecklistDialog::UncheckAllActionExecute(TObject * /*Sender*/)
  865. {
  866. CheckAll(false);
  867. }
  868. //---------------------------------------------------------------------------
  869. bool TSynchronizeChecklistDialog::IterateItems(TListItem *& Item, TItemStates States)
  870. {
  871. Item = ListView->GetNextItem(Item, sdAll, States);
  872. return (Item != NULL);
  873. }
  874. //---------------------------------------------------------------------------
  875. bool TSynchronizeChecklistDialog::IterateSelectedItems(TListItem *& Item)
  876. {
  877. return IterateItems(Item, TItemStates() << isSelected);
  878. }
  879. //---------------------------------------------------------------------------
  880. void __fastcall TSynchronizeChecklistDialog::Check(bool Check)
  881. {
  882. FChangingItemMass = true;
  883. try
  884. {
  885. TListItem * Item = NULL;
  886. while (IterateSelectedItems(Item))
  887. {
  888. Item->Checked = Check;
  889. }
  890. }
  891. __finally
  892. {
  893. FChangingItemMass = false;
  894. }
  895. UpdateControls();
  896. }
  897. //---------------------------------------------------------------------------
  898. void __fastcall TSynchronizeChecklistDialog::CheckActionExecute(TObject * /*Sender*/)
  899. {
  900. Check(true);
  901. }
  902. //---------------------------------------------------------------------------
  903. void __fastcall TSynchronizeChecklistDialog::UncheckActionExecute(TObject * /*Sender*/)
  904. {
  905. Check(false);
  906. }
  907. //---------------------------------------------------------------------------
  908. void __fastcall TSynchronizeChecklistDialog::ListViewSelectItem(
  909. TObject * /*Sender*/, TListItem * /*Item*/, bool /*Selected*/)
  910. {
  911. // Delayed update of button status in case many items are being selected at once
  912. // Also change of selection causes buttons to flash, as for short period of time,
  913. // no item is selected
  914. UpdateTimer->Enabled = true;
  915. }
  916. //---------------------------------------------------------------------------
  917. void __fastcall TSynchronizeChecklistDialog::UpdateTimerTimer(
  918. TObject * /*Sender*/)
  919. {
  920. UpdateTimer->Enabled = false;
  921. UpdateControls();
  922. }
  923. //---------------------------------------------------------------------------
  924. TListItem * __fastcall TSynchronizeChecklistDialog::SelectAll(bool Select, int Action,
  925. bool OnlyTheAction)
  926. {
  927. TListItem * Result = NULL;
  928. for (int Index = 0; Index < ListView->Items->Count; Index++)
  929. {
  930. TListItem * Item = ListView->Items->Item[Index];
  931. if (Action == 0)
  932. {
  933. Item->Selected = Select;
  934. if (Result == NULL)
  935. {
  936. Result = Item;
  937. }
  938. }
  939. else
  940. {
  941. const TSynchronizeChecklist::TItem * ChecklistItem = GetChecklistItem(Item);
  942. bool WantedAction = (int(GetChecklistItemAction(ChecklistItem)) == Action);
  943. if (WantedAction || !OnlyTheAction)
  944. {
  945. Item->Selected = Select && WantedAction;
  946. if (WantedAction && (Result == NULL))
  947. {
  948. Result = Item;
  949. }
  950. }
  951. }
  952. }
  953. return Result;
  954. }
  955. //---------------------------------------------------------------------------
  956. void __fastcall TSynchronizeChecklistDialog::SelectAllActionExecute(
  957. TObject * /*Sender*/)
  958. {
  959. SelectAll(true);
  960. }
  961. //---------------------------------------------------------------------------
  962. void __fastcall TSynchronizeChecklistDialog::StatusBarMouseDown(
  963. TObject * /*Sender*/, TMouseButton /*Button*/, TShiftState Shift, int X,
  964. int /*Y*/)
  965. {
  966. int IPanel = PanelAt(X);
  967. if (IPanel >= 0)
  968. {
  969. TListItem * Item = SelectAll(true, IPanel, Shift.Contains(ssCtrl));
  970. if (Item != NULL)
  971. {
  972. Item->MakeVisible(false);
  973. Item->Focused = true;
  974. ListView->SetFocus();
  975. }
  976. }
  977. }
  978. //---------------------------------------------------------------------------
  979. TIEListViewColProperties * TSynchronizeChecklistDialog::GetColProperties()
  980. {
  981. return dynamic_cast<TIEListViewColProperties *>(ListView->ColProperties);
  982. }
  983. //---------------------------------------------------------------------------
  984. void __fastcall TSynchronizeChecklistDialog::ListViewCompare(
  985. TObject * /*Sender*/, TListItem * Item1, TListItem * Item2, int /*Data*/,
  986. int & Compare)
  987. {
  988. const TSynchronizeChecklist::TItem * ChecklistItem1 = GetChecklistItem(Item1);
  989. const TSynchronizeChecklist::TItem * ChecklistItem2 = GetChecklistItem(Item2);
  990. TIEListViewColProperties * ColProperties = GetColProperties();
  991. switch (ColProperties->SortColumn)
  992. {
  993. case 0: // name
  994. Compare = AnsiCompareText(ChecklistItem1->GetFileName(), ChecklistItem2->GetFileName());
  995. break;
  996. // sorting by local and remote dir is the same
  997. case 1: // local dir
  998. case 5: // remote dir
  999. Compare = 0; // default sorting
  1000. break;
  1001. case 2: // local size
  1002. Compare = CompareNumber(ChecklistItem1->Local.Size, ChecklistItem2->Local.Size);
  1003. break;
  1004. case 3: // local changed
  1005. Compare = CompareFileTime(ChecklistItem1->Local.Modification,
  1006. ChecklistItem2->Local.Modification);
  1007. break;
  1008. case ImageColumnIndex: // action
  1009. Compare = CompareNumber(GetChecklistItemAction(ChecklistItem1), GetChecklistItemAction(ChecklistItem2));
  1010. break;
  1011. case 6: // remote size
  1012. Compare = CompareNumber(ChecklistItem1->Remote.Size, ChecklistItem2->Remote.Size);
  1013. break;
  1014. case 7: // remote changed
  1015. Compare = CompareFileTime(ChecklistItem1->Remote.Modification,
  1016. ChecklistItem2->Remote.Modification);
  1017. break;
  1018. }
  1019. if (Compare == 0)
  1020. {
  1021. Compare = TSynchronizeChecklist::Compare(ChecklistItem1, ChecklistItem2);
  1022. }
  1023. if (!ColProperties->SortAscending)
  1024. {
  1025. Compare = -Compare;
  1026. }
  1027. }
  1028. //---------------------------------------------------------------------------
  1029. void __fastcall TSynchronizeChecklistDialog::ListViewSecondaryColumnHeader(
  1030. TCustomIEListView * /*Sender*/, int Index, int & SecondaryColumn)
  1031. {
  1032. // "remote dir" column is sorting alias for "local dir" column
  1033. if (Index == 5)
  1034. {
  1035. SecondaryColumn = 1;
  1036. }
  1037. else
  1038. {
  1039. SecondaryColumn = -1;
  1040. }
  1041. }
  1042. //---------------------------------------------------------------------------
  1043. void __fastcall TSynchronizeChecklistDialog::ListViewContextPopup(
  1044. TObject * Sender, TPoint & MousePos, bool & Handled)
  1045. {
  1046. // to update source popup menu before TBX menu is created
  1047. UpdateControls();
  1048. MenuPopup(Sender, MousePos, Handled);
  1049. }
  1050. //---------------------------------------------------------------------------
  1051. void __fastcall TSynchronizeChecklistDialog::CustomCommandsActionExecute(
  1052. TObject * /*Sender*/)
  1053. {
  1054. TStrings * LocalFileList = new TStringList();
  1055. TStrings * RemoteFileList = new TStringList();
  1056. try
  1057. {
  1058. TListItem * Item = NULL;
  1059. while (IterateSelectedItems(Item))
  1060. {
  1061. const TSynchronizeChecklist::TItem * ChecklistItem = GetChecklistItem(Item);
  1062. DebugAssert((GetChecklistItemAction(ChecklistItem) == TSynchronizeChecklist::saUploadUpdate) ||
  1063. (GetChecklistItemAction(ChecklistItem) == TSynchronizeChecklist::saDownloadUpdate));
  1064. DebugAssert(ChecklistItem->RemoteFile != NULL);
  1065. UnicodeString LocalPath = ChecklistItem->GetLocalPath();
  1066. LocalFileList->Add(LocalPath);
  1067. UnicodeString RemotePath = ChecklistItem->GetRemotePath();
  1068. RemoteFileList->AddObject(RemotePath, ChecklistItem->RemoteFile);
  1069. }
  1070. }
  1071. catch(...)
  1072. {
  1073. delete LocalFileList;
  1074. delete RemoteFileList;
  1075. throw;
  1076. }
  1077. DebugAssert(FOnCustomCommandMenu != NULL);
  1078. FOnCustomCommandMenu(CustomCommandsAction, LocalFileList, RemoteFileList);
  1079. }
  1080. //---------------------------------------------------------------------------
  1081. void __fastcall TSynchronizeChecklistDialog::UpdateStatusBarSize()
  1082. {
  1083. int PanelWidth = Min(StatusBar->Width / PanelCount(), ScaleByTextHeight(this, 160));
  1084. for (int Index = 0; Index < PanelCount(); Index++)
  1085. {
  1086. StatusBar->Panels->Items[Index]->Width = PanelWidth;
  1087. }
  1088. }
  1089. //---------------------------------------------------------------------------
  1090. void __fastcall TSynchronizeChecklistDialog::StatusBarResize(TObject * /*Sender*/)
  1091. {
  1092. UpdateStatusBarSize();
  1093. }
  1094. //---------------------------------------------------------------------------
  1095. const TSynchronizeChecklist::TItem * TSynchronizeChecklistDialog::GetChecklistItem(
  1096. TListItem * Item)
  1097. {
  1098. return static_cast<const TSynchronizeChecklist::TItem *>(Item->Data);
  1099. }
  1100. //---------------------------------------------------------------------------
  1101. TSynchronizeChecklist::TAction & TSynchronizeChecklistDialog::GetChecklistItemAction(
  1102. const TSynchronizeChecklist::TItem * ChecklistItem)
  1103. {
  1104. TActions::iterator i = FActions.find(ChecklistItem);
  1105. if (i == FActions.end())
  1106. {
  1107. throw EInvalidOperation(L"");
  1108. }
  1109. return i->second;
  1110. }
  1111. //---------------------------------------------------------------------------
  1112. void __fastcall TSynchronizeChecklistDialog::ReverseActionExecute(TObject * /*Sender*/)
  1113. {
  1114. TAutoFlag Flag(FChangingItemMass);
  1115. TListItem * Item = NULL;
  1116. while (IterateSelectedItems(Item))
  1117. {
  1118. const TSynchronizeChecklist::TItem * ChecklistItem = GetChecklistItem(Item);
  1119. TSynchronizeChecklist::TAction & Action = GetChecklistItemAction(ChecklistItem);
  1120. TSynchronizeChecklist::TAction NewAction = TSynchronizeChecklist::Reverse(Action);
  1121. if (DebugAlwaysTrue(Action != NewAction))
  1122. {
  1123. CountItemTotal(ChecklistItem, -1);
  1124. if (Item->Checked)
  1125. {
  1126. CountItem(ChecklistItem, -1);
  1127. }
  1128. Action = NewAction;
  1129. CountItemTotal(ChecklistItem, 1);
  1130. if (Item->Checked)
  1131. {
  1132. // item size may differ with action (0 for delete, but non-0 for new file transfer)
  1133. CountItem(ChecklistItem, 1);
  1134. }
  1135. LoadItem(Item);
  1136. }
  1137. }
  1138. Flag.Release();
  1139. UpdateControls();
  1140. }
  1141. //---------------------------------------------------------------------------
  1142. void __fastcall TSynchronizeChecklistDialog::ListViewClick(TObject * /*Sender*/)
  1143. {
  1144. TKeyboardState KeyState;
  1145. GetKeyboardState(KeyState);
  1146. TShiftState ShiftState = KeyboardStateToShiftState(KeyState);
  1147. // when selecting, do not reverse, even when user clicked on action column
  1148. if (!ShiftState.Contains(ssShift) && !ShiftState.Contains(ssCtrl))
  1149. {
  1150. TPoint P = ListView->ScreenToClient(Mouse->CursorPos);
  1151. TRect R = GetColumnHeaderRect(ImageColumnIndex);
  1152. if ((R.Left <= P.x) && (P.x <= R.Right))
  1153. {
  1154. // If no item was selected before the click, the action is not enabled yet here,
  1155. // and Execute would be noop, force update
  1156. UpdateControls();
  1157. ReverseAction->Execute();
  1158. }
  1159. }
  1160. }
  1161. //---------------------------------------------------------------------------
  1162. void __fastcall TSynchronizeChecklistDialog::Dispatch(void * AMessage)
  1163. {
  1164. TMessage & Message = *reinterpret_cast<TMessage *>(AMessage);
  1165. if (Message.Msg == WM_SYSCOMMAND)
  1166. {
  1167. if (!HandleMinimizeSysCommand(Message))
  1168. {
  1169. TForm::Dispatch(AMessage);
  1170. }
  1171. }
  1172. else if (Message.Msg == WM_WANTS_MOUSEWHEEL_INACTIVE)
  1173. {
  1174. Message.Result = FSynchronizing ? 1 : 0;
  1175. }
  1176. else if (Message.Msg == WM_MANAGES_CAPTION)
  1177. {
  1178. // calling UpdateControls would cause status bar flicker
  1179. UpdateCaption();
  1180. Message.Result = 1;
  1181. }
  1182. else
  1183. {
  1184. TForm::Dispatch(AMessage);
  1185. }
  1186. }
  1187. //---------------------------------------------------------------------------
  1188. void __fastcall TSynchronizeChecklistDialog::UpdateImages()
  1189. {
  1190. ListView->SmallImages = ShellImageListForControl(this, ilsSmall);
  1191. }
  1192. //---------------------------------------------------------------------------
  1193. void __fastcall TSynchronizeChecklistDialog::FormAfterMonitorDpiChanged(TObject *, int OldDPI, int NewDPI)
  1194. {
  1195. DebugUsedParam2(OldDPI, NewDPI);
  1196. UpdateImages();
  1197. UpdateStatusBarSize();
  1198. }
  1199. //---------------------------------------------------------------------------
  1200. void __fastcall TSynchronizeChecklistDialog::ProcessedItem(void * /*Token*/, const TSynchronizeChecklist::TItem * ChecklistItem)
  1201. {
  1202. TListItem * Item = FChecklistToListViewMap[ChecklistItem];
  1203. DebugAssert(Item->Checked);
  1204. Item->Checked = false;
  1205. Item->MakeVisible(false);
  1206. }
  1207. //---------------------------------------------------------------------------
  1208. void __fastcall TSynchronizeChecklistDialog::UpdatedSynchronizationChecklistItems(
  1209. const TSynchronizeChecklist::TItemList & Items)
  1210. {
  1211. TSynchronizeChecklist::TItemList::const_iterator Iter = Items.begin();
  1212. while (Iter != Items.end())
  1213. {
  1214. const TSynchronizeChecklist::TItem * ChecklistItem = *Iter;
  1215. TListItem * Item = FChecklistToListViewMap[ChecklistItem];
  1216. LoadItem(Item);
  1217. // When called from FOnSynchronize, we rely on a caller never to update size of an item that had size already,
  1218. // otherwise we get it counted twice here.
  1219. if (Item->Checked)
  1220. {
  1221. CountItemSize(ChecklistItem, 1);
  1222. }
  1223. Iter++;
  1224. }
  1225. UpdateControls();
  1226. }
  1227. //---------------------------------------------------------------------------
  1228. void TSynchronizeChecklistDialog::DoSynchronize(bool Queue)
  1229. {
  1230. ListView->SelectAll(smNone);
  1231. for (int Index = 0; Index < ListView->Items->Count; Index++)
  1232. {
  1233. TListItem * Item = ListView->Items->Item[Index];
  1234. const TSynchronizeChecklist::TItem * ChecklistItem = GetChecklistItem(Item);
  1235. FChecklist->Update(ChecklistItem, Item->Checked, GetChecklistItemAction(ChecklistItem));
  1236. }
  1237. TAutoFlag Flag(FSynchronizing);
  1238. UpdateControls();
  1239. try
  1240. {
  1241. if (Queue)
  1242. {
  1243. FOnQueueSynchronize(FToken);
  1244. }
  1245. else
  1246. {
  1247. FOnSynchronize(FToken, ProcessedItem, UpdatedSynchronizationChecklistItems);
  1248. }
  1249. }
  1250. catch (Exception & E)
  1251. {
  1252. FException.reset(CloneException(&E));
  1253. }
  1254. // Needed when called from the drop down menu
  1255. ModalResult = OkButton->ModalResult;
  1256. }
  1257. //---------------------------------------------------------------------------
  1258. void __fastcall TSynchronizeChecklistDialog::OkButtonClick(TObject *)
  1259. {
  1260. bool Queue = OpenInNewWindow();
  1261. if (Queue && (FOnQueueSynchronize == NULL))
  1262. {
  1263. Beep();
  1264. ModalResult = mrNone;
  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 == ListView->Items->Count - 1)
  1326. {
  1327. FocusIndex--;
  1328. }
  1329. else
  1330. {
  1331. FocusIndex++;
  1332. }
  1333. ListView->ItemFocused = ListView->Items->Item[FocusIndex];
  1334. }
  1335. ListView->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. ListView->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 < ListView->Items->Count; Index++)
  1422. {
  1423. TListItem * Item = ListView->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::DoBrowse(TOperationSide Side)
  1449. {
  1450. const TSynchronizeChecklist::TItem * ChecklistItem = GetChecklistItem(ListView->Selected);
  1451. TSynchronizeChecklist::TAction Action = GetChecklistItemAction(ChecklistItem);
  1452. FOnSynchronizeBrowse(Side, Action, ChecklistItem);
  1453. }
  1454. //---------------------------------------------------------------------------
  1455. void __fastcall TSynchronizeChecklistDialog::BrowseLocalActionExecute(TObject *)
  1456. {
  1457. DoBrowse(osLocal);
  1458. }
  1459. //---------------------------------------------------------------------------
  1460. void __fastcall TSynchronizeChecklistDialog::BrowseRemoteActionExecute(TObject *)
  1461. {
  1462. DoBrowse(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::ListViewRecreate(TObject *)
  1490. {
  1491. FChecklistToListViewMap.clear();
  1492. for (int Index = 0; Index < ListView->Items->Count; Index++)
  1493. {
  1494. TListItem * Item = ListView->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 < ListView->Items->Count; Index++)
  1514. {
  1515. const TSynchronizeChecklist::TItem * ChecklistItem = GetChecklistItem(ListView->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 = ListView->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(ListView->Selected != NULL))
  1541. {
  1542. Item = ListView->Selected;
  1543. }
  1544. else if (ListView->Items->Count > 0)
  1545. {
  1546. Item = ListView->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 && (ListView->SelCount == 2))
  1631. {
  1632. TListItem * NextSelected = ListView->GetNextItem(Item, sdAll, TItemStates() << isSelected);
  1633. if (NextSelected == NULL)
  1634. {
  1635. NextSelected = ListView->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 < ListView->Items->Count; Index++)
  1651. {
  1652. TListItem * ItemI = ListView->Items->Item[Index];
  1653. ItemI->Selected = (ItemI == Item) || (ItemI == Item2);
  1654. }
  1655. ListView->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 = !ListView->IsItemVisible(Item) || !ListView->IsItemVisible(Item2);
  1659. if (FlickerExpected)
  1660. {
  1661. // does not seem to have any effect
  1662. ListView->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. ListView->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 = ListView->GetNextItem(Item, sdAll, TItemStates());
  1694. if (Item == NULL)
  1695. {
  1696. Item = ListView->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. //---------------------------------------------------------------------------