Configuration.cpp 61 KB

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