1 //=============================================================================
2 //
3 // File : KviIdentityProfileSet.cpp
4 // Creation date : Thu Dec 30 2010 15:54:48 by Elvio Basello
5 //
6 // This file is part of the KVIrc IRC client distribution
7 // Copyright (C) 2010 Elvio Basello (hellvis69 at netsons dot org)
8 //
9 // This program is FREE software. You can redistribute it and/or
10 // modify it under the terms of the GNU General Public License
11 // as published by the Free Software Foundation; either version 2
12 // of the License, or (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.
17 // See the 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, write to the Free Software Foundation,
21 // Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 //
23 //=============================================================================
24
25 #include "KviIdentityProfileSet.h"
26 #include "KviConfigurationFile.h"
27
28 KviIdentityProfileSet * KviIdentityProfileSet::m_pSelf = nullptr;
29 unsigned int KviIdentityProfileSet::m_uCount = 0;
30
KviIdentityProfileSet()31 KviIdentityProfileSet::KviIdentityProfileSet()
32 : KviHeapObject()
33 {
34 m_bEnabled = false;
35 m_pProfiles = nullptr;
36 }
37
KviIdentityProfileSet(const KviIdentityProfileSet & set)38 KviIdentityProfileSet::KviIdentityProfileSet(const KviIdentityProfileSet & set)
39 {
40 m_pProfiles = nullptr;
41 copyFrom(set);
42 }
43
~KviIdentityProfileSet()44 KviIdentityProfileSet::~KviIdentityProfileSet()
45 {
46 if(m_pProfiles)
47 delete m_pProfiles;
48 }
49
init()50 void KviIdentityProfileSet::init()
51 {
52 if((!m_pSelf) && (m_pSelf->count() == 0))
53 {
54 m_pSelf = new KviIdentityProfileSet();
55 m_uCount++;
56 }
57 }
58
done()59 void KviIdentityProfileSet::done()
60 {
61 m_uCount--;
62 if(m_pSelf->count() == 0)
63 delete m_pSelf;
64 }
65
clear()66 void KviIdentityProfileSet::clear()
67 {
68 if(m_pProfiles)
69 {
70 delete m_pProfiles;
71 m_pProfiles = nullptr;
72 }
73 m_bEnabled = false;
74 }
75
findName(const QString & szName)76 KviIdentityProfile * KviIdentityProfileSet::findName(const QString & szName)
77 {
78 if(!m_pProfiles)
79 return nullptr;
80
81 KviIdentityProfile * pProfile;
82 for(pProfile = m_pProfiles->first(); pProfile; pProfile = m_pProfiles->next())
83 {
84 if(KviQString::matchString(pProfile->name(), szName, false, true))
85 return pProfile;
86 }
87
88 return nullptr;
89 }
90
findNetwork(const QString & szNetwork)91 KviIdentityProfile * KviIdentityProfileSet::findNetwork(const QString & szNetwork)
92 {
93 if(!m_pProfiles)
94 return nullptr;
95
96 KviIdentityProfile * pProfile;
97 for(pProfile = m_pProfiles->first(); pProfile; pProfile = m_pProfiles->next())
98 {
99 if(KviQString::matchString(pProfile->network(), szNetwork, false, true))
100 return pProfile;
101 }
102
103 return nullptr;
104 }
105
copyFrom(const KviIdentityProfileSet & src)106 void KviIdentityProfileSet::copyFrom(const KviIdentityProfileSet & src)
107 {
108 if(src.m_pProfiles)
109 {
110 if(m_pProfiles)
111 m_pProfiles->clear();
112 else
113 {
114 m_pProfiles = new KviPointerList<KviIdentityProfile>;
115 m_pProfiles->setAutoDelete(true);
116 }
117
118 for(KviIdentityProfile * pSrcProfile = src.m_pProfiles->first(); pSrcProfile; pSrcProfile = src.m_pProfiles->next())
119 {
120 KviIdentityProfile * pProfile = new KviIdentityProfile();
121 pProfile->copyFrom(*pSrcProfile);
122 m_pProfiles->append(pProfile);
123 }
124
125 if(m_pProfiles->isEmpty())
126 {
127 m_bEnabled = false;
128 delete m_pProfiles;
129 m_pProfiles = nullptr;
130 }
131 else
132 {
133 m_bEnabled = src.m_bEnabled;
134 }
135 }
136 else
137 {
138 m_bEnabled = false;
139 if(m_pProfiles)
140 {
141 delete m_pProfiles;
142 m_pProfiles = nullptr;
143 }
144 }
145 }
146
addProfile(KviIdentityProfile * pProfile)147 void KviIdentityProfileSet::addProfile(KviIdentityProfile * pProfile)
148 {
149 if(!m_pProfiles)
150 {
151 m_pProfiles = new KviPointerList<KviIdentityProfile>;
152 m_pProfiles->setAutoDelete(true);
153 }
154
155 m_pProfiles->append(pProfile);
156 }
157
load(const QString & szConfigFile)158 void KviIdentityProfileSet::load(const QString & szConfigFile)
159 {
160 clear();
161 KviConfigurationFile cfg(szConfigFile, KviConfigurationFile::Read);
162
163 QString szTmp = "ProfilesNumber";
164
165 unsigned int uCount = cfg.readUIntEntry(szTmp, 0);
166 if(uCount == 0)
167 return;
168 loadPrivate(&cfg, QString(""), uCount);
169 }
170
save(const QString & szConfigFile)171 void KviIdentityProfileSet::save(const QString & szConfigFile)
172 {
173 KviConfigurationFile cfg(szConfigFile, KviConfigurationFile::Write);
174 cfg.clear();
175 save(&cfg, QString(""));
176 }
177
save(KviConfigurationFile * pCfg,const QString & szPrefix)178 void KviIdentityProfileSet::save(KviConfigurationFile * pCfg, const QString & szPrefix)
179 {
180 if(!m_pProfiles)
181 return;
182 if(m_pProfiles->isEmpty())
183 return;
184
185 QString szTmp;
186 if(m_bEnabled)
187 {
188 szTmp = QString("%1ProfilesEnabled").arg(szPrefix);
189 pCfg->writeEntry(szTmp, m_bEnabled);
190 }
191
192 szTmp = QString("%1ProfilesNumber").arg(szPrefix);
193 pCfg->writeEntry(szTmp, m_pProfiles->count());
194
195 int iIdx = 0;
196 for(KviIdentityProfile * pProfile = m_pProfiles->first(); pProfile; pProfile = m_pProfiles->next())
197 {
198 szTmp = QString("%1Profile%2_").arg(szPrefix).arg(iIdx);
199 pProfile->save(pCfg, szTmp);
200 iIdx++;
201 }
202 }
203
loadPrivate(KviConfigurationFile * pCfg,const QString & szPrefix,unsigned int uEntries)204 bool KviIdentityProfileSet::loadPrivate(KviConfigurationFile * pCfg, const QString & szPrefix, unsigned int uEntries)
205 {
206 if(m_pProfiles)
207 m_pProfiles->clear();
208 else
209 {
210 m_pProfiles = new KviPointerList<KviIdentityProfile>;
211 m_pProfiles->setAutoDelete(true);
212 }
213
214 if(uEntries != 0)
215 {
216 QString szTmp = QString("%1ProfilesEnabled").arg(szPrefix);
217 m_bEnabled = pCfg->readBoolEntry(szTmp, false);
218
219 for(unsigned int u = 0; u < uEntries; u++)
220 {
221 szTmp = QString("%1Profile%2_").arg(szPrefix, u);
222 KviIdentityProfile * pProfile = new KviIdentityProfile();
223 if(!pProfile->load(pCfg, szTmp))
224 delete pProfile;
225 else
226 m_pProfiles->append(pProfile);
227 }
228 }
229
230 if(m_pProfiles->isEmpty())
231 {
232 m_bEnabled = false;
233 delete m_pProfiles;
234 m_pProfiles = nullptr;
235 return false;
236 }
237 return true;
238 }
239