Configuration.cpp 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include <FileInfo.h>
  5. #include "Common.h"
  6. #include "Exceptions.h"
  7. #include "Configuration.h"
  8. #include "PuttyIntf.h"
  9. #include "TextsCore.h"
  10. #include "Interface.h"
  11. #include "CoreMain.h"
  12. #include "Security.h"
  13. #include "FileMasks.h"
  14. #include "CopyParam.h"
  15. #include <shlobj.h>
  16. #include <System.IOUtils.hpp>
  17. #include <System.StrUtils.hpp>
  18. //---------------------------------------------------------------------------
  19. #pragma package(smart_init)
  20. //---------------------------------------------------------------------------
  21. const wchar_t * AutoSwitchNames = L"On;Off;Auto";
  22. const wchar_t * NotAutoSwitchNames = L"Off;On;Auto";
  23. //---------------------------------------------------------------------------
  24. // See https://www.iana.org/assignments/hash-function-text-names/hash-function-text-names.xhtml
  25. const UnicodeString Sha1ChecksumAlg(L"sha-1");
  26. const UnicodeString Sha224ChecksumAlg(L"sha-224");
  27. const UnicodeString Sha256ChecksumAlg(L"sha-256");
  28. const UnicodeString Sha384ChecksumAlg(L"sha-384");
  29. const UnicodeString Sha512ChecksumAlg(L"sha-512");
  30. const UnicodeString Md5ChecksumAlg(L"md5");
  31. // Not defined by IANA
  32. const UnicodeString Crc32ChecksumAlg(L"crc32");
  33. //---------------------------------------------------------------------------
  34. const UnicodeString SshFingerprintType(L"ssh");
  35. const UnicodeString TlsFingerprintType(L"tls");
  36. //---------------------------------------------------------------------------
  37. const UnicodeString FtpsCertificateStorageKey(L"FtpsCertificates");
  38. const UnicodeString HttpsCertificateStorageKey(L"HttpsCertificates");
  39. const UnicodeString LastFingerprintsStorageKey(L"LastFingerprints");
  40. //---------------------------------------------------------------------------
  41. __fastcall TConfiguration::TConfiguration()
  42. {
  43. FCriticalSection = new TCriticalSection();
  44. FUpdating = 0;
  45. FStorage = stDetect;
  46. FDontSave = false;
  47. FForceSave = false;
  48. FApplicationInfo = NULL;
  49. FUsage = new TUsage(this);
  50. FDefaultCollectUsage = false;
  51. FScripting = false;
  52. UnicodeString RandomSeedPath;
  53. if (!GetEnvironmentVariable(L"APPDATA").IsEmpty())
  54. {
  55. RandomSeedPath = L"%APPDATA%";
  56. }
  57. else
  58. {
  59. RandomSeedPath = GetShellFolderPath(CSIDL_LOCAL_APPDATA);
  60. if (RandomSeedPath.IsEmpty())
  61. {
  62. RandomSeedPath = GetShellFolderPath(CSIDL_APPDATA);
  63. }
  64. }
  65. FDefaultRandomSeedFile = IncludeTrailingBackslash(RandomSeedPath) + L"winscp.rnd";
  66. }
  67. //---------------------------------------------------------------------------
  68. void __fastcall TConfiguration::Default()
  69. {
  70. TGuard Guard(FCriticalSection);
  71. FDisablePasswordStoring = false;
  72. FForceBanners = false;
  73. FDisableAcceptingHostKeys = false;
  74. TRegistryStorage * AdminStorage;
  75. AdminStorage = new TRegistryStorage(RegistryStorageKey, HKEY_LOCAL_MACHINE);
  76. try
  77. {
  78. if (AdminStorage->OpenRootKey(false))
  79. {
  80. LoadAdmin(AdminStorage);
  81. AdminStorage->CloseSubKey();
  82. }
  83. }
  84. __finally
  85. {
  86. delete AdminStorage;
  87. }
  88. RandomSeedFile = FDefaultRandomSeedFile;
  89. PuttyRegistryStorageKey = OriginalPuttyRegistryStorageKey;
  90. FConfirmOverwriting = true;
  91. FConfirmResume = true;
  92. FAutoReadDirectoryAfterOp = true;
  93. FSessionReopenAuto = 5000;
  94. FSessionReopenBackground = 2000;
  95. FSessionReopenTimeout = 0;
  96. FSessionReopenAutoStall = 60000;
  97. FTunnelLocalPortNumberLow = 50000;
  98. FTunnelLocalPortNumberHigh = 50099;
  99. FCacheDirectoryChangesMaxSize = 100;
  100. FShowFtpWelcomeMessage = false;
  101. FExternalIpAddress = L"";
  102. FTryFtpWhenSshFails = true;
  103. FParallelDurationThreshold = 10;
  104. FMimeTypes = UnicodeString();
  105. FDontReloadMoreThanSessions = 1000;
  106. FScriptProgressFileNameLimit = 25;
  107. CollectUsage = FDefaultCollectUsage;
  108. FLogging = false;
  109. FPermanentLogging = false;
  110. FLogFileName = DefaultLogFileName;
  111. FPermanentLogFileName = FLogFileName;
  112. FLogFileAppend = true;
  113. FLogSensitive = false;
  114. FPermanentLogSensitive = FLogSensitive;
  115. FLogMaxSize = 0;
  116. FPermanentLogMaxSize = FLogMaxSize;
  117. FLogMaxCount = 0;
  118. FPermanentLogMaxCount = FLogMaxCount;
  119. FLogProtocol = 0;
  120. FPermanentLogProtocol = FLogProtocol;
  121. UpdateActualLogProtocol();
  122. FLogActions = false;
  123. FPermanentLogActions = false;
  124. FLogActionsRequired = false;
  125. FActionsLogFileName = L"%TEMP%\\!S.xml";
  126. FPermanentActionsLogFileName = FActionsLogFileName;
  127. FProgramIniPathWrittable = -1;
  128. FCustomIniFileStorageName = LoadCustomIniFileStorageName();
  129. Changed();
  130. }
  131. //---------------------------------------------------------------------------
  132. __fastcall TConfiguration::~TConfiguration()
  133. {
  134. DebugAssert(!FUpdating);
  135. if (FApplicationInfo) FreeFileInfo(FApplicationInfo);
  136. delete FCriticalSection;
  137. delete FUsage;
  138. }
  139. //---------------------------------------------------------------------------
  140. void __fastcall TConfiguration::UpdateStaticUsage()
  141. {
  142. Usage->Set(L"ConfigurationIniFile", (Storage == stIniFile));
  143. Usage->Set(L"ConfigurationIniFileCustom", !CustomIniFileStorageName.IsEmpty());
  144. Usage->Set("Unofficial", IsUnofficial);
  145. // this is called from here, because we are guarded from calling into
  146. // master password handler here, see TWinConfiguration::UpdateStaticUsage
  147. StoredSessions->UpdateStaticUsage();
  148. }
  149. //---------------------------------------------------------------------------
  150. THierarchicalStorage * TConfiguration::CreateConfigStorage()
  151. {
  152. bool SessionList = false;
  153. return CreateScpStorage(SessionList);
  154. }
  155. //---------------------------------------------------------------------------
  156. THierarchicalStorage * TConfiguration::CreateScpStorage(bool & SessionList)
  157. {
  158. TGuard Guard(FCriticalSection);
  159. THierarchicalStorage * Result;
  160. if (Storage == stRegistry)
  161. {
  162. Result = new TRegistryStorage(RegistryStorageKey);
  163. }
  164. else if (Storage == stNul)
  165. {
  166. Result = TIniFileStorage::CreateFromPath(INI_NUL);
  167. }
  168. else
  169. {
  170. UnicodeString StorageName = IniFileStorageName;
  171. Result = TIniFileStorage::CreateFromPath(StorageName);
  172. }
  173. if ((FOptionsStorage.get() != NULL) && (FOptionsStorage->Count > 0))
  174. {
  175. if (!SessionList)
  176. {
  177. Result = new TOptionsStorage(FOptionsStorage.get(), ConfigurationSubKey, Result);
  178. }
  179. else
  180. {
  181. // cannot reuse session list storage for configuration as for it we need
  182. // the option-override storage above
  183. }
  184. }
  185. else
  186. {
  187. // All the above stores can be reused for configuration,
  188. // if no options-overrides are set
  189. SessionList = false;
  190. }
  191. return Result;
  192. }
  193. //---------------------------------------------------------------------------
  194. UnicodeString __fastcall TConfiguration::PropertyToKey(const UnicodeString & Property)
  195. {
  196. // no longer useful
  197. int P = Property.LastDelimiter(L".>");
  198. UnicodeString Result = Property.SubString(P + 1, Property.Length() - P);
  199. if ((Result[1] == L'F') && ((wchar_t)toupper(Result[2]) == Result[2]))
  200. {
  201. Result.Delete(1, 1);
  202. }
  203. return Result;
  204. }
  205. //---------------------------------------------------------------------------
  206. #define BLOCK(KEY, CANCREATE, BLOCK) \
  207. if (Storage->OpenSubKeyPath(KEY, CANCREATE)) try { BLOCK } __finally { Storage->CloseSubKeyPath(); }
  208. #define KEY(TYPE, VAR) KEYEX(TYPE, VAR, PropertyToKey(TEXT(#VAR)))
  209. #define REGCONFIG(CANCREATE) \
  210. BLOCK(L"Interface", CANCREATE, \
  211. KEY(String, RandomSeedFile); \
  212. KEY(String, PuttyRegistryStorageKey); \
  213. KEY(Bool, ConfirmOverwriting); \
  214. KEY(Bool, ConfirmResume); \
  215. KEY(Bool, AutoReadDirectoryAfterOp); \
  216. KEY(Integer, SessionReopenAuto); \
  217. KEY(Integer, SessionReopenBackground); \
  218. KEY(Integer, SessionReopenTimeout); \
  219. KEY(Integer, SessionReopenAutoStall); \
  220. KEY(Integer, TunnelLocalPortNumberLow); \
  221. KEY(Integer, TunnelLocalPortNumberHigh); \
  222. KEY(Integer, CacheDirectoryChangesMaxSize); \
  223. KEY(Bool, ShowFtpWelcomeMessage); \
  224. KEY(String, ExternalIpAddress); \
  225. KEY(Bool, TryFtpWhenSshFails); \
  226. KEY(Integer, ParallelDurationThreshold); \
  227. KEY(String, MimeTypes); \
  228. KEY(Integer, DontReloadMoreThanSessions); \
  229. KEY(Integer, ScriptProgressFileNameLimit); \
  230. KEY(Bool, CollectUsage); \
  231. ); \
  232. BLOCK(L"Logging", CANCREATE, \
  233. KEYEX(Bool, PermanentLogging, L"Logging"); \
  234. KEYEX(String,PermanentLogFileName, L"LogFileName"); \
  235. KEY(Bool, LogFileAppend); \
  236. KEYEX(Bool, PermanentLogSensitive, L"LogSensitive"); \
  237. KEYEX(Int64, PermanentLogMaxSize, L"LogMaxSize"); \
  238. KEYEX(Integer, PermanentLogMaxCount, L"LogMaxCount"); \
  239. KEYEX(Integer,PermanentLogProtocol, L"LogProtocol"); \
  240. KEYEX(Bool, PermanentLogActions, L"LogActions"); \
  241. KEYEX(String,PermanentActionsLogFileName, L"ActionsLogFileName"); \
  242. );
  243. //---------------------------------------------------------------------------
  244. void __fastcall TConfiguration::SaveData(THierarchicalStorage * Storage, bool /*All*/)
  245. {
  246. #define KEYEX(TYPE, VAR, NAME) Storage->Write ## TYPE(NAME, VAR)
  247. REGCONFIG(true);
  248. #undef KEYEX
  249. if (Storage->OpenSubKey(L"Usage", true))
  250. {
  251. FUsage->Save(Storage);
  252. Storage->CloseSubKey();
  253. }
  254. }
  255. //---------------------------------------------------------------------------
  256. void __fastcall TConfiguration::Save()
  257. {
  258. // only modified, implicit
  259. DoSave(false, false);
  260. }
  261. //---------------------------------------------------------------------------
  262. void __fastcall TConfiguration::SaveExplicit()
  263. {
  264. // only modified, explicit
  265. DoSave(false, true);
  266. }
  267. //---------------------------------------------------------------------------
  268. void __fastcall TConfiguration::DoSave(bool All, bool Explicit)
  269. {
  270. if (FDontSave) return;
  271. THierarchicalStorage * AStorage = CreateConfigStorage();
  272. try
  273. {
  274. AStorage->AccessMode = smReadWrite;
  275. AStorage->Explicit = Explicit;
  276. AStorage->ForceSave = FForceSave;
  277. if (AStorage->OpenSubKey(ConfigurationSubKey, true))
  278. {
  279. // if saving to TOptionsStorage, make sure we save everything so that
  280. // all configuration is properly transferred to the master storage
  281. bool ConfigAll = All || AStorage->Temporary;
  282. SaveData(AStorage, ConfigAll);
  283. }
  284. }
  285. __finally
  286. {
  287. delete AStorage;
  288. }
  289. Saved();
  290. if (All)
  291. {
  292. StoredSessions->Save(true, Explicit);
  293. }
  294. // clean up as last, so that if it fails (read only INI), the saving can proceed
  295. if (Storage == stRegistry)
  296. {
  297. CleanupIniFile();
  298. }
  299. SaveCustomIniFileStorageName();
  300. }
  301. //---------------------------------------------------------------------------
  302. void __fastcall TConfiguration::SaveCustomIniFileStorageName()
  303. {
  304. // Particularly, not to create an empty "Override" key, unless the custom INI file is ever set
  305. if (CustomIniFileStorageName != LoadCustomIniFileStorageName())
  306. {
  307. std::unique_ptr<TRegistryStorage> RegistryStorage(new TRegistryStorage(GetRegistryStorageOverrideKey()));
  308. RegistryStorage->AccessMode = smReadWrite;
  309. RegistryStorage->Explicit = true;
  310. if (RegistryStorage->OpenRootKey(true))
  311. {
  312. RegistryStorage->WriteString(L"IniFile", CustomIniFileStorageName);
  313. RegistryStorage->CloseSubKey();
  314. }
  315. }
  316. }
  317. //---------------------------------------------------------------------------
  318. void __fastcall TConfiguration::Export(const UnicodeString & FileName)
  319. {
  320. // not to "append" the export to an existing file
  321. if (FileExists(FileName))
  322. {
  323. DeleteFileChecked(FileName);
  324. }
  325. THierarchicalStorage * Storage = NULL;
  326. THierarchicalStorage * ExportStorage = NULL;
  327. try
  328. {
  329. ExportStorage = TIniFileStorage::CreateFromPath(FileName);
  330. ExportStorage->AccessMode = smReadWrite;
  331. ExportStorage->Explicit = true;
  332. Storage = CreateConfigStorage();
  333. Storage->AccessMode = smRead;
  334. CopyData(Storage, ExportStorage);
  335. if (ExportStorage->OpenSubKey(ConfigurationSubKey, true))
  336. {
  337. SaveData(ExportStorage, true);
  338. }
  339. }
  340. __finally
  341. {
  342. delete ExportStorage;
  343. delete Storage;
  344. }
  345. StoredSessions->Export(FileName);
  346. }
  347. //---------------------------------------------------------------------------
  348. void __fastcall TConfiguration::Import(const UnicodeString & FileName)
  349. {
  350. THierarchicalStorage * Storage = NULL;
  351. THierarchicalStorage * ImportStorage = NULL;
  352. try
  353. {
  354. ImportStorage = TIniFileStorage::CreateFromPath(FileName);
  355. ImportStorage->AccessMode = smRead;
  356. Storage = CreateConfigStorage();
  357. Storage->AccessMode = smReadWrite;
  358. Storage->Explicit = true;
  359. CopyData(ImportStorage, Storage);
  360. Default();
  361. LoadFrom(ImportStorage);
  362. if (ImportStorage->OpenSubKey(Configuration->StoredSessionsSubKey, false))
  363. {
  364. StoredSessions->Clear();
  365. StoredSessions->DefaultSettings->Default();
  366. StoredSessions->Load(ImportStorage);
  367. }
  368. }
  369. __finally
  370. {
  371. delete ImportStorage;
  372. delete Storage;
  373. }
  374. // save all and explicit
  375. DoSave(true, true);
  376. }
  377. //---------------------------------------------------------------------------
  378. void __fastcall TConfiguration::LoadData(THierarchicalStorage * Storage)
  379. {
  380. #define KEYEX(TYPE, VAR, NAME) VAR = Storage->Read ## TYPE(NAME, VAR)
  381. #pragma warn -eas
  382. REGCONFIG(false);
  383. #pragma warn +eas
  384. #undef KEYEX
  385. if (Storage->OpenSubKey(L"Usage", false))
  386. {
  387. FUsage->Load(Storage);
  388. Storage->CloseSubKey();
  389. }
  390. if (FPermanentLogActions && FPermanentActionsLogFileName.IsEmpty() &&
  391. FPermanentLogging && !FPermanentLogFileName.IsEmpty())
  392. {
  393. FPermanentActionsLogFileName = FPermanentLogFileName;
  394. FPermanentLogging = false;
  395. FPermanentLogFileName = L"";
  396. }
  397. }
  398. //---------------------------------------------------------------------------
  399. void __fastcall TConfiguration::LoadAdmin(THierarchicalStorage * Storage)
  400. {
  401. FDisablePasswordStoring = Storage->ReadBool(L"DisablePasswordStoring", FDisablePasswordStoring);
  402. FForceBanners = Storage->ReadBool(L"ForceBanners", FForceBanners);
  403. FDisableAcceptingHostKeys = Storage->ReadBool(L"DisableAcceptingHostKeys", FDisableAcceptingHostKeys);
  404. FDefaultCollectUsage = Storage->ReadBool(L"DefaultCollectUsage", FDefaultCollectUsage);
  405. }
  406. //---------------------------------------------------------------------------
  407. void __fastcall TConfiguration::LoadFrom(THierarchicalStorage * Storage)
  408. {
  409. if (Storage->OpenSubKey(ConfigurationSubKey, false))
  410. {
  411. LoadData(Storage);
  412. Storage->CloseSubKey();
  413. }
  414. }
  415. //---------------------------------------------------------------------------
  416. UnicodeString __fastcall TConfiguration::GetRegistryStorageOverrideKey()
  417. {
  418. return GetRegistryStorageKey() + L" Override";
  419. }
  420. //---------------------------------------------------------------------------
  421. UnicodeString __fastcall TConfiguration::LoadCustomIniFileStorageName()
  422. {
  423. UnicodeString Result;
  424. std::unique_ptr<TRegistryStorage> RegistryStorage(new TRegistryStorage(GetRegistryStorageOverrideKey()));
  425. if (RegistryStorage->OpenRootKey(false))
  426. {
  427. Result = RegistryStorage->ReadString(L"IniFile", L"");
  428. RegistryStorage->CloseSubKey();
  429. }
  430. RegistryStorage.reset(NULL);
  431. return Result;
  432. }
  433. //---------------------------------------------------------------------------
  434. void __fastcall TConfiguration::Load(THierarchicalStorage * Storage)
  435. {
  436. TGuard Guard(FCriticalSection);
  437. TStorageAccessMode StorageAccessMode = Storage->AccessMode;
  438. try
  439. {
  440. Storage->AccessMode = smRead;
  441. LoadFrom(Storage);
  442. }
  443. __finally
  444. {
  445. Storage->AccessMode = StorageAccessMode;
  446. }
  447. }
  448. //---------------------------------------------------------------------------
  449. bool __fastcall TConfiguration::CopySubKey(THierarchicalStorage * Source, THierarchicalStorage * Target, const UnicodeString & Name)
  450. {
  451. bool Result = Source->OpenSubKey(Name, false);
  452. if (Result)
  453. {
  454. Result = Target->OpenSubKey(Name, true);
  455. if (!Result)
  456. {
  457. Source->CloseSubKey();
  458. }
  459. }
  460. return Result;
  461. }
  462. //---------------------------------------------------------------------------
  463. void __fastcall TConfiguration::CopyAllStringsInSubKey(
  464. THierarchicalStorage * Source, THierarchicalStorage * Target, const UnicodeString & Name)
  465. {
  466. if (CopySubKey(Source, Target, Name))
  467. {
  468. std::unique_ptr<TStrings> Names(new TStringList());
  469. Source->GetValueNames(Names.get());
  470. for (int Index = 0; Index < Names->Count; Index++)
  471. {
  472. UnicodeString Buf = Source->ReadStringRaw(Names->Strings[Index], UnicodeString());
  473. Target->WriteStringRaw(Names->Strings[Index], Buf);
  474. }
  475. Target->CloseSubKey();
  476. Source->CloseSubKey();
  477. }
  478. }
  479. //---------------------------------------------------------------------------
  480. void __fastcall TConfiguration::CopyData(THierarchicalStorage * Source,
  481. THierarchicalStorage * Target)
  482. {
  483. if (CopySubKey(Source, Target, ConfigurationSubKey))
  484. {
  485. if (CopySubKey(Source, Target, L"CDCache"))
  486. {
  487. std::unique_ptr<TStrings> Names(new TStringList());
  488. Source->GetValueNames(Names.get());
  489. for (int Index = 0; Index < Names->Count; Index++)
  490. {
  491. Target->WriteBinaryData(Names->Strings[Index], Source->ReadBinaryData(Names->Strings[Index]));
  492. }
  493. Target->CloseSubKey();
  494. Source->CloseSubKey();
  495. }
  496. CopyAllStringsInSubKey(Source, Target, L"Banners");
  497. CopyAllStringsInSubKey(Source, Target, LastFingerprintsStorageKey);
  498. Target->CloseSubKey();
  499. Source->CloseSubKey();
  500. }
  501. CopyAllStringsInSubKey(Source, Target, SshHostKeysSubKey);
  502. CopyAllStringsInSubKey(Source, Target, FtpsCertificateStorageKey);
  503. CopyAllStringsInSubKey(Source, Target, HttpsCertificateStorageKey);
  504. }
  505. //---------------------------------------------------------------------------
  506. void __fastcall TConfiguration::LoadDirectoryChangesCache(const UnicodeString SessionKey,
  507. TRemoteDirectoryChangesCache * DirectoryChangesCache)
  508. {
  509. THierarchicalStorage * Storage = CreateConfigStorage();
  510. try
  511. {
  512. Storage->AccessMode = smRead;
  513. if (Storage->OpenSubKey(ConfigurationSubKey, false) &&
  514. Storage->OpenSubKey(L"CDCache", false) &&
  515. Storage->ValueExists(SessionKey))
  516. {
  517. DirectoryChangesCache->Deserialize(Storage->ReadBinaryData(SessionKey));
  518. }
  519. }
  520. __finally
  521. {
  522. delete Storage;
  523. }
  524. }
  525. //---------------------------------------------------------------------------
  526. void __fastcall TConfiguration::SaveDirectoryChangesCache(const UnicodeString SessionKey,
  527. TRemoteDirectoryChangesCache * DirectoryChangesCache)
  528. {
  529. THierarchicalStorage * Storage = CreateConfigStorage();
  530. try
  531. {
  532. Storage->AccessMode = smReadWrite;
  533. if (Storage->OpenSubKey(ConfigurationSubKey, true) &&
  534. Storage->OpenSubKey(L"CDCache", true))
  535. {
  536. UnicodeString Data;
  537. DirectoryChangesCache->Serialize(Data);
  538. Storage->WriteBinaryData(SessionKey, Data);
  539. }
  540. }
  541. __finally
  542. {
  543. delete Storage;
  544. }
  545. }
  546. //---------------------------------------------------------------------------
  547. UnicodeString __fastcall TConfiguration::BannerHash(const UnicodeString & Banner)
  548. {
  549. RawByteString Result;
  550. Result.SetLength(16);
  551. md5checksum(
  552. reinterpret_cast<const char*>(Banner.c_str()), Banner.Length() * sizeof(wchar_t),
  553. (unsigned char*)Result.c_str());
  554. return BytesToHex(Result);
  555. }
  556. //---------------------------------------------------------------------------
  557. void __fastcall TConfiguration::GetBannerData(
  558. const UnicodeString & SessionKey, UnicodeString & BannerHash, unsigned int & Params)
  559. {
  560. BannerHash = UnicodeString();
  561. Params = 0;
  562. std::unique_ptr<THierarchicalStorage> Storage(CreateConfigStorage());
  563. Storage->AccessMode = smRead;
  564. if (Storage->OpenSubKey(ConfigurationSubKey, false) &&
  565. Storage->OpenSubKey(L"Banners", false))
  566. {
  567. UnicodeString S = Storage->ReadString(SessionKey, L"");
  568. BannerHash = CutToChar(S, L',', true);
  569. Params = StrToIntDef(L"$" + CutToChar(S, L',', true), 0);
  570. }
  571. }
  572. //---------------------------------------------------------------------------
  573. bool __fastcall TConfiguration::ShowBanner(
  574. const UnicodeString & SessionKey, const UnicodeString & Banner, unsigned int & Params)
  575. {
  576. UnicodeString StoredBannerHash;
  577. GetBannerData(SessionKey, StoredBannerHash, Params);
  578. bool Result = (StoredBannerHash != BannerHash(Banner));
  579. return Result;
  580. }
  581. //---------------------------------------------------------------------------
  582. void __fastcall TConfiguration::SetBannerData(
  583. const UnicodeString & SessionKey, const UnicodeString & BannerHash, unsigned int Params)
  584. {
  585. std::unique_ptr<THierarchicalStorage> Storage(CreateConfigStorage());
  586. Storage->AccessMode = smReadWrite;
  587. if (Storage->OpenSubKey(ConfigurationSubKey, true) &&
  588. Storage->OpenSubKey(L"Banners", true))
  589. {
  590. Storage->WriteString(SessionKey, BannerHash + L"," + UIntToStr(Params));
  591. }
  592. }
  593. //---------------------------------------------------------------------------
  594. void __fastcall TConfiguration::NeverShowBanner(const UnicodeString & SessionKey, const UnicodeString & Banner)
  595. {
  596. UnicodeString DummyBannerHash;
  597. unsigned int Params;
  598. GetBannerData(SessionKey, DummyBannerHash, Params);
  599. SetBannerData(SessionKey, BannerHash(Banner), Params);
  600. }
  601. //---------------------------------------------------------------------------
  602. void __fastcall TConfiguration::SetBannerParams(const UnicodeString & SessionKey, unsigned int Params)
  603. {
  604. UnicodeString BannerHash;
  605. unsigned int DummyParams;
  606. GetBannerData(SessionKey, BannerHash, DummyParams);
  607. SetBannerData(SessionKey, BannerHash, Params);
  608. }
  609. //---------------------------------------------------------------------------
  610. UnicodeString __fastcall TConfiguration::FormatFingerprintKey(const UnicodeString & SiteKey, const UnicodeString & FingerprintType)
  611. {
  612. return FORMAT(L"%s:%s", (SiteKey, FingerprintType));
  613. }
  614. //---------------------------------------------------------------------------
  615. void __fastcall TConfiguration::RememberLastFingerprint(const UnicodeString & SiteKey, const UnicodeString & FingerprintType, const UnicodeString & Fingerprint)
  616. {
  617. std::unique_ptr<THierarchicalStorage> Storage(CreateConfigStorage());
  618. Storage->AccessMode = smReadWrite;
  619. if (Storage->OpenSubKey(ConfigurationSubKey, true) &&
  620. Storage->OpenSubKey(LastFingerprintsStorageKey, true))
  621. {
  622. UnicodeString FingerprintKey = FormatFingerprintKey(SiteKey, FingerprintType);
  623. Storage->WriteString(FingerprintKey, Fingerprint);
  624. }
  625. }
  626. //---------------------------------------------------------------------------
  627. UnicodeString __fastcall TConfiguration::LastFingerprint(const UnicodeString & SiteKey, const UnicodeString & FingerprintType)
  628. {
  629. UnicodeString Result;
  630. std::unique_ptr<THierarchicalStorage> Storage(CreateConfigStorage());
  631. Storage->AccessMode = smRead;
  632. if (Storage->OpenSubKey(ConfigurationSubKey, false) &&
  633. Storage->OpenSubKey(LastFingerprintsStorageKey, false))
  634. {
  635. UnicodeString FingerprintKey = FormatFingerprintKey(SiteKey, FingerprintType);
  636. Result = Storage->ReadString(FingerprintKey, L"");
  637. }
  638. return Result;
  639. }
  640. //---------------------------------------------------------------------------
  641. void __fastcall TConfiguration::Changed()
  642. {
  643. TNotifyEvent AOnChange = NULL;
  644. {
  645. TGuard Guard(FCriticalSection);
  646. if (FUpdating == 0)
  647. {
  648. AOnChange = OnChange;
  649. }
  650. else
  651. {
  652. FChanged = true;
  653. }
  654. }
  655. // No specific reason to call this outside of a guard, just that it is less of a change to a previous unguarded code
  656. if (AOnChange != NULL)
  657. {
  658. AOnChange(this);
  659. }
  660. }
  661. //---------------------------------------------------------------------------
  662. void __fastcall TConfiguration::BeginUpdate()
  663. {
  664. FCriticalSection->Enter();
  665. if (FUpdating == 0)
  666. {
  667. FChanged = false;
  668. }
  669. FUpdating++;
  670. // Greater value would probably indicate some nesting problem in code
  671. DebugAssert(FUpdating < 6);
  672. }
  673. //---------------------------------------------------------------------------
  674. void __fastcall TConfiguration::EndUpdate()
  675. {
  676. DebugAssert(FUpdating > 0);
  677. FUpdating--;
  678. if ((FUpdating == 0) && FChanged)
  679. {
  680. FChanged = false;
  681. Changed();
  682. }
  683. FCriticalSection->Leave();
  684. }
  685. //---------------------------------------------------------------------------
  686. void __fastcall TConfiguration::CleanupConfiguration()
  687. {
  688. try
  689. {
  690. CleanupRegistry(ConfigurationSubKey);
  691. if (Storage == stRegistry)
  692. {
  693. FDontSave = true;
  694. }
  695. }
  696. catch (Exception &E)
  697. {
  698. throw ExtException(&E, LoadStr(CLEANUP_CONFIG_ERROR));
  699. }
  700. }
  701. //---------------------------------------------------------------------------
  702. void __fastcall TConfiguration::CleanupRegistry(UnicodeString CleanupSubKey)
  703. {
  704. TRegistryStorage *Registry = new TRegistryStorage(RegistryStorageKey);
  705. try
  706. {
  707. Registry->RecursiveDeleteSubKey(CleanupSubKey);
  708. }
  709. __finally
  710. {
  711. delete Registry;
  712. }
  713. }
  714. //---------------------------------------------------------------------------
  715. void __fastcall TConfiguration::CleanupHostKeys()
  716. {
  717. try
  718. {
  719. CleanupRegistry(SshHostKeysSubKey);
  720. }
  721. catch (Exception &E)
  722. {
  723. throw ExtException(&E, LoadStr(CLEANUP_HOSTKEYS_ERROR));
  724. }
  725. }
  726. //---------------------------------------------------------------------------
  727. void __fastcall TConfiguration::CleanupRandomSeedFile()
  728. {
  729. try
  730. {
  731. DontSaveRandomSeed();
  732. if (FileExists(ApiPath(RandomSeedFileName)))
  733. {
  734. DeleteFileChecked(RandomSeedFileName);
  735. }
  736. }
  737. catch (Exception &E)
  738. {
  739. throw ExtException(&E, LoadStr(CLEANUP_SEEDFILE_ERROR));
  740. }
  741. }
  742. //---------------------------------------------------------------------------
  743. void __fastcall TConfiguration::CleanupIniFile()
  744. {
  745. try
  746. {
  747. if (FileExists(ApiPath(IniFileStorageNameForReading)))
  748. {
  749. DeleteFileChecked(IniFileStorageNameForReading);
  750. }
  751. if (Storage == stIniFile)
  752. {
  753. FDontSave = true;
  754. }
  755. }
  756. catch (Exception &E)
  757. {
  758. throw ExtException(&E, LoadStr(CLEANUP_INIFILE_ERROR));
  759. }
  760. }
  761. //---------------------------------------------------------------------------
  762. void __fastcall TConfiguration::DontSave()
  763. {
  764. FDontSave = true;
  765. }
  766. //---------------------------------------------------------------------------
  767. RawByteString __fastcall TConfiguration::EncryptPassword(UnicodeString Password, UnicodeString Key)
  768. {
  769. if (Password.IsEmpty())
  770. {
  771. return RawByteString();
  772. }
  773. else
  774. {
  775. return ::EncryptPassword(Password, Key);
  776. }
  777. }
  778. //---------------------------------------------------------------------------
  779. UnicodeString __fastcall TConfiguration::DecryptPassword(RawByteString Password, UnicodeString Key)
  780. {
  781. if (Password.IsEmpty())
  782. {
  783. return UnicodeString();
  784. }
  785. else
  786. {
  787. return ::DecryptPassword(Password, Key);
  788. }
  789. }
  790. //---------------------------------------------------------------------------
  791. RawByteString __fastcall TConfiguration::StronglyRecryptPassword(RawByteString Password, UnicodeString /*Key*/)
  792. {
  793. return Password;
  794. }
  795. //---------------------------------------------------------------------------
  796. TVSFixedFileInfo *__fastcall TConfiguration::GetFixedApplicationInfo()
  797. {
  798. return GetFixedFileInfo(ApplicationInfo);
  799. }
  800. //---------------------------------------------------------------------------
  801. int __fastcall TConfiguration::GetCompoundVersion()
  802. {
  803. TVSFixedFileInfo * FileInfo = FixedApplicationInfo;
  804. return CalculateCompoundVersion(
  805. HIWORD(FileInfo->dwFileVersionMS), LOWORD(FileInfo->dwFileVersionMS),
  806. HIWORD(FileInfo->dwFileVersionLS), LOWORD(FileInfo->dwFileVersionLS));
  807. }
  808. //---------------------------------------------------------------------------
  809. UnicodeString __fastcall TConfiguration::ModuleFileName()
  810. {
  811. return ParamStr(0);
  812. }
  813. //---------------------------------------------------------------------------
  814. void * __fastcall TConfiguration::GetFileApplicationInfo(const UnicodeString FileName)
  815. {
  816. void * Result;
  817. if (FileName.IsEmpty())
  818. {
  819. if (!FApplicationInfo)
  820. {
  821. FApplicationInfo = CreateFileInfo(ModuleFileName());
  822. }
  823. Result = FApplicationInfo;
  824. }
  825. else
  826. {
  827. Result = CreateFileInfo(FileName);
  828. }
  829. return Result;
  830. }
  831. //---------------------------------------------------------------------------
  832. void * __fastcall TConfiguration::GetApplicationInfo()
  833. {
  834. return GetFileApplicationInfo("");
  835. }
  836. //---------------------------------------------------------------------------
  837. UnicodeString __fastcall TConfiguration::GetFileProductName(const UnicodeString FileName)
  838. {
  839. return GetFileFileInfoString(L"ProductName", FileName);
  840. }
  841. //---------------------------------------------------------------------------
  842. UnicodeString __fastcall TConfiguration::GetFileCompanyName(const UnicodeString FileName)
  843. {
  844. // particularly in IDE build, company name is empty
  845. return GetFileFileInfoString(L"CompanyName", FileName, true);
  846. }
  847. //---------------------------------------------------------------------------
  848. UnicodeString __fastcall TConfiguration::GetProductName()
  849. {
  850. return GetFileProductName(L"");
  851. }
  852. //---------------------------------------------------------------------------
  853. UnicodeString __fastcall TConfiguration::GetCompanyName()
  854. {
  855. return GetFileCompanyName(L"");
  856. }
  857. //---------------------------------------------------------------------------
  858. UnicodeString __fastcall TConfiguration::GetFileProductVersion(const UnicodeString FileName)
  859. {
  860. return TrimVersion(GetFileFileInfoString(L"ProductVersion", FileName));
  861. }
  862. //---------------------------------------------------------------------------
  863. UnicodeString __fastcall TConfiguration::GetFileDescription(const UnicodeString & FileName)
  864. {
  865. return GetFileFileInfoString(L"FileDescription", FileName);
  866. }
  867. //---------------------------------------------------------------------------
  868. UnicodeString __fastcall TConfiguration::GetProductVersion()
  869. {
  870. return GetFileProductVersion(L"");
  871. }
  872. //---------------------------------------------------------------------------
  873. UnicodeString __fastcall TConfiguration::GetReleaseType()
  874. {
  875. return GetFileInfoString(L"ReleaseType");
  876. }
  877. //---------------------------------------------------------------------------
  878. bool __fastcall TConfiguration::GetIsUnofficial()
  879. {
  880. #ifdef BUILD_OFFICIAL
  881. return false;
  882. #else
  883. return true;
  884. #endif
  885. }
  886. //---------------------------------------------------------------------------
  887. UnicodeString __fastcall TConfiguration::GetVersionStr()
  888. {
  889. TGuard Guard(FCriticalSection);
  890. try
  891. {
  892. UnicodeString BuildStr;
  893. if (!IsUnofficial)
  894. {
  895. BuildStr = LoadStr(VERSION_BUILD);
  896. }
  897. else
  898. {
  899. #ifdef _DEBUG
  900. BuildStr = LoadStr(VERSION_DEBUG_BUILD);
  901. #else
  902. BuildStr = LoadStr(VERSION_DEV_BUILD);
  903. #endif
  904. }
  905. int Build = LOWORD(FixedApplicationInfo->dwFileVersionLS);
  906. if (Build > 0)
  907. {
  908. BuildStr += L" " + IntToStr(Build);
  909. }
  910. #ifndef BUILD_OFFICIAL
  911. UnicodeString BuildDate = __DATE__;
  912. UnicodeString MonthStr = CutToChar(BuildDate, L' ', true);
  913. int Month = ParseShortEngMonthName(MonthStr);
  914. int Day = StrToInt(CutToChar(BuildDate, L' ', true));
  915. int Year = StrToInt(Trim(BuildDate));
  916. UnicodeString DateStr = FORMAT(L"%d-%2.2d-%2.2d", (Year, Month, Day));
  917. AddToList(BuildStr, DateStr, L" ");
  918. #endif
  919. UnicodeString FullVersion = Version;
  920. UnicodeString AReleaseType = GetReleaseType();
  921. if (DebugAlwaysTrue(!AReleaseType.IsEmpty()) &&
  922. !SameText(AReleaseType, L"stable") &&
  923. !SameText(AReleaseType, L"development"))
  924. {
  925. FullVersion += L" " + AReleaseType;
  926. }
  927. UnicodeString Result = FMTLOAD(VERSION2, (FullVersion, BuildStr));
  928. #ifndef BUILD_OFFICIAL
  929. Result += L" " + LoadStr(VERSION_DONT_DISTRIBUTE);
  930. #endif
  931. return Result;
  932. }
  933. catch (Exception &E)
  934. {
  935. throw ExtException(&E, L"Can't get application version");
  936. }
  937. }
  938. //---------------------------------------------------------------------------
  939. UnicodeString __fastcall TConfiguration::GetFileVersion(const UnicodeString & FileName)
  940. {
  941. UnicodeString Result;
  942. void * FileInfo = CreateFileInfo(FileName);
  943. try
  944. {
  945. Result = GetFileVersion(GetFixedFileInfo(FileInfo));
  946. }
  947. __finally
  948. {
  949. FreeFileInfo(FileInfo);
  950. }
  951. return Result;
  952. }
  953. //---------------------------------------------------------------------------
  954. UnicodeString __fastcall TConfiguration::GetFileVersion(TVSFixedFileInfo * Info)
  955. {
  956. TGuard Guard(FCriticalSection);
  957. try
  958. {
  959. UnicodeString Result =
  960. FormatVersion(
  961. HIWORD(Info->dwFileVersionMS),
  962. LOWORD(Info->dwFileVersionMS),
  963. HIWORD(Info->dwFileVersionLS));
  964. return Result;
  965. }
  966. catch (Exception &E)
  967. {
  968. throw ExtException(&E, L"Can't get file version");
  969. }
  970. }
  971. //---------------------------------------------------------------------------
  972. UnicodeString __fastcall TConfiguration::GetVersion()
  973. {
  974. return GetFileVersion(FixedApplicationInfo);
  975. }
  976. //---------------------------------------------------------------------------
  977. UnicodeString __fastcall TConfiguration::GetFileFileInfoString(const UnicodeString Key,
  978. const UnicodeString FileName, bool AllowEmpty)
  979. {
  980. TGuard Guard(FCriticalSection);
  981. UnicodeString Result;
  982. void * Info = GetFileApplicationInfo(FileName);
  983. try
  984. {
  985. if ((Info != NULL) && (GetTranslationCount(Info) > 0))
  986. {
  987. TTranslation Translation;
  988. Translation = GetTranslation(Info, 0);
  989. Result = ::GetFileInfoString(Info, Translation, Key, AllowEmpty);
  990. }
  991. else
  992. {
  993. DebugAssert(!FileName.IsEmpty());
  994. }
  995. }
  996. __finally
  997. {
  998. if (!FileName.IsEmpty())
  999. {
  1000. FreeFileInfo(Info);
  1001. }
  1002. }
  1003. return Result;
  1004. }
  1005. //---------------------------------------------------------------------------
  1006. UnicodeString __fastcall TConfiguration::GetFileInfoString(const UnicodeString Key)
  1007. {
  1008. return GetFileFileInfoString(Key, L"");
  1009. }
  1010. //---------------------------------------------------------------------------
  1011. UnicodeString __fastcall TConfiguration::GetFileMimeType(const UnicodeString & FileName)
  1012. {
  1013. UnicodeString Result;
  1014. bool Found = false;
  1015. if (!MimeTypes.IsEmpty())
  1016. {
  1017. UnicodeString FileNameOnly = ExtractFileName(FileName);
  1018. UnicodeString AMimeTypes = MimeTypes;
  1019. while (!Found && !AMimeTypes.IsEmpty())
  1020. {
  1021. UnicodeString Token = CutToChar(AMimeTypes, L',', true);
  1022. UnicodeString MaskStr = CutToChar(Token, L'=', true);
  1023. TFileMasks Mask(MaskStr);
  1024. if (Mask.Matches(FileNameOnly))
  1025. {
  1026. Result = Token.Trim();
  1027. Found = true;
  1028. }
  1029. }
  1030. }
  1031. if (!Found) // allow an override to "no" Content-Type
  1032. {
  1033. Result = ::GetFileMimeType(FileName);
  1034. }
  1035. return Result;
  1036. }
  1037. //---------------------------------------------------------------------------
  1038. UnicodeString __fastcall TConfiguration::GetRegistryStorageKey()
  1039. {
  1040. return GetRegistryKey();
  1041. }
  1042. //---------------------------------------------------------------------------
  1043. void __fastcall TConfiguration::SetNulStorage()
  1044. {
  1045. FStorage = stNul;
  1046. }
  1047. //---------------------------------------------------------------------------
  1048. void __fastcall TConfiguration::SetDefaultStorage()
  1049. {
  1050. FStorage = stDetect;
  1051. }
  1052. //---------------------------------------------------------------------------
  1053. void __fastcall TConfiguration::SetIniFileStorageName(UnicodeString value)
  1054. {
  1055. FIniFileStorageName = value;
  1056. FStorage = stIniFile;
  1057. }
  1058. //---------------------------------------------------------------------------
  1059. UnicodeString __fastcall TConfiguration::GetDefaultIniFileExportPath()
  1060. {
  1061. UnicodeString PersonalDirectory = GetPersonalFolder();
  1062. UnicodeString FileName = IncludeTrailingBackslash(PersonalDirectory) +
  1063. ExtractFileName(ExpandEnvironmentVariables(IniFileStorageName));
  1064. return FileName;
  1065. }
  1066. //---------------------------------------------------------------------------
  1067. UnicodeString __fastcall TConfiguration::GetIniFileStorageNameForReading()
  1068. {
  1069. return GetIniFileStorageName(true);
  1070. }
  1071. //---------------------------------------------------------------------------
  1072. UnicodeString __fastcall TConfiguration::GetIniFileStorageNameForReadingWriting()
  1073. {
  1074. return GetIniFileStorageName(false);
  1075. }
  1076. //---------------------------------------------------------------------------
  1077. UnicodeString __fastcall TConfiguration::GetAutomaticIniFileStorageName(bool ReadingOnly)
  1078. {
  1079. UnicodeString ProgramPath = ParamStr(0);
  1080. UnicodeString ProgramIniPath = ChangeFileExt(ProgramPath, L".ini");
  1081. UnicodeString IniPath;
  1082. if (FileExists(ApiPath(ProgramIniPath)))
  1083. {
  1084. IniPath = ProgramIniPath;
  1085. }
  1086. else
  1087. {
  1088. UnicodeString AppDataIniPath =
  1089. IncludeTrailingBackslash(GetShellFolderPath(CSIDL_APPDATA)) +
  1090. ExtractFileName(ProgramIniPath);
  1091. if (FileExists(ApiPath(AppDataIniPath)))
  1092. {
  1093. IniPath = AppDataIniPath;
  1094. }
  1095. else
  1096. {
  1097. // avoid expensive test if we are interested in existing files only
  1098. if (!ReadingOnly && (FProgramIniPathWrittable < 0))
  1099. {
  1100. UnicodeString ProgramDir = ExtractFilePath(ProgramPath);
  1101. FProgramIniPathWrittable = IsDirectoryWriteable(ProgramDir) ? 1 : 0;
  1102. }
  1103. // does not really matter what we return when < 0
  1104. IniPath = (FProgramIniPathWrittable == 0) ? AppDataIniPath : ProgramIniPath;
  1105. }
  1106. }
  1107. // BACKWARD COMPATIBILITY with 4.x
  1108. if (FVirtualIniFileStorageName.IsEmpty() &&
  1109. TPath::IsDriveRooted(IniPath))
  1110. {
  1111. UnicodeString LocalAppDataPath = GetShellFolderPath(CSIDL_LOCAL_APPDATA);
  1112. // virtual store for non-system drives have a different virtual store,
  1113. // do not bother about them
  1114. if (TPath::IsDriveRooted(LocalAppDataPath) &&
  1115. SameText(ExtractFileDrive(IniPath), ExtractFileDrive(LocalAppDataPath)))
  1116. {
  1117. FVirtualIniFileStorageName =
  1118. IncludeTrailingBackslash(LocalAppDataPath) +
  1119. L"VirtualStore\\" +
  1120. IniPath.SubString(4, IniPath.Length() - 3);
  1121. }
  1122. }
  1123. if (!FVirtualIniFileStorageName.IsEmpty() &&
  1124. FileExists(ApiPath(FVirtualIniFileStorageName)))
  1125. {
  1126. return FVirtualIniFileStorageName;
  1127. }
  1128. else
  1129. {
  1130. return IniPath;
  1131. }
  1132. }
  1133. //---------------------------------------------------------------------------
  1134. UnicodeString __fastcall TConfiguration::GetIniFileStorageName(bool ReadingOnly)
  1135. {
  1136. UnicodeString Result;
  1137. if (!FIniFileStorageName.IsEmpty())
  1138. {
  1139. Result = FIniFileStorageName;
  1140. }
  1141. else if (!FCustomIniFileStorageName.IsEmpty())
  1142. {
  1143. Result = FCustomIniFileStorageName;
  1144. }
  1145. else
  1146. {
  1147. Result = GetAutomaticIniFileStorageName(ReadingOnly);
  1148. }
  1149. return Result;
  1150. }
  1151. //---------------------------------------------------------------------------
  1152. void __fastcall TConfiguration::SetOptionsStorage(TStrings * value)
  1153. {
  1154. TGuard Guard(FCriticalSection);
  1155. if (FOptionsStorage.get() == NULL)
  1156. {
  1157. FOptionsStorage.reset(new TStringList());
  1158. }
  1159. FOptionsStorage->AddStrings(value);
  1160. }
  1161. //---------------------------------------------------------------------------
  1162. TStrings * __fastcall TConfiguration::GetOptionsStorage()
  1163. {
  1164. return FOptionsStorage.get();
  1165. }
  1166. //---------------------------------------------------------------------------
  1167. UnicodeString __fastcall TConfiguration::GetPuttySessionsKey()
  1168. {
  1169. return PuttyRegistryStorageKey + L"\\Sessions";
  1170. }
  1171. //---------------------------------------------------------------------------
  1172. UnicodeString __fastcall TConfiguration::GetStoredSessionsSubKey()
  1173. {
  1174. return L"Sessions";
  1175. }
  1176. //---------------------------------------------------------------------------
  1177. UnicodeString __fastcall TConfiguration::GetSshHostKeysSubKey()
  1178. {
  1179. return L"SshHostKeys";
  1180. }
  1181. //---------------------------------------------------------------------------
  1182. UnicodeString __fastcall TConfiguration::GetConfigurationSubKey()
  1183. {
  1184. return L"Configuration";
  1185. }
  1186. //---------------------------------------------------------------------------
  1187. UnicodeString __fastcall TConfiguration::GetRootKeyStr()
  1188. {
  1189. return RootKeyToStr(HKEY_CURRENT_USER);
  1190. }
  1191. //---------------------------------------------------------------------------
  1192. void __fastcall TConfiguration::MoveStorage(TStorage AStorage, const UnicodeString & ACustomIniFileStorageName)
  1193. {
  1194. if ((FStorage != AStorage) ||
  1195. !IsPathToSameFile(FCustomIniFileStorageName, ACustomIniFileStorageName))
  1196. {
  1197. TStorage StorageBak = FStorage;
  1198. UnicodeString CustomIniFileStorageNameBak = FCustomIniFileStorageName;
  1199. try
  1200. {
  1201. THierarchicalStorage * SourceStorage = NULL;
  1202. THierarchicalStorage * TargetStorage = NULL;
  1203. try
  1204. {
  1205. SourceStorage = CreateConfigStorage();
  1206. SourceStorage->AccessMode = smRead;
  1207. FStorage = AStorage;
  1208. FCustomIniFileStorageName = ACustomIniFileStorageName;
  1209. TargetStorage = CreateConfigStorage();
  1210. TargetStorage->AccessMode = smReadWrite;
  1211. TargetStorage->Explicit = true;
  1212. // copy before save as it removes the ini file,
  1213. // when switching from ini to registry
  1214. CopyData(SourceStorage, TargetStorage);
  1215. }
  1216. __finally
  1217. {
  1218. delete SourceStorage;
  1219. delete TargetStorage;
  1220. }
  1221. // save all and explicit,
  1222. // this also removes an INI file, when switching to registry storage
  1223. DoSave(true, true);
  1224. }
  1225. catch (...)
  1226. {
  1227. // If this fails, do not pretend that storage was switched.
  1228. // For instance:
  1229. // - When writing to an INI file fails (unlikely, as we fallback to user profile)
  1230. // - When removing INI file fails, when switching to registry
  1231. // (possible, when the INI file is in Program Files folder)
  1232. FStorage = StorageBak;
  1233. FCustomIniFileStorageName = CustomIniFileStorageNameBak;
  1234. throw;
  1235. }
  1236. }
  1237. }
  1238. //---------------------------------------------------------------------------
  1239. void __fastcall TConfiguration::ScheduleCustomIniFileStorageUse(const UnicodeString & ACustomIniFileStorageName)
  1240. {
  1241. FStorage = stIniFile;
  1242. FCustomIniFileStorageName = ACustomIniFileStorageName;
  1243. SaveCustomIniFileStorageName();
  1244. }
  1245. //---------------------------------------------------------------------------
  1246. void __fastcall TConfiguration::Saved()
  1247. {
  1248. // nothing
  1249. }
  1250. //---------------------------------------------------------------------------
  1251. TStorage __fastcall TConfiguration::GetStorage()
  1252. {
  1253. TGuard Guard(FCriticalSection);
  1254. if (FStorage == stDetect)
  1255. {
  1256. if (FileExists(ApiPath(IniFileStorageNameForReading)))
  1257. {
  1258. FStorage = stIniFile;
  1259. }
  1260. else
  1261. {
  1262. FStorage = stRegistry;
  1263. }
  1264. }
  1265. return FStorage;
  1266. }
  1267. //---------------------------------------------------------------------
  1268. TStoredSessionList * __fastcall TConfiguration::SelectFilezillaSessionsForImport(
  1269. TStoredSessionList * Sessions, UnicodeString & Error)
  1270. {
  1271. std::unique_ptr<TStoredSessionList> ImportSessionList(new TStoredSessionList(true));
  1272. ImportSessionList->DefaultSettings = Sessions->DefaultSettings;
  1273. UnicodeString AppDataPath = GetShellFolderPath(CSIDL_APPDATA);
  1274. UnicodeString FilezillaSiteManagerFile =
  1275. IncludeTrailingBackslash(AppDataPath) + L"FileZilla\\sitemanager.xml";
  1276. UnicodeString FilezillaConfigurationFile =
  1277. IncludeTrailingBackslash(AppDataPath) + L"FileZilla\\filezilla.xml";
  1278. if (FileExists(ApiPath(FilezillaSiteManagerFile)))
  1279. {
  1280. ImportSessionList->ImportFromFilezilla(FilezillaSiteManagerFile, FilezillaConfigurationFile);
  1281. if (ImportSessionList->Count > 0)
  1282. {
  1283. ImportSessionList->SelectSessionsToImport(Sessions, true);
  1284. }
  1285. else
  1286. {
  1287. Error = FMTLOAD(FILEZILLA_NO_SITES, (FilezillaSiteManagerFile));
  1288. }
  1289. }
  1290. else
  1291. {
  1292. Error = FMTLOAD(FILEZILLA_SITE_MANAGER_NOT_FOUND, (FilezillaSiteManagerFile));
  1293. }
  1294. return ImportSessionList.release();
  1295. }
  1296. //---------------------------------------------------------------------
  1297. bool __fastcall TConfiguration::AnyFilezillaSessionForImport(TStoredSessionList * Sessions)
  1298. {
  1299. try
  1300. {
  1301. UnicodeString Error;
  1302. std::unique_ptr<TStoredSessionList> Sesssions(SelectFilezillaSessionsForImport(Sessions, Error));
  1303. return (Sesssions->Count > 0);
  1304. }
  1305. catch (...)
  1306. {
  1307. return false;
  1308. }
  1309. }
  1310. //---------------------------------------------------------------------
  1311. TStoredSessionList * __fastcall TConfiguration::SelectKnownHostsSessionsForImport(
  1312. TStoredSessionList * Sessions, UnicodeString & Error)
  1313. {
  1314. std::unique_ptr<TStoredSessionList> ImportSessionList(new TStoredSessionList(true));
  1315. ImportSessionList->DefaultSettings = Sessions->DefaultSettings;
  1316. UnicodeString ProfilePath = GetShellFolderPath(CSIDL_PROFILE);
  1317. UnicodeString KnownHostsFile = IncludeTrailingBackslash(ProfilePath) + L".ssh\\known_hosts";
  1318. try
  1319. {
  1320. if (FileExists(ApiPath(KnownHostsFile)))
  1321. {
  1322. std::unique_ptr<TStrings> Lines(new TStringList());
  1323. LoadScriptFromFile(KnownHostsFile, Lines.get(), true);
  1324. ImportSessionList->ImportFromKnownHosts(Lines.get());
  1325. }
  1326. else
  1327. {
  1328. throw Exception(LoadStr(KNOWN_HOSTS_NOT_FOUND));
  1329. }
  1330. }
  1331. catch (Exception & E)
  1332. {
  1333. Error = FORMAT(L"%s\n(%s)", (E.Message, KnownHostsFile));
  1334. }
  1335. return ImportSessionList.release();
  1336. }
  1337. //---------------------------------------------------------------------
  1338. TStoredSessionList * __fastcall TConfiguration::SelectKnownHostsSessionsForImport(
  1339. TStrings * Lines, TStoredSessionList * Sessions, UnicodeString & Error)
  1340. {
  1341. std::unique_ptr<TStoredSessionList> ImportSessionList(new TStoredSessionList(true));
  1342. ImportSessionList->DefaultSettings = Sessions->DefaultSettings;
  1343. try
  1344. {
  1345. ImportSessionList->ImportFromKnownHosts(Lines);
  1346. }
  1347. catch (Exception & E)
  1348. {
  1349. Error = E.Message;
  1350. }
  1351. return ImportSessionList.release();
  1352. }
  1353. //---------------------------------------------------------------------------
  1354. void __fastcall TConfiguration::SetRandomSeedFile(UnicodeString value)
  1355. {
  1356. if (RandomSeedFile != value)
  1357. {
  1358. UnicodeString PrevRandomSeedFileName = RandomSeedFileName;
  1359. FRandomSeedFile = value;
  1360. // never allow empty seed file to avoid Putty trying to reinitialize the path
  1361. if (RandomSeedFileName.IsEmpty())
  1362. {
  1363. FRandomSeedFile = FDefaultRandomSeedFile;
  1364. }
  1365. if (!PrevRandomSeedFileName.IsEmpty() &&
  1366. (PrevRandomSeedFileName != RandomSeedFileName) &&
  1367. FileExists(ApiPath(PrevRandomSeedFileName)))
  1368. {
  1369. // ignore any error
  1370. DeleteFile(ApiPath(PrevRandomSeedFileName));
  1371. }
  1372. }
  1373. }
  1374. //---------------------------------------------------------------------------
  1375. UnicodeString __fastcall TConfiguration::GetDirectoryStatisticsCacheKey(
  1376. const UnicodeString & SessionKey, const UnicodeString & Path, const TCopyParamType & CopyParam)
  1377. {
  1378. std::unique_ptr<TStringList> RawOptions(new TStringList());
  1379. RawOptions->Add(SessionKey);
  1380. RawOptions->Add(UnixExcludeTrailingBackslash(Path));
  1381. TCopyParamType Defaults;
  1382. TCopyParamType FilterCopyParam;
  1383. FilterCopyParam.IncludeFileMask = CopyParam.IncludeFileMask;
  1384. FilterCopyParam.ExcludeHiddenFiles = CopyParam.ExcludeHiddenFiles;
  1385. FilterCopyParam.ExcludeEmptyDirectories = CopyParam.ExcludeEmptyDirectories;
  1386. std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(RawOptions.get(), true));
  1387. FilterCopyParam.Save(OptionsStorage.get(), &Defaults);
  1388. UTF8String RawOptionsBuf(RawOptions->CommaText.LowerCase());
  1389. UnicodeString Result = Sha256(RawOptionsBuf.c_str(), RawOptionsBuf.Length());
  1390. return Result;
  1391. }
  1392. //---------------------------------------------------------------------------
  1393. THierarchicalStorage * TConfiguration::OpenDirectoryStatisticsCache(bool CanCreate)
  1394. {
  1395. std::unique_ptr<THierarchicalStorage> Storage(Configuration->CreateConfigStorage());
  1396. Storage->AccessMode = CanCreate ? smReadWrite : smRead;
  1397. if (!Storage->OpenSubKey(L"DirectoryStatisticsCache", CanCreate))
  1398. {
  1399. Storage.reset(NULL);
  1400. }
  1401. return Storage.release();
  1402. }
  1403. //---------------------------------------------------------------------------
  1404. TStrings * __fastcall TConfiguration::LoadDirectoryStatisticsCache(
  1405. const UnicodeString & SessionKey, const UnicodeString & Path, const TCopyParamType & CopyParam)
  1406. {
  1407. std::unique_ptr<THierarchicalStorage> Storage(OpenDirectoryStatisticsCache(false));
  1408. std::unique_ptr<TStringList> Result(new TStringList());
  1409. if (Storage.get() != NULL)
  1410. {
  1411. UnicodeString Key = GetDirectoryStatisticsCacheKey(SessionKey, Path, CopyParam);
  1412. UnicodeString Buf = Storage->ReadString(Key, UnicodeString());
  1413. Result->CommaText = Buf;
  1414. }
  1415. return Result.release();
  1416. }
  1417. //---------------------------------------------------------------------------
  1418. void __fastcall TConfiguration::SaveDirectoryStatisticsCache(
  1419. const UnicodeString & SessionKey, const UnicodeString & Path, const TCopyParamType & CopyParam, TStrings * DataList)
  1420. {
  1421. std::unique_ptr<THierarchicalStorage> Storage(OpenDirectoryStatisticsCache(true));
  1422. if (Storage.get() != NULL)
  1423. {
  1424. UnicodeString Key = GetDirectoryStatisticsCacheKey(SessionKey, Path, CopyParam);
  1425. UnicodeString Buf = DataList->CommaText;
  1426. Storage->WriteString(Key, Buf);
  1427. }
  1428. }
  1429. //---------------------------------------------------------------------
  1430. UnicodeString __fastcall TConfiguration::GetRandomSeedFileName()
  1431. {
  1432. // StripPathQuotes should not be needed as we do not feed quotes anymore
  1433. return StripPathQuotes(ExpandEnvironmentVariables(FRandomSeedFile)).Trim();
  1434. }
  1435. //---------------------------------------------------------------------
  1436. void __fastcall TConfiguration::SetExternalIpAddress(UnicodeString value)
  1437. {
  1438. SET_CONFIG_PROPERTY(ExternalIpAddress);
  1439. }
  1440. //---------------------------------------------------------------------
  1441. void __fastcall TConfiguration::SetMimeTypes(UnicodeString value)
  1442. {
  1443. SET_CONFIG_PROPERTY(MimeTypes);
  1444. }
  1445. //---------------------------------------------------------------------
  1446. void __fastcall TConfiguration::SetTryFtpWhenSshFails(bool value)
  1447. {
  1448. SET_CONFIG_PROPERTY(TryFtpWhenSshFails);
  1449. }
  1450. //---------------------------------------------------------------------
  1451. void __fastcall TConfiguration::SetParallelDurationThreshold(int value)
  1452. {
  1453. SET_CONFIG_PROPERTY(ParallelDurationThreshold);
  1454. }
  1455. //---------------------------------------------------------------------
  1456. void __fastcall TConfiguration::SetPuttyRegistryStorageKey(UnicodeString value)
  1457. {
  1458. SET_CONFIG_PROPERTY(PuttyRegistryStorageKey);
  1459. }
  1460. //---------------------------------------------------------------------------
  1461. TEOLType __fastcall TConfiguration::GetLocalEOLType()
  1462. {
  1463. return eolCRLF;
  1464. }
  1465. //---------------------------------------------------------------------
  1466. bool __fastcall TConfiguration::GetCollectUsage()
  1467. {
  1468. return FUsage->Collect;
  1469. }
  1470. //---------------------------------------------------------------------
  1471. void __fastcall TConfiguration::SetCollectUsage(bool value)
  1472. {
  1473. FUsage->Collect = value;
  1474. }
  1475. //---------------------------------------------------------------------
  1476. void __fastcall TConfiguration::TemporaryLogging(const UnicodeString ALogFileName)
  1477. {
  1478. if (SameText(ExtractFileExt(ALogFileName), L".xml"))
  1479. {
  1480. TemporaryActionsLogging(ALogFileName);
  1481. }
  1482. else
  1483. {
  1484. FLogging = true;
  1485. FLogFileName = ALogFileName;
  1486. UpdateActualLogProtocol();
  1487. }
  1488. }
  1489. //---------------------------------------------------------------------
  1490. void __fastcall TConfiguration::TemporaryActionsLogging(const UnicodeString ALogFileName)
  1491. {
  1492. FLogActions = true;
  1493. FActionsLogFileName = ALogFileName;
  1494. }
  1495. //---------------------------------------------------------------------
  1496. void __fastcall TConfiguration::TemporaryLogProtocol(int ALogProtocol)
  1497. {
  1498. FLogProtocol = ALogProtocol;
  1499. UpdateActualLogProtocol();
  1500. }
  1501. //---------------------------------------------------------------------
  1502. void __fastcall TConfiguration::TemporaryLogSensitive(bool ALogSensitive)
  1503. {
  1504. FLogSensitive = ALogSensitive;
  1505. }
  1506. //---------------------------------------------------------------------
  1507. void __fastcall TConfiguration::TemporaryLogMaxSize(__int64 ALogMaxSize)
  1508. {
  1509. FLogMaxSize = ALogMaxSize;
  1510. }
  1511. //---------------------------------------------------------------------
  1512. void __fastcall TConfiguration::TemporaryLogMaxCount(int ALogMaxCount)
  1513. {
  1514. FLogMaxCount = ALogMaxCount;
  1515. }
  1516. //---------------------------------------------------------------------
  1517. void __fastcall TConfiguration::SetLogging(bool value)
  1518. {
  1519. TGuard Guard(FCriticalSection);
  1520. if (Logging != value)
  1521. {
  1522. FPermanentLogging = value;
  1523. FLogging = value;
  1524. UpdateActualLogProtocol();
  1525. Changed();
  1526. }
  1527. }
  1528. //---------------------------------------------------------------------
  1529. bool __fastcall TConfiguration::GetLogging()
  1530. {
  1531. TGuard Guard(FCriticalSection);
  1532. return FPermanentLogging;
  1533. }
  1534. //---------------------------------------------------------------------
  1535. void __fastcall TConfiguration::SetLogFileName(UnicodeString value)
  1536. {
  1537. TGuard Guard(FCriticalSection);
  1538. if (LogFileName != value)
  1539. {
  1540. FPermanentLogFileName = value;
  1541. FLogFileName = value;
  1542. Changed();
  1543. }
  1544. }
  1545. //---------------------------------------------------------------------
  1546. UnicodeString __fastcall TConfiguration::GetLogFileName()
  1547. {
  1548. TGuard Guard(FCriticalSection);
  1549. return FPermanentLogFileName;
  1550. }
  1551. //---------------------------------------------------------------------
  1552. void __fastcall TConfiguration::SetActionsLogFileName(UnicodeString value)
  1553. {
  1554. TGuard Guard(FCriticalSection);
  1555. if (ActionsLogFileName != value)
  1556. {
  1557. FPermanentActionsLogFileName = value;
  1558. FActionsLogFileName = value;
  1559. Changed();
  1560. }
  1561. }
  1562. //---------------------------------------------------------------------
  1563. UnicodeString __fastcall TConfiguration::GetPermanentActionsLogFileName()
  1564. {
  1565. TGuard Guard(FCriticalSection);
  1566. return FPermanentActionsLogFileName;
  1567. }
  1568. //---------------------------------------------------------------------
  1569. UnicodeString __fastcall TConfiguration::GetActionsLogFileName()
  1570. {
  1571. TGuard Guard(FCriticalSection);
  1572. return FActionsLogFileName;
  1573. }
  1574. //---------------------------------------------------------------------
  1575. bool __fastcall TConfiguration::GetLogToFile()
  1576. {
  1577. // guarded within GetLogFileName
  1578. return !LogFileName.IsEmpty();
  1579. }
  1580. //---------------------------------------------------------------------
  1581. void __fastcall TConfiguration::UpdateActualLogProtocol()
  1582. {
  1583. FActualLogProtocol = FLogging ? FLogProtocol : 0;
  1584. }
  1585. //---------------------------------------------------------------------
  1586. void __fastcall TConfiguration::SetLogProtocol(int value)
  1587. {
  1588. TGuard Guard(FCriticalSection);
  1589. if (LogProtocol != value)
  1590. {
  1591. FPermanentLogProtocol = value;
  1592. FLogProtocol = value;
  1593. Changed();
  1594. UpdateActualLogProtocol();
  1595. }
  1596. }
  1597. //---------------------------------------------------------------------
  1598. void __fastcall TConfiguration::SetLogActions(bool value)
  1599. {
  1600. TGuard Guard(FCriticalSection);
  1601. if (LogActions != value)
  1602. {
  1603. FPermanentLogActions = value;
  1604. FLogActions = value;
  1605. Changed();
  1606. }
  1607. }
  1608. //---------------------------------------------------------------------
  1609. bool __fastcall TConfiguration::GetLogActions()
  1610. {
  1611. TGuard Guard(FCriticalSection);
  1612. return FPermanentLogActions;
  1613. }
  1614. //---------------------------------------------------------------------
  1615. void __fastcall TConfiguration::SetLogFileAppend(bool value)
  1616. {
  1617. SET_CONFIG_PROPERTY(LogFileAppend);
  1618. }
  1619. //---------------------------------------------------------------------
  1620. void __fastcall TConfiguration::SetLogSensitive(bool value)
  1621. {
  1622. if (LogSensitive != value)
  1623. {
  1624. FPermanentLogSensitive = value;
  1625. FLogSensitive = value;
  1626. Changed();
  1627. }
  1628. }
  1629. //---------------------------------------------------------------------
  1630. void __fastcall TConfiguration::SetLogMaxSize(__int64 value)
  1631. {
  1632. TGuard Guard(FCriticalSection);
  1633. if (LogMaxSize != value)
  1634. {
  1635. FPermanentLogMaxSize = value;
  1636. FLogMaxSize = value;
  1637. Changed();
  1638. }
  1639. }
  1640. //---------------------------------------------------------------------
  1641. __int64 __fastcall TConfiguration::GetLogMaxSize()
  1642. {
  1643. TGuard Guard(FCriticalSection);
  1644. return FPermanentLogMaxSize;
  1645. }
  1646. //---------------------------------------------------------------------
  1647. void __fastcall TConfiguration::SetLogMaxCount(int value)
  1648. {
  1649. if (LogMaxCount != value)
  1650. {
  1651. FPermanentLogMaxCount = value;
  1652. FLogMaxCount = value;
  1653. Changed();
  1654. }
  1655. }
  1656. //---------------------------------------------------------------------
  1657. int __fastcall TConfiguration::GetLogMaxCount()
  1658. {
  1659. TGuard Guard(FCriticalSection);
  1660. return FPermanentLogMaxCount;
  1661. }
  1662. //---------------------------------------------------------------------
  1663. UnicodeString __fastcall TConfiguration::GetDefaultLogFileName()
  1664. {
  1665. return L"%TEMP%\\!S.log";
  1666. }
  1667. //---------------------------------------------------------------------------
  1668. void __fastcall TConfiguration::SetConfirmOverwriting(bool value)
  1669. {
  1670. TGuard Guard(FCriticalSection);
  1671. SET_CONFIG_PROPERTY(ConfirmOverwriting);
  1672. }
  1673. //---------------------------------------------------------------------------
  1674. bool __fastcall TConfiguration::GetConfirmOverwriting()
  1675. {
  1676. TGuard Guard(FCriticalSection);
  1677. return FConfirmOverwriting;
  1678. }
  1679. //---------------------------------------------------------------------------
  1680. void __fastcall TConfiguration::SetConfirmResume(bool value)
  1681. {
  1682. TGuard Guard(FCriticalSection);
  1683. SET_CONFIG_PROPERTY(ConfirmResume);
  1684. }
  1685. //---------------------------------------------------------------------------
  1686. bool __fastcall TConfiguration::GetConfirmResume()
  1687. {
  1688. TGuard Guard(FCriticalSection);
  1689. return FConfirmResume;
  1690. }
  1691. //---------------------------------------------------------------------------
  1692. void __fastcall TConfiguration::SetAutoReadDirectoryAfterOp(bool value)
  1693. {
  1694. TGuard Guard(FCriticalSection);
  1695. SET_CONFIG_PROPERTY(AutoReadDirectoryAfterOp);
  1696. }
  1697. //---------------------------------------------------------------------------
  1698. bool __fastcall TConfiguration::GetAutoReadDirectoryAfterOp()
  1699. {
  1700. TGuard Guard(FCriticalSection);
  1701. return FAutoReadDirectoryAfterOp;
  1702. }
  1703. //---------------------------------------------------------------------------
  1704. UnicodeString __fastcall TConfiguration::GetTimeFormat()
  1705. {
  1706. return L"h:nn:ss";
  1707. }
  1708. //---------------------------------------------------------------------------
  1709. UnicodeString __fastcall TConfiguration::GetPartialExt() const
  1710. {
  1711. return PARTIAL_EXT;
  1712. }
  1713. //---------------------------------------------------------------------------
  1714. UnicodeString __fastcall TConfiguration::GetDefaultKeyFile()
  1715. {
  1716. return L"";
  1717. }
  1718. //---------------------------------------------------------------------------
  1719. bool __fastcall TConfiguration::GetRememberPassword()
  1720. {
  1721. return false;
  1722. }
  1723. //---------------------------------------------------------------------------
  1724. void __fastcall TConfiguration::SetSessionReopenAuto(int value)
  1725. {
  1726. SET_CONFIG_PROPERTY(SessionReopenAuto);
  1727. }
  1728. //---------------------------------------------------------------------------
  1729. void __fastcall TConfiguration::SetSessionReopenBackground(int value)
  1730. {
  1731. SET_CONFIG_PROPERTY(SessionReopenBackground);
  1732. }
  1733. //---------------------------------------------------------------------------
  1734. void __fastcall TConfiguration::SetSessionReopenTimeout(int value)
  1735. {
  1736. SET_CONFIG_PROPERTY(SessionReopenTimeout);
  1737. }
  1738. //---------------------------------------------------------------------------
  1739. void __fastcall TConfiguration::SetSessionReopenAutoStall(int value)
  1740. {
  1741. SET_CONFIG_PROPERTY(SessionReopenAutoStall);
  1742. }
  1743. //---------------------------------------------------------------------------
  1744. void __fastcall TConfiguration::SetTunnelLocalPortNumberLow(int value)
  1745. {
  1746. SET_CONFIG_PROPERTY(TunnelLocalPortNumberLow);
  1747. }
  1748. //---------------------------------------------------------------------------
  1749. void __fastcall TConfiguration::SetTunnelLocalPortNumberHigh(int value)
  1750. {
  1751. SET_CONFIG_PROPERTY(TunnelLocalPortNumberHigh);
  1752. }
  1753. //---------------------------------------------------------------------------
  1754. void __fastcall TConfiguration::SetCacheDirectoryChangesMaxSize(int value)
  1755. {
  1756. SET_CONFIG_PROPERTY(CacheDirectoryChangesMaxSize);
  1757. }
  1758. //---------------------------------------------------------------------------
  1759. void __fastcall TConfiguration::SetShowFtpWelcomeMessage(bool value)
  1760. {
  1761. SET_CONFIG_PROPERTY(ShowFtpWelcomeMessage);
  1762. }
  1763. //---------------------------------------------------------------------------
  1764. bool __fastcall TConfiguration::GetPersistent()
  1765. {
  1766. return (Storage != stNul) && !FDontSave;
  1767. }
  1768. //---------------------------------------------------------------------------
  1769. //---------------------------------------------------------------------------
  1770. void __fastcall TShortCuts::Add(TShortCut ShortCut)
  1771. {
  1772. FShortCuts.insert(ShortCut);
  1773. }
  1774. //---------------------------------------------------------------------------
  1775. bool __fastcall TShortCuts::Has(TShortCut ShortCut) const
  1776. {
  1777. return (FShortCuts.count(ShortCut) != 0);
  1778. }