SynchronizeChecklist.cpp 63 KB

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