GUIConfiguration.cpp 47 KB

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