aboutsummaryrefslogtreecommitdiffstats
path: root/src/kvilib/irc/KviIdentityProfileSet.cpp
blob: 68611406685b144b9ce94f5e87b5998decd91047 (about) (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
//=============================================================================
//
//   File : KviIdentityProfileSet.cpp
//   Creation date : Thu Dec 30 2010 15:54:48 by Elvio Basello
//
//   This file is part of the KVIrc IRC client distribution
//   Copyright (C) 2010 Elvio Basello (hellvis69 at netsons dot org)
//
//   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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
//=============================================================================

#include "KviIdentityProfileSet.h"
#include "KviConfigurationFile.h"

KviIdentityProfileSet * KviIdentityProfileSet::m_pSelf = nullptr;

KviIdentityProfileSet::KviIdentityProfileSet()
    : KviHeapObject()
{
	m_bEnabled = false;
	m_pProfiles = nullptr;
}

KviIdentityProfileSet::KviIdentityProfileSet(const KviIdentityProfileSet & set)
{
	m_pProfiles = nullptr;
	copyFrom(set);
}

KviIdentityProfileSet::~KviIdentityProfileSet()
{
	if(m_pProfiles)
		delete m_pProfiles;
}

void KviIdentityProfileSet::init()
{
	if(!m_pSelf)
	{
		m_pSelf = new KviIdentityProfileSet();
	}
}

void KviIdentityProfileSet::done()
{
	if(m_pSelf) {
		delete m_pSelf;
		m_pSelf = nullptr;
	}
}

void KviIdentityProfileSet::clear()
{
	if(m_pProfiles)
	{
		delete m_pProfiles;
		m_pProfiles = nullptr;
	}
	m_bEnabled = false;
}

KviIdentityProfile * KviIdentityProfileSet::findName(const QString & szName)
{
	if(!m_pProfiles)
		return nullptr;

	KviIdentityProfile * pProfile;
	for(pProfile = m_pProfiles->first(); pProfile; pProfile = m_pProfiles->next())
	{
		if(KviQString::matchString(pProfile->name(), szName, false, true))
			return pProfile;
	}

	return nullptr;
}

KviIdentityProfile * KviIdentityProfileSet::findNetwork(const QString & szNetwork)
{
	if(!m_pProfiles)
		return nullptr;

	KviIdentityProfile * pProfile;
	for(pProfile = m_pProfiles->first(); pProfile; pProfile = m_pProfiles->next())
	{
		if(KviQString::matchString(pProfile->network(), szNetwork, false, true))
			return pProfile;
	}

	return nullptr;
}

void KviIdentityProfileSet::copyFrom(const KviIdentityProfileSet & src)
{
	if(src.m_pProfiles)
	{
		if(m_pProfiles)
			m_pProfiles->clear();
		else
		{
			m_pProfiles = new KviPointerList<KviIdentityProfile>;
			m_pProfiles->setAutoDelete(true);
		}

		for(KviIdentityProfile * pSrcProfile = src.m_pProfiles->first(); pSrcProfile; pSrcProfile = src.m_pProfiles->next())
		{
			KviIdentityProfile * pProfile = new KviIdentityProfile();
			pProfile->copyFrom(*pSrcProfile);
			m_pProfiles->append(pProfile);
		}

		if(m_pProfiles->isEmpty())
		{
			m_bEnabled = false;
			delete m_pProfiles;
			m_pProfiles = nullptr;
		}
		else
		{
			m_bEnabled = src.m_bEnabled;
		}
	}
	else
	{
		m_bEnabled = false;
		if(m_pProfiles)
		{
			delete m_pProfiles;
			m_pProfiles = nullptr;
		}
	}
}

void KviIdentityProfileSet::addProfile(KviIdentityProfile * pProfile)
{
	if(!m_pProfiles)
	{
		m_pProfiles = new KviPointerList<KviIdentityProfile>;
		m_pProfiles->setAutoDelete(true);
	}

	m_pProfiles->append(pProfile);
}

void KviIdentityProfileSet::load(const QString & szConfigFile)
{
	clear();
	KviConfigurationFile cfg(szConfigFile, KviConfigurationFile::Read);

	QString szTmp = "ProfilesNumber";

	unsigned int uCount = cfg.readUIntEntry(szTmp, 0);
	if(uCount == 0)
		return;
	loadPrivate(&cfg, QString(""), uCount);
}

void KviIdentityProfileSet::save(const QString & szConfigFile)
{
	KviConfigurationFile cfg(szConfigFile, KviConfigurationFile::Write);
	cfg.clear();
	save(&cfg, QString(""));
}

void KviIdentityProfileSet::save(KviConfigurationFile * pCfg, const QString & szPrefix)
{
	if(!m_pProfiles)
		return;
	if(m_pProfiles->isEmpty())
		return;

	QString szTmp;
	if(m_bEnabled)
	{
		szTmp = QString("%1ProfilesEnabled").arg(szPrefix);
		pCfg->writeEntry(szTmp, m_bEnabled);
	}

	szTmp = QString("%1ProfilesNumber").arg(szPrefix);
	pCfg->writeEntry(szTmp, m_pProfiles->count());

	int iIdx = 0;
	for(KviIdentityProfile * pProfile = m_pProfiles->first(); pProfile; pProfile = m_pProfiles->next())
	{
		szTmp = QString("%1Profile%2_").arg(szPrefix).arg(iIdx);
		pProfile->save(pCfg, szTmp);
		iIdx++;
	}
}

bool KviIdentityProfileSet::loadPrivate(KviConfigurationFile * pCfg, const QString & szPrefix, unsigned int uEntries)
{
	if(m_pProfiles)
		m_pProfiles->clear();
	else
	{
		m_pProfiles = new KviPointerList<KviIdentityProfile>;
		m_pProfiles->setAutoDelete(true);
	}

	if(uEntries != 0)
	{
		QString szTmp = QString("%1ProfilesEnabled").arg(szPrefix);
		m_bEnabled = pCfg->readBoolEntry(szTmp, false);

		for(unsigned int u = 0; u < uEntries; u++)
		{
			szTmp = QString("%1Profile%2_").arg(szPrefix, u);
			KviIdentityProfile * pProfile = new KviIdentityProfile();
			if(!pProfile->load(pCfg, szTmp))
				delete pProfile;
			else
				m_pProfiles->append(pProfile);
		}
	}

	if(m_pProfiles->isEmpty())
	{
		m_bEnabled = false;
		delete m_pProfiles;
		m_pProfiles = nullptr;
		return false;
	}
	return true;
}