1 
2 /***************************************************************************
3                    jabberaccountwidget.cpp  -  Account widget for Jabber
4                              -------------------
5     begin                : Mon Dec 9 2002
6     copyright            : (C) 2002-2003 by Till Gerken <till@tantalo.net>
7                            Based on code by Olivier Goffart <ogoffart@kde.org>
8     email                : kopete-devel@kde.org
9  ***************************************************************************/
10 
11 /***************************************************************************
12  *                                                                         *
13  *   This program is free software; you can redistribute it and/or modify  *
14  *   it under the terms of the GNU General Public License as published by  *
15  *   the Free Software Foundation; either version 2 of the License, or     *
16  *   (at your option) any later version.                                   *
17  *                                                                         *
18  ***************************************************************************/
19 
20 #include "jabbereditaccountwidget.h"
21 #include "jabber_protocol_debug.h"
22 #include <qlineedit.h>
23 #include <qcheckbox.h>
24 #include <qpushbutton.h>
25 #include <qspinbox.h>
26 #include <qlabel.h>
27 
28 #include <kconfig.h>
29 #include <kmessagebox.h>
30 #include <KLocalizedString>
31 
32 #include <QLineEdit>
33 #include <kopetepassword.h>
34 #include <kopetepasswordedaccount.h>
35 
36 //#include <solid/devicenotifier.h>
37 //#include <solid/device.h>
38 #include <KSharedConfig>
39 
40 #include "kopetecontact.h"
41 
42 #include "kopeteuiglobal.h"
43 #include "kopetepasswordwidget.h"
44 
45 #include "jabberclient.h"
46 #include "jabberregisteraccount.h"
47 #include "dlgjabberchangepassword.h"
48 #include "dlgjabberxoauth2.h"
49 #include "privacydlg.h"
50 
51 #include "xmpp.h"
52 
53 #ifdef JINGLE_SUPPORT
54 //FIXME:Should be replaced by Solid.
55 #include "alsaio.h"
56 #endif
57 
JabberEditAccountWidget(JabberProtocol * proto,JabberAccount * ident,QWidget * parent)58 JabberEditAccountWidget::JabberEditAccountWidget (JabberProtocol * proto, JabberAccount * ident, QWidget * parent)
59 						: QWidget(parent), DlgJabberEditAccountWidget(), KopeteEditAccountWidget (ident)
60 {
61 	setupUi(this);
62 
63 	m_protocol = proto;
64 
65 	connect (mID, SIGNAL (textChanged(QString)), this, SLOT (updateServerField()));
66 	connect (cbCustomServer, SIGNAL (toggled(bool)), this, SLOT (updateServerField()));
67 
68 	connect (cbUseSSL, SIGNAL (toggled(bool)), this, SLOT (sslToggled(bool)));
69 
70 	connect (btnChangePassword, SIGNAL (clicked()), this, SLOT (slotChangePasswordClicked()));
71 
72 	connect (btnXOAuth2, SIGNAL (clicked()), this, SLOT (slotManageXOAuth2Clicked()));
73 
74 	connect (privacyListsButton, SIGNAL (clicked()), this, SLOT (slotPrivacyListsClicked()) );
75 
76 	connect (cbAdjustPriority, SIGNAL (toggled(bool)), this, SLOT (awayPriorityToggled(bool)));
77 
78 #ifdef JINGLE_SUPPORT
79 	checkAudioDevices();
80 #else
81 	/*Remove the Jingle tab*/
82 	for (int i = 0; i < tabWidget10->count(); i++)
83 	{
84 		if (tabWidget10->tabText(i) == QLatin1String("&Jingle"))
85 		{
86 			tabWidget10->removeTab(i);
87 			break;
88 		}
89 	}
90 #endif
91 
92 #ifndef LIBJINGLE_SUPPORT
93 	//Remove Libjingle tab
94 	for ( int i=0; i<tabWidget10->count(); ++i )
95 	{
96 		if ( tabWidget10->tabText(i) == QLatin1String("&Libjingle") )
97 		{
98 			tabWidget10->removeTab(i);
99 			break;
100 		}
101 	}
102 #endif
103 
104 	if (account())
105 	{
106 		// we are working with an existing account
107 		reopen ();
108 		registrationGroupBox->hide();
109 		btnRegister->setEnabled ( false );
110 
111 		if ( account()->myself()->isOnline() )
112 			privacyListsButton->setEnabled (true);
113 		else
114 			privacyListsButton->setEnabled (false);
115 	}
116 	else
117 	{
118 		// this is a new account
119 		changePasswordGroupBox->hide();
120 		btnChangePassword->setEnabled ( false );
121 		connect (btnRegister, SIGNAL (clicked()), this, SLOT (registerClicked()));
122 
123 		privacyListsButton->setEnabled (false);
124 	}
125     if (parent && parent->layout()) {
126         parent->layout()->addWidget(this);
127     }
128 }
129 
~JabberEditAccountWidget()130 JabberEditAccountWidget::~JabberEditAccountWidget ()
131 {
132 }
133 
134 #ifdef JINGLE_SUPPORT
checkAudioDevices()135 void JabberEditAccountWidget::checkAudioDevices()
136 {
137 	qCDebug(JABBER_PROTOCOL_LOG) << "Start.";
138 	/*Solid::DeviceNotifier *notifier = Solid::DeviceNotifier::instance();
139 	foreach (const Solid::Device &device, Solid::Device::listFromType(Solid::DeviceInterface::AudioInterface, QString()))
140 	//foreach (const Solid::Device &device, Solid::Device::allDevices())
141 	{
142 		qCDebug(JABBER_PROTOCOL_LOG) << "Found :";
143 		qCDebug(JABBER_PROTOCOL_LOG) << device.udi().toLatin1().constData();
144 	}*/
145 	QList<Item> devices = getAlsaItems();
146 	for (int i = 0; i < devices.count(); i++)
147 	{
148 		if (devices.at(i).dir == Item::Input)
149 		{
150 			qCDebug(JABBER_PROTOCOL_LOG) << "Microphone :" << devices.at(i).name << "(" << devices.at(i).id << ")";
151 			audioInputsCombo->addItem(devices.at(i).name);
152 			inputDevices << devices.at(i);
153 		}
154 		else if (devices.at(i).dir == Item::Output)
155 		{
156 			qCDebug(JABBER_PROTOCOL_LOG) << "Audio output :" << devices.at(i).name << "(" << devices.at(i).id << ")";
157 			audioOutputsCombo->addItem(devices.at(i).name);
158 			outputDevices << devices.at(i);
159 		}
160 
161 	}
162 	qCDebug(JABBER_PROTOCOL_LOG) << "End.";
163 
164 }
165 #endif
166 
account()167 JabberAccount *JabberEditAccountWidget::account ()
168 {
169 
170 	return dynamic_cast<JabberAccount *>(KopeteEditAccountWidget::account () );
171 
172 }
173 
reopen()174 void JabberEditAccountWidget::reopen ()
175 {
176 
177 	// FIXME: this is temporary until Kopete supports accound ID changes!
178 	mID->setReadOnly(true);
179 
180 	mID->setText (account()->accountId ());
181 	mPass->load (&account()->password ());
182 	cbAutoConnect->setChecked (account()->excludeConnect());
183 
184 	mResource->setText (account()->configGroup()->readEntry ("Resource", QStringLiteral("Kopete")));
185 	mPriority->setValue (account()->configGroup()->readEntry ("Priority", 5));
186 	mServer->setText (account()->configGroup()->readEntry ("Server", QString()));
187 
188 	cbUseSSL->setChecked (account()->configGroup()->readEntry( "UseSSL", false));
189 
190 	mPort->setValue (account()->configGroup()->readEntry("Port", 5222));
191 
192 	QString auth = account()->configGroup()->readEntry("AuthType", QString());
193 
194 	cbCustomServer->setChecked (account()->configGroup()->readEntry("CustomServer",false));
195 
196 	if(cbCustomServer->isChecked ())
197 	{
198 		labelServer->setEnabled(true);
199 		mServer->setEnabled(true);
200 		labelPort->setEnabled(true);
201 		mPort->setEnabled(true);
202 	}
203 	else
204 	{
205 		mServer->setEnabled (false);
206 		mServer->setText(mID->text().section('@', 1));
207 	}
208 
209 	if ( account()->configGroup()->hasKey("AwayPriority") )
210 	{
211 		cbAdjustPriority->setChecked(true);
212 		mAwayPriority->setValue( account()->configGroup()->readEntry("AwayPriority",0));
213 	}
214 	else
215 	{
216 		cbAdjustPriority->setChecked(false);
217 		mAwayPriority->setEnabled(false);
218 	}
219 
220 	cbAllowPlainTextPassword->setChecked (account()->configGroup()->readEntry("AllowPlainTextPassword", true));
221 	cbUseXOAuth2->setChecked (account()->configGroup()->readEntry("UseXOAuth2", false));
222 
223 	KConfigGroup config = KSharedConfig::openConfig()->group("Jabber");
224 	leLocalIP->setText (config.readEntry("LocalIP", QString()));
225 	sbLocalPort->setValue (config.readEntry("LocalPort", 8010));
226 
227 	leProxyJID->setText (account()->configGroup()->readEntry("ProxyJID", QString()));
228 
229 #ifdef JINGLE_SUPPORT
230 	//Jingle
231 	firstPortEdit->setValue(account()->configGroup()->readEntry("JingleFirstPort", QString("9000")).toInt());
232 
233 	for (int i = 0; i < inputDevices.count(); i++)
234 	{
235 		if (inputDevices.at(i).id == account()->configGroup()->readEntry("JingleInputDevice", QString("default")))
236 		{
237 			audioInputsCombo->setCurrentIndex(i);
238 			break;
239 		}
240 	}
241 
242 	for (int i = 0; i < outputDevices.count(); i++)
243 	{
244 		if (outputDevices.at(i).id == account()->configGroup()->readEntry("JingleOutputDevice", QString("default")))
245 		{
246 			audioOutputsCombo->setCurrentIndex(i);
247 			break;
248 		}
249 	}
250 
251 	autoDetectIPBox->setChecked(account()->configGroup()->readEntry("JingleAutoDetectIP", false));
252 #endif
253 
254 	// Privacy
255 	cbSendEvents->setChecked( account()->configGroup()->readEntry("SendEvents", true) );
256 	cbSendDeliveredEvent->setChecked( account()->configGroup()->readEntry("SendDeliveredEvent", true) );
257 	cbSendDisplayedEvent->setChecked( account()->configGroup()->readEntry("SendDisplayedEvent", true) );
258 	cbSendComposingEvent->setChecked( account()->configGroup()->readEntry("SendComposingEvent", true) );
259 	cbSendGoneEvent->setChecked( account()->configGroup()->readEntry("SendGoneEvent", true) );
260 
261 	cbHideSystemInfo->setChecked( account()->configGroup()->readEntry("HideSystemInfo", false) );
262 
263 	mergeMessages->setChecked(account()->mergeMessages());
264 	oldEncrypted->setChecked(account()->oldEncrypted());
265 
266 #ifdef LIBJINGLE_SUPPORT
267 	Libjingle->setChecked(account()->enabledLibjingle());
268 #endif
269 
270 }
271 
apply()272 Kopete::Account *JabberEditAccountWidget::apply ()
273 {
274 	qCDebug(JABBER_PROTOCOL_LOG) << "JabberEditAccount::apply()";
275 
276 	if (!account())
277 	{
278 		setAccount(new JabberAccount (m_protocol, mID->text ()));
279 	}
280 
281 	if(account()->isConnected())
282 	{
283 		KMessageBox::information(this,
284 					i18n("The changes you just made will take effect next time you log in with Jabber."),
285 					i18n("Jabber Changes During Online Jabber Session"));
286 	}
287 
288 	this->writeConfig ();
289 
290 	static_cast<JabberAccount*>(account())->setS5BServerPort ( sbLocalPort->value () );
291 
292 	return account();
293 }
294 
writeConfig()295 void JabberEditAccountWidget::writeConfig ()
296 {
297 	account()->configGroup()->writeEntry("UseSSL", cbUseSSL->isChecked());
298 
299 	if (!cbUseXOAuth2->isChecked())
300 	mPass->save(&account()->password ());
301 
302 	account()->configGroup()->writeEntry("CustomServer", cbCustomServer->isChecked());
303 
304 	// FIXME: The call below represents a flaw in the current Kopete API.
305 	// Once the API is cleaned up, this will most likely require a change.
306 	//account()->setAccountId(mID->text());
307 
308 	account()->configGroup()->writeEntry("AllowPlainTextPassword", cbAllowPlainTextPassword->isChecked());
309 	account()->configGroup()->writeEntry("UseXOAuth2", cbUseXOAuth2->isChecked());
310 	account()->configGroup()->writeEntry("Server", mServer->text().trimmed ());
311 	account()->configGroup()->writeEntry("Resource", mResource->text ());
312 	account()->configGroup()->writeEntry("Priority", QString::number (mPriority->value ()));
313 
314 	if ( cbAdjustPriority->isChecked() )
315 	{
316 		account()->configGroup()->writeEntry("AwayPriority", QString::number( mAwayPriority->value ()));
317 	}
318 	else
319 	{
320 		account()->configGroup()->deleteEntry("AwayPriority");
321 	}
322 
323 	account()->configGroup()->writeEntry("Port", QString::number (mPort->value ()));
324 
325 #ifdef JINGLE_SUPPORT
326 	account()->configGroup()->writeEntry("JingleFirstPort", QString::number(firstPortEdit->value()));
327 	account()->configGroup()->writeEntry("JingleInputDevice", inputDevices.at(audioInputsCombo->currentIndex()).id);
328 	account()->configGroup()->writeEntry("JingleOutputDevice", outputDevices.at(audioOutputsCombo->currentIndex()).id);
329 
330 	account()->configGroup()->writeEntry("JingleAutoDetectIP", autoDetectIPBox->isChecked());
331 #endif
332 
333 	account()->setExcludeConnect(cbAutoConnect->isChecked());
334 
335 	KConfigGroup config = KSharedConfig::openConfig()->group("Jabber");
336 
337 	config.writeEntry("LocalIP", leLocalIP->text());
338 	config.writeEntry("LocalPort", sbLocalPort->value());
339 
340 	account()->configGroup()->writeEntry("ProxyJID", leProxyJID->text());
341 
342 	// Privacy
343 	account()->configGroup()->writeEntry("SendEvents", cbSendEvents->isChecked());
344 	account()->configGroup()->writeEntry("SendDeliveredEvent", cbSendDeliveredEvent->isChecked());
345 	account()->configGroup()->writeEntry("SendDisplayedEvent", cbSendDisplayedEvent->isChecked());
346 	account()->configGroup()->writeEntry("SendComposingEvent", cbSendComposingEvent->isChecked());
347 	account()->configGroup()->writeEntry("SendGoneEvent", cbSendGoneEvent->isChecked());
348 
349 	account()->configGroup()->writeEntry("HideSystemInfo", cbHideSystemInfo->isChecked());
350 
351 	account()->setMergeMessages(mergeMessages->isChecked());
352 	account()->setOldEncrypted(oldEncrypted->isChecked());
353 
354 #ifdef LIBJINGLE_SUPPORT
355 	account()->enableLibjingle(Libjingle->isChecked() && !cbUseXOAuth2->isChecked());
356 #endif
357 
358 }
359 
validateData()360 bool JabberEditAccountWidget::validateData ()
361 {
362 
363 	if(!mID->text().contains('@'))
364 	{
365 		KMessageBox::sorry(this, i18n("The Jabber ID you have chosen is invalid. "
366 							"Please make sure it is in the form user@server.com, like an email address."),
367 							i18n("Invalid Jabber ID"));
368 
369 		return false;
370 	}
371 
372 	return true;
373 }
374 
updateServerField()375 void JabberEditAccountWidget::updateServerField ()
376 {
377 
378 	if(!cbCustomServer->isChecked())
379 	{
380 		QString newServer = mID->text().section('@', 1);
381 		mPort->setValue(5222);
382 		// check if ssl is enabled and set the port correctly
383 		sslToggled(cbUseSSL->isChecked());
384 		mServer->setText(newServer);
385 		labelServer->setEnabled(false);
386 		mServer->setEnabled(false);
387 		labelPort->setEnabled(false);
388 		mPort->setEnabled(false);
389 	}
390 	else
391 	{
392 		labelServer->setEnabled(true);
393 		mServer->setEnabled(true);
394 		labelPort->setEnabled(true);
395 		mPort->setEnabled(true);
396 	}
397 
398 }
399 
awayPriorityToggled(bool enabled)400 void JabberEditAccountWidget::awayPriorityToggled(bool enabled)
401 {
402 	mAwayPriority->setEnabled(enabled);
403 }
404 
deleteClicked()405 void JabberEditAccountWidget::deleteClicked ()
406 {
407 
408 	// delete account here
409 
410 }
411 
registerClicked()412 void JabberEditAccountWidget::registerClicked ()
413 {
414 
415 	JabberRegisterAccount *registerDlg = new JabberRegisterAccount ( this );
416 
417 	registerDlg->show ();
418 
419 }
420 
slotChangePasswordClicked()421 void JabberEditAccountWidget::slotChangePasswordClicked ()
422 {
423 
424 	DlgJabberChangePassword *passwordDlg = new DlgJabberChangePassword ( account (), this );
425 
426 	connect ( passwordDlg, SIGNAL (destroyed()), this, SLOT (slotChangePasswordFinished()) );
427 
428 	passwordDlg->show ();
429 
430 }
431 
slotChangePasswordFinished()432 void JabberEditAccountWidget::slotChangePasswordFinished ()
433 {
434 
435 	// in case the password has been changed, we need to update it in the UI
436 	reopen ();
437 
438 }
439 
slotManageXOAuth2Clicked()440 void JabberEditAccountWidget::slotManageXOAuth2Clicked ()
441 {
442 
443 	DlgJabberXOAuth2 *xoath2Dlg = new DlgJabberXOAuth2 ( account(), this );
444 
445 	xoath2Dlg->show();
446 
447 	mPass->setPassword ( QString() );
448 
449 }
450 
sslToggled(bool value)451 void JabberEditAccountWidget::sslToggled (bool value)
452 {
453 	if (value && (mPort->value() == 5222))
454 		mPort->stepUp ();
455 	else
456 		if(!value && (mPort->value() == 5223))
457 			mPort->stepDown ();
458 }
459 
slotPrivacyListsClicked()460 void JabberEditAccountWidget::slotPrivacyListsClicked()
461 {
462 	PrivacyDlg * dialog = new PrivacyDlg (account(), this);
463 	dialog->show();
464 }
465 
466