FullSynchronize.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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. FSynchronizeBySizeCaption = SynchronizeBySizeCheck->Caption;
  67. HotTrackLabel(CopyParamLabel);
  68. CopyParamListButton(TransferSettingsButton);
  69. }
  70. //---------------------------------------------------------------------------
  71. __fastcall TFullSynchronizeDialog::~TFullSynchronizeDialog()
  72. {
  73. delete FPresetsMenu;
  74. }
  75. //---------------------------------------------------------------------------
  76. void __fastcall TFullSynchronizeDialog::UpdateControls()
  77. {
  78. EnableControl(SynchronizeTimestampsButton, FLAGCLEAR(Options, fsoDisableTimestamp));
  79. if (SynchronizeTimestampsButton->Checked)
  80. {
  81. SynchronizeExistingOnlyCheck->Checked = true;
  82. SynchronizeDeleteCheck->Checked = false;
  83. SynchronizeByTimeCheck->Checked = true;
  84. }
  85. if (SynchronizeBothButton->Checked)
  86. {
  87. SynchronizeByTimeCheck->Checked = true;
  88. SynchronizeBySizeCheck->Checked = false;
  89. if (MirrorFilesButton->Checked)
  90. {
  91. SynchronizeFilesButton->Checked = true;
  92. }
  93. }
  94. EnableControl(MirrorFilesButton, !SynchronizeBothButton->Checked);
  95. EnableControl(SynchronizeDeleteCheck, !SynchronizeBothButton->Checked &&
  96. !SynchronizeTimestampsButton->Checked);
  97. EnableControl(SynchronizeExistingOnlyCheck, !SynchronizeTimestampsButton->Checked);
  98. EnableControl(SynchronizeByTimeCheck, !SynchronizeBothButton->Checked &&
  99. !SynchronizeTimestampsButton->Checked);
  100. EnableControl(SynchronizeBySizeCheck, !SynchronizeBothButton->Checked);
  101. EnableControl(SynchronizeSelectedOnlyCheck, FLAGSET(FOptions, fsoAllowSelectedOnly));
  102. EnableControl(OkButton, !LocalDirectoryEdit->Text.IsEmpty() &&
  103. !RemoteDirectoryEdit->Text.IsEmpty());
  104. UnicodeString InfoStr = FCopyParams.GetInfoStr(L"; ", ActualCopyParamAttrs());
  105. CopyParamLabel->Caption = InfoStr;
  106. CopyParamLabel->Hint = InfoStr;
  107. CopyParamLabel->ShowHint =
  108. (CopyParamLabel->Canvas->TextWidth(InfoStr) > (CopyParamLabel->Width * 3 / 2));
  109. SynchronizeBySizeCheck->Caption = SynchronizeTimestampsButton->Checked ?
  110. LoadStr(SYNCHRONIZE_SAME_SIZE) : UnicodeString(FSynchronizeBySizeCaption);
  111. TransferSettingsButton->Style =
  112. FLAGCLEAR(Options, fsoDoNotUsePresets) ?
  113. TCustomButton::bsSplitButton : TCustomButton::bsPushButton;
  114. }
  115. //---------------------------------------------------------------------------
  116. int __fastcall TFullSynchronizeDialog::ActualCopyParamAttrs()
  117. {
  118. int Result;
  119. if (SynchronizeTimestampsButton->Checked)
  120. {
  121. Result = cpaIncludeMaskOnly;
  122. }
  123. else
  124. {
  125. switch (Mode)
  126. {
  127. case smRemote:
  128. Result = CopyParamAttrs.Upload;
  129. break;
  130. case smLocal:
  131. Result = CopyParamAttrs.Download;
  132. break;
  133. default:
  134. assert(false);
  135. //fallthru
  136. case smBoth:
  137. Result = CopyParamAttrs.General;
  138. break;
  139. }
  140. }
  141. return
  142. Result |
  143. FLAGMASK(SynchronizeByTimeCheck->Checked, cpaNoPreserveTime) |
  144. cpaNoNewerOnly;
  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,
  305. cplCustomize | AdditionalOptions,
  306. ActualCopyParamAttrs());
  307. }
  308. //---------------------------------------------------------------------------
  309. void __fastcall TFullSynchronizeDialog::TransferSettingsButtonClick(
  310. TObject * /*Sender*/)
  311. {
  312. if (FLAGCLEAR(FOptions, fsoDoNotUsePresets) && !SupportsSplitButton())
  313. {
  314. CopyParamListPopup(
  315. TransferSettingsButton->ClientToScreen(TPoint(0, TransferSettingsButton->Height)),
  316. 0);
  317. }
  318. else
  319. {
  320. CopyParamGroupClick(NULL);
  321. }
  322. }
  323. //---------------------------------------------------------------------------
  324. void __fastcall TFullSynchronizeDialog::CopyParamClick(TObject * Sender)
  325. {
  326. assert(FLAGCLEAR(FOptions, fsoDoNotUsePresets));
  327. // PreserveTime is forced for some settings, but avoid hard-setting it until
  328. // user really confirms it on custom dialog
  329. TCopyParamType ACopyParams = CopyParams;
  330. if (CopyParamListPopupClick(Sender, ACopyParams, FPreset,
  331. ActualCopyParamAttrs()))
  332. {
  333. FCopyParams = ACopyParams;
  334. UpdateControls();
  335. }
  336. }
  337. //---------------------------------------------------------------------------
  338. void __fastcall TFullSynchronizeDialog::FormShow(TObject * /*Sender*/)
  339. {
  340. InstallPathWordBreakProc(LocalDirectoryEdit);
  341. InstallPathWordBreakProc(RemoteDirectoryEdit);
  342. UpdateControls();
  343. }
  344. //---------------------------------------------------------------------------
  345. void __fastcall TFullSynchronizeDialog::FormCloseQuery(TObject * /*Sender*/,
  346. bool & CanClose)
  347. {
  348. if ((ModalResult != mrCancel) &&
  349. SaveSettings && (FOrigMode != Mode) && !FSaveMode)
  350. {
  351. switch (MessageDialog(LoadStr(SAVE_SYNCHRONIZE_MODE),
  352. qtConfirmation, qaYes | qaNo | qaCancel, HELP_SYNCHRONIZE_SAVE_MODE))
  353. {
  354. case qaYes:
  355. FSaveMode = true;
  356. break;
  357. case qaCancel:
  358. CanClose = false;
  359. break;
  360. }
  361. }
  362. }
  363. //---------------------------------------------------------------------------
  364. TCopyParamType __fastcall TFullSynchronizeDialog::GetCopyParams()
  365. {
  366. TCopyParamType Result = FCopyParams;
  367. // when synchronizing by time, we force preserving time,
  368. // otherwise it does not make any sense
  369. if (FLAGCLEAR(Params, spNotByTime))
  370. {
  371. Result.PreserveTime = true;
  372. }
  373. Result.NewerOnly = false;
  374. return Result;
  375. }
  376. //---------------------------------------------------------------------------
  377. void __fastcall TFullSynchronizeDialog::SetCopyParams(const TCopyParamType & value)
  378. {
  379. FCopyParams = value;
  380. UpdateControls();
  381. }
  382. //---------------------------------------------------------------------------
  383. void __fastcall TFullSynchronizeDialog::CopyParamGroupContextPopup(
  384. TObject * /*Sender*/, TPoint & MousePos, bool & Handled)
  385. {
  386. if (FLAGCLEAR(FOptions, fsoDoNotUsePresets))
  387. {
  388. CopyParamListPopup(CopyParamGroup->ClientToScreen(MousePos), cplCustomizeDefault);
  389. Handled = true;
  390. }
  391. }
  392. //---------------------------------------------------------------------------
  393. void __fastcall TFullSynchronizeDialog::CopyParamGroupClick(
  394. TObject * /*Sender*/)
  395. {
  396. // PreserveTime is forced for some settings, but avoid hard-setting it until
  397. // user really confirms it on custom dialog
  398. TCopyParamType ACopyParams = CopyParams;
  399. if (DoCopyParamCustomDialog(ACopyParams, ActualCopyParamAttrs()))
  400. {
  401. FCopyParams = ACopyParams;
  402. UpdateControls();
  403. }
  404. }
  405. //---------------------------------------------------------------------------
  406. void __fastcall TFullSynchronizeDialog::HelpButtonClick(TObject * /*Sender*/)
  407. {
  408. FormHelp(this);
  409. }
  410. //---------------------------------------------------------------------------
  411. void __fastcall TFullSynchronizeDialog::TransferSettingsButtonDropDownClick(TObject * /*Sender*/)
  412. {
  413. CopyParamListPopup(
  414. TransferSettingsButton->ClientToScreen(TPoint(0, TransferSettingsButton->Height)),
  415. cplCustomizeDefault);
  416. }
  417. //---------------------------------------------------------------------------