SynchronizeChecklist.cpp 37 KB

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