Main Page | Class List | File List | Class Members | Related Pages

profilemanager.cpp

00001 /***************************************************************************
00002                            profilemanager.cpp
00003                            -------------------
00004     begin                : 2004-09-22 12:42
00005     copyright            : (C) 2004-2005 by Dominik Haumann
00006     email                : dhaumann@users.sourceforge.net
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  This program is free software; you can redistribute it and/or
00011  modify it under the terms of the GNU General Public License
00012  as published by the Free Software Foundation; either version 2
00013  of the License, or (at your option) any later version.
00014 
00015  This program is distributed in the hope that it will be useful,
00016  but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  GNU General Public License for more details.
00019 
00020  You should have received a copy of the GNU General Public License
00021  along with this program; if not, write to the Free Software
00022  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00023  ***************************************************************************/
00024 
00025 #include "profilemanager.h"
00026 
00027 extern ofstream logFile;
00028 
00029 namespace WDS
00030 {
00031 
00032 // Statische Variablen initialiseren
00033 CProfile* CProfileManager::m_pCurrentProfile = NULL;
00034 
00035 //-------------------------------------------------------------------------
00036 //BEGIN class CProfile
00037 //-------------------------------------------------------------------------
00038 CProfile::CProfile() :
00039     m_name     (""),
00040     m_map      (""),
00041     m_dat      (""),
00042     m_usephpmap(false),
00043     m_user     (""),
00044     m_password (""),
00045     m_server   (""),
00046     m_timestamp(1),
00047     m_fogofwar (false),
00048     m_dirty    (false),
00049     m_last_scroll_x(0),
00050     m_last_scroll_y(0),
00051     m_last_zoom(1.0f)
00052 {
00053     m_bookmarks.clear();
00054     m_recents.clear();
00055 }
00056 
00057 CProfile::~CProfile()
00058 {
00059 }
00060 
00061 void CProfile::SetName(const wxString& name)
00062 {
00063     m_name = name;
00064 }
00065 
00066 wxString CProfile::GetName() const
00067 {
00068     return m_name;
00069 }
00070 
00071 void CProfile::SetMap(const wxString& strMap )
00072 {
00073     m_map = strMap;
00074 }
00075 
00076 wxString CProfile::GetMap() const
00077 {
00078     return m_map;
00079 }
00080 
00081 void CProfile::SetDat(const wxString& dat)
00082 {
00083     m_dat = dat;
00084 }
00085 
00086 wxString CProfile::GetDat() const
00087 {
00088     return m_dat;
00089 }
00090 
00091 bool CProfile::IsValid() const
00092 {
00093     return m_map != "";
00094 }
00095 
00096 void CProfile::SetUser(const wxString& user)
00097 {
00098     m_user = user;
00099 }
00100 
00101 wxString CProfile::GetUser() const
00102 {
00103     return m_user;
00104 }
00105 
00106 void CProfile::SetPassword(const wxString& pw)
00107 {
00108     m_password = pw;
00109 }
00110 
00111 wxString CProfile::GetPassword() const
00112 {
00113     return m_password;
00114 }
00115 
00116 void CProfile::SetServer(const wxString& server)
00117 {
00118     m_server = server;
00119 }
00120 
00121 wxString CProfile::GetServer() const
00122 {
00123     return m_server;
00124 }
00125 
00126 void CProfile::SetUsePhpmap(bool b_use)
00127 {
00128     m_usephpmap = b_use;
00129 }
00130 
00131 bool CProfile::GetUsePhpmap() const
00132 {
00133     return m_usephpmap;
00134 }
00135 
00136 void CProfile::SetFogOfWar(bool fow)
00137 {
00138     m_fogofwar = fow;
00139 }
00140 
00141 bool CProfile::GetFogOfWar() const
00142 {
00143     return m_fogofwar;
00144 }
00145 
00146 void CProfile::SetTimestamp(int ts)
00147 {
00148     m_timestamp = ts;
00149 }
00150 
00151 int CProfile::GetTimestamp() const
00152 {
00153     return m_timestamp;
00154 }
00155 
00156 void CProfile::SetDirty(bool dirty)
00157 {
00158     m_dirty = dirty;
00159 }
00160 
00161 bool CProfile::GetDirty() const
00162 {
00163     return m_dirty;
00164 }
00165 
00166 GoToArray& CProfile::GetBookmarks()
00167 {
00168     return m_bookmarks;
00169 }
00170 
00171 GoToArray& CProfile::GetRecentGoTos()
00172 {
00173     return m_recents;
00174 }
00175 
00176 void CProfile::AddBookmark( int x, int y, const wxString& comment )
00177 {
00178     CGoToEntry entry ( x, y, comment );
00179 
00180     GoToArray::iterator it = m_bookmarks.begin();
00181 
00182     for ( ; it != m_bookmarks.end(); it++ ) {
00183         if ( it->x == x && it->y == y ) {
00184             m_bookmarks.erase( it );
00185             break;
00186         }
00187     }
00188 
00189     m_bookmarks.push_back( entry );
00190 }
00191 
00192 void CProfile::AddRecentGoTo( int x, int y )
00193 {
00194     CGoToEntry entry ( x, y );
00195 
00196     GoToArray::iterator it = m_recents.begin();
00197 
00198     for ( ; it != m_recents.end(); it++ ) {
00199         if ( it->x == x && it->y == y ) {
00200             m_recents.erase( it );
00201             break;
00202         }
00203     }
00204 
00205     m_recents.push_back( entry );
00206 
00207     // limit to 20 entries
00208     if ( m_recents.size() > 20 ) {
00209         m_recents.erase( m_recents.begin() );
00210     }
00211 }
00212 
00213 void CProfile::Load( wxFileConfig *pConfig )
00214 {
00215     SetName( pConfig->Read(wxString("name"), wxString("unknown")) );
00216     SetMap ( pConfig->Read(wxString("mapfile"), wxString("")) );
00217     SetDat ( pConfig->Read(wxString("datfile"), wxString("")) );
00218 
00219     SetUsePhpmap( pConfig->Read(wxString("use_phpmap" ), 0L) );
00220     SetServer   ( pConfig->Read(wxString("server" ), wxString("")) );
00221     SetUser     ( pConfig->Read(wxString("user" ), wxString("")) );
00222     SetPassword ( pConfig->Read(wxString("password" ), wxString("")) );
00223     SetFogOfWar ( pConfig->Read(wxString("fogofwar" ), 1L) );
00224     SetTimestamp( pConfig->Read(wxString("timestamp" ), 0L) );
00225 
00226     SetLastScrollPosition( pConfig->Read(wxString("last_scroll_x" ), 0L),
00227                            pConfig->Read(wxString("last_scroll_y" ), 0L) );
00228     SetLastZoom ( ( (float)pConfig->Read(wxString("last_zoom" ), 100L) ) / 100.0f );
00229 
00230     LoadBookmarks( pConfig );
00231     LoadRecents( pConfig );
00232 }
00233 
00234 void CProfile::Save( wxFileConfig *pConfig )
00235 {
00236     pConfig->Write( wxString("name"   ), GetName() );
00237     pConfig->Write( wxString("mapfile"), GetMap() );
00238     pConfig->Write( wxString("datfile"), GetDat() );
00239 
00240     pConfig->Write( wxString("use_phpmap"), long(GetUsePhpmap()) );
00241     pConfig->Write( wxString("server"    ), GetServer() );
00242     pConfig->Write( wxString("user"      ), GetUser() );
00243     pConfig->Write( wxString("password"  ), GetPassword() );
00244     pConfig->Write( wxString("fogofwar"  ), GetFogOfWar() );
00245     pConfig->Write( wxString("timestamp" ), GetTimestamp() );
00246 
00247     pConfig->Write( wxString("last_scroll_x" ), GetLastScrollPositionX() );
00248     pConfig->Write( wxString("last_scroll_y" ), GetLastScrollPositionY() );
00249 
00250     pConfig->Write( wxString("last_zoom" ), int(GetLastZoom()*100.0) );
00251 
00252     SaveBookmarks( pConfig );
00253     SaveRecents( pConfig );
00254 }
00255 
00256 void CProfile::LoadBookmarks( wxFileConfig *pConfig )
00257 {
00258     wxString value = pConfig->Read( wxString("bookmarks" ), wxString("") );
00259 
00260     while ( value.length() > 0 ) {
00261         CGoToEntry entry;
00262 
00263         value.BeforeFirst(':').ToLong( (long int*)&entry.x );
00264         value = value.AfterFirst(':');
00265         value.BeforeFirst(';').ToLong( (long int*)&entry.y );
00266         value = value.AfterFirst(';');
00267         entry.comment = value.BeforeFirst('\n');
00268         value = value.AfterFirst('\n');
00269 
00270         m_bookmarks.push_back( entry );
00271     }
00272 }
00273 
00274 void CProfile::LoadRecents( wxFileConfig *pConfig )
00275 {
00276     wxString value = pConfig->Read( wxString("recents" ), wxString("") );
00277 
00278     while ( value.length() > 0 ) {
00279         CGoToEntry entry;
00280 
00281         value.BeforeFirst(':').ToLong( (long int*)&entry.x );
00282         value = value.AfterFirst(':');
00283         value.BeforeFirst(';').ToLong( (long int*)&entry.y );
00284         value = value.AfterFirst(';');
00285 
00286         m_recents.push_back( entry );
00287     }
00288 }
00289 
00290 void CProfile::SaveBookmarks( wxFileConfig *pConfig )
00291 {
00292     wxString tmp;
00293     GoToArray::iterator it = m_bookmarks.begin();
00294 
00295     while ( it != m_bookmarks.end() ) {
00296         tmp += wxString::Format("%d:%d;", it->x, it->y ) + it->comment.c_str() + "\n";
00297         ++it;
00298     }
00299 
00300     pConfig->Write( wxString("bookmarks"), tmp );
00301 }
00302 
00303 void CProfile::SaveRecents( wxFileConfig *pConfig )
00304 {
00305     wxString tmp;
00306     GoToArray::iterator it = m_recents.begin();
00307 
00308     while ( it != m_recents.end() ) {
00309         tmp += wxString::Format("%d:%d;", it->x, it->y );
00310         ++it;
00311     }
00312 
00313     pConfig->Write( wxString("recents"), tmp );
00314 }
00315 
00316 void CProfile::SetLastScrollPosition(int x, int y)
00317 {
00318     m_last_scroll_x = x;
00319     m_last_scroll_y = y;
00320 }
00321 
00322 int CProfile::GetLastScrollPositionX() const
00323 {
00324     return m_last_scroll_x;
00325 }
00326 
00327 int CProfile::GetLastScrollPositionY() const
00328 {
00329     return m_last_scroll_y;
00330 }
00331 
00332 void CProfile::SetLastZoom( float zoom )
00333 {
00334     m_last_zoom = zoom;
00335 }
00336 
00337 float CProfile::GetLastZoom( ) const
00338 {
00339     return m_last_zoom;
00340 }
00341 //-------------------------------------------------------------------------
00342 //END class CProfile
00343 //-------------------------------------------------------------------------
00344 
00345 //-------------------------------------------------------------------------
00346 //BEGIN class CProfileManager
00347 //-------------------------------------------------------------------------
00348 CProfileManager::CProfileManager(wxFileConfig* pConfig)
00349     : m_pConfig(pConfig)
00350 {
00351     m_pCurrentProfile = NULL;
00352 
00353     Init();
00354 }
00355 
00356 CProfileManager::~CProfileManager()
00357 {
00358     vector <CProfile*>::iterator it = m_profiles.begin();
00359     for ( ; it != m_profiles.end(); it++)
00360     {
00361         delete (*it);
00362     }
00363 
00364     m_profiles.clear();
00365 }
00366 
00367 void CProfileManager::Init()
00368 {
00369     int nProfiles, nLastProfile;
00370 
00371     // load all profiles
00372     m_pConfig->SetPath("/Profile");
00373     m_pConfig->Read( "profiles", &nProfiles, 0);
00374     m_pConfig->Read( "last_profile", &nLastProfile, 0);
00375 
00376     // read all profiles
00377     for (int i = 0; i < nProfiles; ++i)
00378     {
00379         wxString section("/Profile/");
00380         section << i;
00381         m_pConfig->SetPath( section );
00382 
00383         CProfile *p = new CProfile();
00384         p->Load( m_pConfig );
00385         m_profiles.push_back( p );
00386     }
00387 
00388     // Letzte Profil als aktuelles setzen
00389     CProfile *pProfile = GetProfile( nLastProfile );
00390 
00391     // Profil als aktuelles Profil setzen
00392     SetCurrentProfile( pProfile );
00393 }
00394 
00395 void CProfileManager::save()
00396 {
00397     // save number of profiles
00398     m_pConfig->SetPath( "/Profile" );
00399     m_pConfig->Write( "profiles",     long(m_profiles.size()) );
00400     m_pConfig->Write( "last_profile", long(GetIndexFromProfile( m_pCurrentProfile )) );
00401 
00402     // save all profiles
00403     vector <CProfile*>::iterator it = m_profiles.begin();
00404     int i = 0;
00405     for ( ; it != m_profiles.end(); it++)
00406     {
00407         wxString section("/Profile/");
00408         section << i++;
00409         m_pConfig->SetPath( section );
00410 
00411         (*it)->Save( m_pConfig );
00412     }
00413 }
00414 
00415 int CProfileManager::size() const
00416 {
00417     return m_profiles.size();
00418 }
00419 
00420 CProfile* CProfileManager::GetProfile (int index) const
00421 {
00422     if (index >= int(m_profiles.size()) || index < 0)
00423         return 0L;
00424     else
00425         return m_profiles[index];
00426 }
00427 
00428 int CProfileManager::GetIndexFromProfile(const CProfile* profile)
00429 {
00430     if ( !profile )
00431         return -1;
00432 
00433     vector <CProfile*>::iterator it = m_profiles.begin();
00434     int index = 0;
00435 
00436     for ( ; it != m_profiles.end(); it++)
00437     {
00438         if ((*it) == profile)
00439             return index;
00440         ++index;
00441     }
00442 
00443     return -1;
00444 }
00445 
00446 bool CProfileManager::DeleteProfile (int index)
00447 {
00448     if (index >= int(m_profiles.size())) return false;
00449 
00450     CProfile* p = m_profiles[index];
00451     vector <CProfile*>::iterator result = find(m_profiles.begin(), m_profiles.end(), p);
00452 
00453     m_profiles.erase(result);
00454     delete p;
00455     p = 0;
00456 
00457     return true;
00458 }
00459 
00460 bool CProfileManager::DeleteProfile (CProfile* p)
00461 {
00462     vector <CProfile*>::iterator result = find(m_profiles.begin(), m_profiles.end(), p);
00463 
00464     if (result == m_profiles.end()) return false;
00465 
00466     m_profiles.erase(result);
00467     delete p;
00468     p = 0;
00469 
00470     return true;
00471 }
00472 
00473 bool CProfileManager::AddProfile (CProfile* p)
00474 {
00475     if (find(m_profiles.begin(), m_profiles.end(), p) == m_profiles.end()) {
00476         m_profiles.push_back(p);
00477         return true;
00478     } else {
00479         return false; // profile already exists
00480     }
00481 }
00482 //-------------------------------------------------------------------------
00483 //END class CProfileManager
00484 //-------------------------------------------------------------------------
00485 
00486 }
00487 // kate: space-indent off; tab-width 4; replace-tabs off;

Generated on Sun Jan 16 18:20:26 2005 for WDSMap by  doxygen 1.3.9.1