FullSynchronize.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include <Common.h>
  5. #include "WinInterface.h"
  6. #include "FullSynchronize.h"
  7. #include "CopyParams.h"
  8. #include "VCLCommon.h"
  9. #include <CoreMain.h>
  10. #include <Configuration.h>
  11. #include <TextsWin.h>
  12. #include <HelpWin.h>
  13. #include <GUITools.h>
  14. #include <CustomWinConfiguration.h>
  15. //---------------------------------------------------------------------------
  16. #pragma package(smart_init)
  17. #pragma link "HistoryComboBox"
  18. #ifndef NO_RESOURCES
  19. #pragma resource "*.dfm"
  20. #endif
  21. //---------------------------------------------------------------------------
  22. bool __fastcall DoFullSynchronizeDialog(TSynchronizeMode & Mode, int & Params,
  23. UnicodeString & LocalDirectory, UnicodeString & RemoteDirectory,
  24. TCopyParamType * CopyParams, bool & SaveSettings, bool & SaveMode, int Options,
  25. const TUsableCopyParamAttrs & CopyParamAttrs)
  26. {
  27. bool Result;
  28. TFullSynchronizeDialog * Dialog = SafeFormCreate<TFullSynchronizeDialog>();
  29. try
  30. {
  31. Dialog->Mode = Mode;
  32. Dialog->Options = Options;
  33. Dialog->Params = Params;
  34. Dialog->LocalDirectory = LocalDirectory;
  35. Dialog->RemoteDirectory = RemoteDirectory;
  36. Dialog->CopyParams = *CopyParams;
  37. Dialog->SaveSettings = SaveSettings;
  38. Dialog->SaveMode = SaveMode;
  39. Dialog->CopyParamAttrs = CopyParamAttrs;
  40. Result = Dialog->Execute();
  41. if (Result)
  42. {
  43. Mode = Dialog->Mode;
  44. Params = Dialog->Params;
  45. LocalDirectory = Dialog->LocalDirectory;
  46. RemoteDirectory = Dialog->RemoteDirectory;
  47. *CopyParams = Dialog->CopyParams;
  48. SaveSettings = Dialog->SaveSettings;
  49. SaveMode = Dialog->SaveMode;
  50. }
  51. }
  52. __finally
  53. {
  54. delete Dialog;
  55. }
  56. return Result;
  57. }
  58. //---------------------------------------------------------------------------
  59. __fastcall TFullSynchronizeDialog::TFullSynchronizeDialog(TComponent* Owner)
  60. : TForm(Owner)
  61. {
  62. UseSystemSettings(this);
  63. FParams = 0;
  64. FSaveMode = false;
  65. FOptions = 0;
  66. FPresetsMenu = new TPopupMenu(this);
  67. FSynchronizeBySizeCaption = SynchronizeBySizeCheck->Caption;
  68. HotTrackLabel(CopyParamLabel);
  69. CopyParamListButton(TransferSettingsButton);
  70. LoadDialogImage(Image, L"Synchronize directories");
  71. }
  72. //---------------------------------------------------------------------------
  73. __fastcall TFullSynchronizeDialog::~TFullSynchronizeDialog()
  74. {
  75. delete FPresetsMenu;
  76. }
  77. //---------------------------------------------------------------------------
  78. void __fastcall TFullSynchronizeDialog::UpdateControls()
  79. {
  80. EnableControl(SynchronizeTimestampsButton, FLAGCLEAR(Options, fsoDisableTimestamp));
  81. if (SynchronizeTimestampsButton->Checked)
  82. {
  83. SynchronizeExistingOnlyCheck->Checked = true;
  84. SynchronizeDeleteCheck->Checked = false;
  85. SynchronizeByTimeCheck->Checked = true;
  86. }
  87. if (SynchronizeBothButton->Checked)
  88. {
  89. SynchronizeByTimeCheck->Checked = true;
  90. SynchronizeBySizeCheck->Checked = false;
  91. if (MirrorFilesButton->Checked)
  92. {
  93. SynchronizeFilesButton->Checked = true;
  94. }
  95. }
  96. EnableControl(MirrorFilesButton, !SynchronizeBothButton->Checked);
  97. EnableControl(SynchronizeDeleteCheck, !SynchronizeBothButton->Checked &&
  98. !SynchronizeTimestampsButton->Checked);
  99. EnableControl(SynchronizeExistingOnlyCheck, !SynchronizeTimestampsButton->Checked);
  100. EnableControl(SynchronizeByTimeCheck, !SynchronizeBothButton->Checked &&
  101. !SynchronizeTimestampsButton->Checked);
  102. EnableControl(SynchronizeBySizeCheck, !SynchronizeBothButton->Checked);
  103. EnableControl(SynchronizeSelectedOnlyCheck, FLAGSET(FOptions, fsoAllowSelectedOnly));
  104. EnableControl(OkButton, !LocalDirectoryEdit->Text.IsEmpty() &&
  105. !RemoteDirectoryEdit->Text.IsEmpty());
  106. UnicodeString InfoStr = FCopyParams.GetInfoStr(L"; ", ActualCopyParamAttrs());
  107. SetLabelHintPopup(CopyParamLabel, InfoStr);
  108. SynchronizeBySizeCheck->Caption = SynchronizeTimestampsButton->Checked ?
  109. LoadStr(SYNCHRONIZE_SAME_SIZE) : UnicodeString(FSynchronizeBySizeCaption);
  110. TransferSettingsButton->Style =
  111. FLAGCLEAR(Options, fsoDoNotUsePresets) ?
  112. TCustomButton::bsSplitButton : TCustomButton::bsPushButton;
  113. }
  114. //---------------------------------------------------------------------------
  115. int __fastcall TFullSynchronizeDialog::ActualCopyParamAttrs()
  116. {
  117. int Result;
  118. if (SynchronizeTimestampsButton->Checked)
  119. {
  120. Result = cpaIncludeMaskOnly;
  121. }
  122. else
  123. {
  124. switch (Mode)
  125. {
  126. case smRemote:
  127. Result = CopyParamAttrs.Upload;
  128. break;
  129. case smLocal:
  130. Result = CopyParamAttrs.Download;
  131. break;
  132. default:
  133. DebugFail();
  134. //fallthru
  135. case smBoth:
  136. Result = CopyParamAttrs.General;
  137. break;
  138. }
  139. }
  140. return
  141. Result |
  142. FLAGMASK(SynchronizeByTimeCheck->Checked, cpaNoPreserveTime) |
  143. cpaNoNewerOnly;
  144. }
  145. //---------------------------------------------------------------------------
  146. void __fastcall TFullSynchronizeDialog::ControlChange(TObject * /*Sender*/)
  147. {
  148. UpdateControls();
  149. }
  150. //---------------------------------------------------------------------------
  151. bool __fastcall TFullSynchronizeDialog::Execute()
  152. {
  153. // at start assume that copy param is current preset
  154. FPreset = GUIConfiguration->CopyParamCurrent;
  155. LocalDirectoryEdit->Items = CustomWinConfiguration->History[L"LocalDirectory"];
  156. RemoteDirectoryEdit->Items = CustomWinConfiguration->History[L"RemoteDirectory"];
  157. bool Result = (ShowModal() == DefaultResult(this));
  158. if (Result)
  159. {
  160. LocalDirectoryEdit->SaveToHistory();
  161. CustomWinConfiguration->History[L"LocalDirectory"] = LocalDirectoryEdit->Items;
  162. RemoteDirectoryEdit->SaveToHistory();
  163. CustomWinConfiguration->History[L"RemoteDirectory"] = RemoteDirectoryEdit->Items;
  164. }
  165. return Result;
  166. }
  167. //---------------------------------------------------------------------------
  168. void __fastcall TFullSynchronizeDialog::SetRemoteDirectory(const UnicodeString value)
  169. {
  170. RemoteDirectoryEdit->Text = value;
  171. }
  172. //---------------------------------------------------------------------------
  173. UnicodeString __fastcall TFullSynchronizeDialog::GetRemoteDirectory()
  174. {
  175. return RemoteDirectoryEdit->Text;
  176. }
  177. //---------------------------------------------------------------------------
  178. void __fastcall TFullSynchronizeDialog::SetLocalDirectory(const UnicodeString value)
  179. {
  180. LocalDirectoryEdit->Text = value;
  181. }
  182. //---------------------------------------------------------------------------
  183. UnicodeString __fastcall TFullSynchronizeDialog::GetLocalDirectory()
  184. {
  185. return LocalDirectoryEdit->Text;
  186. }
  187. //---------------------------------------------------------------------------
  188. void __fastcall TFullSynchronizeDialog::SetMode(TSynchronizeMode value)
  189. {
  190. FOrigMode = value;
  191. switch (value)
  192. {
  193. case smRemote:
  194. SynchronizeRemoteButton->Checked = true;
  195. break;
  196. case smLocal:
  197. SynchronizeLocalButton->Checked = true;
  198. break;
  199. case smBoth:
  200. SynchronizeBothButton->Checked = true;
  201. break;
  202. default:
  203. DebugFail();
  204. }
  205. }
  206. //---------------------------------------------------------------------------
  207. TSynchronizeMode __fastcall TFullSynchronizeDialog::GetMode()
  208. {
  209. if (SynchronizeRemoteButton->Checked)
  210. {
  211. return smRemote;
  212. }
  213. else if (SynchronizeLocalButton->Checked)
  214. {
  215. return smLocal;
  216. }
  217. else
  218. {
  219. DebugAssert(SynchronizeBothButton->Checked);
  220. return smBoth;
  221. }
  222. }
  223. //---------------------------------------------------------------------------
  224. void __fastcall TFullSynchronizeDialog::SetParams(int value)
  225. {
  226. FParams = value & ~(spDelete | spExistingOnly |
  227. spPreviewChanges | spTimestamp | spNotByTime | spBySize | spSelectedOnly | spMirror);
  228. SynchronizeDeleteCheck->Checked = FLAGSET(value, spDelete);
  229. SynchronizeExistingOnlyCheck->Checked = FLAGSET(value, spExistingOnly);
  230. SynchronizePreviewChangesCheck->Checked = FLAGSET(value, spPreviewChanges);
  231. SynchronizeSelectedOnlyCheck->Checked = FLAGSET(value, spSelectedOnly);
  232. if (FLAGSET(value, spTimestamp) && FLAGCLEAR(Options, fsoDisableTimestamp))
  233. {
  234. SynchronizeTimestampsButton->Checked = true;
  235. }
  236. else if (FLAGSET(value, spMirror))
  237. {
  238. MirrorFilesButton->Checked = true;
  239. }
  240. else
  241. {
  242. SynchronizeFilesButton->Checked = true;
  243. }
  244. SynchronizeByTimeCheck->Checked = FLAGCLEAR(value, spNotByTime);
  245. SynchronizeBySizeCheck->Checked = FLAGSET(value, spBySize);
  246. UpdateControls();
  247. }
  248. //---------------------------------------------------------------------------
  249. int __fastcall TFullSynchronizeDialog::GetParams()
  250. {
  251. return FParams |
  252. FLAGMASK(SynchronizeDeleteCheck->Checked, spDelete) |
  253. FLAGMASK(SynchronizeExistingOnlyCheck->Checked, spExistingOnly) |
  254. FLAGMASK(SynchronizePreviewChangesCheck->Checked, spPreviewChanges) |
  255. FLAGMASK(SynchronizeSelectedOnlyCheck->Checked, spSelectedOnly) |
  256. FLAGMASK(SynchronizeTimestampsButton->Checked && FLAGCLEAR(Options, fsoDisableTimestamp),
  257. spTimestamp) |
  258. FLAGMASK(MirrorFilesButton->Checked, spMirror) |
  259. FLAGMASK(!SynchronizeByTimeCheck->Checked, spNotByTime) |
  260. FLAGMASK(SynchronizeBySizeCheck->Checked, spBySize);
  261. }
  262. //---------------------------------------------------------------------------
  263. void __fastcall TFullSynchronizeDialog::LocalDirectoryBrowseButtonClick(
  264. TObject * /*Sender*/)
  265. {
  266. UnicodeString Directory = LocalDirectoryEdit->Text;
  267. if (SelectDirectory(Directory, LoadStr(SELECT_LOCAL_DIRECTORY), false))
  268. {
  269. LocalDirectoryEdit->Text = Directory;
  270. }
  271. }
  272. //---------------------------------------------------------------------------
  273. void __fastcall TFullSynchronizeDialog::SetSaveSettings(bool value)
  274. {
  275. SaveSettingsCheck->Checked = value;
  276. }
  277. //---------------------------------------------------------------------------
  278. bool __fastcall TFullSynchronizeDialog::GetSaveSettings()
  279. {
  280. return SaveSettingsCheck->Checked;
  281. }
  282. //---------------------------------------------------------------------------
  283. void __fastcall TFullSynchronizeDialog::SetOptions(int value)
  284. {
  285. if (Options != value)
  286. {
  287. FOptions = value;
  288. if (FLAGSET(Options, fsoDisableTimestamp) &&
  289. SynchronizeTimestampsButton->Checked)
  290. {
  291. SynchronizeFilesButton->Checked = true;
  292. }
  293. UpdateControls();
  294. }
  295. }
  296. //---------------------------------------------------------------------------
  297. void __fastcall TFullSynchronizeDialog::CopyParamListPopup(TRect R, int AdditionalOptions)
  298. {
  299. // We pass in FCopyParams, although it may not be the exact copy param
  300. // that will be used (because of Preservetime). The reason is to
  301. // display checkbox next to user-selected preset
  302. ::CopyParamListPopup(
  303. R, FPresetsMenu, FCopyParams, FPreset, CopyParamClick,
  304. cplCustomize | AdditionalOptions,
  305. ActualCopyParamAttrs());
  306. }
  307. //---------------------------------------------------------------------------
  308. void __fastcall TFullSynchronizeDialog::TransferSettingsButtonClick(
  309. TObject * /*Sender*/)
  310. {
  311. if (FLAGCLEAR(FOptions, fsoDoNotUsePresets) && !SupportsSplitButton())
  312. {
  313. CopyParamListPopup(CalculatePopupRect(TransferSettingsButton), 0);
  314. }
  315. else
  316. {
  317. CopyParamGroupClick(NULL);
  318. }
  319. }
  320. //---------------------------------------------------------------------------
  321. void __fastcall TFullSynchronizeDialog::CopyParamClick(TObject * Sender)
  322. {
  323. DebugAssert(FLAGCLEAR(FOptions, fsoDoNotUsePresets));
  324. // PreserveTime is forced for some settings, but avoid hard-setting it until
  325. // user really confirms it on custom dialog
  326. TCopyParamType ACopyParams = CopyParams;
  327. if (CopyParamListPopupClick(Sender, ACopyParams, FPreset,
  328. ActualCopyParamAttrs()) > 0)
  329. {
  330. FCopyParams = ACopyParams;
  331. UpdateControls();
  332. }
  333. }
  334. //---------------------------------------------------------------------------
  335. void __fastcall TFullSynchronizeDialog::FormShow(TObject * /*Sender*/)
  336. {
  337. InstallPathWordBreakProc(LocalDirectoryEdit);
  338. InstallPathWordBreakProc(RemoteDirectoryEdit);
  339. UpdateControls();
  340. }
  341. //---------------------------------------------------------------------------
  342. void __fastcall TFullSynchronizeDialog::FormCloseQuery(TObject * /*Sender*/,
  343. bool & CanClose)
  344. {
  345. if ((ModalResult == DefaultResult(this)) &&
  346. SaveSettings && (FOrigMode != Mode) && !FSaveMode)
  347. {
  348. switch (MessageDialog(LoadStr(SAVE_SYNCHRONIZE_MODE2),
  349. qtConfirmation, qaYes | qaNo | qaCancel, HELP_SYNCHRONIZE_SAVE_MODE))
  350. {
  351. case qaYes:
  352. FSaveMode = true;
  353. break;
  354. case qaCancel:
  355. CanClose = false;
  356. break;
  357. }
  358. }
  359. }
  360. //---------------------------------------------------------------------------
  361. TCopyParamType __fastcall TFullSynchronizeDialog::GetCopyParams()
  362. {
  363. TCopyParamType Result = FCopyParams;
  364. // when synchronizing by time, we force preserving time,
  365. // otherwise it does not make any sense
  366. if (FLAGCLEAR(Params, spNotByTime))
  367. {
  368. Result.PreserveTime = true;
  369. }
  370. Result.NewerOnly = false;
  371. return Result;
  372. }
  373. //---------------------------------------------------------------------------
  374. void __fastcall TFullSynchronizeDialog::SetCopyParams(const TCopyParamType & value)
  375. {
  376. FCopyParams = value;
  377. UpdateControls();
  378. }
  379. //---------------------------------------------------------------------------
  380. void __fastcall TFullSynchronizeDialog::CopyParamGroupContextPopup(
  381. TObject * /*Sender*/, TPoint & MousePos, bool & Handled)
  382. {
  383. if (FLAGCLEAR(FOptions, fsoDoNotUsePresets))
  384. {
  385. CopyParamListPopup(CalculatePopupRect(CopyParamGroup, MousePos), cplCustomizeDefault);
  386. Handled = true;
  387. }
  388. }
  389. //---------------------------------------------------------------------------
  390. void __fastcall TFullSynchronizeDialog::CopyParamGroupClick(
  391. TObject * /*Sender*/)
  392. {
  393. // PreserveTime is forced for some settings, but avoid hard-setting it until
  394. // user really confirms it on custom dialog
  395. TCopyParamType ACopyParams = CopyParams;
  396. if (DoCopyParamCustomDialog(ACopyParams, ActualCopyParamAttrs()))
  397. {
  398. FCopyParams = ACopyParams;
  399. UpdateControls();
  400. }
  401. }
  402. //---------------------------------------------------------------------------
  403. void __fastcall TFullSynchronizeDialog::HelpButtonClick(TObject * /*Sender*/)
  404. {
  405. FormHelp(this);
  406. }
  407. //---------------------------------------------------------------------------
  408. void __fastcall TFullSynchronizeDialog::TransferSettingsButtonDropDownClick(TObject * /*Sender*/)
  409. {
  410. CopyParamListPopup(CalculatePopupRect(TransferSettingsButton), cplCustomizeDefault);
  411. }
  412. //---------------------------------------------------------------------------