Synchronize.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include <Common.h>
  5. #include "WinInterface.h"
  6. #include "Synchronize.h"
  7. #include "VCLCommon.h"
  8. #include "CopyParams.h"
  9. #include "Terminal.h"
  10. #include "GUITools.h"
  11. #include <ScpMain.h>
  12. #include <Configuration.h>
  13. #include <TextsWin.h>
  14. #include <HelpWin.h>
  15. #include <CustomWinConfiguration.h>
  16. //---------------------------------------------------------------------------
  17. #pragma package(smart_init)
  18. #pragma link "XPThemes"
  19. #pragma link "HistoryComboBox"
  20. #pragma link "GrayedCheckBox"
  21. #pragma resource "*.dfm"
  22. //---------------------------------------------------------------------------
  23. const int WM_USER_STOP = WM_WINSCP_USER + 2;
  24. //---------------------------------------------------------------------------
  25. bool __fastcall DoSynchronizeDialog(TSynchronizeParamType & Params,
  26. const TCopyParamType * CopyParams, TSynchronizeStartStopEvent OnStartStop,
  27. bool & SaveSettings, int Options, TGetSynchronizeOptionsEvent OnGetOptions)
  28. {
  29. bool Result;
  30. TSynchronizeDialog * Dialog = new TSynchronizeDialog(Application,
  31. OnStartStop, OnGetOptions);
  32. try
  33. {
  34. Dialog->Options = Options;
  35. Dialog->Params = Params;
  36. Dialog->CopyParams = *CopyParams;
  37. Dialog->SaveSettings = SaveSettings;
  38. Result = Dialog->Execute();
  39. if (Result)
  40. {
  41. SaveSettings = Dialog->SaveSettings;
  42. Params = Dialog->Params;
  43. }
  44. }
  45. __finally
  46. {
  47. delete Dialog;
  48. }
  49. return Result;
  50. }
  51. //---------------------------------------------------------------------------
  52. const TSynchronizeDialog::MaxLogItems = 1000;
  53. //---------------------------------------------------------------------------
  54. __fastcall TSynchronizeDialog::TSynchronizeDialog(TComponent * Owner,
  55. TSynchronizeStartStopEvent OnStartStop, TGetSynchronizeOptionsEvent OnGetOptions)
  56. : TForm(Owner)
  57. {
  58. UseSystemSettings(this);
  59. FOptions = 0;
  60. FSynchronizing = false;
  61. FMinimizedByMe = false;
  62. FPresetsMenu = new TPopupMenu(this);
  63. FOnStartStop = OnStartStop;
  64. FOnGetOptions = OnGetOptions;
  65. FSynchronizeOptions = NULL;
  66. InstallPathWordBreakProc(LocalDirectoryEdit);
  67. InstallPathWordBreakProc(RemoteDirectoryEdit);
  68. }
  69. //---------------------------------------------------------------------------
  70. __fastcall TSynchronizeDialog::~TSynchronizeDialog()
  71. {
  72. delete FSynchronizeOptions;
  73. delete FPresetsMenu;
  74. }
  75. //---------------------------------------------------------------------------
  76. void __fastcall TSynchronizeDialog::UpdateControls()
  77. {
  78. EnableControl(StartButton, !LocalDirectoryEdit->Text.IsEmpty() &&
  79. !RemoteDirectoryEdit->Text.IsEmpty());
  80. TButton * OldButton = FSynchronizing ? StartButton : StopButton;
  81. TButton * NewButton = FSynchronizing ? StopButton : StartButton;
  82. if (!NewButton->Visible || OldButton->Visible)
  83. {
  84. NewButton->Visible = true;
  85. if (OldButton->Focused())
  86. {
  87. NewButton->SetFocus();
  88. }
  89. OldButton->Default = false;
  90. NewButton->Default = true;
  91. OldButton->Visible = false;
  92. // some of the above steps hides accelerators when start button is pressed with mouse
  93. ResetSystemSettings(this);
  94. }
  95. Caption = LoadStr(FSynchronizing ? SYNCHRONIZE_SYCHRONIZING : SYNCHRONIZE_TITLE);
  96. EnableControl(TransferSettingsButton, !FSynchronizing);
  97. CancelButton->Visible = !FSynchronizing || FLAGSET(FOptions, soNoMinimize);
  98. EnableControl(CancelButton, !FSynchronizing);
  99. EnableControl(DirectoriesGroup, !FSynchronizing);
  100. EnableControl(OptionsGroup, !FSynchronizing);
  101. EnableControl(CopyParamGroup, !FSynchronizing);
  102. MinimizeButton->Visible = FSynchronizing && FLAGCLEAR(FOptions, soNoMinimize);
  103. EnableControl(SynchronizeSelectedOnlyCheck,
  104. OptionsGroup->Enabled && FLAGSET(FOptions, soAllowSelectedOnly));
  105. AnsiString InfoStr = CopyParams.GetInfoStr("; ");
  106. CopyParamLabel->Caption = InfoStr;
  107. CopyParamLabel->Hint = InfoStr;
  108. CopyParamLabel->ShowHint =
  109. (CopyParamLabel->Canvas->TextWidth(InfoStr) > (CopyParamLabel->Width * 3 / 2));
  110. if (LogPanel->Visible != FSynchronizing)
  111. {
  112. if (FSynchronizing)
  113. {
  114. LogPanel->Visible = true;
  115. ClientHeight = ClientHeight + LogPanel->Height;
  116. }
  117. else
  118. {
  119. ClientHeight = ClientHeight - LogPanel->Height;
  120. LogPanel->Visible = false;
  121. }
  122. }
  123. }
  124. //---------------------------------------------------------------------------
  125. void __fastcall TSynchronizeDialog::ControlChange(TObject * /*Sender*/)
  126. {
  127. UpdateControls();
  128. }
  129. //---------------------------------------------------------------------------
  130. bool __fastcall TSynchronizeDialog::Execute()
  131. {
  132. // at start assume that copy param is current preset
  133. FPreset = GUIConfiguration->CopyParamCurrent;
  134. LocalDirectoryEdit->Items = CustomWinConfiguration->History["LocalDirectory"];
  135. RemoteDirectoryEdit->Items = CustomWinConfiguration->History["RemoteDirectory"];
  136. ShowModal();
  137. return true;
  138. }
  139. //---------------------------------------------------------------------------
  140. void __fastcall TSynchronizeDialog::SetParams(const TSynchronizeParamType& value)
  141. {
  142. FParams = value;
  143. RemoteDirectoryEdit->Text = value.RemoteDirectory;
  144. LocalDirectoryEdit->Text = value.LocalDirectory;
  145. SynchronizeDeleteCheck->Checked = FLAGSET(value.Params, spDelete);
  146. SynchronizeExistingOnlyCheck->Checked = FLAGSET(value.Params, spExistingOnly);
  147. SynchronizeSelectedOnlyCheck->Checked = FLAGSET(value.Params, spSelectedOnly);
  148. SynchronizeRecursiveCheck->Checked = FLAGSET(value.Options, soRecurse);
  149. SynchronizeSynchronizeCheck->State =
  150. FLAGSET(value.Options, soSynchronizeAsk) ? cbGrayed :
  151. (FLAGSET(value.Options, soSynchronize) ? cbChecked : cbUnchecked);
  152. }
  153. //---------------------------------------------------------------------------
  154. TSynchronizeParamType __fastcall TSynchronizeDialog::GetParams()
  155. {
  156. TSynchronizeParamType Result = FParams;
  157. Result.RemoteDirectory = RemoteDirectoryEdit->Text;
  158. Result.LocalDirectory = LocalDirectoryEdit->Text;
  159. Result.Params =
  160. (Result.Params & ~(spDelete | spExistingOnly | spSelectedOnly | spTimestamp)) |
  161. FLAGMASK(SynchronizeDeleteCheck->Checked, spDelete) |
  162. FLAGMASK(SynchronizeExistingOnlyCheck->Checked, spExistingOnly) |
  163. FLAGMASK(SynchronizeSelectedOnlyCheck->Checked, spSelectedOnly);
  164. Result.Options =
  165. (Result.Options & ~(soRecurse | soSynchronize | soSynchronizeAsk)) |
  166. FLAGMASK(SynchronizeRecursiveCheck->Checked, soRecurse) |
  167. FLAGMASK(SynchronizeSynchronizeCheck->State == cbChecked, soSynchronize) |
  168. FLAGMASK(SynchronizeSynchronizeCheck->State == cbGrayed, soSynchronizeAsk);
  169. return Result;
  170. }
  171. //---------------------------------------------------------------------------
  172. void __fastcall TSynchronizeDialog::LocalDirectoryBrowseButtonClick(
  173. TObject * /*Sender*/)
  174. {
  175. AnsiString Directory = LocalDirectoryEdit->Text;
  176. if (SelectDirectory(Directory, LoadStr(SELECT_LOCAL_DIRECTORY), false))
  177. {
  178. LocalDirectoryEdit->Text = Directory;
  179. }
  180. }
  181. //---------------------------------------------------------------------------
  182. void __fastcall TSynchronizeDialog::SetOptions(int value)
  183. {
  184. if (Options != value)
  185. {
  186. FOptions = value;
  187. UpdateControls();
  188. }
  189. }
  190. //---------------------------------------------------------------------------
  191. void __fastcall TSynchronizeDialog::TransferSettingsButtonClick(
  192. TObject * /*Sender*/)
  193. {
  194. if (FLAGCLEAR(FOptions, soDoNotUsePresets))
  195. {
  196. CopyParamListPopup(
  197. TransferSettingsButton->ClientToScreen(TPoint(0, TransferSettingsButton->Height)),
  198. FPresetsMenu, FCopyParams, FPreset, CopyParamClick, cplCustomize);
  199. }
  200. else
  201. {
  202. CopyParamGroupDblClick(NULL);
  203. }
  204. }
  205. //---------------------------------------------------------------------------
  206. void __fastcall TSynchronizeDialog::DoStartStop(bool Start, bool Synchronize)
  207. {
  208. if (FOnStartStop)
  209. {
  210. TSynchronizeParamType SParams = GetParams();
  211. SParams.Options =
  212. (SParams.Options & ~(soSynchronize | soSynchronizeAsk)) |
  213. FLAGMASK(Synchronize, soSynchronize);
  214. if (Start)
  215. {
  216. delete FSynchronizeOptions;
  217. FSynchronizeOptions = new TSynchronizeOptions;
  218. FOnGetOptions(SParams.Params, *FSynchronizeOptions);
  219. }
  220. FOnStartStop(this, Start, SParams, CopyParams, FSynchronizeOptions, DoAbort,
  221. NULL, DoLog);
  222. }
  223. }
  224. //---------------------------------------------------------------------------
  225. void __fastcall TSynchronizeDialog::Dispatch(void * Message)
  226. {
  227. assert(Message);
  228. if ((reinterpret_cast<TMessage *>(Message)->Msg == WM_USER_STOP) && FAbort)
  229. {
  230. if (FSynchronizing)
  231. {
  232. Stop();
  233. }
  234. if (FClose)
  235. {
  236. FClose = false;
  237. ModalResult = mrCancel;
  238. }
  239. }
  240. else
  241. {
  242. TForm::Dispatch(Message);
  243. }
  244. }
  245. //---------------------------------------------------------------------------
  246. void __fastcall TSynchronizeDialog::DoAbort(TObject * /*Sender*/, bool Close)
  247. {
  248. FAbort = true;
  249. FClose = Close;
  250. PostMessage(Handle, WM_USER_STOP, 0, 0);
  251. }
  252. //---------------------------------------------------------------------------
  253. void __fastcall TSynchronizeDialog::DoLog(TSynchronizeController * /*Controller*/,
  254. TSynchronizeLogEntry Entry, const AnsiString Message)
  255. {
  256. LogView->Items->BeginUpdate();
  257. try
  258. {
  259. TListItem * Item = LogView->Items->Add();
  260. Item->Caption = Now().TimeString();
  261. Item->SubItems->Add(Message);
  262. Item->MakeVisible(false);
  263. while (LogView->Items->Count > MaxLogItems)
  264. {
  265. LogView->Items->Delete(0);
  266. }
  267. }
  268. __finally
  269. {
  270. LogView->Items->EndUpdate();
  271. if (Entry == slScan)
  272. {
  273. // redraw log before the scanning block update
  274. LogView->Repaint();
  275. }
  276. }
  277. }
  278. //---------------------------------------------------------------------------
  279. void __fastcall TSynchronizeDialog::StartButtonClick(TObject * /*Sender*/)
  280. {
  281. bool Synchronize;
  282. bool Continue = true;
  283. if (SynchronizeSynchronizeCheck->State == cbGrayed)
  284. {
  285. TMessageParams Params(mpNeverAskAgainCheck);
  286. switch (MoreMessageDialog(LoadStr(SYNCHRONISE_BEFORE_KEEPUPTODATE),
  287. NULL, qtConfirmation, qaYes | qaNo | qaCancel, HELP_KEEPUPTODATE_SYNCHRONIZE,
  288. &Params))
  289. {
  290. case qaNeverAskAgain:
  291. SynchronizeSynchronizeCheck->State = cbChecked;
  292. // fall thru
  293. break;
  294. case qaYes:
  295. Synchronize = true;
  296. break;
  297. case qaNo:
  298. Synchronize = false;
  299. break;
  300. default:
  301. case qaCancel:
  302. Continue = false;
  303. break;
  304. };
  305. }
  306. else
  307. {
  308. Synchronize = SynchronizeSynchronizeCheck->Checked;
  309. }
  310. if (Continue)
  311. {
  312. assert(!FSynchronizing);
  313. LocalDirectoryEdit->SaveToHistory();
  314. CustomWinConfiguration->History["LocalDirectory"] = LocalDirectoryEdit->Items;
  315. RemoteDirectoryEdit->SaveToHistory();
  316. CustomWinConfiguration->History["RemoteDirectory"] = RemoteDirectoryEdit->Items;
  317. FSynchronizing = true;
  318. try
  319. {
  320. UpdateControls();
  321. Repaint();
  322. FAbort = false;
  323. DoStartStop(true, Synchronize);
  324. }
  325. catch(...)
  326. {
  327. FSynchronizing = false;
  328. UpdateControls();
  329. throw;
  330. }
  331. }
  332. }
  333. //---------------------------------------------------------------------------
  334. void __fastcall TSynchronizeDialog::StopButtonClick(TObject * /*Sender*/)
  335. {
  336. Stop();
  337. }
  338. //---------------------------------------------------------------------------
  339. void __fastcall TSynchronizeDialog::Stop()
  340. {
  341. FSynchronizing = false;
  342. DoStartStop(false, false);
  343. UpdateControls();
  344. Repaint();
  345. if (IsIconic(Application->Handle) && FMinimizedByMe)
  346. {
  347. FMinimizedByMe = false;
  348. Application->Restore();
  349. }
  350. }
  351. //---------------------------------------------------------------------------
  352. void __fastcall TSynchronizeDialog::MinimizeButtonClick(TObject * /*Sender*/)
  353. {
  354. Application->Minimize();
  355. FMinimizedByMe = true;
  356. }
  357. //---------------------------------------------------------------------------
  358. void __fastcall TSynchronizeDialog::SetSaveSettings(bool value)
  359. {
  360. SaveSettingsCheck->Checked = value;
  361. }
  362. //---------------------------------------------------------------------------
  363. bool __fastcall TSynchronizeDialog::GetSaveSettings()
  364. {
  365. return SaveSettingsCheck->Checked;
  366. }
  367. //---------------------------------------------------------------------------
  368. void __fastcall TSynchronizeDialog::FormShow(TObject * /*Sender*/)
  369. {
  370. ClearLog();
  371. UpdateControls();
  372. }
  373. //---------------------------------------------------------------------------
  374. void __fastcall TSynchronizeDialog::FormCloseQuery(TObject * /*Sender*/,
  375. bool & /*CanClose*/)
  376. {
  377. if (FSynchronizing)
  378. {
  379. Stop();
  380. }
  381. }
  382. //---------------------------------------------------------------------------
  383. TCopyParamType __fastcall TSynchronizeDialog::GetCopyParams()
  384. {
  385. TCopyParamType Result = FCopyParams;
  386. Result.PreserveTime = true;
  387. return Result;
  388. }
  389. //---------------------------------------------------------------------------
  390. void __fastcall TSynchronizeDialog::SetCopyParams(const TCopyParamType & value)
  391. {
  392. FCopyParams = value;
  393. UpdateControls();
  394. }
  395. //---------------------------------------------------------------------------
  396. int __fastcall TSynchronizeDialog::CopyParamCustomDialogOptions()
  397. {
  398. return cfAllowTransferMode | cfAllowExcludeMask | cfAllowClearArchive | cfDisablePreserveTime;
  399. }
  400. //---------------------------------------------------------------------------
  401. void __fastcall TSynchronizeDialog::CopyParamClick(TObject * Sender)
  402. {
  403. assert(FLAGCLEAR(FOptions, soDoNotUsePresets));
  404. if (CopyParamListPopupClick(Sender, FCopyParams, FPreset,
  405. CopyParamCustomDialogOptions()))
  406. {
  407. UpdateControls();
  408. }
  409. }
  410. //---------------------------------------------------------------------------
  411. void __fastcall TSynchronizeDialog::CopyParamGroupContextPopup(
  412. TObject * /*Sender*/, TPoint & MousePos, bool & Handled)
  413. {
  414. if (FLAGCLEAR(FOptions, soDoNotUsePresets))
  415. {
  416. CopyParamListPopup(CopyParamGroup->ClientToScreen(MousePos), FPresetsMenu,
  417. FCopyParams, FPreset, CopyParamClick, cplCustomize | cplCustomizeDefault);
  418. Handled = true;
  419. }
  420. }
  421. //---------------------------------------------------------------------------
  422. void __fastcall TSynchronizeDialog::CopyParamGroupDblClick(TObject * /*Sender*/)
  423. {
  424. if (DoCopyParamCustomDialog(FCopyParams, CopyParamCustomDialogOptions()))
  425. {
  426. UpdateControls();
  427. }
  428. }
  429. //---------------------------------------------------------------------------
  430. void __fastcall TSynchronizeDialog::HelpButtonClick(TObject * /*Sender*/)
  431. {
  432. FormHelp(this);
  433. }
  434. //---------------------------------------------------------------------------
  435. void __fastcall TSynchronizeDialog::ClearLog()
  436. {
  437. // TListItems::Clear() does nothing without allocated handle
  438. LogView->HandleNeeded();
  439. LogView->Items->Clear();
  440. }
  441. //---------------------------------------------------------------------------
  442. void __fastcall TSynchronizeDialog::CopyLog()
  443. {
  444. AnsiString Content;
  445. for (int i = 0; i < LogView->Items->Count; i++)
  446. {
  447. TListItem * Item = LogView->Items->Item[i];
  448. Content += Item->Caption + "\t" + Item->SubItems->Strings[0] + "\r\n";
  449. }
  450. CopyToClipboard(Content);
  451. }
  452. //---------------------------------------------------------------------------
  453. void __fastcall TSynchronizeDialog::LogViewKeyDown(TObject * /*Sender*/,
  454. WORD & Key, TShiftState Shift)
  455. {
  456. if (Key == VK_DELETE)
  457. {
  458. ClearLog();
  459. Key = 0;
  460. }
  461. else if ((Key == 'C') && Shift.Contains(ssCtrl) && (LogView->Items->Count > 0))
  462. {
  463. CopyLog();
  464. Key = 0;
  465. }
  466. }
  467. //---------------------------------------------------------------------------
  468. void __fastcall TSynchronizeDialog::FormKeyDown(TObject * /*Sender*/, WORD & Key,
  469. TShiftState /*Shift*/)
  470. {
  471. if ((Key == VK_ESCAPE) && FSynchronizing)
  472. {
  473. Stop();
  474. Key = 0;
  475. }
  476. }
  477. //---------------------------------------------------------------------------