SynchronizeChecklist.cpp 37 KB

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