CustomWinConfiguration.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include <Common.h>
  5. #include <TextsCore.h>
  6. #include <SessionData.h>
  7. #include <CoreMain.h>
  8. #include <Interface.h>
  9. #include "CustomWinConfiguration.h"
  10. //---------------------------------------------------------------------------
  11. #pragma package(smart_init)
  12. //---------------------------------------------------------------------------
  13. class THistoryStrings : public TStringList
  14. {
  15. public:
  16. __fastcall THistoryStrings() : TStringList()
  17. {
  18. FModified = false;
  19. };
  20. __property bool Modified = { read = FModified, write = FModified };
  21. private:
  22. bool FModified;
  23. };
  24. //---------------------------------------------------------------------------
  25. __fastcall TCustomWinConfiguration::TCustomWinConfiguration():
  26. TGUIConfiguration()
  27. {
  28. FHistory = new TStringList();
  29. FEmptyHistory = new TStringList();
  30. FDefaultInterface = ifCommander;
  31. FDefaultShowAdvancedLoginOptions = false;
  32. }
  33. //---------------------------------------------------------------------------
  34. __fastcall TCustomWinConfiguration::~TCustomWinConfiguration()
  35. {
  36. ClearHistory();
  37. delete FHistory;
  38. delete FEmptyHistory;
  39. }
  40. //---------------------------------------------------------------------------
  41. void __fastcall TCustomWinConfiguration::ClearHistory()
  42. {
  43. assert(FHistory != NULL);
  44. THistoryStrings * HistoryStrings;
  45. for (int Index = 0; Index < FHistory->Count; Index++)
  46. {
  47. HistoryStrings = dynamic_cast<THistoryStrings *>(FHistory->Objects[Index]);
  48. FHistory->Objects[Index] = NULL;
  49. delete HistoryStrings;
  50. }
  51. FHistory->Clear();
  52. }
  53. //---------------------------------------------------------------------------
  54. void __fastcall TCustomWinConfiguration::DefaultHistory()
  55. {
  56. ClearHistory();
  57. // defaults for speed limits
  58. THistoryStrings * Strings = new THistoryStrings();
  59. Strings->Add(LoadStr(SPEED_UNLIMITED));
  60. unsigned long Speed = 1024;
  61. while (Speed >= 8)
  62. {
  63. Strings->Add(IntToStr(Speed));
  64. Speed = Speed / 2;
  65. }
  66. FHistory->AddObject("SpeedLimit", Strings);
  67. }
  68. //---------------------------------------------------------------------------
  69. void __fastcall TCustomWinConfiguration::Default()
  70. {
  71. TGUIConfiguration::Default();
  72. FShowAdvancedLoginOptions = FDefaultShowAdvancedLoginOptions;
  73. FInterface = FDefaultInterface;
  74. FLogView = lvNone;
  75. FSynchronizeChecklist.WindowParams = "0;-1;-1;600;450;0";
  76. FSynchronizeChecklist.ListParams = "1;1|150,1;100,1;80,1;130,1;25,1;100,1;80,1;130,1|0;1;2;3;4;5;6;7";
  77. FFindFile.WindowParams = "646,481";
  78. FFindFile.ListParams = "3;1|125,1;181,1;80,1;122,1|0;1;2;3";
  79. FConsoleWin.WindowSize = "570,430";
  80. FConfirmExitOnCompletion = true;
  81. FOperationProgressOnTop = true;
  82. DefaultHistory();
  83. }
  84. //---------------------------------------------------------------------------
  85. void __fastcall TCustomWinConfiguration::Saved()
  86. {
  87. TGUIConfiguration::Saved();
  88. THistoryStrings * HistoryStrings;
  89. for (int Index = 0; Index < FHistory->Count; Index++)
  90. {
  91. HistoryStrings = dynamic_cast<THistoryStrings *>(FHistory->Objects[Index]);
  92. assert(HistoryStrings != NULL);
  93. HistoryStrings->Modified = false;
  94. }
  95. }
  96. //---------------------------------------------------------------------------
  97. // duplicated from core\configuration.cpp
  98. #define LASTELEM(ELEM) \
  99. ELEM.SubString(ELEM.LastDelimiter(".>")+1, ELEM.Length() - ELEM.LastDelimiter(".>"))
  100. #define BLOCK(KEY, CANCREATE, BLOCK) \
  101. if (Storage->OpenSubKey(KEY, CANCREATE, true)) try { BLOCK } __finally { Storage->CloseSubKey(); }
  102. #define REGCONFIG(CANCREATE) \
  103. BLOCK("Interface", CANCREATE, \
  104. KEY(Integer, Interface); \
  105. KEY(Bool, ShowAdvancedLoginOptions); \
  106. KEY(Bool, ConfirmExitOnCompletion); \
  107. ) \
  108. BLOCK("Logging", CANCREATE, \
  109. KEY(Integer, LogView); \
  110. ) \
  111. BLOCK("Interface\\SynchronizeChecklist", CANCREATE, \
  112. KEY(String, SynchronizeChecklist.WindowParams); \
  113. KEY(String, SynchronizeChecklist.ListParams); \
  114. ); \
  115. BLOCK("Interface\\FindFile", CANCREATE, \
  116. KEY(String, FindFile.WindowParams); \
  117. KEY(String, FindFile.ListParams); \
  118. ); \
  119. BLOCK("Interface\\ConsoleWin", CANCREATE, \
  120. KEY(String, ConsoleWin.WindowSize); \
  121. ); \
  122. //---------------------------------------------------------------------------
  123. void __fastcall TCustomWinConfiguration::SaveData(
  124. THierarchicalStorage * Storage, bool All)
  125. {
  126. TGUIConfiguration::SaveData(Storage, All);
  127. // duplicated from core\configuration.cpp
  128. #define KEY(TYPE, VAR) Storage->Write ## TYPE(LASTELEM(AnsiString(#VAR)), VAR)
  129. REGCONFIG(true);
  130. #undef KEY
  131. if (FHistory->Count > 0)
  132. {
  133. if (Storage->OpenSubKey("History", true))
  134. {
  135. try
  136. {
  137. THistoryStrings * HistoryStrings;
  138. for (int Index = 0; Index < FHistory->Count; Index++)
  139. {
  140. HistoryStrings = dynamic_cast<THistoryStrings *>(FHistory->Objects[Index]);
  141. assert(HistoryStrings != NULL);
  142. if (All || HistoryStrings->Modified)
  143. {
  144. if (Storage->OpenSubKey(FHistory->Strings[Index], true))
  145. {
  146. try
  147. {
  148. Storage->WriteValues(HistoryStrings);
  149. }
  150. __finally
  151. {
  152. Storage->CloseSubKey();
  153. }
  154. }
  155. }
  156. }
  157. }
  158. __finally
  159. {
  160. Storage->CloseSubKey();
  161. }
  162. }
  163. if (Storage->OpenSubKey("HistoryParams", true))
  164. {
  165. try
  166. {
  167. THistoryStrings * HistoryStrings;
  168. for (int Index = 0; Index < FHistory->Count; Index++)
  169. {
  170. HistoryStrings = dynamic_cast<THistoryStrings *>(FHistory->Objects[Index]);
  171. assert(HistoryStrings != NULL);
  172. if (All || HistoryStrings->Modified)
  173. {
  174. bool HasData = false;
  175. for (int VIndex = 0; !HasData && (VIndex < HistoryStrings->Count); VIndex++)
  176. {
  177. HasData = (HistoryStrings->Objects[VIndex] != NULL);
  178. }
  179. if (!HasData)
  180. {
  181. Storage->RecursiveDeleteSubKey(FHistory->Strings[Index]);
  182. }
  183. else if (Storage->OpenSubKey(FHistory->Strings[Index], true))
  184. {
  185. try
  186. {
  187. Storage->ClearValues();
  188. for (int VIndex = 0; VIndex < HistoryStrings->Count; VIndex++)
  189. {
  190. void * Data = HistoryStrings->Objects[VIndex];
  191. Storage->WriteBinaryData(IntToStr(VIndex), &Data, sizeof(Data));
  192. }
  193. }
  194. __finally
  195. {
  196. Storage->CloseSubKey();
  197. }
  198. }
  199. }
  200. }
  201. }
  202. __finally
  203. {
  204. Storage->CloseSubKey();
  205. }
  206. }
  207. }
  208. }
  209. //---------------------------------------------------------------------------
  210. void __fastcall TCustomWinConfiguration::LoadData(
  211. THierarchicalStorage * Storage)
  212. {
  213. TGUIConfiguration::LoadData(Storage);
  214. // duplicated from core\configuration.cpp
  215. #define KEY(TYPE, VAR) VAR = Storage->Read ## TYPE(LASTELEM(AnsiString(#VAR)), VAR)
  216. #pragma warn -eas
  217. REGCONFIG(false);
  218. #pragma warn +eas
  219. #undef KEY
  220. DefaultHistory();
  221. if (Storage->OpenSubKey("History", false))
  222. {
  223. TStrings * Names = NULL;
  224. try
  225. {
  226. Names = new TStringList();
  227. Storage->GetSubKeyNames(Names);
  228. for (int Index = 0; Index < Names->Count; Index++)
  229. {
  230. if (Storage->OpenSubKey(Names->Strings[Index], false))
  231. {
  232. THistoryStrings * HistoryStrings = NULL;
  233. try
  234. {
  235. // remove defaults, if any
  236. int HIndex = FHistory->IndexOf(Names->Strings[Index]);
  237. if (HIndex >= 0)
  238. {
  239. THistoryStrings * DefaultStrings = dynamic_cast<THistoryStrings *>(FHistory->Objects[HIndex]);
  240. delete DefaultStrings;
  241. FHistory->Delete(HIndex);
  242. }
  243. HistoryStrings = new THistoryStrings();
  244. Storage->ReadValues(HistoryStrings);
  245. FHistory->AddObject(Names->Strings[Index], HistoryStrings);
  246. HistoryStrings = NULL;
  247. }
  248. __finally
  249. {
  250. Storage->CloseSubKey();
  251. delete HistoryStrings;
  252. }
  253. }
  254. }
  255. }
  256. __finally
  257. {
  258. Storage->CloseSubKey();
  259. delete Names;
  260. }
  261. }
  262. if (Storage->OpenSubKey("HistoryParams", false))
  263. {
  264. try
  265. {
  266. THistoryStrings * HistoryStrings;
  267. for (int Index = 0; Index < FHistory->Count; Index++)
  268. {
  269. HistoryStrings = dynamic_cast<THistoryStrings *>(FHistory->Objects[Index]);
  270. if (Storage->OpenSubKey(FHistory->Strings[Index], false))
  271. {
  272. try
  273. {
  274. for (int VIndex = 0; VIndex < HistoryStrings->Count; VIndex++)
  275. {
  276. void * Data;
  277. if (Storage->ReadBinaryData(IntToStr(VIndex), &Data, sizeof(Data)) ==
  278. sizeof(Data))
  279. {
  280. HistoryStrings->Objects[VIndex] = reinterpret_cast<TObject *>(Data);
  281. }
  282. }
  283. }
  284. __finally
  285. {
  286. Storage->CloseSubKey();
  287. }
  288. }
  289. }
  290. }
  291. __finally
  292. {
  293. Storage->CloseSubKey();
  294. }
  295. }
  296. }
  297. //---------------------------------------------------------------------------
  298. void __fastcall TCustomWinConfiguration::LoadAdmin(THierarchicalStorage * Storage)
  299. {
  300. TGUIConfiguration::LoadAdmin(Storage);
  301. FDefaultInterface = TInterface(Storage->ReadInteger("DefaultInterfaceInterface", FDefaultInterface));
  302. FDefaultShowAdvancedLoginOptions = Storage->ReadBool("DefaultInterfaceShowAdvancedLoginOptions", FDefaultShowAdvancedLoginOptions);
  303. }
  304. //---------------------------------------------------------------------------
  305. void __fastcall TCustomWinConfiguration::RecryptPasswords()
  306. {
  307. Busy(true);
  308. try
  309. {
  310. StoredSessions->RecryptPasswords();
  311. if (OnMasterPasswordRecrypt != NULL)
  312. {
  313. OnMasterPasswordRecrypt(NULL);
  314. }
  315. }
  316. __finally
  317. {
  318. Busy(false);
  319. }
  320. }
  321. //---------------------------------------------------------------------------
  322. void __fastcall TCustomWinConfiguration::SetShowAdvancedLoginOptions(bool value)
  323. {
  324. SET_CONFIG_PROPERTY(ShowAdvancedLoginOptions);
  325. }
  326. //---------------------------------------------------------------------
  327. void __fastcall TCustomWinConfiguration::SetLogView(TLogView value)
  328. {
  329. SET_CONFIG_PROPERTY(LogView);
  330. }
  331. //---------------------------------------------------------------------------
  332. void __fastcall TCustomWinConfiguration::SetInterface(TInterface value)
  333. {
  334. SET_CONFIG_PROPERTY(Interface);
  335. }
  336. //---------------------------------------------------------------------------
  337. void __fastcall TCustomWinConfiguration::SetHistory(const AnsiString Index,
  338. TStrings * value)
  339. {
  340. int I = FHistory->IndexOf(Index);
  341. bool NonEmpty = (value != NULL) && (value->Count > 0);
  342. THistoryStrings * HistoryStrings = NULL;
  343. if (I >= 0)
  344. {
  345. HistoryStrings = dynamic_cast<THistoryStrings *>(FHistory->Objects[I]);
  346. if (HistoryStrings->Equals(value))
  347. {
  348. HistoryStrings = NULL;
  349. }
  350. }
  351. else if (NonEmpty)
  352. {
  353. HistoryStrings = new THistoryStrings();
  354. FHistory->AddObject(Index, HistoryStrings);
  355. }
  356. if (HistoryStrings != NULL)
  357. {
  358. if (NonEmpty)
  359. {
  360. HistoryStrings->Assign(value);
  361. while (HistoryStrings->Count > MaxHistoryCount)
  362. {
  363. HistoryStrings->Delete(HistoryStrings->Count - 1);
  364. }
  365. }
  366. else
  367. {
  368. HistoryStrings->Clear();
  369. }
  370. HistoryStrings->Modified = true;
  371. }
  372. }
  373. //---------------------------------------------------------------------------
  374. TStrings * __fastcall TCustomWinConfiguration::GetHistory(const AnsiString Index)
  375. {
  376. int I = FHistory->IndexOf(Index);
  377. return I >= 0 ? dynamic_cast<TStrings *>(FHistory->Objects[I]) : FEmptyHistory;
  378. }
  379. //---------------------------------------------------------------------------
  380. void __fastcall TCustomWinConfiguration::SetSynchronizeChecklist(TSynchronizeChecklistConfiguration value)
  381. {
  382. SET_CONFIG_PROPERTY(SynchronizeChecklist);
  383. }
  384. //---------------------------------------------------------------------------
  385. void __fastcall TCustomWinConfiguration::SetFindFile(TFindFileConfiguration value)
  386. {
  387. SET_CONFIG_PROPERTY(FindFile);
  388. }
  389. //---------------------------------------------------------------------------
  390. void __fastcall TCustomWinConfiguration::SetConsoleWin(TConsoleWinConfiguration value)
  391. {
  392. SET_CONFIG_PROPERTY(ConsoleWin);
  393. }
  394. //---------------------------------------------------------------------------
  395. void __fastcall TCustomWinConfiguration::SetConfirmExitOnCompletion(bool value)
  396. {
  397. SET_CONFIG_PROPERTY(ConfirmExitOnCompletion);
  398. }