1 //=============================================================================
2 //
3 //   File : OptionsWidget_sound.cpp
4 //   Creation date : Fri Sep  6 02:18:23 2002 GMT by Szymon Stefanek
5 //
6 //   This file is part of the KVsound sound client distribution
7 //   Copyright (C) 2002-2010 Szymon Stefanek (pragma at kvsound 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 "OptionsWidget_sound.h"
26 
27 #include "kvi_settings.h"
28 #include "KviLocale.h"
29 #include "KviOptions.h"
30 #include "KviModuleManager.h"
31 #include "KviPointerList.h"
32 #include "KviCString.h"
33 #include "KviTalHBox.h"
34 #include "KviTalToolTip.h"
35 #include "KviApplication.h"
36 
37 #include <QLabel>
38 #include <QPushButton>
39 
40 // FIXME: This module doesn't ???? properly when auto-detection is performed!
41 
OptionsWidget_sound(QWidget * pParent)42 OptionsWidget_sound::OptionsWidget_sound(QWidget * pParent)
43     : KviOptionsWidget(pParent)
44 {
45 }
46 
47 OptionsWidget_sound::~OptionsWidget_sound()
48     = default;
49 
OptionsWidget_soundGeneral(QWidget * parent)50 OptionsWidget_soundGeneral::OptionsWidget_soundGeneral(QWidget * parent)
51     : KviOptionsWidget(parent)
52 {
53 	m_bFirstShow = true;
54 
55 	setObjectName("sound_system_options_widget");
56 	createLayout();
57 
58 	KviTalGroupBox * g = addGroupBox(0, 0, 0, 0, Qt::Horizontal, __tr2qs_ctx("Sound System", "options"), true);
59 
60 	KviTalToolTip::add(g, __tr2qs_ctx("This allows you to select the sound system to be used with KVIrc.", "options"));
61 
62 	KviTalHBox * h = new KviTalHBox(g);
63 
64 	m_pSoundSystemBox = new QComboBox(h);
65 
66 	m_pSoundAutoDetectButton = new QPushButton(__tr2qs_ctx("Auto-detect", "options"), h);
67 	connect(m_pSoundAutoDetectButton, SIGNAL(clicked()), this, SLOT(soundAutoDetect()));
68 
69 	m_pSoundTestButton = new QPushButton(__tr2qs_ctx("Test", "options"), h);
70 	connect(m_pSoundTestButton, SIGNAL(clicked()), this, SLOT(soundTest()));
71 
72 	g = addGroupBox(0, 1, 0, 1, Qt::Horizontal, __tr2qs_ctx("Media Player", "options"), true);
73 
74 	KviTalToolTip::add(g, __tr2qs_ctx("This allows you to select the preferred media player to be used with "
75 	                                  "the mediaplayer.* module commands and functions.",
76 	                          "options"));
77 
78 	h = new KviTalHBox(g);
79 
80 	m_pMediaPlayerBox = new QComboBox(h);
81 
82 	m_pMediaAutoDetectButton = new QPushButton(__tr2qs_ctx("Auto-detect", "options"), h);
83 	connect(m_pMediaAutoDetectButton, SIGNAL(clicked()), this, SLOT(mediaAutoDetect()));
84 
85 	m_pMediaTestButton = new QPushButton(__tr2qs_ctx("Test", "options"), h);
86 	connect(m_pMediaTestButton, SIGNAL(clicked()), this, SLOT(mediaTest()));
87 
88 	m_pMediaTestButton->setEnabled(false);
89 
90 	g = addGroupBox(0, 2, 0, 2, Qt::Horizontal, __tr2qs_ctx("ID3 Tags Encoding", "options"), true);
91 
92 	KviTalToolTip::add(g, __tr2qs_ctx("This allows you to select encoding of mp3 tags.", "options"));
93 
94 	h = new KviTalHBox(g);
95 
96 	m_pTagsEncodingCombo = new QComboBox(h);
97 	m_pTagsEncodingCombo->addItem(__tr2qs_ctx("Use Language Encoding", "options"));
98 
99 	int i = 0;
100 	int iMatch = 0;
101 
102 	KviLocale::EncodingDescription * d = KviLocale::instance()->encodingDescription(i);
103 	while(d->pcName)
104 	{
105 		if(KviQString::equalCI(d->pcName, KVI_OPTION_STRING(KviOption_stringMp3TagsEncoding)))
106 			iMatch = i + 1;
107 		m_pTagsEncodingCombo->insertItem(m_pTagsEncodingCombo->count(), d->pcName);
108 		i++;
109 		d = KviLocale::instance()->encodingDescription(i);
110 	}
111 	m_pTagsEncodingCombo->setCurrentIndex(iMatch);
112 
113 #if defined(COMPILE_ON_WINDOWS) || defined(COMPILE_ON_MINGW)
114 	g = addGroupBox(0, 3, 0, 3, Qt::Horizontal, __tr2qs_ctx("Winamp messages encoding", "options"), true);
115 
116 	KviTalToolTip::add(g, __tr2qs_ctx("This allows you to select encoding of winamp messages.", "options"));
117 
118 	h = new KviTalHBox(g);
119 
120 	m_pWinampEncodingCombo = new QComboBox(h);
121 
122 	m_pWinampEncodingCombo->addItem(__tr2qs_ctx("Use Language Encoding", "options"));
123 	i = 0;
124 	iMatch = 0;
125 
126 	d = KviLocale::instance()->encodingDescription(i);
127 	while(d->pcName)
128 	{
129 		if(KviQString::equalCI(d->pcName, KVI_OPTION_STRING(KviOption_stringWinampTextEncoding)))
130 			iMatch = i + 1;
131 		m_pWinampEncodingCombo->insertItem(m_pWinampEncodingCombo->count(), d->pcName);
132 		i++;
133 		d = KviLocale::instance()->encodingDescription(i);
134 	}
135 	m_pWinampEncodingCombo->setCurrentIndex(iMatch);
136 
137 	addRowSpacer(0, 4, 0, 4);
138 #else
139 	addRowSpacer(0, 3, 0, 3);
140 #endif
141 }
142 
143 OptionsWidget_soundGeneral::~OptionsWidget_soundGeneral()
144     = default;
145 
showEvent(QShowEvent *)146 void OptionsWidget_soundGeneral::showEvent(QShowEvent *)
147 {
148 	if(!m_bFirstShow)
149 		return;
150 	// We fill these boxes only before the first show since the soundFillBox()
151 	// is likely to trigger sound system-detection which may take time...
152 
153 	g_pApp->setOverrideCursor(Qt::WaitCursor);
154 
155 	soundFillBox();
156 	mediaFillBox();
157 
158 	g_pApp->restoreOverrideCursor();
159 
160 	m_bFirstShow = false;
161 }
162 
soundTest()163 void OptionsWidget_soundGeneral::soundTest()
164 {
165 	QString szSavedSoundSystem = KVI_OPTION_STRING(KviOption_stringSoundSystem);
166 	KVI_OPTION_STRING(KviOption_stringSoundSystem) = m_pSoundSystemBox->currentText();
167 
168 	KviModule * m = g_pModuleManager->getModule("snd");
169 
170 	if(!m)
171 		return; // doh
172 
173 	QString szFileName;
174 
175 	if(!g_pApp->findAudioFile(szFileName, QString::fromUtf8("jingle.wav")))
176 	{
177 		qDebug("Can't find the jingle.wav file: was it shipped with your KVIrc installation?");
178 		return;
179 	}
180 
181 	m->ctrl("play", &szFileName);
182 
183 	KVI_OPTION_STRING(KviOption_stringSoundSystem) = szSavedSoundSystem;
184 }
185 
mediaTest()186 void OptionsWidget_soundGeneral::mediaTest()
187 {
188 }
189 
soundAutoDetect()190 void OptionsWidget_soundGeneral::soundAutoDetect()
191 {
192 	KviModule * m = g_pModuleManager->getModule("snd");
193 	if(!m)
194 		return;
195 
196 	g_pApp->setOverrideCursor(Qt::WaitCursor);
197 
198 	m->ctrl("detectSoundSystem", nullptr);
199 	soundFillBox();
200 
201 	g_pApp->restoreOverrideCursor();
202 }
203 
mediaAutoDetect()204 void OptionsWidget_soundGeneral::mediaAutoDetect()
205 {
206 	KviModule * m = g_pModuleManager->getModule("mediaplayer");
207 	if(!m)
208 		return;
209 
210 	g_pApp->setOverrideCursor(Qt::WaitCursor);
211 
212 	m->ctrl("detectMediaPlayer", nullptr);
213 	mediaFillBox();
214 
215 	g_pApp->restoreOverrideCursor();
216 }
217 
soundFillBox()218 void OptionsWidget_soundGeneral::soundFillBox()
219 {
220 	QStringList l;
221 	unsigned int cnt, i;
222 	KviModule * m = g_pModuleManager->getModule("snd");
223 
224 	if(!m || !m->ctrl("getAvailableSoundSystems", &l))
225 		goto disable;
226 
227 	m_pSoundSystemBox->clear();
228 
229 	for(const auto& it : l)
230 		m_pSoundSystemBox->addItem(it);
231 
232 	cnt = m_pSoundSystemBox->count();
233 	for(i = 0; i < cnt; i++)
234 	{
235 		QString t = m_pSoundSystemBox->itemText(i);
236 		if(KviQString::equalCI(t, KVI_OPTION_STRING(KviOption_stringSoundSystem)))
237 		{
238 			m_pSoundSystemBox->setCurrentIndex(i);
239 			break;
240 		}
241 	}
242 
243 	return;
244 disable:
245 	m_pSoundSystemBox->clear();
246 	m_pSoundSystemBox->setEnabled(false);
247 	m_pSoundTestButton->setEnabled(false);
248 	m_pSoundAutoDetectButton->setEnabled(false);
249 }
250 
mediaFillBox()251 void OptionsWidget_soundGeneral::mediaFillBox()
252 {
253 	QStringList l;
254 	unsigned int cnt, i;
255 	KviModule * m = g_pModuleManager->getModule("mediaplayer");
256 
257 	if(!m || !m->ctrl("getAvailableMediaPlayers", &l))
258 		goto disable;
259 
260 	m_pMediaPlayerBox->clear();
261 
262 	for(const auto& it : l)
263 		m_pMediaPlayerBox->addItem(it);
264 
265 	cnt = m_pMediaPlayerBox->count();
266 	for(i = 0; i < cnt; i++)
267 	{
268 		QString t = m_pMediaPlayerBox->itemText(i);
269 		if(KviQString::equalCI(t, KVI_OPTION_STRING(KviOption_stringPreferredMediaPlayer)))
270 		{
271 			m_pMediaPlayerBox->setCurrentIndex(i);
272 			break;
273 		}
274 	}
275 
276 	return;
277 disable:
278 	m_pMediaPlayerBox->clear();
279 	m_pMediaPlayerBox->setEnabled(false);
280 	m_pMediaTestButton->setEnabled(false);
281 	m_pMediaAutoDetectButton->setEnabled(false);
282 }
283 
commit()284 void OptionsWidget_soundGeneral::commit()
285 {
286 	// avoid to commit if we've never been shown (and initialized)
287 	if(m_bFirstShow)
288 		return;
289 
290 	KviOptionsWidget::commit();
291 	KVI_OPTION_STRING(KviOption_stringSoundSystem) = m_pSoundSystemBox->currentText();
292 	KVI_OPTION_STRING(KviOption_stringPreferredMediaPlayer) = m_pMediaPlayerBox->currentText();
293 
294 	int idx = m_pTagsEncodingCombo->currentIndex();
295 	if(idx <= 0)
296 	{
297 		// guess from locale
298 		KVI_OPTION_STRING(KviOption_stringMp3TagsEncoding) = "";
299 	}
300 	else
301 	{
302 		KVI_OPTION_STRING(KviOption_stringMp3TagsEncoding) = m_pTagsEncodingCombo->itemText(idx);
303 	}
304 
305 #if defined(COMPILE_ON_WINDOWS) || defined(COMPILE_ON_MINGW)
306 	idx = m_pWinampEncodingCombo->currentIndex();
307 	if(idx <= 0)
308 	{
309 		// guess from locale
310 		KVI_OPTION_STRING(KviOption_stringWinampTextEncoding) = "";
311 	}
312 	else
313 	{
314 		KVI_OPTION_STRING(KviOption_stringWinampTextEncoding) = m_pWinampEncodingCombo->itemText(idx);
315 	}
316 #endif
317 }
318 
OptionsWidget_sounds(QWidget * parent)319 OptionsWidget_sounds::OptionsWidget_sounds(QWidget * parent)
320     : KviOptionsWidget(parent)
321 {
322 	setObjectName("sound_options_widget");
323 	createLayout();
324 	addLabel(0, 0, 0, 0, __tr2qs_ctx("New query opened:", "options"));
325 	addSoundSelector(1, 0, 1, 0, "", KviOption_stringOnNewQueryOpenedSound);
326 
327 	addLabel(0, 1, 0, 1, __tr2qs_ctx("New message in inactive query:", "options"));
328 	addSoundSelector(1, 1, 1, 1, "", KviOption_stringOnQueryMessageSound);
329 
330 	addLabel(0, 2, 0, 2, __tr2qs_ctx("Highlighted message in inactive window:", "options"));
331 	addSoundSelector(1, 2, 1, 2, "", KviOption_stringOnHighlightedMessageSound);
332 
333 	addLabel(0, 3, 0, 3, __tr2qs_ctx("When I am kicked:", "options"));
334 	addSoundSelector(1, 3, 1, 3, "", KviOption_stringOnMeKickedSound);
335 
336 	addRowSpacer(0, 4, 1, 4);
337 }
338 
339 OptionsWidget_sounds::~OptionsWidget_sounds()
340     = default;
341