SynchronizeChecklist.cpp 42 KB

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