SynchronizeChecklist.cpp 48 KB

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