GUIConfiguration.cpp 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include <LanguagesDEPfix.hpp>
  5. #include "GUIConfiguration.h"
  6. #include "GUITools.h"
  7. #include <Common.h>
  8. #include <FileInfo.h>
  9. #include <TextsCore.h>
  10. #include <Terminal.h>
  11. #include <CoreMain.h>
  12. //---------------------------------------------------------------------------
  13. #pragma package(smart_init)
  14. //---------------------------------------------------------------------------
  15. const int ccLocal = ccUser;
  16. const int ccShowResults = ccUser << 1;
  17. const int ccCopyResults = ccUser << 2;
  18. const int ccSet = 0x80000000;
  19. //---------------------------------------------------------------------------
  20. static const unsigned int AdditionaLanguageMask = 0xFFFFFF00;
  21. static const UnicodeString AdditionaLanguagePrefix(L"XX");
  22. //---------------------------------------------------------------------------
  23. __fastcall TGUICopyParamType::TGUICopyParamType()
  24. : TCopyParamType()
  25. {
  26. GUIDefault();
  27. }
  28. //---------------------------------------------------------------------------
  29. __fastcall TGUICopyParamType::TGUICopyParamType(const TCopyParamType & Source)
  30. : TCopyParamType(Source)
  31. {
  32. GUIDefault();
  33. }
  34. //---------------------------------------------------------------------------
  35. __fastcall TGUICopyParamType::TGUICopyParamType(const TGUICopyParamType & Source)
  36. : TCopyParamType(Source)
  37. {
  38. GUIAssign(&Source);
  39. }
  40. //---------------------------------------------------------------------------
  41. void __fastcall TGUICopyParamType::Assign(const TCopyParamType * Source)
  42. {
  43. TCopyParamType::Assign(Source);
  44. const TGUICopyParamType * GUISource;
  45. GUISource = dynamic_cast<const TGUICopyParamType *>(Source);
  46. if (GUISource != NULL)
  47. {
  48. GUIAssign(GUISource);
  49. }
  50. }
  51. //---------------------------------------------------------------------------
  52. void __fastcall TGUICopyParamType::GUIAssign(const TGUICopyParamType * Source)
  53. {
  54. Queue = Source->Queue;
  55. QueueNoConfirmation = Source->QueueNoConfirmation;
  56. QueueIndividually = Source->QueueIndividually;
  57. NewerOnly = Source->NewerOnly;
  58. }
  59. //---------------------------------------------------------------------------
  60. void __fastcall TGUICopyParamType::Default()
  61. {
  62. TCopyParamType::Default();
  63. GUIDefault();
  64. }
  65. //---------------------------------------------------------------------------
  66. void __fastcall TGUICopyParamType::GUIDefault()
  67. {
  68. Queue = false;
  69. QueueNoConfirmation = true;
  70. QueueIndividually = false;
  71. NewerOnly = false;
  72. }
  73. //---------------------------------------------------------------------------
  74. void __fastcall TGUICopyParamType::Load(THierarchicalStorage * Storage)
  75. {
  76. TCopyParamType::Load(Storage);
  77. Queue = Storage->ReadBool(L"Queue", Queue);
  78. QueueNoConfirmation = Storage->ReadBool(L"QueueNoConfirmation", QueueNoConfirmation);
  79. QueueIndividually = Storage->ReadBool(L"QueueIndividually", QueueIndividually);
  80. NewerOnly = Storage->ReadBool(L"NewerOnly", NewerOnly);
  81. }
  82. //---------------------------------------------------------------------------
  83. void __fastcall TGUICopyParamType::Save(THierarchicalStorage * Storage)
  84. {
  85. TCopyParamType::Save(Storage);
  86. Storage->WriteBool(L"Queue", Queue);
  87. Storage->WriteBool(L"QueueNoConfirmation", QueueNoConfirmation);
  88. Storage->WriteBool(L"QueueIndividually", QueueIndividually);
  89. Storage->WriteBool(L"NewerOnly", NewerOnly);
  90. }
  91. //---------------------------------------------------------------------------
  92. TGUICopyParamType & __fastcall TGUICopyParamType::operator =(const TCopyParamType & rhp)
  93. {
  94. Assign(&rhp);
  95. return *this;
  96. }
  97. //---------------------------------------------------------------------------
  98. TGUICopyParamType & __fastcall TGUICopyParamType::operator =(const TGUICopyParamType & rhp)
  99. {
  100. Assign(&rhp);
  101. return *this;
  102. }
  103. //---------------------------------------------------------------------------
  104. //---------------------------------------------------------------------------
  105. void __fastcall TCopyParamRuleData::Default()
  106. {
  107. HostName = L"";
  108. UserName = L"";
  109. RemoteDirectory = L"";
  110. LocalDirectory = L"";
  111. }
  112. //---------------------------------------------------------------------------
  113. //---------------------------------------------------------------------------
  114. __fastcall TCopyParamRule::TCopyParamRule()
  115. {
  116. }
  117. //---------------------------------------------------------------------------
  118. __fastcall TCopyParamRule::TCopyParamRule(const TCopyParamRuleData & Data)
  119. {
  120. FData = Data;
  121. }
  122. //---------------------------------------------------------------------------
  123. __fastcall TCopyParamRule::TCopyParamRule(const TCopyParamRule & Source)
  124. {
  125. FData.HostName = Source.FData.HostName;
  126. FData.UserName = Source.FData.UserName;
  127. FData.RemoteDirectory = Source.FData.RemoteDirectory;
  128. FData.LocalDirectory = Source.FData.LocalDirectory;
  129. }
  130. //---------------------------------------------------------------------------
  131. #define C(Property) (Property == rhp.Property)
  132. bool __fastcall TCopyParamRule::operator==(const TCopyParamRule & rhp) const
  133. {
  134. return
  135. C(FData.HostName) &&
  136. C(FData.UserName) &&
  137. C(FData.RemoteDirectory) &&
  138. C(FData.LocalDirectory) &&
  139. true;
  140. }
  141. #undef C
  142. //---------------------------------------------------------------------------
  143. bool __fastcall TCopyParamRule::Match(const UnicodeString & Mask,
  144. const UnicodeString & Value, bool Path, bool Local) const
  145. {
  146. bool Result;
  147. if (Mask.IsEmpty())
  148. {
  149. Result = true;
  150. }
  151. else
  152. {
  153. TFileMasks M(Mask);
  154. if (Path)
  155. {
  156. Result = M.Matches(Value, Local, true);
  157. }
  158. else
  159. {
  160. Result = M.Matches(Value, false);
  161. }
  162. }
  163. return Result;
  164. }
  165. //---------------------------------------------------------------------------
  166. bool __fastcall TCopyParamRule::Matches(const TCopyParamRuleData & Value) const
  167. {
  168. return
  169. Match(FData.HostName, Value.HostName, false) &&
  170. Match(FData.UserName, Value.UserName, false) &&
  171. Match(FData.RemoteDirectory, Value.RemoteDirectory, true, false) &&
  172. Match(FData.LocalDirectory, Value.LocalDirectory, true, true);
  173. }
  174. //---------------------------------------------------------------------------
  175. void __fastcall TCopyParamRule::Load(THierarchicalStorage * Storage)
  176. {
  177. FData.HostName = Storage->ReadString(L"HostName", FData.HostName);
  178. FData.UserName = Storage->ReadString(L"UserName", FData.UserName);
  179. FData.RemoteDirectory = Storage->ReadString(L"RemoteDirectory", FData.RemoteDirectory);
  180. FData.LocalDirectory = Storage->ReadString(L"LocalDirectory", FData.LocalDirectory);
  181. }
  182. //---------------------------------------------------------------------------
  183. void __fastcall TCopyParamRule::Save(THierarchicalStorage * Storage) const
  184. {
  185. Storage->WriteString(L"HostName", FData.HostName);
  186. Storage->WriteString(L"UserName", FData.UserName);
  187. Storage->WriteString(L"RemoteDirectory", FData.RemoteDirectory);
  188. Storage->WriteString(L"LocalDirectory", FData.LocalDirectory);
  189. }
  190. //---------------------------------------------------------------------------
  191. bool __fastcall TCopyParamRule::GetEmpty() const
  192. {
  193. return
  194. FData.HostName.IsEmpty() &&
  195. FData.UserName.IsEmpty() &&
  196. FData.RemoteDirectory.IsEmpty() &&
  197. FData.LocalDirectory.IsEmpty();
  198. }
  199. //---------------------------------------------------------------------------
  200. UnicodeString __fastcall TCopyParamRule::GetInfoStr(UnicodeString Separator) const
  201. {
  202. UnicodeString Result;
  203. #define ADD(FMT, ELEM) \
  204. if (!FData.ELEM.IsEmpty()) \
  205. Result += (Result.IsEmpty() ? UnicodeString() : Separator) + FMTLOAD(FMT, (FData.ELEM));
  206. ADD(COPY_RULE_HOSTNAME, HostName);
  207. ADD(COPY_RULE_USERNAME, UserName);
  208. ADD(COPY_RULE_REMOTE_DIR, RemoteDirectory);
  209. ADD(COPY_RULE_LOCAL_DIR, LocalDirectory);
  210. #undef ADD
  211. return Result;
  212. }
  213. //---------------------------------------------------------------------------
  214. //---------------------------------------------------------------------------
  215. UnicodeString TCopyParamList::FInvalidChars(L"/\\[]");
  216. //---------------------------------------------------------------------------
  217. __fastcall TCopyParamList::TCopyParamList()
  218. {
  219. Init();
  220. }
  221. //---------------------------------------------------------------------------
  222. void __fastcall TCopyParamList::Init()
  223. {
  224. FCopyParams = new TList();
  225. FRules = new TList();
  226. FNames = new TStringList();
  227. FNameList = NULL;
  228. FModified = false;
  229. }
  230. //---------------------------------------------------------------------------
  231. __fastcall TCopyParamList::~TCopyParamList()
  232. {
  233. Clear();
  234. delete FCopyParams;
  235. delete FRules;
  236. delete FNames;
  237. delete FNameList;
  238. }
  239. //---------------------------------------------------------------------------
  240. void __fastcall TCopyParamList::Reset()
  241. {
  242. SAFE_DESTROY(FNameList);
  243. FModified = false;
  244. }
  245. //---------------------------------------------------------------------
  246. void __fastcall TCopyParamList::Modify()
  247. {
  248. SAFE_DESTROY(FNameList);
  249. FModified = true;
  250. }
  251. //---------------------------------------------------------------------
  252. void __fastcall TCopyParamList::ValidateName(const UnicodeString Name)
  253. {
  254. if (Name.LastDelimiter(FInvalidChars) > 0)
  255. {
  256. throw Exception(FMTLOAD(ITEM_NAME_INVALID, (Name, FInvalidChars)));
  257. }
  258. }
  259. //---------------------------------------------------------------------------
  260. TCopyParamList & __fastcall TCopyParamList::operator=(const TCopyParamList & rhl)
  261. {
  262. Clear();
  263. for (int Index = 0; Index < rhl.Count; Index++)
  264. {
  265. TCopyParamType * CopyParam = new TCopyParamType(*rhl.CopyParams[Index]);
  266. TCopyParamRule * Rule = NULL;
  267. if (rhl.Rules[Index] != NULL)
  268. {
  269. Rule = new TCopyParamRule(*rhl.Rules[Index]);
  270. }
  271. Add(rhl.Names[Index], CopyParam, Rule);
  272. }
  273. // there should be comparison of with the assigned list, but we rely on caller
  274. // to do it instead (TGUIConfiguration::SetCopyParamList)
  275. Modify();
  276. return *this;
  277. }
  278. //---------------------------------------------------------------------------
  279. bool __fastcall TCopyParamList::operator==(const TCopyParamList & rhl) const
  280. {
  281. bool Result = (Count == rhl.Count);
  282. if (Result)
  283. {
  284. int i = 0;
  285. while ((i < Count) && Result)
  286. {
  287. Result =
  288. (Names[i] == rhl.Names[i]) &&
  289. CompareItem(i, rhl.CopyParams[i], rhl.Rules[i]);
  290. i++;
  291. }
  292. }
  293. return Result;
  294. }
  295. //---------------------------------------------------------------------------
  296. int __fastcall TCopyParamList::IndexOfName(const UnicodeString Name) const
  297. {
  298. return FNames->IndexOf(Name);
  299. }
  300. //---------------------------------------------------------------------------
  301. bool __fastcall TCopyParamList::CompareItem(int Index,
  302. const TCopyParamType * CopyParam, const TCopyParamRule * Rule) const
  303. {
  304. return
  305. ((*CopyParams[Index]) == *CopyParam) &&
  306. ((Rules[Index] == NULL) ?
  307. (Rule == NULL) :
  308. ((Rule != NULL) && (*Rules[Index]) == (*Rule)));
  309. }
  310. //---------------------------------------------------------------------------
  311. void __fastcall TCopyParamList::Clear()
  312. {
  313. for (int i = 0; i < Count; i++)
  314. {
  315. delete CopyParams[i];
  316. delete Rules[i];
  317. }
  318. FCopyParams->Clear();
  319. FRules->Clear();
  320. FNames->Clear();
  321. }
  322. //---------------------------------------------------------------------------
  323. void __fastcall TCopyParamList::Add(const UnicodeString Name,
  324. TCopyParamType * CopyParam, TCopyParamRule * Rule)
  325. {
  326. Insert(Count, Name, CopyParam, Rule);
  327. }
  328. //---------------------------------------------------------------------------
  329. void __fastcall TCopyParamList::Insert(int Index, const UnicodeString Name,
  330. TCopyParamType * CopyParam, TCopyParamRule * Rule)
  331. {
  332. assert(FNames->IndexOf(Name) < 0);
  333. FNames->Insert(Index, Name);
  334. assert(CopyParam != NULL);
  335. FCopyParams->Insert(Index, reinterpret_cast<TObject *>(CopyParam));
  336. FRules->Insert(Index, reinterpret_cast<TObject *>(Rule));
  337. Modify();
  338. }
  339. //---------------------------------------------------------------------------
  340. void __fastcall TCopyParamList::Change(int Index, const UnicodeString Name,
  341. TCopyParamType * CopyParam, TCopyParamRule * Rule)
  342. {
  343. if ((Name != Names[Index]) || !CompareItem(Index, CopyParam, Rule))
  344. {
  345. FNames->Strings[Index] = Name;
  346. delete CopyParams[Index];
  347. FCopyParams->Items[Index] = (reinterpret_cast<TObject *>(CopyParam));
  348. delete Rules[Index];
  349. FRules->Items[Index] = (reinterpret_cast<TObject *>(Rule));
  350. Modify();
  351. }
  352. else
  353. {
  354. delete CopyParam;
  355. delete Rule;
  356. }
  357. }
  358. //---------------------------------------------------------------------------
  359. void __fastcall TCopyParamList::Move(int CurIndex, int NewIndex)
  360. {
  361. if (CurIndex != NewIndex)
  362. {
  363. FNames->Move(CurIndex, NewIndex);
  364. FCopyParams->Move(CurIndex, NewIndex);
  365. FRules->Move(CurIndex, NewIndex);
  366. Modify();
  367. }
  368. }
  369. //---------------------------------------------------------------------------
  370. void __fastcall TCopyParamList::Delete(int Index)
  371. {
  372. assert((Index >= 0) && (Index < Count));
  373. FNames->Delete(Index);
  374. delete CopyParams[Index];
  375. FCopyParams->Delete(Index);
  376. delete Rules[Index];
  377. FRules->Delete(Index);
  378. Modify();
  379. }
  380. //---------------------------------------------------------------------------
  381. int __fastcall TCopyParamList::Find(const TCopyParamRuleData & Value) const
  382. {
  383. int Result = -1;
  384. int i = 0;
  385. while ((i < FRules->Count) && (Result < 0))
  386. {
  387. if (FRules->Items[i] != NULL)
  388. {
  389. if (Rules[i]->Matches(Value))
  390. {
  391. Result = i;
  392. }
  393. }
  394. i++;
  395. }
  396. return Result;
  397. }
  398. //---------------------------------------------------------------------------
  399. void __fastcall TCopyParamList::Load(THierarchicalStorage * Storage, int ACount)
  400. {
  401. for (int Index = 0; Index < ACount; Index++)
  402. {
  403. UnicodeString Name = IntToStr(Index);
  404. TCopyParamRule * Rule = NULL;
  405. TCopyParamType * CopyParam = new TCopyParamType();
  406. try
  407. {
  408. if (Storage->OpenSubKey(Name, false))
  409. {
  410. try
  411. {
  412. Name = Storage->ReadString(L"Name", Name);
  413. CopyParam->Load(Storage);
  414. if (Storage->ReadBool(L"HasRule", false))
  415. {
  416. Rule = new TCopyParamRule();
  417. Rule->Load(Storage);
  418. }
  419. }
  420. __finally
  421. {
  422. Storage->CloseSubKey();
  423. }
  424. }
  425. }
  426. catch(...)
  427. {
  428. delete CopyParam;
  429. delete Rule;
  430. throw;
  431. }
  432. FCopyParams->Add(reinterpret_cast<TObject *>(CopyParam));
  433. FRules->Add(reinterpret_cast<TObject *>(Rule));
  434. FNames->Add(Name);
  435. }
  436. Reset();
  437. }
  438. //---------------------------------------------------------------------------
  439. void __fastcall TCopyParamList::Save(THierarchicalStorage * Storage) const
  440. {
  441. Storage->ClearSubKeys();
  442. for (int Index = 0; Index < Count; Index++)
  443. {
  444. if (Storage->OpenSubKey(IntToStr(Index), true))
  445. {
  446. try
  447. {
  448. const TCopyParamType * CopyParam = CopyParams[Index];
  449. const TCopyParamRule * Rule = Rules[Index];
  450. Storage->WriteString(L"Name", Names[Index]);
  451. CopyParam->Save(Storage);
  452. Storage->WriteBool(L"HasRule", (Rule != NULL));
  453. if (Rule != NULL)
  454. {
  455. Rule->Save(Storage);
  456. }
  457. }
  458. __finally
  459. {
  460. Storage->CloseSubKey();
  461. }
  462. }
  463. }
  464. }
  465. //---------------------------------------------------------------------------
  466. int __fastcall TCopyParamList::GetCount() const
  467. {
  468. return FCopyParams->Count;
  469. }
  470. //---------------------------------------------------------------------------
  471. const TCopyParamRule * __fastcall TCopyParamList::GetRule(int Index) const
  472. {
  473. return reinterpret_cast<TCopyParamRule *>(FRules->Items[Index]);
  474. }
  475. //---------------------------------------------------------------------------
  476. const TCopyParamType * __fastcall TCopyParamList::GetCopyParam(int Index) const
  477. {
  478. return reinterpret_cast<TCopyParamType *>(FCopyParams->Items[Index]);
  479. }
  480. //---------------------------------------------------------------------------
  481. UnicodeString __fastcall TCopyParamList::GetName(int Index) const
  482. {
  483. return FNames->Strings[Index];
  484. }
  485. //---------------------------------------------------------------------------
  486. TStrings * __fastcall TCopyParamList::GetNameList() const
  487. {
  488. if (FNameList == NULL)
  489. {
  490. FNameList = new TStringList();
  491. for (int i = 0; i < Count; i++)
  492. {
  493. FNameList->Add(FNames->Strings[i]);
  494. }
  495. }
  496. return FNameList;
  497. }
  498. //---------------------------------------------------------------------------
  499. bool __fastcall TCopyParamList::GetAnyRule() const
  500. {
  501. bool Result = false;
  502. int i = 0;
  503. while ((i < Count) && !Result)
  504. {
  505. Result = (Rules[i] != NULL);
  506. i++;
  507. }
  508. return Result;
  509. }
  510. //---------------------------------------------------------------------------
  511. //---------------------------------------------------------------------------
  512. __fastcall TGUIConfiguration::TGUIConfiguration(): TConfiguration()
  513. {
  514. FLocale = 0;
  515. FLocales = new TStringList();
  516. FLastLocalesExts = L"*";
  517. dynamic_cast<TStringList*>(FLocales)->Sorted = true;
  518. dynamic_cast<TStringList*>(FLocales)->CaseSensitive = false;
  519. FCopyParamList = new TCopyParamList();
  520. CoreSetResourceModule(GetResourceModule());
  521. }
  522. //---------------------------------------------------------------------------
  523. __fastcall TGUIConfiguration::~TGUIConfiguration()
  524. {
  525. delete FLocales;
  526. delete FCopyParamList;
  527. }
  528. //---------------------------------------------------------------------------
  529. void __fastcall TGUIConfiguration::Default()
  530. {
  531. TConfiguration::Default();
  532. // reset before call to DefaultLocalized()
  533. FDefaultCopyParam.Default();
  534. FCopyParamListDefaults = true;
  535. DefaultLocalized();
  536. FIgnoreCancelBeforeFinish = TDateTime(0, 0, 3, 0);
  537. FContinueOnError = false;
  538. FConfirmCommandSession = true;
  539. FSynchronizeParams = TTerminal::spNoConfirmation | TTerminal::spPreviewChanges;
  540. FSynchronizeModeAuto = -1;
  541. FSynchronizeMode = TTerminal::smRemote;
  542. FMaxWatchDirectories = 500;
  543. FSynchronizeOptions = soRecurse | soSynchronizeAsk;
  544. FQueueTransfersLimit = 2;
  545. FQueueAutoPopup = true;
  546. FQueueRememberPassword = false;
  547. UnicodeString ProgramsFolder;
  548. SpecialFolderLocation(CSIDL_PROGRAM_FILES, ProgramsFolder);
  549. FDefaultPuttyPathOnly = IncludeTrailingBackslash(ProgramsFolder) + L"PuTTY\\putty.exe";
  550. FDefaultPuttyPath = FormatCommand(L"%PROGRAMFILES%\\PuTTY\\putty.exe", L"");
  551. FPuttyPath = FDefaultPuttyPath;
  552. PSftpPath = FormatCommand(L"%PROGRAMFILES%\\PuTTY\\psftp.exe", L"");
  553. FPuttyPassword = false;
  554. FTelnetForFtpInPutty = true;
  555. FPuttySession = L"WinSCP temporary session";
  556. FBeepOnFinish = false;
  557. FBeepOnFinishAfter = TDateTime(0, 0, 30, 0);
  558. FCopyParamCurrent = L"";
  559. FKeepUpToDateChangeDelay = 500;
  560. FChecksumAlg = L"md5";
  561. FSessionReopenAutoIdle = 5000;
  562. FNewDirectoryProperties.Default();
  563. FNewDirectoryProperties.Rights = TRights::rfDefault | TRights::rfExec;
  564. }
  565. //---------------------------------------------------------------------------
  566. void __fastcall TGUIConfiguration::DefaultLocalized()
  567. {
  568. if (FCopyParamListDefaults)
  569. {
  570. FCopyParamList->Clear();
  571. // guard against "empty resource string" from obsolete traslations
  572. // (DefaultLocalized is called for the first time before detection of
  573. // obsolete translations)
  574. if (!LoadStr(COPY_PARAM_PRESET_ASCII).IsEmpty())
  575. {
  576. TCopyParamType * CopyParam;
  577. CopyParam = new TCopyParamType(FDefaultCopyParam);
  578. CopyParam->TransferMode = tmAscii;
  579. FCopyParamList->Add(LoadStr(COPY_PARAM_PRESET_ASCII), CopyParam, NULL);
  580. CopyParam = new TCopyParamType(FDefaultCopyParam);
  581. CopyParam->TransferMode = tmBinary;
  582. FCopyParamList->Add(LoadStr(COPY_PARAM_PRESET_BINARY), CopyParam, NULL);
  583. CopyParam = new TCopyParamType(FDefaultCopyParam);
  584. CopyParam->IncludeFileMask.Masks = L"|*.bak; *.tmp; ~$*; *.wbk; *~; #*; .#*";
  585. FCopyParamList->Add(LoadStr(COPY_PARAM_PRESET_EXCLUDE), CopyParam, NULL);
  586. }
  587. FCopyParamList->Reset();
  588. }
  589. }
  590. //---------------------------------------------------------------------------
  591. void __fastcall TGUIConfiguration::UpdateStaticUsage()
  592. {
  593. Usage->Set(L"CopyParamsCount", (FCopyParamListDefaults ? 0 : FCopyParamList->Count));
  594. }
  595. //---------------------------------------------------------------------------
  596. UnicodeString __fastcall TGUIConfiguration::PropertyToKey(const UnicodeString Property)
  597. {
  598. // no longer useful
  599. int P = Property.LastDelimiter(L".>");
  600. return Property.SubString(P + 1, Property.Length() - P);
  601. }
  602. //---------------------------------------------------------------------------
  603. // duplicated from core\configuration.cpp
  604. #define BLOCK(KEY, CANCREATE, BLOCK) \
  605. if (Storage->OpenSubKey(KEY, CANCREATE, true)) try { BLOCK } __finally { Storage->CloseSubKey(); }
  606. #define REGCONFIG(CANCREATE) \
  607. BLOCK(L"Interface", CANCREATE, \
  608. KEY(Bool, ContinueOnError); \
  609. KEY(Bool, ConfirmCommandSession); \
  610. KEY(Integer, SynchronizeParams); \
  611. KEY(Integer, SynchronizeOptions); \
  612. KEY(Integer, SynchronizeModeAuto); \
  613. KEY(Integer, SynchronizeMode); \
  614. KEY(Integer, MaxWatchDirectories); \
  615. KEY(Integer, QueueTransfersLimit); \
  616. KEY(Bool, QueueAutoPopup); \
  617. KEY(Bool, QueueRememberPassword); \
  618. KEY(String, PuttySession); \
  619. KEY(String, PuttyPath); \
  620. KEY(Bool, PuttyPassword); \
  621. KEY(Bool, TelnetForFtpInPutty); \
  622. KEY(DateTime, IgnoreCancelBeforeFinish); \
  623. KEY(Bool, BeepOnFinish); \
  624. KEY(DateTime, BeepOnFinishAfter); \
  625. KEY(Integer, KeepUpToDateChangeDelay); \
  626. KEY(String, ChecksumAlg); \
  627. KEY(Integer, SessionReopenAutoIdle); \
  628. ); \
  629. //---------------------------------------------------------------------------
  630. void __fastcall TGUIConfiguration::SaveData(THierarchicalStorage * Storage, bool All)
  631. {
  632. TConfiguration::SaveData(Storage, All);
  633. // duplicated from core\configuration.cpp
  634. #define KEY(TYPE, VAR) Storage->Write ## TYPE(PropertyToKey(TEXT(#VAR)), VAR)
  635. REGCONFIG(true);
  636. #undef KEY
  637. if (Storage->OpenSubKey(L"Interface\\CopyParam", true, true))
  638. try
  639. {
  640. FDefaultCopyParam.Save(Storage);
  641. if (FCopyParamListDefaults)
  642. {
  643. assert(!FCopyParamList->Modified);
  644. Storage->WriteInteger(L"CopyParamList", -1);
  645. }
  646. else if (All || FCopyParamList->Modified)
  647. {
  648. Storage->WriteInteger(L"CopyParamList", FCopyParamList->Count);
  649. FCopyParamList->Save(Storage);
  650. }
  651. }
  652. __finally
  653. {
  654. Storage->CloseSubKey();
  655. }
  656. if (Storage->OpenSubKey(L"Interface\\NewDirectory2", true, true))
  657. try
  658. {
  659. FNewDirectoryProperties.Save(Storage);
  660. }
  661. __finally
  662. {
  663. Storage->CloseSubKey();
  664. }
  665. }
  666. //---------------------------------------------------------------------------
  667. void __fastcall TGUIConfiguration::LoadData(THierarchicalStorage * Storage)
  668. {
  669. TConfiguration::LoadData(Storage);
  670. // duplicated from core\configuration.cpp
  671. #define KEY(TYPE, VAR) VAR = Storage->Read ## TYPE(PropertyToKey(TEXT(#VAR)), VAR)
  672. #pragma warn -eas
  673. REGCONFIG(false);
  674. #pragma warn +eas
  675. #undef KEY
  676. if (Storage->OpenSubKey(L"Interface\\CopyParam", false, true))
  677. try
  678. {
  679. // must be loaded before eventual setting defaults for CopyParamList
  680. FDefaultCopyParam.Load(Storage);
  681. int CopyParamListCount = Storage->ReadInteger(L"CopyParamList", -1);
  682. FCopyParamListDefaults = (CopyParamListCount < 0);
  683. if (!FCopyParamListDefaults)
  684. {
  685. FCopyParamList->Clear();
  686. FCopyParamList->Load(Storage, CopyParamListCount);
  687. }
  688. else if (FCopyParamList->Modified)
  689. {
  690. FCopyParamList->Clear();
  691. FCopyParamListDefaults = false;
  692. }
  693. FCopyParamList->Reset();
  694. }
  695. __finally
  696. {
  697. Storage->CloseSubKey();
  698. }
  699. // Make it compatible with versions prior to 3.7.1 that have not saved PuttyPath
  700. // with quotes. First check for absence of quotes.
  701. // Add quotes either if the path is set to default putty path (even if it does
  702. // not exists) or when the path points to existing file (so there are no parameters
  703. // yet in the string). Note that FileExists may display error dialog, but as
  704. // it should be called only for custom users path, let's expect that the user
  705. // can take care of it.
  706. if ((FPuttyPath.SubString(1, 1) != L"\"") &&
  707. (CompareFileName(ExpandEnvironmentVariables(FPuttyPath), FDefaultPuttyPathOnly) ||
  708. FileExists(ExpandEnvironmentVariables(FPuttyPath))))
  709. {
  710. FPuttyPath = FormatCommand(FPuttyPath, L"");
  711. }
  712. if (Storage->OpenSubKey(L"Interface\\NewDirectory2", false, true))
  713. try
  714. {
  715. FNewDirectoryProperties.Load(Storage);
  716. }
  717. __finally
  718. {
  719. Storage->CloseSubKey();
  720. }
  721. }
  722. //---------------------------------------------------------------------------
  723. void __fastcall TGUIConfiguration::Saved()
  724. {
  725. TConfiguration::Saved();
  726. FCopyParamList->Reset();
  727. }
  728. //---------------------------------------------------------------------------
  729. //---------------------------------------------------------------------------
  730. HINSTANCE __fastcall TGUIConfiguration::LoadNewResourceModule(LCID ALocale,
  731. UnicodeString * FileName)
  732. {
  733. UnicodeString LibraryFileName;
  734. HINSTANCE NewInstance = 0;
  735. bool Internal = (ALocale == InternalLocale());
  736. if (!Internal)
  737. {
  738. UnicodeString Module;
  739. UnicodeString LocaleName;
  740. Module = ModuleFileName();
  741. if ((ALocale & AdditionaLanguageMask) != AdditionaLanguageMask)
  742. {
  743. wchar_t LocaleStr[4];
  744. GetLocaleInfo(ALocale, LOCALE_SABBREVLANGNAME, LocaleStr, LENOF(LocaleStr));
  745. LocaleName = LocaleStr;
  746. assert(!LocaleName.IsEmpty());
  747. }
  748. else
  749. {
  750. LocaleName = AdditionaLanguagePrefix +
  751. char(ALocale & ~AdditionaLanguageMask);
  752. }
  753. Module = ChangeFileExt(Module, UnicodeString(L".") + LocaleName);
  754. // Look for a potential language/country translation
  755. NewInstance = LoadLibraryEx(Module.c_str(), 0, LOAD_LIBRARY_AS_DATAFILE);
  756. if (!NewInstance)
  757. {
  758. // Finally look for a language only translation
  759. Module.SetLength(Module.Length() - 1);
  760. NewInstance = LoadLibraryEx(Module.c_str(), 0, LOAD_LIBRARY_AS_DATAFILE);
  761. if (NewInstance)
  762. {
  763. LibraryFileName = Module;
  764. }
  765. }
  766. else
  767. {
  768. LibraryFileName = Module;
  769. }
  770. }
  771. if (!NewInstance && !Internal)
  772. {
  773. throw Exception(FMTLOAD(LOCALE_LOAD_ERROR, (int(ALocale))));
  774. }
  775. else
  776. {
  777. if (Internal)
  778. {
  779. NewInstance = HInstance;
  780. }
  781. }
  782. if (FileName != NULL)
  783. {
  784. *FileName = LibraryFileName;
  785. }
  786. return NewInstance;
  787. }
  788. //---------------------------------------------------------------------------
  789. LCID __fastcall TGUIConfiguration::InternalLocale()
  790. {
  791. LCID Result;
  792. if (GetTranslationCount(ApplicationInfo) > 0)
  793. {
  794. TTranslation Translation;
  795. Translation = GetTranslation(ApplicationInfo, 0);
  796. Result = MAKELANGID(PRIMARYLANGID(Translation.Language), SUBLANG_DEFAULT);
  797. }
  798. else
  799. {
  800. assert(false);
  801. Result = 0;
  802. }
  803. return Result;
  804. }
  805. //---------------------------------------------------------------------------
  806. LCID __fastcall TGUIConfiguration::GetLocale()
  807. {
  808. if (!FLocale)
  809. {
  810. FLocale = InternalLocale();
  811. }
  812. return FLocale;
  813. }
  814. //---------------------------------------------------------------------------
  815. void __fastcall TGUIConfiguration::SetLocale(LCID value)
  816. {
  817. if (Locale != value)
  818. {
  819. HINSTANCE Module = LoadNewResourceModule(value);
  820. if (Module != NULL)
  821. {
  822. FLocale = value;
  823. SetResourceModule(Module);
  824. }
  825. else
  826. {
  827. assert(false);
  828. }
  829. }
  830. }
  831. //---------------------------------------------------------------------------
  832. void __fastcall TGUIConfiguration::SetLocaleSafe(LCID value)
  833. {
  834. if (Locale != value)
  835. {
  836. HINSTANCE Module;
  837. try
  838. {
  839. Module = LoadNewResourceModule(value);
  840. }
  841. catch(...)
  842. {
  843. // ignore any exception while loading locale
  844. Module = NULL;
  845. }
  846. if (Module != NULL)
  847. {
  848. FLocale = value;
  849. SetResourceModule(Module);
  850. }
  851. }
  852. }
  853. //---------------------------------------------------------------------------
  854. void __fastcall TGUIConfiguration::FreeResourceModule(HANDLE Instance)
  855. {
  856. TLibModule * MainModule = FindModule(HInstance);
  857. if ((unsigned)Instance != MainModule->Instance)
  858. {
  859. FreeLibrary(static_cast<HMODULE>(Instance));
  860. }
  861. }
  862. //---------------------------------------------------------------------------
  863. HANDLE __fastcall TGUIConfiguration::ChangeResourceModule(HANDLE Instance)
  864. {
  865. if (Instance == NULL)
  866. {
  867. Instance = HInstance;
  868. }
  869. TLibModule * MainModule = FindModule(HInstance);
  870. HANDLE Result = (HANDLE)MainModule->ResInstance;
  871. MainModule->ResInstance = (unsigned)Instance;
  872. CoreSetResourceModule(Instance);
  873. return Result;
  874. }
  875. //---------------------------------------------------------------------------
  876. HANDLE __fastcall TGUIConfiguration::GetResourceModule()
  877. {
  878. return (HANDLE)FindModule(HInstance)->ResInstance;
  879. }
  880. //---------------------------------------------------------------------------
  881. void __fastcall TGUIConfiguration::SetResourceModule(HINSTANCE Instance)
  882. {
  883. HANDLE PrevHandle = ChangeResourceModule(Instance);
  884. FreeResourceModule(PrevHandle);
  885. DefaultLocalized();
  886. }
  887. //---------------------------------------------------------------------------
  888. TStrings * __fastcall TGUIConfiguration::GetLocales()
  889. {
  890. UnicodeString LocalesExts;
  891. TStringList * Exts = new TStringList();
  892. try
  893. {
  894. Exts->Sorted = true;
  895. Exts->CaseSensitive = false;
  896. int FindAttrs = faReadOnly | faArchive;
  897. TSearchRec SearchRec;
  898. bool Found;
  899. Found = (bool)(FindFirst(ChangeFileExt(ModuleFileName(), L".*"),
  900. FindAttrs, SearchRec) == 0);
  901. try
  902. {
  903. UnicodeString Ext;
  904. while (Found)
  905. {
  906. Ext = ExtractFileExt(SearchRec.Name).UpperCase();
  907. if ((Ext.Length() >= 3) && (Ext != L".EXE") && (Ext != L".COM") &&
  908. (Ext != L".DLL") && (Ext != L".INI"))
  909. {
  910. Ext = Ext.SubString(2, Ext.Length() - 1);
  911. LocalesExts += Ext;
  912. Exts->Add(Ext);
  913. }
  914. Found = (FindNext(SearchRec) == 0);
  915. }
  916. }
  917. __finally
  918. {
  919. FindClose(SearchRec);
  920. }
  921. if (FLastLocalesExts != LocalesExts)
  922. {
  923. FLastLocalesExts = LocalesExts;
  924. FLocales->Clear();
  925. TLanguages * Langs = LanguagesDEPF();
  926. int Ext, Index, Count;
  927. wchar_t LocaleStr[255];
  928. LCID Locale;
  929. Count = Langs->Count;
  930. Index = -1;
  931. while (Index < Count)
  932. {
  933. if (Index >= 0)
  934. {
  935. Locale = Langs->LocaleID[Index];
  936. Ext = Exts->IndexOf(Langs->Ext[Index]);
  937. if (Ext < 0)
  938. {
  939. Ext = Exts->IndexOf(Langs->Ext[Index].SubString(1, 2));
  940. if (Ext >= 0)
  941. {
  942. Locale = MAKELANGID(PRIMARYLANGID(Locale), SUBLANG_DEFAULT);
  943. }
  944. }
  945. if (Ext >= 0)
  946. {
  947. Exts->Objects[Ext] = reinterpret_cast<TObject*>(Locale);
  948. }
  949. else
  950. {
  951. Locale = 0;
  952. }
  953. }
  954. else
  955. {
  956. Locale = InternalLocale();
  957. }
  958. if (Locale)
  959. {
  960. UnicodeString Name;
  961. GetLocaleInfo(Locale, LOCALE_SENGLANGUAGE,
  962. LocaleStr, LENOF(LocaleStr));
  963. Name = LocaleStr;
  964. Name += L" - ";
  965. // LOCALE_SNATIVELANGNAME
  966. GetLocaleInfo(Locale, LOCALE_SLANGUAGE,
  967. LocaleStr, LENOF(LocaleStr));
  968. Name += LocaleStr;
  969. FLocales->AddObject(Name, reinterpret_cast<TObject*>(Locale));
  970. }
  971. Index++;
  972. }
  973. for (int Index = 0; Index < Exts->Count; Index++)
  974. {
  975. if ((Exts->Objects[Index] == NULL) &&
  976. (Exts->Strings[Index].Length() == 3) &&
  977. SameText(Exts->Strings[Index].SubString(1, 2), AdditionaLanguagePrefix))
  978. {
  979. UnicodeString LangName = GetFileFileInfoString(L"LangName",
  980. ChangeFileExt(ModuleFileName(), UnicodeString(L".") + Exts->Strings[Index]));
  981. if (!LangName.IsEmpty())
  982. {
  983. FLocales->AddObject(LangName, reinterpret_cast<TObject*>(
  984. AdditionaLanguageMask + Exts->Strings[Index][3]));
  985. }
  986. }
  987. }
  988. }
  989. }
  990. __finally
  991. {
  992. delete Exts;
  993. }
  994. return FLocales;
  995. }
  996. //---------------------------------------------------------------------------
  997. void __fastcall TGUIConfiguration::SetDefaultCopyParam(const TGUICopyParamType & value)
  998. {
  999. FDefaultCopyParam.Assign(&value);
  1000. Changed();
  1001. }
  1002. //---------------------------------------------------------------------------
  1003. bool __fastcall TGUIConfiguration::GetRememberPassword()
  1004. {
  1005. return QueueRememberPassword || PuttyPassword;
  1006. }
  1007. //---------------------------------------------------------------------------
  1008. const TCopyParamList * __fastcall TGUIConfiguration::GetCopyParamList()
  1009. {
  1010. return FCopyParamList;
  1011. }
  1012. //---------------------------------------------------------------------------
  1013. void __fastcall TGUIConfiguration::SetCopyParamList(const TCopyParamList * value)
  1014. {
  1015. if (!(*FCopyParamList == *value))
  1016. {
  1017. *FCopyParamList = *value;
  1018. FCopyParamListDefaults = false;
  1019. Changed();
  1020. }
  1021. }
  1022. //---------------------------------------------------------------------------
  1023. int __fastcall TGUIConfiguration::GetCopyParamIndex()
  1024. {
  1025. int Result;
  1026. if (FCopyParamCurrent.IsEmpty())
  1027. {
  1028. Result = -1;
  1029. }
  1030. else
  1031. {
  1032. Result = FCopyParamList->IndexOfName(FCopyParamCurrent);
  1033. }
  1034. return Result;
  1035. }
  1036. //---------------------------------------------------------------------------
  1037. void __fastcall TGUIConfiguration::SetCopyParamIndex(int value)
  1038. {
  1039. UnicodeString Name;
  1040. if (value < 0)
  1041. {
  1042. Name = L"";
  1043. }
  1044. else
  1045. {
  1046. Name = FCopyParamList->Names[value];
  1047. }
  1048. CopyParamCurrent = Name;
  1049. }
  1050. //---------------------------------------------------------------------------
  1051. void __fastcall TGUIConfiguration::SetCopyParamCurrent(UnicodeString value)
  1052. {
  1053. SET_CONFIG_PROPERTY(CopyParamCurrent);
  1054. }
  1055. //---------------------------------------------------------------------------
  1056. TGUICopyParamType __fastcall TGUIConfiguration::GetCurrentCopyParam()
  1057. {
  1058. return CopyParamPreset[CopyParamCurrent];
  1059. }
  1060. //---------------------------------------------------------------------------
  1061. TGUICopyParamType __fastcall TGUIConfiguration::GetCopyParamPreset(UnicodeString Name)
  1062. {
  1063. TGUICopyParamType Result = FDefaultCopyParam;
  1064. if (!Name.IsEmpty())
  1065. {
  1066. int Index = FCopyParamList->IndexOfName(Name);
  1067. assert(Index >= 0);
  1068. if (Index >= 0)
  1069. {
  1070. const TCopyParamType * Preset = FCopyParamList->CopyParams[Index];
  1071. assert(Preset != NULL);
  1072. Result.Assign(Preset); // overwrite all but GUI options
  1073. // reset all options known not to be configurable per-preset
  1074. // kind of hack
  1075. Result.ResumeSupport = FDefaultCopyParam.ResumeSupport;
  1076. Result.ResumeThreshold = FDefaultCopyParam.ResumeThreshold;
  1077. Result.LocalInvalidChars = FDefaultCopyParam.LocalInvalidChars;
  1078. }
  1079. }
  1080. return Result;
  1081. }
  1082. //---------------------------------------------------------------------------
  1083. bool __fastcall TGUIConfiguration::GetHasCopyParamPreset(UnicodeString Name)
  1084. {
  1085. return Name.IsEmpty() || (FCopyParamList->IndexOfName(Name) >= 0);
  1086. }
  1087. //---------------------------------------------------------------------------
  1088. void __fastcall TGUIConfiguration::SetNewDirectoryProperties(
  1089. const TRemoteProperties & value)
  1090. {
  1091. SET_CONFIG_PROPERTY(NewDirectoryProperties);
  1092. }
  1093. //---------------------------------------------------------------------------