aboutsummaryrefslogtreecommitdiffstats
path: root/src/kvilib/irc/KviUserIdentityManager.cpp
blob: f8e1d6cef4599a2ab6b0b64f74e67fa47ce8d899 (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
//=============================================================================
//
//   File : KviUserIdentityManager.cpp
//   Creation date : Thu Dec Jan 2007 05:03:47 by Elvio Basello
//
//   This file is part of the KVIrc IRC Client distribution
//   Copyright (C) 2010 Elvio Basello (hellvis69 at gmail dot com)
//
//   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 "KviUserIdentityManager.h"
#include "KviConfigurationFile.h"
#include "KviLocale.h"
#include "KviUserIdentity.h"
#include "kvi_defaults.h"

KviUserIdentityManager * KviUserIdentityManager::m_pInstance = nullptr;

KviUserIdentityManager::KviUserIdentityManager()
    : KviHeapObject()
{
	m_pIdentityDict = new KviPointerHashTable<QString, 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 = nullptr;
}

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

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

	pUser->setId(__tr2qs("Default"));
	pUser->setNickName(KVI_DEFAULT_NICKNAME1);

	pUser->setAltNickName1(QString(KVI_DEFAULT_NICKNAME2).replace(KVI_DEFAULT_NICKNAME_TOKEN, KVI_DEFAULT_NICKNAME1));
	pUser->setAltNickName2(QString(KVI_DEFAULT_NICKNAME3).replace(KVI_DEFAULT_NICKNAME_TOKEN, KVI_DEFAULT_NICKNAME1));
	pUser->setAltNickName3(QString(KVI_DEFAULT_NICKNAME4).replace(KVI_DEFAULT_NICKNAME_TOKEN, KVI_DEFAULT_NICKNAME1));
	pUser->setUserName(KVI_DEFAULT_USERNAME);
	pUser->setRealName(KVI_DEFAULT_REALNAME);
	pUser->setPartMessage(KVI_DEFAULT_PART_MESSAGE);
	pUser->setQuitMessage(KVI_DEFAULT_QUIT_MESSAGE);

	m_pIdentityDict->replace(pUser->id(), pUser);

	return pUser;
}

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

	KviConfigurationFile cfg(szFileName, KviConfigurationFile::Read);

	cfg.setGroup("KVIrc");

	m_szDefaultIdentity = cfg.readEntry("DefaultIdentity", "");

	KviConfigurationFileIterator it(*(cfg.dict()));
	while((it.current()))
	{
		if(!KviQString::equalCI(it.currentKey(), "KVIrc"))
		{
			cfg.setGroup(it.currentKey());

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

void KviUserIdentityManager::save(const QString & szFileName)
{
	KviConfigurationFile cfg(szFileName, KviConfigurationFile::Write);
	cfg.clear();

	cfg.setGroup("KVIrc");

	cfg.writeEntry("DefaultIdentity", m_szDefaultIdentity);

	KviPointerHashTableIterator<QString, KviUserIdentity> it(*m_pIdentityDict);
	while(KviUserIdentity * pUser = it.current())
	{
		pUser->save(cfg);
		++it;
	}
}

void KviUserIdentityManager::copyFrom(KviUserIdentityManager * pWorkingCopy)
{
	m_pIdentityDict->clear();
	m_szDefaultIdentity = pWorkingCopy->m_szDefaultIdentity;
	KviPointerHashTableIterator<QString, KviUserIdentity> it(*(pWorkingCopy->m_pIdentityDict));
	while(KviUserIdentity * pUser = it.current())
	{
		KviUserIdentity * pNew = new KviUserIdentity();
		pNew->copyFrom(*pUser);
		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;
}