SynchronizeChecklist.cpp 37 KB

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