00001 //--------------------------------------------------------------------------- 00002 /// \file CATPrefs.h 00003 /// \brief Preference storage class 00004 /// \ingroup CATGUI 00005 /// 00006 /// Copyright (c) 2003-2008 by Michael Ellison. 00007 /// See COPYING.txt for the \ref gaslicense License (MIT License). 00008 /// 00009 // $Author: mikeellison $ 00010 // $Date: 2008-01-23 01:43:25 -0600 (Wed, 23 Jan 2008) $ 00011 // $Revision: $ 00012 // $NoKeywords: $ 00013 // 00014 //--------------------------------------------------------------------------- 00015 #ifndef CATPrefs_H_ 00016 #define CATPrefs_H_ 00017 00018 #include "CATInternal.h" 00019 #include "CATString.h" 00020 #include "CATXMLObject.h" 00021 #include "CATMutex.h" 00022 00023 /// \class CATPrefs CATPrefs.h 00024 /// \brief Preference storage class 00025 /// \ingroup CATGUI 00026 /// 00027 /// Changing this up a bit... making the interface more neutral for various 00028 /// types of storage. Probably moving to registry for prefs on Windows for 00029 /// now. 00030 class CATPrefs 00031 { 00032 public: 00033 /// CATPrefs constructor 00034 /// 00035 /// \param appName - name of prefs set (registry key/filepath/etc) 00036 /// If no none is given then load/save have no effect (runtime prefs) 00037 CATPrefs(const CATString& appName = ""); 00038 00039 virtual ~CATPrefs(); 00040 00041 // Note: the GetPref / Save pref simply get and save from and to memory. 00042 // you must call Load/Save to persist settings. 00043 // To revert, just call Load() without first calling Save(). 00044 00045 /// GetPref() retrieves the value of the requested preference. 00046 /// 00047 /// \param prefSection - section name of preference (window name, etc.) 00048 /// \param prefName - name of preference 00049 /// \param prefValue - ref to value of preference (several overloaded types) 00050 /// \return bool - true if the preference existed, false otherwise. 00051 /// If not found, prefValue remains unchanged. 00052 /// \sa SetPref() 00053 bool GetPref(const CATString& prefSection, const CATString& prefName, CATString& prefValue); 00054 00055 template<class T> 00056 bool GetPref(const CATString& prefSection, const CATString& prefName, T& prefValue) 00057 { 00058 CATString prefString; 00059 if (GetPref(prefSection, prefName, prefString)) 00060 { 00061 // CATString performs conversion automatically. 00062 prefValue = (T)prefString; 00063 return true; 00064 } 00065 00066 return false; 00067 } 00068 00069 /// SetPref() sets the value of the specified preference. 00070 /// 00071 /// \param prefSection - section name of preference (window name, etc.) 00072 /// \param prefName - name of preference 00073 /// \param prefValue - value of preference (several overloaded types) 00074 /// \return bool - true on success. 00075 /// \sa SetPref() 00076 bool SetPref(const CATString& prefSection, const CATString& prefName, const CATString& prefValue); 00077 00078 template<class T> 00079 bool SetPref(const CATString& prefSection, const CATString& prefName, const T prefValue) 00080 { 00081 CATString prefString(prefValue); 00082 return this->SetPref(prefSection,prefName,prefString); 00083 } 00084 00085 /// Save() save prefs to registry/file 00086 virtual CATResult Save(); 00087 00088 /// Load() loads prefs from registry/file - WARNING: clears previous prefs! 00089 virtual CATResult Load(); 00090 00091 /// Export() saves prefs to specified file location. 00092 virtual CATResult Export(const CATString& path); 00093 00094 /// Import() loads prefs from a specified file location 00095 virtual CATResult Import(const CATString& path); 00096 00097 00098 // Clear() resets all preferences (in memory). 00099 void Clear(); 00100 00101 /// FindSection returns the XML object for the specified preference 00102 /// \return CATXMLObject* - preference object or 0 if not found. 00103 CATXMLObject* FindPref(const CATString& prefSection, const CATString& prefName); 00104 00105 /// FindSection returns the XML object for the preference section 00106 /// \return CATXMLObject* - section object or 0 if not found. 00107 CATXMLObject* FindSection(const CATString& prefSection); 00108 00109 private: 00110 CATString fPrefFile; // Preferences filename 00111 CATXMLObject* fRootNode; // Root XML object for sections. 00112 CATMutex fPrefLock; // Mutex for threadsafing. 00113 }; 00114 00115 #endif // CATPrefs_H_