Configuration.cpp 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912
  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. if (!FIniFileStorageName.IsEmpty())
  1184. {
  1185. Result = FIniFileStorageName;
  1186. }
  1187. else if (!FCustomIniFileStorageName.IsEmpty())
  1188. {
  1189. Result = FCustomIniFileStorageName;
  1190. }
  1191. else
  1192. {
  1193. Result = GetAutomaticIniFileStorageName(ReadingOnly);
  1194. }
  1195. return Result;
  1196. }
  1197. //---------------------------------------------------------------------------
  1198. void __fastcall TConfiguration::SetOptionsStorage(TStrings * value)
  1199. {
  1200. TGuard Guard(FCriticalSection);
  1201. if (FOptionsStorage.get() == NULL)
  1202. {
  1203. FOptionsStorage.reset(new TStringList());
  1204. }
  1205. FOptionsStorage->AddStrings(value);
  1206. }
  1207. //---------------------------------------------------------------------------
  1208. TStrings * __fastcall TConfiguration::GetOptionsStorage()
  1209. {
  1210. return FOptionsStorage.get();
  1211. }
  1212. //---------------------------------------------------------------------------
  1213. UnicodeString __fastcall TConfiguration::GetPuttySessionsSubKey()
  1214. {
  1215. return StoredSessionsSubKey;
  1216. }
  1217. //---------------------------------------------------------------------------
  1218. UnicodeString __fastcall TConfiguration::GetPuttySessionsKey()
  1219. {
  1220. return PuttyRegistryStorageKey + L"\\" + PuttySessionsSubKey;
  1221. }
  1222. //---------------------------------------------------------------------------
  1223. UnicodeString __fastcall TConfiguration::GetStoredSessionsSubKey()
  1224. {
  1225. return L"Sessions";
  1226. }
  1227. //---------------------------------------------------------------------------
  1228. UnicodeString __fastcall TConfiguration::GetSshHostKeysSubKey()
  1229. {
  1230. return L"SshHostKeys";
  1231. }
  1232. //---------------------------------------------------------------------------
  1233. UnicodeString __fastcall TConfiguration::GetConfigurationSubKey()
  1234. {
  1235. return L"Configuration";
  1236. }
  1237. //---------------------------------------------------------------------------
  1238. UnicodeString __fastcall TConfiguration::GetRootKeyStr()
  1239. {
  1240. return RootKeyToStr(HKEY_CURRENT_USER);
  1241. }
  1242. //---------------------------------------------------------------------------
  1243. void __fastcall TConfiguration::MoveStorage(TStorage AStorage, const UnicodeString & ACustomIniFileStorageName)
  1244. {
  1245. if ((FStorage != AStorage) ||
  1246. !IsPathToSameFile(FCustomIniFileStorageName, ACustomIniFileStorageName))
  1247. {
  1248. TStorage StorageBak = FStorage;
  1249. UnicodeString CustomIniFileStorageNameBak = FCustomIniFileStorageName;
  1250. try
  1251. {
  1252. THierarchicalStorage * SourceStorage = NULL;
  1253. THierarchicalStorage * TargetStorage = NULL;
  1254. try
  1255. {
  1256. SourceStorage = CreateConfigStorage();
  1257. SourceStorage->AccessMode = smRead;
  1258. FStorage = AStorage;
  1259. FCustomIniFileStorageName = ACustomIniFileStorageName;
  1260. TargetStorage = CreateConfigStorage();
  1261. TargetStorage->AccessMode = smReadWrite;
  1262. TargetStorage->Explicit = true;
  1263. // copy before save as it removes the ini file,
  1264. // when switching from ini to registry
  1265. CopyData(SourceStorage, TargetStorage);
  1266. }
  1267. __finally
  1268. {
  1269. delete SourceStorage;
  1270. delete TargetStorage;
  1271. }
  1272. // save all and explicit,
  1273. // this also removes an INI file, when switching to registry storage
  1274. DoSave(true, true);
  1275. }
  1276. catch (...)
  1277. {
  1278. // If this fails, do not pretend that storage was switched.
  1279. // For instance:
  1280. // - When writing to an INI file fails (unlikely, as we fallback to user profile)
  1281. // - When removing INI file fails, when switching to registry
  1282. // (possible, when the INI file is in Program Files folder)
  1283. FStorage = StorageBak;
  1284. FCustomIniFileStorageName = CustomIniFileStorageNameBak;
  1285. throw;
  1286. }
  1287. }
  1288. }
  1289. //---------------------------------------------------------------------------
  1290. void __fastcall TConfiguration::ScheduleCustomIniFileStorageUse(const UnicodeString & ACustomIniFileStorageName)
  1291. {
  1292. FStorage = stIniFile;
  1293. FCustomIniFileStorageName = ACustomIniFileStorageName;
  1294. SaveCustomIniFileStorageName();
  1295. }
  1296. //---------------------------------------------------------------------------
  1297. void __fastcall TConfiguration::Saved()
  1298. {
  1299. // nothing
  1300. }
  1301. //---------------------------------------------------------------------------
  1302. TStorage __fastcall TConfiguration::GetStorage()
  1303. {
  1304. TGuard Guard(FCriticalSection);
  1305. if (FStorage == stDetect)
  1306. {
  1307. if (FileExists(ApiPath(IniFileStorageNameForReading)))
  1308. {
  1309. FStorage = stIniFile;
  1310. }
  1311. else
  1312. {
  1313. FStorage = stRegistry;
  1314. }
  1315. }
  1316. return FStorage;
  1317. }
  1318. //---------------------------------------------------------------------
  1319. TStoredSessionList * __fastcall TConfiguration::SelectFilezillaSessionsForImport(
  1320. TStoredSessionList * Sessions, UnicodeString & Error)
  1321. {
  1322. std::unique_ptr<TStoredSessionList> ImportSessionList(new TStoredSessionList(true));
  1323. ImportSessionList->DefaultSettings = Sessions->DefaultSettings;
  1324. UnicodeString AppDataPath = GetShellFolderPath(CSIDL_APPDATA);
  1325. UnicodeString FilezillaSiteManagerFile =
  1326. IncludeTrailingBackslash(AppDataPath) + L"FileZilla\\sitemanager.xml";
  1327. UnicodeString FilezillaConfigurationFile =
  1328. IncludeTrailingBackslash(AppDataPath) + L"FileZilla\\filezilla.xml";
  1329. if (FileExists(ApiPath(FilezillaSiteManagerFile)))
  1330. {
  1331. ImportSessionList->ImportFromFilezilla(FilezillaSiteManagerFile, FilezillaConfigurationFile);
  1332. if (ImportSessionList->Count > 0)
  1333. {
  1334. ImportSessionList->SelectSessionsToImport(Sessions, true);
  1335. }
  1336. else
  1337. {
  1338. Error = FMTLOAD(FILEZILLA_NO_SITES, (FilezillaSiteManagerFile));
  1339. }
  1340. }
  1341. else
  1342. {
  1343. Error = FMTLOAD(FILEZILLA_SITE_MANAGER_NOT_FOUND, (FilezillaSiteManagerFile));
  1344. }
  1345. return ImportSessionList.release();
  1346. }
  1347. //---------------------------------------------------------------------
  1348. bool __fastcall TConfiguration::AnyFilezillaSessionForImport(TStoredSessionList * Sessions)
  1349. {
  1350. try
  1351. {
  1352. UnicodeString Error;
  1353. std::unique_ptr<TStoredSessionList> Sesssions(SelectFilezillaSessionsForImport(Sessions, Error));
  1354. return (Sesssions->Count > 0);
  1355. }
  1356. catch (...)
  1357. {
  1358. return false;
  1359. }
  1360. }
  1361. //---------------------------------------------------------------------
  1362. TStoredSessionList * __fastcall TConfiguration::SelectKnownHostsSessionsForImport(
  1363. TStoredSessionList * Sessions, UnicodeString & Error)
  1364. {
  1365. std::unique_ptr<TStoredSessionList> ImportSessionList(new TStoredSessionList(true));
  1366. ImportSessionList->DefaultSettings = Sessions->DefaultSettings;
  1367. UnicodeString ProfilePath = GetShellFolderPath(CSIDL_PROFILE);
  1368. UnicodeString KnownHostsFile = IncludeTrailingBackslash(ProfilePath) + L".ssh\\known_hosts";
  1369. try
  1370. {
  1371. if (FileExists(ApiPath(KnownHostsFile)))
  1372. {
  1373. std::unique_ptr<TStrings> Lines(new TStringList());
  1374. LoadScriptFromFile(KnownHostsFile, Lines.get(), true);
  1375. ImportSessionList->ImportFromKnownHosts(Lines.get());
  1376. }
  1377. else
  1378. {
  1379. throw Exception(LoadStr(KNOWN_HOSTS_NOT_FOUND));
  1380. }
  1381. }
  1382. catch (Exception & E)
  1383. {
  1384. Error = FORMAT(L"%s\n(%s)", (E.Message, KnownHostsFile));
  1385. }
  1386. return ImportSessionList.release();
  1387. }
  1388. //---------------------------------------------------------------------
  1389. TStoredSessionList * __fastcall TConfiguration::SelectKnownHostsSessionsForImport(
  1390. TStrings * Lines, TStoredSessionList * Sessions, UnicodeString & Error)
  1391. {
  1392. std::unique_ptr<TStoredSessionList> ImportSessionList(new TStoredSessionList(true));
  1393. ImportSessionList->DefaultSettings = Sessions->DefaultSettings;
  1394. try
  1395. {
  1396. ImportSessionList->ImportFromKnownHosts(Lines);
  1397. }
  1398. catch (Exception & E)
  1399. {
  1400. Error = E.Message;
  1401. }
  1402. return ImportSessionList.release();
  1403. }
  1404. //---------------------------------------------------------------------------
  1405. void __fastcall TConfiguration::SetRandomSeedFile(UnicodeString value)
  1406. {
  1407. if (RandomSeedFile != value)
  1408. {
  1409. UnicodeString PrevRandomSeedFileName = RandomSeedFileName;
  1410. FRandomSeedFile = value;
  1411. // never allow empty seed file to avoid Putty trying to reinitialize the path
  1412. if (RandomSeedFileName.IsEmpty())
  1413. {
  1414. FRandomSeedFile = FDefaultRandomSeedFile;
  1415. }
  1416. if (!PrevRandomSeedFileName.IsEmpty() &&
  1417. (PrevRandomSeedFileName != RandomSeedFileName) &&
  1418. FileExists(ApiPath(PrevRandomSeedFileName)))
  1419. {
  1420. // ignore any error
  1421. DeleteFile(ApiPath(PrevRandomSeedFileName));
  1422. }
  1423. }
  1424. }
  1425. //---------------------------------------------------------------------------
  1426. UnicodeString __fastcall TConfiguration::GetDirectoryStatisticsCacheKey(
  1427. const UnicodeString & SessionKey, const UnicodeString & Path, const TCopyParamType & CopyParam)
  1428. {
  1429. std::unique_ptr<TStringList> RawOptions(new TStringList());
  1430. RawOptions->Add(SessionKey);
  1431. RawOptions->Add(UnixExcludeTrailingBackslash(Path));
  1432. TCopyParamType Defaults;
  1433. TCopyParamType FilterCopyParam;
  1434. FilterCopyParam.IncludeFileMask = CopyParam.IncludeFileMask;
  1435. FilterCopyParam.ExcludeHiddenFiles = CopyParam.ExcludeHiddenFiles;
  1436. FilterCopyParam.ExcludeEmptyDirectories = CopyParam.ExcludeEmptyDirectories;
  1437. std::unique_ptr<TOptionsStorage> OptionsStorage(new TOptionsStorage(RawOptions.get(), true));
  1438. FilterCopyParam.Save(OptionsStorage.get(), &Defaults);
  1439. UTF8String RawOptionsBuf(RawOptions->CommaText.LowerCase());
  1440. UnicodeString Result = Sha256(RawOptionsBuf.c_str(), RawOptionsBuf.Length());
  1441. return Result;
  1442. }
  1443. //---------------------------------------------------------------------------
  1444. THierarchicalStorage * TConfiguration::OpenDirectoryStatisticsCache(bool CanCreate)
  1445. {
  1446. std::unique_ptr<THierarchicalStorage> Storage(Configuration->CreateConfigStorage());
  1447. Storage->AccessMode = CanCreate ? smReadWrite : smRead;
  1448. if (!Storage->OpenSubKey(DirectoryStatisticsCacheKey, CanCreate))
  1449. {
  1450. Storage.reset(NULL);
  1451. }
  1452. return Storage.release();
  1453. }
  1454. //---------------------------------------------------------------------------
  1455. TStrings * __fastcall TConfiguration::LoadDirectoryStatisticsCache(
  1456. const UnicodeString & SessionKey, const UnicodeString & Path, const TCopyParamType & CopyParam)
  1457. {
  1458. std::unique_ptr<THierarchicalStorage> Storage(OpenDirectoryStatisticsCache(false));
  1459. std::unique_ptr<TStringList> Result(new TStringList());
  1460. if (Storage.get() != NULL)
  1461. {
  1462. UnicodeString Key = GetDirectoryStatisticsCacheKey(SessionKey, Path, CopyParam);
  1463. UnicodeString Buf = Storage->ReadString(Key, UnicodeString());
  1464. Result->CommaText = Buf;
  1465. }
  1466. return Result.release();
  1467. }
  1468. //---------------------------------------------------------------------------
  1469. void __fastcall TConfiguration::SaveDirectoryStatisticsCache(
  1470. const UnicodeString & SessionKey, const UnicodeString & Path, const TCopyParamType & CopyParam, TStrings * DataList)
  1471. {
  1472. std::unique_ptr<THierarchicalStorage> Storage(OpenDirectoryStatisticsCache(true));
  1473. if (Storage.get() != NULL)
  1474. {
  1475. UnicodeString Key = GetDirectoryStatisticsCacheKey(SessionKey, Path, CopyParam);
  1476. UnicodeString Buf = DataList->CommaText;
  1477. Storage->WriteString(Key, Buf);
  1478. }
  1479. }
  1480. //---------------------------------------------------------------------
  1481. UnicodeString __fastcall TConfiguration::GetRandomSeedFileName()
  1482. {
  1483. // StripPathQuotes should not be needed as we do not feed quotes anymore
  1484. return StripPathQuotes(ExpandEnvironmentVariables(FRandomSeedFile)).Trim();
  1485. }
  1486. //---------------------------------------------------------------------
  1487. void __fastcall TConfiguration::SetExternalIpAddress(UnicodeString value)
  1488. {
  1489. SET_CONFIG_PROPERTY(ExternalIpAddress);
  1490. }
  1491. //---------------------------------------------------------------------
  1492. void __fastcall TConfiguration::SetMimeTypes(UnicodeString value)
  1493. {
  1494. SET_CONFIG_PROPERTY(MimeTypes);
  1495. }
  1496. //---------------------------------------------------------------------
  1497. void __fastcall TConfiguration::SetTryFtpWhenSshFails(bool value)
  1498. {
  1499. SET_CONFIG_PROPERTY(TryFtpWhenSshFails);
  1500. }
  1501. //---------------------------------------------------------------------
  1502. void __fastcall TConfiguration::SetParallelDurationThreshold(int value)
  1503. {
  1504. SET_CONFIG_PROPERTY(ParallelDurationThreshold);
  1505. }
  1506. //---------------------------------------------------------------------
  1507. void __fastcall TConfiguration::SetPuttyRegistryStorageKey(UnicodeString value)
  1508. {
  1509. SET_CONFIG_PROPERTY(PuttyRegistryStorageKey);
  1510. }
  1511. //---------------------------------------------------------------------------
  1512. TEOLType __fastcall TConfiguration::GetLocalEOLType()
  1513. {
  1514. return eolCRLF;
  1515. }
  1516. //---------------------------------------------------------------------
  1517. bool __fastcall TConfiguration::GetCollectUsage()
  1518. {
  1519. return FUsage->Collect;
  1520. }
  1521. //---------------------------------------------------------------------
  1522. void __fastcall TConfiguration::SetCollectUsage(bool value)
  1523. {
  1524. FUsage->Collect = value;
  1525. }
  1526. //---------------------------------------------------------------------
  1527. void __fastcall TConfiguration::TemporaryLogging(const UnicodeString ALogFileName)
  1528. {
  1529. if (SameText(ExtractFileExt(ALogFileName), L".xml"))
  1530. {
  1531. TemporaryActionsLogging(ALogFileName);
  1532. }
  1533. else
  1534. {
  1535. FLogging = true;
  1536. FLogFileName = ALogFileName;
  1537. UpdateActualLogProtocol();
  1538. }
  1539. }
  1540. //---------------------------------------------------------------------
  1541. void __fastcall TConfiguration::TemporaryActionsLogging(const UnicodeString ALogFileName)
  1542. {
  1543. FLogActions = true;
  1544. FActionsLogFileName = ALogFileName;
  1545. }
  1546. //---------------------------------------------------------------------
  1547. void __fastcall TConfiguration::TemporaryLogProtocol(int ALogProtocol)
  1548. {
  1549. FLogProtocol = ALogProtocol;
  1550. UpdateActualLogProtocol();
  1551. }
  1552. //---------------------------------------------------------------------
  1553. void __fastcall TConfiguration::TemporaryLogSensitive(bool ALogSensitive)
  1554. {
  1555. FLogSensitive = ALogSensitive;
  1556. }
  1557. //---------------------------------------------------------------------
  1558. void __fastcall TConfiguration::TemporaryLogMaxSize(__int64 ALogMaxSize)
  1559. {
  1560. FLogMaxSize = ALogMaxSize;
  1561. }
  1562. //---------------------------------------------------------------------
  1563. void __fastcall TConfiguration::TemporaryLogMaxCount(int ALogMaxCount)
  1564. {
  1565. FLogMaxCount = ALogMaxCount;
  1566. }
  1567. //---------------------------------------------------------------------
  1568. void __fastcall TConfiguration::SetLogging(bool value)
  1569. {
  1570. TGuard Guard(FCriticalSection);
  1571. if (Logging != value)
  1572. {
  1573. FPermanentLogging = value;
  1574. FLogging = value;
  1575. UpdateActualLogProtocol();
  1576. Changed();
  1577. }
  1578. }
  1579. //---------------------------------------------------------------------
  1580. bool __fastcall TConfiguration::GetLogging()
  1581. {
  1582. TGuard Guard(FCriticalSection);
  1583. return FPermanentLogging;
  1584. }
  1585. //---------------------------------------------------------------------
  1586. void __fastcall TConfiguration::SetLogFileName(UnicodeString value)
  1587. {
  1588. TGuard Guard(FCriticalSection);
  1589. if (LogFileName != value)
  1590. {
  1591. FPermanentLogFileName = value;
  1592. FLogFileName = value;
  1593. Changed();
  1594. }
  1595. }
  1596. //---------------------------------------------------------------------
  1597. UnicodeString __fastcall TConfiguration::GetLogFileName()
  1598. {
  1599. TGuard Guard(FCriticalSection);
  1600. return FPermanentLogFileName;
  1601. }
  1602. //---------------------------------------------------------------------
  1603. void __fastcall TConfiguration::SetActionsLogFileName(UnicodeString value)
  1604. {
  1605. TGuard Guard(FCriticalSection);
  1606. if (ActionsLogFileName != value)
  1607. {
  1608. FPermanentActionsLogFileName = value;
  1609. FActionsLogFileName = value;
  1610. Changed();
  1611. }
  1612. }
  1613. //---------------------------------------------------------------------
  1614. UnicodeString __fastcall TConfiguration::GetPermanentActionsLogFileName()
  1615. {
  1616. TGuard Guard(FCriticalSection);
  1617. return FPermanentActionsLogFileName;
  1618. }
  1619. //---------------------------------------------------------------------
  1620. UnicodeString __fastcall TConfiguration::GetActionsLogFileName()
  1621. {
  1622. TGuard Guard(FCriticalSection);
  1623. return FActionsLogFileName;
  1624. }
  1625. //---------------------------------------------------------------------
  1626. bool __fastcall TConfiguration::GetLogToFile()
  1627. {
  1628. // guarded within GetLogFileName
  1629. return !LogFileName.IsEmpty();
  1630. }
  1631. //---------------------------------------------------------------------
  1632. void __fastcall TConfiguration::UpdateActualLogProtocol()
  1633. {
  1634. FActualLogProtocol = FLogging ? FLogProtocol : 0;
  1635. }
  1636. //---------------------------------------------------------------------
  1637. void __fastcall TConfiguration::SetLogProtocol(int value)
  1638. {
  1639. TGuard Guard(FCriticalSection);
  1640. if (LogProtocol != value)
  1641. {
  1642. FPermanentLogProtocol = value;
  1643. FLogProtocol = value;
  1644. Changed();
  1645. UpdateActualLogProtocol();
  1646. }
  1647. }
  1648. //---------------------------------------------------------------------
  1649. void __fastcall TConfiguration::SetLogActions(bool value)
  1650. {
  1651. TGuard Guard(FCriticalSection);
  1652. if (LogActions != value)
  1653. {
  1654. FPermanentLogActions = value;
  1655. FLogActions = value;
  1656. Changed();
  1657. }
  1658. }
  1659. //---------------------------------------------------------------------
  1660. bool __fastcall TConfiguration::GetLogActions()
  1661. {
  1662. TGuard Guard(FCriticalSection);
  1663. return FPermanentLogActions;
  1664. }
  1665. //---------------------------------------------------------------------
  1666. void __fastcall TConfiguration::SetLogFileAppend(bool value)
  1667. {
  1668. SET_CONFIG_PROPERTY(LogFileAppend);
  1669. }
  1670. //---------------------------------------------------------------------
  1671. void __fastcall TConfiguration::SetLogSensitive(bool value)
  1672. {
  1673. if (LogSensitive != value)
  1674. {
  1675. FPermanentLogSensitive = value;
  1676. FLogSensitive = value;
  1677. Changed();
  1678. }
  1679. }
  1680. //---------------------------------------------------------------------
  1681. void __fastcall TConfiguration::SetLogMaxSize(__int64 value)
  1682. {
  1683. TGuard Guard(FCriticalSection);
  1684. if (LogMaxSize != value)
  1685. {
  1686. FPermanentLogMaxSize = value;
  1687. FLogMaxSize = value;
  1688. Changed();
  1689. }
  1690. }
  1691. //---------------------------------------------------------------------
  1692. __int64 __fastcall TConfiguration::GetLogMaxSize()
  1693. {
  1694. TGuard Guard(FCriticalSection);
  1695. return FPermanentLogMaxSize;
  1696. }
  1697. //---------------------------------------------------------------------
  1698. void __fastcall TConfiguration::SetLogMaxCount(int value)
  1699. {
  1700. if (LogMaxCount != value)
  1701. {
  1702. FPermanentLogMaxCount = value;
  1703. FLogMaxCount = value;
  1704. Changed();
  1705. }
  1706. }
  1707. //---------------------------------------------------------------------
  1708. int __fastcall TConfiguration::GetLogMaxCount()
  1709. {
  1710. TGuard Guard(FCriticalSection);
  1711. return FPermanentLogMaxCount;
  1712. }
  1713. //---------------------------------------------------------------------
  1714. UnicodeString __fastcall TConfiguration::GetDefaultLogFileName()
  1715. {
  1716. return L"%TEMP%\\!S.log";
  1717. }
  1718. //---------------------------------------------------------------------------
  1719. void __fastcall TConfiguration::SetConfirmOverwriting(bool value)
  1720. {
  1721. TGuard Guard(FCriticalSection);
  1722. SET_CONFIG_PROPERTY(ConfirmOverwriting);
  1723. }
  1724. //---------------------------------------------------------------------------
  1725. bool __fastcall TConfiguration::GetConfirmOverwriting()
  1726. {
  1727. TGuard Guard(FCriticalSection);
  1728. return FConfirmOverwriting;
  1729. }
  1730. //---------------------------------------------------------------------------
  1731. void __fastcall TConfiguration::SetConfirmResume(bool value)
  1732. {
  1733. TGuard Guard(FCriticalSection);
  1734. SET_CONFIG_PROPERTY(ConfirmResume);
  1735. }
  1736. //---------------------------------------------------------------------------
  1737. bool __fastcall TConfiguration::GetConfirmResume()
  1738. {
  1739. TGuard Guard(FCriticalSection);
  1740. return FConfirmResume;
  1741. }
  1742. //---------------------------------------------------------------------------
  1743. void __fastcall TConfiguration::SetAutoReadDirectoryAfterOp(bool value)
  1744. {
  1745. TGuard Guard(FCriticalSection);
  1746. SET_CONFIG_PROPERTY(AutoReadDirectoryAfterOp);
  1747. }
  1748. //---------------------------------------------------------------------------
  1749. bool __fastcall TConfiguration::GetAutoReadDirectoryAfterOp()
  1750. {
  1751. TGuard Guard(FCriticalSection);
  1752. return FAutoReadDirectoryAfterOp;
  1753. }
  1754. //---------------------------------------------------------------------------
  1755. UnicodeString __fastcall TConfiguration::GetTimeFormat()
  1756. {
  1757. return L"h:nn:ss";
  1758. }
  1759. //---------------------------------------------------------------------------
  1760. UnicodeString __fastcall TConfiguration::GetPartialExt() const
  1761. {
  1762. return PARTIAL_EXT;
  1763. }
  1764. //---------------------------------------------------------------------------
  1765. UnicodeString __fastcall TConfiguration::GetDefaultKeyFile()
  1766. {
  1767. return L"";
  1768. }
  1769. //---------------------------------------------------------------------------
  1770. bool __fastcall TConfiguration::GetRememberPassword()
  1771. {
  1772. return false;
  1773. }
  1774. //---------------------------------------------------------------------------
  1775. void __fastcall TConfiguration::SetSessionReopenAuto(int value)
  1776. {
  1777. SET_CONFIG_PROPERTY(SessionReopenAuto);
  1778. }
  1779. //---------------------------------------------------------------------------
  1780. void __fastcall TConfiguration::SetSessionReopenBackground(int value)
  1781. {
  1782. SET_CONFIG_PROPERTY(SessionReopenBackground);
  1783. }
  1784. //---------------------------------------------------------------------------
  1785. void __fastcall TConfiguration::SetSessionReopenTimeout(int value)
  1786. {
  1787. SET_CONFIG_PROPERTY(SessionReopenTimeout);
  1788. }
  1789. //---------------------------------------------------------------------------
  1790. void __fastcall TConfiguration::SetSessionReopenAutoStall(int value)
  1791. {
  1792. SET_CONFIG_PROPERTY(SessionReopenAutoStall);
  1793. }
  1794. //---------------------------------------------------------------------------
  1795. void __fastcall TConfiguration::SetTunnelLocalPortNumberLow(int value)
  1796. {
  1797. SET_CONFIG_PROPERTY(TunnelLocalPortNumberLow);
  1798. }
  1799. //---------------------------------------------------------------------------
  1800. void __fastcall TConfiguration::SetTunnelLocalPortNumberHigh(int value)
  1801. {
  1802. SET_CONFIG_PROPERTY(TunnelLocalPortNumberHigh);
  1803. }
  1804. //---------------------------------------------------------------------------
  1805. void __fastcall TConfiguration::SetCacheDirectoryChangesMaxSize(int value)
  1806. {
  1807. SET_CONFIG_PROPERTY(CacheDirectoryChangesMaxSize);
  1808. }
  1809. //---------------------------------------------------------------------------
  1810. void __fastcall TConfiguration::SetShowFtpWelcomeMessage(bool value)
  1811. {
  1812. SET_CONFIG_PROPERTY(ShowFtpWelcomeMessage);
  1813. }
  1814. //---------------------------------------------------------------------------
  1815. bool __fastcall TConfiguration::GetPersistent()
  1816. {
  1817. return (Storage != stNul) && !FDontSave;
  1818. }
  1819. //---------------------------------------------------------------------------
  1820. //---------------------------------------------------------------------------
  1821. void __fastcall TShortCuts::Add(TShortCut ShortCut)
  1822. {
  1823. FShortCuts.insert(ShortCut);
  1824. }
  1825. //---------------------------------------------------------------------------
  1826. bool __fastcall TShortCuts::Has(TShortCut ShortCut) const
  1827. {
  1828. return (FShortCuts.count(ShortCut) != 0);
  1829. }