SynchronizeChecklist.cpp 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088
  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 from 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. // Can be simplified using GetScrollPos
  404. TScrollInfo ScrollInfo;
  405. ZeroMemory(&ScrollInfo, sizeof(ScrollInfo));
  406. ScrollInfo.cbSize = sizeof(ScrollInfo);
  407. ScrollInfo.fMask = SIF_POS;
  408. GetScrollInfo(ListView->Handle, SB_HORZ, &ScrollInfo);
  409. R.Left -= ScrollInfo.nPos;
  410. R.Right -= ScrollInfo.nPos;
  411. return R;
  412. }
  413. //---------------------------------------------------------------------------
  414. void __fastcall TSynchronizeChecklistDialog::ListViewWindowProc(TMessage & Message)
  415. {
  416. if (Message.Msg == CN_NOTIFY)
  417. {
  418. TWMNotify & NotifyMessage = reinterpret_cast<TWMNotify &>(Message);
  419. if (NotifyMessage.NMHdr->code == NM_CUSTOMDRAW)
  420. {
  421. // workaround
  422. // Due to a bug in VCL, OnAdvancedCustomDrawSubItem is not called for any
  423. // other stage except for cdPrePaint. So we must call it ourselves.
  424. TNMLVCustomDraw * CustomDraw =
  425. reinterpret_cast<TNMLVCustomDraw *>(NotifyMessage.NMHdr);
  426. if (FLAGSET(CustomDraw->nmcd.dwDrawStage, CDDS_ITEM) &&
  427. FLAGSET(CustomDraw->nmcd.dwDrawStage, CDDS_SUBITEM) &&
  428. FLAGSET(CustomDraw->nmcd.dwDrawStage, CDDS_ITEMPOSTPAINT) &&
  429. (CustomDraw->iSubItem == ImageColumnIndex) &&
  430. (ActionImages->Width <= ListView->Columns->Items[CustomDraw->iSubItem]->Width))
  431. {
  432. TListItem * Item = ListView->Items->Item[CustomDraw->nmcd.dwItemSpec];
  433. const TSynchronizeChecklist::TItem * ChecklistItem = GetChecklistItem(Item);
  434. TRect HeaderR = GetColumnHeaderRect(CustomDraw->iSubItem);
  435. TRect R = Item->DisplayRect(drBounds);
  436. R.Left = HeaderR.Left + (HeaderR.Width() - ActionImages->Width) / 2;
  437. R.Right = HeaderR.Right;
  438. // workaround
  439. // doing this from ListViewAdvancedCustomDraw corrupts list view on Windows 7
  440. ImageList_Draw(reinterpret_cast<HIMAGELIST>(ActionImages->Handle),
  441. int(GetChecklistItemAction(ChecklistItem)), CustomDraw->nmcd.hdc,
  442. R.Left, ((R.Top + R.Bottom - ActionImages->Height) / 2), ILD_TRANSPARENT);
  443. }
  444. }
  445. }
  446. FOrigListViewWindowProc(Message);
  447. }
  448. //---------------------------------------------------------------------------
  449. void __fastcall TSynchronizeChecklistDialog::ListViewAdvancedCustomDrawSubItem(
  450. TCustomListView * /*Sender*/, TListItem * /*Item*/, int /*SubItem*/,
  451. TCustomDrawState /*State*/, TCustomDrawStage /*Stage*/, bool & /*DefaultDraw*/)
  452. {
  453. // this is just fake handler that makes the list view request custom draw notification above
  454. }
  455. //---------------------------------------------------------------------------
  456. void __fastcall TSynchronizeChecklistDialog::StatusBarDrawPanel(
  457. TStatusBar * StatusBar, TStatusPanel * Panel, const TRect & Rect)
  458. {
  459. bool Possible;
  460. int ActionIndex = Panel->Index;
  461. TSynchronizeChecklist::TAction Action = TSynchronizeChecklist::TAction(ActionIndex);
  462. if (FTotals[ActionIndex] > 0)
  463. {
  464. // if direction is overriden to an action that is otherwise not possible
  465. // in given synchronization mode, we still want to show the stats
  466. Possible = true;
  467. }
  468. else
  469. {
  470. switch (Action)
  471. {
  472. case TSynchronizeChecklist::saNone:
  473. Possible = true;
  474. break;
  475. case TSynchronizeChecklist::saUploadNew:
  476. Possible = ((FMode == smRemote) || (FMode == smBoth)) &&
  477. FLAGCLEAR(FParams, TTerminal::spTimestamp);
  478. break;
  479. case TSynchronizeChecklist::saDownloadNew:
  480. Possible = ((FMode == smLocal) || (FMode == smBoth)) &&
  481. FLAGCLEAR(FParams, TTerminal::spTimestamp);
  482. break;
  483. case TSynchronizeChecklist::saUploadUpdate:
  484. Possible =
  485. ((FMode == smRemote) || (FMode == smBoth)) &&
  486. (FLAGCLEAR(FParams, TTerminal::spNotByTime) || FLAGSET(FParams, TTerminal::spBySize));
  487. break;
  488. case TSynchronizeChecklist::saDownloadUpdate:
  489. Possible =
  490. ((FMode == smLocal) || (FMode == smBoth)) &&
  491. (FLAGCLEAR(FParams, TTerminal::spNotByTime) || FLAGSET(FParams, TTerminal::spBySize));
  492. break;
  493. case TSynchronizeChecklist::saDeleteRemote:
  494. Possible = (FMode == smRemote) &&
  495. FLAGCLEAR(FParams, TTerminal::spTimestamp);
  496. break;
  497. case TSynchronizeChecklist::saDeleteLocal:
  498. Possible = (FMode == smLocal) &&
  499. FLAGCLEAR(FParams, TTerminal::spTimestamp);
  500. break;
  501. default:
  502. DebugFail();
  503. Possible = false;
  504. break;
  505. }
  506. }
  507. UnicodeString PanelText;
  508. if (Possible)
  509. {
  510. PanelText = FORMAT(LoadStrPart(SYNCHRONIZE_SELECTED_ACTIONS, 1),
  511. (FormatNumber(FChecked[ActionIndex]),
  512. FormatNumber(FTotals[ActionIndex])));
  513. if ((FChecked[ActionIndex] > 0) &&
  514. ((ActionIndex == 0) || !IsItemSizeIrrelevant(Action)))
  515. {
  516. PanelText += FORMAT(L" (%s)", (FormatBytes(FCheckedSize[ActionIndex])));
  517. }
  518. }
  519. else
  520. {
  521. PanelText = LoadStrPart(SYNCHRONIZE_SELECTED_ACTIONS, 2);
  522. }
  523. int TextHeight = StatusBar->Canvas->TextHeight(PanelText);
  524. int X = Rect.Left + ActionImages->Width + 4;
  525. int Y = (Rect.Top + Rect.Bottom - TextHeight) / 2;
  526. StatusBar->Canvas->TextRect(Rect, X, Y, PanelText);
  527. X = Rect.Left + 1;
  528. Y = ((Rect.Top + Rect.Bottom - ActionImages->Height) / 2);
  529. int ImageIndex = ActionIndex;
  530. ActionImages->Draw(StatusBar->Canvas, X, Y, ImageIndex, Possible);
  531. }
  532. //---------------------------------------------------------------------------
  533. int __fastcall TSynchronizeChecklistDialog::PanelCount()
  534. {
  535. // last "panel" is technical
  536. return StatusBar->Panels->Count - 1;
  537. }
  538. //---------------------------------------------------------------------------
  539. int __fastcall TSynchronizeChecklistDialog::PanelAt(int X)
  540. {
  541. int Result = 0;
  542. while ((X > StatusBar->Panels->Items[Result]->Width) &&
  543. (Result < PanelCount()))
  544. {
  545. X -= StatusBar->Panels->Items[Result]->Width;
  546. Result++;
  547. }
  548. return ((Result < StatusBar->Panels->Count - 1) ? Result : -1);
  549. }
  550. //---------------------------------------------------------------------------
  551. void __fastcall TSynchronizeChecklistDialog::StatusBarMouseMove(
  552. TObject * /*Sender*/, TShiftState /*Shift*/, int X, int /*Y*/)
  553. {
  554. UnicodeString Hint;
  555. int IPanel = PanelAt(X);
  556. if (IPanel >= 0)
  557. {
  558. Hint = StatusBar->Panels->Items[IPanel]->Text;
  559. if (IPanel > 0)
  560. {
  561. Hint = FORMAT(L"%s\n%s", (Hint, FGeneralHint));
  562. }
  563. }
  564. if (Hint != StatusBar->Hint)
  565. {
  566. Application->CancelHint();
  567. StatusBar->Hint = Hint;
  568. }
  569. }
  570. //---------------------------------------------------------------------------
  571. void __fastcall TSynchronizeChecklistDialog::ListViewChange(
  572. TObject * /*Sender*/, TListItem * Item, TItemChange Change)
  573. {
  574. if ((Change == ctState) && (FChangingItem == Item) && (FChangingItem != NULL))
  575. {
  576. if (!FChangingItemIgnore)
  577. {
  578. DebugAssert(Item->Data != NULL);
  579. if ((FChangingItemChecked != Item->Checked) && (Item->Data != NULL))
  580. {
  581. const TSynchronizeChecklist::TItem * ChecklistItem = GetChecklistItem(Item);
  582. int ActionIndex = int(GetChecklistItemAction(ChecklistItem));
  583. int Diff = Item->Checked ? 1 : -1;
  584. FChecked[ActionIndex] += Diff;
  585. FChecked[0] += Diff;
  586. __int64 ItemSize = GetItemSize(ChecklistItem);
  587. if (!Item->Checked)
  588. {
  589. ItemSize = -ItemSize;
  590. }
  591. FCheckedSize[ActionIndex] += ItemSize;
  592. FCheckedSize[0] += ItemSize;
  593. if (!FChangingItemMass)
  594. {
  595. UpdateControls();
  596. }
  597. }
  598. }
  599. FChangingItem = NULL;
  600. }
  601. }
  602. //---------------------------------------------------------------------------
  603. void __fastcall TSynchronizeChecklistDialog::ListViewChanging(
  604. TObject * /*Sender*/, TListItem * Item, TItemChange Change,
  605. bool & /*AllowChange*/)
  606. {
  607. if (Change == ctState)
  608. {
  609. FChangingItem = Item;
  610. FChangingItemChecked = Item->Checked;
  611. }
  612. else
  613. {
  614. DebugAssert(FChangingItem == NULL);
  615. FChangingItem = NULL;
  616. }
  617. }
  618. //---------------------------------------------------------------------------
  619. void __fastcall TSynchronizeChecklistDialog::CheckAll(bool Check)
  620. {
  621. FChangingItemMass = true;
  622. try
  623. {
  624. for (int Index = 0; Index < ListView->Items->Count; Index++)
  625. {
  626. ListView->Items->Item[Index]->Checked = Check;
  627. }
  628. }
  629. __finally
  630. {
  631. FChangingItemMass = false;
  632. }
  633. UpdateControls();
  634. }
  635. //---------------------------------------------------------------------------
  636. void __fastcall TSynchronizeChecklistDialog::CheckAllActionExecute(TObject * /*Sender*/)
  637. {
  638. CheckAll(true);
  639. }
  640. //---------------------------------------------------------------------------
  641. void __fastcall TSynchronizeChecklistDialog::UncheckAllActionExecute(TObject * /*Sender*/)
  642. {
  643. CheckAll(false);
  644. }
  645. //---------------------------------------------------------------------------
  646. void __fastcall TSynchronizeChecklistDialog::Check(bool Check)
  647. {
  648. FChangingItemMass = true;
  649. try
  650. {
  651. TListItem * Item = ListView->Selected;
  652. while (Item != NULL)
  653. {
  654. Item->Checked = Check;
  655. Item = ListView->GetNextItem(Item, sdAll, TItemStates() << isSelected);
  656. }
  657. }
  658. __finally
  659. {
  660. FChangingItemMass = false;
  661. }
  662. UpdateControls();
  663. }
  664. //---------------------------------------------------------------------------
  665. void __fastcall TSynchronizeChecklistDialog::CheckActionExecute(TObject * /*Sender*/)
  666. {
  667. Check(true);
  668. }
  669. //---------------------------------------------------------------------------
  670. void __fastcall TSynchronizeChecklistDialog::UncheckActionExecute(TObject * /*Sender*/)
  671. {
  672. Check(false);
  673. }
  674. //---------------------------------------------------------------------------
  675. void __fastcall TSynchronizeChecklistDialog::ListViewSelectItem(
  676. TObject * /*Sender*/, TListItem * /*Item*/, bool /*Selected*/)
  677. {
  678. // Delayed update of button status in case many items are being selected at once
  679. // Also change of selection causes buttons to flash, as for short period of time,
  680. // no item is selected
  681. UpdateTimer->Enabled = true;
  682. }
  683. //---------------------------------------------------------------------------
  684. void __fastcall TSynchronizeChecklistDialog::UpdateTimerTimer(
  685. TObject * /*Sender*/)
  686. {
  687. UpdateTimer->Enabled = false;
  688. UpdateControls();
  689. }
  690. //---------------------------------------------------------------------------
  691. TListItem * __fastcall TSynchronizeChecklistDialog::SelectAll(bool Select, int Action,
  692. bool OnlyTheAction)
  693. {
  694. TListItem * Result = NULL;
  695. for (int Index = 0; Index < ListView->Items->Count; Index++)
  696. {
  697. TListItem * Item = ListView->Items->Item[Index];
  698. if (Action == 0)
  699. {
  700. Item->Selected = Select;
  701. if (Result == NULL)
  702. {
  703. Result = Item;
  704. }
  705. }
  706. else
  707. {
  708. const TSynchronizeChecklist::TItem * ChecklistItem = GetChecklistItem(Item);
  709. bool WantedAction = (int(GetChecklistItemAction(ChecklistItem)) == Action);
  710. if (WantedAction || !OnlyTheAction)
  711. {
  712. Item->Selected = Select && WantedAction;
  713. if (WantedAction && (Result == NULL))
  714. {
  715. Result = Item;
  716. }
  717. }
  718. }
  719. }
  720. return Result;
  721. }
  722. //---------------------------------------------------------------------------
  723. void __fastcall TSynchronizeChecklistDialog::SelectAllActionExecute(
  724. TObject * /*Sender*/)
  725. {
  726. SelectAll(true);
  727. }
  728. //---------------------------------------------------------------------------
  729. void __fastcall TSynchronizeChecklistDialog::StatusBarMouseDown(
  730. TObject * /*Sender*/, TMouseButton /*Button*/, TShiftState Shift, int X,
  731. int /*Y*/)
  732. {
  733. int IPanel = PanelAt(X);
  734. if (IPanel >= 0)
  735. {
  736. TListItem * Item = SelectAll(true, IPanel, Shift.Contains(ssCtrl));
  737. if (Item != NULL)
  738. {
  739. Item->MakeVisible(false);
  740. Item->Focused = true;
  741. ListView->SetFocus();
  742. }
  743. }
  744. }
  745. //---------------------------------------------------------------------------
  746. int __fastcall TSynchronizeChecklistDialog::CompareNumber(__int64 Value1,
  747. __int64 Value2)
  748. {
  749. int Result;
  750. if (Value1 < Value2)
  751. {
  752. Result = -1;
  753. }
  754. else if (Value1 == Value2)
  755. {
  756. Result = 0;
  757. }
  758. else
  759. {
  760. Result = 1;
  761. }
  762. return Result;
  763. }
  764. //---------------------------------------------------------------------------
  765. void __fastcall TSynchronizeChecklistDialog::ListViewCompare(
  766. TObject * /*Sender*/, TListItem * Item1, TListItem * Item2, int /*Data*/,
  767. int & Compare)
  768. {
  769. const TSynchronizeChecklist::TItem * ChecklistItem1 = GetChecklistItem(Item1);
  770. const TSynchronizeChecklist::TItem * ChecklistItem2 = GetChecklistItem(Item2);
  771. TIEListViewColProperties * ColProperties =
  772. dynamic_cast<TIEListViewColProperties *>(ListView->ColProperties);
  773. switch (ColProperties->SortColumn)
  774. {
  775. case 0: // name
  776. Compare = AnsiCompareText(ChecklistItem1->GetFileName(), ChecklistItem2->GetFileName());
  777. break;
  778. // sorting by local and remote dir is the same
  779. case 1: // local dir
  780. case 5: // remote dir
  781. Compare = 0; // default sorting
  782. break;
  783. case 2: // local size
  784. Compare = CompareNumber(ChecklistItem1->Local.Size, ChecklistItem2->Local.Size);
  785. break;
  786. case 3: // local changed
  787. Compare = CompareFileTime(ChecklistItem1->Local.Modification,
  788. ChecklistItem2->Local.Modification);
  789. break;
  790. case ImageColumnIndex: // action
  791. Compare = CompareNumber(GetChecklistItemAction(ChecklistItem1), GetChecklistItemAction(ChecklistItem2));
  792. break;
  793. case 6: // remote size
  794. Compare = CompareNumber(ChecklistItem1->Remote.Size, ChecklistItem2->Remote.Size);
  795. break;
  796. case 7: // remote changed
  797. Compare = CompareFileTime(ChecklistItem1->Remote.Modification,
  798. ChecklistItem2->Remote.Modification);
  799. break;
  800. }
  801. if (Compare == 0)
  802. {
  803. if (!ChecklistItem1->Local.Directory.IsEmpty())
  804. {
  805. Compare = AnsiCompareText(ChecklistItem1->Local.Directory, ChecklistItem2->Local.Directory);
  806. }
  807. else
  808. {
  809. DebugAssert(!ChecklistItem1->Remote.Directory.IsEmpty());
  810. Compare = AnsiCompareText(ChecklistItem1->Remote.Directory, ChecklistItem2->Remote.Directory);
  811. }
  812. if (Compare == 0)
  813. {
  814. Compare = AnsiCompareText(ChecklistItem1->GetFileName(), ChecklistItem2->GetFileName());
  815. }
  816. }
  817. if (!ColProperties->SortAscending)
  818. {
  819. Compare = -Compare;
  820. }
  821. }
  822. //---------------------------------------------------------------------------
  823. void __fastcall TSynchronizeChecklistDialog::ListViewSecondaryColumnHeader(
  824. TCustomIEListView * /*Sender*/, int Index, int & SecondaryColumn)
  825. {
  826. // "remote dir" column is sorting alias for "local dir" column
  827. if (Index == 5)
  828. {
  829. SecondaryColumn = 1;
  830. }
  831. else
  832. {
  833. SecondaryColumn = -1;
  834. }
  835. }
  836. //---------------------------------------------------------------------------
  837. void __fastcall TSynchronizeChecklistDialog::ListViewContextPopup(
  838. TObject * Sender, TPoint & MousePos, bool & Handled)
  839. {
  840. // to update source popup menu before TBX menu is created
  841. UpdateControls();
  842. MenuPopup(Sender, MousePos, Handled);
  843. }
  844. //---------------------------------------------------------------------------
  845. void __fastcall TSynchronizeChecklistDialog::CustomCommandsActionExecute(
  846. TObject * /*Sender*/)
  847. {
  848. TStrings * LocalFileList = new TStringList();
  849. TStrings * RemoteFileList = new TStringList();
  850. try
  851. {
  852. TListItem * Item = ListView->Selected;
  853. DebugAssert(Item != NULL);
  854. while (Item != NULL)
  855. {
  856. const TSynchronizeChecklist::TItem * ChecklistItem = GetChecklistItem(Item);
  857. DebugAssert((GetChecklistItemAction(ChecklistItem) == TSynchronizeChecklist::saUploadUpdate) ||
  858. (GetChecklistItemAction(ChecklistItem) == TSynchronizeChecklist::saDownloadUpdate));
  859. DebugAssert(ChecklistItem->RemoteFile != NULL);
  860. UnicodeString LocalPath =
  861. IncludeTrailingBackslash(ChecklistItem->Local.Directory) +
  862. ChecklistItem->Local.FileName;
  863. LocalFileList->Add(LocalPath);
  864. UnicodeString RemotePath =
  865. UnixIncludeTrailingBackslash(ChecklistItem->Remote.Directory) +
  866. ChecklistItem->Remote.FileName;
  867. RemoteFileList->AddObject(RemotePath, ChecklistItem->RemoteFile);
  868. Item = ListView->GetNextItem(Item, sdAll, TItemStates() << isSelected);
  869. }
  870. }
  871. catch(...)
  872. {
  873. delete LocalFileList;
  874. delete RemoteFileList;
  875. throw;
  876. }
  877. DebugAssert(FOnCustomCommandMenu != NULL);
  878. FOnCustomCommandMenu(CustomCommandsAction, LocalFileList, RemoteFileList);
  879. }
  880. //---------------------------------------------------------------------------
  881. void __fastcall TSynchronizeChecklistDialog::UpdateStatusBarSize()
  882. {
  883. int PanelWidth = Min(StatusBar->Width / PanelCount(), ScaleByTextHeight(this, 160));
  884. for (int Index = 0; Index < PanelCount(); Index++)
  885. {
  886. StatusBar->Panels->Items[Index]->Width = PanelWidth;
  887. }
  888. }
  889. //---------------------------------------------------------------------------
  890. void __fastcall TSynchronizeChecklistDialog::StatusBarResize(TObject * /*Sender*/)
  891. {
  892. UpdateStatusBarSize();
  893. }
  894. //---------------------------------------------------------------------------
  895. const TSynchronizeChecklist::TItem * TSynchronizeChecklistDialog::GetChecklistItem(
  896. TListItem * Item)
  897. {
  898. return static_cast<const TSynchronizeChecklist::TItem *>(Item->Data);
  899. }
  900. //---------------------------------------------------------------------------
  901. TSynchronizeChecklist::TAction & TSynchronizeChecklistDialog::GetChecklistItemAction(
  902. const TSynchronizeChecklist::TItem * ChecklistItem)
  903. {
  904. TActions::iterator i = FActions.find(ChecklistItem);
  905. if (i == FActions.end())
  906. {
  907. throw EInvalidOperation(L"");
  908. }
  909. return i->second;
  910. }
  911. //---------------------------------------------------------------------------
  912. void __fastcall TSynchronizeChecklistDialog::ReverseActionExecute(TObject * /*Sender*/)
  913. {
  914. TAutoFlag Flag(FChangingItemMass);
  915. TListItem * Item = ListView->Selected;
  916. while (Item != NULL)
  917. {
  918. const TSynchronizeChecklist::TItem * ChecklistItem = GetChecklistItem(Item);
  919. TSynchronizeChecklist::TAction & Action = GetChecklistItemAction(ChecklistItem);
  920. TSynchronizeChecklist::TAction NewAction = TSynchronizeChecklist::Reverse(Action);
  921. if (DebugAlwaysTrue(Action != NewAction))
  922. {
  923. int ActionIndex = int(Action);
  924. FTotals[ActionIndex]--;
  925. if (Item->Checked)
  926. {
  927. FChecked[ActionIndex]--;
  928. __int64 ItemSize = GetItemSize(ChecklistItem);
  929. FCheckedSize[ActionIndex] -= ItemSize;
  930. FCheckedSize[0] -= ItemSize;
  931. }
  932. Action = NewAction;
  933. ActionIndex = int(Action);
  934. FTotals[ActionIndex]++;
  935. if (Item->Checked)
  936. {
  937. FChecked[ActionIndex]++;
  938. // item size may differ with action (0 for delete, but non-0 for new file transfer)
  939. __int64 ItemSize = GetItemSize(ChecklistItem);
  940. FCheckedSize[ActionIndex] += ItemSize;
  941. FCheckedSize[0] += ItemSize;
  942. }
  943. LoadItem(Item);
  944. }
  945. Item = ListView->GetNextItem(Item, sdAll, TItemStates() << isSelected);
  946. }
  947. Flag.Release();
  948. UpdateControls();
  949. }
  950. //---------------------------------------------------------------------------
  951. void __fastcall TSynchronizeChecklistDialog::ListViewClick(TObject * /*Sender*/)
  952. {
  953. TKeyboardState KeyState;
  954. GetKeyboardState(KeyState);
  955. TShiftState ShiftState = KeyboardStateToShiftState(KeyState);
  956. // when selecting, do not reverse, even when user clicked on action column
  957. if (!ShiftState.Contains(ssShift) && !ShiftState.Contains(ssCtrl))
  958. {
  959. TPoint P = ListView->ScreenToClient(Mouse->CursorPos);
  960. TRect R = GetColumnHeaderRect(ImageColumnIndex);
  961. if ((R.Left <= P.x) && (P.x <= R.Right))
  962. {
  963. // If no item was selected before the click, the action is not enabled yet here,
  964. // and Execute would be noop, force update
  965. UpdateControls();
  966. ReverseAction->Execute();
  967. }
  968. }
  969. }
  970. //---------------------------------------------------------------------------
  971. void __fastcall TSynchronizeChecklistDialog::Dispatch(void * Message)
  972. {
  973. TMessage * M = reinterpret_cast<TMessage*>(Message);
  974. if (M->Msg == WM_SYSCOMMAND)
  975. {
  976. if (!HandleMinimizeSysCommand(*M))
  977. {
  978. TForm::Dispatch(Message);
  979. }
  980. }
  981. else if (M->Msg == CM_DPICHANGED)
  982. {
  983. CMDpiChanged(*M);
  984. }
  985. else
  986. {
  987. TForm::Dispatch(Message);
  988. }
  989. }
  990. //---------------------------------------------------------------------------
  991. void __fastcall TSynchronizeChecklistDialog::UpdateImages()
  992. {
  993. ListView->SmallImages = ShellImageListForControl(this, ilsSmall);
  994. }
  995. //---------------------------------------------------------------------------
  996. void __fastcall TSynchronizeChecklistDialog::CMDpiChanged(TMessage & Message)
  997. {
  998. TForm::Dispatch(&Message);
  999. UpdateImages();
  1000. }
  1001. //---------------------------------------------------------------------------