1
0

SynchronizeChecklist.cpp 33 KB

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