PageRenderTime 419ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/libusbK/src/inf-wizard2/src/ViewConfigSection.cpp

http://usb-travis.googlecode.com/
C++ | 879 lines | 449 code | 78 blank | 352 comment | 44 complexity | eb641b2bb4d6af042f4cb9a24dea4854 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-3.0, LGPL-2.0
  1. //------------------------------------------------------------------------
  2. // Author: Rolf Kristensen
  3. // Source: http://www.codeproject.com/KB/list/CGridListCtrlEx.aspx
  4. // License: Free to use for all (New BSD License)
  5. //------------------------------------------------------------------------
  6. #include "stdafx.h"
  7. #include "ViewConfigSection.h"
  8. //------------------------------------------------------------------------
  9. //! CViewConfigSection - Constructor
  10. //!
  11. //! @param strViewName Name to identify and persist the configuration
  12. //------------------------------------------------------------------------
  13. CViewConfigSection::CViewConfigSection(const CString& strViewName)
  14. : m_ViewName(strViewName)
  15. {
  16. }
  17. //------------------------------------------------------------------------
  18. //! CViewConfigSection - Destructor
  19. //------------------------------------------------------------------------
  20. CViewConfigSection::~CViewConfigSection()
  21. {
  22. }
  23. //------------------------------------------------------------------------
  24. //! Retrieves a setting value for the view
  25. //!
  26. //! @param strName Name of setting
  27. //! @param strDefval Default value to return if no value was found
  28. //! @return Value of the setting
  29. //------------------------------------------------------------------------
  30. CString CViewConfigSection::GetSetting(const CString& strName, const CString& strDefval) const
  31. {
  32. return ReadSetting(GetSectionName(), strName, strDefval);
  33. }
  34. //------------------------------------------------------------------------
  35. //! Updates a setting value for the view
  36. //!
  37. //! @param strName Name of setting
  38. //! @param strValue New setting value
  39. //------------------------------------------------------------------------
  40. void CViewConfigSection::SetSetting(const CString& strName, const CString& strValue)
  41. {
  42. WriteSetting(GetSectionName(), strName, strValue);
  43. }
  44. //------------------------------------------------------------------------
  45. //! Retrieves the current section name to store the settings
  46. //!
  47. //! @return Current section name
  48. //------------------------------------------------------------------------
  49. const CString& CViewConfigSection::GetSectionName() const
  50. {
  51. return m_ViewName;
  52. }
  53. //------------------------------------------------------------------------
  54. //! Removes the current configuration
  55. //------------------------------------------------------------------------
  56. void CViewConfigSection::RemoveCurrentConfig()
  57. {
  58. RemoveSection(GetSectionName());
  59. }
  60. //------------------------------------------------------------------------
  61. //! Retrieves a BOOL setting value for the view
  62. //!
  63. //! @param strName Name of setting
  64. //! @param bDefval Default value to return if no value was found
  65. //! @return Value of the setting
  66. //------------------------------------------------------------------------
  67. BOOL CViewConfigSection::GetBoolSetting(const CString& strName, BOOL bDefval) const
  68. {
  69. const CString& strValue = GetSetting(strName, ConvertBoolSetting(bDefval));
  70. if (strValue == _T("TRUE"))
  71. return true;
  72. else if (strValue == _T("FALSE"))
  73. return false;
  74. else
  75. return bDefval;
  76. }
  77. //------------------------------------------------------------------------
  78. //! Converts a BOOL setting to a string value
  79. //!
  80. //! @param bValue The setting value
  81. //! @return The setting value as string
  82. //------------------------------------------------------------------------
  83. CString CViewConfigSection::ConvertBoolSetting(BOOL bValue) const
  84. {
  85. return bValue ? _T("TRUE") : _T("FALSE");
  86. }
  87. //------------------------------------------------------------------------
  88. //! Updates the value of a BOOL setting
  89. //!
  90. //! @param strName The setting name
  91. //! @param bValue The new setting value
  92. //------------------------------------------------------------------------
  93. void CViewConfigSection::SetBoolSetting(const CString& strName, BOOL bValue)
  94. {
  95. SetSetting(strName, ConvertBoolSetting(bValue));
  96. }
  97. //------------------------------------------------------------------------
  98. //! Retrieves an integer setting value for the view
  99. //!
  100. //! @param strName Name of setting
  101. //! @param nDefval Default value to return if no value was found
  102. //! @return Value of the setting
  103. //------------------------------------------------------------------------
  104. int CViewConfigSection::GetIntSetting(const CString& strName, int nDefval) const
  105. {
  106. const CString& value = GetSetting(strName, ConvertIntSetting(nDefval));
  107. return _ttoi(value);
  108. }
  109. //------------------------------------------------------------------------
  110. //! Converts an integer setting to a string value
  111. //!
  112. //! @param nValue The setting value
  113. //! @return The setting value as string
  114. //------------------------------------------------------------------------
  115. CString CViewConfigSection::ConvertIntSetting(int nValue) const
  116. {
  117. CString strValue;
  118. strValue.Format(_T("%d"), nValue);
  119. return strValue;
  120. }
  121. //------------------------------------------------------------------------
  122. //! Updates the value of an integer setting
  123. //!
  124. //! @param strName The setting name
  125. //! @param nValue The new setting value
  126. //------------------------------------------------------------------------
  127. void CViewConfigSection::SetIntSetting(const CString& strName, int nValue)
  128. {
  129. SetSetting(strName, ConvertIntSetting(nValue));
  130. }
  131. //------------------------------------------------------------------------
  132. //! Retrieves a float setting value for the view
  133. //!
  134. //! @param strName Name of setting
  135. //! @param nDefval Default value to return if no value was found
  136. //! @return Value of the setting
  137. //------------------------------------------------------------------------
  138. double CViewConfigSection::GetFloatSetting(const CString& strName, double nDefval) const
  139. {
  140. const CString& value = GetSetting(strName, ConvertFloatSetting(nDefval));
  141. #ifdef _UNICODE
  142. return wcstod(value, NULL);
  143. #else
  144. return strtod(value, NULL);
  145. #endif
  146. }
  147. //------------------------------------------------------------------------
  148. //! Converts a float setting to a string value
  149. //!
  150. //! @param nValue The setting value
  151. //! @param nDecimals The number of decimals to persist
  152. //! @return The setting value as string
  153. //------------------------------------------------------------------------
  154. CString CViewConfigSection::ConvertFloatSetting(double nValue, int nDecimals) const
  155. {
  156. CString strFormat;
  157. strFormat.Format(_T("%%.%df"), nDecimals);
  158. CString strValue;
  159. strValue.Format(strFormat, nValue);
  160. return strValue;
  161. }
  162. //------------------------------------------------------------------------
  163. //! Updates the value of a float setting
  164. //!
  165. //! @param strName The setting name
  166. //! @param nValue The new setting value
  167. //! @param nDecimals The number of decimals to persist
  168. //------------------------------------------------------------------------
  169. void CViewConfigSection::SetFloatSetting(const CString& strName, double nValue, int nDecimals)
  170. {
  171. SetSetting(strName, ConvertFloatSetting(nValue, nDecimals));
  172. }
  173. //------------------------------------------------------------------------
  174. //! Splits a delimited string into a string-array
  175. //!
  176. //! @param strArray The delimited string
  177. //! @param values The string array
  178. //! @param strDelimiter The delimiter
  179. //------------------------------------------------------------------------
  180. void CViewConfigSection::SplitArraySetting(const CString& strArray, CSimpleArray<CString>& values, const CString& strDelimiter) const
  181. {
  182. // Perform tokenize using strDelimiter
  183. int cur_pos = 0;
  184. int prev_pos = 0;
  185. int length = strArray.GetLength();
  186. while(cur_pos < length)
  187. {
  188. cur_pos = strArray.Find(strDelimiter, prev_pos);
  189. if (cur_pos == -1)
  190. {
  191. CString value = strArray.Mid(prev_pos, length - prev_pos);
  192. values.Add(value);
  193. break;
  194. }
  195. else
  196. {
  197. CString value = strArray.Mid(prev_pos, cur_pos - prev_pos);
  198. values.Add(value);
  199. prev_pos = cur_pos + strDelimiter.GetLength();
  200. }
  201. }
  202. }
  203. //------------------------------------------------------------------------
  204. //! Retrieves a string-array setting value for the view
  205. //!
  206. //! @param strName Name of setting
  207. //! @param values String-array
  208. //! @param strDelimiter The delimiter for splitting a single string into an array
  209. //------------------------------------------------------------------------
  210. void CViewConfigSection::GetArraySetting(const CString& strName, CSimpleArray<CString>& values, const CString& strDelimiter) const
  211. {
  212. const CString& strArray = GetSetting(strName, _T(""));
  213. if (strArray.IsEmpty())
  214. return;
  215. SplitArraySetting(strArray, values, strDelimiter);
  216. }
  217. //------------------------------------------------------------------------
  218. //! Converts a string-array setting value into a delimited string
  219. //!
  220. //! @param values String-array
  221. //! @param strDelimiter The delimiter for combining the values of an array to a string
  222. //! @return The string-array combined into a single string
  223. //------------------------------------------------------------------------
  224. CString CViewConfigSection::ConvertArraySetting(const CSimpleArray<CString>& values, const CString& strDelimiter) const
  225. {
  226. CString strValue;
  227. for(int i = 0; i < values.GetSize() ; ++i)
  228. {
  229. if (!strValue.IsEmpty())
  230. strValue += strDelimiter;
  231. strValue += values[i];
  232. }
  233. return strValue;
  234. }
  235. //------------------------------------------------------------------------
  236. //! Updates the value of a string-array setting
  237. //!
  238. //! @param strName The setting name
  239. //! @param values The new string array
  240. //! @param strDelimiter The delimiter for combining the values of an array to a string
  241. //------------------------------------------------------------------------
  242. void CViewConfigSection::SetArraySetting(const CString& strName, const CSimpleArray<CString>& values, const CString& strDelimiter)
  243. {
  244. SetSetting(strName, ConvertArraySetting(values, strDelimiter));
  245. }
  246. //------------------------------------------------------------------------
  247. //! Retrieves a integer-array setting value for the view
  248. //!
  249. //! @param strName Name of setting
  250. //! @param values integer-array
  251. //! @param strDelimiter The delimiter for splitting a single string into an array
  252. //------------------------------------------------------------------------
  253. void CViewConfigSection::GetArraySetting(const CString& strName, CSimpleArray<int>& values, const CString& strDelimiter) const
  254. {
  255. CSimpleArray<CString> strArray;
  256. GetArraySetting(strName, strArray, strDelimiter);
  257. for(int i = 0 ; i < strArray.GetSize(); ++i)
  258. {
  259. int value = _ttoi(strArray[i]);
  260. values.Add(value);
  261. }
  262. }
  263. //------------------------------------------------------------------------
  264. //! Converts an integer-array setting value into a delimited string
  265. //!
  266. //! @param values Integer-array
  267. //! @param strDelimiter The delimiter for combining the values of an array to a string
  268. //! @return The string-array combined into a single string
  269. //------------------------------------------------------------------------
  270. CString CViewConfigSection::ConvertArraySetting(const CSimpleArray<int>& values, const CString& strDelimiter) const
  271. {
  272. CString strValue;
  273. CString strArray;
  274. for(int i = 0; i < values.GetSize(); ++i)
  275. {
  276. if (!strArray.IsEmpty())
  277. strArray += strDelimiter;
  278. strValue.Format( _T("%d"), values[i]);
  279. strArray += strValue;
  280. }
  281. return strArray;
  282. }
  283. //------------------------------------------------------------------------
  284. //! Updates the value of an integer-array setting
  285. //!
  286. //! @param strName The setting name
  287. //! @param values The integer array
  288. //! @param strDelimiter The delimiter for combining the values of an array to a string
  289. //------------------------------------------------------------------------
  290. void CViewConfigSection::SetArraySetting(const CString& strName, const CSimpleArray<int>& values, const CString& strDelimiter)
  291. {
  292. SetSetting(strName, ConvertArraySetting(values, strDelimiter));
  293. }
  294. //------------------------------------------------------------------------
  295. //! Retrieves a font setting value for the view
  296. //!
  297. //! @param strName Name of setting
  298. //! @return Value of the setting
  299. //------------------------------------------------------------------------
  300. LOGFONT CViewConfigSection::GetLogFontSetting(const CString& strName) const
  301. {
  302. LOGFONT font = {0};
  303. CSimpleArray<CString> strArray;
  304. GetArraySetting(strName, strArray);
  305. if (strArray.GetSize() != 13)
  306. return font;
  307. #if __STDC_WANT_SECURE_LIB__
  308. _tcscpy_s(font.lfFaceName, sizeof(font.lfFaceName) / sizeof(TCHAR), strArray[0]);
  309. #else
  310. _tcsncpy(font.lfFaceName, strArray[0], sizeof(font.lfFaceName) / sizeof(TCHAR));
  311. #endif
  312. font.lfHeight = _ttoi(strArray[1]);
  313. font.lfWidth = _ttoi(strArray[2]);
  314. font.lfEscapement = _ttoi(strArray[3]);
  315. font.lfOrientation = _ttoi(strArray[4]);
  316. font.lfWeight = _ttoi(strArray[5]);
  317. font.lfItalic = (BYTE)_ttoi(strArray[6]);
  318. font.lfUnderline = (BYTE)_ttoi(strArray[7]);
  319. font.lfStrikeOut = (BYTE)_ttoi(strArray[8]);
  320. font.lfCharSet = (BYTE)_ttoi(strArray[9]);
  321. font.lfOutPrecision = (BYTE)_ttoi(strArray[10]);
  322. font.lfQuality = (BYTE)_ttoi(strArray[11]);
  323. font.lfPitchAndFamily = (BYTE)_ttoi(strArray[12]);
  324. return font;
  325. }
  326. //------------------------------------------------------------------------
  327. //! Converts a font setting value into a delimited string
  328. //!
  329. //! @param font The setting value
  330. //! @return The delimited string
  331. //------------------------------------------------------------------------
  332. CString CViewConfigSection::ConvertLogFontSetting(const LOGFONT& font) const
  333. {
  334. CSimpleArray<CString> strArray;
  335. CString strValue(font.lfFaceName, sizeof(font.lfFaceName) / sizeof(TCHAR));
  336. strArray.Add(strValue);
  337. strValue.Format(_T("%d"), font.lfHeight);
  338. strArray.Add(strValue);
  339. strValue.Format(_T("%d"), font.lfWidth);
  340. strArray.Add(strValue);
  341. strValue.Format(_T("%d"), font.lfEscapement);
  342. strArray.Add(strValue);
  343. strValue.Format(_T("%d"), font.lfOrientation);
  344. strArray.Add(strValue);
  345. strValue.Format(_T("%d"), font.lfWeight);
  346. strArray.Add(strValue);
  347. strValue.Format(_T("%d"), font.lfItalic);
  348. strArray.Add(strValue);
  349. strValue.Format(_T("%d"), font.lfUnderline);
  350. strArray.Add(strValue);
  351. strValue.Format(_T("%d"), font.lfStrikeOut);
  352. strArray.Add(strValue);
  353. strValue.Format(_T("%d"), font.lfCharSet);
  354. strArray.Add(strValue);
  355. strValue.Format(_T("%d"), font.lfOutPrecision);
  356. strArray.Add(strValue);
  357. strValue.Format(_T("%d"), font.lfQuality);
  358. strArray.Add(strValue);
  359. strValue.Format(_T("%d"), font.lfPitchAndFamily);
  360. strArray.Add(strValue);
  361. return ConvertArraySetting(strArray);
  362. }
  363. //------------------------------------------------------------------------
  364. //! Updates the value of a font setting
  365. //!
  366. //! @param strName The setting name
  367. //! @param font The new setting value
  368. //------------------------------------------------------------------------
  369. void CViewConfigSection::SetLogFontSetting(const CString& strName, const LOGFONT& font)
  370. {
  371. SetSetting(strName, ConvertLogFontSetting(font));
  372. }
  373. //------------------------------------------------------------------------
  374. //! Retrieves a rectangle setting value for the view
  375. //!
  376. //! @param strName Name of setting
  377. //! @param rectDefval Default value to return if no value was found
  378. //! @return Value of the setting
  379. //------------------------------------------------------------------------
  380. CRect CViewConfigSection::GetRectSetting(const CString& strName, const CRect& rectDefval) const
  381. {
  382. CSimpleArray<CString> strArray;
  383. GetArraySetting(strName, strArray);
  384. if (strArray.GetSize() != 4)
  385. return rectDefval;
  386. CRect rect(0, 0, 0, 0);
  387. rect.left = _ttoi(strArray[0]);
  388. rect.top = _ttoi(strArray[1]);
  389. rect.right = _ttoi(strArray[2]);
  390. rect.bottom = _ttoi(strArray[3]);
  391. return rect;
  392. }
  393. //------------------------------------------------------------------------
  394. //! Converts a rectangle setting value into a delimited string
  395. //!
  396. //! @param rect The setting value
  397. //! @return The delimited string
  398. //------------------------------------------------------------------------
  399. CString CViewConfigSection::ConvertRectSetting(const RECT& rect) const
  400. {
  401. CSimpleArray<CString> strArray;
  402. CString strValue;
  403. strValue.Format(_T("%d"), rect.left);
  404. strArray.Add(strValue);
  405. strValue.Format(_T("%d"), rect.top);
  406. strArray.Add(strValue);
  407. strValue.Format(_T("%d"), rect.right);
  408. strArray.Add(strValue);
  409. strValue.Format(_T("%d"), rect.bottom);
  410. strArray.Add(strValue);
  411. return ConvertArraySetting(strArray);
  412. }
  413. //------------------------------------------------------------------------
  414. //! Updates the value of a rectangle setting
  415. //!
  416. //! @param strName The setting name
  417. //! @param rect The new setting value
  418. //------------------------------------------------------------------------
  419. void CViewConfigSection::SetRectSetting(const CString& strName, const RECT& rect)
  420. {
  421. SetSetting(strName, ConvertRectSetting(rect));
  422. }
  423. //------------------------------------------------------------------------
  424. //! Retrieves a color setting value for the view
  425. //!
  426. //! @param strName Name of setting
  427. //! @param colorDefval Default value to return if no value was found
  428. //! @return Value of the setting
  429. //------------------------------------------------------------------------
  430. COLORREF CViewConfigSection::GetColorSetting(const CString& strName, const COLORREF colorDefval) const
  431. {
  432. CSimpleArray<CString> strArray;
  433. GetArraySetting(strName, strArray);
  434. if (strArray.GetSize() != 3)
  435. return colorDefval;
  436. int r = _ttoi(strArray[0]);
  437. int g = _ttoi(strArray[1]);
  438. int b = _ttoi(strArray[2]);
  439. return RGB(r, g, b);
  440. }
  441. //------------------------------------------------------------------------
  442. //! Converts a color setting value into a delimited string
  443. //!
  444. //! @param color The setting value
  445. //! @return The delimited string
  446. //------------------------------------------------------------------------
  447. CString CViewConfigSection::ConvertColorSetting(COLORREF color) const
  448. {
  449. CSimpleArray<CString> strArray;
  450. CString strValue;
  451. strValue.Format(_T("%d"), GetRValue(color));
  452. strArray.Add(strValue);
  453. strValue.Format(_T("%d"), GetGValue(color));
  454. strArray.Add(strValue);
  455. strValue.Format(_T("%d"), GetBValue(color));
  456. strArray.Add(strValue);
  457. return ConvertArraySetting(strArray);
  458. }
  459. //------------------------------------------------------------------------
  460. //! Updates the value of a color setting
  461. //!
  462. //! @param strName The setting name
  463. //! @param color The new setting value
  464. //------------------------------------------------------------------------
  465. void CViewConfigSection::SetColorSetting(const CString& strName, COLORREF color)
  466. {
  467. SetSetting(strName, ConvertColorSetting(color));
  468. }
  469. //------------------------------------------------------------------------
  470. //! CViewConfigSectionLocal - Constructor
  471. //!
  472. //! @param strViewName Name to identify and persist the configuration
  473. //------------------------------------------------------------------------
  474. CViewConfigSectionDefault::CViewConfigSectionLocal::CViewConfigSectionLocal(const CString& strViewName)
  475. : CViewConfigSection(strViewName)
  476. {}
  477. //------------------------------------------------------------------------
  478. //! CViewConfigSectionLocal - Copy constructor to ensure proper
  479. //! copy of m_LocalSettings.
  480. //------------------------------------------------------------------------
  481. CViewConfigSectionDefault::CViewConfigSectionLocal::CViewConfigSectionLocal(const CViewConfigSectionDefault::CViewConfigSectionLocal& other)
  482. : CViewConfigSection(other)
  483. {
  484. *this = other;
  485. }
  486. //------------------------------------------------------------------------
  487. //! CViewConfigSectionLocal - Assignment operator to ensure proper
  488. //! assignment of m_LocalSettings.
  489. //------------------------------------------------------------------------
  490. CViewConfigSectionDefault::CViewConfigSectionLocal& CViewConfigSectionDefault::CViewConfigSectionLocal::operator=(const CViewConfigSectionDefault::CViewConfigSectionLocal& other)
  491. {
  492. if (this == &other)
  493. return *this;
  494. static_cast<CViewConfigSection&>(*this) = other;
  495. m_LocalSettings.RemoveAll();
  496. for(int i = 0; i < other.m_LocalSettings.GetSize(); ++i)
  497. m_LocalSettings.Add(other.m_LocalSettings.GetKeyAt(i), other.m_LocalSettings.GetValueAt(i));
  498. return *this;
  499. }
  500. //------------------------------------------------------------------------
  501. //! Implements an interface for reading the setting value from memory
  502. //!
  503. //! @param strSection Name of section
  504. //! @param strSetting Name of setting
  505. //! @param strDefval Default value to return if no value was found
  506. //! @return Value of the setting
  507. //------------------------------------------------------------------------
  508. CString CViewConfigSectionDefault::CViewConfigSectionLocal::ReadSetting(const CString& strSection, const CString& strSetting, const CString& strDefval) const
  509. {
  510. for(int i = 0; i < m_LocalSettings.GetSize(); ++i)
  511. if (m_LocalSettings.GetKeyAt(i) == strSetting)
  512. return m_LocalSettings.GetValueAt(i);
  513. return strDefval;
  514. }
  515. //------------------------------------------------------------------------
  516. //! Implements an interface for writing the setting value to memory
  517. //!
  518. //! @param strSection Name of section
  519. //! @param strSetting Name of setting
  520. //! @param strValue New setting value
  521. //------------------------------------------------------------------------
  522. void CViewConfigSectionDefault::CViewConfigSectionLocal::WriteSetting(const CString& strSection, const CString& strSetting, const CString& strValue)
  523. {
  524. m_LocalSettings.Add(strSetting, strValue);
  525. }
  526. //------------------------------------------------------------------------
  527. //! Implements an interface for removing the setting section from memory
  528. //!
  529. //! @param strSection Name of section
  530. //------------------------------------------------------------------------
  531. void CViewConfigSectionDefault::CViewConfigSectionLocal::RemoveSection(const CString& strSection)
  532. {
  533. m_LocalSettings.RemoveAll();
  534. }
  535. //------------------------------------------------------------------------
  536. //! Contains default configuration
  537. //!
  538. //! @return Default configuration available (true/false)
  539. //------------------------------------------------------------------------
  540. BOOL CViewConfigSectionDefault::CViewConfigSectionLocal::HasSettings() const
  541. {
  542. return m_LocalSettings.GetSize() > 0;
  543. }
  544. //------------------------------------------------------------------------
  545. //! Copy the default values to another persistence layer
  546. //!
  547. //! @param destination The other persistence layer
  548. //------------------------------------------------------------------------
  549. void CViewConfigSectionDefault::CViewConfigSectionLocal::CopySettings(CViewConfigSection& destination) const
  550. {
  551. for(int i = 0; i < m_LocalSettings.GetSize(); ++i)
  552. destination.SetSetting(m_LocalSettings.GetKeyAt(i), m_LocalSettings.GetValueAt(i));
  553. }
  554. //------------------------------------------------------------------------
  555. //! CViewConfigSectionDefault - Constructor
  556. //!
  557. //! @param strViewName Name to identify and persist the configuration
  558. //------------------------------------------------------------------------
  559. CViewConfigSectionDefault::CViewConfigSectionDefault(const CString& strViewName)
  560. : CViewConfigSection(strViewName)
  561. , m_DefaultConfig(strViewName)
  562. {}
  563. //------------------------------------------------------------------------
  564. //! Retrieves a setting value for the view. If the value is not available
  565. //! then it returns the value from the default configuration.
  566. //!
  567. //! @param strName Name of setting
  568. //! @param strDefval Default value to return if no value was found
  569. //! @return Value of the setting
  570. //------------------------------------------------------------------------
  571. CString CViewConfigSectionDefault::GetSetting(const CString& strName, const CString& strDefval) const
  572. {
  573. return CViewConfigSection::GetSetting(strName, m_DefaultConfig.GetSetting(strName, strDefval));
  574. }
  575. //------------------------------------------------------------------------
  576. //! Retrieve the in memory default configuration
  577. //!
  578. //! @return Default configuration
  579. //------------------------------------------------------------------------
  580. CViewConfigSection& CViewConfigSectionDefault::GetDefaultConfig()
  581. {
  582. return m_DefaultConfig;
  583. }
  584. //------------------------------------------------------------------------
  585. //! Contains default configuration
  586. //!
  587. //! @return Default configuration available (true/false)
  588. //------------------------------------------------------------------------
  589. BOOL CViewConfigSectionDefault::HasDefaultConfig() const
  590. {
  591. return m_DefaultConfig.HasSettings();
  592. }
  593. //------------------------------------------------------------------------
  594. //! Resets the current configuration by deleting it and restoring it
  595. //! from the in memory default configuration.
  596. //------------------------------------------------------------------------
  597. void CViewConfigSectionDefault::ResetConfigDefault()
  598. {
  599. RemoveSection(GetSectionName());
  600. m_DefaultConfig.CopySettings(*this);
  601. }
  602. //------------------------------------------------------------------------
  603. //! CViewConfigSectionProfiles - Constructor
  604. //!
  605. //! @param strViewName Name to identify and persist the configuration
  606. //------------------------------------------------------------------------
  607. CViewConfigSectionProfiles::CViewConfigSectionProfiles(const CString& strViewName)
  608. : CViewConfigSectionDefault(strViewName)
  609. {
  610. m_CurrentSection = strViewName;
  611. }
  612. //------------------------------------------------------------------------
  613. //! Splits the section name into a view-name and profile-name
  614. //!
  615. //! @param strSection Name of the section
  616. //! @param strViewName Name of the configuration
  617. //! @param strProfile Name of a profile in the configuration
  618. //------------------------------------------------------------------------
  619. void CViewConfigSectionProfiles::SplitSectionName(const CString& strSection, CString& strViewName, CString& strProfile)
  620. {
  621. int pos_profile = strSection.Find(_T("__"));
  622. if (pos_profile > 0)
  623. {
  624. strViewName = strSection.Mid(0, pos_profile);
  625. if (strViewName != m_ViewName)
  626. strViewName = m_ViewName;
  627. else
  628. strProfile = strSection.Mid(pos_profile + 2);
  629. }
  630. else
  631. {
  632. strViewName = m_ViewName;
  633. }
  634. }
  635. //------------------------------------------------------------------------
  636. //! Combines the view-name and the profile-name into a section name
  637. //!
  638. //! @param strViewName Name of the configuration
  639. //! @param strProfile Name of a profile in the configuration
  640. //! @return The section name
  641. //------------------------------------------------------------------------
  642. CString CViewConfigSectionProfiles::JoinSectionName(const CString& strViewName, const CString& strProfile) const
  643. {
  644. if (strProfile.IsEmpty())
  645. return strViewName;
  646. else
  647. return strViewName + _T("__") + strProfile;
  648. }
  649. //------------------------------------------------------------------------
  650. //! Retrieves the section name of the currently active configuration profile
  651. //!
  652. //! @return Current section name
  653. //------------------------------------------------------------------------
  654. const CString& CViewConfigSectionProfiles::GetSectionName() const
  655. {
  656. if (m_CurrentSection == m_ViewName)
  657. {
  658. CString strProfile = ReadSetting(m_ViewName, _T("ActiveProfile"), _T(""));
  659. if (strProfile.IsEmpty())
  660. {
  661. CSimpleArray<CString> profiles;
  662. GetProfiles(profiles);
  663. if (profiles.GetSize() > 0)
  664. strProfile = profiles[0];
  665. }
  666. m_CurrentSection = JoinSectionName(m_ViewName, strProfile);
  667. }
  668. return m_CurrentSection;
  669. }
  670. //------------------------------------------------------------------------
  671. //! Retrieves the list of currently available configuration profiles
  672. //!
  673. //! @param profiles List of profile names
  674. //------------------------------------------------------------------------
  675. void CViewConfigSectionProfiles::GetProfiles(CSimpleArray<CString>& profiles) const
  676. {
  677. const CString& strProfiles = ReadSetting(m_ViewName, _T("CurrentProfiles"), _T(""));
  678. SplitArraySetting(strProfiles, profiles, _T(", "));
  679. }
  680. //------------------------------------------------------------------------
  681. //! Retrieves the currently active profile
  682. //!
  683. //! @return Name of the profile
  684. //------------------------------------------------------------------------
  685. CString CViewConfigSectionProfiles::GetActiveProfile()
  686. {
  687. CString strViewName, strProfile;
  688. SplitSectionName(m_CurrentSection, strViewName, strProfile);
  689. return strProfile;
  690. }
  691. //------------------------------------------------------------------------
  692. //! Switches to a new active configuration profile
  693. //!
  694. //! @param strProfile Name of the new profile
  695. //------------------------------------------------------------------------
  696. void CViewConfigSectionProfiles::SetActiveProfile(const CString& strProfile)
  697. {
  698. // Make the new strProfile the active ones
  699. WriteSetting(m_ViewName, _T("ActiveProfile"), strProfile);
  700. m_CurrentSection = JoinSectionName(m_ViewName, strProfile);
  701. if (strProfile.IsEmpty())
  702. return;
  703. AddProfile(strProfile);
  704. }
  705. //------------------------------------------------------------------------
  706. //! Adds a profile to the official list of column configuration profiles
  707. //!
  708. //! @param strProfile Name of the profile
  709. //------------------------------------------------------------------------
  710. void CViewConfigSectionProfiles::AddProfile(const CString& strProfile)
  711. {
  712. // Add the strProfile to the list if not already there
  713. CSimpleArray<CString> profiles;
  714. GetProfiles(profiles);
  715. for(int i = 0; i < profiles.GetSize(); ++i)
  716. if (profiles[i] == strProfile)
  717. return;
  718. CString noconst(strProfile);
  719. profiles.Add(noconst);
  720. WriteSetting(m_ViewName, _T("CurrentProfiles"), ConvertArraySetting(profiles, _T(", ")));
  721. }
  722. //------------------------------------------------------------------------
  723. //! Removes a profile from the official list of column configuration profiles
  724. //!
  725. //! @param strProfile Name of the profile
  726. //------------------------------------------------------------------------
  727. void CViewConfigSectionProfiles::DeleteProfile(const CString& strProfile)
  728. {
  729. if (strProfile.IsEmpty())
  730. return;
  731. // Remove any settings
  732. RemoveSection(JoinSectionName(m_ViewName, strProfile));
  733. // Remove the strProfile from the list
  734. CSimpleArray<CString> profiles;
  735. GetProfiles(profiles);
  736. for(int i = 0; i < profiles.GetSize(); ++i)
  737. if (profiles[i] == strProfile)
  738. profiles.RemoveAt(i);
  739. WriteSetting(m_ViewName, _T("CurrentProfiles"), ConvertArraySetting(profiles, _T(", ")));
  740. }
  741. //------------------------------------------------------------------------
  742. //! Removes the current configuration
  743. //------------------------------------------------------------------------
  744. void CViewConfigSectionProfiles::RemoveCurrentConfig()
  745. {
  746. if (GetSectionName() == m_ViewName)
  747. {
  748. // Backup strProfile-settings and reset the other settings
  749. const CString& strProfiles = ReadSetting(m_ViewName, _T("CurrentProfiles"), _T(""));
  750. const CString& activeProfile = ReadSetting(m_ViewName, _T("ActiveProfile"), _T(""));
  751. CViewConfigSectionDefault::RemoveCurrentConfig();
  752. WriteSetting(m_ViewName, _T("CurrentProfiles"), strProfiles);
  753. WriteSetting(m_ViewName, _T("ActiveProfile"), activeProfile);
  754. }
  755. else
  756. {
  757. CViewConfigSectionDefault::RemoveCurrentConfig();
  758. }
  759. }
  760. //------------------------------------------------------------------------
  761. //! CViewConfigSectionWinApp - Constructor
  762. //!
  763. //! @param strViewName Name to identify and persist the configuration
  764. //------------------------------------------------------------------------
  765. CViewConfigSectionWinApp::CViewConfigSectionWinApp(const CString& strViewName)
  766. : CViewConfigSectionProfiles(strViewName)
  767. {
  768. }
  769. //------------------------------------------------------------------------
  770. //! Implements an interface for reading the setting value CWinApp profile
  771. //!
  772. //! @param strSection Name of section
  773. //! @param strSetting Name of setting
  774. //! @param strDefval Default value to return if no value was found
  775. //! @return Value of the setting
  776. //------------------------------------------------------------------------
  777. CString CViewConfigSectionWinApp::ReadSetting(const CString& strSection, const CString& strSetting, const CString& strDefval) const
  778. {
  779. return AfxGetApp()->GetProfileString(strSection, strSetting, strDefval);
  780. }
  781. //------------------------------------------------------------------------
  782. //! Implements an interface for writing the setting value to CWinApp profile
  783. //!
  784. //! @param strSection Name of section
  785. //! @param strSetting Name of setting
  786. //! @param strValue New setting value
  787. //------------------------------------------------------------------------
  788. void CViewConfigSectionWinApp::WriteSetting(const CString& strSection, const CString& strSetting, const CString& strValue)
  789. {
  790. AfxGetApp()->WriteProfileString(strSection, strSetting, strValue);
  791. }
  792. //------------------------------------------------------------------------
  793. //! Implements an interface for removing the setting section from memory
  794. //!
  795. //! @param strSection Name of section
  796. //------------------------------------------------------------------------
  797. void CViewConfigSectionWinApp::RemoveSection(const CString& strSection)
  798. {
  799. // Section is deleted when providing NULL as entry
  800. AfxGetApp()->WriteProfileString(strSection, NULL, NULL);
  801. }