// preferences.cpp -- management of general preferences // // Written by Frederic Bouvier, started March 2002. // // Copyright (C) 2002 Frederic Bouvier - fredb@users.sourceforge.net // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License as // published by the Free Software Foundation; either version 2 of the // License, or (at your option) any later version. // // This program is distributed in the hope that it will be useful, but // WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. // // $Id: preferences.cpp,v 1.18 2005/06/25 16:31:14 fredb2 Exp $ #ifdef _MSC_VER #pragma warning( disable : 4786 ) #endif #ifdef HAVE_CONFIG_H #include "config.h" #endif #include #define PREF_FILE_NAME ".fgsdrc" #include "preferences.hpp" #include #include FGSD_Preferences::FGSD_Preferences() : _historyLength( 4 ) , _zoomLevel( 0 ) , _showOutline( false ) , _showLevelCurve( false ) , _showTiles( false ) , _showStatic( true ) , _showTileFrames( true ) , _showConstraints( false ) , _showVertices( false ) , _showInfiniteEdges( false ) , _alphaValue( 0.0 ) , _airportNumber( 1 ) , _viewMode( "Normal" ) { } FGSD_Preferences::~FGSD_Preferences() { } void FGSD_Preferences::load() { std::string fileName; char *home = getenv( "HOME" ); if ( home != 0 ) { fileName = home; if ( fileName[ fileName.size() - 1 ] != '/' ) { fileName += '/'; } } fileName += PREF_FILE_NAME; struct stat buffer; if ( stat( fileName.c_str(), &buffer ) == 0 ) { SGPropertyNode prefs; readProperties( fileName, &prefs ); SGPropertyNode *hnode = prefs.getChild( "history" ); if ( hnode ) { SGPropertyNode *node = hnode->getChild( "length" ); if ( node ) { _historyLength = node->getIntValue(); } int n = 1; for ( size_t i = 1; i <= _historyLength; ++i ) { node = hnode->getChild( "file", n ); if ( node ) { std::string file = node->getStringValue(); _history.push_back( file ); } ++n; } } SGPropertyNode *pnode = prefs.getChild( "output" ); if ( pnode ) _outputPath = pnode->getStringValue(); pnode = prefs.getChild( "fg-root" ); if ( pnode ) _fgRootPath = pnode->getStringValue(); pnode = prefs.getChild( "fg-scenery" ); if ( pnode ) _fgSceneryPath = pnode->getStringValue(); pnode = prefs.getChild( "fg-cmd-line" ); if ( pnode ) _fgCommandLine = pnode->getStringValue(); pnode = prefs.getChild( "zoom-level" ); if ( pnode ) _zoomLevel = pnode->getIntValue(); pnode = prefs.getChild( "show-outline" ); if ( pnode ) _showOutline = pnode->getBoolValue(); pnode = prefs.getChild( "show-level-curve" ); if ( pnode ) _showLevelCurve = pnode->getBoolValue(); pnode = prefs.getChild( "show-tiles" ); if ( pnode ) _showTiles = pnode->getBoolValue(); pnode = prefs.getChild( "show-static" ); if ( pnode ) _showStatic = pnode->getBoolValue(); pnode = prefs.getChild( "show-tile-frames" ); if ( pnode ) _showTileFrames = pnode->getBoolValue(); pnode = prefs.getChild( "view-mode" ); if ( pnode ) _viewMode = pnode->getStringValue(); pnode = prefs.getChild( "alpha-value" ); if ( pnode ) _alphaValue = pnode->getDoubleValue(); pnode = prefs.getChild( "airport-number" ); if ( pnode ) _airportNumber = pnode->getIntValue(); pnode = prefs.getChild( "show-constraints" ); if ( pnode ) _showConstraints = pnode->getBoolValue(); pnode = prefs.getChild( "show-vertices" ); if ( pnode ) _showVertices = pnode->getBoolValue(); pnode = prefs.getChild( "show-infinite-edges" ); if ( pnode ) _showInfiniteEdges = pnode->getBoolValue(); pnode = prefs.getChild( "current-main-dir" ); if ( pnode ) _currentMainDirectory = pnode->getStringValue(); pnode = prefs.getChild( "current-input-dir" ); if ( pnode ) _currentInputDirectory = pnode->getStringValue(); } } void FGSD_Preferences::save() { std::string fileName; char *home = getenv( "HOME" ); if ( home != 0 ) { fileName = home; if ( fileName[ fileName.size() - 1 ] != '/' ) { fileName += '/'; } } fileName += PREF_FILE_NAME; SGPropertyNode prefs; const int attrRWA = SGPropertyNode::READ | SGPropertyNode::WRITE | SGPropertyNode::ARCHIVE; SGPropertyNode *hnode = prefs.getChild( "history", 0, true ); hnode->setAttributes( attrRWA ); SGPropertyNode *node = hnode->getChild( "length", 0, true ); node->setAttributes( attrRWA ); node->setIntValue( _historyLength ); int n = 1; for ( FileList::iterator i = _history.begin(); i != _history.end(); ++i ) { node = hnode->getChild( "file", n, true ); node->setAttributes( attrRWA ); node->setStringValue( (*i).c_str() ); n += 1; } SGPropertyNode *pnode = prefs.getChild( "output", 0, true ); pnode->setAttributes( attrRWA ); pnode->setStringValue( _outputPath.c_str() ); pnode = prefs.getChild( "fg-root", 0, true ); pnode->setAttributes( attrRWA ); pnode->setStringValue( _fgRootPath.c_str() ); pnode = prefs.getChild( "fg-scenery", 0, true ); pnode->setAttributes( attrRWA ); pnode->setStringValue( _fgSceneryPath.c_str() ); pnode = prefs.getChild( "fg-cmd-line", 0, true ); pnode->setAttributes( attrRWA ); pnode->setStringValue( _fgCommandLine.c_str() ); pnode = prefs.getChild( "zoom-level", 0, true ); pnode->setAttributes( attrRWA ); pnode->setIntValue( _zoomLevel ); pnode = prefs.getChild( "show-outline", 0, true ); pnode->setAttributes( attrRWA ); pnode->setBoolValue( _showOutline ); pnode = prefs.getChild( "show-level-curve", 0, true ); pnode->setAttributes( attrRWA ); pnode->setBoolValue( _showLevelCurve ); pnode = prefs.getChild( "show-tiles", 0, true ); pnode->setAttributes( attrRWA ); pnode->setBoolValue( _showTiles ); pnode = prefs.getChild( "show-static", 0, true ); pnode->setAttributes( attrRWA ); pnode->setBoolValue( _showStatic ); pnode = prefs.getChild( "show-tile-frames", 0, true ); pnode->setAttributes( attrRWA ); pnode->setBoolValue( _showTileFrames ); pnode = prefs.getChild( "view-mode", 0, true ); pnode->setAttributes( attrRWA ); pnode->setStringValue( _viewMode.c_str() ); pnode = prefs.getChild( "alpha-value", 0, true ); pnode->setAttributes( attrRWA ); pnode->setDoubleValue( _alphaValue ); pnode = prefs.getChild( "airport-number", 0, true ); pnode->setAttributes( attrRWA ); pnode->setIntValue( _airportNumber ); pnode = prefs.getChild( "show-constraints", 0, true ); pnode->setAttributes( attrRWA ); pnode->setBoolValue( _showConstraints ); pnode = prefs.getChild( "show-vertices", 0, true ); pnode->setAttributes( attrRWA ); pnode->setBoolValue( _showVertices ); pnode = prefs.getChild( "show-infinite-edges", 0, true ); pnode->setAttributes( attrRWA ); pnode->setBoolValue( _showInfiniteEdges ); pnode = prefs.getChild( "current-main-dir", 0, true ); pnode->setAttributes( attrRWA ); pnode->setStringValue( _currentMainDirectory.c_str() ); pnode = prefs.getChild( "current-input-dir", 0, true ); pnode->setAttributes( attrRWA ); pnode->setStringValue( _currentInputDirectory.c_str() ); writeProperties( fileName, &prefs ); } void FGSD_Preferences::historyLength( size_t __len ) { _historyLength = __len; while ( _history.size() >= _historyLength ) { _history.pop_back(); } } std::string FGSD_Preferences::getFileFromHistory( size_t n ) const { std::string ret; FileList::const_iterator i = _history.begin(); while ( i != _history.end() && n ) { ++i; --n; } if ( i != _history.end() ) { ret = *i; } return ret; } void FGSD_Preferences::addFileToHistory( const std::string &__file ) { FileList::iterator pos = std::find( _history.begin(), _history.end(), __file ); if ( pos != _history.end() ) { _history.erase( pos ); } else { while ( _history.size() >= _historyLength ) { _history.pop_back(); } } _history.push_front( __file ); }