GUIConfiguration.cpp 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412
  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.MatchesFileName(Value);
  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. FQueueBootstrap = false;
  548. FQueueKeepDoneItems = true;
  549. FQueueKeepDoneItemsFor = 15;
  550. FQueueAutoPopup = true;
  551. FSessionRememberPassword = true;
  552. UnicodeString ProgramsFolder;
  553. SpecialFolderLocation(CSIDL_PROGRAM_FILES, ProgramsFolder);
  554. FDefaultPuttyPathOnly = IncludeTrailingBackslash(ProgramsFolder) + L"PuTTY\\" + OriginalPuttyExecutable;
  555. FDefaultPuttyPath = L"%ProgramFiles%\\PuTTY\\" + OriginalPuttyExecutable;
  556. FPuttyPath = FormatCommand(FDefaultPuttyPath, L"");
  557. FPuttyPassword = false;
  558. FTelnetForFtpInPutty = true;
  559. FPuttySession = L"WinSCP temporary session";
  560. FBeepOnFinish = false;
  561. FBeepOnFinishAfter = TDateTime(0, 0, 30, 0);
  562. FBeepSound = L"SystemDefault";
  563. FCopyParamCurrent = L"";
  564. FKeepUpToDateChangeDelay = 500;
  565. FChecksumAlg = L"sha1";
  566. FSessionReopenAutoIdle = 9000;
  567. FNewDirectoryProperties.Default();
  568. FNewDirectoryProperties.Rights = TRights::rfDefault | TRights::rfExec;
  569. }
  570. //---------------------------------------------------------------------------
  571. void __fastcall TGUIConfiguration::DefaultLocalized()
  572. {
  573. if (FCopyParamListDefaults)
  574. {
  575. FCopyParamList->Clear();
  576. // guard against "empty resource string" from obsolete traslations
  577. // (DefaultLocalized is called for the first time before detection of
  578. // obsolete translations)
  579. if (!LoadStr(COPY_PARAM_PRESET_ASCII).IsEmpty())
  580. {
  581. TCopyParamType * CopyParam;
  582. CopyParam = new TCopyParamType(FDefaultCopyParam);
  583. CopyParam->TransferMode = tmAscii;
  584. FCopyParamList->Add(LoadStr(COPY_PARAM_PRESET_ASCII), CopyParam, NULL);
  585. CopyParam = new TCopyParamType(FDefaultCopyParam);
  586. CopyParam->TransferMode = tmBinary;
  587. FCopyParamList->Add(LoadStr(COPY_PARAM_PRESET_BINARY), CopyParam, NULL);
  588. CopyParam = new TCopyParamType(FDefaultCopyParam);
  589. CopyParam->NewerOnly = true;
  590. FCopyParamList->Add(LoadStr(COPY_PARAM_NEWER_ONLY), CopyParam, NULL);
  591. CopyParam = new TCopyParamType(FDefaultCopyParam);
  592. CopyParam->IncludeFileMask = TFileMasks(FORMAT(L"%s */", (IncludeExcludeFileMasksDelimiter)));
  593. FCopyParamList->Add(LoadStr(COPY_PARAM_PRESET_EXCLUDE_ALL_DIR), CopyParam, NULL);
  594. }
  595. FCopyParamList->Reset();
  596. }
  597. }
  598. //---------------------------------------------------------------------------
  599. void __fastcall TGUIConfiguration::UpdateStaticUsage()
  600. {
  601. TConfiguration::UpdateStaticUsage();
  602. Usage->Set(L"CopyParamsCount", (FCopyParamListDefaults ? 0 : FCopyParamList->Count));
  603. Usage->Set(L"Putty", ExtractProgramName(PuttyPath));
  604. }
  605. //---------------------------------------------------------------------------
  606. // duplicated from core\configuration.cpp
  607. #define BLOCK(KEY, CANCREATE, BLOCK) \
  608. if (Storage->OpenSubKeyPath(KEY, CANCREATE)) try { BLOCK } __finally { Storage->CloseSubKeyPath(); }
  609. #define KEY(TYPE, VAR) KEYEX(TYPE, VAR, PropertyToKey(TEXT(#VAR)))
  610. #define REGCONFIG(CANCREATE) \
  611. BLOCK(L"Interface", CANCREATE, \
  612. KEY(Bool, ContinueOnError); \
  613. KEY(Bool, ConfirmCommandSession); \
  614. KEY(Integer, SynchronizeParams); \
  615. KEY(Integer, SynchronizeOptions); \
  616. KEY(Integer, SynchronizeModeAuto); \
  617. KEY(Integer, SynchronizeMode); \
  618. KEY(Integer, MaxWatchDirectories); \
  619. KEY(Bool, QueueBootstrap); \
  620. KEY(Integer, QueueKeepDoneItems); \
  621. KEY(Integer, QueueKeepDoneItemsFor); \
  622. KEY(Bool, QueueAutoPopup); \
  623. KEYEX(Bool, SessionRememberPassword, L"QueueRememberPassword"); \
  624. KEY(String, PuttySession); \
  625. KEY(String, PuttyPath); \
  626. KEY(Bool, PuttyPassword); \
  627. KEY(Bool, TelnetForFtpInPutty); \
  628. KEY(DateTime, IgnoreCancelBeforeFinish); \
  629. KEY(Bool, BeepOnFinish); \
  630. KEY(DateTime, BeepOnFinishAfter); \
  631. KEY(String, BeepSound); \
  632. KEY(Integer, KeepUpToDateChangeDelay); \
  633. KEY(String, ChecksumAlg); \
  634. KEY(Integer, SessionReopenAutoIdle); \
  635. ); \
  636. //---------------------------------------------------------------------------
  637. bool __fastcall TGUIConfiguration::DoSaveCopyParam(THierarchicalStorage * Storage, const TCopyParamType * CopyParam, const TCopyParamType * Defaults)
  638. {
  639. bool Result = Storage->OpenSubKeyPath(L"Interface\\CopyParam", true);
  640. if (Result)
  641. {
  642. CopyParam->Save(Storage, Defaults);
  643. }
  644. return Result;
  645. }
  646. //---------------------------------------------------------------------------
  647. void __fastcall TGUIConfiguration::SaveData(THierarchicalStorage * Storage, bool All)
  648. {
  649. TConfiguration::SaveData(Storage, All);
  650. // duplicated from core\configuration.cpp
  651. #define KEYEX(TYPE, VAR, NAME) Storage->Write ## TYPE(NAME, VAR)
  652. REGCONFIG(true);
  653. #undef KEYEX
  654. if (DoSaveCopyParam(Storage, &FDefaultCopyParam, NULL))
  655. try
  656. {
  657. FDefaultCopyParam.Save(Storage);
  658. if (FCopyParamListDefaults)
  659. {
  660. DebugAssert(!FCopyParamList->Modified);
  661. Storage->WriteInteger(L"CopyParamList", -1);
  662. }
  663. else if (All || FCopyParamList->Modified)
  664. {
  665. Storage->WriteInteger(L"CopyParamList", FCopyParamList->Count);
  666. FCopyParamList->Save(Storage);
  667. }
  668. }
  669. __finally
  670. {
  671. Storage->CloseSubKeyPath();
  672. }
  673. if (Storage->OpenSubKeyPath(L"Interface\\NewDirectory2", true))
  674. try
  675. {
  676. FNewDirectoryProperties.Save(Storage);
  677. }
  678. __finally
  679. {
  680. Storage->CloseSubKeyPath();
  681. }
  682. }
  683. //---------------------------------------------------------------------------
  684. bool __fastcall TGUIConfiguration::LoadCopyParam(THierarchicalStorage * Storage, TCopyParamType * CopyParam)
  685. {
  686. bool Result =
  687. Storage->OpenSubKeyPath(L"Interface\\CopyParam", false);
  688. if (Result)
  689. {
  690. try
  691. {
  692. CopyParam->Load(Storage);
  693. }
  694. catch (...)
  695. {
  696. Storage->CloseSubKeyPath();
  697. throw;
  698. }
  699. }
  700. return Result;
  701. }
  702. //---------------------------------------------------------------------------
  703. void __fastcall TGUIConfiguration::LoadDefaultCopyParam(THierarchicalStorage * Storage)
  704. {
  705. FDefaultCopyParam.Load(Storage);
  706. }
  707. //---------------------------------------------------------------------------
  708. void __fastcall TGUIConfiguration::LoadData(THierarchicalStorage * Storage)
  709. {
  710. TConfiguration::LoadData(Storage);
  711. // duplicated from core\configuration.cpp
  712. #define KEYEX(TYPE, VAR, NAME) VAR = Storage->Read ## TYPE(NAME, VAR)
  713. #pragma warn -eas
  714. REGCONFIG(false);
  715. #pragma warn +eas
  716. #undef KEYEX
  717. // FDefaultCopyParam must be loaded before eventual setting defaults for CopyParamList
  718. if (LoadCopyParam(Storage, &FDefaultCopyParam))
  719. try
  720. {
  721. int CopyParamListCount = Storage->ReadInteger(L"CopyParamList", -1);
  722. FCopyParamListDefaults = (CopyParamListCount < 0);
  723. if (!FCopyParamListDefaults)
  724. {
  725. FCopyParamList->Clear();
  726. FCopyParamList->Load(Storage, CopyParamListCount);
  727. }
  728. else if (FCopyParamList->Modified)
  729. {
  730. FCopyParamList->Clear();
  731. FCopyParamListDefaults = false;
  732. }
  733. FCopyParamList->Reset();
  734. }
  735. __finally
  736. {
  737. Storage->CloseSubKeyPath();
  738. }
  739. // Make it compatible with versions prior to 3.7.1 that have not saved PuttyPath
  740. // with quotes. First check for absence of quotes.
  741. // Add quotes either if the path is set to default putty path (even if it does
  742. // not exists) or when the path points to existing file (so there are no parameters
  743. // yet in the string). Note that FileExists may display error dialog, but as
  744. // it should be called only for custom users path, let's expect that the user
  745. // can take care of it.
  746. if ((FPuttyPath.SubString(1, 1) != L"\"") &&
  747. (IsPathToSameFile(ExpandEnvironmentVariables(FPuttyPath), FDefaultPuttyPathOnly) ||
  748. FileExists(ApiPath(ExpandEnvironmentVariables(FPuttyPath)))))
  749. {
  750. FPuttyPath = FormatCommand(FPuttyPath, L"");
  751. }
  752. if (Storage->OpenSubKeyPath(L"Interface\\NewDirectory2", false))
  753. try
  754. {
  755. FNewDirectoryProperties.Load(Storage);
  756. }
  757. __finally
  758. {
  759. Storage->CloseSubKeyPath();
  760. }
  761. }
  762. //---------------------------------------------------------------------------
  763. void __fastcall TGUIConfiguration::Saved()
  764. {
  765. TConfiguration::Saved();
  766. FCopyParamList->Reset();
  767. }
  768. //---------------------------------------------------------------------------
  769. //---------------------------------------------------------------------------
  770. UnicodeString __fastcall TGUIConfiguration::GetTranslationModule(const UnicodeString & Path)
  771. {
  772. UnicodeString SubPath = AddTranslationsSubFolder(Path);
  773. UnicodeString Result;
  774. // Prefer the SubPath. Default to SubPath.
  775. if (FileExists(Path) && !FileExists(SubPath))
  776. {
  777. Result = Path;
  778. }
  779. else
  780. {
  781. Result = SubPath;
  782. }
  783. return Result;
  784. }
  785. //---------------------------------------------------------------------------
  786. UnicodeString __fastcall TGUIConfiguration::AddTranslationsSubFolder(const UnicodeString & Path)
  787. {
  788. return
  789. IncludeTrailingBackslash(IncludeTrailingBackslash(ExtractFilePath(Path)) + TranslationsSubFolder) +
  790. ExtractFileName(Path);
  791. }
  792. //---------------------------------------------------------------------------
  793. HINSTANCE __fastcall TGUIConfiguration::LoadNewResourceModule(LCID ALocale,
  794. UnicodeString & FileName)
  795. {
  796. UnicodeString LibraryFileName;
  797. HINSTANCE NewInstance = 0;
  798. LCID AInternalLocale = InternalLocale();
  799. bool Internal = (ALocale == AInternalLocale);
  800. DWORD PrimaryLang = PRIMARYLANGID(ALocale);
  801. if (!Internal)
  802. {
  803. UnicodeString Module;
  804. UnicodeString LocaleName;
  805. Module = ModuleFileName();
  806. if ((ALocale & AdditionaLanguageMask) != AdditionaLanguageMask)
  807. {
  808. wchar_t LocaleStr[4];
  809. GetLocaleInfo(ALocale, LOCALE_SABBREVLANGNAME, LocaleStr, LENOF(LocaleStr));
  810. LocaleName = LocaleStr;
  811. DebugAssert(!LocaleName.IsEmpty());
  812. }
  813. else
  814. {
  815. LocaleName = AdditionaLanguagePrefix +
  816. char(ALocale & ~AdditionaLanguageMask);
  817. }
  818. Module = ChangeFileExt(Module, UnicodeString(L".") + LocaleName);
  819. // Look for a potential language/country translation
  820. UnicodeString ModulePath = GetTranslationModule(Module);
  821. NewInstance = LoadLibraryEx(ModulePath.c_str(), 0, LOAD_LIBRARY_AS_DATAFILE);
  822. if (NewInstance)
  823. {
  824. LibraryFileName = ModulePath;
  825. }
  826. else
  827. {
  828. DWORD SubLang = SUBLANGID(ALocale);
  829. DebugAssert(SUBLANG_DEFAULT == SUBLANG_CHINESE_TRADITIONAL);
  830. // Finally look for a language-only translation.
  831. // But for Chinese, never use "traditional" (what is the "default" Chinese), if we want "Simplified"
  832. // (the same what Inno Setup does)
  833. if ((PrimaryLang != LANG_CHINESE) ||
  834. (SubLang == SUBLANG_CHINESE_TRADITIONAL))
  835. {
  836. Module.SetLength(Module.Length() - 1);
  837. ModulePath = GetTranslationModule(Module);
  838. NewInstance = LoadLibraryEx(ModulePath.c_str(), 0, LOAD_LIBRARY_AS_DATAFILE);
  839. if (NewInstance)
  840. {
  841. LibraryFileName = ModulePath;
  842. }
  843. }
  844. }
  845. }
  846. // If the locale is non-US English and we do not have that translation (and it's unlikely we ever have),
  847. // treat it as if it were US English.
  848. if (!NewInstance && !Internal && (PrimaryLang == static_cast<DWORD>(PRIMARYLANGID(AInternalLocale))))
  849. {
  850. Internal = true;
  851. }
  852. if (!NewInstance && !Internal)
  853. {
  854. throw Exception(FMTLOAD(LOCALE_LOAD_ERROR, (int(ALocale))));
  855. }
  856. else
  857. {
  858. if (Internal)
  859. {
  860. NewInstance = HInstance;
  861. }
  862. }
  863. FileName = LibraryFileName;
  864. return NewInstance;
  865. }
  866. //---------------------------------------------------------------------------
  867. LCID __fastcall TGUIConfiguration::InternalLocale()
  868. {
  869. LCID Result;
  870. if (GetTranslationCount(ApplicationInfo) > 0)
  871. {
  872. TTranslation Translation;
  873. Translation = GetTranslation(ApplicationInfo, 0);
  874. Result = MAKELANGID(PRIMARYLANGID(Translation.Language), SUBLANG_DEFAULT);
  875. }
  876. else
  877. {
  878. DebugFail();
  879. Result = 0;
  880. }
  881. return Result;
  882. }
  883. //---------------------------------------------------------------------------
  884. LCID __fastcall TGUIConfiguration::GetLocale()
  885. {
  886. return FLocale;
  887. }
  888. //---------------------------------------------------------------------------
  889. void __fastcall TGUIConfiguration::SetLocale(LCID value)
  890. {
  891. if (Locale != value)
  892. {
  893. SetLocaleInternal(value, false, false);
  894. }
  895. }
  896. //---------------------------------------------------------------------------
  897. void __fastcall TGUIConfiguration::SetLocaleSafe(LCID value)
  898. {
  899. if (Locale != value)
  900. {
  901. SetLocaleInternal(value, true, false);
  902. }
  903. }
  904. //---------------------------------------------------------------------------
  905. UnicodeString __fastcall TGUIConfiguration::GetAppliedLocaleHex()
  906. {
  907. return IntToHex(__int64(AppliedLocale), 4);
  908. }
  909. //---------------------------------------------------------------------------
  910. int __fastcall TGUIConfiguration::GetResourceModuleCompleteness(HINSTANCE /*Module*/)
  911. {
  912. return 100;
  913. }
  914. //---------------------------------------------------------------------------
  915. bool __fastcall TGUIConfiguration::IsTranslationComplete(HINSTANCE /*Module*/)
  916. {
  917. return true;
  918. }
  919. //---------------------------------------------------------------------------
  920. void __fastcall TGUIConfiguration::SetLocaleInternal(LCID value, bool Safe, bool CompleteOnly)
  921. {
  922. LCID L = value;
  923. if (L == NULL)
  924. {
  925. L = GetUserDefaultUILanguage();
  926. }
  927. HINSTANCE Module = NULL;
  928. UnicodeString FileName;
  929. try
  930. {
  931. Module = LoadNewResourceModule(L, FileName);
  932. DebugAssert(Module != NULL);
  933. if (CompleteOnly && !IsTranslationComplete(Module))
  934. {
  935. Abort();
  936. }
  937. }
  938. catch (...)
  939. {
  940. if (Module != NULL)
  941. {
  942. FreeResourceModule(Module);
  943. Module = NULL;
  944. }
  945. if (Safe)
  946. {
  947. // ignore any exception while loading locale
  948. }
  949. else
  950. {
  951. throw;
  952. }
  953. }
  954. if (Module != NULL)
  955. {
  956. FLocale = value;
  957. if (CanApplyLocaleImmediately)
  958. {
  959. SetAppliedLocale(L, FileName);
  960. SetResourceModule(Module);
  961. }
  962. }
  963. }
  964. //---------------------------------------------------------------------------
  965. bool __fastcall TGUIConfiguration::GetCanApplyLocaleImmediately()
  966. {
  967. return
  968. (Screen->FormCount == 0) &&
  969. (Screen->DataModuleCount == 0);
  970. }
  971. //---------------------------------------------------------------------------
  972. bool __fastcall TGUIConfiguration::UsingInternalTranslation()
  973. {
  974. return FLocaleModuleName.IsEmpty();
  975. }
  976. //---------------------------------------------------------------------------
  977. UnicodeString __fastcall TGUIConfiguration::AppliedLocaleCopyright()
  978. {
  979. UnicodeString Result;
  980. if (UsingInternalTranslation())
  981. {
  982. DebugFail(); // we do not expect to get called with internal locale
  983. Result = UnicodeString();
  984. }
  985. else
  986. {
  987. Result = GetFileFileInfoString(L"LegalCopyright", FLocaleModuleName);
  988. }
  989. return Result;
  990. }
  991. //---------------------------------------------------------------------------
  992. UnicodeString __fastcall TGUIConfiguration::AppliedLocaleVersion()
  993. {
  994. UnicodeString Result;
  995. if (UsingInternalTranslation())
  996. {
  997. // noop
  998. }
  999. else
  1000. {
  1001. Result = GetFileVersion(FLocaleModuleName);
  1002. }
  1003. return Result;
  1004. }
  1005. //---------------------------------------------------------------------------
  1006. void __fastcall TGUIConfiguration::SetAppliedLocale(LCID AppliedLocale, const UnicodeString & LocaleModuleName)
  1007. {
  1008. FAppliedLocale = AppliedLocale;
  1009. FLocaleModuleName = LocaleModuleName;
  1010. }
  1011. //---------------------------------------------------------------------------
  1012. void __fastcall TGUIConfiguration::FreeResourceModule(HANDLE Instance)
  1013. {
  1014. TLibModule * MainModule = FindModule(HInstance);
  1015. if ((unsigned)Instance != MainModule->Instance)
  1016. {
  1017. FreeLibrary(static_cast<HMODULE>(Instance));
  1018. }
  1019. }
  1020. //---------------------------------------------------------------------------
  1021. HANDLE __fastcall TGUIConfiguration::ChangeToDefaultResourceModule()
  1022. {
  1023. return ChangeResourceModule(NULL);
  1024. }
  1025. //---------------------------------------------------------------------------
  1026. HANDLE __fastcall TGUIConfiguration::ChangeResourceModule(HANDLE Instance)
  1027. {
  1028. if (Instance == NULL)
  1029. {
  1030. Instance = HInstance;
  1031. }
  1032. TLibModule * MainModule = FindModule(HInstance);
  1033. HANDLE Result = (HANDLE)MainModule->ResInstance;
  1034. MainModule->ResInstance = (unsigned)Instance;
  1035. CoreSetResourceModule(Instance);
  1036. return Result;
  1037. }
  1038. //---------------------------------------------------------------------------
  1039. HANDLE __fastcall TGUIConfiguration::GetResourceModule()
  1040. {
  1041. return (HANDLE)FindModule(HInstance)->ResInstance;
  1042. }
  1043. //---------------------------------------------------------------------------
  1044. void __fastcall TGUIConfiguration::SetResourceModule(HINSTANCE Instance)
  1045. {
  1046. HANDLE PrevHandle = ChangeResourceModule(Instance);
  1047. FreeResourceModule(PrevHandle);
  1048. DefaultLocalized();
  1049. }
  1050. //---------------------------------------------------------------------------
  1051. void __fastcall TGUIConfiguration::FindLocales(const UnicodeString & LocalesMask, TStrings * Exts, UnicodeString & LocalesExts)
  1052. {
  1053. int FindAttrs = faReadOnly | faArchive;
  1054. TSearchRecOwned SearchRec;
  1055. bool Found = (FindFirstUnchecked(LocalesMask, FindAttrs, SearchRec) == 0);
  1056. while (Found)
  1057. {
  1058. UnicodeString Ext = ExtractFileExt(SearchRec.Name).UpperCase();
  1059. // DLL is a remnant from times the .NET assembly was winscp.dll, not winscpnet.dll
  1060. if ((Ext.Length() >= 3) && (Ext != L".EXE") && (Ext != L".COM") &&
  1061. (Ext != L".DLL") && (Ext != L".INI") && (Ext != L".MAP"))
  1062. {
  1063. Ext = Ext.SubString(2, Ext.Length() - 1);
  1064. LocalesExts += Ext;
  1065. Exts->Add(Ext);
  1066. }
  1067. Found = (FindNextChecked(SearchRec) == 0);
  1068. }
  1069. }
  1070. //---------------------------------------------------------------------------
  1071. void __fastcall TGUIConfiguration::AddLocale(LCID Locale, const UnicodeString & Name)
  1072. {
  1073. std::unique_ptr<TLocaleInfo> LocaleInfo(new TLocaleInfo());
  1074. LocaleInfo->Locale = Locale;
  1075. LocaleInfo->Name = Name;
  1076. try
  1077. {
  1078. UnicodeString FileName;
  1079. HINSTANCE Module = LoadNewResourceModule(Locale, FileName);
  1080. try
  1081. {
  1082. LocaleInfo->Completeness = GetResourceModuleCompleteness(Module);
  1083. }
  1084. __finally
  1085. {
  1086. FreeResourceModule(Module);
  1087. }
  1088. }
  1089. catch (...)
  1090. {
  1091. LocaleInfo->Completeness = -1;
  1092. }
  1093. FLocales->Add(LocaleInfo.release());
  1094. }
  1095. //---------------------------------------------------------------------------
  1096. int __fastcall TGUIConfiguration::LocalesCompare(void * Item1, void * Item2)
  1097. {
  1098. TLocaleInfo * LocaleInfo1 = static_cast<TLocaleInfo *>(Item1);
  1099. TLocaleInfo * LocaleInfo2 = static_cast<TLocaleInfo *>(Item2);
  1100. return CompareText(LocaleInfo1->Name, LocaleInfo2->Name);
  1101. }
  1102. //---------------------------------------------------------------------------
  1103. TObjectList * __fastcall TGUIConfiguration::GetLocales()
  1104. {
  1105. UnicodeString LocalesMask = ChangeFileExt(ModuleFileName(), L".*");
  1106. UnicodeString SubLocalesMask = AddTranslationsSubFolder(LocalesMask);
  1107. UnicodeString LocalesExts;
  1108. std::unique_ptr<TStringList> Exts(CreateSortedStringList());
  1109. FindLocales(SubLocalesMask, Exts.get(), LocalesExts);
  1110. FindLocales(LocalesMask, Exts.get(), LocalesExts);
  1111. if (FLastLocalesExts != LocalesExts)
  1112. {
  1113. FLastLocalesExts = LocalesExts;
  1114. FLocales->Clear();
  1115. TLanguages * Langs = Languages();
  1116. int Count = Langs->Count;
  1117. int Index = -1;
  1118. while (Index < Count)
  1119. {
  1120. LCID Locale;
  1121. if (Index >= 0)
  1122. {
  1123. Locale = Langs->LocaleID[Index];
  1124. DWORD SubLang = SUBLANGID(Locale);
  1125. int Ext = Exts->IndexOf(Langs->Ext[Index]);
  1126. if ((Ext >= 0) && (Exts->Objects[Ext] == NULL))
  1127. {
  1128. // noop
  1129. }
  1130. else if (SubLang == SUBLANG_DEFAULT)
  1131. {
  1132. Ext = Exts->IndexOf(Langs->Ext[Index].SubString(1, 2));
  1133. if ((Ext >= 0) && (Exts->Objects[Ext] == NULL))
  1134. {
  1135. Locale = MAKELANGID(PRIMARYLANGID(Locale), SUBLANG_DEFAULT);
  1136. }
  1137. }
  1138. if (Ext >= 0)
  1139. {
  1140. Exts->Objects[Ext] = reinterpret_cast<TObject*>(Locale);
  1141. }
  1142. else
  1143. {
  1144. Locale = 0;
  1145. }
  1146. }
  1147. else
  1148. {
  1149. Locale = InternalLocale();
  1150. }
  1151. if (Locale)
  1152. {
  1153. wchar_t LocaleStr[255];
  1154. GetLocaleInfo(Locale, LOCALE_SENGLANGUAGE,
  1155. LocaleStr, LENOF(LocaleStr));
  1156. UnicodeString Name = LocaleStr;
  1157. Name += TitleSeparator;
  1158. // LOCALE_SNATIVELANGNAME
  1159. GetLocaleInfo(Locale, LOCALE_SLANGUAGE,
  1160. LocaleStr, LENOF(LocaleStr));
  1161. Name += LocaleStr;
  1162. AddLocale(Locale, Name);
  1163. }
  1164. Index++;
  1165. }
  1166. for (int Index = 0; Index < Exts->Count; Index++)
  1167. {
  1168. if ((Exts->Objects[Index] == NULL) &&
  1169. (Exts->Strings[Index].Length() == 3) &&
  1170. SameText(Exts->Strings[Index].SubString(1, 2), AdditionaLanguagePrefix))
  1171. {
  1172. UnicodeString ModulePath = ChangeFileExt(ModuleFileName(), UnicodeString(L".") + Exts->Strings[Index]);
  1173. ModulePath = GetTranslationModule(ModulePath);
  1174. UnicodeString LangName = GetFileFileInfoString(L"LangName", ModulePath);
  1175. if (!LangName.IsEmpty())
  1176. {
  1177. AddLocale(AdditionaLanguageMask + Exts->Strings[Index][3], LangName);
  1178. }
  1179. }
  1180. }
  1181. FLocales->Sort(LocalesCompare);
  1182. }
  1183. return FLocales;
  1184. }
  1185. //---------------------------------------------------------------------------
  1186. void __fastcall TGUIConfiguration::SetDefaultCopyParam(const TGUICopyParamType & value)
  1187. {
  1188. FDefaultCopyParam.Assign(&value);
  1189. Changed();
  1190. }
  1191. //---------------------------------------------------------------------------
  1192. bool __fastcall TGUIConfiguration::GetRememberPassword()
  1193. {
  1194. bool Result = SessionRememberPassword || PuttyPassword;
  1195. if (!Result)
  1196. {
  1197. try
  1198. {
  1199. TRemoteCustomCommand RemoteCustomCommand;
  1200. TInteractiveCustomCommand InteractiveCustomCommand(&RemoteCustomCommand);
  1201. UnicodeString APuttyPath = InteractiveCustomCommand.Complete(PuttyPath, false);
  1202. Result = RemoteCustomCommand.IsPasswordCommand(PuttyPath);
  1203. }
  1204. catch (...)
  1205. {
  1206. // noop
  1207. }
  1208. }
  1209. return Result;
  1210. }
  1211. //---------------------------------------------------------------------------
  1212. const TCopyParamList * __fastcall TGUIConfiguration::GetCopyParamList()
  1213. {
  1214. return FCopyParamList;
  1215. }
  1216. //---------------------------------------------------------------------------
  1217. void __fastcall TGUIConfiguration::SetCopyParamList(const TCopyParamList * value)
  1218. {
  1219. if (!(*FCopyParamList == *value))
  1220. {
  1221. *FCopyParamList = *value;
  1222. FCopyParamListDefaults = false;
  1223. Changed();
  1224. }
  1225. }
  1226. //---------------------------------------------------------------------------
  1227. int __fastcall TGUIConfiguration::GetCopyParamIndex()
  1228. {
  1229. int Result;
  1230. if (FCopyParamCurrent.IsEmpty())
  1231. {
  1232. Result = -1;
  1233. }
  1234. else
  1235. {
  1236. Result = FCopyParamList->IndexOfName(FCopyParamCurrent);
  1237. }
  1238. return Result;
  1239. }
  1240. //---------------------------------------------------------------------------
  1241. void __fastcall TGUIConfiguration::SetCopyParamIndex(int value)
  1242. {
  1243. UnicodeString Name;
  1244. if (value < 0)
  1245. {
  1246. Name = L"";
  1247. }
  1248. else
  1249. {
  1250. Name = FCopyParamList->Names[value];
  1251. }
  1252. CopyParamCurrent = Name;
  1253. }
  1254. //---------------------------------------------------------------------------
  1255. void __fastcall TGUIConfiguration::SetCopyParamCurrent(UnicodeString value)
  1256. {
  1257. SET_CONFIG_PROPERTY(CopyParamCurrent);
  1258. }
  1259. //---------------------------------------------------------------------------
  1260. TGUICopyParamType __fastcall TGUIConfiguration::GetCurrentCopyParam()
  1261. {
  1262. return CopyParamPreset[CopyParamCurrent];
  1263. }
  1264. //---------------------------------------------------------------------------
  1265. TGUICopyParamType __fastcall TGUIConfiguration::GetCopyParamPreset(UnicodeString Name)
  1266. {
  1267. TGUICopyParamType Result = FDefaultCopyParam;
  1268. if (!Name.IsEmpty())
  1269. {
  1270. int Index = FCopyParamList->IndexOfName(Name);
  1271. DebugAssert(Index >= 0);
  1272. if (Index >= 0)
  1273. {
  1274. const TCopyParamType * Preset = FCopyParamList->CopyParams[Index];
  1275. DebugAssert(Preset != NULL);
  1276. Result.Assign(Preset); // overwrite all but GUI options
  1277. // reset all options known not to be configurable per-preset
  1278. // kind of hack
  1279. Result.ResumeSupport = FDefaultCopyParam.ResumeSupport;
  1280. Result.ResumeThreshold = FDefaultCopyParam.ResumeThreshold;
  1281. Result.LocalInvalidChars = FDefaultCopyParam.LocalInvalidChars;
  1282. }
  1283. }
  1284. return Result;
  1285. }
  1286. //---------------------------------------------------------------------------
  1287. bool __fastcall TGUIConfiguration::GetHasCopyParamPreset(UnicodeString Name)
  1288. {
  1289. return Name.IsEmpty() || (FCopyParamList->IndexOfName(Name) >= 0);
  1290. }
  1291. //---------------------------------------------------------------------------
  1292. void __fastcall TGUIConfiguration::SetNewDirectoryProperties(
  1293. const TRemoteProperties & value)
  1294. {
  1295. SET_CONFIG_PROPERTY(NewDirectoryProperties);
  1296. }
  1297. //---------------------------------------------------------------------------
  1298. void __fastcall TGUIConfiguration::SetQueueBootstrap(bool value)
  1299. {
  1300. SET_CONFIG_PROPERTY(QueueBootstrap);
  1301. }
  1302. //---------------------------------------------------------------------------
  1303. void __fastcall TGUIConfiguration::SetQueueKeepDoneItems(bool value)
  1304. {
  1305. SET_CONFIG_PROPERTY(QueueKeepDoneItems);
  1306. }
  1307. //---------------------------------------------------------------------------
  1308. void __fastcall TGUIConfiguration::SetQueueKeepDoneItemsFor(int value)
  1309. {
  1310. SET_CONFIG_PROPERTY(QueueKeepDoneItemsFor);
  1311. }
  1312. //---------------------------------------------------------------------
  1313. TStoredSessionList * __fastcall TGUIConfiguration::SelectPuttySessionsForImport(
  1314. const UnicodeString & RootKey, const UnicodeString & Source, TStoredSessionList * Sessions, UnicodeString & Error)
  1315. {
  1316. std::unique_ptr<TStoredSessionList> ImportSessionList(new TStoredSessionList(true));
  1317. ImportSessionList->DefaultSettings = Sessions->DefaultSettings;
  1318. UnicodeString SessionsKey = GetPuttySessionsKey(RootKey);
  1319. std::unique_ptr<TRegistryStorage> Storage(new TRegistryStorage(SessionsKey));
  1320. Storage->ConfigureForPutty();
  1321. if (Storage->OpenRootKey(false))
  1322. {
  1323. ImportSessionList->Load(Storage.get(), false, true, true);
  1324. }
  1325. TSessionData * PuttySessionData =
  1326. (TSessionData *)ImportSessionList->FindByName(PuttySession);
  1327. if (PuttySessionData != NULL)
  1328. {
  1329. ImportSessionList->Remove(PuttySessionData);
  1330. }
  1331. if (ImportSessionList->Count > 0)
  1332. {
  1333. ImportSessionList->SelectSessionsToImport(Sessions, true);
  1334. }
  1335. else
  1336. {
  1337. Error = FMTLOAD(PUTTY_NO_SITES2, (Source, SessionsKey));
  1338. }
  1339. return ImportSessionList.release();
  1340. }
  1341. //---------------------------------------------------------------------
  1342. bool __fastcall TGUIConfiguration::AnyPuttySessionForImport(TStoredSessionList * Sessions)
  1343. {
  1344. try
  1345. {
  1346. UnicodeString Error;
  1347. std::unique_ptr<TStoredSessionList> SessionsForImport(
  1348. SelectPuttySessionsForImport(OriginalPuttyRegistryStorageKey, L"PuTTY", Sessions, Error));
  1349. return (SessionsForImport->Count > 0);
  1350. }
  1351. catch (...)
  1352. {
  1353. return false;
  1354. }
  1355. }
  1356. //---------------------------------------------------------------------------