Configuration.cpp 60 KB

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