Configuration.cpp 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913
  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. bool TConfiguration::RegistryPathExists(const UnicodeString & RegistryPath)
  706. {
  707. std::unique_ptr<TRegistryStorage> Registry(new TRegistryStorage(RegistryStorageKey));
  708. UnicodeString ParentKey = ExtractFileDir(RegistryPath);
  709. UnicodeString SubKey = ExtractFileName(RegistryPath);
  710. return
  711. Registry->OpenRootKey(false) &&
  712. (ParentKey.IsEmpty() ||
  713. Registry->OpenSubKeyPath(ParentKey, false)) &&
  714. Registry->KeyExists(SubKey);
  715. }
  716. //---------------------------------------------------------------------------
  717. void __fastcall TConfiguration::CleanupRegistry(const UnicodeString & RegistryPath)
  718. {
  719. std::unique_ptr<TRegistryStorage> Registry(new TRegistryStorage(RegistryStorageKey));
  720. UnicodeString ParentKey = ExtractFileDir(RegistryPath);
  721. if (ParentKey.IsEmpty() ||
  722. Registry->OpenSubKeyPath(ParentKey, false))
  723. {
  724. UnicodeString SubKey = ExtractFileName(RegistryPath);
  725. Registry->RecursiveDeleteSubKey(SubKey);
  726. }
  727. }
  728. //---------------------------------------------------------------------------
  729. TStrings * TConfiguration::GetCaches()
  730. {
  731. std::unique_ptr<TStrings> Result(new TStringList());
  732. Result->Add(SshHostKeysSubKey);
  733. Result->Add(FtpsCertificateStorageKey);
  734. Result->Add(HttpsCertificateStorageKey);
  735. Result->Add(DirectoryStatisticsCacheKey);
  736. Result->Add(TPath::Combine(ConfigurationSubKey, CDCacheKey));
  737. Result->Add(TPath::Combine(ConfigurationSubKey, BannersKey));
  738. Result->Add(TPath::Combine(ConfigurationSubKey, LastFingerprintsStorageKey));
  739. return Result.release();
  740. }
  741. //---------------------------------------------------------------------------
  742. bool __fastcall TConfiguration::HasAnyCache()
  743. {
  744. bool Result = false;
  745. std::unique_ptr<TStrings> Caches(GetCaches());
  746. for (int Index = 0; Index < Caches->Count; Index++)
  747. {
  748. if (RegistryPathExists(Caches->Strings[Index]))
  749. {
  750. Result = true;
  751. break;
  752. }
  753. }
  754. return Result;
  755. }
  756. //---------------------------------------------------------------------------
  757. void __fastcall TConfiguration::CleanupCaches()
  758. {
  759. try
  760. {
  761. std::unique_ptr<TStrings> Caches(GetCaches());
  762. for (int Index = 0; Index < Caches->Count; Index++)
  763. {
  764. CleanupRegistry(Caches->Strings[Index]);
  765. }
  766. }
  767. catch (Exception & E)
  768. {
  769. throw ExtException(&E, LoadStr(CLEANUP_HOSTKEYS_ERROR));
  770. }
  771. }
  772. //---------------------------------------------------------------------------
  773. void __fastcall TConfiguration::CleanupRandomSeedFile()
  774. {
  775. try
  776. {
  777. DontSaveRandomSeed();
  778. if (FileExists(ApiPath(RandomSeedFileName)))
  779. {
  780. DeleteFileChecked(RandomSeedFileName);
  781. }
  782. }
  783. catch (Exception &E)
  784. {
  785. throw ExtException(&E, LoadStr(CLEANUP_SEEDFILE_ERROR));
  786. }
  787. }
  788. //---------------------------------------------------------------------------
  789. void __fastcall TConfiguration::CleanupIniFile()
  790. {
  791. try
  792. {
  793. if (FileExists(ApiPath(IniFileStorageNameForReading)))
  794. {
  795. DeleteFileChecked(IniFileStorageNameForReading);
  796. }
  797. if (Storage == stIniFile)
  798. {
  799. FDontSave = true;
  800. }
  801. }
  802. catch (Exception &E)
  803. {
  804. throw ExtException(&E, LoadStr(CLEANUP_INIFILE_ERROR));
  805. }
  806. }
  807. //---------------------------------------------------------------------------
  808. void __fastcall TConfiguration::DontSave()
  809. {
  810. FDontSave = true;
  811. }
  812. //---------------------------------------------------------------------------
  813. RawByteString __fastcall TConfiguration::EncryptPassword(UnicodeString Password, UnicodeString Key)
  814. {
  815. if (Password.IsEmpty())
  816. {
  817. return RawByteString();
  818. }
  819. else
  820. {
  821. return ::EncryptPassword(Password, Key);
  822. }
  823. }
  824. //---------------------------------------------------------------------------
  825. UnicodeString __fastcall TConfiguration::DecryptPassword(RawByteString Password, UnicodeString Key)
  826. {
  827. if (Password.IsEmpty())
  828. {
  829. return UnicodeString();
  830. }
  831. else
  832. {
  833. return ::DecryptPassword(Password, Key);
  834. }
  835. }
  836. //---------------------------------------------------------------------------
  837. RawByteString __fastcall TConfiguration::StronglyRecryptPassword(RawByteString Password, UnicodeString /*Key*/)
  838. {
  839. return Password;
  840. }
  841. //---------------------------------------------------------------------------
  842. TVSFixedFileInfo *__fastcall TConfiguration::GetFixedApplicationInfo()
  843. {
  844. return GetFixedFileInfo(ApplicationInfo);
  845. }
  846. //---------------------------------------------------------------------------
  847. int __fastcall TConfiguration::GetCompoundVersion()
  848. {
  849. TVSFixedFileInfo * FileInfo = FixedApplicationInfo;
  850. return CalculateCompoundVersion(
  851. HIWORD(FileInfo->dwFileVersionMS), LOWORD(FileInfo->dwFileVersionMS),
  852. HIWORD(FileInfo->dwFileVersionLS));
  853. }
  854. //---------------------------------------------------------------------------
  855. UnicodeString __fastcall TConfiguration::ModuleFileName()
  856. {
  857. return ParamStr(0);
  858. }
  859. //---------------------------------------------------------------------------
  860. void * __fastcall TConfiguration::GetFileApplicationInfo(const UnicodeString FileName)
  861. {
  862. void * Result;
  863. if (FileName.IsEmpty())
  864. {
  865. if (!FApplicationInfo)
  866. {
  867. FApplicationInfo = CreateFileInfo(ModuleFileName());
  868. }
  869. Result = FApplicationInfo;
  870. }
  871. else
  872. {
  873. Result = CreateFileInfo(FileName);
  874. }
  875. return Result;
  876. }
  877. //---------------------------------------------------------------------------
  878. void * __fastcall TConfiguration::GetApplicationInfo()
  879. {
  880. return GetFileApplicationInfo("");
  881. }
  882. //---------------------------------------------------------------------------
  883. UnicodeString __fastcall TConfiguration::GetFileProductName(const UnicodeString FileName)
  884. {
  885. return GetFileFileInfoString(L"ProductName", FileName);
  886. }
  887. //---------------------------------------------------------------------------
  888. UnicodeString __fastcall TConfiguration::GetFileCompanyName(const UnicodeString FileName)
  889. {
  890. // particularly in IDE build, company name is empty
  891. return GetFileFileInfoString(L"CompanyName", FileName, true);
  892. }
  893. //---------------------------------------------------------------------------
  894. UnicodeString __fastcall TConfiguration::GetProductName()
  895. {
  896. return GetFileProductName(L"");
  897. }
  898. //---------------------------------------------------------------------------
  899. UnicodeString __fastcall TConfiguration::GetCompanyName()
  900. {
  901. return GetFileCompanyName(L"");
  902. }
  903. //---------------------------------------------------------------------------
  904. UnicodeString __fastcall TConfiguration::GetFileProductVersion(const UnicodeString FileName)
  905. {
  906. return TrimVersion(GetFileFileInfoString(L"ProductVersion", FileName));
  907. }
  908. //---------------------------------------------------------------------------
  909. UnicodeString __fastcall TConfiguration::GetFileDescription(const UnicodeString & FileName)
  910. {
  911. return GetFileFileInfoString(L"FileDescription", FileName);
  912. }
  913. //---------------------------------------------------------------------------
  914. UnicodeString __fastcall TConfiguration::GetProductVersion()
  915. {
  916. return GetFileProductVersion(L"");
  917. }
  918. //---------------------------------------------------------------------------
  919. UnicodeString __fastcall TConfiguration::GetReleaseType()
  920. {
  921. return GetFileInfoString(L"ReleaseType");
  922. }
  923. //---------------------------------------------------------------------------
  924. bool __fastcall TConfiguration::GetIsUnofficial()
  925. {
  926. #ifdef BUILD_OFFICIAL
  927. return false;
  928. #else
  929. return true;
  930. #endif
  931. }
  932. //---------------------------------------------------------------------------
  933. UnicodeString __fastcall TConfiguration::GetVersionStr()
  934. {
  935. TGuard Guard(FCriticalSection);
  936. try
  937. {
  938. UnicodeString BuildStr;
  939. if (!IsUnofficial)
  940. {
  941. BuildStr = LoadStr(VERSION_BUILD);
  942. }
  943. else
  944. {
  945. #ifdef _DEBUG
  946. BuildStr = LoadStr(VERSION_DEBUG_BUILD);
  947. #else
  948. BuildStr = LoadStr(VERSION_DEV_BUILD);
  949. #endif
  950. }
  951. int Build = LOWORD(FixedApplicationInfo->dwFileVersionLS);
  952. if (Build > 0)
  953. {
  954. BuildStr += L" " + IntToStr(Build);
  955. }
  956. #ifndef BUILD_OFFICIAL
  957. UnicodeString BuildDate = __DATE__;
  958. UnicodeString MonthStr = CutToChar(BuildDate, L' ', true);
  959. int Month = ParseShortEngMonthName(MonthStr);
  960. int Day = StrToInt(CutToChar(BuildDate, L' ', true));
  961. int Year = StrToInt(Trim(BuildDate));
  962. UnicodeString DateStr = FORMAT(L"%d-%2.2d-%2.2d", (Year, Month, Day));
  963. AddToList(BuildStr, DateStr, L" ");
  964. #endif
  965. UnicodeString FullVersion = Version;
  966. UnicodeString AReleaseType = GetReleaseType();
  967. if (DebugAlwaysTrue(!AReleaseType.IsEmpty()) &&
  968. !SameText(AReleaseType, L"stable") &&
  969. !SameText(AReleaseType, L"development"))
  970. {
  971. FullVersion += L" " + AReleaseType;
  972. }
  973. UnicodeString Result = FMTLOAD(VERSION2, (FullVersion, BuildStr));
  974. #ifndef BUILD_OFFICIAL
  975. Result += L" " + LoadStr(VERSION_DONT_DISTRIBUTE);
  976. #endif
  977. return Result;
  978. }
  979. catch (Exception &E)
  980. {
  981. throw ExtException(&E, L"Can't get application version");
  982. }
  983. }
  984. //---------------------------------------------------------------------------
  985. UnicodeString __fastcall TConfiguration::GetFileVersion(const UnicodeString & FileName)
  986. {
  987. UnicodeString Result;
  988. void * FileInfo = CreateFileInfo(FileName);
  989. try
  990. {
  991. Result = GetFileVersion(GetFixedFileInfo(FileInfo));
  992. }
  993. __finally
  994. {
  995. FreeFileInfo(FileInfo);
  996. }
  997. return Result;
  998. }
  999. //---------------------------------------------------------------------------
  1000. UnicodeString __fastcall TConfiguration::GetFileVersion(TVSFixedFileInfo * Info)
  1001. {
  1002. TGuard Guard(FCriticalSection);
  1003. try
  1004. {
  1005. UnicodeString Result =
  1006. FormatVersion(
  1007. HIWORD(Info->dwFileVersionMS),
  1008. LOWORD(Info->dwFileVersionMS),
  1009. HIWORD(Info->dwFileVersionLS));
  1010. return Result;
  1011. }
  1012. catch (Exception &E)
  1013. {
  1014. throw ExtException(&E, L"Can't get file version");
  1015. }
  1016. }
  1017. //---------------------------------------------------------------------------
  1018. UnicodeString __fastcall TConfiguration::GetVersion()
  1019. {
  1020. return GetFileVersion(FixedApplicationInfo);
  1021. }
  1022. //---------------------------------------------------------------------------
  1023. UnicodeString __fastcall TConfiguration::GetFileFileInfoString(const UnicodeString Key,
  1024. const UnicodeString FileName, bool AllowEmpty)
  1025. {
  1026. TGuard Guard(FCriticalSection);
  1027. UnicodeString Result;
  1028. void * Info = GetFileApplicationInfo(FileName);
  1029. try
  1030. {
  1031. if ((Info != NULL) && (GetTranslationCount(Info) > 0))
  1032. {
  1033. TTranslation Translation;
  1034. Translation = GetTranslation(Info, 0);
  1035. Result = ::GetFileInfoString(Info, Translation, Key, AllowEmpty);
  1036. }
  1037. else
  1038. {
  1039. DebugAssert(!FileName.IsEmpty());
  1040. }
  1041. }
  1042. __finally
  1043. {
  1044. if (!FileName.IsEmpty())
  1045. {
  1046. FreeFileInfo(Info);
  1047. }
  1048. }
  1049. return Result;
  1050. }
  1051. //---------------------------------------------------------------------------
  1052. UnicodeString __fastcall TConfiguration::GetFileInfoString(const UnicodeString Key)
  1053. {
  1054. return GetFileFileInfoString(Key, L"");
  1055. }
  1056. //---------------------------------------------------------------------------
  1057. UnicodeString __fastcall TConfiguration::GetFileMimeType(const UnicodeString & FileName)
  1058. {
  1059. UnicodeString Result;
  1060. bool Found = false;
  1061. if (!MimeTypes.IsEmpty())
  1062. {
  1063. UnicodeString FileNameOnly = ExtractFileName(FileName);
  1064. UnicodeString AMimeTypes = MimeTypes;
  1065. while (!Found && !AMimeTypes.IsEmpty())
  1066. {
  1067. UnicodeString Token = CutToChar(AMimeTypes, L',', true);
  1068. UnicodeString MaskStr = CutToChar(Token, L'=', true);
  1069. TFileMasks Mask(MaskStr);
  1070. if (Mask.Matches(FileNameOnly))
  1071. {
  1072. Result = Token.Trim();
  1073. Found = true;
  1074. }
  1075. }
  1076. }
  1077. if (!Found) // allow an override to "no" Content-Type
  1078. {
  1079. Result = ::GetFileMimeType(FileName);
  1080. }
  1081. return Result;
  1082. }
  1083. //---------------------------------------------------------------------------
  1084. UnicodeString __fastcall TConfiguration::GetRegistryStorageKey()
  1085. {
  1086. return GetRegistryKey();
  1087. }
  1088. //---------------------------------------------------------------------------
  1089. void __fastcall TConfiguration::SetNulStorage()
  1090. {
  1091. FStorage = stNul;
  1092. }
  1093. //---------------------------------------------------------------------------
  1094. void __fastcall TConfiguration::SetDefaultStorage()
  1095. {
  1096. FStorage = stDetect;
  1097. }
  1098. //---------------------------------------------------------------------------
  1099. void __fastcall TConfiguration::SetIniFileStorageName(UnicodeString value)
  1100. {
  1101. FIniFileStorageName = value;
  1102. FStorage = stIniFile;
  1103. }
  1104. //---------------------------------------------------------------------------
  1105. UnicodeString __fastcall TConfiguration::GetDefaultIniFileExportPath()
  1106. {
  1107. UnicodeString PersonalDirectory = GetPersonalFolder();
  1108. UnicodeString FileName = IncludeTrailingBackslash(PersonalDirectory) +
  1109. ExtractFileName(ExpandEnvironmentVariables(IniFileStorageName));
  1110. return FileName;
  1111. }
  1112. //---------------------------------------------------------------------------
  1113. UnicodeString __fastcall TConfiguration::GetIniFileStorageNameForReading()
  1114. {
  1115. return GetIniFileStorageName(true);
  1116. }
  1117. //---------------------------------------------------------------------------
  1118. UnicodeString __fastcall TConfiguration::GetIniFileStorageNameForReadingWriting()
  1119. {
  1120. return GetIniFileStorageName(false);
  1121. }
  1122. //---------------------------------------------------------------------------
  1123. UnicodeString __fastcall TConfiguration::GetAutomaticIniFileStorageName(bool ReadingOnly)
  1124. {
  1125. UnicodeString ProgramPath = ParamStr(0);
  1126. UnicodeString ProgramIniPath = ChangeFileExt(ProgramPath, L".ini");
  1127. UnicodeString IniPath;
  1128. if (FileExists(ApiPath(ProgramIniPath)))
  1129. {
  1130. IniPath = ProgramIniPath;
  1131. }
  1132. else
  1133. {
  1134. UnicodeString AppDataIniPath =
  1135. IncludeTrailingBackslash(GetShellFolderPath(CSIDL_APPDATA)) +
  1136. ExtractFileName(ProgramIniPath);
  1137. if (FileExists(ApiPath(AppDataIniPath)))
  1138. {
  1139. IniPath = AppDataIniPath;
  1140. }
  1141. else
  1142. {
  1143. // avoid expensive test if we are interested in existing files only
  1144. if (!ReadingOnly && (FProgramIniPathWrittable < 0))
  1145. {
  1146. UnicodeString ProgramDir = ExtractFilePath(ProgramPath);
  1147. FProgramIniPathWrittable = IsDirectoryWriteable(ProgramDir) ? 1 : 0;
  1148. }
  1149. // does not really matter what we return when < 0
  1150. IniPath = (FProgramIniPathWrittable == 0) ? AppDataIniPath : ProgramIniPath;
  1151. }
  1152. }
  1153. // BACKWARD COMPATIBILITY with 4.x
  1154. if (FVirtualIniFileStorageName.IsEmpty() &&
  1155. TPath::IsDriveRooted(IniPath))
  1156. {
  1157. UnicodeString LocalAppDataPath = GetShellFolderPath(CSIDL_LOCAL_APPDATA);
  1158. // virtual store for non-system drives have a different virtual store,
  1159. // do not bother about them
  1160. if (TPath::IsDriveRooted(LocalAppDataPath) &&
  1161. SameText(ExtractFileDrive(IniPath), ExtractFileDrive(LocalAppDataPath)))
  1162. {
  1163. FVirtualIniFileStorageName =
  1164. IncludeTrailingBackslash(LocalAppDataPath) +
  1165. L"VirtualStore\\" +
  1166. IniPath.SubString(4, IniPath.Length() - 3);
  1167. }
  1168. }
  1169. if (!FVirtualIniFileStorageName.IsEmpty() &&
  1170. FileExists(ApiPath(FVirtualIniFileStorageName)))
  1171. {
  1172. return FVirtualIniFileStorageName;
  1173. }
  1174. else
  1175. {
  1176. return IniPath;
  1177. }
  1178. }
  1179. //---------------------------------------------------------------------------
  1180. UnicodeString __fastcall TConfiguration::GetIniFileStorageName(bool ReadingOnly)
  1181. {
  1182. UnicodeString Result;
  1183. // This does not work correctly with MoveStorage
  1184. if (!FIniFileStorageName.IsEmpty())
  1185. {
  1186. Result = FIniFileStorageName;
  1187. }
  1188. else if (!FCustomIniFileStorageName.IsEmpty())
  1189. {
  1190. Result = FCustomIniFileStorageName;
  1191. }
  1192. else
  1193. {
  1194. Result = GetAutomaticIniFileStorageName(ReadingOnly);
  1195. }
  1196. return Result;
  1197. }
  1198. //---------------------------------------------------------------------------
  1199. void __fastcall TConfiguration::SetOptionsStorage(TStrings * value)
  1200. {
  1201. TGuard Guard(FCriticalSection);
  1202. if (FOptionsStorage.get() == NULL)
  1203. {
  1204. FOptionsStorage.reset(new TStringList());
  1205. }
  1206. FOptionsStorage->AddStrings(value);
  1207. }
  1208. //---------------------------------------------------------------------------
  1209. TStrings * __fastcall TConfiguration::GetOptionsStorage()
  1210. {
  1211. return FOptionsStorage.get();
  1212. }
  1213. //---------------------------------------------------------------------------
  1214. UnicodeString __fastcall TConfiguration::GetPuttySessionsSubKey()
  1215. {
  1216. return StoredSessionsSubKey;
  1217. }
  1218. //---------------------------------------------------------------------------
  1219. UnicodeString __fastcall TConfiguration::GetPuttySessionsKey()
  1220. {
  1221. return PuttyRegistryStorageKey + L"\\" + PuttySessionsSubKey;
  1222. }
  1223. //---------------------------------------------------------------------------
  1224. UnicodeString __fastcall TConfiguration::GetStoredSessionsSubKey()
  1225. {
  1226. return L"Sessions";
  1227. }
  1228. //---------------------------------------------------------------------------
  1229. UnicodeString __fastcall TConfiguration::GetSshHostKeysSubKey()
  1230. {
  1231. return L"SshHostKeys";
  1232. }
  1233. //---------------------------------------------------------------------------
  1234. UnicodeString __fastcall TConfiguration::GetConfigurationSubKey()
  1235. {
  1236. return L"Configuration";
  1237. }
  1238. //---------------------------------------------------------------------------
  1239. UnicodeString __fastcall TConfiguration::GetRootKeyStr()
  1240. {
  1241. return RootKeyToStr(HKEY_CURRENT_USER);
  1242. }
  1243. //---------------------------------------------------------------------------
  1244. void __fastcall TConfiguration::MoveStorage(TStorage AStorage, const UnicodeString & ACustomIniFileStorageName)
  1245. {
  1246. if ((FStorage != AStorage) ||
  1247. !IsPathToSameFile(FCustomIniFileStorageName, ACustomIniFileStorageName))
  1248. {
  1249. TStorage StorageBak = FStorage;
  1250. UnicodeString CustomIniFileStorageNameBak = FCustomIniFileStorageName;
  1251. try
  1252. {
  1253. THierarchicalStorage * SourceStorage = NULL;
  1254. THierarchicalStorage * TargetStorage = NULL;
  1255. try
  1256. {
  1257. SourceStorage = CreateConfigStorage();
  1258. SourceStorage->AccessMode = smRead;
  1259. FStorage = AStorage;
  1260. FCustomIniFileStorageName = ACustomIniFileStorageName;
  1261. TargetStorage = CreateConfigStorage();
  1262. TargetStorage->AccessMode = smReadWrite;
  1263. TargetStorage->Explicit = true;
  1264. // copy before save as it removes the ini file,
  1265. // when switching from ini to registry
  1266. CopyData(SourceStorage, TargetStorage);
  1267. }
  1268. __finally
  1269. {
  1270. delete SourceStorage;
  1271. delete TargetStorage;
  1272. }
  1273. // save all and explicit,
  1274. // this also removes an INI file, when switching to registry storage
  1275. DoSave(true, true);
  1276. }
  1277. catch (...)
  1278. {
  1279. // If this fails, do not pretend that storage was switched.
  1280. // For instance:
  1281. // - When writing to an INI file fails (unlikely, as we fallback to user profile)
  1282. // - When removing INI file fails, when switching to registry
  1283. // (possible, when the INI file is in Program Files folder)
  1284. FStorage = StorageBak;
  1285. FCustomIniFileStorageName = CustomIniFileStorageNameBak;
  1286. throw;
  1287. }
  1288. }
  1289. }
  1290. //---------------------------------------------------------------------------
  1291. void __fastcall TConfiguration::ScheduleCustomIniFileStorageUse(const UnicodeString & ACustomIniFileStorageName)
  1292. {
  1293. FStorage = stIniFile;
  1294. FCustomIniFileStorageName = ACustomIniFileStorageName;
  1295. SaveCustomIniFileStorageName();
  1296. }
  1297. //---------------------------------------------------------------------------
  1298. void __fastcall TConfiguration::Saved()
  1299. {
  1300. // nothing
  1301. }
  1302. //---------------------------------------------------------------------------
  1303. TStorage __fastcall TConfiguration::GetStorage()
  1304. {
  1305. TGuard Guard(FCriticalSection);
  1306. if (FStorage == stDetect)
  1307. {
  1308. if (FileExists(ApiPath(IniFileStorageNameForReading)))
  1309. {
  1310. FStorage = stIniFile;
  1311. }
  1312. else
  1313. {
  1314. FStorage = stRegistry;
  1315. }
  1316. }
  1317. return FStorage;
  1318. }
  1319. //---------------------------------------------------------------------
  1320. TStoredSessionList * __fastcall TConfiguration::SelectFilezillaSessionsForImport(
  1321. TStoredSessionList * Sessions, UnicodeString & Error)
  1322. {
  1323. std::unique_ptr<TStoredSessionList> ImportSessionList(new TStoredSessionList(true));
  1324. ImportSessionList->DefaultSettings = Sessions->DefaultSettings;
  1325. UnicodeString AppDataPath = GetShellFolderPath(CSIDL_APPDATA);
  1326. UnicodeString FilezillaSiteManagerFile =
  1327. IncludeTrailingBackslash(AppDataPath) + L"FileZilla\\sitemanager.xml";
  1328. UnicodeString FilezillaConfigurationFile =
  1329. IncludeTrailingBackslash(AppDataPath) + L"FileZilla\\filezilla.xml";
  1330. if (FileExists(ApiPath(FilezillaSiteManagerFile)))
  1331. {
  1332. ImportSessionList->ImportFromFilezilla(FilezillaSiteManagerFile, FilezillaConfigurationFile);
  1333. if (ImportSessionList->Count > 0)
  1334. {
  1335. ImportSessionList->SelectSessionsToImport(Sessions, true);
  1336. }
  1337. else
  1338. {
  1339. Error = FMTLOAD(FILEZILLA_NO_SITES, (FilezillaSiteManagerFile));
  1340. }
  1341. }
  1342. else
  1343. {
  1344. Error = FMTLOAD(FILEZILLA_SITE_MANAGER_NOT_FOUND, (FilezillaSiteManagerFile));
  1345. }
  1346. return ImportSessionList.release();
  1347. }
  1348. //---------------------------------------------------------------------
  1349. bool __fastcall TConfiguration::AnyFilezillaSessionForImport(TStoredSessionList * Sessions)
  1350. {
  1351. try
  1352. {
  1353. UnicodeString Error;
  1354. std::unique_ptr<TStoredSessionList> Sesssions(SelectFilezillaSessionsForImport(Sessions, Error));
  1355. return (Sesssions->Count > 0);
  1356. }
  1357. catch (...)
  1358. {
  1359. return false;
  1360. }
  1361. }
  1362. //---------------------------------------------------------------------
  1363. TStoredSessionList * __fastcall TConfiguration::SelectKnownHostsSessionsForImport(
  1364. TStoredSessionList * Sessions, UnicodeString & Error)
  1365. {
  1366. std::unique_ptr<TStoredSessionList> ImportSessionList(new TStoredSessionList(true));
  1367. ImportSessionList->DefaultSettings = Sessions->DefaultSettings;
  1368. UnicodeString ProfilePath = GetShellFolderPath(CSIDL_PROFILE);
  1369. UnicodeString KnownHostsFile = IncludeTrailingBackslash(ProfilePath) + L".ssh\\known_hosts";
  1370. try
  1371. {
  1372. if (FileExists(ApiPath(KnownHostsFile)))
  1373. {
  1374. std::unique_ptr<TStrings> Lines(new TStringList());
  1375. LoadScriptFromFile(KnownHostsFile, Lines.get(), true);
  1376. ImportSessionList->ImportFromKnownHosts(Lines.get());
  1377. }
  1378. else
  1379. {
  1380. throw Exception(LoadStr(KNOWN_HOSTS_NOT_FOUND));
  1381. }
  1382. }
  1383. catch (Exception & E)
  1384. {
  1385. Error = FORMAT(L"%s\n(%s)", (E.Message, KnownHostsFile));
  1386. }
  1387. return ImportSessionList.release();
  1388. }
  1389. //---------------------------------------------------------------------
  1390. TStoredSessionList * __fastcall TConfiguration::SelectKnownHostsSessionsForImport(
  1391. TStrings * Lines, TStoredSessionList * Sessions, UnicodeString & Error)
  1392. {
  1393. std::unique_ptr<TStoredSessionList> ImportSessionList(new TStoredSessionList(true));
  1394. ImportSessionList->DefaultSettings = Sessions->DefaultSettings;
  1395. try
  1396. {
  1397. ImportSessionList->ImportFromKnownHosts(Lines);
  1398. }
  1399. catch (Exception & E)
  1400. {
  1401. Error = E.Message;
  1402. }
  1403. return ImportSessionList.release();
  1404. }
  1405. //---------------------------------------------------------------------------
  1406. void __fastcall TConfiguration::SetRandomSeedFile(UnicodeString value)
  1407. {
  1408. if (RandomSeedFile != value)
  1409. {
  1410. UnicodeString PrevRandomSeedFileName = RandomSeedFileName;
  1411. FRandomSeedFile = value;
  1412. // never allow empty seed file to avoid Putty trying to reinitialize the path
  1413. if (RandomSeedFileName.IsEmpty())
  1414. {
  1415. FRandomSeedFile = FDefaultRandomSeedFile;
  1416. }
  1417. if (!PrevRandomSeedFileName.IsEmpty() &&
  1418. (PrevRandomSeedFileName != RandomSeedFileName) &&
  1419. FileExists(ApiPath(PrevRandomSeedFileName)))
  1420. {
  1421. // ignore any error
  1422. DeleteFile(ApiPath(PrevRandomSeedFileName));
  1423. }
  1424. }
  1425. }
  1426. //---------------------------------------------------------------------------
  1427. UnicodeString __fastcall TConfiguration::GetDirectoryStatisticsCacheKey(
  1428. const UnicodeString & SessionKey, const UnicodeString & Path, const TCopyParamType & CopyParam)
  1429. {
  1430. std::unique_ptr<TStringList> RawOptions(new TStringList());
  1431. RawOptions->Add(SessionKey);
  1432. RawOptions->Add(UnixExcludeTrailingBackslash(Path));
  1433. TCopyParamType Defaults;
  1434. TCopyParamType FilterCopyParam;
  1435. FilterCopyParam.IncludeFileMask = CopyParam.IncludeFileMask;
  1436. FilterCopyParam.ExcludeHiddenFiles = CopyParam.ExcludeHiddenFiles;
  1437. FilterCopyParam.ExcludeEmptyDirectories = CopyParam.ExcludeEmptyDirectories;
  1438. std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(RawOptions.get(), true));
  1439. FilterCopyParam.Save(OptionsStorage.get(), &Defaults);
  1440. UTF8String RawOptionsBuf(RawOptions->CommaText.LowerCase());
  1441. UnicodeString Result = Sha256(RawOptionsBuf.c_str(), RawOptionsBuf.Length());
  1442. return Result;
  1443. }
  1444. //---------------------------------------------------------------------------
  1445. THierarchicalStorage * TConfiguration::OpenDirectoryStatisticsCache(bool CanCreate)
  1446. {
  1447. std::unique_ptr<THierarchicalStorage> Storage(Configuration->CreateConfigStorage());
  1448. Storage->AccessMode = CanCreate ? smReadWrite : smRead;
  1449. if (!Storage->OpenSubKey(DirectoryStatisticsCacheKey, CanCreate))
  1450. {
  1451. Storage.reset(NULL);
  1452. }
  1453. return Storage.release();
  1454. }
  1455. //---------------------------------------------------------------------------
  1456. TStrings * __fastcall TConfiguration::LoadDirectoryStatisticsCache(
  1457. const UnicodeString & SessionKey, const UnicodeString & Path, const TCopyParamType & CopyParam)
  1458. {
  1459. std::unique_ptr<THierarchicalStorage> Storage(OpenDirectoryStatisticsCache(false));
  1460. std::unique_ptr<TStringList> Result(new TStringList());
  1461. if (Storage.get() != NULL)
  1462. {
  1463. UnicodeString Key = GetDirectoryStatisticsCacheKey(SessionKey, Path, CopyParam);
  1464. UnicodeString Buf = Storage->ReadString(Key, UnicodeString());
  1465. Result->CommaText = Buf;
  1466. }
  1467. return Result.release();
  1468. }
  1469. //---------------------------------------------------------------------------
  1470. void __fastcall TConfiguration::SaveDirectoryStatisticsCache(
  1471. const UnicodeString & SessionKey, const UnicodeString & Path, const TCopyParamType & CopyParam, TStrings * DataList)
  1472. {
  1473. std::unique_ptr<THierarchicalStorage> Storage(OpenDirectoryStatisticsCache(true));
  1474. if (Storage.get() != NULL)
  1475. {
  1476. UnicodeString Key = GetDirectoryStatisticsCacheKey(SessionKey, Path, CopyParam);
  1477. UnicodeString Buf = DataList->CommaText;
  1478. Storage->WriteString(Key, Buf);
  1479. }
  1480. }
  1481. //---------------------------------------------------------------------
  1482. UnicodeString __fastcall TConfiguration::GetRandomSeedFileName()
  1483. {
  1484. // StripPathQuotes should not be needed as we do not feed quotes anymore
  1485. return StripPathQuotes(ExpandEnvironmentVariables(FRandomSeedFile)).Trim();
  1486. }
  1487. //---------------------------------------------------------------------
  1488. void __fastcall TConfiguration::SetExternalIpAddress(UnicodeString value)
  1489. {
  1490. SET_CONFIG_PROPERTY(ExternalIpAddress);
  1491. }
  1492. //---------------------------------------------------------------------
  1493. void __fastcall TConfiguration::SetMimeTypes(UnicodeString value)
  1494. {
  1495. SET_CONFIG_PROPERTY(MimeTypes);
  1496. }
  1497. //---------------------------------------------------------------------
  1498. void __fastcall TConfiguration::SetTryFtpWhenSshFails(bool value)
  1499. {
  1500. SET_CONFIG_PROPERTY(TryFtpWhenSshFails);
  1501. }
  1502. //---------------------------------------------------------------------
  1503. void __fastcall TConfiguration::SetParallelDurationThreshold(int value)
  1504. {
  1505. SET_CONFIG_PROPERTY(ParallelDurationThreshold);
  1506. }
  1507. //---------------------------------------------------------------------
  1508. void __fastcall TConfiguration::SetPuttyRegistryStorageKey(UnicodeString value)
  1509. {
  1510. SET_CONFIG_PROPERTY(PuttyRegistryStorageKey);
  1511. }
  1512. //---------------------------------------------------------------------------
  1513. TEOLType __fastcall TConfiguration::GetLocalEOLType()
  1514. {
  1515. return eolCRLF;
  1516. }
  1517. //---------------------------------------------------------------------
  1518. bool __fastcall TConfiguration::GetCollectUsage()
  1519. {
  1520. return FUsage->Collect;
  1521. }
  1522. //---------------------------------------------------------------------
  1523. void __fastcall TConfiguration::SetCollectUsage(bool value)
  1524. {
  1525. FUsage->Collect = value;
  1526. }
  1527. //---------------------------------------------------------------------
  1528. void __fastcall TConfiguration::TemporaryLogging(const UnicodeString ALogFileName)
  1529. {
  1530. if (SameText(ExtractFileExt(ALogFileName), L".xml"))
  1531. {
  1532. TemporaryActionsLogging(ALogFileName);
  1533. }
  1534. else
  1535. {
  1536. FLogging = true;
  1537. FLogFileName = ALogFileName;
  1538. UpdateActualLogProtocol();
  1539. }
  1540. }
  1541. //---------------------------------------------------------------------
  1542. void __fastcall TConfiguration::TemporaryActionsLogging(const UnicodeString ALogFileName)
  1543. {
  1544. FLogActions = true;
  1545. FActionsLogFileName = ALogFileName;
  1546. }
  1547. //---------------------------------------------------------------------
  1548. void __fastcall TConfiguration::TemporaryLogProtocol(int ALogProtocol)
  1549. {
  1550. FLogProtocol = ALogProtocol;
  1551. UpdateActualLogProtocol();
  1552. }
  1553. //---------------------------------------------------------------------
  1554. void __fastcall TConfiguration::TemporaryLogSensitive(bool ALogSensitive)
  1555. {
  1556. FLogSensitive = ALogSensitive;
  1557. }
  1558. //---------------------------------------------------------------------
  1559. void __fastcall TConfiguration::TemporaryLogMaxSize(__int64 ALogMaxSize)
  1560. {
  1561. FLogMaxSize = ALogMaxSize;
  1562. }
  1563. //---------------------------------------------------------------------
  1564. void __fastcall TConfiguration::TemporaryLogMaxCount(int ALogMaxCount)
  1565. {
  1566. FLogMaxCount = ALogMaxCount;
  1567. }
  1568. //---------------------------------------------------------------------
  1569. void __fastcall TConfiguration::SetLogging(bool value)
  1570. {
  1571. TGuard Guard(FCriticalSection);
  1572. if (Logging != value)
  1573. {
  1574. FPermanentLogging = value;
  1575. FLogging = value;
  1576. UpdateActualLogProtocol();
  1577. Changed();
  1578. }
  1579. }
  1580. //---------------------------------------------------------------------
  1581. bool __fastcall TConfiguration::GetLogging()
  1582. {
  1583. TGuard Guard(FCriticalSection);
  1584. return FPermanentLogging;
  1585. }
  1586. //---------------------------------------------------------------------
  1587. void __fastcall TConfiguration::SetLogFileName(UnicodeString value)
  1588. {
  1589. TGuard Guard(FCriticalSection);
  1590. if (LogFileName != value)
  1591. {
  1592. FPermanentLogFileName = value;
  1593. FLogFileName = value;
  1594. Changed();
  1595. }
  1596. }
  1597. //---------------------------------------------------------------------
  1598. UnicodeString __fastcall TConfiguration::GetLogFileName()
  1599. {
  1600. TGuard Guard(FCriticalSection);
  1601. return FPermanentLogFileName;
  1602. }
  1603. //---------------------------------------------------------------------
  1604. void __fastcall TConfiguration::SetActionsLogFileName(UnicodeString value)
  1605. {
  1606. TGuard Guard(FCriticalSection);
  1607. if (ActionsLogFileName != value)
  1608. {
  1609. FPermanentActionsLogFileName = value;
  1610. FActionsLogFileName = value;
  1611. Changed();
  1612. }
  1613. }
  1614. //---------------------------------------------------------------------
  1615. UnicodeString __fastcall TConfiguration::GetPermanentActionsLogFileName()
  1616. {
  1617. TGuard Guard(FCriticalSection);
  1618. return FPermanentActionsLogFileName;
  1619. }
  1620. //---------------------------------------------------------------------
  1621. UnicodeString __fastcall TConfiguration::GetActionsLogFileName()
  1622. {
  1623. TGuard Guard(FCriticalSection);
  1624. return FActionsLogFileName;
  1625. }
  1626. //---------------------------------------------------------------------
  1627. bool __fastcall TConfiguration::GetLogToFile()
  1628. {
  1629. // guarded within GetLogFileName
  1630. return !LogFileName.IsEmpty();
  1631. }
  1632. //---------------------------------------------------------------------
  1633. void __fastcall TConfiguration::UpdateActualLogProtocol()
  1634. {
  1635. FActualLogProtocol = FLogging ? FLogProtocol : 0;
  1636. }
  1637. //---------------------------------------------------------------------
  1638. void __fastcall TConfiguration::SetLogProtocol(int value)
  1639. {
  1640. TGuard Guard(FCriticalSection);
  1641. if (LogProtocol != value)
  1642. {
  1643. FPermanentLogProtocol = value;
  1644. FLogProtocol = value;
  1645. Changed();
  1646. UpdateActualLogProtocol();
  1647. }
  1648. }
  1649. //---------------------------------------------------------------------
  1650. void __fastcall TConfiguration::SetLogActions(bool value)
  1651. {
  1652. TGuard Guard(FCriticalSection);
  1653. if (LogActions != value)
  1654. {
  1655. FPermanentLogActions = value;
  1656. FLogActions = value;
  1657. Changed();
  1658. }
  1659. }
  1660. //---------------------------------------------------------------------
  1661. bool __fastcall TConfiguration::GetLogActions()
  1662. {
  1663. TGuard Guard(FCriticalSection);
  1664. return FPermanentLogActions;
  1665. }
  1666. //---------------------------------------------------------------------
  1667. void __fastcall TConfiguration::SetLogFileAppend(bool value)
  1668. {
  1669. SET_CONFIG_PROPERTY(LogFileAppend);
  1670. }
  1671. //---------------------------------------------------------------------
  1672. void __fastcall TConfiguration::SetLogSensitive(bool value)
  1673. {
  1674. if (LogSensitive != value)
  1675. {
  1676. FPermanentLogSensitive = value;
  1677. FLogSensitive = value;
  1678. Changed();
  1679. }
  1680. }
  1681. //---------------------------------------------------------------------
  1682. void __fastcall TConfiguration::SetLogMaxSize(__int64 value)
  1683. {
  1684. TGuard Guard(FCriticalSection);
  1685. if (LogMaxSize != value)
  1686. {
  1687. FPermanentLogMaxSize = value;
  1688. FLogMaxSize = value;
  1689. Changed();
  1690. }
  1691. }
  1692. //---------------------------------------------------------------------
  1693. __int64 __fastcall TConfiguration::GetLogMaxSize()
  1694. {
  1695. TGuard Guard(FCriticalSection);
  1696. return FPermanentLogMaxSize;
  1697. }
  1698. //---------------------------------------------------------------------
  1699. void __fastcall TConfiguration::SetLogMaxCount(int value)
  1700. {
  1701. if (LogMaxCount != value)
  1702. {
  1703. FPermanentLogMaxCount = value;
  1704. FLogMaxCount = value;
  1705. Changed();
  1706. }
  1707. }
  1708. //---------------------------------------------------------------------
  1709. int __fastcall TConfiguration::GetLogMaxCount()
  1710. {
  1711. TGuard Guard(FCriticalSection);
  1712. return FPermanentLogMaxCount;
  1713. }
  1714. //---------------------------------------------------------------------
  1715. UnicodeString __fastcall TConfiguration::GetDefaultLogFileName()
  1716. {
  1717. return L"%TEMP%\\!S.log";
  1718. }
  1719. //---------------------------------------------------------------------------
  1720. void __fastcall TConfiguration::SetConfirmOverwriting(bool value)
  1721. {
  1722. TGuard Guard(FCriticalSection);
  1723. SET_CONFIG_PROPERTY(ConfirmOverwriting);
  1724. }
  1725. //---------------------------------------------------------------------------
  1726. bool __fastcall TConfiguration::GetConfirmOverwriting()
  1727. {
  1728. TGuard Guard(FCriticalSection);
  1729. return FConfirmOverwriting;
  1730. }
  1731. //---------------------------------------------------------------------------
  1732. void __fastcall TConfiguration::SetConfirmResume(bool value)
  1733. {
  1734. TGuard Guard(FCriticalSection);
  1735. SET_CONFIG_PROPERTY(ConfirmResume);
  1736. }
  1737. //---------------------------------------------------------------------------
  1738. bool __fastcall TConfiguration::GetConfirmResume()
  1739. {
  1740. TGuard Guard(FCriticalSection);
  1741. return FConfirmResume;
  1742. }
  1743. //---------------------------------------------------------------------------
  1744. void __fastcall TConfiguration::SetAutoReadDirectoryAfterOp(bool value)
  1745. {
  1746. TGuard Guard(FCriticalSection);
  1747. SET_CONFIG_PROPERTY(AutoReadDirectoryAfterOp);
  1748. }
  1749. //---------------------------------------------------------------------------
  1750. bool __fastcall TConfiguration::GetAutoReadDirectoryAfterOp()
  1751. {
  1752. TGuard Guard(FCriticalSection);
  1753. return FAutoReadDirectoryAfterOp;
  1754. }
  1755. //---------------------------------------------------------------------------
  1756. UnicodeString __fastcall TConfiguration::GetTimeFormat()
  1757. {
  1758. return L"h:nn:ss";
  1759. }
  1760. //---------------------------------------------------------------------------
  1761. UnicodeString __fastcall TConfiguration::GetPartialExt() const
  1762. {
  1763. return PARTIAL_EXT;
  1764. }
  1765. //---------------------------------------------------------------------------
  1766. UnicodeString __fastcall TConfiguration::GetDefaultKeyFile()
  1767. {
  1768. return L"";
  1769. }
  1770. //---------------------------------------------------------------------------
  1771. bool __fastcall TConfiguration::GetRememberPassword()
  1772. {
  1773. return false;
  1774. }
  1775. //---------------------------------------------------------------------------
  1776. void __fastcall TConfiguration::SetSessionReopenAuto(int value)
  1777. {
  1778. SET_CONFIG_PROPERTY(SessionReopenAuto);
  1779. }
  1780. //---------------------------------------------------------------------------
  1781. void __fastcall TConfiguration::SetSessionReopenBackground(int value)
  1782. {
  1783. SET_CONFIG_PROPERTY(SessionReopenBackground);
  1784. }
  1785. //---------------------------------------------------------------------------
  1786. void __fastcall TConfiguration::SetSessionReopenTimeout(int value)
  1787. {
  1788. SET_CONFIG_PROPERTY(SessionReopenTimeout);
  1789. }
  1790. //---------------------------------------------------------------------------
  1791. void __fastcall TConfiguration::SetSessionReopenAutoStall(int value)
  1792. {
  1793. SET_CONFIG_PROPERTY(SessionReopenAutoStall);
  1794. }
  1795. //---------------------------------------------------------------------------
  1796. void __fastcall TConfiguration::SetTunnelLocalPortNumberLow(int value)
  1797. {
  1798. SET_CONFIG_PROPERTY(TunnelLocalPortNumberLow);
  1799. }
  1800. //---------------------------------------------------------------------------
  1801. void __fastcall TConfiguration::SetTunnelLocalPortNumberHigh(int value)
  1802. {
  1803. SET_CONFIG_PROPERTY(TunnelLocalPortNumberHigh);
  1804. }
  1805. //---------------------------------------------------------------------------
  1806. void __fastcall TConfiguration::SetCacheDirectoryChangesMaxSize(int value)
  1807. {
  1808. SET_CONFIG_PROPERTY(CacheDirectoryChangesMaxSize);
  1809. }
  1810. //---------------------------------------------------------------------------
  1811. void __fastcall TConfiguration::SetShowFtpWelcomeMessage(bool value)
  1812. {
  1813. SET_CONFIG_PROPERTY(ShowFtpWelcomeMessage);
  1814. }
  1815. //---------------------------------------------------------------------------
  1816. bool __fastcall TConfiguration::GetPersistent()
  1817. {
  1818. return (Storage != stNul) && !FDontSave;
  1819. }
  1820. //---------------------------------------------------------------------------
  1821. //---------------------------------------------------------------------------
  1822. void __fastcall TShortCuts::Add(TShortCut ShortCut)
  1823. {
  1824. FShortCuts.insert(ShortCut);
  1825. }
  1826. //---------------------------------------------------------------------------
  1827. bool __fastcall TShortCuts::Has(TShortCut ShortCut) const
  1828. {
  1829. return (FShortCuts.count(ShortCut) != 0);
  1830. }