Game Accessibility Library logo SourceForge.net Logo
Game Accessibility Suite: CATGUI/CATPrefs.cpp Source File

CATPrefs.cpp

Go to the documentation of this file.
00001 //---------------------------------------------------------------------------
00002 /// \file CATPrefs.cpp
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-27 21:24:38 -0600 (Sun, 27 Jan 2008) $
00011 // $Revision:   $
00012 // $NoKeywords: $
00013 //---------------------------------------------------------------------------
00014 
00015 #include "CATPrefs.h"
00016 #include "CATXMLFactory.h"
00017 #include "CATXMLParser.h"
00018 #include "CATFileSystem.h"
00019 #include "CATApp.h"
00020 #include "CATEventDefs.h"
00021 
00022 //---------------------------------------------------------------------------
00023 // Constructor - just store the prefs name
00024 //---------------------------------------------------------------------------
00025 CATPrefs::CATPrefs(const CATString& prefFile)
00026 {
00027     fRootNode = 0;
00028     fPrefFile = prefFile;
00029     fRootNode = new CATXMLObject(L"Preferences");
00030 }
00031 
00032 //---------------------------------------------------------------------------
00033 // Destructor - clear prefs lists
00034 //---------------------------------------------------------------------------
00035 CATPrefs::~CATPrefs()
00036 {   
00037     Clear();   
00038 }
00039 
00040 //---------------------------------------------------------------------------
00041 // Clear prefs - clean up any prefs in our lists 
00042 //---------------------------------------------------------------------------
00043 void CATPrefs::Clear()
00044 {
00045     this->fPrefLock.Wait();
00046 
00047     if (fRootNode)
00048     {
00049         delete fRootNode;
00050         fRootNode = 0;
00051     }
00052 
00053     this->fPrefLock.Release();
00054 }
00055 
00056 //---------------------------------------------------------------------------
00057 // Note: the GetPref / Save pref simply get and save from and to memory.
00058 //       you must call Load/Save to access the actual file.  
00059 //       To revert, call load again w/o saving
00060 // Retrieve the preference of whatever type
00061 //---------------------------------------------------------------------------
00062 bool CATPrefs::GetPref(  const CATString& prefSection, 
00063                          const CATString& prefName, 
00064                          CATString&       prefValue)
00065 {
00066     this->fPrefLock.Wait();
00067 
00068     CATXMLObject* curObj = FindPref(prefSection,prefName); 
00069 
00070     if (curObj == 0)
00071     {
00072         this->fPrefLock.Release();
00073         return false;
00074     }
00075 
00076     prefValue = curObj->GetAttribute(L"Value");
00077     prefValue.Trim();
00078 
00079     this->fPrefLock.Release();
00080     return true;
00081 }
00082 
00083 
00084 //---------------------------------------------------------------------------
00085 // Set the specified preference with whatever type
00086 bool CATPrefs::SetPref(  const CATString& prefSection, 
00087                          const CATString& prefName, 
00088                          const CATString& prefValue)
00089 {
00090 
00091     CATResult result = CAT_SUCCESS;
00092 
00093     // Trim incoming pref value.
00094     CATString val = prefValue;
00095     val.Trim();
00096 
00097     // Find the preference object if it exists...
00098     CATXMLObject* curObj = FindPref(prefSection,prefName); 
00099 
00100     // Check if we got it..
00101     if (curObj == 0)
00102     {
00103         // Need to add object to section
00104         CATXMLObject* parentObj = FindSection(prefSection);
00105         // Check if we need to add the section as well....
00106         if (parentObj == 0)
00107         {
00108             // Need to add the section too....
00109             parentObj = new CATXMLObject(prefSection);
00110             fRootNode->AddChild(parentObj);
00111         }
00112 
00113         curObj = new CATXMLObject(prefName);
00114         curObj->AddAttribute(L"Value",val);
00115         parentObj->AddChild(curObj);
00116 
00117         // All done, pref added.
00118         this->fPrefLock.Release();
00119         return true;
00120     }   
00121 
00122     // Pref already there - just set the value.
00123     if (CATSUCCEEDED(result = curObj->AddAttribute(L"Value",val)))
00124     {
00125         this->fPrefLock.Release();      
00126         return true;
00127     }
00128 
00129     // Error setting the value? bizarre... throw error.
00130     this->fPrefLock.Release();
00131     throw(result);
00132 
00133     return false;
00134 }
00135 
00136 
00137 //---------------------------------------------------------------------------
00138 // Load() loads the preferences from the prefs file
00139 //---------------------------------------------------------------------------
00140 CATResult CATPrefs::Import(const CATString& path)
00141 {
00142     fPrefLock.Wait();
00143     // We're using pretty simple XML here, so no overriding is currently needed.
00144     // The root level elements are sections, the children are preferences.
00145     // Values for the preferences are kept in the "Value" attribute of the
00146     // preference entries.
00147     CATResult result = CAT_SUCCESS;
00148 
00149     CATXMLFactory factory;
00150 
00151     CATFileSystem* fs = gApp->GetGlobalFileSystem();
00152     if (fs == 0)
00153     {
00154         this->fPrefLock.Release();
00155         return CATRESULT(CAT_ERR_PREFS_NO_FILESYSTEM);
00156     }
00157 
00158     this->Clear();
00159 
00160     // If the file exists, then load the prefs.
00161     if (CATSUCCEEDED(result = fs->FileExists(path)))
00162     {      
00163         if (CATSUCCEEDED(result = CATXMLParser::Parse(   path, 
00164                                                          &factory, 
00165                                                          this->fRootNode)))
00166         {
00167             this->fPrefLock.Release();
00168             return result;
00169         }
00170         else
00171         {
00172             gApp->DisplayError(result);
00173         }
00174     }
00175 
00176     // An error occurred - clear the tree, set the root node
00177     this->Clear();
00178 
00179     this->fRootNode = new CATXMLObject(L"Preferences");
00180 
00181     // An error occurred - return it.
00182     this->fPrefLock.Release();
00183     return result;;
00184 }
00185 
00186 
00187 //---------------------------------------------------------------------------
00188 // FindSection returns the XML object for the specified preference
00189 //
00190 // \return CATXMLObject* - preference object or 0 if not found.
00191 //---------------------------------------------------------------------------
00192 CATXMLObject* CATPrefs::FindPref(const CATString& prefSection, const CATString& prefName)
00193 {
00194     CATXMLObject* curObj = 0;
00195     CATXMLObject* curSection = this->FindSection(prefSection);
00196 
00197     // Section doesn't exist.  Pref certainly doesn't.
00198     if (curSection == 0)
00199     {
00200         return curObj;
00201     }
00202 
00203     CATUInt32 i;
00204     CATUInt32 numChildren = curSection->GetNumChildren();
00205     for (i = 0; i < numChildren; i++)
00206     {
00207         curObj = curSection->GetChild(i);
00208         if (0 == wcscmp(curObj->GetType(),prefName))
00209         {
00210             return curObj;
00211         }
00212     }
00213 
00214     // Didn't find it.
00215     return 0;
00216 }
00217 
00218 //---------------------------------------------------------------------------
00219 // FindSection returns the XML object for the preference section
00220 //
00221 // \return CATXMLObject* - section object or 0 if not found.
00222 //---------------------------------------------------------------------------
00223 CATXMLObject* CATPrefs::FindSection(const CATString& prefSection)
00224 {
00225     CATXMLObject* curSection = 0;
00226 
00227     CATUInt32 i;
00228     CATUInt32 numChildren = fRootNode->GetNumChildren();
00229     for (i = 0; i < numChildren; i++)
00230     {
00231         curSection = fRootNode->GetChild(i);
00232         if (0 == wcscmp(curSection->GetType(),prefSection) )
00233         {
00234             return curSection;
00235         }
00236     }
00237 
00238     // Didn't find it.
00239     return 0;  
00240 }
00241 
00242 /// Save() save prefs to registry/file
00243 CATResult CATPrefs::Save()
00244 {
00245     //return Export(L"C:\\testprefs.xml");    
00246     return CAT_SUCCESS;
00247 }
00248 
00249 CATResult CATPrefs::Load()
00250 {
00251     //return Import(L"C:\\testprefs.xml");    
00252     return CAT_SUCCESS;
00253 }
00254 
00255 /// Export() saves prefs to specified file location.
00256 CATResult CATPrefs::Export(const CATString& path)
00257 {
00258     fPrefLock.Wait();
00259     // We're using pretty simple XML here, so no overriding is currently needed.
00260     // The root level elements are sections, the children are preferences.
00261     // Values for the preferences are kept in the "Value" attribute of the
00262     // preference entries.
00263     CATResult result = CAT_SUCCESS;
00264 
00265     CATXMLFactory factory;
00266 
00267     result = CATXMLParser::Write(path,fRootNode);
00268 
00269     this->fPrefLock.Release();
00270     return result;;
00271 }
00272 
00273 

Generated on Mon Feb 11 04:09:55 2008 for Game Accessibility Suite by doxygen 1.5.4