SynchronizeChecklist.cpp 32 KB

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