SynchronizeChecklist.cpp 49 KB

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