1 //=============================================================================
2 //
3 //   File : KviCustomToolBarManager.cpp
4 //   Creation date : Sun 05 Dec 2004 18:20:18 by Szymon Stefanek
5 //
6 //   This file is part of the KVIrc IRC client distribution
7 //   Copyright (C) 2004-2010 Szymon Stefanek <pragma at kvirc dot net>
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 "KviCustomToolBarManager.h"
26 #include "KviCustomToolBarDescriptor.h"
27 #include "KviConfigurationFile.h"
28 #include "KviLocale.h"
29 
30 KviCustomToolBarManager * KviCustomToolBarManager::m_pInstance = nullptr;
31 
KviCustomToolBarManager()32 KviCustomToolBarManager::KviCustomToolBarManager()
33 {
34 	m_pDescriptors = new KviPointerHashTable<QString, KviCustomToolBarDescriptor>(17, false);
35 	m_pDescriptors->setAutoDelete(true);
36 }
37 
~KviCustomToolBarManager()38 KviCustomToolBarManager::~KviCustomToolBarManager()
39 {
40 	delete m_pDescriptors;
41 }
42 
firstExistingToolBar()43 KviCustomToolBar * KviCustomToolBarManager::firstExistingToolBar()
44 {
45 	KviPointerHashTableIterator<QString, KviCustomToolBarDescriptor> it(*m_pDescriptors);
46 	while(KviCustomToolBarDescriptor * d = it.current())
47 	{
48 		if(d->toolBar())
49 			return d->toolBar();
50 		++it;
51 	}
52 	return nullptr;
53 }
54 
init()55 void KviCustomToolBarManager::init()
56 {
57 	if(!m_pInstance)
58 		m_pInstance = new KviCustomToolBarManager();
59 }
60 
done()61 void KviCustomToolBarManager::done()
62 {
63 	if(m_pInstance)
64 	{
65 		delete m_pInstance;
66 		m_pInstance = nullptr;
67 	}
68 }
69 
idForNewToolBar(const QString & szTemplate)70 QString KviCustomToolBarManager::idForNewToolBar(const QString & szTemplate)
71 {
72 	QString s;
73 	QString szTT = szTemplate.toLower();
74 	szTT.remove(' ');
75 	szTT.remove("$tr");
76 	szTT.remove('(');
77 	szTT.remove(')');
78 	szTT.remove('"');
79 	for(int idx = 0;; idx++)
80 	{
81 		s = szTT;
82 		if(idx > 0)
83 			s += QString::number(idx);
84 		if(!m_pDescriptors->find(s))
85 			return s;
86 	}
87 	return s;
88 }
89 
findDescriptorByInternalId(int id)90 KviCustomToolBarDescriptor * KviCustomToolBarManager::findDescriptorByInternalId(int id)
91 {
92 	KviPointerHashTableIterator<QString, KviCustomToolBarDescriptor> it(*m_pDescriptors);
93 	while(KviCustomToolBarDescriptor * d = it.current())
94 	{
95 		if(d->internalId() == id)
96 			return d;
97 		++it;
98 	}
99 	return nullptr;
100 }
101 
renameDescriptor(const QString & szId,const QString & szNewId,const QString & szNewLabelCode)102 bool KviCustomToolBarManager::renameDescriptor(const QString & szId, const QString & szNewId, const QString & szNewLabelCode)
103 {
104 	KviCustomToolBarDescriptor * d = m_pDescriptors->find(szId);
105 	if(!d)
106 		return false;
107 	d->rename(szNewLabelCode);
108 	if(szId == szNewId)
109 		return true; // already done
110 	m_pDescriptors->setAutoDelete(false);
111 	m_pDescriptors->remove(szId);
112 	m_pDescriptors->replace(szNewId, d);
113 	m_pDescriptors->setAutoDelete(true);
114 	return true;
115 }
116 
destroyDescriptor(const QString & szId)117 bool KviCustomToolBarManager::destroyDescriptor(const QString & szId)
118 {
119 	KviCustomToolBarDescriptor * d = m_pDescriptors->find(szId);
120 	if(!d)
121 		return false;
122 	m_pDescriptors->remove(szId); // will delete it too!
123 	return true;
124 }
125 
clear()126 void KviCustomToolBarManager::clear()
127 {
128 	m_pDescriptors->clear(); // bye!
129 }
130 
create(const QString & szId,const QString & szLabelCode)131 KviCustomToolBarDescriptor * KviCustomToolBarManager::create(const QString & szId, const QString & szLabelCode)
132 {
133 	KviCustomToolBarDescriptor * d = m_pDescriptors->find(szId);
134 	if(d)
135 		return d;
136 	d = new KviCustomToolBarDescriptor(szId, szLabelCode);
137 	m_pDescriptors->replace(szId, d);
138 	return d;
139 }
140 
storeVisibilityState()141 void KviCustomToolBarManager::storeVisibilityState()
142 {
143 	KviPointerHashTableIterator<QString, KviCustomToolBarDescriptor> it(*m_pDescriptors);
144 	while(KviCustomToolBarDescriptor * d = it.current())
145 	{
146 		d->m_bVisibleAtStartup = d->toolBar() != nullptr;
147 		++it;
148 	}
149 }
150 
visibleToolBarCount()151 int KviCustomToolBarManager::visibleToolBarCount()
152 {
153 	int cnt = 0;
154 	KviPointerHashTableIterator<QString, KviCustomToolBarDescriptor> it(*m_pDescriptors);
155 	while(KviCustomToolBarDescriptor * d = it.current())
156 	{
157 		if(d->toolBar())
158 			cnt++;
159 		++it;
160 	}
161 	return cnt;
162 }
163 
createToolBarsVisibleAtStartup()164 void KviCustomToolBarManager::createToolBarsVisibleAtStartup()
165 {
166 	KviPointerHashTableIterator<QString, KviCustomToolBarDescriptor> it(*m_pDescriptors);
167 	while(KviCustomToolBarDescriptor * d = it.current())
168 	{
169 		if(d->m_bVisibleAtStartup && (!d->toolBar()))
170 			d->createToolBar();
171 		++it;
172 	}
173 }
174 
updateVisibleToolBars()175 void KviCustomToolBarManager::updateVisibleToolBars()
176 {
177 	KviPointerHashTableIterator<QString, KviCustomToolBarDescriptor> it(*m_pDescriptors);
178 	while(KviCustomToolBarDescriptor * d = it.current())
179 	{
180 		if(d->toolBar())
181 			d->updateToolBar();
182 		++it;
183 	}
184 }
185 
load(const QString & szFileName)186 void KviCustomToolBarManager::load(const QString & szFileName)
187 {
188 	KviConfigurationFile cfg(szFileName, KviConfigurationFile::Read);
189 
190 	KviConfigurationFileIterator it(*(cfg.dict()));
191 	while(it.current())
192 	{
193 		cfg.setGroup(it.currentKey());
194 		KviCustomToolBarDescriptor * d = new KviCustomToolBarDescriptor(it.currentKey(), QString());
195 		d->m_bVisibleAtStartup = (cfg.readIntEntry("Visible", 0) > 0);
196 		if(!d->load(&cfg))
197 			delete d;
198 		else
199 			m_pDescriptors->replace(it.currentKey(), d);
200 		++it;
201 	}
202 }
203 
save(const QString & szFileName)204 void KviCustomToolBarManager::save(const QString & szFileName)
205 {
206 	KviConfigurationFile cfg(szFileName, KviConfigurationFile::Write);
207 	KviPointerHashTableIterator<QString, KviCustomToolBarDescriptor> it(*m_pDescriptors);
208 	while(KviCustomToolBarDescriptor * d = it.current())
209 	{
210 		cfg.setGroup(d->id());
211 		cfg.writeEntry("Visible", d->m_bVisibleAtStartup ? 1 : 0);
212 		d->save(&cfg);
213 		++it;
214 	}
215 }
216