FullSynchronize.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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. CopyParamLabel->Caption = InfoStr;
  108. CopyParamLabel->Hint = InfoStr;
  109. CopyParamLabel->ShowHint =
  110. (CopyParamLabel->Canvas->TextWidth(InfoStr) > (CopyParamLabel->Width * 3 / 2));
  111. SynchronizeBySizeCheck->Caption = SynchronizeTimestampsButton->Checked ?
  112. LoadStr(SYNCHRONIZE_SAME_SIZE) : UnicodeString(FSynchronizeBySizeCaption);
  113. TransferSettingsButton->Style =
  114. FLAGCLEAR(Options, fsoDoNotUsePresets) ?
  115. TCustomButton::bsSplitButton : TCustomButton::bsPushButton;
  116. }
  117. //---------------------------------------------------------------------------
  118. int __fastcall TFullSynchronizeDialog::ActualCopyParamAttrs()
  119. {
  120. int Result;
  121. if (SynchronizeTimestampsButton->Checked)
  122. {
  123. Result = cpaIncludeMaskOnly;
  124. }
  125. else
  126. {
  127. switch (Mode)
  128. {
  129. case smRemote:
  130. Result = CopyParamAttrs.Upload;
  131. break;
  132. case smLocal:
  133. Result = CopyParamAttrs.Download;
  134. break;
  135. default:
  136. DebugFail();
  137. //fallthru
  138. case smBoth:
  139. Result = CopyParamAttrs.General;
  140. break;
  141. }
  142. }
  143. return
  144. Result |
  145. FLAGMASK(SynchronizeByTimeCheck->Checked, cpaNoPreserveTime) |
  146. cpaNoNewerOnly;
  147. }
  148. //---------------------------------------------------------------------------
  149. void __fastcall TFullSynchronizeDialog::ControlChange(TObject * /*Sender*/)
  150. {
  151. UpdateControls();
  152. }
  153. //---------------------------------------------------------------------------
  154. bool __fastcall TFullSynchronizeDialog::Execute()
  155. {
  156. // at start assume that copy param is current preset
  157. FPreset = GUIConfiguration->CopyParamCurrent;
  158. LocalDirectoryEdit->Items = CustomWinConfiguration->History[L"LocalDirectory"];
  159. RemoteDirectoryEdit->Items = CustomWinConfiguration->History[L"RemoteDirectory"];
  160. bool Result = (ShowModal() == DefaultResult(this));
  161. if (Result)
  162. {
  163. LocalDirectoryEdit->SaveToHistory();
  164. CustomWinConfiguration->History[L"LocalDirectory"] = LocalDirectoryEdit->Items;
  165. RemoteDirectoryEdit->SaveToHistory();
  166. CustomWinConfiguration->History[L"RemoteDirectory"] = RemoteDirectoryEdit->Items;
  167. }
  168. return Result;
  169. }
  170. //---------------------------------------------------------------------------
  171. void __fastcall TFullSynchronizeDialog::SetRemoteDirectory(const UnicodeString value)
  172. {
  173. RemoteDirectoryEdit->Text = value;
  174. }
  175. //---------------------------------------------------------------------------
  176. UnicodeString __fastcall TFullSynchronizeDialog::GetRemoteDirectory()
  177. {
  178. return RemoteDirectoryEdit->Text;
  179. }
  180. //---------------------------------------------------------------------------
  181. void __fastcall TFullSynchronizeDialog::SetLocalDirectory(const UnicodeString value)
  182. {
  183. LocalDirectoryEdit->Text = value;
  184. }
  185. //---------------------------------------------------------------------------
  186. UnicodeString __fastcall TFullSynchronizeDialog::GetLocalDirectory()
  187. {
  188. return LocalDirectoryEdit->Text;
  189. }
  190. //---------------------------------------------------------------------------
  191. void __fastcall TFullSynchronizeDialog::SetMode(TSynchronizeMode value)
  192. {
  193. FOrigMode = value;
  194. switch (value)
  195. {
  196. case smRemote:
  197. SynchronizeRemoteButton->Checked = true;
  198. break;
  199. case smLocal:
  200. SynchronizeLocalButton->Checked = true;
  201. break;
  202. case smBoth:
  203. SynchronizeBothButton->Checked = true;
  204. break;
  205. default:
  206. DebugFail();
  207. }
  208. }
  209. //---------------------------------------------------------------------------
  210. TSynchronizeMode __fastcall TFullSynchronizeDialog::GetMode()
  211. {
  212. if (SynchronizeRemoteButton->Checked)
  213. {
  214. return smRemote;
  215. }
  216. else if (SynchronizeLocalButton->Checked)
  217. {
  218. return smLocal;
  219. }
  220. else
  221. {
  222. DebugAssert(SynchronizeBothButton->Checked);
  223. return smBoth;
  224. }
  225. }
  226. //---------------------------------------------------------------------------
  227. void __fastcall TFullSynchronizeDialog::SetParams(int value)
  228. {
  229. FParams = value & ~(spDelete | spExistingOnly |
  230. spPreviewChanges | spTimestamp | spNotByTime | spBySize | spSelectedOnly | spMirror);
  231. SynchronizeDeleteCheck->Checked = FLAGSET(value, spDelete);
  232. SynchronizeExistingOnlyCheck->Checked = FLAGSET(value, spExistingOnly);
  233. SynchronizePreviewChangesCheck->Checked = FLAGSET(value, spPreviewChanges);
  234. SynchronizeSelectedOnlyCheck->Checked = FLAGSET(value, spSelectedOnly);
  235. if (FLAGSET(value, spTimestamp) && FLAGCLEAR(Options, fsoDisableTimestamp))
  236. {
  237. SynchronizeTimestampsButton->Checked = true;
  238. }
  239. else if (FLAGSET(value, spMirror))
  240. {
  241. MirrorFilesButton->Checked = true;
  242. }
  243. else
  244. {
  245. SynchronizeFilesButton->Checked = true;
  246. }
  247. SynchronizeByTimeCheck->Checked = FLAGCLEAR(value, spNotByTime);
  248. SynchronizeBySizeCheck->Checked = FLAGSET(value, spBySize);
  249. UpdateControls();
  250. }
  251. //---------------------------------------------------------------------------
  252. int __fastcall TFullSynchronizeDialog::GetParams()
  253. {
  254. return FParams |
  255. FLAGMASK(SynchronizeDeleteCheck->Checked, spDelete) |
  256. FLAGMASK(SynchronizeExistingOnlyCheck->Checked, spExistingOnly) |
  257. FLAGMASK(SynchronizePreviewChangesCheck->Checked, spPreviewChanges) |
  258. FLAGMASK(SynchronizeSelectedOnlyCheck->Checked, spSelectedOnly) |
  259. FLAGMASK(SynchronizeTimestampsButton->Checked && FLAGCLEAR(Options, fsoDisableTimestamp),
  260. spTimestamp) |
  261. FLAGMASK(MirrorFilesButton->Checked, spMirror) |
  262. FLAGMASK(!SynchronizeByTimeCheck->Checked, spNotByTime) |
  263. FLAGMASK(SynchronizeBySizeCheck->Checked, spBySize);
  264. }
  265. //---------------------------------------------------------------------------
  266. void __fastcall TFullSynchronizeDialog::LocalDirectoryBrowseButtonClick(
  267. TObject * /*Sender*/)
  268. {
  269. UnicodeString Directory = LocalDirectoryEdit->Text;
  270. if (SelectDirectory(Directory, LoadStr(SELECT_LOCAL_DIRECTORY), false))
  271. {
  272. LocalDirectoryEdit->Text = Directory;
  273. }
  274. }
  275. //---------------------------------------------------------------------------
  276. void __fastcall TFullSynchronizeDialog::SetSaveSettings(bool value)
  277. {
  278. SaveSettingsCheck->Checked = value;
  279. }
  280. //---------------------------------------------------------------------------
  281. bool __fastcall TFullSynchronizeDialog::GetSaveSettings()
  282. {
  283. return SaveSettingsCheck->Checked;
  284. }
  285. //---------------------------------------------------------------------------
  286. void __fastcall TFullSynchronizeDialog::SetOptions(int value)
  287. {
  288. if (Options != value)
  289. {
  290. FOptions = value;
  291. if (FLAGSET(Options, fsoDisableTimestamp) &&
  292. SynchronizeTimestampsButton->Checked)
  293. {
  294. SynchronizeFilesButton->Checked = true;
  295. }
  296. UpdateControls();
  297. }
  298. }
  299. //---------------------------------------------------------------------------
  300. void __fastcall TFullSynchronizeDialog::CopyParamListPopup(TRect R, int AdditionalOptions)
  301. {
  302. // We pass in FCopyParams, although it may not be the exact copy param
  303. // that will be used (because of Preservetime). The reason is to
  304. // display checkbox next to user-selected preset
  305. ::CopyParamListPopup(
  306. R, FPresetsMenu, FCopyParams, FPreset, CopyParamClick,
  307. cplCustomize | AdditionalOptions,
  308. ActualCopyParamAttrs());
  309. }
  310. //---------------------------------------------------------------------------
  311. void __fastcall TFullSynchronizeDialog::TransferSettingsButtonClick(
  312. TObject * /*Sender*/)
  313. {
  314. if (FLAGCLEAR(FOptions, fsoDoNotUsePresets) && !SupportsSplitButton())
  315. {
  316. CopyParamListPopup(CalculatePopupRect(TransferSettingsButton), 0);
  317. }
  318. else
  319. {
  320. CopyParamGroupClick(NULL);
  321. }
  322. }
  323. //---------------------------------------------------------------------------
  324. void __fastcall TFullSynchronizeDialog::CopyParamClick(TObject * Sender)
  325. {
  326. DebugAssert(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 == DefaultResult(this)) &&
  349. SaveSettings && (FOrigMode != Mode) && !FSaveMode)
  350. {
  351. switch (MessageDialog(LoadStr(SAVE_SYNCHRONIZE_MODE2),
  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(CalculatePopupRect(CopyParamGroup, 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(CalculatePopupRect(TransferSettingsButton), cplCustomizeDefault);
  414. }
  415. //---------------------------------------------------------------------------