00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
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
00024
00025 CATPrefs::CATPrefs(const CATString& prefFile)
00026 {
00027 fRootNode = 0;
00028 fPrefFile = prefFile;
00029 fRootNode = new CATXMLObject(L"Preferences");
00030 }
00031
00032
00033
00034
00035 CATPrefs::~CATPrefs()
00036 {
00037 Clear();
00038 }
00039
00040
00041
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
00058
00059
00060
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
00086 bool CATPrefs::SetPref( const CATString& prefSection,
00087 const CATString& prefName,
00088 const CATString& prefValue)
00089 {
00090
00091 CATResult result = CAT_SUCCESS;
00092
00093
00094 CATString val = prefValue;
00095 val.Trim();
00096
00097
00098 CATXMLObject* curObj = FindPref(prefSection,prefName);
00099
00100
00101 if (curObj == 0)
00102 {
00103
00104 CATXMLObject* parentObj = FindSection(prefSection);
00105
00106 if (parentObj == 0)
00107 {
00108
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
00118 this->fPrefLock.Release();
00119 return true;
00120 }
00121
00122
00123 if (CATSUCCEEDED(result = curObj->AddAttribute(L"Value",val)))
00124 {
00125 this->fPrefLock.Release();
00126 return true;
00127 }
00128
00129
00130 this->fPrefLock.Release();
00131 throw(result);
00132
00133 return false;
00134 }
00135
00136
00137
00138
00139
00140 CATResult CATPrefs::Import(const CATString& path)
00141 {
00142 fPrefLock.Wait();
00143
00144
00145
00146
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
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
00177 this->Clear();
00178
00179 this->fRootNode = new CATXMLObject(L"Preferences");
00180
00181
00182 this->fPrefLock.Release();
00183 return result;;
00184 }
00185
00186
00187
00188
00189
00190
00191
00192 CATXMLObject* CATPrefs::FindPref(const CATString& prefSection, const CATString& prefName)
00193 {
00194 CATXMLObject* curObj = 0;
00195 CATXMLObject* curSection = this->FindSection(prefSection);
00196
00197
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
00215 return 0;
00216 }
00217
00218
00219
00220
00221
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
00239 return 0;
00240 }
00241
00242
00243 CATResult CATPrefs::Save()
00244 {
00245
00246 return CAT_SUCCESS;
00247 }
00248
00249 CATResult CATPrefs::Load()
00250 {
00251
00252 return CAT_SUCCESS;
00253 }
00254
00255
00256 CATResult CATPrefs::Export(const CATString& path)
00257 {
00258 fPrefLock.Wait();
00259
00260
00261
00262
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