SynchronizeChecklist.cpp 34 KB

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