1 /*
2     This file is part of Knights, a chess board for KDE SC 4.
3     Copyright 2009,2010,2011  Miha Čančula <miha@noughmad.eu>
4     Copyright 2016 Alexander Semke <alexander.semke@web.de>
5 
6     This program is free software; you can redistribute it and/or
7     modify it under the terms of the GNU General Public License as
8     published by the Free Software Foundation; either version 2 of
9     the License or (at your option) version 3 or any later version
10     accepted by the membership of KDE e.V. (or its successor approved
11     by the membership of KDE e.V.), which shall act as a proxy
12     defined in Section 14 of version 3 of the license.
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.  See the
17     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, see <http://www.gnu.org/licenses/>.
21 */
22 
23 #include "proto/ficsdialog.h"
24 #include "ui_ficsdialog.h"
25 #include "settings.h"
26 #include "knightsdebug.h"
27 
28 #include <KWallet>
29 
30 #include <QDesktopServices>
31 
32 using namespace Knights;
33 using KWallet::Wallet;
34 
FicsDialog(QWidget * parent,Qt::WindowFlags f)35 FicsDialog::FicsDialog ( QWidget* parent, Qt::WindowFlags f ) : QWidget ( parent, f ) {
36 	ui = new Ui::FicsDialog;
37 	ui->setupUi ( this );
38 
39 	for ( int i = 1; i < 4; ++i )
40 		ui->tabWidget->setTabEnabled ( i, false );
41 	ui->tabWidget->setCurrentIndex ( 0 );
42 
43 	connect ( ui->tabWidget, &QTabWidget::currentChanged, this, &FicsDialog::currentTabChanged );
44 	connect ( ui->seekButton, &QPushButton::toggled, this, &FicsDialog::seekingChanged );
45 	ui->seekButton->setIcon( QIcon::fromTheme ( QStringLiteral("edit-find") ) );
46 	connect ( ui->logInButton, &QPushButton::clicked, this, &FicsDialog::slotLogin );
47 	ui->logInButton->setIcon ( QIcon::fromTheme ( QStringLiteral ( "network-connect" ) ) );
48 
49 	connect ( ui->registerButton, &QPushButton::clicked, this, &FicsDialog::slotCreateAccount );
50 	ui->registerButton->setIcon ( QIcon::fromTheme ( QStringLiteral ( "list-add" ) ) );
51 
52 	ui->usernameLineEdit->setText ( Settings::ficsUsername() );
53 
54 	ui->challengeListView->setModel ( &m_challengeModel );
55 
56 	connect ( ui->graphView, &SeekGraph::seekClicked, this, &FicsDialog::acceptSeek );
57 
58 	ui->rememberCheckBox->setChecked(Settings::autoLogin());
59 
60 	connect ( ui->rememberCheckBox, &QCheckBox::stateChanged, this, &FicsDialog::rememberCheckBoxChanged );
61 }
62 
~FicsDialog()63 FicsDialog::~FicsDialog() {
64 	delete ui;
65 }
66 
sizeHint() const67 QSize FicsDialog::sizeHint() const {
68 	return QSize(800,500);
69 }
70 
slotSessionStarted()71 void FicsDialog::slotSessionStarted() {
72 	setStatus ( i18n ( "Session started" ) );
73 	ui->logInButton->setEnabled ( false );
74 	for ( int i = 1; i < 4; ++i )
75 		ui->tabWidget->setTabEnabled ( i, true );
76 	ui->tabWidget->setCurrentIndex ( 1 );
77 	Q_EMIT acceptButtonNeeded ( true );
78 	saveFicsSettings();
79 }
80 
slotLogin()81 void FicsDialog::slotLogin() {
82 	setLoginEnabled ( false );
83 	setStatus ( i18n("Logging in...") );
84 	Q_EMIT login ( ui->usernameLineEdit->text(), ui->passwordLineEdit->text() );
85 }
86 
slotCreateAccount()87 void FicsDialog::slotCreateAccount() {
88 	QUrl url;
89 	url.setScheme ( QStringLiteral ( "http" ) );
90 	url.setHost ( serverName );
91 	if ( serverName == QLatin1String ( "freechess.org" ) )
92 		url.setPath ( QStringLiteral ( "/Register/index.html" ) );
93 	QDesktopServices::openUrl(url);
94 }
95 
96 
addGameOffer(const FicsGameOffer & offer)97 void FicsDialog::addGameOffer ( const FicsGameOffer& offer ) {
98 	int row = ui->offerTable->rowCount();
99 	ui->offerTable->insertRow ( row );
100 	m_gameId << offer.gameId;
101 
102 	ui->offerTable->setItem ( row, 0, new QTableWidgetItem ( offer.player.first ) );
103 	if ( offer.player.second != 0 )
104 		ui->offerTable->setItem ( row, 1, new QTableWidgetItem ( QString::number ( offer.player.second ) ) );
105 
106 	QTime baseTime = QTime();
107 	baseTime.setHMS ( 0, offer.baseTime, 0 );
108 	ui->offerTable->setItem ( row, 2, new QTableWidgetItem (baseTime.toString()) );
109 
110 	QTime incTime = QTime();
111 	incTime.setHMS ( 0, 0, offer.timeIncrement );
112 	ui->offerTable->setItem ( row, 3, new QTableWidgetItem ( incTime.toString()) );
113 
114 	QCheckBox* rated = new QCheckBox ( this );
115 	rated->setEnabled ( false );
116 	rated->setChecked ( offer.rated );
117 	ui->offerTable->setCellWidget ( row, 4, rated );
118 	ui->offerTable->setItem ( row, 5, new QTableWidgetItem ( offer.variant ) );
119 	ui->offerTable->resizeColumnToContents(0);
120 	ui->graphView->addSeek( offer );
121 }
122 
addChallenge(const FicsChallenge & challenge)123 void FicsDialog::addChallenge ( const FicsChallenge& challenge ) {
124 	QString item = i18nc ( "PlayerName (rating)", "%1 (%2)", challenge.player.first, challenge.player.second );
125 	m_challengeModel.setStringList ( m_challengeModel.stringList() << item );
126 	m_challengeId << challenge.gameId;
127 	Q_EMIT declineButtonNeeded ( true );
128 }
129 
clearOffers()130 void FicsDialog::clearOffers() {
131 	ui->offerTable->setRowCount ( 0 );
132 	ui->graphView->clearOffers();
133 	m_gameId.clear();
134 }
135 
accept()136 void FicsDialog::accept() {
137 	if ( ui->seekButton->isChecked() )
138 		Q_EMIT acceptChallenge( m_challengeId[ui->challengeListView->currentIndex().row()] );
139 	else
140 		Q_EMIT acceptSeek ( m_gameId[ui->offerTable->currentRow() ] );
141 }
142 
decline()143 void FicsDialog::decline() {
144 	Q_EMIT declineChallenge ( m_challengeId[ui->challengeListView->currentIndex().row()] );
145 }
146 
currentTabChanged(int tab)147 void FicsDialog::currentTabChanged ( int tab ) {
148 	Q_EMIT declineButtonNeeded ( tab == 3 );
149 	Q_EMIT acceptButtonNeeded ( tab == 1 || tab == 2 || tab == 3 );
150 }
151 
setServerName(const QString & name)152 void FicsDialog::setServerName ( const QString& name ) {
153 	qCDebug(LOG_KNIGHTS) << name;
154 	WId id = 0;
155 	if ( qApp->activeWindow() )
156 		id = qApp->activeWindow()->winId();
157 	QString password;
158 	Wallet* wallet = Wallet::openWallet ( Wallet::NetworkWallet(), id );
159 	if ( wallet ) {
160 		QLatin1String folder ( "Knights" );
161 		if ( !wallet->hasFolder ( folder ) )
162 			wallet->createFolder ( folder );
163 		wallet->setFolder ( folder );
164 		QString key = ui->usernameLineEdit->text() + QLatin1Char ( '@' ) + name;
165 		wallet->readPassword ( key, password );
166 	} else
167 		qCDebug(LOG_KNIGHTS) << "KWallet not available";
168 	ui->passwordLineEdit->setText ( password );
169 	serverName = name;
170 }
171 
setConsoleWidget(QWidget * widget)172 void FicsDialog::setConsoleWidget ( QWidget* widget ) {
173 	ui->tabWidget->widget ( 4 )->layout()->addWidget ( widget );
174 }
175 
focusOnLogin()176 void FicsDialog::focusOnLogin() {
177 	ui->tabWidget->setCurrentIndex ( 0 );
178 }
179 
setStatus(const QString & status,bool error)180 void FicsDialog::setStatus ( const QString& status, bool error ) {
181 	if ( error )
182 		ui->logInStatusLabel->setText ( i18n ( "<font color='red'>Error: %1</font>", status ) );
183 	else
184 		ui->logInStatusLabel->setText ( status );
185 }
186 
remember()187 bool FicsDialog::remember() {
188 	return ui->rememberCheckBox->isChecked();
189 }
190 
saveFicsSettings()191 void FicsDialog::saveFicsSettings() {
192 	Settings::setAutoLogin(ui->rememberCheckBox->isChecked());
193 
194 	Settings::setFicsUsername ( ui->usernameLineEdit->text() );
195 	Settings::setGuest ( !ui->registeredCheckBox->isChecked() );
196 
197 	WId id = 0;
198 	if ( qApp->activeWindow() )
199 		id = qApp->activeWindow()->winId();
200 	Wallet* wallet = Wallet::openWallet ( Wallet::NetworkWallet(), id );
201 	if ( wallet ) {
202 		QLatin1String folder ( "Knights" );
203 		if ( !wallet->hasFolder ( folder ) )
204 			wallet->createFolder ( folder );
205 		wallet->setFolder ( folder );
206 		QString key = ui->usernameLineEdit->text() + QLatin1Char ( '@' ) + serverName;
207 		wallet->writePassword ( key, ui->passwordLineEdit->text() );
208 	}
209 	Settings::self()->save();
210 }
211 
removeGameOffer(int id)212 void FicsDialog::removeGameOffer ( int id ) {
213 	if (!m_gameId.contains(id))
214 		return;
215 	ui->offerTable->removeRow(m_gameId.indexOf(id));
216 	ui->graphView->removeSeek(id);
217 	m_gameId.removeAll(id);
218 }
219 
setLoginEnabled(bool enable)220 void FicsDialog::setLoginEnabled ( bool enable ) {
221 	ui->logInButton->setEnabled ( enable );
222 }
223 
removeChallenge(int id)224 void FicsDialog::removeChallenge ( int id ) {
225 	m_challengeModel.removeRows ( m_challengeId.indexOf(id), 1 );
226 	m_challengeId.removeAll(id);
227 }
228 
autoAcceptChallenge()229 bool FicsDialog::autoAcceptChallenge() {
230 	return ui->autoCheckBox->isChecked();
231 }
232 
rated()233 bool FicsDialog::rated() {
234 	return ui->ratedCheckBox->isChecked();
235 }
236 
rememberCheckBoxChanged(int state)237 void FicsDialog::rememberCheckBoxChanged( int state ) {
238 	Q_UNUSED(state)
239 	Settings::setAutoLogin(ui->rememberCheckBox->isChecked());
240 	Settings::self()->save();
241 }
242