SynchronizeChecklist.cpp 42 KB

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