SynchronizeChecklist.cpp 45 KB

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