source: eviacam/trunk/fuentes/wxutil/configmanager.cpp @ 567

Last change on this file since 567 was 567, checked in by mabarracus, 4 years ago

updated sources to version 2.0.3

  • Property svn:executable set to *
File size: 6.4 KB
Line 
1/////////////////////////////////////////////////////////////////////////////
2// Name:        configmanager.cpp
3// Purpose: 
4// Author:      Cesar Mauri Loba (cesar at crea-si dot com)
5// Modified by:
6// Created:     
7// Copyright:   (C) 2009 Cesar Mauri Loba - CREA Software Systems
8//
9//  This program is free software: you can redistribute it and/or modify
10//  it under the terms of the GNU General Public License as published by
11//  the Free Software Foundation, either version 3 of the License, or
12//  (at your option) any later version.
13//
14//  This program is distributed in the hope that it will be useful,
15//  but WITHOUT ANY WARRANTY; without even the implied warranty of
16//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17//  GNU General Public License for more details.
18//
19//  You should have received a copy of the GNU General Public License
20//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
21/////////////////////////////////////////////////////////////////////////////
22
23#include "configmanager.h"
24
25#include <wx/intl.h>
26#include <wx/arrstr.h>
27
28#define DEFAULT_PROFILE_KEY _T("currentProfile")
29#define DEFAULT_PROFILE_GROUP_KEY _T("default")
30#define DEFAULT_PROFILE_NAME _("default")
31#define LANGUAGE_KEY _T("language")
32#define BASE_PATH _T("/settings")
33
34CConfigManager::CConfigManager(CConfigBase* configRoot)
35{
36        assert (configRoot);
37
38        m_configRoot= configRoot;
39        m_currentProfile= DEFAULT_PROFILE_GROUP_KEY;
40        wxConfigBase::Get()->SetPath (BASE_PATH);
41        assert (wxConfigBase::Get()->GetPath().Cmp(BASE_PATH)== 0);
42}
43
44int CConfigManager::ReadLanguage()
45{
46        wxString language;
47        const wxLanguageInfo *info= NULL;
48
49        assert (wxConfigBase::Get()->GetPath().Cmp(BASE_PATH)== 0);
50
51        // Load language name
52        wxConfigBase::Get()->Read(LANGUAGE_KEY, &language);
53        if (language.Length()> 0)
54        {
55                // Check if valid
56                info= wxLocale::FindLanguageInfo (language);
57                if (info)
58                {
59                        // Ok
60                        return info->Language;
61                }               
62        }
63       
64        return wxLANGUAGE_DEFAULT;
65}
66
67void CConfigManager::WriteLanguage (const int id)
68{
69        assert (wxConfigBase::Get()->GetPath().Cmp(BASE_PATH)== 0);
70
71        // Assumes a valid language id
72        if (id== wxLANGUAGE_DEFAULT)
73        {
74                wxConfigBase::Get()->DeleteEntry (LANGUAGE_KEY);
75        }
76        else
77        {
78                const wxLanguageInfo *info= wxLocale::GetLanguageInfo(id);
79                assert (info);
80                wxConfigBase::Get()->Write (LANGUAGE_KEY, info->Description);
81        }
82}
83
84void CConfigManager::ReadAll ()
85{
86        wxConfigBase* pConfObj= wxConfigBase::Get();
87       
88        pConfObj->Read(DEFAULT_PROFILE_KEY, &m_currentProfile); 
89
90        // If current profile doesn't exists set to default
91        if (!pConfObj->HasGroup(m_currentProfile))
92                m_currentProfile= DEFAULT_PROFILE_GROUP_KEY;
93       
94        // App wide data
95        m_configRoot->ReadAppData (pConfObj);
96
97        // Profile related data
98        if (m_currentProfile.Length()== 0)
99                pConfObj->SetPath (DEFAULT_PROFILE_GROUP_KEY);
100        else
101                pConfObj->SetPath (m_currentProfile);
102       
103        m_configRoot->ReadProfileData (pConfObj);
104
105        pConfObj->SetPath (_T(".."));
106
107        assert (wxConfigBase::Get()->GetPath().Cmp(BASE_PATH)== 0);
108}
109
110void CConfigManager::WriteAll()
111{
112        assert (wxConfigBase::Get()->GetPath().Cmp(BASE_PATH)== 0);
113
114        wxConfigBase* pConfObj= wxConfigBase::Get();
115       
116        // App wide data
117        pConfObj->Write(DEFAULT_PROFILE_KEY, m_currentProfile);
118        m_configRoot->WriteAppData (pConfObj);
119
120        // Profile related data
121        if (m_currentProfile.Length()== 0)
122                pConfObj->SetPath (DEFAULT_PROFILE_GROUP_KEY);
123        else
124                pConfObj->SetPath (m_currentProfile);
125       
126        m_configRoot->WriteProfileData (pConfObj);
127
128        pConfObj->SetPath (_T(".."));
129       
130        pConfObj->Flush();
131
132        assert (wxConfigBase::Get()->GetPath().Cmp(BASE_PATH)== 0);
133}
134
135int CConfigManager::GetCurrentProfile() const
136{
137        if (m_currentProfile== DEFAULT_PROFILE_GROUP_KEY) return 0;
138        wxArrayString pList= GetProfileList ();
139        int index= pList.Index (m_currentProfile);
140
141        assert (index> 0 && index!= wxNOT_FOUND);
142        return index;
143}
144
145void CConfigManager::ChangeCurrentProfile(int index)
146{
147        assert (wxConfigBase::Get()->GetPath().Cmp(BASE_PATH)== 0);
148        if (index!= GetCurrentProfile())
149        {
150                if (index== 0) m_currentProfile= DEFAULT_PROFILE_GROUP_KEY;
151                else
152                {       
153                        wxArrayString pList= GetProfileList ();
154
155                        assert (index>0 && index< (int) pList.GetCount());
156
157                        m_currentProfile= pList[index];
158                }
159
160                // Save new profile before reading again
161                wxConfigBase* pConfObj= wxConfigBase::Get();   
162                pConfObj->Write(DEFAULT_PROFILE_KEY, m_currentProfile);
163                pConfObj->Flush();
164
165                ReadAll ();             
166        }
167        assert (wxConfigBase::Get()->GetPath().Cmp(BASE_PATH)== 0);
168}
169
170// Return 0 is ok, 1 if profile exists or 2 if profile name is invalid
171int CConfigManager::AddNewProfile(const wxString& name)
172{
173        assert (wxConfigBase::Get()->GetPath().Cmp(BASE_PATH)== 0);
174        if (name== DEFAULT_PROFILE_GROUP_KEY) return 2;
175        if (name== DEFAULT_PROFILE_KEY) return 2;
176        if (name== DEFAULT_PROFILE_NAME) return 2;
177        if (name.Contains(_T("/"))) return 2;
178        if (name.Contains(_T("\\"))) return 2;
179       
180        // Check existence
181        wxConfigBase* pConfObj= wxConfigBase::Get();
182        if (pConfObj->HasGroup(name)) return 1;
183       
184        // Add the new profile and change to
185        m_currentProfile= name;
186
187        WriteAll();
188
189        assert (wxConfigBase::Get()->GetPath().Cmp(BASE_PATH)== 0);
190        return 0;
191}
192
193void CConfigManager::DeleteCurrentProfile()
194{
195        assert (wxConfigBase::Get()->GetPath().Cmp(BASE_PATH)== 0);
196        // Default profile deletion not allowed
197        if (m_currentProfile!= DEFAULT_PROFILE_GROUP_KEY)
198        {
199                wxString oldProfile= m_currentProfile;
200
201                // Switch to default profile
202                ChangeCurrentProfile(0);
203
204                // Delete old profile
205                wxConfigBase* pConfObj= wxConfigBase::Get();
206                pConfObj->DeleteGroup(oldProfile);
207                pConfObj->Flush();
208        }       
209        assert (wxConfigBase::Get()->GetPath().Cmp(BASE_PATH)== 0);
210}
211
212wxArrayString CConfigManager::GetProfileList () const
213{
214        wxArrayString pList;
215        wxConfigBase* pConfObj= wxConfigBase::Get();
216        wxString str;
217        long dummy;
218        assert (wxConfigBase::Get()->GetPath().Cmp(BASE_PATH)== 0);
219
220        // Add default profile name (is always id=0)
221        pList.Add (DEFAULT_PROFILE_NAME);
222
223        bool bCont = pConfObj->GetFirstGroup(str, dummy);
224        while (bCont) {
225                // FIXME: this condition ensures that:
226                //  1) the default entry is not already added to the list
227                //  2) another entry in the parent directory called "wxWidgets"
228                //     is not added to the list. This only happens with wxConfigFile
229                //     and it looks like a wxConfigFile bug
230                if (str!= DEFAULT_PROFILE_GROUP_KEY && str!= _T("wxWidgets")) 
231                        pList.Add (str);       
232                bCont = pConfObj->GetNextGroup(str, dummy);
233        }
234        assert (wxConfigBase::Get()->GetPath().Cmp(BASE_PATH)== 0);
235        return pList;
236}
Note: See TracBrowser for help on using the repository browser.