FullSynchronize.cpp 15 KB

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