GUIConfiguration.cpp 45 KB

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