SynchronizeChecklist.cpp 35 KB

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