SynchronizeChecklist.cpp 64 KB

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