summaryrefslogtreecommitdiffstats
path: root/src/kvilib/irc/kvi_useridentity.cpp
blob: 4e3468dfb52bebbc6b999765dfdfe36aea2abe00 (plain)
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
//=============================================================================
//
//   File : kvi_useridentity.cpp
//   Created on Sun 21 Jan 2007 04:31:47 by Szymon Stefanek
//
//   This file is part of the KVIrc IRC Client distribution
//   Copyright (C) 2007 Szymon Stefanek <pragma at kvirc dot 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 opinion) 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.
//
//=============================================================================

#define __KVILIB__

#include "kvi_useridentity.h"
#include "kvi_locale.h"
#include "kvi_defaults.h"

// FIXME: Put here also the default away message, default away nick, default ctcp replies etc ?

bool KviUserIdentity::load(KviConfig &cfg)
{
	m_szId = cfg.group();
	m_szNickName = cfg.readTQStringEntry("NickName");
	m_szAltNickName1 = cfg.readTQStringEntry("AltNickName1");
	m_szAltNickName2 = cfg.readTQStringEntry("AltNickName2");
	m_szAltNickName3 = cfg.readTQStringEntry("AltNickName3");
	m_szUserName = cfg.readTQStringEntry("UserName");
	// FIXME: At least scramble the pass ?
	m_szPassword = cfg.readTQStringEntry("Password");
	KviPixmap def;
	m_pixAvatar = cfg.readPixmapEntry("Avatar",def);
	m_szPartMessage = cfg.readTQStringEntry("PartMessage");
	m_szQuitMessage= cfg.readTQStringEntry("QuitMessage");
	m_szAge = cfg.readTQStringEntry("Age");
	m_szGender = cfg.readTQStringEntry("Gender");
	m_szLocation = cfg.readTQStringEntry("Location");
	m_szLanguages = cfg.readTQStringEntry("Languages");
	m_szOtherInfo = cfg.readTQStringEntry("OtherInfo");
	m_szUserMode = cfg.readTQStringEntry("UserMode");
	m_szOnConnectCommand = cfg.readTQStringEntry("OnConnectCommand");
	m_szOnLoginCommand = cfg.readTQStringEntry("OnLoginCommand");
	return !(m_szId.isEmpty() || m_szNickName.isEmpty());
}

bool KviUserIdentity::save(KviConfig &cfg)
{
	cfg.setGroup(m_szId);
	cfg.writeEntry("NickName",m_szNickName);
	cfg.writeEntry("AltNickName1",m_szAltNickName1);
	cfg.writeEntry("AltNickName2",m_szAltNickName2);
	cfg.writeEntry("AltNickName3",m_szAltNickName3);
	cfg.writeEntry("UserName",m_szUserName);
	// FIXME: At least scramble the pass ?
	cfg.writeEntry("Password",m_szPassword);
	cfg.writeEntry("Avatar",m_pixAvatar);
	cfg.writeEntry("PartMessage",m_szPartMessage);
	cfg.writeEntry("QuitMessage",m_szQuitMessage);
	cfg.writeEntry("Age",m_szAge);
	cfg.writeEntry("Gender",m_szGender);
	cfg.writeEntry("Location",m_szLocation);
	cfg.writeEntry("Languages",m_szLanguages);
	cfg.writeEntry("OtherInfo",m_szOtherInfo);
	cfg.writeEntry("UserMode",m_szUserMode);
	cfg.writeEntry("OnConnectCommand",m_szOnConnectCommand);
	cfg.writeEntry("OnLoginCommand",m_szOnLoginCommand);
	return true;
}

void KviUserIdentity::copyFrom(const KviUserIdentity &src)
{
	m_szId = src.m_szId;
	m_szNickName = src.m_szNickName;
	
	m_szAltNickName1 = src.m_szAltNickName1;
	m_szAltNickName2 = src.m_szAltNickName2;
	m_szAltNickName3 = src.m_szAltNickName3;

	m_szUserName = src.m_szUserName;
	m_szRealName = src.m_szRealName;
	m_szPassword = src.m_szPassword;
	
	m_pixAvatar = src.m_pixAvatar;

	m_szUserName = src.m_szUserName;
	m_szRealName = src.m_szRealName;
	m_szPassword = src.m_szPassword;
	
	m_pixAvatar = src.m_pixAvatar;
	
	m_szPartMessage = src.m_szPartMessage;
	m_szQuitMessage = src.m_szQuitMessage;
	
	m_szAge = src.m_szAge;
	m_szGender = src.m_szGender;
	m_szLocation = src.m_szLocation;
	m_szLanguages = src.m_szLanguages;
	m_szOtherInfo = src.m_szOtherInfo;
	
	m_szUserMode = src.m_szUserMode;
	
	m_szOnConnectCommand = src.m_szOnConnectCommand;
	m_szOnLoginCommand = src.m_szOnLoginCommand;
}


KviUserIdentityManager * KviUserIdentityManager::m_pInstance = 0;

KviUserIdentityManager::KviUserIdentityManager()
: KviHeapObject()
{
	m_pIdentityDict = new KviPointerHashTable<TQString,KviUserIdentity>();
	m_pIdentityDict->setAutoDelete(true);
}

KviUserIdentityManager::~KviUserIdentityManager()
{
	delete m_pIdentityDict;
}

void KviUserIdentityManager::init()
{
	if(m_pInstance)return;
	m_pInstance = new KviUserIdentityManager();
}

void KviUserIdentityManager::done()
{
	if(!m_pInstance)return;
	delete m_pInstance;
	m_pInstance = 0;
}

const KviUserIdentity * KviUserIdentityManager::defaultIdentity()
{
	KviUserIdentity * ret;
	if(!m_szDefaultIdentity.isEmpty())
	{
		ret = m_pIdentityDict->find(m_szDefaultIdentity);
		if(ret)return ret;
	}

	// the default identity is borken :/
	// grab the first one
	KviPointerHashTableIterator<TQString,KviUserIdentity> it(*m_pIdentityDict);
	ret = it.current();
	if(ret)
	{
		m_szDefaultIdentity = ret->id();
		return ret;
	}
	// no identities available: create the default
	ret = new KviUserIdentity();

	ret->setId(__tr2qs("Default"));
	ret->setNickName(KVI_DEFAULT_NICKNAME1);
	ret->setAltNickName1(KVI_DEFAULT_NICKNAME2);
	ret->setAltNickName2(KVI_DEFAULT_NICKNAME3);
	ret->setAltNickName3(KVI_DEFAULT_NICKNAME4);
	ret->setUserName(KVI_DEFAULT_USERNAME);
	ret->setRealName(KVI_DEFAULT_REALNAME);
	ret->setPartMessage(KVI_DEFAULT_PART_MESSAGE);
	ret->setQuitMessage(KVI_DEFAULT_TQUIT_MESSAGE);
	
	m_pIdentityDict->replace(ret->id(),ret);
	
	return ret;
}

void KviUserIdentityManager::load(const TQString &szFileName)
{
	m_pIdentityDict->clear();

	KviConfig cfg(szFileName,KviConfig::Read);

	cfg.setGroup("KVIrc");
	
	m_szDefaultIdentity = cfg.readTQStringEntry("DefaultIdentity","");

	KviConfigIterator it(*(cfg.dict()));
	while(KviConfigGroup * grp = it.current())
	{
		if(!KviTQString::equalCI(it.currentKey(),"KVIrc"))
		{
			cfg.setGroup(it.currentKey());

			KviUserIdentity * id = new KviUserIdentity();
			if(id->load(cfg))
				m_pIdentityDict->replace(id->id(),id);
			else
				delete id;
		}
		++it;
	}
}

void KviUserIdentityManager::save(const TQString &szFileName)
{
	KviConfig cfg(szFileName,KviConfig::Write);
	cfg.clear();
	
	cfg.setGroup("KVIrc");

	cfg.writeEntry("DefaultIdentity",m_szDefaultIdentity);
	
	KviPointerHashTableIterator<TQString,KviUserIdentity> it(*m_pIdentityDict);
	while(KviUserIdentity * id = it.current())
	{
		id->save(cfg);
		++it;
	}
}

void KviUserIdentityManager::copyFrom(KviUserIdentityManager * pWorkingCopy)
{
	m_pIdentityDict->clear();
	m_szDefaultIdentity = pWorkingCopy->m_szDefaultIdentity;
	KviPointerHashTableIterator<TQString,KviUserIdentity> it(*(pWorkingCopy->m_pIdentityDict));
	while(KviUserIdentity * id = it.current())
	{
		KviUserIdentity * pNew = new KviUserIdentity();
		pNew->copyFrom(*id);
		m_pIdentityDict->replace(pNew->id(),pNew);
		++it;
	}
}

KviUserIdentityManager * KviUserIdentityManager::createWorkingCopy()
{
	KviUserIdentityManager * pCopy = new KviUserIdentityManager();
	pCopy->copyFrom(this);
	return pCopy;
}

void KviUserIdentityManager::releaseWorkingCopy(KviUserIdentityManager * pWorkingCopy)
{
	if(pWorkingCopy)delete pWorkingCopy;
}