Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members

appprefs.cpp

Go to the documentation of this file.
00001 /* 00002 * wxChecksums 00003 * Copyright (C) 2003-2004 Julien Couot 00004 * 00005 * This program is free software; you can redistribute it and/or 00006 * modify it under the terms of the GNU General Public License 00007 * as published by the Free Software Foundation; either version 2 00008 * of the License, or (at your option) any later version. 00009 * 00010 * This program is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 * GNU General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU General Public License 00016 * along with this program; if not, write to the Free Software 00017 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00018 */ 00019 00020 /** 00021 * \file appprefs.cpp 00022 * Common preferences for the application. 00023 */ 00024 00025 //--------------------------------------------------------------------------- 00026 // For compilers that support precompilation, includes "wx.h". 00027 #include <wx/wxprec.h> 00028 00029 #ifdef __BORLANDC__ 00030 #pragma hdrstop 00031 #endif 00032 00033 #ifndef WX_PRECOMP 00034 // Include your minimal set of headers here, or wx.h 00035 #include <wx/wx.h> 00036 #endif 00037 00038 #include <wx/file.h> 00039 #include <wx/filename.h> 00040 #include <wx/config.h> 00041 #include <wx/fileconf.h> 00042 #include <wx/tokenzr.h> 00043 #include <wx/txtstrm.h> 00044 00045 #ifdef __WINDOWS__ 00046 #include <windows.h> 00047 #include <shlobj.h> 00048 00049 #if defined(__CYGWIN__) || defined(__MINGW32__) 00050 // My version of Cygwin doesn't know these constants for the SHGetFolderPath 00051 // API call. 00052 #define SHGFP_TYPE_CURRENT 0 00053 #define SHGFP_TYPE_DEFAULT 1 00054 #endif // __CYGWIN__ 00055 #endif // __WINDOWS__ 00056 00057 #include "appprefs.hpp" 00058 #include "comdefs.hpp" 00059 #include "cmdlnopt.hpp" 00060 #include "osdep.hpp" 00061 00062 #include "compat.hpp" 00063 //--------------------------------------------------------------------------- 00064 00065 /// The C++ standard namespace. 00066 using namespace std; 00067 00068 00069 //########################################################################### 00070 // Useful fonctions 00071 //########################################################################### 00072 00073 static wxString addPathSeparatorAtEnd(wxString dirName); 00074 static bool dirExists(const wxString& dirName, bool create); 00075 #if defined(__WINDOWS__) 00076 static wxString getFolderPath(int folder); 00077 #endif // defined(__WINDOWS__) 00078 //--------------------------------------------------------------------------- 00079 00080 00081 //########################################################################### 00082 // Preference value members 00083 //########################################################################### 00084 00085 00086 /** 00087 * Default constructor. 00088 */ 00089 PreferenceValue::PreferenceValue() 00090 { 00091 _valueType = ptNotDefined; 00092 _value.valLong = NULL; 00093 } 00094 //--------------------------------------------------------------------------- 00095 00096 00097 /** 00098 * Clones the source instance in this instance. 00099 * 00100 * @param source Source instance. 00101 */ 00102 void PreferenceValue::clone(const PreferenceValue& source) 00103 { 00104 if (this != &source) 00105 { 00106 // Cleans up memory. 00107 cleanup(); 00108 00109 // Copies data from the source instance. 00110 this->_valueType = source._valueType; 00111 switch (_valueType) 00112 { 00113 case ptLong : 00114 _value.valLong = new long(*(source._value.valLong)); 00115 break; 00116 case ptString : 00117 _value.valString = new wxString(*(source._value.valString)); 00118 break; 00119 case ptDouble : 00120 _value.valDouble = new double(*(source._value.valDouble)); 00121 break; 00122 case ptBoolean : 00123 _value.valBool = new bool(*(source._value.valBool)); 00124 break; 00125 case ptRect : 00126 _value.valRect = new wxRect(*(source._value.valRect)); 00127 break; 00128 case ptPoint : 00129 _value.valPoint = new wxPoint(*(source._value.valPoint)); 00130 break; 00131 case ptSize : 00132 _value.valSize = new wxSize(*(source._value.valSize)); 00133 break; 00134 default : 00135 _value.valLong = NULL; 00136 } 00137 this->_cfgKey = source._cfgKey; 00138 } 00139 } 00140 //--------------------------------------------------------------------------- 00141 00142 00143 /** 00144 * Copy constructor. 00145 * 00146 * @param source Source instance. 00147 */ 00148 PreferenceValue::PreferenceValue(const PreferenceValue& source) 00149 { 00150 _valueType = ptNotDefined; 00151 _value.valLong = NULL; 00152 clone(source); 00153 } 00154 //--------------------------------------------------------------------------- 00155 00156 00157 /** 00158 * Assignment operator. 00159 * 00160 * @param source Source instance. 00161 * @return A reference on the instance. 00162 */ 00163 PreferenceValue& PreferenceValue::operator=(const PreferenceValue& source) 00164 { 00165 clone(source); 00166 return *this; 00167 } 00168 //--------------------------------------------------------------------------- 00169 00170 00171 /** 00172 * Constructor with a long integer type. 00173 * 00174 * @param value The value of long integer type. 00175 * @param cfgKey Corresponding key in the configuration file. Must be empty 00176 * if this item isn't stored in the configuration file. 00177 * @param initFromCfgFile If <CODE>true</CODE> and if a key was given, tries 00178 * first to initialize the value from the configuration 00179 * file. 00180 */ 00181 PreferenceValue::PreferenceValue(const long value, const wxString& cfgKey, const bool initFromCfgFile) 00182 { 00183 _valueType = ptLong; 00184 _value.valLong = new long(value); 00185 _cfgKey = cfgKey; 00186 00187 if (!cfgKey.empty() && initFromCfgFile) 00188 wxConfig::Get()->Read(cfgKey, _value.valLong); 00189 } 00190 //--------------------------------------------------------------------------- 00191 00192 00193 /** 00194 * Constructor with a string type. 00195 * 00196 * @param value The value of string type. 00197 * @param cfgKey Corresponding key in the configuration file. Must be empty 00198 * if this item isn't stored in the configuration file. 00199 * @param initFromCfgFile If <CODE>true</CODE> and if a key was given, tries 00200 * first to initialize the value from the configuration 00201 * file. */ 00202 PreferenceValue::PreferenceValue(const wxString& value, const wxString& cfgKey, const bool initFromCfgFile) 00203 { 00204 _valueType = ptString; 00205 _value.valString = new wxString(value); 00206 _cfgKey = cfgKey; 00207 00208 if (!cfgKey.empty() && initFromCfgFile) 00209 wxConfig::Get()->Read(cfgKey, _value.valString); 00210 } 00211 //--------------------------------------------------------------------------- 00212 00213 00214 /** 00215 * Constructor with a double type. 00216 * 00217 * @param value The value of double type. 00218 * @param cfgKey Corresponding key in the configuration file. Must be empty 00219 * if this item isn't stored in the configuration file. 00220 * @param initFromCfgFile If <CODE>true</CODE> and if a key was given, tries 00221 * first to initialize the value from the configuration 00222 * file. */ 00223 PreferenceValue::PreferenceValue(const double value, const wxString& cfgKey, const bool initFromCfgFile) 00224 { 00225 _valueType = ptDouble; 00226 _value.valDouble = new double(value); 00227 _cfgKey = cfgKey; 00228 00229 if (!cfgKey.empty() && initFromCfgFile) 00230 wxConfig::Get()->Read(cfgKey, _value.valDouble); 00231 } 00232 //--------------------------------------------------------------------------- 00233 00234 00235 /** 00236 * Constructor with a boolean type. 00237 * 00238 * @param value The value of boolean type. 00239 * @param cfgKey Corresponding key in the configuration file. Must be empty 00240 * if this item isn't stored in the configuration file. 00241 * @param initFromCfgFile If <CODE>true</CODE> and if a key was given, tries 00242 * first to initialize the value from the configuration 00243 * file. */ 00244 PreferenceValue::PreferenceValue(const bool value, const wxString& cfgKey, const bool initFromCfgFile) 00245 { 00246 _valueType = ptBoolean; 00247 _value.valBool = new bool(value); 00248 _cfgKey = cfgKey; 00249 00250 if (!cfgKey.empty() && initFromCfgFile) 00251 wxConfig::Get()->Read(cfgKey, _value.valBool); 00252 } 00253 //--------------------------------------------------------------------------- 00254 00255 00256 /** 00257 * Constructor with a wxRect type. 00258 * 00259 * @param value The value of wxRect type. 00260 * @param cfgKey Corresponding key in the configuration file. Must be empty 00261 * if this item isn't stored in the configuration file. 00262 * @param initFromCfgFile If <CODE>true</CODE> and if a key was given, tries 00263 * first to initialize the value from the configuration 00264 * file. */ 00265 PreferenceValue::PreferenceValue(const wxRect& value, const wxString& cfgKey, const bool initFromCfgFile) 00266 { 00267 _valueType = ptRect; 00268 _value.valRect = new wxRect(value); 00269 _cfgKey = cfgKey; 00270 00271 if (!cfgKey.empty() && initFromCfgFile) 00272 read(cfgKey, _value.valRect); 00273 } 00274 //--------------------------------------------------------------------------- 00275 00276 00277 /** 00278 * Constructor with a wxPoint type. 00279 * 00280 * @param value The value of wxPoint type. 00281 * @param cfgKey Corresponding key in the configuration file. Must be empty 00282 * if this item isn't stored in the configuration file. 00283 * @param initFromCfgFile If <CODE>true</CODE> and if a key was given, tries 00284 * first to initialize the value from the configuration 00285 * file. */ 00286 PreferenceValue::PreferenceValue(const wxPoint& value, const wxString& cfgKey, const bool initFromCfgFile) 00287 { 00288 _valueType = ptPoint; 00289 _value.valPoint = new wxPoint(value); 00290 _cfgKey = cfgKey; 00291 00292 if (!cfgKey.empty() && initFromCfgFile) 00293 read(cfgKey, _value.valPoint); 00294 } 00295 //--------------------------------------------------------------------------- 00296 00297 00298 /** 00299 * Constructor with a wxSize type. 00300 * 00301 * @param value The value of wxSize type. 00302 * @param cfgKey Corresponding key in the configuration file. Must be empty 00303 * if this item isn't stored in the configuration file. 00304 * @param initFromCfgFile If <CODE>true</CODE> and if a key was given, tries 00305 * first to initialize the value from the configuration 00306 * file. */ 00307 PreferenceValue::PreferenceValue(const wxSize& value, const wxString& cfgKey, const bool initFromCfgFile) 00308 { 00309 _valueType = ptSize; 00310 _value.valSize = new wxSize(value); 00311 _cfgKey = cfgKey; 00312 00313 if (!cfgKey.empty() && initFromCfgFile) 00314 read(cfgKey, _value.valSize); 00315 } 00316 //--------------------------------------------------------------------------- 00317 00318 00319 /** 00320 * Destructor. 00321 */ 00322 PreferenceValue::~PreferenceValue() 00323 { 00324 cleanup(); 00325 } 00326 //--------------------------------------------------------------------------- 00327 00328 00329 /** 00330 * Cleans up the memory. 00331 */ 00332 void PreferenceValue::cleanup() 00333 { 00334 switch (_valueType) 00335 { 00336 case ptLong : 00337 delete _value.valLong; 00338 break; 00339 case ptString : 00340 delete _value.valString; 00341 break; 00342 case ptDouble : 00343 delete _value.valDouble; 00344 break; 00345 case ptBoolean : 00346 delete _value.valBool; 00347 break; 00348 case ptRect : 00349 delete _value.valRect; 00350 break; 00351 case ptPoint : 00352 delete _value.valPoint; 00353 break; 00354 case ptSize : 00355 delete _value.valSize; 00356 break; 00357 } 00358 _valueType = ptNotDefined; 00359 } 00360 //--------------------------------------------------------------------------- 00361 00362 00363 /** 00364 * Gets the key of the preference in the configuration file. 00365 * 00366 * @return The key of the preference in the configuration file or an empty 00367 * string if there is no key. 00368 */ 00369 wxString PreferenceValue::getConfigKey() const 00370 { 00371 return _cfgKey; 00372 } 00373 //--------------------------------------------------------------------------- 00374 00375 00376 /** 00377 * Gets a long integer value. 00378 * 00379 * @param value A long integer where the preference value will be stored. 00380 * @return <CODE>true</CODE> if the preference value is an long integer, 00381 * <CODE>false</CODE> otherwise. 00382 */ 00383 bool PreferenceValue::get(long& value) const 00384 { 00385 if (_valueType == ptLong) 00386 { 00387 value = *(_value.valLong); 00388 return true; 00389 } 00390 else 00391 return false; 00392 } 00393 //--------------------------------------------------------------------------- 00394 00395 00396 /** 00397 * Sets a long integer value. 00398 * 00399 * @param newValue The new value of the preference. If the preference has a 00400 * corresponding key in the configuration file, the value 00401 * will be written in the configuration file. 00402 * @return <CODE>true</CODE> if the type of the preference is a long integer and 00403 * if the value has been successfully written in the configuration file 00404 * (if needed), <CODE>false</CODE> otherwise. 00405 * @remark The configuration file is given by the static method 00406 * <CODE>wxConfig::Get()</CODE>. 00407 */ 00408 bool PreferenceValue::set(const long newValue) 00409 { 00410 if (_valueType == ptLong) 00411 { 00412 *(_value.valLong) = newValue; 00413 if (_cfgKey.empty()) 00414 return true; 00415 else 00416 return wxConfig::Get()->Write(_cfgKey, newValue); 00417 } 00418 else 00419 return false; 00420 } 00421 //--------------------------------------------------------------------------- 00422 00423 00424 /** 00425 * Gets a string value. 00426 * 00427 * @param value A string where the preference value will be stored. 00428 * @return <CODE>true</CODE> if the preference value is a string, 00429 * <CODE>false</CODE> otherwise. 00430 */ 00431 bool PreferenceValue::get(wxString& value) const 00432 { 00433 if (_valueType == ptString) 00434 { 00435 value = *(_value.valString); 00436 return true; 00437 } 00438 else 00439 return false; 00440 } 00441 //--------------------------------------------------------------------------- 00442 00443 00444 /** 00445 * Sets a string value. 00446 * 00447 * @param newValue The new value of the preference. If the preference has a 00448 * corresponding key in the configuration file, the value 00449 * will be written in the configuration file. 00450 * @return <CODE>true</CODE> if the type of the preference is a string and 00451 * if the value has been successfully written in the configuration file 00452 * (if needed), <CODE>false</CODE> otherwise. 00453 * @remark The configuration file is given by the static method 00454 * <CODE>wxConfig::Get()</CODE>. 00455 */ 00456 bool PreferenceValue::set(const wxString& newValue) 00457 { 00458 if (_valueType == ptString) 00459 { 00460 *(_value.valString) = newValue; 00461 if (_cfgKey.empty()) 00462 return true; 00463 else 00464 return wxConfig::Get()->Write(_cfgKey, newValue); 00465 } 00466 else 00467 return false; 00468 } 00469 //--------------------------------------------------------------------------- 00470 00471 00472 /** 00473 * Gets a double value. 00474 * 00475 * @param value A double where the preference value will be stored. 00476 * @return <CODE>true</CODE> if the preference value is a double, 00477 * <CODE>false</CODE> otherwise. 00478 */ 00479 bool PreferenceValue::get(double& value) const 00480 { 00481 if (_valueType == ptDouble) 00482 { 00483 value = *(_value.valDouble); 00484 return true; 00485 } 00486 else 00487 return false; 00488 } 00489 //--------------------------------------------------------------------------- 00490 00491 00492 /** 00493 * Sets a string value. 00494 * 00495 * @param newValue The new value of the preference. If the preference has a 00496 * corresponding key in the configuration file, the value 00497 * will be written in the configuration file. 00498 * @return <CODE>true</CODE> if the type of the preference is a double and 00499 * if the value has been successfully written in the configuration file 00500 * (if needed), <CODE>false</CODE> otherwise. 00501 * @remark The configuration file is given by the static method 00502 * <CODE>wxConfig::Get()</CODE>. 00503 */ 00504 bool PreferenceValue::set(const double newValue) 00505 { 00506 if (_valueType == ptDouble) 00507 { 00508 *(_value.valDouble) = newValue; 00509 if (_cfgKey.empty()) 00510 return true; 00511 else 00512 return wxConfig::Get()->Write(_cfgKey, newValue); 00513 } 00514 else 00515 return false; 00516 } 00517 //--------------------------------------------------------------------------- 00518 00519 00520 /** 00521 * Gets a boolean value. 00522 * 00523 * @param value A boolean where the preference value will be stored. 00524 * @return <CODE>true</CODE> if the preference value is a boolean, 00525 * <CODE>false</CODE> otherwise. 00526 */ 00527 bool PreferenceValue::get(bool& value) const 00528 { 00529 if (_valueType == ptBoolean) 00530 { 00531 value = *(_value.valBool); 00532 return true; 00533 } 00534 else 00535 return false; 00536 } 00537 //--------------------------------------------------------------------------- 00538 00539 00540 /** 00541 * Sets a boolean value. 00542 * 00543 * @param newValue The new value of the preference. If the preference has a 00544 * corresponding key in the configuration file, the value 00545 * will be written in the configuration file. 00546 * @return <CODE>true</CODE> if the type of the preference is a boolean and 00547 * if the value has been successfully written in the configuration file 00548 * (if needed), <CODE>false</CODE> otherwise. 00549 * @remark The configuration file is given by the static method 00550 * <CODE>wxConfig::Get()</CODE>. 00551 */ 00552 bool PreferenceValue::set(const bool newValue) 00553 { 00554 if (_valueType == ptBoolean) 00555 { 00556 *(_value.valBool) = newValue; 00557 if (_cfgKey.empty()) 00558 return true; 00559 else 00560 return wxConfig::Get()->Write(_cfgKey, newValue); 00561 } 00562 else 00563 return false; 00564 } 00565 //--------------------------------------------------------------------------- 00566 00567 00568 /** 00569 * Gets a wxRect value. 00570 * 00571 * @param value A wxRect instance where the preference value will be stored. 00572 * @return <CODE>true</CODE> if the preference value is a wxRect, 00573 * <CODE>false</CODE> otherwise. 00574 */ 00575 bool PreferenceValue::get(wxRect& value) const 00576 { 00577 if (_valueType == ptRect) 00578 { 00579 value = *(_value.valRect); 00580 return true; 00581 } 00582 else 00583 return false; 00584 } 00585 //--------------------------------------------------------------------------- 00586 00587 00588 /** 00589 * Sets a wxRect value. 00590 * 00591 * @param newValue The new value of the preference. If the preference has a 00592 * corresponding key in the configuration file, the value 00593 * will be written in the configuration file. 00594 * @return <CODE>true</CODE> if the type of the preference is a wxRect and 00595 * if the value has been successfully written in the configuration file 00596 * (if needed), <CODE>false</CODE> otherwise. 00597 * @remark The configuration file is given by the static method 00598 * <CODE>wxConfig::Get()</CODE>. 00599 */ 00600 bool PreferenceValue::set(const wxRect& newValue) 00601 { 00602 if (_valueType == ptRect) 00603 { 00604 *(_value.valRect) = newValue; 00605 if (_cfgKey.empty()) 00606 return true; 00607 else 00608 { 00609 wxString r; 00610 r.Printf(wxT("%d,%d,%d,%d"), newValue.GetX(), newValue.GetY(), 00611 newValue.GetWidth(), newValue.GetHeight()); 00612 return wxConfig::Get()->Write(_cfgKey, r); 00613 } 00614 } 00615 else 00616 return false; 00617 } 00618 //--------------------------------------------------------------------------- 00619 00620 00621 /** 00622 * Gets a wxPoint value. 00623 * 00624 * @param value A wxPoint instance where the preference value will be stored. 00625 * @return <CODE>true</CODE> if the preference value is a wxPoint, 00626 * <CODE>false</CODE> otherwise. 00627 */ 00628 bool PreferenceValue::get(wxPoint& value) const 00629 { 00630 if (_valueType == ptPoint) 00631 { 00632 value = *(_value.valPoint); 00633 return true; 00634 } 00635 else 00636 return false; 00637 } 00638 //--------------------------------------------------------------------------- 00639 00640 00641 /** 00642 * Sets a wxPoint value. 00643 * 00644 * @param newValue The new value of the preference. If the preference has a 00645 * corresponding key in the configuration file, the value 00646 * will be written in the configuration file. 00647 * @return <CODE>true</CODE> if the type of the preference is a wxPoint and 00648 * if the value has been successfully written in the configuration file 00649 * (if needed), <CODE>false</CODE> otherwise. 00650 * @remark The configuration file is given by the static method 00651 * <CODE>wxConfig::Get()</CODE>. 00652 */ 00653 bool PreferenceValue::set(const wxPoint& newValue) 00654 { 00655 if (_valueType == ptPoint) 00656 { 00657 *(_value.valPoint) = newValue; 00658 if (_cfgKey.empty()) 00659 return true; 00660 else 00661 { 00662 wxString r; 00663 r.Printf(wxT("%d,%d"), newValue.x, newValue.y); 00664 return wxConfig::Get()->Write(_cfgKey, r); 00665 } 00666 } 00667 else 00668 return false; 00669 } 00670 //--------------------------------------------------------------------------- 00671 00672 00673 /** 00674 * Gets a wxSize value. 00675 * 00676 * @param value A wxSize instance where the preference value will be stored. 00677 * @return <CODE>true</CODE> if the preference value is a wxSize, 00678 * <CODE>false</CODE> otherwise. 00679 */ 00680 bool PreferenceValue::get(wxSize& value) const 00681 { 00682 if (_valueType == ptSize) 00683 { 00684 value = *(_value.valSize); 00685 return true; 00686 } 00687 else 00688 return false; 00689 } 00690 //--------------------------------------------------------------------------- 00691 00692 00693 /** 00694 * Sets a wxSize value. 00695 * 00696 * @param newValue The new value of the preference. If the preference has a 00697 * corresponding key in the configuration file, the value 00698 * will be written in the configuration file. 00699 * @return <CODE>true</CODE> if the type of the preference is a wxSize and 00700 * if the value has been successfully written in the configuration file 00701 * (if needed), <CODE>false</CODE> otherwise. 00702 * @remark The configuration file is given by the static method 00703 * <CODE>wxConfig::Get()</CODE>. 00704 */ 00705 bool PreferenceValue::set(const wxSize& newValue) 00706 { 00707 if (_valueType == ptSize) 00708 { 00709 *(_value.valSize) = newValue; 00710 if (_cfgKey.empty()) 00711 return true; 00712 else 00713 { 00714 wxString r; 00715 r.Printf(wxT("%d,%d"), newValue.GetWidth(), newValue.GetHeight()); 00716 return wxConfig::Get()->Write(_cfgKey, r); 00717 } 00718 } 00719 else 00720 return false; 00721 } 00722 //--------------------------------------------------------------------------- 00723 00724 00725 /** 00726 * Reads a wxRect value from the configuration file. 00727 * 00728 * @param key The key whose associated value is to be returned. 00729 * @param r A pointer on the wxRect in which the value will be stored. 00730 * @param cfg A pointer on the configuration instance from which the value 00731 * must be read. If <CODE>NULL</CODE>, the global configuration 00732 * instance (given by <CODE>wxConfig::Get()</CODE>) will be used. 00733 * @return <CODE>true</CODE> if the value was read, <CODE>false</CODE> 00734 * otherwise. If the key was not found, <CODE>r</CODE> is not changed. 00735 */ 00736 bool PreferenceValue::read(const wxString& key, wxRect* r, wxConfigBase* cfg) 00737 { 00738 // Gets the configuration object instance. 00739 wxConfigBase* c = (cfg != NULL) ? cfg : wxConfig::Get(); 00740 00741 // Gets the rects from the configuration file. 00742 wxString res = c->Read(key, wxEmptyString); 00743 if (res.empty()) 00744 return false; 00745 else 00746 { 00747 // Reads the tokens and converts them in integers values. 00748 wxStringTokenizer st(res, wxT(",")); 00749 if (st.CountTokens() != 4) 00750 return false; 00751 else 00752 { 00753 bool ok = true; 00754 int res[4]; 00755 int i = 0; 00756 long l; 00757 while (ok && st.HasMoreTokens()) 00758 { 00759 wxString s = st.GetNextToken(); 00760 if (s.ToLong(&l)) 00761 res[i++] = static_cast<int>(l); // res[i++] : ugly ! ^^; 00762 else 00763 ok = false; 00764 } 00765 if (ok) 00766 { 00767 r->SetX(res[0]); 00768 r->SetY(res[1]); 00769 r->SetWidth(res[2]); 00770 r->SetHeight(res[3]); 00771 } 00772 return ok; 00773 } 00774 } 00775 } 00776 //--------------------------------------------------------------------------- 00777 00778 00779 /** 00780 * Reads a wxRect value from the configuration file. 00781 * 00782 * @param key The key whose associated value is to be returned. 00783 * @param def The default value to return if the value can't be read. 00784 * @param cfg A pointer on the configuration instance from which the value 00785 * must be read. If <CODE>NULL</CODE>, the global configuration 00786 * instance (given by <CODE>wxConfig::Get()</CODE>) will be used. 00787 * @return The value corresponding to the key or the default value if the value 00788 * hasn't been read successfully. 00789 */ 00790 wxRect PreferenceValue::read(const wxString& key, const wxRect& def, wxConfigBase* cfg) 00791 { 00792 wxRect r; 00793 00794 if (read(key, &r, cfg)) 00795 return r; 00796 else 00797 return def; 00798 } 00799 //--------------------------------------------------------------------------- 00800 00801 00802 /** 00803 * Reads a wxPoint value from the configuration file. 00804 * 00805 * @param key The key whose associated value is to be returned. 00806 * @param r A pointer on the wxPoint in which the value will be stored. 00807 * @param cfg A pointer on the configuration instance from which the value 00808 * must be read. If <CODE>NULL</CODE>, the global configuration 00809 * instance (given by <CODE>wxConfig::Get()</CODE>) will be used. 00810 * @return <CODE>true</CODE> if the value was read, <CODE>false</CODE> 00811 * otherwise. If the key was not found, <CODE>r</CODE> is not changed. 00812 */ 00813 bool PreferenceValue::read(const wxString& key, wxPoint* r, wxConfigBase* cfg) 00814 { 00815 // Gets the configuration object instance. 00816 wxConfigBase* c = (cfg != NULL) ? cfg : wxConfig::Get(); 00817 00818 // Gets the point from the configuration file. 00819 wxString res = c->Read(key, wxEmptyString); 00820 if (res.empty()) 00821 return false; 00822 else 00823 { 00824 // Reads the tokens and converts them in integers values. 00825 wxStringTokenizer st(res, wxT(",")); 00826 if (st.CountTokens() != 2) 00827 return false; 00828 else 00829 { 00830 bool ok = true; 00831 int res[2]; 00832 int i = 0; 00833 long l; 00834 while (ok && st.HasMoreTokens()) 00835 { 00836 wxString s = st.GetNextToken(); 00837 if (s.ToLong(&l)) 00838 res[i++] = static_cast<int>(l); // res[i++] : ugly ! ^^; 00839 else 00840 ok = false; 00841 } 00842 if (ok) 00843 { 00844 r->x = res[0]; 00845 r->y = res[1]; 00846 } 00847 return ok; 00848 } 00849 } 00850 } 00851 //--------------------------------------------------------------------------- 00852 00853 00854 /** 00855 * Reads a wxPoint value from the configuration file. 00856 * 00857 * @param key The key whose associated value is to be returned. 00858 * @param def The default value to return if the value can't be read. 00859 * @param cfg A pointer on the configuration instance from which the value 00860 * must be read. If <CODE>NULL</CODE>, the global configuration 00861 * instance (given by <CODE>wxConfig::Get()</CODE>) will be used. 00862 * @return The value corresponding to the key or the default value if the value 00863 * hasn't been read successfully. 00864 */ 00865 wxPoint PreferenceValue::read(const wxString& key, const wxPoint& def, wxConfigBase* cfg) 00866 { 00867 wxPoint r; 00868 00869 if (read(key, &r, cfg)) 00870 return r; 00871 else 00872 return def; 00873 } 00874 //--------------------------------------------------------------------------- 00875 00876 00877 /** 00878 * Reads a wxSize value from the configuration file. 00879 * 00880 * @param key The key whose associated value is to be returned. 00881 * @param r A pointer on the wxSize in which the value will be stored. 00882 * @param cfg A pointer on the configuration instance from which the value 00883 * must be read. If <CODE>NULL</CODE>, the global configuration 00884 * instance (given by <CODE>wxConfig::Get()</CODE>) will be used. 00885 * @return <CODE>true</CODE> if the value was read, <CODE>false</CODE> 00886 * otherwise. If the key was not found, <CODE>r</CODE> is not changed. 00887 */ 00888 bool PreferenceValue::read(const wxString& key, wxSize* r, wxConfigBase* cfg) 00889 { 00890 // Gets the configuration object instance. 00891 wxConfigBase* c = (cfg != NULL) ? cfg : wxConfig::Get(); 00892 00893 // Gets the size from the configuration file. 00894 wxString res = c->Read(key, wxEmptyString); 00895 if (res.empty()) 00896 return false; 00897 else 00898 { 00899 // Reads the tokens and converts them in integers values. 00900 wxStringTokenizer st(res, wxT(",")); 00901 if (st.CountTokens() != 2) 00902 return false; 00903 else 00904 { 00905 bool ok = true; 00906 int res[2]; 00907 int i = 0; 00908 long l; 00909 while (ok && st.HasMoreTokens()) 00910 { 00911 wxString s = st.GetNextToken(); 00912 if (s.ToLong(&l)) 00913 res[i++] = static_cast<int>(l); // res[i++] : ugly ! ^^; 00914 else 00915 ok = false; 00916 } 00917 if (ok) 00918 { 00919 r->SetWidth(res[0]); 00920 r->SetHeight(res[1]); 00921 } 00922 return ok; 00923 } 00924 } 00925 } 00926 //--------------------------------------------------------------------------- 00927 00928 00929 /** 00930 * Reads a wxSize value from the configuration file. 00931 * 00932 * @param key The key whose associated value is to be returned. 00933 * @param def The default value to return if the value can't be read. 00934 * @param cfg A pointer on the configuration instance from which the value 00935 * must be read. If <CODE>NULL</CODE>, the global configuration 00936 * instance (given by <CODE>wxConfig::Get()</CODE>) will be used. 00937 * @return The value corresponding to the key or the default value if the value 00938 * hasn't been read successfully. 00939 */ 00940 wxSize PreferenceValue::read(const wxString& key, const wxSize& def, wxConfigBase* cfg) 00941 { 00942 wxSize r; 00943 00944 if (read(key, &r, cfg)) 00945 return r; 00946 else 00947 return def; 00948 } 00949 //--------------------------------------------------------------------------- 00950 00951 00952 00953 //########################################################################### 00954 // AppPrefs members 00955 //########################################################################### 00956 00957 // Static attributes of the AppPrefs class 00958 AppPrefs* AppPrefs::global = NULL; 00959 wxString AppPrefs::configDir; 00960 wxString AppPrefs::configFileName; 00961 //--------------------------------------------------------------------------- 00962 00963 00964 /** 00965 * Default constructor. 00966 */ 00967 AppPrefs::AppPrefs() 00968 { 00969 init(); 00970 } 00971 //--------------------------------------------------------------------------- 00972 00973 00974 /** 00975 * Inits the instance. 00976 * 00977 * This method creates the preferences that will be valid and associates to them 00978 * an unique integer key. 00979 * 00980 * The preferences values are read from the configuration file if it's 00981 * available, else default values are used. 00982 */ 00983 void AppPrefs::init() 00984 { 00985 wxString path; 00986 00987 // GUI preferences 00988 // Configuration dialog preferencies 00989 prefs.insert(HashPrefs::value_type(prGUI_CONF_WINDOW_SIZE, PreferenceValue(wxSize(-1, -1), wxT("GUI/ConfigDlg/WindowSize")))); 00990 prefs.insert(HashPrefs::value_type(prGUI_CONF_SASH_POSITION, PreferenceValue(100L, wxT("GUI/ConfigDlg/SashPosition")))); 00991 00992 // Main window 00993 path = wxT("GUI/Main/"); 00994 wxRect r = getDefaultMainWindowRect(); 00995 prefs.insert(HashPrefs::value_type(prGUI_MAIN_SAVE_WINDOW_POSITION, PreferenceValue(true, path + wxT("SaveWindowPosition")))); 00996 prefs.insert(HashPrefs::value_type(prGUI_MAIN_SAVE_WINDOW_SIZE, PreferenceValue(true, path + wxT("SaveWindowSize")))); 00997 prefs.insert(HashPrefs::value_type(prGUI_MAIN_WINDOW_POSITION, PreferenceValue(wxPoint(r.GetX(), r.GetY()), path + wxT("WindowPosition")))); 00998 prefs.insert(HashPrefs::value_type(prGUI_MAIN_WINDOW_SIZE, PreferenceValue(wxSize(r.GetWidth(), r.GetHeight()), path + wxT("WindowSize")))); 00999 prefs.insert(HashPrefs::value_type(prGUI_MAIN_SHOW_TOOLBAR, PreferenceValue(true, path + wxT("ShowToolbar")))); 01000 prefs.insert(HashPrefs::value_type(prGUI_MAIN_SHOW_STATUSBAR, PreferenceValue(true, path + wxT("ShowStatusbar")))); 01001 prefs.insert(HashPrefs::value_type(prGUI_MAIN_LAST_DIRECTORY, PreferenceValue(wxString(), path + wxT("LastDirectory")))); 01002 01003 // Checksums list 01004 path = wxT("GUI/Main/ChecksumsList/"); 01005 prefs.insert(HashPrefs::value_type(prGUI_MAIN_SUMS_SAVECOLUMNTOSORT, PreferenceValue(false, path + wxT("SaveColumnToSort")))); 01006 prefs.insert(HashPrefs::value_type(prGUI_MAIN_SUMS_COLUMNTOSORT, PreferenceValue(0L, path + wxT("ColumnToSort")))); 01007 prefs.insert(HashPrefs::value_type(prGUI_MAIN_SUMS_COLUMNSORTORDER, PreferenceValue(0L, path + wxT("SaveColumnSortOrder")))); 01008 prefs.insert(HashPrefs::value_type(prGUI_MAIN_SUMS_SAVECOLUMNSWIDTHS, PreferenceValue(true, path + wxT("SaveColumnsWidths")))); 01009 prefs.insert(HashPrefs::value_type(prGUI_MAIN_SUMS_DIRSINABSOLUTEPATH, PreferenceValue(false, path + wxT("DirsInAbsolutePath")))); 01010 prefs.insert(HashPrefs::value_type(prGUI_MAIN_SUMS_UPPERCASE, PreferenceValue(true, path + wxT("Uppercase")))); 01011 prefs.insert(HashPrefs::value_type(prGUI_MAIN_SUMS_HRULES, PreferenceValue(false, path + wxT("HRules")))); 01012 prefs.insert(HashPrefs::value_type(prGUI_MAIN_SUMS_VRULES, PreferenceValue(false, path + wxT("VRules")))); 01013 prefs.insert(HashPrefs::value_type(prGUI_MAIN_SUMS_COL_FILENAME_WIDTH, PreferenceValue(150L, path + wxT("ColumnWidthFileName")))); 01014 prefs.insert(HashPrefs::value_type(prGUI_MAIN_SUMS_COL_DIRECTORY_WIDTH, PreferenceValue(150L, path + wxT("ColumnWidthDirectory")))); 01015 prefs.insert(HashPrefs::value_type(prGUI_MAIN_SUMS_COL_CHECKSUM_WIDTH, PreferenceValue(150L, path + wxT("ColumnWidthChecksumValue")))); 01016 prefs.insert(HashPrefs::value_type(prGUI_MAIN_SUMS_COL_STATE_WIDTH, PreferenceValue(150L, path + wxT("ColumnWidthState")))); 01017 prefs.insert(HashPrefs::value_type(prGUI_MAIN_SUMS_COL_FIRST, PreferenceValue(0L, path + wxT("ColumnFirst")))); 01018 prefs.insert(HashPrefs::value_type(prGUI_MAIN_SUMS_COL_SECOND, PreferenceValue(1L, path + wxT("ColumnSecond")))); 01019 prefs.insert(HashPrefs::value_type(prGUI_MAIN_SUMS_COL_THIRD, PreferenceValue(2L, path + wxT("ColumnThird")))); 01020 prefs.insert(HashPrefs::value_type(prGUI_MAIN_SUMS_COL_FOURTH, PreferenceValue(3L, path + wxT("ColumnFourth")))); 01021 01022 // New files 01023 prefs.insert(HashPrefs::value_type(prGUI_NEWFILE_LAST_DIRECTORY, PreferenceValue(wxString(), wxT("GUI/NewFile/LastDirectory")))); 01024 prefs.insert(HashPrefs::value_type(prGUI_NEWFILE_LAST_FILETYPE, PreferenceValue(0L, wxT("GUI/NewFile/LastFileType")))); 01025 01026 // GUI behavior 01027 path = wxT("GUI/Behavior/"); 01028 prefs.insert(HashPrefs::value_type(prGUI_AUTO_CHECK_ON_OPEN, PreferenceValue(true, path + wxT("AutoCheckOnOpen")))); 01029 prefs.insert(HashPrefs::value_type(prGUI_DLG_SUMUP_CHECK, PreferenceValue(false, path + wxT("DlgSumUpCheck")))); 01030 prefs.insert(HashPrefs::value_type(prGUI_WARN_ON_INVALID_WHEN_SAVING,PreferenceValue(true, path + wxT("WarnOnInvalidWhenSaving")))); 01031 01032 // Command line options 01033 path = wxT("GUI/CommandLine/"); 01034 prefs.insert(HashPrefs::value_type(prGUI_CLN_VERIFY_DONT_SHOW_GUI, PreferenceValue(false, path + wxT("DontShowWhenAllCorrect")))); 01035 prefs.insert(HashPrefs::value_type(prGUI_CLN_APPEND_SHOW_GUI, PreferenceValue(static_cast<long>(clgOnWarning), path + wxT("AppendShowGUI")))); 01036 prefs.insert(HashPrefs::value_type(prGUI_CLN_CREATE_SHOW_GUI, PreferenceValue(static_cast<long>(clgOnWarning), path + wxT("CreateShowGUI")))); 01037 01038 // SFV files 01039 path = wxT("ChecksumsFiles/SFV/"); 01040 prefs.insert(HashPrefs::value_type(prSFV_READ_PATH_SEPARATOR, PreferenceValue(static_cast<long>(wxPATH_NATIVE), path + wxT("ReadPathSeparator")))); 01041 prefs.insert(HashPrefs::value_type(prSFV_WRITE_GEN_AND_DATE, PreferenceValue(true, path + wxT("WriteGeneratorAndDate")))); 01042 prefs.insert(HashPrefs::value_type(prSFV_IDENTIFY_AS, PreferenceValue(wxString(), path + wxT("IdentifyAs/Generator")))); 01043 prefs.insert(HashPrefs::value_type(prSFV_WRITE_FILE_SIZE_AND_DATE, PreferenceValue(true, path + wxT("WriteFileSizeAndDate")))); 01044 prefs.insert(HashPrefs::value_type(prSFV_WRITE_PATH_SEPARATOR, PreferenceValue(static_cast<long>(wxPATH_WIN), path + wxT("WritePathSeparator")))); 01045 prefs.insert(HashPrefs::value_type(prSFV_WRITE_EOL, PreferenceValue(static_cast<long>(wxEOL_DOS), path + wxT("WriteEndOfLine")))); 01046 01047 // MD5 files 01048 path = wxT("ChecksumsFiles/MD5/"); 01049 prefs.insert(HashPrefs::value_type(prMD5_READ_PATH_SEPARATOR, PreferenceValue(static_cast<long>(wxPATH_NATIVE), path + wxT("ReadPathSeparator")))); 01050 prefs.insert(HashPrefs::value_type(prMD5_WRITE_GEN_AND_DATE, PreferenceValue(false, path + wxT("WriteGeneratorAndDate")))); 01051 prefs.insert(HashPrefs::value_type(prMD5_WRITE_FILE_SIZE_AND_DATE, PreferenceValue(false, path + wxT("WriteFileSizeAndDate")))); 01052 prefs.insert(HashPrefs::value_type(prMD5_WRITE_PATH_SEPARATOR, PreferenceValue(static_cast<long>(wxPATH_UNIX), path + wxT("WritePathSeparator")))); 01053 prefs.insert(HashPrefs::value_type(prMD5_WRITE_EOL, PreferenceValue(static_cast<long>(wxEOL_UNIX), path + wxT("WriteEndOfLine")))); 01054 01055 // Engine preferences 01056 prefs.insert(HashPrefs::value_type(prENGINE_READ_BUFFER, PreferenceValue(0xFFFFL, wxT("Engine/ReadBuffer")))); 01057 01058 // Language preferences 01059 prefs.insert(HashPrefs::value_type(prLANGUAGE_NAME, PreferenceValue(wxString(), wxT("Language/Name")))); 01060 01061 // Multi-check preferences 01062 path = wxT("GUI/MultiCheck/"); 01063 prefs.insert(HashPrefs::value_type(prMC_WINDOW_SIZE, PreferenceValue(wxSize(-1, -1), path + wxT("WindowSize")))); 01064 prefs.insert(HashPrefs::value_type(prMC_GLOBAL_SUMMARY, PreferenceValue(true, path + wxT("GlobalSummary")))); 01065 prefs.insert(HashPrefs::value_type(prMC_CHECKSUMS_FILE_SUMMARY, PreferenceValue(true, path + wxT("ChecksumsFileSummary")))); 01066 prefs.insert(HashPrefs::value_type(prMC_FILE_STATE, PreferenceValue(false, path + wxT("FileState")))); 01067 prefs.insert(HashPrefs::value_type(prMC_NO_CORRECT_FILE_STATE, PreferenceValue(true, path + wxT("NoCorrectFileState")))); 01068 prefs.insert(HashPrefs::value_type(prMC_NORMAL_COLOUR, PreferenceValue(0x000000L, path + wxT("NormalColour")))); 01069 prefs.insert(HashPrefs::value_type(prMC_SUCCESS_COLOUR, PreferenceValue(0x008000L, path + wxT("SuccessColour")))); 01070 prefs.insert(HashPrefs::value_type(prMC_WARNING_COLOUR, PreferenceValue(0xFF8000L, path + wxT("WarningColour")))); 01071 prefs.insert(HashPrefs::value_type(prMC_ERROR_COLOUR, PreferenceValue(0x800000L, path + wxT("ErrorColour")))); 01072 01073 01074 // Batch creation preferences 01075 path = wxT("GUI/BatchCreation/"); 01076 prefs.insert(HashPrefs::value_type(prBC_WINDOW_SIZE, PreferenceValue(wxSize(-1, -1), path + wxT("WindowSize")))); 01077 prefs.insert(HashPrefs::value_type(prBC_OVERWRITE_WHEN_CKFILE_EXISTS, PreferenceValue(false, path + wxT("OvrCkFileWhenItExists")))); 01078 prefs.insert(HashPrefs::value_type(prBC_REPLACE_FILE_EXTENSION, PreferenceValue(true, path + wxT("ReplaceExtension")))); 01079 prefs.insert(HashPrefs::value_type(prBC_VERBOSITY_LEVEL, PreferenceValue(2L, path + wxT("VerbosityLevel")))); 01080 prefs.insert(HashPrefs::value_type(prBC_NORMAL_COLOUR, PreferenceValue(0x000000L, path + wxT("NormalColour")))); 01081 prefs.insert(HashPrefs::value_type(prBC_SUCCESS_COLOUR, PreferenceValue(0x008000L, path + wxT("SuccessColour")))); 01082 prefs.insert(HashPrefs::value_type(prBC_WARNING_COLOUR, PreferenceValue(0xFF8000L, path + wxT("WarningColour")))); 01083 prefs.insert(HashPrefs::value_type(prBC_ERROR_COLOUR, PreferenceValue(0x800000L, path + wxT("ErrorColour")))); 01084 } 01085 //--------------------------------------------------------------------------- 01086 01087 01088 /** 01089 * Clones the source instance in this instance. 01090 * 01091 * @param source Source instance. 01092 */ 01093 void AppPrefs::clone(const AppPrefs& source) 01094 { 01095 if (this != &source) 01096 this->prefs = source.prefs; 01097 } 01098 //--------------------------------------------------------------------------- 01099 01100 01101 /** 01102 * Copy constructor. 01103 * 01104 * @param source Source instance. 01105 */ 01106 AppPrefs::AppPrefs(const AppPrefs& source) 01107 { 01108 clone(source); 01109 } 01110 //--------------------------------------------------------------------------- 01111 01112 01113 /** 01114 * Assignment operator. 01115 * 01116 * @param source Source instance. 01117 */ 01118 AppPrefs& AppPrefs::operator=(const AppPrefs& source) 01119 { 01120 clone(source); 01121 return *this; 01122 } 01123 //--------------------------------------------------------------------------- 01124 01125 01126 /** 01127 * Gets the key of the preference in the configuration file. 01128 * 01129 * @param key The key whose configuration key in the configuration file is to 01130 * be returned. 01131 * @return The key of the preference in the configuration file or an empty 01132 * string if there is no key. 01133 */ 01134 wxString AppPrefs::getConfigKey(const int key) const 01135 { 01136 wxString res; 01137 01138 HashPrefs::const_iterator it = prefs.find(key); 01139 if (it != prefs.end()) 01140 res = it->second.getConfigKey(); 01141 01142 return res; 01143 } 01144 //--------------------------------------------------------------------------- 01145 01146 01147 /** 01148 * Reads a string from the key. 01149 * 01150 * @param key The key whose associated value is to be returned. 01151 * @param str The string in which the value will be stored. 01152 * @return <CODE>true</CODE> if the value was read, <CODE>false</CODE> 01153 * otherwise. If the key was not found, <CODE>str</CODE> is not changed. 01154 */ 01155 bool AppPrefs::read(const int key, wxString& str) const 01156 { 01157 HashPrefs::const_iterator it = prefs.find(key); 01158 if (it == prefs.end()) 01159 return false; 01160 else 01161 return it->second.get(str); 01162 } 01163 //--------------------------------------------------------------------------- 01164 01165 01166 /** 01167 * Reads a string from the key. 01168 * 01169 * @param key The key whose associated value is to be returned. 01170 * @param def The default value to return if the value can't be read. 01171 * @return The value corresponding to the key or the default value if the value 01172 * hasn't been read successfully. 01173 */ 01174 wxString AppPrefs::readString(const int key, const wxString& def) const 01175 { 01176 wxString res; 01177 01178 if (read(key, res)) 01179 return res; 01180 else 01181 return def; 01182 } 01183 //--------------------------------------------------------------------------- 01184 01185 01186 /** 01187 * Reads a long integer value from the key. 01188 * 01189 * @param key The key whose associated value is to be returned. 01190 * @param l The long integer in which the value will be stored. 01191 * @return <CODE>true</CODE> if the value was read, <CODE>false</CODE> 01192 * otherwise. If the key was not found, <CODE>l</CODE> is not changed. 01193 */ 01194 bool AppPrefs::read(const int key, long& l) const 01195 { 01196 HashPrefs::const_iterator it = prefs.find(key); 01197 if (it == prefs.end()) 01198 return false; 01199 else 01200 return it->second.get(l); 01201 } 01202 //--------------------------------------------------------------------------- 01203 01204 01205 /** 01206 * Reads a long integer value from the key. 01207 * 01208 * @param key The key whose associated value is to be returned. 01209 * @param def The default value to return if the value can't be read. 01210 * @return The value corresponding to the key or the default value if the value 01211 * hasn't been read successfully. 01212 */ 01213 long AppPrefs::readLong(const int key, const long def) const 01214 { 01215 long res; 01216 01217 if (read(key, res)) 01218 return res; 01219 else 01220 return def; 01221 } 01222 //--------------------------------------------------------------------------- 01223 01224 01225 /** 01226 * Reads a double value from the key. 01227 * 01228 * @param key The key whose associated value is to be returned. 01229 * @param d The double in which the value will be stored. 01230 * @return <CODE>true</CODE> if the value was read, <CODE>false</CODE> 01231 * otherwise. If the key was not found, <CODE>d</CODE> is not changed. 01232 */ 01233 bool AppPrefs::read(const int key, double& d) const 01234 { 01235 HashPrefs::const_iterator it = prefs.find(key); 01236 if (it == prefs.end()) 01237 return false; 01238 else 01239 return it->second.get(d); 01240 } 01241 //--------------------------------------------------------------------------- 01242 01243 01244 /** 01245 * Reads a double value from the key. 01246 * 01247 * @param key The key whose associated value is to be returned. 01248 * @param def The default value to return if the value can't be read. 01249 * @return The value corresponding to the key or the default value if the value 01250 * hasn't been read successfully. 01251 */ 01252 double AppPrefs::readDouble(const int key, const double def) const 01253 { 01254 double res; 01255 01256 if (read(key, res)) 01257 return res; 01258 else 01259 return def; 01260 } 01261 //--------------------------------------------------------------------------- 01262 01263 01264 /** 01265 * Reads a boolean value from the key. 01266 * 01267 * @param key The key whose associated value is to be returned. 01268 * @param b The boolean in which the value will be stored. 01269 * @return <CODE>true</CODE> if the value was read, <CODE>false</CODE> 01270 * otherwise. If the key was not found, <CODE>b</CODE> is not changed. 01271 */ 01272 bool AppPrefs::read(const int key, bool& b) const 01273 { 01274 HashPrefs::const_iterator it = prefs.find(key); 01275 if (it == prefs.end()) 01276 return false; 01277 else 01278 return it->second.get(b); 01279 } 01280 //--------------------------------------------------------------------------- 01281 01282 01283 /** 01284 * Reads a boolean value from the key. 01285 * 01286 * @param key The key whose associated value is to be returned. 01287 * @param def The default value to return if the value can't be read. 01288 * @return The value corresponding to the key or the default value if the value 01289 * hasn't been read successfully. 01290 */ 01291 bool AppPrefs::readBool(const int key, const bool def) const 01292 { 01293 bool res; 01294 01295 if (read(key, res)) 01296 return res; 01297 else 01298 return def; 01299 } 01300 //--------------------------------------------------------------------------- 01301 01302 01303 /** 01304 * Reads a wxRect value from the key. 01305 * 01306 * @param key The key whose associated value is to be returned. 01307 * @param r The wxRect in which the value will be stored. 01308 * @return <CODE>true</CODE> if the value was read, <CODE>false</CODE> 01309 * otherwise. If the key was not found, <CODE>r</CODE> is not changed. 01310 */ 01311 bool AppPrefs::read(const int key, wxRect& r) const 01312 { 01313 HashPrefs::const_iterator it = prefs.find(key); 01314 if (it == prefs.end()) 01315 return false; 01316 else 01317 return it->second.get(r); 01318 } 01319 //--------------------------------------------------------------------------- 01320 01321 01322 /** 01323 * Reads a wxRect value from the key. 01324 * 01325 * @param key The key whose associated value is to be returned. 01326 * @param def The default value to return if the value can't be read. 01327 * @return The value corresponding to the key or the default value if the value 01328 * hasn't been read successfully. 01329 */ 01330 wxRect AppPrefs::readRect(const int key, const wxRect& def) const 01331 { 01332 wxRect res; 01333 01334 if (read(key, res)) 01335 return res; 01336 else 01337 return def; 01338 } 01339 //--------------------------------------------------------------------------- 01340 01341 01342 /** 01343 * Reads a wxPoint value from the key. 01344 * 01345 * @param key The key whose associated value is to be returned. 01346 * @param p The wxPoint in which the value will be stored. 01347 * @return <CODE>true</CODE> if the value was read, <CODE>false</CODE> 01348 * otherwise. If the key was not found, <CODE>r</CODE> is not changed. 01349 */ 01350 bool AppPrefs::read(const int key, wxPoint& p) const 01351 { 01352 HashPrefs::const_iterator it = prefs.find(key); 01353 if (it == prefs.end()) 01354 return false; 01355 else 01356 return it->second.get(p); 01357 } 01358 //--------------------------------------------------------------------------- 01359 01360 01361 /** 01362 * Reads a wxPoint value from the key. 01363 * 01364 * @param key The key whose associated value is to be returned. 01365 * @param def The default value to return if the value can't be read. 01366 * @return The value corresponding to the key or the default value if the value 01367 * hasn't been read successfully. 01368 */ 01369 wxPoint AppPrefs::readPoint(const int key, const wxPoint& def) const 01370 { 01371 wxPoint res; 01372 01373 if (read(key, res)) 01374 return res; 01375 else 01376 return def; 01377 } 01378 //--------------------------------------------------------------------------- 01379 01380 01381 /** 01382 * Reads a wxSize value from the key. 01383 * 01384 * @param key The key whose associated value is to be returned. 01385 * @param s The wxSize in which the value will be stored. 01386 * @return <CODE>true</CODE> if the value was read, <CODE>false</CODE> 01387 * otherwise. If the key was not found, <CODE>r</CODE> is not changed. 01388 */ 01389 bool AppPrefs::read(const int key, wxSize& s) const 01390 { 01391 HashPrefs::const_iterator it = prefs.find(key); 01392 if (it == prefs.end()) 01393 return false; 01394 else 01395 return it->second.get(s); 01396 } 01397 //--------------------------------------------------------------------------- 01398 01399 01400 /** 01401 * Reads a wxSize value from the key. 01402 * 01403 * @param key The key whose associated value is to be returned. 01404 * @param def The default value to return if the value can't be read. 01405 * @return The value corresponding to the key or the default value if the value 01406 * hasn't been read successfully. 01407 */ 01408 wxSize AppPrefs::readSize(const int key, const wxSize& def) const 01409 { 01410 wxSize res; 01411 01412 if (read(key, res)) 01413 return res; 01414 else 01415 return def; 01416 } 01417 //--------------------------------------------------------------------------- 01418 01419 01420 /** 01421 * Write a string in the preference given by the key. 01422 * 01423 * @param key The key of the preference where the value will be stored. 01424 * @param str The value to store. 01425 * @return <CODE>true</CODE> if the value has been stored successfully, 01426 * <CODE>false</CODE> otherwise. 01427 */ 01428 bool AppPrefs::write(const int key, const wxString& str) 01429 { 01430 HashPrefs::iterator it = prefs.find(key); 01431 if (it == prefs.end()) 01432 return false; 01433 else 01434 return it->second.set(str); 01435 } 01436 //--------------------------------------------------------------------------- 01437 01438 01439 /** 01440 * Write a long integer value in the preference given by the key. 01441 * 01442 * @param key The key of the preference where the value will be stored. 01443 * @param l The value to store. 01444 * @return <CODE>true</CODE> if the value has been stored successfully, 01445 * <CODE>false</CODE> otherwise. 01446 */ 01447 bool AppPrefs::write(const int key, const long l) 01448 { 01449 HashPrefs::iterator it = prefs.find(key); 01450 if (it == prefs.end()) 01451 return false; 01452 else 01453 return it->second.set(l); 01454 } 01455 //--------------------------------------------------------------------------- 01456 01457 01458 /** 01459 * Write a double value in the preference given by the key. 01460 * 01461 * @param key The key of the preference where the value will be stored. 01462 * @param d The value to store. 01463 * @return <CODE>true</CODE> if the value has been stored successfully, 01464 * <CODE>false</CODE> otherwise. 01465 */ 01466 bool AppPrefs::write(const int key, const double d) 01467 { 01468 HashPrefs::iterator it = prefs.find(key); 01469 if (it == prefs.end()) 01470 return false; 01471 else 01472 return it->second.set(d); 01473 } 01474 //--------------------------------------------------------------------------- 01475 01476 01477 /** 01478 * Write a boolean value in the preference given by the key. 01479 * 01480 * @param key The key of the preference where the value will be stored. 01481 * @param b The value to store. 01482 * @return <CODE>true</CODE> if the value has been stored successfully, 01483 * <CODE>false</CODE> otherwise. 01484 */ 01485 bool AppPrefs::write(const int key, const bool b) 01486 { 01487 HashPrefs::iterator it = prefs.find(key); 01488 if (it == prefs.end()) 01489 return false; 01490 else 01491 return it->second.set(b); 01492 } 01493 //--------------------------------------------------------------------------- 01494 01495 01496 /** 01497 * Write a wxRect value in the preference given by the key. 01498 * 01499 * @param key The key of the preference where the value will be stored. 01500 * @param r The value to store. 01501 * @return <CODE>true</CODE> if the value has been stored successfully, 01502 * <CODE>false</CODE> otherwise. 01503 */ 01504 bool AppPrefs::write(const int key, const wxRect& r) 01505 { 01506 HashPrefs::iterator it = prefs.find(key); 01507 if (it == prefs.end()) 01508 return false; 01509 else 01510 return it->second.set(r); 01511 } 01512 //--------------------------------------------------------------------------- 01513 01514 01515 /** 01516 * Write a wxPoint value in the preference given by the key. 01517 * 01518 * @param key The key of the preference where the value will be stored. 01519 * @param p The value to store. 01520 * @return <CODE>true</CODE> if the value has been stored successfully, 01521 * <CODE>false</CODE> otherwise. 01522 */ 01523 bool AppPrefs::write(const int key, const wxPoint& p) 01524 { 01525 HashPrefs::iterator it = prefs.find(key); 01526 if (it == prefs.end()) 01527 return false; 01528 else 01529 return it->second.set(p); 01530 } 01531 //--------------------------------------------------------------------------- 01532 01533 01534 /** 01535 * Write a wxSize value in the preference given by the key. 01536 * 01537 * @param key The key of the preference where the value will be stored. 01538 * @param s The value to store. 01539 * @return <CODE>true</CODE> if the value has been stored successfully, 01540 * <CODE>false</CODE> otherwise. 01541 */ 01542 bool AppPrefs::write(const int key, const wxSize& s) 01543 { 01544 HashPrefs::iterator it = prefs.find(key); 01545 if (it == prefs.end()) 01546 return false; 01547 else 01548 return it->second.set(s); 01549 } 01550 //--------------------------------------------------------------------------- 01551 01552 01553 /** 01554 * Gets a pointer on the global preferences. 01555 * 01556 * If no global instance of the class AppPrefs exists, a new instance is 01557 * created. 01558 * 01559 * @return A pointer on the global preferences. 01560 */ 01561 AppPrefs* AppPrefs::get() 01562 { 01563 if (global == NULL) 01564 global = new AppPrefs(); 01565 01566 return global; 01567 } 01568 //--------------------------------------------------------------------------- 01569 01570 01571 /** 01572 * Sets the global preferences. 01573 * 01574 * @param pAppPrefs A pointer on the new global preferences. 01575 * @return A pointer on the old global preferences or <CODE>NULL</CODE> if 01576 * there wasn't global preferences. 01577 */ 01578 AppPrefs* AppPrefs::set(AppPrefs* pAppPrefs) 01579 { 01580 AppPrefs* old = global; 01581 global = pAppPrefs; 01582 return old; 01583 } 01584 //--------------------------------------------------------------------------- 01585 01586 01587 /** 01588 * Cleans up the global instance. 01589 * 01590 * Use the <CODE>delete</CODE> operator to delete the memory taken by the 01591 * global instance of <CODE>AppPrefs</CODE>. 01592 * 01593 * It is safe to call this static method if no global preferences have been 01594 * set. 01595 */ 01596 void AppPrefs::cleanupGlobal() 01597 { 01598 AppPrefs* old = set(NULL); 01599 if (old != NULL) 01600 delete old; 01601 } 01602 //--------------------------------------------------------------------------- 01603 01604 01605 /** 01606 * Gets the full name of the directory where the preferences of the application are stored. 01607 * 01608 * @return A string that contains the directory where the preferences of the 01609 * application are stored. 01610 */ 01611 wxString AppPrefs::getConfigDir() 01612 { 01613 if (!configDir.empty()) 01614 return configDir; 01615 01616 wxString path; 01617 wxString configDirName; 01618 #if defined(__UNIX__) && !defined(__WINDOWS__) 01619 configDirName = wxT('.') + getConfigDirName(); 01620 #else 01621 configDirName = getConfigDirName(); 01622 #endif // __UNIX__ 01623 01624 #ifdef __WINDOWS__ 01625 // Try special directories 01626 if (!(path = getFolderPath(CSIDL_COMMON_APPDATA)).empty()) 01627 { 01628 path = addPathSeparatorAtEnd(path) + configDirName; 01629 // Tests if the configuration files are present and readeable/writeable. 01630 if (!(wxFile::Access(addPathSeparatorAtEnd(path) + getConfigFileName(), wxFile::read) && 01631 wxFile::Access(addPathSeparatorAtEnd(path) + getConfigFileName(), wxFile::write))) 01632 path.clear(); 01633 } 01634 01635 if (path.empty()) 01636 { 01637 path = getFolderPath(CSIDL_APPDATA); 01638 if (!dirExists(path, false)) 01639 path.clear(); 01640 else 01641 { 01642 path = addPathSeparatorAtEnd(path) + configDirName; 01643 if (!dirExists(path, true)) 01644 path.clear(); 01645 } 01646 } 01647 01648 if (path.empty()) 01649 { 01650 path = getFolderPath(CSIDL_LOCAL_APPDATA); 01651 if (!dirExists(path, false)) 01652 path.clear(); 01653 else 01654 { 01655 path = addPathSeparatorAtEnd(path) + configDirName; 01656 if (!dirExists(path, true)) 01657 path.clear(); 01658 } 01659 } 01660 #endif 01661 01662 // Try the HOME environnement variable 01663 if (path.empty()) 01664 if (::wxGetEnv(wxT("HOME"), &path)) 01665 { 01666 path = addPathSeparatorAtEnd(path) + configDirName; 01667 if (!dirExists(path, true)) 01668 path.clear(); 01669 } 01670 01671 if (!path.empty()) 01672 configDir = path; 01673 01674 return configDir; 01675 } 01676 //--------------------------------------------------------------------------- 01677 01678 01679 /** 01680 * Gets the name of the file which contains the application's preferences. 01681 * 01682 * @return A string that contains the name of the file which contains the 01683 * application's preferences. 01684 */ 01685 wxString AppPrefs::getConfigFileName() 01686 { 01687 if (configFileName.empty()) 01688 configFileName = wxString(wxT(APP_NAME)) + wxT(".ini"); 01689 01690 return configFileName; 01691 } 01692 //--------------------------------------------------------------------------- 01693 01694 01695 /** 01696 * Gets the name of the file which contains the application's languages 01697 * settings. 01698 * 01699 * @return A string that contains the name of the file which contains the 01700 * application's languages settings. 01701 */ 01702 wxString AppPrefs::getLanguagesFileName() 01703 { 01704 return wxT("languages.ini"); 01705 } 01706 //--------------------------------------------------------------------------- 01707 01708 01709 /** 01710 * Gets the name of the directory which contains the application's preferences. 01711 * 01712 * @return A string that contains the name of the directory which contains the 01713 * application's preferences. 01714 */ 01715 wxString AppPrefs::getConfigDirName() 01716 { 01717 return wxString(wxT(APP_NAME)); 01718 } 01719 //--------------------------------------------------------------------------- 01720 01721 01722 /** 01723 * Gets the full path of the file which contains the application's preferences. 01724 * 01725 * @return A string that contains the full path name of the file which contains 01726 * the application's preferences. 01727 */ 01728 wxString AppPrefs::getConfigPathName() 01729 { 01730 return addPathSeparatorAtEnd(getConfigDir()) + getConfigFileName(); 01731 } 01732 //--------------------------------------------------------------------------- 01733 01734 01735 /** 01736 * Gets the full path of the file which contains the application's languages 01737 * settings. 01738 * 01739 * @return A string that contains the full path name of the file which contains 01740 * the application's languages settings. 01741 */ 01742 wxString AppPrefs::getLanguagesPathName() 01743 { 01744 wxString res; 01745 size_t i; 01746 01747 // Gets the standard paths where the file of application's languages settings 01748 // could be found. 01749 wxString langIni = getLanguagesFileName(); 01750 wxArrayString paths = ::getResourcesPaths(); 01751 01752 // Add the name of the file of the application's languages settings on each 01753 // paths of the possible paths. 01754 for (i = 0; i < paths.GetCount(); i++) 01755 { 01756 wxFileName fn(paths[i], langIni); 01757 paths[i] = fn.GetFullPath(); 01758 } 01759 01760 wxString langsRC; 01761 if (::wxGetEnv(wxT("WXCKSUMS_LANGS_RC"), &langsRC)) 01762 // Adds the full path of the file of the application's languages settings at 01763 // the beginning of the array. 01764 paths.Insert(langsRC, 0); 01765 01766 i = 0; 01767 while (res.IsEmpty() && i < paths.GetCount()) 01768 { 01769 if (wxFile::Access(paths[i], wxFile::read)) 01770 res = paths[i]; 01771 else 01772 i++; 01773 } 01774 01775 return res; 01776 } 01777 //--------------------------------------------------------------------------- 01778 01779 01780 /** 01781 * Gets the default rects of the main window. 01782 * 01783 * @return The default rects of the main window. 01784 */ 01785 wxRect AppPrefs::getDefaultMainWindowRect() 01786 { 01787 int h, w; 01788 wxRect r; 01789 01790 // Sets the default size of the window (5/8 of the screen) 01791 ::wxDisplaySize(&w, &h); 01792 r.SetWidth(static_cast<int>(w * 0.625)); 01793 r.SetHeight(static_cast<int>(h * 0.625)); 01794 r.SetX((w - r.GetWidth()) / 2); 01795 r.SetY((h - r.GetHeight()) / 2); 01796 01797 return r; 01798 } 01799 //--------------------------------------------------------------------------- 01800 01801 01802 /** 01803 * Gets the directory where the user stores its documents. 01804 * 01805 * This method looks for the <CODE>HOME</CODE> environment variable. 01806 * On Windows only it looks for the <CODE>My Documents</CODE> directory. 01807 * 01808 * @return The directory where the user stores its documents or an empty string 01809 * if this directory cannot be found. 01810 */ 01811 wxString AppPrefs::getUserDocumentsDirName() 01812 { 01813 wxString path; 01814 01815 // Try the HOME environnement variable 01816 if (::wxGetEnv(wxT("HOME"), &path)) 01817 if (::wxDirExists(path)) 01818 return path; 01819 01820 #ifdef __WINDOWS__ 01821 path = getFolderPath(CSIDL_PERSONAL); 01822 if (::wxDirExists(path)) 01823 return path; 01824 #endif // __WINDOWS__ 01825 01826 return wxEmptyString; 01827 } 01828 //--------------------------------------------------------------------------- 01829 01830 01831 //########################################################################### 01832 // Useful fonctions 01833 //########################################################################### 01834 01835 /** 01836 * Adds a path separator at the end of the directory name if it's not already 01837 * present. 01838 * 01839 * If the directory name is empty, the path separator isn't added. 01840 * 01841 * @param dirName Name of the directory where the path separator will be added. 01842 * @return The name of the directory with a path separator at the end. 01843 */ 01844 static wxString addPathSeparatorAtEnd(wxString dirName) 01845 { 01846 if (dirName.empty()) 01847 return dirName; 01848 01849 wxChar pathSep = wxFileName::GetPathSeparator(); 01850 if (dirName[dirName.size() - 1] != pathSep) 01851 dirName += pathSep; 01852 01853 return dirName; 01854 } 01855 //--------------------------------------------------------------------------- 01856 01857 01858 /** 01859 * Indicates if the specified directory exists. 01860 * 01861 * @param dirName Name of the directory to test the existance. 01862 * @param create If the directory doesn't exist, try to create it. 01863 * @return <CODE>true</CODE> if the directory exists, 01864 * <CODE>false</CODE> otherwise. 01865 * @note The algorithm of creation of directory of this fonction is dumb. 01866 * It just tries to create the full directory name.<BR> 01867 * For example, if only the <I>C:\\Program Files\\MyApp</I> directory 01868 * exists, and the <I>C:\\Program Files\\MyApp\\Config\\6.0</I> directory 01869 * is tested for existance and creation, this fonction will try to 01870 * create the <I>C:\\Program Files\\MyApp\\Config\\6.0</I> directory and 01871 * not the <I>C:\\Program Files\\MyApp\\Config</I> directory and next 01872 * <I>C:\\Program Files\\MyApp\\Config\\6.0</I>. 01873 */ 01874 static bool dirExists(const wxString& dirName, bool create) 01875 { 01876 if (wxDirExists(dirName)) 01877 return true; 01878 else 01879 if (create) 01880 if (wxFileExists(dirName)) 01881 return false; 01882 else 01883 return wxMkdir(dirName); 01884 else 01885 return false; 01886 } 01887 //--------------------------------------------------------------------------- 01888 01889 01890 #ifdef __WINDOWS__ 01891 /** 01892 * Takes the CSIDL of a folder and returns the pathname (Windows only). 01893 * 01894 * @param folder A CSIDL value that identifies the folder whose path is to be 01895 * retrieved. Only real folders are valid. If a virtual folder 01896 * is specified, this function will fail. 01897 * @return A string which contains the pathname on success. An empty string 01898 * otherwise. 01899 */ 01900 static wxString getFolderPath(int folder) 01901 { 01902 TCHAR szPath[MAX_PATH]; 01903 wxString res; 01904 01905 if (SUCCEEDED(SHGetFolderPath(NULL, folder, NULL, SHGFP_TYPE_CURRENT, szPath))) 01906 res = szPath; 01907 01908 return res; 01909 } 01910 //--------------------------------------------------------------------------- 01911 #endif // __WINDOWS__

Generated on Sun May 30 13:37:43 2004 for wxChecksums by doxygen 1.3.7