SynchronizeChecklist.cpp 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391
  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. void __fastcall TSynchronizeChecklistDialog::ListViewCompare(
  827. TObject * /*Sender*/, TListItem * Item1, TListItem * Item2, int /*Data*/,
  828. int & Compare)
  829. {
  830. const TSynchronizeChecklist::TItem * ChecklistItem1 = GetChecklistItem(Item1);
  831. const TSynchronizeChecklist::TItem * ChecklistItem2 = GetChecklistItem(Item2);
  832. TIEListViewColProperties * ColProperties =
  833. dynamic_cast<TIEListViewColProperties *>(ListView->ColProperties);
  834. switch (ColProperties->SortColumn)
  835. {
  836. case 0: // name
  837. Compare = AnsiCompareText(ChecklistItem1->GetFileName(), ChecklistItem2->GetFileName());
  838. break;
  839. // sorting by local and remote dir is the same
  840. case 1: // local dir
  841. case 5: // remote dir
  842. Compare = 0; // default sorting
  843. break;
  844. case 2: // local size
  845. Compare = CompareNumber(ChecklistItem1->Local.Size, ChecklistItem2->Local.Size);
  846. break;
  847. case 3: // local changed
  848. Compare = CompareFileTime(ChecklistItem1->Local.Modification,
  849. ChecklistItem2->Local.Modification);
  850. break;
  851. case ImageColumnIndex: // action
  852. Compare = CompareNumber(GetChecklistItemAction(ChecklistItem1), GetChecklistItemAction(ChecklistItem2));
  853. break;
  854. case 6: // remote size
  855. Compare = CompareNumber(ChecklistItem1->Remote.Size, ChecklistItem2->Remote.Size);
  856. break;
  857. case 7: // remote changed
  858. Compare = CompareFileTime(ChecklistItem1->Remote.Modification,
  859. ChecklistItem2->Remote.Modification);
  860. break;
  861. }
  862. if (Compare == 0)
  863. {
  864. if (!ChecklistItem1->Local.Directory.IsEmpty())
  865. {
  866. Compare = AnsiCompareText(ChecklistItem1->Local.Directory, ChecklistItem2->Local.Directory);
  867. }
  868. else
  869. {
  870. DebugAssert(!ChecklistItem1->Remote.Directory.IsEmpty());
  871. Compare = AnsiCompareText(ChecklistItem1->Remote.Directory, ChecklistItem2->Remote.Directory);
  872. }
  873. if (Compare == 0)
  874. {
  875. Compare = AnsiCompareText(ChecklistItem1->GetFileName(), ChecklistItem2->GetFileName());
  876. }
  877. }
  878. if (!ColProperties->SortAscending)
  879. {
  880. Compare = -Compare;
  881. }
  882. }
  883. //---------------------------------------------------------------------------
  884. void __fastcall TSynchronizeChecklistDialog::ListViewSecondaryColumnHeader(
  885. TCustomIEListView * /*Sender*/, int Index, int & SecondaryColumn)
  886. {
  887. // "remote dir" column is sorting alias for "local dir" column
  888. if (Index == 5)
  889. {
  890. SecondaryColumn = 1;
  891. }
  892. else
  893. {
  894. SecondaryColumn = -1;
  895. }
  896. }
  897. //---------------------------------------------------------------------------
  898. void __fastcall TSynchronizeChecklistDialog::ListViewContextPopup(
  899. TObject * Sender, TPoint & MousePos, bool & Handled)
  900. {
  901. // to update source popup menu before TBX menu is created
  902. UpdateControls();
  903. MenuPopup(Sender, MousePos, Handled);
  904. }
  905. //---------------------------------------------------------------------------
  906. void __fastcall TSynchronizeChecklistDialog::CustomCommandsActionExecute(
  907. TObject * /*Sender*/)
  908. {
  909. TStrings * LocalFileList = new TStringList();
  910. TStrings * RemoteFileList = new TStringList();
  911. try
  912. {
  913. TListItem * Item = ListView->Selected;
  914. DebugAssert(Item != NULL);
  915. while (Item != NULL)
  916. {
  917. const TSynchronizeChecklist::TItem * ChecklistItem = GetChecklistItem(Item);
  918. DebugAssert((GetChecklistItemAction(ChecklistItem) == TSynchronizeChecklist::saUploadUpdate) ||
  919. (GetChecklistItemAction(ChecklistItem) == TSynchronizeChecklist::saDownloadUpdate));
  920. DebugAssert(ChecklistItem->RemoteFile != NULL);
  921. UnicodeString LocalPath =
  922. IncludeTrailingBackslash(ChecklistItem->Local.Directory) +
  923. ChecklistItem->Local.FileName;
  924. LocalFileList->Add(LocalPath);
  925. UnicodeString RemotePath =
  926. UnixIncludeTrailingBackslash(ChecklistItem->Remote.Directory) +
  927. ChecklistItem->Remote.FileName;
  928. RemoteFileList->AddObject(RemotePath, ChecklistItem->RemoteFile);
  929. Item = ListView->GetNextItem(Item, sdAll, TItemStates() << isSelected);
  930. }
  931. }
  932. catch(...)
  933. {
  934. delete LocalFileList;
  935. delete RemoteFileList;
  936. throw;
  937. }
  938. DebugAssert(FOnCustomCommandMenu != NULL);
  939. FOnCustomCommandMenu(CustomCommandsAction, LocalFileList, RemoteFileList);
  940. }
  941. //---------------------------------------------------------------------------
  942. void __fastcall TSynchronizeChecklistDialog::UpdateStatusBarSize()
  943. {
  944. int PanelWidth = Min(StatusBar->Width / PanelCount(), ScaleByTextHeight(this, 160));
  945. for (int Index = 0; Index < PanelCount(); Index++)
  946. {
  947. StatusBar->Panels->Items[Index]->Width = PanelWidth;
  948. }
  949. }
  950. //---------------------------------------------------------------------------
  951. void __fastcall TSynchronizeChecklistDialog::StatusBarResize(TObject * /*Sender*/)
  952. {
  953. UpdateStatusBarSize();
  954. }
  955. //---------------------------------------------------------------------------
  956. const TSynchronizeChecklist::TItem * TSynchronizeChecklistDialog::GetChecklistItem(
  957. TListItem * Item)
  958. {
  959. return static_cast<const TSynchronizeChecklist::TItem *>(Item->Data);
  960. }
  961. //---------------------------------------------------------------------------
  962. TSynchronizeChecklist::TAction & TSynchronizeChecklistDialog::GetChecklistItemAction(
  963. const TSynchronizeChecklist::TItem * ChecklistItem)
  964. {
  965. TActions::iterator i = FActions.find(ChecklistItem);
  966. if (i == FActions.end())
  967. {
  968. throw EInvalidOperation(L"");
  969. }
  970. return i->second;
  971. }
  972. //---------------------------------------------------------------------------
  973. void __fastcall TSynchronizeChecklistDialog::ReverseActionExecute(TObject * /*Sender*/)
  974. {
  975. TAutoFlag Flag(FChangingItemMass);
  976. TListItem * Item = ListView->Selected;
  977. while (Item != NULL)
  978. {
  979. const TSynchronizeChecklist::TItem * ChecklistItem = GetChecklistItem(Item);
  980. TSynchronizeChecklist::TAction & Action = GetChecklistItemAction(ChecklistItem);
  981. TSynchronizeChecklist::TAction NewAction = TSynchronizeChecklist::Reverse(Action);
  982. if (DebugAlwaysTrue(Action != NewAction))
  983. {
  984. CountItemTotal(ChecklistItem, -1);
  985. if (Item->Checked)
  986. {
  987. CountItem(ChecklistItem, -1);
  988. }
  989. Action = NewAction;
  990. CountItemTotal(ChecklistItem, 1);
  991. if (Item->Checked)
  992. {
  993. // item size may differ with action (0 for delete, but non-0 for new file transfer)
  994. CountItem(ChecklistItem, 1);
  995. }
  996. LoadItem(Item);
  997. }
  998. Item = ListView->GetNextItem(Item, sdAll, TItemStates() << isSelected);
  999. }
  1000. Flag.Release();
  1001. UpdateControls();
  1002. }
  1003. //---------------------------------------------------------------------------
  1004. void __fastcall TSynchronizeChecklistDialog::ListViewClick(TObject * /*Sender*/)
  1005. {
  1006. TKeyboardState KeyState;
  1007. GetKeyboardState(KeyState);
  1008. TShiftState ShiftState = KeyboardStateToShiftState(KeyState);
  1009. // when selecting, do not reverse, even when user clicked on action column
  1010. if (!ShiftState.Contains(ssShift) && !ShiftState.Contains(ssCtrl))
  1011. {
  1012. TPoint P = ListView->ScreenToClient(Mouse->CursorPos);
  1013. TRect R = GetColumnHeaderRect(ImageColumnIndex);
  1014. if ((R.Left <= P.x) && (P.x <= R.Right))
  1015. {
  1016. // If no item was selected before the click, the action is not enabled yet here,
  1017. // and Execute would be noop, force update
  1018. UpdateControls();
  1019. ReverseAction->Execute();
  1020. }
  1021. }
  1022. }
  1023. //---------------------------------------------------------------------------
  1024. void __fastcall TSynchronizeChecklistDialog::Dispatch(void * AMessage)
  1025. {
  1026. TMessage & Message = *reinterpret_cast<TMessage *>(AMessage);
  1027. if (Message.Msg == WM_SYSCOMMAND)
  1028. {
  1029. if (!HandleMinimizeSysCommand(Message))
  1030. {
  1031. TForm::Dispatch(AMessage);
  1032. }
  1033. }
  1034. else if (Message.Msg == CM_DPICHANGED)
  1035. {
  1036. CMDpiChanged(Message);
  1037. }
  1038. else if (Message.Msg == WM_WANTS_MOUSEWHEEL_INACTIVE)
  1039. {
  1040. Message.Result = FSynchronizing ? 1 : 0;
  1041. }
  1042. else if (Message.Msg == WM_MANAGES_CAPTION)
  1043. {
  1044. // calling UpdateControls would cause status bar flicker
  1045. UpdateCaption();
  1046. Message.Result = 1;
  1047. }
  1048. else
  1049. {
  1050. TForm::Dispatch(AMessage);
  1051. }
  1052. }
  1053. //---------------------------------------------------------------------------
  1054. void __fastcall TSynchronizeChecklistDialog::UpdateImages()
  1055. {
  1056. ListView->SmallImages = ShellImageListForControl(this, ilsSmall);
  1057. }
  1058. //---------------------------------------------------------------------------
  1059. void __fastcall TSynchronizeChecklistDialog::CMDpiChanged(TMessage & Message)
  1060. {
  1061. TForm::Dispatch(&Message);
  1062. UpdateImages();
  1063. }
  1064. //---------------------------------------------------------------------------
  1065. void __fastcall TSynchronizeChecklistDialog::ProcessedItem(void * /*Token*/, const TSynchronizeChecklist::TItem * ChecklistItem)
  1066. {
  1067. TListItem * Item = FChecklistToListViewMap[ChecklistItem];
  1068. DebugAssert(Item->Checked);
  1069. Item->Checked = false;
  1070. Item->MakeVisible(false);
  1071. }
  1072. //---------------------------------------------------------------------------
  1073. void __fastcall TSynchronizeChecklistDialog::UpdatedSynchronizationChecklistItems(
  1074. const TSynchronizeChecklist::TItemList & Items)
  1075. {
  1076. TSynchronizeChecklist::TItemList::const_iterator Iter = Items.begin();
  1077. while (Iter != Items.end())
  1078. {
  1079. const TSynchronizeChecklist::TItem * ChecklistItem = *Iter;
  1080. TListItem * Item = FChecklistToListViewMap[ChecklistItem];
  1081. LoadItem(Item);
  1082. // When called from FOnSynchronize, we rely on a caller never to update size of an item that had size already,
  1083. // otherwise we get it counted twice here.
  1084. if (Item->Checked)
  1085. {
  1086. CountItemSize(ChecklistItem, 1);
  1087. }
  1088. Iter++;
  1089. }
  1090. UpdateControls();
  1091. }
  1092. //---------------------------------------------------------------------------
  1093. void __fastcall TSynchronizeChecklistDialog::OkButtonClick(TObject * /*Sender*/)
  1094. {
  1095. ListView->SelectAll(smNone);
  1096. for (int Index = 0; Index < ListView->Items->Count; Index++)
  1097. {
  1098. TListItem * Item = ListView->Items->Item[Index];
  1099. const TSynchronizeChecklist::TItem * ChecklistItem = GetChecklistItem(Item);
  1100. FChecklist->Update(ChecklistItem, Item->Checked, GetChecklistItemAction(ChecklistItem));
  1101. }
  1102. TAutoFlag Flag(FSynchronizing);
  1103. UpdateControls();
  1104. try
  1105. {
  1106. FOnSynchronize(FToken, ProcessedItem, UpdatedSynchronizationChecklistItems);
  1107. }
  1108. catch (Exception & E)
  1109. {
  1110. FException.reset(CloneException(&E));
  1111. }
  1112. }
  1113. //---------------------------------------------------------------------------
  1114. void __fastcall TSynchronizeChecklistDialog::CalculateSizeActionExecute(TObject * /*Sender*/)
  1115. {
  1116. TItemStates States;
  1117. if (!IsKeyPressed(VK_CONTROL))
  1118. {
  1119. States << isSelected;
  1120. }
  1121. TSynchronizeChecklist::TItemList Items;
  1122. TListItem * Item = NULL;
  1123. while ((Item = ListView->GetNextItem(Item, sdAll, States)) != NULL)
  1124. {
  1125. const TSynchronizeChecklist::TItem * ChecklistItem = GetChecklistItem(Item);
  1126. Items.push_back(ChecklistItem);
  1127. if (Item->Checked)
  1128. {
  1129. CountItemSize(ChecklistItem, -1);
  1130. }
  1131. }
  1132. try
  1133. {
  1134. FOnSynchronizeChecklistCalculateSize(FChecklist, Items, FToken);
  1135. }
  1136. __finally
  1137. {
  1138. UpdatedSynchronizationChecklistItems(Items);
  1139. }
  1140. }
  1141. //---------------------------------------------------------------------------
  1142. void __fastcall TSynchronizeChecklistDialog::CalculateSizeAllActionExecute(TObject * Sender)
  1143. {
  1144. DebugAssert(IsKeyPressed(VK_CONTROL));
  1145. CalculateSizeActionExecute(Sender);
  1146. }
  1147. //---------------------------------------------------------------------------
  1148. TSynchronizeChecklistDialog::TSynchronizeMoveItems __fastcall TSynchronizeChecklistDialog::GetMoveItems()
  1149. {
  1150. if ((ListView->SelCount != 2) || DebugAlwaysFalse(FSynchronizing))
  1151. {
  1152. return TSynchronizeMoveItems();
  1153. }
  1154. else
  1155. {
  1156. TListItem * Item1 = ListView->Selected;
  1157. const TSynchronizeChecklist::TItem * ChecklistItem1 = GetChecklistItem(DebugNotNull(Item1));
  1158. TListItem * Item2 = ListView->GetNextItem(ListView->Selected, sdAll, TItemStates() << isSelected);
  1159. const TSynchronizeChecklist::TItem * ChecklistItem2 = GetChecklistItem(DebugNotNull(Item2));
  1160. if (ChecklistItem1->IsDirectory != ChecklistItem2->IsDirectory)
  1161. {
  1162. return TSynchronizeMoveItems();
  1163. }
  1164. else
  1165. {
  1166. TSynchronizeChecklist::TAction Action1 = GetChecklistItemAction(ChecklistItem1);
  1167. TSynchronizeChecklist::TAction Action2 = GetChecklistItemAction(ChecklistItem2);
  1168. if ((Action1 == TSynchronizeChecklist::saUploadNew) && (Action2 == TSynchronizeChecklist::saDeleteRemote))
  1169. {
  1170. return TSynchronizeMoveItems(ChecklistItem1, ChecklistItem2);
  1171. }
  1172. else if ((Action1 == TSynchronizeChecklist::saDownloadNew) && (Action2 == TSynchronizeChecklist::saDeleteLocal))
  1173. {
  1174. return TSynchronizeMoveItems(ChecklistItem1, ChecklistItem2);
  1175. }
  1176. else if ((Action1 == TSynchronizeChecklist::saDeleteRemote) && (Action2 == TSynchronizeChecklist::saUploadNew))
  1177. {
  1178. return TSynchronizeMoveItems(ChecklistItem2, ChecklistItem1);
  1179. }
  1180. else if ((Action1 == TSynchronizeChecklist::saDeleteLocal) && (Action2 == TSynchronizeChecklist::saDownloadNew))
  1181. {
  1182. return TSynchronizeMoveItems(ChecklistItem2, ChecklistItem1);
  1183. }
  1184. else
  1185. {
  1186. return TSynchronizeMoveItems();
  1187. }
  1188. }
  1189. }
  1190. }
  1191. //---------------------------------------------------------------------------
  1192. void __fastcall TSynchronizeChecklistDialog::DeleteItem(TListItem * Item)
  1193. {
  1194. const TSynchronizeChecklist::TItem * ChecklistItem = GetChecklistItem(Item);
  1195. CountItemTotal(ChecklistItem, -1);
  1196. if (Item->Checked)
  1197. {
  1198. CountItem(ChecklistItem, -1);
  1199. }
  1200. FActions.erase(ChecklistItem);
  1201. FChecklistToListViewMap.erase(ChecklistItem);
  1202. FChecklist->Delete(ChecklistItem);
  1203. ListView->Items->Delete(Item->Index);
  1204. }
  1205. //---------------------------------------------------------------------------
  1206. void __fastcall TSynchronizeChecklistDialog::MoveActionExecute(TObject *)
  1207. {
  1208. TSynchronizeMoveItems MoveItems = GetMoveItems();
  1209. TSynchronizeChecklist::TAction Action2 = GetChecklistItemAction(MoveItems.second);
  1210. TOperationSide Side;
  1211. UnicodeString FileName;
  1212. UnicodeString NewFileName;
  1213. TRemoteFile * RemoteFile;
  1214. if (Action2 == TSynchronizeChecklist::saDeleteRemote)
  1215. {
  1216. Side = osRemote;
  1217. FileName = UnixCombinePaths(MoveItems.second->Remote.Directory, MoveItems.second->Remote.FileName);
  1218. NewFileName = UnixCombinePaths(MoveItems.first->Remote.Directory, MoveItems.first->Local.FileName);
  1219. RemoteFile = MoveItems.second->RemoteFile;
  1220. }
  1221. else if (Action2 == TSynchronizeChecklist::saDeleteLocal)
  1222. {
  1223. Side = osLocal;
  1224. FileName = TPath::Combine(MoveItems.second->Local.Directory, MoveItems.second->Local.FileName);
  1225. NewFileName = TPath::Combine(MoveItems.first->Local.Directory, MoveItems.first->Remote.FileName);
  1226. RemoteFile = NULL;
  1227. }
  1228. else
  1229. {
  1230. DebugFail();
  1231. Abort();
  1232. }
  1233. FOnSynchronizeMove(Side, FileName, NewFileName, RemoteFile);
  1234. TListItem * Item1 = DebugNotNull(ListView->FindData(0, const_cast<TSynchronizeChecklist::TItem *>(MoveItems.first), true, false));
  1235. TListItem * Item2 = DebugNotNull(ListView->FindData(0, const_cast<TSynchronizeChecklist::TItem *>(MoveItems.second), true, false));
  1236. DeleteItem(Item1);
  1237. DeleteItem(Item2);
  1238. }
  1239. //---------------------------------------------------------------------------
  1240. void __fastcall TSynchronizeChecklistDialog::CheckDirectory(bool Check)
  1241. {
  1242. std::unique_ptr<TStringList> Directories(new TStringList());
  1243. TListItem * Item = ListView->Selected;
  1244. while (Item != NULL)
  1245. {
  1246. const TSynchronizeChecklist::TItem * ChecklistItem = GetChecklistItem(Item);
  1247. // It does not matter if we use local or remote directory
  1248. Directories->Add(IncludeTrailingBackslash(ChecklistItem->Local.Directory));
  1249. Item = ListView->GetNextItem(Item, sdAll, TItemStates() << isSelected);
  1250. }
  1251. TAutoFlag ChangingItemMassSwitch(FChangingItemMass);
  1252. for (int Index = 0; Index < ListView->Items->Count; Index++)
  1253. {
  1254. TListItem * Item = ListView->Items->Item[Index];
  1255. const TSynchronizeChecklist::TItem * ChecklistItem = GetChecklistItem(Item);
  1256. UnicodeString Directory = IncludeTrailingBackslash(ChecklistItem->Local.Directory);
  1257. for (int Index2 = 0; Index2 < Directories->Count; Index2++)
  1258. {
  1259. if (StartsText(Directories->Strings[Index2], Directory))
  1260. {
  1261. Item->Checked = Check;
  1262. }
  1263. }
  1264. }
  1265. ChangingItemMassSwitch.Release();
  1266. UpdateControls();
  1267. }
  1268. //---------------------------------------------------------------------------
  1269. void __fastcall TSynchronizeChecklistDialog::CheckDirectoryActionExecute(TObject *)
  1270. {
  1271. CheckDirectory(true);
  1272. }
  1273. //---------------------------------------------------------------------------
  1274. void __fastcall TSynchronizeChecklistDialog::UncheckDirectoryActionExecute(TObject *)
  1275. {
  1276. CheckDirectory(false);
  1277. }
  1278. //---------------------------------------------------------------------------
  1279. void __fastcall TSynchronizeChecklistDialog::DoBrowse(TOperationSide Side)
  1280. {
  1281. const TSynchronizeChecklist::TItem * ChecklistItem = GetChecklistItem(ListView->Selected);
  1282. TSynchronizeChecklist::TAction Action = GetChecklistItemAction(ChecklistItem);
  1283. FOnSynchronizeBrowse(Side, Action, ChecklistItem);
  1284. }
  1285. //---------------------------------------------------------------------------
  1286. void __fastcall TSynchronizeChecklistDialog::BrowseLocalActionExecute(TObject *)
  1287. {
  1288. DoBrowse(osLocal);
  1289. }
  1290. //---------------------------------------------------------------------------
  1291. void __fastcall TSynchronizeChecklistDialog::BrowseRemoteActionExecute(TObject *)
  1292. {
  1293. DoBrowse(osRemote);
  1294. }
  1295. //---------------------------------------------------------------------------