00001 // $Id: oilprefs.cpp 1466 2006-07-18 16:24:30Z alex $ 00002 /* @@tag:xara-cn@@ DO NOT MODIFY THIS LINE 00003 ================================XARAHEADERSTART=========================== 00004 00005 Xara LX, a vector drawing and manipulation program. 00006 Copyright (C) 1993-2006 Xara Group Ltd. 00007 Copyright on certain contributions may be held in joint with their 00008 respective authors. See AUTHORS file for details. 00009 00010 LICENSE TO USE AND MODIFY SOFTWARE 00011 ---------------------------------- 00012 00013 This file is part of Xara LX. 00014 00015 Xara LX is free software; you can redistribute it and/or modify it 00016 under the terms of the GNU General Public License version 2 as published 00017 by the Free Software Foundation. 00018 00019 Xara LX and its component source files are distributed in the hope 00020 that it will be useful, but WITHOUT ANY WARRANTY; without even the 00021 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 00022 See the GNU General Public License for more details. 00023 00024 You should have received a copy of the GNU General Public License along 00025 with Xara LX (see the file GPL in the root directory of the 00026 distribution); if not, write to the Free Software Foundation, Inc., 51 00027 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00028 00029 00030 ADDITIONAL RIGHTS 00031 ----------------- 00032 00033 Conditional upon your continuing compliance with the GNU General Public 00034 License described above, Xara Group Ltd grants to you certain additional 00035 rights. 00036 00037 The additional rights are to use, modify, and distribute the software 00038 together with the wxWidgets library, the wxXtra library, and the "CDraw" 00039 library and any other such library that any version of Xara LX relased 00040 by Xara Group Ltd requires in order to compile and execute, including 00041 the static linking of that library to XaraLX. In the case of the 00042 "CDraw" library, you may satisfy obligation under the GNU General Public 00043 License to provide source code by providing a binary copy of the library 00044 concerned and a copy of the license accompanying it. 00045 00046 Nothing in this section restricts any of the rights you have under 00047 the GNU General Public License. 00048 00049 00050 SCOPE OF LICENSE 00051 ---------------- 00052 00053 This license applies to this program (XaraLX) and its constituent source 00054 files only, and does not necessarily apply to other Xara products which may 00055 in part share the same code base, and are subject to their own licensing 00056 terms. 00057 00058 This license does not apply to files in the wxXtra directory, which 00059 are built into a separate library, and are subject to the wxWindows 00060 license contained within that directory in the file "WXXTRA-LICENSE". 00061 00062 This license does not apply to the binary libraries (if any) within 00063 the "libs" directory, which are subject to a separate license contained 00064 within that directory in the file "LIBS-LICENSE". 00065 00066 00067 ARRANGEMENTS FOR CONTRIBUTION OF MODIFICATIONS 00068 ---------------------------------------------- 00069 00070 Subject to the terms of the GNU Public License (see above), you are 00071 free to do whatever you like with your modifications. However, you may 00072 (at your option) wish contribute them to Xara's source tree. You can 00073 find details of how to do this at: 00074 http://www.xaraxtreme.org/developers/ 00075 00076 Prior to contributing your modifications, you will need to complete our 00077 contributor agreement. This can be found at: 00078 http://www.xaraxtreme.org/developers/contribute/ 00079 00080 Please note that Xara will not accept modifications which modify any of 00081 the text between the start and end of this header (marked 00082 XARAHEADERSTART and XARAHEADEREND). 00083 00084 00085 MARKS 00086 ----- 00087 00088 Xara, Xara LX, Xara X, Xara X/Xtreme, Xara Xtreme, the Xtreme and Xara 00089 designs are registered or unregistered trademarks, design-marks, and/or 00090 service marks of Xara Group Ltd. All rights in these marks are reserved. 00091 00092 00093 Xara Group Ltd, Gaddesden Place, Hemel Hempstead, HP2 6EX, UK. 00094 http://www.xara.com/ 00095 00096 =================================XARAHEADEREND============================ 00097 */ 00098 00099 // This code handles the interface between the kernel preference system, and 00100 // the platform's own particular method of storing preferences. 00101 00102 00103 #include "camtypes.h" 00104 00105 //#include "ensure.h" - in camtypes.h [AUTOMATICALLY REMOVED] 00106 //#include "errors.h" - in camtypes.h [AUTOMATICALLY REMOVED] 00107 00108 #include "oilprefs.h" 00109 00110 #include "product.h" // PRODUCT_OPTIONS_REGISTRYKEY 00111 00112 00113 /******************************************************************************************** 00114 00115 > static OILPreferences* OILPreferences::Init() 00116 00117 Author: Luke_Hart & Phil_Martin (Xara Group Ltd) <camelotdev@xara.com> 00118 Created: 8/Jun/2006 00119 Returns: OILPreferences objects if initialisation succeeded, NULL otherwise. 00120 Purpose: Performs any initialisation of the OIL layer preferences mechanisms that 00121 may fail. 00122 00123 ********************************************************************************************/ 00124 00125 OILPreferences* OILPreferences::Init() 00126 { 00127 wxStandardPaths Paths; 00128 wxString strPath( Paths.GetUserConfigDir() ); 00129 strPath += _T("/.xaralx"); 00130 00131 // Delete any file that exists where the directory should be 00132 if (wxFile::Exists(strPath)) 00133 ::wxRemoveFile(strPath); 00134 00135 // Create directory iff not exist 00136 if( !wxDir::Exists( strPath ) ) 00137 ::wxMkdir( strPath ); 00138 00139 TRACEUSER( "jlh92", _T("OILPreferences::Init %s\n"), PCTSTR(strPath) ); 00140 00141 // Open config storage 00142 strPath += _T("/preferences"); 00143 OILPreferences* pPrefs = new OILPreferences(_T("xaralx"), _T("Xara"), strPath); 00144 00145 TRACEUSER( "jlh92", _T("OILPreferences::Init2 %s\n"), PCTSTR(strPath) ); 00146 00147 // Return the object we made or a NULL pointer if we failed 00148 return pPrefs; 00149 } 00150 00151 /******************************************************************************************** 00152 00153 > void OILPreferences::WipePreferenceFile() 00154 00155 Author: Tim_Browse (Xara Group Ltd) <camelotdev@xara.com> 00156 Created: 24/11/93 00157 Purpose: Remove all entries from a preference file. This is done by deleting the 00158 file and creating a new one. 00159 00160 ********************************************************************************************/ 00161 00162 void OILPreferences::WipePreferenceFile() 00163 { 00164 DeleteAll(); 00165 } 00166 00167 /******************************************************************************************** 00168 00169 > void OILPreferences::WipeDangerousPrefs() 00170 00171 Author: Chris_Gallimore (Xara Group Ltd) <camelotdev@xara.com> 00172 Created: 8/2/2001 00173 Purpose: Remove potentially dangerous entries from the registry. 00174 00175 ********************************************************************************************/ 00176 00177 void OILPreferences::WipeDangerousPrefs() 00178 { 00179 // wipe the potentially dangerous settings from the registry 00180 DeleteGroup(_T("Gallery")); 00181 DeleteGroup(_T("Options/Templates")); 00182 DeleteGroup(_T("Options/NewTemplates")); 00183 00184 // DeleteRegKeyAndSubKeys (hAppStateRegKey, PRODUCT_REGISTRYKEY_GALLERY); 00185 // HKEY optsKey = OpenRegKey(hAppStateRegKey, PRODUCT_REGISTRYKEY_OPTIONS); 00186 // DeleteRegKeyAndSubKeys (optsKey, TEXT ("Templates")); 00187 // DeleteRegKeyAndSubKeys (optsKey, TEXT ("NewTemplates")); 00188 // CloseRegKey (optsKey); 00189 } 00190 00191 /******************************************************************************************** 00192 00193 > void OILPreferences::Write(LPTCHAR Section, LPTCHAR PrefName, PreferenceType Type, 00194 PrefData Data) 00195 00196 Author: Tim_Browse (Xara Group Ltd) <camelotdev@xara.com> 00197 Created: 24/8/93 00198 Inputs: Section - Name of the section to write the preference into. 00199 PrefName - Name of the preference itself. 00200 Type - Type of the preference (INT32, UINT32, etc.) 00201 Data - Union holding the pointer to the data to write out to the 00202 preference file. 00203 Purpose: This function writes a given preference out to the application's own 00204 initialisation (profile/preference) file. 00205 SeeAlso: OILPreferences; OILPreferences::Read 00206 00207 ********************************************************************************************/ 00208 00209 void OILPreferences::Write(LPTCHAR Section, LPTCHAR PrefName, PreferenceType Type, 00210 PrefData Data) 00211 { 00212 wxString strKey = _T("/Options/") + wxString(Section) + _T("/") + wxString(PrefName); 00213 00214 BOOL Worked=FALSE; 00215 00216 switch (Type) 00217 { 00218 case PREF_INT: 00219 { 00220 /*TYPENOTE: Correct*/ long l = (long)(*Data.pInt); 00221 Worked = wxConfig::Write(strKey, l); 00222 break; 00223 } 00224 00225 case PREF_UINT: 00226 { 00227 // Note that unsigned cast to signed here will appear to store the wrong value 00228 // in the Config store but this apparent mistake will be rectified when the 00229 // value is read in and cast back to UINT32 again. 00230 // (Could write UINT32 prefs as strings...) 00231 // 00232 /*TYPENOTE: Correct*/ long l = (long)(*Data.pUInt); 00233 Worked = wxConfig::Write(strKey, l); 00234 break; 00235 } 00236 00237 case PREF_DOUBLE: 00238 Worked = wxConfig::Write(strKey, (double)*(Data.pDouble)); 00239 break; 00240 00241 case PREF_STRING: 00242 Worked = wxConfig::Write(strKey, wxString((TCHAR*)*(Data.pString))); 00243 // Worked = wxConfig::Write(strKey, *(Data.pString)); // use this form when StringBase derived classes support direct conversion 00244 break; 00245 00246 default: 00247 ENSURE(FALSE, "OILPreferences::Write() called with an illegal preference type"); 00248 break; 00249 } 00250 00251 if (!Worked) 00252 { 00253 ERROR3 ("Error saving preference - See trace message for details"); 00254 TRACE( _T("Error saving preference '%s'\n"), PrefName); 00255 } 00256 } 00257 00258 /******************************************************************************************** 00259 00260 > void OILPreferences::Read(LPTCHAR Section, LPTCHAR PrefName, 00261 PreferenceType Type, PrefData pData) 00262 00263 Author: Tim_Browse (Xara Group Ltd) <camelotdev@xara.com> 00264 Created: 24/8/93 00265 Inputs: Section - The name of the preferences section to search. 00266 PrefName - The name of the preference to search for. 00267 Type - The data-type of the preference. 00268 pData - Pointer to the location in which to put the value read from the 00269 preference file. 00270 MaxSize - If the preference is a string, the size of the buffer that pData 00271 points to. 00272 Outputs: pData - the data pointed to by pData is updated from the preference file (if 00273 the preference was found - otherwise it is unchanged). 00274 Returns: - 00275 Purpose: This function attempts to read the given preference in the given section 00276 from the preference file. No error is raised if the preference cannot be 00277 found, because this is a perfectly valid occurence - e.g. when a tool is 00278 being used for the first time, it has no preferences in the preference 00279 file. 00280 Errors: - 00281 SeeAlso: - 00282 00283 ********************************************************************************************/ 00284 00285 /******************************************************************************************** 00286 00287 TECHNICAL NOTES 00288 00289 This function does not use the MFC GetProfileString() function becuase this returns a CString 00290 and we're trying to avoid these in Camelot. It uses the standard SDK function instead. 00291 00292 ********************************************************************************************/ 00293 00294 void OILPreferences::Read(LPTCHAR Section, LPTCHAR PrefName, 00295 PreferenceType Type, PrefData pData) 00296 { 00297 wxString strKey = _T("/Options/") + wxString(Section) + _T("/") + wxString(PrefName); 00298 00299 switch (Type) 00300 { 00301 case PREF_INT: 00302 { 00303 // Use the value already in pData->Int as the value to return if the 00304 // preference is not found. 00305 /*TYPENOTE: Correct*/ long l; 00306 if (wxConfig::Read(strKey, &l)) 00307 *pData.pInt = (INT32)l; // Do not write directly as may be longer than 32 bits 00308 break; 00309 } 00310 00311 case PREF_UINT: 00312 { 00313 // Note that signed value is read and cast directly into Unsigned memory 00314 // allocation reversing the effects fo the cast used in Write above... 00315 /*TYPENOTE: Correct*/ long l; 00316 if (wxConfig::Read(strKey, &l)) 00317 *pData.pUInt = (UINT32)l; // Do not write directly as may be longer than 32 bits 00318 break; 00319 } 00320 case PREF_DOUBLE: 00321 { 00322 // Get the textual version of the double and convert it to a double. 00323 // default to null string 00324 wxConfig::Read(strKey, (double*)(pData.pDouble)); 00325 break; 00326 } 00327 case PREF_STRING: 00328 { 00329 // Just get the string - need to ask for the address of the String's 00330 // text buffer so we can pass it to the SDK profile API. 00331 wxString str; 00332 if (wxConfig::Read(strKey, &str)) 00333 { 00334 str.Truncate(256); 00335 *(pData.pString) = (LPCTSTR)str; 00336 // *(pData.pString) = String_256(str); // use this form when StringBase derived classes support direct conversion 00337 } 00338 break; 00339 } 00340 00341 default: 00342 ENSURE(FALSE, "OILPreferences::Read() called with an illegal preference type"); 00343 break; 00344 } 00345 } 00346 00347 /******************************************************************************************** 00348 00349 > BOOL OILPreferences::OpenInput() 00350 00351 Author: Tim_Browse (Xara Group Ltd) <camelotdev@xara.com> 00352 Created: 24/8/93 00353 Returns: TRUE if the INT32 file was successfully opened, FALSE otherwise. 00354 Purpose: Prepares the preference system for reading in preferences. 00355 00356 ********************************************************************************************/ 00357 00358 BOOL OILPreferences::OpenInput() 00359 { 00360 // Tell wxWidgets this is the default wxConfig object 00361 wxConfig::Set(this); 00362 00363 return TRUE; 00364 } 00365 00366 /******************************************************************************************** 00367 00368 > BOOL OILPreferences::CloseInput() 00369 00370 Author: Tim_Browse (Xara Group Ltd) <camelotdev@xara.com> 00371 Created: 24/8/93 00372 Returns: TRUE if the input file was successfully closed, FALSE otherwise. 00373 Purpose: Performs clean-up necessary when the preferences file is no longer needed 00374 for reading. 00375 00376 ********************************************************************************************/ 00377 00378 BOOL OILPreferences::CloseInput() 00379 { 00380 wxConfig::Set(NULL); 00381 00382 return TRUE; 00383 } 00384 00385 00386 /******************************************************************************************** 00387 00388 > BOOL OILPreferences::OpenOutput() 00389 00390 Author: Tim_Browse (Xara Group Ltd) <camelotdev@xara.com> 00391 Created: 24/8/93 00392 Returns: TRUE if the output file was successfully opened, FALSE otherwise. 00393 Purpose: Prepares the OIL for writing preferences out to the preferences file. 00394 00395 ********************************************************************************************/ 00396 00397 BOOL OILPreferences::OpenOutput() 00398 { 00399 return TRUE; 00400 } 00401 00402 /******************************************************************************************** 00403 00404 > BOOL OILPreferences::CloseOutput() 00405 00406 Author: Tim_Browse (Xara Group Ltd) <camelotdev@xara.com> 00407 Created: 24/8/93 00408 Returns: TRUE if the output file was successfully closeed, FALSE otherwise. 00409 Purpose: Closes the preference file when all the preferences have been written. 00410 00411 ********************************************************************************************/ 00412 00413 BOOL OILPreferences::CloseOutput() 00414 { 00415 Flush(); 00416 return TRUE; 00417 }