GUIConfiguration.cpp 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include <Common.h>
  5. #include "GUIConfiguration.h"
  6. #include "GUITools.h"
  7. #include <FileInfo.h>
  8. #include <TextsCore.h>
  9. #include <TextsWin.h>
  10. #include <Terminal.h>
  11. #include <CoreMain.h>
  12. #include <shlobj.h>
  13. //---------------------------------------------------------------------------
  14. #pragma package(smart_init)
  15. //---------------------------------------------------------------------------
  16. const int ccLocal = ccUser;
  17. const int ccShowResults = ccUser << 1;
  18. const int ccCopyResults = ccUser << 2;
  19. const int ccRemoteFiles = ccUser << 3;
  20. const int ccShowResultsInMsgBox = ccUser << 4;
  21. const int ccSet = 0x80000000;
  22. //---------------------------------------------------------------------------
  23. static const unsigned int AdditionaLanguageMask = 0xFFFFFF00;
  24. static const UnicodeString AdditionaLanguagePrefix(L"XX");
  25. static const UnicodeString TranslationsSubFolder(L"Translations");
  26. //---------------------------------------------------------------------------
  27. TGUIConfiguration * GUIConfiguration = NULL;
  28. //---------------------------------------------------------------------------
  29. __fastcall TGUICopyParamType::TGUICopyParamType()
  30. : TCopyParamType()
  31. {
  32. GUIDefault();
  33. }
  34. //---------------------------------------------------------------------------
  35. __fastcall TGUICopyParamType::TGUICopyParamType(const TCopyParamType & Source)
  36. : TCopyParamType(Source)
  37. {
  38. GUIDefault();
  39. }
  40. //---------------------------------------------------------------------------
  41. __fastcall TGUICopyParamType::TGUICopyParamType(const TGUICopyParamType & Source)
  42. : TCopyParamType(Source)
  43. {
  44. GUIAssign(&Source);
  45. }
  46. //---------------------------------------------------------------------------
  47. void __fastcall TGUICopyParamType::Assign(const TCopyParamType * Source)
  48. {
  49. TCopyParamType::Assign(Source);
  50. const TGUICopyParamType * GUISource;
  51. GUISource = dynamic_cast<const TGUICopyParamType *>(Source);
  52. if (GUISource != NULL)
  53. {
  54. GUIAssign(GUISource);
  55. }
  56. }
  57. //---------------------------------------------------------------------------
  58. void __fastcall TGUICopyParamType::GUIAssign(const TGUICopyParamType * Source)
  59. {
  60. Queue = Source->Queue;
  61. QueueNoConfirmation = Source->QueueNoConfirmation;
  62. QueueParallel = Source->QueueParallel;
  63. }
  64. //---------------------------------------------------------------------------
  65. void __fastcall TGUICopyParamType::Default()
  66. {
  67. TCopyParamType::Default();
  68. GUIDefault();
  69. }
  70. //---------------------------------------------------------------------------
  71. void __fastcall TGUICopyParamType::GUIDefault()
  72. {
  73. Queue = false;
  74. QueueNoConfirmation = true;
  75. QueueParallel = true;
  76. }
  77. //---------------------------------------------------------------------------
  78. void __fastcall TGUICopyParamType::Load(THierarchicalStorage * Storage)
  79. {
  80. TCopyParamType::Load(Storage);
  81. Queue = Storage->ReadBool(L"Queue", Queue);
  82. QueueNoConfirmation = Storage->ReadBool(L"QueueNoConfirmation", QueueNoConfirmation);
  83. QueueParallel = Storage->ReadBool(L"QueueParallel", QueueParallel);
  84. }
  85. //---------------------------------------------------------------------------
  86. void __fastcall TGUICopyParamType::Save(THierarchicalStorage * Storage, const TCopyParamType * Defaults) const
  87. {
  88. DebugAssert(Defaults == NULL);
  89. TCopyParamType::Save(Storage, Defaults);
  90. Storage->WriteBool(L"Queue", Queue);
  91. Storage->WriteBool(L"QueueNoConfirmation", QueueNoConfirmation);
  92. Storage->WriteBool(L"QueueParallel", QueueParallel);
  93. }
  94. //---------------------------------------------------------------------------
  95. TGUICopyParamType & __fastcall TGUICopyParamType::operator =(const TCopyParamType & rhp)
  96. {
  97. Assign(&rhp);
  98. return *this;
  99. }
  100. //---------------------------------------------------------------------------
  101. TGUICopyParamType & __fastcall TGUICopyParamType::operator =(const TGUICopyParamType & rhp)
  102. {
  103. Assign(&rhp);
  104. return *this;
  105. }
  106. //---------------------------------------------------------------------------
  107. //---------------------------------------------------------------------------
  108. void __fastcall TCopyParamRuleData::Default()
  109. {
  110. HostName = L"";
  111. UserName = L"";
  112. RemoteDirectory = L"";
  113. LocalDirectory = L"";
  114. }
  115. //---------------------------------------------------------------------------
  116. //---------------------------------------------------------------------------
  117. __fastcall TCopyParamRule::TCopyParamRule()
  118. {
  119. }
  120. //---------------------------------------------------------------------------
  121. __fastcall TCopyParamRule::TCopyParamRule(const TCopyParamRuleData & Data)
  122. {
  123. FData = Data;
  124. }
  125. //---------------------------------------------------------------------------
  126. __fastcall TCopyParamRule::TCopyParamRule(const TCopyParamRule & Source)
  127. {
  128. FData.HostName = Source.FData.HostName;
  129. FData.UserName = Source.FData.UserName;
  130. FData.RemoteDirectory = Source.FData.RemoteDirectory;
  131. FData.LocalDirectory = Source.FData.LocalDirectory;
  132. }
  133. //---------------------------------------------------------------------------
  134. #define C(Property) (Property == rhp.Property)
  135. bool __fastcall TCopyParamRule::operator==(const TCopyParamRule & rhp) const
  136. {
  137. return
  138. C(FData.HostName) &&
  139. C(FData.UserName) &&
  140. C(FData.RemoteDirectory) &&
  141. C(FData.LocalDirectory) &&
  142. true;
  143. }
  144. #undef C
  145. //---------------------------------------------------------------------------
  146. bool __fastcall TCopyParamRule::Match(const UnicodeString & Mask,
  147. const UnicodeString & Value, bool Path, bool Local, int ForceDirectoryMasks) const
  148. {
  149. bool Result;
  150. if (Mask.IsEmpty())
  151. {
  152. Result = true;
  153. }
  154. else
  155. {
  156. TFileMasks M(ForceDirectoryMasks);
  157. M.Masks = Mask;
  158. if (Path)
  159. {
  160. Result = M.Matches(Value, Local, true);
  161. }
  162. else
  163. {
  164. Result = M.Matches(Value, false);
  165. }
  166. }
  167. return Result;
  168. }
  169. //---------------------------------------------------------------------------
  170. bool __fastcall TCopyParamRule::Matches(const TCopyParamRuleData & Value) const
  171. {
  172. return
  173. Match(FData.HostName, Value.HostName, false, true, 0) &&
  174. Match(FData.UserName, Value.UserName, false, true, 0) &&
  175. Match(FData.RemoteDirectory, Value.RemoteDirectory, true, false, 1) &&
  176. Match(FData.LocalDirectory, Value.LocalDirectory, true, true, 1);
  177. }
  178. //---------------------------------------------------------------------------
  179. void __fastcall TCopyParamRule::Load(THierarchicalStorage * Storage)
  180. {
  181. FData.HostName = Storage->ReadString(L"HostName", FData.HostName);
  182. FData.UserName = Storage->ReadString(L"UserName", FData.UserName);
  183. FData.RemoteDirectory = Storage->ReadString(L"RemoteDirectory", FData.RemoteDirectory);
  184. FData.LocalDirectory = Storage->ReadString(L"LocalDirectory", FData.LocalDirectory);
  185. }
  186. //---------------------------------------------------------------------------
  187. void __fastcall TCopyParamRule::Save(THierarchicalStorage * Storage) const
  188. {
  189. Storage->WriteString(L"HostName", FData.HostName);
  190. Storage->WriteString(L"UserName", FData.UserName);
  191. Storage->WriteString(L"RemoteDirectory", FData.RemoteDirectory);
  192. Storage->WriteString(L"LocalDirectory", FData.LocalDirectory);
  193. }
  194. //---------------------------------------------------------------------------
  195. bool __fastcall TCopyParamRule::GetEmpty() const
  196. {
  197. return
  198. FData.HostName.IsEmpty() &&
  199. FData.UserName.IsEmpty() &&
  200. FData.RemoteDirectory.IsEmpty() &&
  201. FData.LocalDirectory.IsEmpty();
  202. }
  203. //---------------------------------------------------------------------------
  204. UnicodeString __fastcall TCopyParamRule::GetInfoStr(UnicodeString Separator) const
  205. {
  206. UnicodeString Result;
  207. #define ADD(FMT, ELEM) \
  208. if (!FData.ELEM.IsEmpty()) \
  209. Result += (Result.IsEmpty() ? UnicodeString() : Separator) + FMTLOAD(FMT, (FData.ELEM));
  210. ADD(COPY_RULE_HOSTNAME, HostName);
  211. ADD(COPY_RULE_USERNAME, UserName);
  212. ADD(COPY_RULE_REMOTE_DIR, RemoteDirectory);
  213. ADD(COPY_RULE_LOCAL_DIR, LocalDirectory);
  214. #undef ADD
  215. return Result;
  216. }
  217. //---------------------------------------------------------------------------
  218. //---------------------------------------------------------------------------
  219. UnicodeString TCopyParamList::FInvalidChars(L"/\\[]");
  220. //---------------------------------------------------------------------------
  221. __fastcall TCopyParamList::TCopyParamList()
  222. {
  223. Init();
  224. }
  225. //---------------------------------------------------------------------------
  226. void __fastcall TCopyParamList::Init()
  227. {
  228. FCopyParams = new TList();
  229. FRules = new TList();
  230. FNames = new TStringList();
  231. FNameList = NULL;
  232. FModified = false;
  233. }
  234. //---------------------------------------------------------------------------
  235. __fastcall TCopyParamList::~TCopyParamList()
  236. {
  237. Clear();
  238. delete FCopyParams;
  239. delete FRules;
  240. delete FNames;
  241. delete FNameList;
  242. }
  243. //---------------------------------------------------------------------------
  244. void __fastcall TCopyParamList::Reset()
  245. {
  246. SAFE_DESTROY(FNameList);
  247. FModified = false;
  248. }
  249. //---------------------------------------------------------------------
  250. void __fastcall TCopyParamList::Modify()
  251. {
  252. SAFE_DESTROY(FNameList);
  253. FModified = true;
  254. }
  255. //---------------------------------------------------------------------
  256. void __fastcall TCopyParamList::ValidateName(const UnicodeString Name)
  257. {
  258. if (Name.LastDelimiter(FInvalidChars) > 0)
  259. {
  260. throw Exception(FMTLOAD(ITEM_NAME_INVALID, (Name, FInvalidChars)));
  261. }
  262. }
  263. //---------------------------------------------------------------------------
  264. TCopyParamList & __fastcall TCopyParamList::operator=(const TCopyParamList & rhl)
  265. {
  266. Clear();
  267. for (int Index = 0; Index < rhl.Count; Index++)
  268. {
  269. TCopyParamType * CopyParam = new TCopyParamType(*rhl.CopyParams[Index]);
  270. TCopyParamRule * Rule = NULL;
  271. if (rhl.Rules[Index] != NULL)
  272. {
  273. Rule = new TCopyParamRule(*rhl.Rules[Index]);
  274. }
  275. Add(rhl.Names[Index], CopyParam, Rule);
  276. }
  277. // there should be comparison of with the assigned list, but we rely on caller
  278. // to do it instead (TGUIConfiguration::SetCopyParamList)
  279. Modify();
  280. return *this;
  281. }
  282. //---------------------------------------------------------------------------
  283. bool __fastcall TCopyParamList::operator==(const TCopyParamList & rhl) const
  284. {
  285. bool Result = (Count == rhl.Count);
  286. if (Result)
  287. {
  288. int i = 0;
  289. while ((i < Count) && Result)
  290. {
  291. Result =
  292. (Names[i] == rhl.Names[i]) &&
  293. CompareItem(i, rhl.CopyParams[i], rhl.Rules[i]);
  294. i++;
  295. }
  296. }
  297. return Result;
  298. }
  299. //---------------------------------------------------------------------------
  300. int __fastcall TCopyParamList::IndexOfName(const UnicodeString Name) const
  301. {
  302. return FNames->IndexOf(Name);
  303. }
  304. //---------------------------------------------------------------------------
  305. bool __fastcall TCopyParamList::CompareItem(int Index,
  306. const TCopyParamType * CopyParam, const TCopyParamRule * Rule) const
  307. {
  308. return
  309. ((*CopyParams[Index]) == *CopyParam) &&
  310. ((Rules[Index] == NULL) ?
  311. (Rule == NULL) :
  312. ((Rule != NULL) && (*Rules[Index]) == (*Rule)));
  313. }
  314. //---------------------------------------------------------------------------
  315. void __fastcall TCopyParamList::Clear()
  316. {
  317. for (int i = 0; i < Count; i++)
  318. {
  319. delete CopyParams[i];
  320. delete Rules[i];
  321. }
  322. FCopyParams->Clear();
  323. FRules->Clear();
  324. FNames->Clear();
  325. }
  326. //---------------------------------------------------------------------------
  327. void __fastcall TCopyParamList::Add(const UnicodeString Name,
  328. TCopyParamType * CopyParam, TCopyParamRule * Rule)
  329. {
  330. Insert(Count, Name, CopyParam, Rule);
  331. }
  332. //---------------------------------------------------------------------------
  333. void __fastcall TCopyParamList::Insert(int Index, const UnicodeString Name,
  334. TCopyParamType * CopyParam, TCopyParamRule * Rule)
  335. {
  336. DebugAssert(FNames->IndexOf(Name) < 0);
  337. FNames->Insert(Index, Name);
  338. DebugAssert(CopyParam != NULL);
  339. FCopyParams->Insert(Index, reinterpret_cast<TObject *>(CopyParam));
  340. FRules->Insert(Index, reinterpret_cast<TObject *>(Rule));
  341. Modify();
  342. }
  343. //---------------------------------------------------------------------------
  344. void __fastcall TCopyParamList::Change(int Index, const UnicodeString Name,
  345. TCopyParamType * CopyParam, TCopyParamRule * Rule)
  346. {
  347. if ((Name != Names[Index]) || !CompareItem(Index, CopyParam, Rule))
  348. {
  349. FNames->Strings[Index] = Name;
  350. delete CopyParams[Index];
  351. FCopyParams->Items[Index] = (reinterpret_cast<TObject *>(CopyParam));
  352. delete Rules[Index];
  353. FRules->Items[Index] = (reinterpret_cast<TObject *>(Rule));
  354. Modify();
  355. }
  356. else
  357. {
  358. delete CopyParam;
  359. delete Rule;
  360. }
  361. }
  362. //---------------------------------------------------------------------------
  363. void __fastcall TCopyParamList::Move(int CurIndex, int NewIndex)
  364. {
  365. if (CurIndex != NewIndex)
  366. {
  367. FNames->Move(CurIndex, NewIndex);
  368. FCopyParams->Move(CurIndex, NewIndex);
  369. FRules->Move(CurIndex, NewIndex);
  370. Modify();
  371. }
  372. }
  373. //---------------------------------------------------------------------------
  374. void __fastcall TCopyParamList::Delete(int Index)
  375. {
  376. DebugAssert((Index >= 0) && (Index < Count));
  377. FNames->Delete(Index);
  378. delete CopyParams[Index];
  379. FCopyParams->Delete(Index);
  380. delete Rules[Index];
  381. FRules->Delete(Index);
  382. Modify();
  383. }
  384. //---------------------------------------------------------------------------
  385. int __fastcall TCopyParamList::Find(const TCopyParamRuleData & Value) const
  386. {
  387. int Result = -1;
  388. int i = 0;
  389. while ((i < FRules->Count) && (Result < 0))
  390. {
  391. if (FRules->Items[i] != NULL)
  392. {
  393. if (Rules[i]->Matches(Value))
  394. {
  395. Result = i;
  396. }
  397. }
  398. i++;
  399. }
  400. return Result;
  401. }
  402. //---------------------------------------------------------------------------
  403. void __fastcall TCopyParamList::Load(THierarchicalStorage * Storage, int ACount)
  404. {
  405. for (int Index = 0; Index < ACount; Index++)
  406. {
  407. UnicodeString Name = IntToStr(Index);
  408. TCopyParamRule * Rule = NULL;
  409. TCopyParamType * CopyParam = new TCopyParamType();
  410. try
  411. {
  412. if (Storage->OpenSubKey(Name, false))
  413. {
  414. try
  415. {
  416. Name = Storage->ReadString(L"Name", Name);
  417. CopyParam->Load(Storage);
  418. if (Storage->ReadBool(L"HasRule", false))
  419. {
  420. Rule = new TCopyParamRule();
  421. Rule->Load(Storage);
  422. }
  423. }
  424. __finally
  425. {
  426. Storage->CloseSubKey();
  427. }
  428. }
  429. }
  430. catch(...)
  431. {
  432. delete CopyParam;
  433. delete Rule;
  434. throw;
  435. }
  436. FCopyParams->Add(reinterpret_cast<TObject *>(CopyParam));
  437. FRules->Add(reinterpret_cast<TObject *>(Rule));
  438. FNames->Add(Name);
  439. }
  440. Reset();
  441. }
  442. //---------------------------------------------------------------------------
  443. void __fastcall TCopyParamList::Save(THierarchicalStorage * Storage) const
  444. {
  445. Storage->ClearSubKeys();
  446. for (int Index = 0; Index < Count; Index++)
  447. {
  448. if (Storage->OpenSubKey(IntToStr(Index), true))
  449. {
  450. try
  451. {
  452. const TCopyParamType * CopyParam = CopyParams[Index];
  453. const TCopyParamRule * Rule = Rules[Index];
  454. Storage->WriteString(L"Name", Names[Index]);
  455. CopyParam->Save(Storage);
  456. Storage->WriteBool(L"HasRule", (Rule != NULL));
  457. if (Rule != NULL)
  458. {
  459. Rule->Save(Storage);
  460. }
  461. }
  462. __finally
  463. {
  464. Storage->CloseSubKey();
  465. }
  466. }
  467. }
  468. }
  469. //---------------------------------------------------------------------------
  470. int __fastcall TCopyParamList::GetCount() const
  471. {
  472. return FCopyParams->Count;
  473. }
  474. //---------------------------------------------------------------------------
  475. const TCopyParamRule * __fastcall TCopyParamList::GetRule(int Index) const
  476. {
  477. return reinterpret_cast<TCopyParamRule *>(FRules->Items[Index]);
  478. }
  479. //---------------------------------------------------------------------------
  480. const TCopyParamType * __fastcall TCopyParamList::GetCopyParam(int Index) const
  481. {
  482. return reinterpret_cast<TCopyParamType *>(FCopyParams->Items[Index]);
  483. }
  484. //---------------------------------------------------------------------------
  485. UnicodeString __fastcall TCopyParamList::GetName(int Index) const
  486. {
  487. return FNames->Strings[Index];
  488. }
  489. //---------------------------------------------------------------------------
  490. TStrings * __fastcall TCopyParamList::GetNameList() const
  491. {
  492. if (FNameList == NULL)
  493. {
  494. FNameList = new TStringList();
  495. for (int i = 0; i < Count; i++)
  496. {
  497. FNameList->Add(FNames->Strings[i]);
  498. }
  499. }
  500. return FNameList;
  501. }
  502. //---------------------------------------------------------------------------
  503. bool __fastcall TCopyParamList::GetAnyRule() const
  504. {
  505. bool Result = false;
  506. int i = 0;
  507. while ((i < Count) && !Result)
  508. {
  509. Result = (Rules[i] != NULL);
  510. i++;
  511. }
  512. return Result;
  513. }
  514. //---------------------------------------------------------------------------
  515. //---------------------------------------------------------------------------
  516. __fastcall TGUIConfiguration::TGUIConfiguration(): TConfiguration()
  517. {
  518. FLocale = 0;
  519. SetAppliedLocale(InternalLocale(), UnicodeString());
  520. FLocales = new TObjectList();
  521. FLastLocalesExts = L"*";
  522. FCopyParamList = new TCopyParamList();
  523. CoreSetResourceModule(GetResourceModule());
  524. }
  525. //---------------------------------------------------------------------------
  526. __fastcall TGUIConfiguration::~TGUIConfiguration()
  527. {
  528. delete FLocales;
  529. delete FCopyParamList;
  530. }
  531. //---------------------------------------------------------------------------
  532. void __fastcall TGUIConfiguration::Default()
  533. {
  534. TConfiguration::Default();
  535. // reset before call to DefaultLocalized()
  536. FDefaultCopyParam.Default();
  537. FCopyParamListDefaults = true;
  538. DefaultLocalized();
  539. FIgnoreCancelBeforeFinish = TDateTime(0, 0, 3, 0);
  540. FContinueOnError = false;
  541. FConfirmCommandSession = true;
  542. FSynchronizeParams = TTerminal::spDefault;
  543. FSynchronizeModeAuto = -1;
  544. FSynchronizeMode = TTerminal::smRemote;
  545. FMaxWatchDirectories = 500;
  546. FSynchronizeOptions = soRecurse | soSynchronizeAsk;
  547. FQueueTransfersLimit = 2;
  548. FQueueBootstrap = false;
  549. FQueueKeepDoneItems = true;
  550. FQueueKeepDoneItemsFor = 15;
  551. FQueueAutoPopup = true;
  552. FSessionRememberPassword = true;
  553. UnicodeString ProgramsFolder;
  554. SpecialFolderLocation(CSIDL_PROGRAM_FILES, ProgramsFolder);
  555. FDefaultPuttyPathOnly = IncludeTrailingBackslash(ProgramsFolder) + L"PuTTY\\" + OriginalPuttyExecutable;
  556. FDefaultPuttyPath = L"%ProgramFiles%\\PuTTY\\" + OriginalPuttyExecutable;
  557. FPuttyPath = FormatCommand(FDefaultPuttyPath, L"");
  558. FPuttyPassword = false;
  559. FTelnetForFtpInPutty = true;
  560. FPuttySession = L"WinSCP temporary session";
  561. FBeepOnFinish = false;
  562. FBeepOnFinishAfter = TDateTime(0, 0, 30, 0);
  563. FBeepSound = L"SystemDefault";
  564. FCopyParamCurrent = L"";
  565. FKeepUpToDateChangeDelay = 500;
  566. FChecksumAlg = L"sha1";
  567. FSessionReopenAutoIdle = 9000;
  568. FNewDirectoryProperties.Default();
  569. FNewDirectoryProperties.Rights = TRights::rfDefault | TRights::rfExec;
  570. }
  571. //---------------------------------------------------------------------------
  572. void __fastcall TGUIConfiguration::DefaultLocalized()
  573. {
  574. if (FCopyParamListDefaults)
  575. {
  576. FCopyParamList->Clear();
  577. // guard against "empty resource string" from obsolete traslations
  578. // (DefaultLocalized is called for the first time before detection of
  579. // obsolete translations)
  580. if (!LoadStr(COPY_PARAM_PRESET_ASCII).IsEmpty())
  581. {
  582. TCopyParamType * CopyParam;
  583. CopyParam = new TCopyParamType(FDefaultCopyParam);
  584. CopyParam->TransferMode = tmAscii;
  585. FCopyParamList->Add(LoadStr(COPY_PARAM_PRESET_ASCII), CopyParam, NULL);
  586. CopyParam = new TCopyParamType(FDefaultCopyParam);
  587. CopyParam->TransferMode = tmBinary;
  588. FCopyParamList->Add(LoadStr(COPY_PARAM_PRESET_BINARY), CopyParam, NULL);
  589. CopyParam = new TCopyParamType(FDefaultCopyParam);
  590. CopyParam->NewerOnly = true;
  591. FCopyParamList->Add(LoadStr(COPY_PARAM_NEWER_ONLY), CopyParam, NULL);
  592. CopyParam = new TCopyParamType(FDefaultCopyParam);
  593. CopyParam->IncludeFileMask = TFileMasks(FORMAT(L"%s */", (IncludeExcludeFileMasksDelimiter)));
  594. FCopyParamList->Add(LoadStr(COPY_PARAM_PRESET_EXCLUDE_ALL_DIR), CopyParam, NULL);
  595. }
  596. FCopyParamList->Reset();
  597. }
  598. }
  599. //---------------------------------------------------------------------------
  600. void __fastcall TGUIConfiguration::UpdateStaticUsage()
  601. {
  602. TConfiguration::UpdateStaticUsage();
  603. Usage->Set(L"CopyParamsCount", (FCopyParamListDefaults ? 0 : FCopyParamList->Count));
  604. Usage->Set(L"Putty", ExtractProgramName(PuttyPath));
  605. }
  606. //---------------------------------------------------------------------------
  607. // duplicated from core\configuration.cpp
  608. #define BLOCK(KEY, CANCREATE, BLOCK) \
  609. if (Storage->OpenSubKeyPath(KEY, CANCREATE)) try { BLOCK } __finally { Storage->CloseSubKeyPath(); }
  610. #define KEY(TYPE, VAR) KEYEX(TYPE, VAR, PropertyToKey(TEXT(#VAR)))
  611. #define REGCONFIG(CANCREATE) \
  612. BLOCK(L"Interface", CANCREATE, \
  613. KEY(Bool, ContinueOnError); \
  614. KEY(Bool, ConfirmCommandSession); \
  615. KEY(Integer, SynchronizeParams); \
  616. KEY(Integer, SynchronizeOptions); \
  617. KEY(Integer, SynchronizeModeAuto); \
  618. KEY(Integer, SynchronizeMode); \
  619. KEY(Integer, MaxWatchDirectories); \
  620. KEY(Integer, QueueTransfersLimit); \
  621. KEY(Bool, QueueBootstrap); \
  622. KEY(Integer, QueueKeepDoneItems); \
  623. KEY(Integer, QueueKeepDoneItemsFor); \
  624. KEY(Bool, QueueAutoPopup); \
  625. KEYEX(Bool, SessionRememberPassword, L"QueueRememberPassword"); \
  626. KEY(String, PuttySession); \
  627. KEY(String, PuttyPath); \
  628. KEY(Bool, PuttyPassword); \
  629. KEY(Bool, TelnetForFtpInPutty); \
  630. KEY(DateTime, IgnoreCancelBeforeFinish); \
  631. KEY(Bool, BeepOnFinish); \
  632. KEY(DateTime, BeepOnFinishAfter); \
  633. KEY(String, BeepSound); \
  634. KEY(Integer, KeepUpToDateChangeDelay); \
  635. KEY(String, ChecksumAlg); \
  636. KEY(Integer, SessionReopenAutoIdle); \
  637. ); \
  638. //---------------------------------------------------------------------------
  639. bool __fastcall TGUIConfiguration::DoSaveCopyParam(THierarchicalStorage * Storage, const TCopyParamType * CopyParam, const TCopyParamType * Defaults)
  640. {
  641. bool Result = Storage->OpenSubKeyPath(L"Interface\\CopyParam", true);
  642. if (Result)
  643. {
  644. CopyParam->Save(Storage, Defaults);
  645. }
  646. return Result;
  647. }
  648. //---------------------------------------------------------------------------
  649. void __fastcall TGUIConfiguration::SaveData(THierarchicalStorage * Storage, bool All)
  650. {
  651. TConfiguration::SaveData(Storage, All);
  652. // duplicated from core\configuration.cpp
  653. #define KEYEX(TYPE, VAR, NAME) Storage->Write ## TYPE(NAME, VAR)
  654. REGCONFIG(true);
  655. #undef KEYEX
  656. if (DoSaveCopyParam(Storage, &FDefaultCopyParam, NULL))
  657. try
  658. {
  659. FDefaultCopyParam.Save(Storage);
  660. if (FCopyParamListDefaults)
  661. {
  662. DebugAssert(!FCopyParamList->Modified);
  663. Storage->WriteInteger(L"CopyParamList", -1);
  664. }
  665. else if (All || FCopyParamList->Modified)
  666. {
  667. Storage->WriteInteger(L"CopyParamList", FCopyParamList->Count);
  668. FCopyParamList->Save(Storage);
  669. }
  670. }
  671. __finally
  672. {
  673. Storage->CloseSubKeyPath();
  674. }
  675. if (Storage->OpenSubKeyPath(L"Interface\\NewDirectory2", true))
  676. try
  677. {
  678. FNewDirectoryProperties.Save(Storage);
  679. }
  680. __finally
  681. {
  682. Storage->CloseSubKeyPath();
  683. }
  684. }
  685. //---------------------------------------------------------------------------
  686. bool __fastcall TGUIConfiguration::LoadCopyParam(THierarchicalStorage * Storage, TCopyParamType * CopyParam)
  687. {
  688. bool Result =
  689. Storage->OpenSubKeyPath(L"Interface\\CopyParam", false);
  690. if (Result)
  691. {
  692. try
  693. {
  694. CopyParam->Load(Storage);
  695. }
  696. catch (...)
  697. {
  698. Storage->CloseSubKeyPath();
  699. throw;
  700. }
  701. }
  702. return Result;
  703. }
  704. //---------------------------------------------------------------------------
  705. void __fastcall TGUIConfiguration::LoadDefaultCopyParam(THierarchicalStorage * Storage)
  706. {
  707. FDefaultCopyParam.Load(Storage);
  708. }
  709. //---------------------------------------------------------------------------
  710. void __fastcall TGUIConfiguration::LoadData(THierarchicalStorage * Storage)
  711. {
  712. TConfiguration::LoadData(Storage);
  713. // duplicated from core\configuration.cpp
  714. #define KEYEX(TYPE, VAR, NAME) VAR = Storage->Read ## TYPE(NAME, VAR)
  715. #pragma warn -eas
  716. REGCONFIG(false);
  717. #pragma warn +eas
  718. #undef KEYEX
  719. // FDefaultCopyParam must be loaded before eventual setting defaults for CopyParamList
  720. if (LoadCopyParam(Storage, &FDefaultCopyParam))
  721. try
  722. {
  723. int CopyParamListCount = Storage->ReadInteger(L"CopyParamList", -1);
  724. FCopyParamListDefaults = (CopyParamListCount < 0);
  725. if (!FCopyParamListDefaults)
  726. {
  727. FCopyParamList->Clear();
  728. FCopyParamList->Load(Storage, CopyParamListCount);
  729. }
  730. else if (FCopyParamList->Modified)
  731. {
  732. FCopyParamList->Clear();
  733. FCopyParamListDefaults = false;
  734. }
  735. FCopyParamList->Reset();
  736. }
  737. __finally
  738. {
  739. Storage->CloseSubKeyPath();
  740. }
  741. // Make it compatible with versions prior to 3.7.1 that have not saved PuttyPath
  742. // with quotes. First check for absence of quotes.
  743. // Add quotes either if the path is set to default putty path (even if it does
  744. // not exists) or when the path points to existing file (so there are no parameters
  745. // yet in the string). Note that FileExists may display error dialog, but as
  746. // it should be called only for custom users path, let's expect that the user
  747. // can take care of it.
  748. if ((FPuttyPath.SubString(1, 1) != L"\"") &&
  749. (IsPathToSameFile(ExpandEnvironmentVariables(FPuttyPath), FDefaultPuttyPathOnly) ||
  750. FileExists(ApiPath(ExpandEnvironmentVariables(FPuttyPath)))))
  751. {
  752. FPuttyPath = FormatCommand(FPuttyPath, L"");
  753. }
  754. if (Storage->OpenSubKeyPath(L"Interface\\NewDirectory2", false))
  755. try
  756. {
  757. FNewDirectoryProperties.Load(Storage);
  758. }
  759. __finally
  760. {
  761. Storage->CloseSubKeyPath();
  762. }
  763. }
  764. //---------------------------------------------------------------------------
  765. void __fastcall TGUIConfiguration::Saved()
  766. {
  767. TConfiguration::Saved();
  768. FCopyParamList->Reset();
  769. }
  770. //---------------------------------------------------------------------------
  771. //---------------------------------------------------------------------------
  772. UnicodeString __fastcall TGUIConfiguration::GetTranslationModule(const UnicodeString & Path)
  773. {
  774. UnicodeString SubPath = AddTranslationsSubFolder(Path);
  775. UnicodeString Result;
  776. // Prefer the SubPath. Default to SubPath.
  777. if (FileExists(Path) && !FileExists(SubPath))
  778. {
  779. Result = Path;
  780. }
  781. else
  782. {
  783. Result = SubPath;
  784. }
  785. return Result;
  786. }
  787. //---------------------------------------------------------------------------
  788. UnicodeString __fastcall TGUIConfiguration::AddTranslationsSubFolder(const UnicodeString & Path)
  789. {
  790. return
  791. IncludeTrailingBackslash(IncludeTrailingBackslash(ExtractFilePath(Path)) + TranslationsSubFolder) +
  792. ExtractFileName(Path);
  793. }
  794. //---------------------------------------------------------------------------
  795. HINSTANCE __fastcall TGUIConfiguration::LoadNewResourceModule(LCID ALocale,
  796. UnicodeString & FileName)
  797. {
  798. UnicodeString LibraryFileName;
  799. HINSTANCE NewInstance = 0;
  800. LCID AInternalLocale = InternalLocale();
  801. bool Internal = (ALocale == AInternalLocale);
  802. DWORD PrimaryLang = PRIMARYLANGID(ALocale);
  803. if (!Internal)
  804. {
  805. UnicodeString Module;
  806. UnicodeString LocaleName;
  807. Module = ModuleFileName();
  808. if ((ALocale & AdditionaLanguageMask) != AdditionaLanguageMask)
  809. {
  810. wchar_t LocaleStr[4];
  811. GetLocaleInfo(ALocale, LOCALE_SABBREVLANGNAME, LocaleStr, LENOF(LocaleStr));
  812. LocaleName = LocaleStr;
  813. DebugAssert(!LocaleName.IsEmpty());
  814. }
  815. else
  816. {
  817. LocaleName = AdditionaLanguagePrefix +
  818. char(ALocale & ~AdditionaLanguageMask);
  819. }
  820. Module = ChangeFileExt(Module, UnicodeString(L".") + LocaleName);
  821. // Look for a potential language/country translation
  822. UnicodeString ModulePath = GetTranslationModule(Module);
  823. NewInstance = LoadLibraryEx(ModulePath.c_str(), 0, LOAD_LIBRARY_AS_DATAFILE);
  824. if (NewInstance)
  825. {
  826. LibraryFileName = ModulePath;
  827. }
  828. else
  829. {
  830. DWORD SubLang = SUBLANGID(ALocale);
  831. DebugAssert(SUBLANG_DEFAULT == SUBLANG_CHINESE_TRADITIONAL);
  832. // Finally look for a language-only translation.
  833. // But for Chinese, never use "traditional" (what is the "default" Chinese), if we want "Simplified"
  834. // (the same what Inno Setup does)
  835. if ((PrimaryLang != LANG_CHINESE) ||
  836. (SubLang == SUBLANG_CHINESE_TRADITIONAL))
  837. {
  838. Module.SetLength(Module.Length() - 1);
  839. ModulePath = GetTranslationModule(Module);
  840. NewInstance = LoadLibraryEx(ModulePath.c_str(), 0, LOAD_LIBRARY_AS_DATAFILE);
  841. if (NewInstance)
  842. {
  843. LibraryFileName = ModulePath;
  844. }
  845. }
  846. }
  847. }
  848. // If the locale is non-US English and we do not have that translation (and it's unlikely we ever have),
  849. // treat it as if it were US English.
  850. if (!NewInstance && !Internal && (PrimaryLang == static_cast<DWORD>(PRIMARYLANGID(AInternalLocale))))
  851. {
  852. Internal = true;
  853. }
  854. if (!NewInstance && !Internal)
  855. {
  856. throw Exception(FMTLOAD(LOCALE_LOAD_ERROR, (int(ALocale))));
  857. }
  858. else
  859. {
  860. if (Internal)
  861. {
  862. NewInstance = HInstance;
  863. }
  864. }
  865. FileName = LibraryFileName;
  866. return NewInstance;
  867. }
  868. //---------------------------------------------------------------------------
  869. LCID __fastcall TGUIConfiguration::InternalLocale()
  870. {
  871. LCID Result;
  872. if (GetTranslationCount(ApplicationInfo) > 0)
  873. {
  874. TTranslation Translation;
  875. Translation = GetTranslation(ApplicationInfo, 0);
  876. Result = MAKELANGID(PRIMARYLANGID(Translation.Language), SUBLANG_DEFAULT);
  877. }
  878. else
  879. {
  880. DebugFail();
  881. Result = 0;
  882. }
  883. return Result;
  884. }
  885. //---------------------------------------------------------------------------
  886. LCID __fastcall TGUIConfiguration::GetLocale()
  887. {
  888. return FLocale;
  889. }
  890. //---------------------------------------------------------------------------
  891. void __fastcall TGUIConfiguration::SetLocale(LCID value)
  892. {
  893. if (Locale != value)
  894. {
  895. SetLocaleInternal(value, false, false);
  896. }
  897. }
  898. //---------------------------------------------------------------------------
  899. void __fastcall TGUIConfiguration::SetLocaleSafe(LCID value)
  900. {
  901. if (Locale != value)
  902. {
  903. SetLocaleInternal(value, true, false);
  904. }
  905. }
  906. //---------------------------------------------------------------------------
  907. UnicodeString __fastcall TGUIConfiguration::GetAppliedLocaleHex()
  908. {
  909. return IntToHex(__int64(AppliedLocale), 4);
  910. }
  911. //---------------------------------------------------------------------------
  912. int __fastcall TGUIConfiguration::GetResourceModuleCompleteness(HINSTANCE /*Module*/)
  913. {
  914. return 100;
  915. }
  916. //---------------------------------------------------------------------------
  917. bool __fastcall TGUIConfiguration::IsTranslationComplete(HINSTANCE /*Module*/)
  918. {
  919. return true;
  920. }
  921. //---------------------------------------------------------------------------
  922. void __fastcall TGUIConfiguration::SetLocaleInternal(LCID value, bool Safe, bool CompleteOnly)
  923. {
  924. LCID L = value;
  925. if (L == NULL)
  926. {
  927. L = GetUserDefaultUILanguage();
  928. }
  929. HINSTANCE Module = NULL;
  930. UnicodeString FileName;
  931. try
  932. {
  933. Module = LoadNewResourceModule(L, FileName);
  934. DebugAssert(Module != NULL);
  935. if (CompleteOnly && !IsTranslationComplete(Module))
  936. {
  937. Abort();
  938. }
  939. }
  940. catch (...)
  941. {
  942. if (Module != NULL)
  943. {
  944. FreeResourceModule(Module);
  945. Module = NULL;
  946. }
  947. if (Safe)
  948. {
  949. // ignore any exception while loading locale
  950. }
  951. else
  952. {
  953. throw;
  954. }
  955. }
  956. if (Module != NULL)
  957. {
  958. FLocale = value;
  959. if (CanApplyLocaleImmediately)
  960. {
  961. SetAppliedLocale(L, FileName);
  962. SetResourceModule(Module);
  963. }
  964. }
  965. }
  966. //---------------------------------------------------------------------------
  967. bool __fastcall TGUIConfiguration::GetCanApplyLocaleImmediately()
  968. {
  969. return
  970. (Screen->FormCount == 0) &&
  971. (Screen->DataModuleCount == 0);
  972. }
  973. //---------------------------------------------------------------------------
  974. bool __fastcall TGUIConfiguration::UsingInternalTranslation()
  975. {
  976. return FLocaleModuleName.IsEmpty();
  977. }
  978. //---------------------------------------------------------------------------
  979. UnicodeString __fastcall TGUIConfiguration::AppliedLocaleCopyright()
  980. {
  981. UnicodeString Result;
  982. if (UsingInternalTranslation())
  983. {
  984. DebugFail(); // we do not expect to get called with internal locale
  985. Result = UnicodeString();
  986. }
  987. else
  988. {
  989. Result = GetFileFileInfoString(L"LegalCopyright", FLocaleModuleName);
  990. }
  991. return Result;
  992. }
  993. //---------------------------------------------------------------------------
  994. UnicodeString __fastcall TGUIConfiguration::AppliedLocaleVersion()
  995. {
  996. UnicodeString Result;
  997. if (UsingInternalTranslation())
  998. {
  999. // noop
  1000. }
  1001. else
  1002. {
  1003. Result = GetFileVersion(FLocaleModuleName);
  1004. }
  1005. return Result;
  1006. }
  1007. //---------------------------------------------------------------------------
  1008. void __fastcall TGUIConfiguration::SetAppliedLocale(LCID AppliedLocale, const UnicodeString & LocaleModuleName)
  1009. {
  1010. FAppliedLocale = AppliedLocale;
  1011. FLocaleModuleName = LocaleModuleName;
  1012. }
  1013. //---------------------------------------------------------------------------
  1014. void __fastcall TGUIConfiguration::FreeResourceModule(HANDLE Instance)
  1015. {
  1016. TLibModule * MainModule = FindModule(HInstance);
  1017. if ((unsigned)Instance != MainModule->Instance)
  1018. {
  1019. FreeLibrary(static_cast<HMODULE>(Instance));
  1020. }
  1021. }
  1022. //---------------------------------------------------------------------------
  1023. HANDLE __fastcall TGUIConfiguration::ChangeToDefaultResourceModule()
  1024. {
  1025. return ChangeResourceModule(NULL);
  1026. }
  1027. //---------------------------------------------------------------------------
  1028. HANDLE __fastcall TGUIConfiguration::ChangeResourceModule(HANDLE Instance)
  1029. {
  1030. if (Instance == NULL)
  1031. {
  1032. Instance = HInstance;
  1033. }
  1034. TLibModule * MainModule = FindModule(HInstance);
  1035. HANDLE Result = (HANDLE)MainModule->ResInstance;
  1036. MainModule->ResInstance = (unsigned)Instance;
  1037. CoreSetResourceModule(Instance);
  1038. return Result;
  1039. }
  1040. //---------------------------------------------------------------------------
  1041. HANDLE __fastcall TGUIConfiguration::GetResourceModule()
  1042. {
  1043. return (HANDLE)FindModule(HInstance)->ResInstance;
  1044. }
  1045. //---------------------------------------------------------------------------
  1046. void __fastcall TGUIConfiguration::SetResourceModule(HINSTANCE Instance)
  1047. {
  1048. HANDLE PrevHandle = ChangeResourceModule(Instance);
  1049. FreeResourceModule(PrevHandle);
  1050. DefaultLocalized();
  1051. }
  1052. //---------------------------------------------------------------------------
  1053. void __fastcall TGUIConfiguration::FindLocales(const UnicodeString & LocalesMask, TStrings * Exts, UnicodeString & LocalesExts)
  1054. {
  1055. int FindAttrs = faReadOnly | faArchive;
  1056. TSearchRecOwned SearchRec;
  1057. bool Found = (FindFirstUnchecked(LocalesMask, FindAttrs, SearchRec) == 0);
  1058. while (Found)
  1059. {
  1060. UnicodeString Ext = ExtractFileExt(SearchRec.Name).UpperCase();
  1061. // DLL is a remnant from times the .NET assembly was winscp.dll, not winscpnet.dll
  1062. if ((Ext.Length() >= 3) && (Ext != L".EXE") && (Ext != L".COM") &&
  1063. (Ext != L".DLL") && (Ext != L".INI") && (Ext != L".MAP"))
  1064. {
  1065. Ext = Ext.SubString(2, Ext.Length() - 1);
  1066. LocalesExts += Ext;
  1067. Exts->Add(Ext);
  1068. }
  1069. Found = (FindNextChecked(SearchRec) == 0);
  1070. }
  1071. }
  1072. //---------------------------------------------------------------------------
  1073. void __fastcall TGUIConfiguration::AddLocale(LCID Locale, const UnicodeString & Name)
  1074. {
  1075. std::unique_ptr<TLocaleInfo> LocaleInfo(new TLocaleInfo());
  1076. LocaleInfo->Locale = Locale;
  1077. LocaleInfo->Name = Name;
  1078. try
  1079. {
  1080. UnicodeString FileName;
  1081. HINSTANCE Module = LoadNewResourceModule(Locale, FileName);
  1082. try
  1083. {
  1084. LocaleInfo->Completeness = GetResourceModuleCompleteness(Module);
  1085. }
  1086. __finally
  1087. {
  1088. FreeResourceModule(Module);
  1089. }
  1090. }
  1091. catch (...)
  1092. {
  1093. LocaleInfo->Completeness = -1;
  1094. }
  1095. FLocales->Add(LocaleInfo.release());
  1096. }
  1097. //---------------------------------------------------------------------------
  1098. int __fastcall TGUIConfiguration::LocalesCompare(void * Item1, void * Item2)
  1099. {
  1100. TLocaleInfo * LocaleInfo1 = static_cast<TLocaleInfo *>(Item1);
  1101. TLocaleInfo * LocaleInfo2 = static_cast<TLocaleInfo *>(Item2);
  1102. return CompareText(LocaleInfo1->Name, LocaleInfo2->Name);
  1103. }
  1104. //---------------------------------------------------------------------------
  1105. TObjectList * __fastcall TGUIConfiguration::GetLocales()
  1106. {
  1107. UnicodeString LocalesMask = ChangeFileExt(ModuleFileName(), L".*");
  1108. UnicodeString SubLocalesMask = AddTranslationsSubFolder(LocalesMask);
  1109. UnicodeString LocalesExts;
  1110. std::unique_ptr<TStringList> Exts(CreateSortedStringList());
  1111. FindLocales(SubLocalesMask, Exts.get(), LocalesExts);
  1112. FindLocales(LocalesMask, Exts.get(), LocalesExts);
  1113. if (FLastLocalesExts != LocalesExts)
  1114. {
  1115. FLastLocalesExts = LocalesExts;
  1116. FLocales->Clear();
  1117. TLanguages * Langs = Languages();
  1118. int Count = Langs->Count;
  1119. int Index = -1;
  1120. while (Index < Count)
  1121. {
  1122. LCID Locale;
  1123. if (Index >= 0)
  1124. {
  1125. Locale = Langs->LocaleID[Index];
  1126. DWORD SubLang = SUBLANGID(Locale);
  1127. int Ext = Exts->IndexOf(Langs->Ext[Index]);
  1128. if ((Ext >= 0) && (Exts->Objects[Ext] == NULL))
  1129. {
  1130. // noop
  1131. }
  1132. else if (SubLang == SUBLANG_DEFAULT)
  1133. {
  1134. Ext = Exts->IndexOf(Langs->Ext[Index].SubString(1, 2));
  1135. if ((Ext >= 0) && (Exts->Objects[Ext] == NULL))
  1136. {
  1137. Locale = MAKELANGID(PRIMARYLANGID(Locale), SUBLANG_DEFAULT);
  1138. }
  1139. }
  1140. if (Ext >= 0)
  1141. {
  1142. Exts->Objects[Ext] = reinterpret_cast<TObject*>(Locale);
  1143. }
  1144. else
  1145. {
  1146. Locale = 0;
  1147. }
  1148. }
  1149. else
  1150. {
  1151. Locale = InternalLocale();
  1152. }
  1153. if (Locale)
  1154. {
  1155. wchar_t LocaleStr[255];
  1156. GetLocaleInfo(Locale, LOCALE_SENGLANGUAGE,
  1157. LocaleStr, LENOF(LocaleStr));
  1158. UnicodeString Name = LocaleStr;
  1159. Name += L" - ";
  1160. // LOCALE_SNATIVELANGNAME
  1161. GetLocaleInfo(Locale, LOCALE_SLANGUAGE,
  1162. LocaleStr, LENOF(LocaleStr));
  1163. Name += LocaleStr;
  1164. AddLocale(Locale, Name);
  1165. }
  1166. Index++;
  1167. }
  1168. for (int Index = 0; Index < Exts->Count; Index++)
  1169. {
  1170. if ((Exts->Objects[Index] == NULL) &&
  1171. (Exts->Strings[Index].Length() == 3) &&
  1172. SameText(Exts->Strings[Index].SubString(1, 2), AdditionaLanguagePrefix))
  1173. {
  1174. UnicodeString ModulePath = ChangeFileExt(ModuleFileName(), UnicodeString(L".") + Exts->Strings[Index]);
  1175. ModulePath = GetTranslationModule(ModulePath);
  1176. UnicodeString LangName = GetFileFileInfoString(L"LangName", ModulePath);
  1177. if (!LangName.IsEmpty())
  1178. {
  1179. AddLocale(AdditionaLanguageMask + Exts->Strings[Index][3], LangName);
  1180. }
  1181. }
  1182. }
  1183. FLocales->Sort(LocalesCompare);
  1184. }
  1185. return FLocales;
  1186. }
  1187. //---------------------------------------------------------------------------
  1188. void __fastcall TGUIConfiguration::SetDefaultCopyParam(const TGUICopyParamType & value)
  1189. {
  1190. FDefaultCopyParam.Assign(&value);
  1191. Changed();
  1192. }
  1193. //---------------------------------------------------------------------------
  1194. bool __fastcall TGUIConfiguration::GetRememberPassword()
  1195. {
  1196. bool Result = SessionRememberPassword || PuttyPassword;
  1197. if (!Result)
  1198. {
  1199. try
  1200. {
  1201. TRemoteCustomCommand RemoteCustomCommand;
  1202. TInteractiveCustomCommand InteractiveCustomCommand(&RemoteCustomCommand);
  1203. UnicodeString APuttyPath = InteractiveCustomCommand.Complete(PuttyPath, false);
  1204. Result = RemoteCustomCommand.IsPasswordCommand(PuttyPath);
  1205. }
  1206. catch (...)
  1207. {
  1208. // noop
  1209. }
  1210. }
  1211. return Result;
  1212. }
  1213. //---------------------------------------------------------------------------
  1214. const TCopyParamList * __fastcall TGUIConfiguration::GetCopyParamList()
  1215. {
  1216. return FCopyParamList;
  1217. }
  1218. //---------------------------------------------------------------------------
  1219. void __fastcall TGUIConfiguration::SetCopyParamList(const TCopyParamList * value)
  1220. {
  1221. if (!(*FCopyParamList == *value))
  1222. {
  1223. *FCopyParamList = *value;
  1224. FCopyParamListDefaults = false;
  1225. Changed();
  1226. }
  1227. }
  1228. //---------------------------------------------------------------------------
  1229. int __fastcall TGUIConfiguration::GetCopyParamIndex()
  1230. {
  1231. int Result;
  1232. if (FCopyParamCurrent.IsEmpty())
  1233. {
  1234. Result = -1;
  1235. }
  1236. else
  1237. {
  1238. Result = FCopyParamList->IndexOfName(FCopyParamCurrent);
  1239. }
  1240. return Result;
  1241. }
  1242. //---------------------------------------------------------------------------
  1243. void __fastcall TGUIConfiguration::SetCopyParamIndex(int value)
  1244. {
  1245. UnicodeString Name;
  1246. if (value < 0)
  1247. {
  1248. Name = L"";
  1249. }
  1250. else
  1251. {
  1252. Name = FCopyParamList->Names[value];
  1253. }
  1254. CopyParamCurrent = Name;
  1255. }
  1256. //---------------------------------------------------------------------------
  1257. void __fastcall TGUIConfiguration::SetCopyParamCurrent(UnicodeString value)
  1258. {
  1259. SET_CONFIG_PROPERTY(CopyParamCurrent);
  1260. }
  1261. //---------------------------------------------------------------------------
  1262. TGUICopyParamType __fastcall TGUIConfiguration::GetCurrentCopyParam()
  1263. {
  1264. return CopyParamPreset[CopyParamCurrent];
  1265. }
  1266. //---------------------------------------------------------------------------
  1267. TGUICopyParamType __fastcall TGUIConfiguration::GetCopyParamPreset(UnicodeString Name)
  1268. {
  1269. TGUICopyParamType Result = FDefaultCopyParam;
  1270. if (!Name.IsEmpty())
  1271. {
  1272. int Index = FCopyParamList->IndexOfName(Name);
  1273. DebugAssert(Index >= 0);
  1274. if (Index >= 0)
  1275. {
  1276. const TCopyParamType * Preset = FCopyParamList->CopyParams[Index];
  1277. DebugAssert(Preset != NULL);
  1278. Result.Assign(Preset); // overwrite all but GUI options
  1279. // reset all options known not to be configurable per-preset
  1280. // kind of hack
  1281. Result.ResumeSupport = FDefaultCopyParam.ResumeSupport;
  1282. Result.ResumeThreshold = FDefaultCopyParam.ResumeThreshold;
  1283. Result.LocalInvalidChars = FDefaultCopyParam.LocalInvalidChars;
  1284. }
  1285. }
  1286. return Result;
  1287. }
  1288. //---------------------------------------------------------------------------
  1289. bool __fastcall TGUIConfiguration::GetHasCopyParamPreset(UnicodeString Name)
  1290. {
  1291. return Name.IsEmpty() || (FCopyParamList->IndexOfName(Name) >= 0);
  1292. }
  1293. //---------------------------------------------------------------------------
  1294. void __fastcall TGUIConfiguration::SetNewDirectoryProperties(
  1295. const TRemoteProperties & value)
  1296. {
  1297. SET_CONFIG_PROPERTY(NewDirectoryProperties);
  1298. }
  1299. //---------------------------------------------------------------------------
  1300. void __fastcall TGUIConfiguration::SetQueueTransfersLimit(int value)
  1301. {
  1302. SET_CONFIG_PROPERTY(QueueTransfersLimit);
  1303. }
  1304. //---------------------------------------------------------------------------
  1305. void __fastcall TGUIConfiguration::SetQueueBootstrap(bool value)
  1306. {
  1307. SET_CONFIG_PROPERTY(QueueBootstrap);
  1308. }
  1309. //---------------------------------------------------------------------------
  1310. void __fastcall TGUIConfiguration::SetQueueKeepDoneItems(bool value)
  1311. {
  1312. SET_CONFIG_PROPERTY(QueueKeepDoneItems);
  1313. }
  1314. //---------------------------------------------------------------------------
  1315. void __fastcall TGUIConfiguration::SetQueueKeepDoneItemsFor(int value)
  1316. {
  1317. SET_CONFIG_PROPERTY(QueueKeepDoneItemsFor);
  1318. }
  1319. //---------------------------------------------------------------------
  1320. TStoredSessionList * __fastcall TGUIConfiguration::SelectPuttySessionsForImport(
  1321. TStoredSessionList * Sessions, UnicodeString & Error)
  1322. {
  1323. std::unique_ptr<TStoredSessionList> ImportSessionList(new TStoredSessionList(true));
  1324. ImportSessionList->DefaultSettings = Sessions->DefaultSettings;
  1325. std::unique_ptr<TRegistryStorage> Storage(new TRegistryStorage(PuttySessionsKey));
  1326. Storage->ConfigureForPutty();
  1327. if (Storage->OpenRootKey(false))
  1328. {
  1329. ImportSessionList->Load(Storage.get(), false, true, true);
  1330. }
  1331. TSessionData * PuttySessionData =
  1332. (TSessionData *)ImportSessionList->FindByName(PuttySession);
  1333. if (PuttySessionData != NULL)
  1334. {
  1335. ImportSessionList->Remove(PuttySessionData);
  1336. }
  1337. if (ImportSessionList->Count > 0)
  1338. {
  1339. ImportSessionList->SelectSessionsToImport(Sessions, true);
  1340. }
  1341. else
  1342. {
  1343. Error = FMTLOAD(PUTTY_NO_SITES, (PuttySessionsKey));
  1344. }
  1345. return ImportSessionList.release();
  1346. }
  1347. //---------------------------------------------------------------------
  1348. bool __fastcall TGUIConfiguration::AnyPuttySessionForImport(TStoredSessionList * Sessions)
  1349. {
  1350. try
  1351. {
  1352. UnicodeString Error;
  1353. std::unique_ptr<TStoredSessionList> Sesssions(SelectPuttySessionsForImport(Sessions, Error));
  1354. return (Sesssions->Count > 0);
  1355. }
  1356. catch (...)
  1357. {
  1358. return false;
  1359. }
  1360. }
  1361. //---------------------------------------------------------------------------