File indexing completed on 2024-05-12 04:04:15
0001 /* 0002 This file is part of Knights, a chess board for KDE SC 4. 0003 SPDX-FileCopyrightText: 2009, 2010, 2011 Miha Čančula <miha@noughmad.eu> 0004 SPDX-FileCopyrightText: 2016 Alexander Semke <alexander.semke@web.de> 0005 0006 SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0007 */ 0008 0009 #include "proto/ficsdialog.h" 0010 #include "ui_ficsdialog.h" 0011 #include "settings.h" 0012 #include "knightsdebug.h" 0013 0014 #include <KWallet> 0015 0016 #include <QDesktopServices> 0017 0018 using namespace Knights; 0019 using KWallet::Wallet; 0020 0021 FicsDialog::FicsDialog ( QWidget* parent, Qt::WindowFlags f ) : QWidget ( parent, f ) { 0022 ui = new Ui::FicsDialog; 0023 ui->setupUi ( this ); 0024 0025 for ( int i = 1; i < 4; ++i ) 0026 ui->tabWidget->setTabEnabled ( i, false ); 0027 ui->tabWidget->setCurrentIndex ( 0 ); 0028 0029 connect ( ui->tabWidget, &QTabWidget::currentChanged, this, &FicsDialog::currentTabChanged ); 0030 connect ( ui->seekButton, &QPushButton::toggled, this, &FicsDialog::seekingChanged ); 0031 ui->seekButton->setIcon( QIcon::fromTheme ( QStringLiteral("edit-find") ) ); 0032 connect ( ui->logInButton, &QPushButton::clicked, this, &FicsDialog::slotLogin ); 0033 ui->logInButton->setIcon ( QIcon::fromTheme ( QStringLiteral ( "network-connect" ) ) ); 0034 0035 connect ( ui->registerButton, &QPushButton::clicked, this, &FicsDialog::slotCreateAccount ); 0036 ui->registerButton->setIcon ( QIcon::fromTheme ( QStringLiteral ( "list-add" ) ) ); 0037 0038 ui->usernameLineEdit->setText ( Settings::ficsUsername() ); 0039 0040 ui->challengeListView->setModel ( &m_challengeModel ); 0041 0042 connect ( ui->graphView, &SeekGraph::seekClicked, this, &FicsDialog::acceptSeek ); 0043 0044 ui->rememberCheckBox->setChecked(Settings::autoLogin()); 0045 0046 connect ( ui->rememberCheckBox, &QCheckBox::stateChanged, this, &FicsDialog::rememberCheckBoxChanged ); 0047 } 0048 0049 FicsDialog::~FicsDialog() { 0050 delete ui; 0051 } 0052 0053 QSize FicsDialog::sizeHint() const { 0054 return QSize(800,500); 0055 } 0056 0057 void FicsDialog::slotSessionStarted() { 0058 setStatus ( i18n ( "Session started" ) ); 0059 ui->logInButton->setEnabled ( false ); 0060 for ( int i = 1; i < 4; ++i ) 0061 ui->tabWidget->setTabEnabled ( i, true ); 0062 ui->tabWidget->setCurrentIndex ( 1 ); 0063 Q_EMIT acceptButtonNeeded ( true ); 0064 saveFicsSettings(); 0065 } 0066 0067 void FicsDialog::slotLogin() { 0068 setLoginEnabled ( false ); 0069 setStatus ( i18n("Logging in...") ); 0070 Q_EMIT login ( ui->usernameLineEdit->text(), ui->passwordLineEdit->text() ); 0071 } 0072 0073 void FicsDialog::slotCreateAccount() { 0074 QUrl url; 0075 url.setScheme ( QStringLiteral ( "https" ) ); 0076 url.setHost ( serverName ); 0077 if ( serverName == QLatin1String ( "freechess.org" ) ) 0078 url.setPath ( QStringLiteral ( "/Register/index.html" ) ); 0079 QDesktopServices::openUrl(url); 0080 } 0081 0082 0083 void FicsDialog::addGameOffer ( const FicsGameOffer& offer ) { 0084 int row = ui->offerTable->rowCount(); 0085 ui->offerTable->insertRow ( row ); 0086 m_gameId << offer.gameId; 0087 0088 ui->offerTable->setItem ( row, 0, new QTableWidgetItem ( offer.player.first ) ); 0089 if ( offer.player.second != 0 ) 0090 ui->offerTable->setItem ( row, 1, new QTableWidgetItem ( QString::number ( offer.player.second ) ) ); 0091 0092 QTime baseTime = QTime(); 0093 baseTime.setHMS ( 0, offer.baseTime, 0 ); 0094 ui->offerTable->setItem ( row, 2, new QTableWidgetItem (baseTime.toString()) ); 0095 0096 QTime incTime = QTime(); 0097 incTime.setHMS ( 0, 0, offer.timeIncrement ); 0098 ui->offerTable->setItem ( row, 3, new QTableWidgetItem ( incTime.toString()) ); 0099 0100 QCheckBox* rated = new QCheckBox ( this ); 0101 rated->setEnabled ( false ); 0102 rated->setChecked ( offer.rated ); 0103 ui->offerTable->setCellWidget ( row, 4, rated ); 0104 ui->offerTable->setItem ( row, 5, new QTableWidgetItem ( offer.variant ) ); 0105 ui->offerTable->resizeColumnToContents(0); 0106 ui->graphView->addSeek( offer ); 0107 } 0108 0109 void FicsDialog::addChallenge ( const FicsChallenge& challenge ) { 0110 QString item = i18nc ( "PlayerName (rating)", "%1 (%2)", challenge.player.first, challenge.player.second ); 0111 m_challengeModel.setStringList ( m_challengeModel.stringList() << item ); 0112 m_challengeId << challenge.gameId; 0113 Q_EMIT declineButtonNeeded ( true ); 0114 } 0115 0116 void FicsDialog::clearOffers() { 0117 ui->offerTable->setRowCount ( 0 ); 0118 ui->graphView->clearOffers(); 0119 m_gameId.clear(); 0120 } 0121 0122 void FicsDialog::accept() { 0123 if ( ui->seekButton->isChecked() ) 0124 Q_EMIT acceptChallenge( m_challengeId[ui->challengeListView->currentIndex().row()] ); 0125 else 0126 Q_EMIT acceptSeek ( m_gameId[ui->offerTable->currentRow() ] ); 0127 } 0128 0129 void FicsDialog::decline() { 0130 Q_EMIT declineChallenge ( m_challengeId[ui->challengeListView->currentIndex().row()] ); 0131 } 0132 0133 void FicsDialog::currentTabChanged ( int tab ) { 0134 Q_EMIT declineButtonNeeded ( tab == 3 ); 0135 Q_EMIT acceptButtonNeeded ( tab == 1 || tab == 2 || tab == 3 ); 0136 } 0137 0138 void FicsDialog::setServerName ( const QString& name ) { 0139 qCDebug(LOG_KNIGHTS) << name; 0140 WId id = 0; 0141 if ( qApp->activeWindow() ) 0142 id = qApp->activeWindow()->winId(); 0143 QString password; 0144 Wallet* wallet = Wallet::openWallet ( Wallet::NetworkWallet(), id ); 0145 if ( wallet ) { 0146 QLatin1String folder ( "Knights" ); 0147 if ( !wallet->hasFolder ( folder ) ) 0148 wallet->createFolder ( folder ); 0149 wallet->setFolder ( folder ); 0150 QString key = ui->usernameLineEdit->text() + QLatin1Char ( '@' ) + name; 0151 wallet->readPassword ( key, password ); 0152 } else 0153 qCDebug(LOG_KNIGHTS) << "KWallet not available"; 0154 ui->passwordLineEdit->setText ( password ); 0155 serverName = name; 0156 } 0157 0158 void FicsDialog::setConsoleWidget ( QWidget* widget ) { 0159 ui->tabWidget->widget ( 4 )->layout()->addWidget ( widget ); 0160 } 0161 0162 void FicsDialog::focusOnLogin() { 0163 ui->tabWidget->setCurrentIndex ( 0 ); 0164 } 0165 0166 void FicsDialog::setStatus ( const QString& status, bool error ) { 0167 if ( error ) 0168 ui->logInStatusLabel->setText ( i18n ( "<font color='red'>Error: %1</font>", status ) ); 0169 else 0170 ui->logInStatusLabel->setText ( status ); 0171 } 0172 0173 bool FicsDialog::remember() { 0174 return ui->rememberCheckBox->isChecked(); 0175 } 0176 0177 void FicsDialog::saveFicsSettings() { 0178 Settings::setAutoLogin(ui->rememberCheckBox->isChecked()); 0179 0180 Settings::setFicsUsername ( ui->usernameLineEdit->text() ); 0181 Settings::setGuest ( !ui->registeredCheckBox->isChecked() ); 0182 0183 WId id = 0; 0184 if ( qApp->activeWindow() ) 0185 id = qApp->activeWindow()->winId(); 0186 Wallet* wallet = Wallet::openWallet ( Wallet::NetworkWallet(), id ); 0187 if ( wallet ) { 0188 QLatin1String folder ( "Knights" ); 0189 if ( !wallet->hasFolder ( folder ) ) 0190 wallet->createFolder ( folder ); 0191 wallet->setFolder ( folder ); 0192 QString key = ui->usernameLineEdit->text() + QLatin1Char ( '@' ) + serverName; 0193 wallet->writePassword ( key, ui->passwordLineEdit->text() ); 0194 } 0195 Settings::self()->save(); 0196 } 0197 0198 void FicsDialog::removeGameOffer ( int id ) { 0199 if (!m_gameId.contains(id)) 0200 return; 0201 ui->offerTable->removeRow(m_gameId.indexOf(id)); 0202 ui->graphView->removeSeek(id); 0203 m_gameId.removeAll(id); 0204 } 0205 0206 void FicsDialog::setLoginEnabled ( bool enable ) { 0207 ui->logInButton->setEnabled ( enable ); 0208 } 0209 0210 void FicsDialog::removeChallenge ( int id ) { 0211 m_challengeModel.removeRows ( m_challengeId.indexOf(id), 1 ); 0212 m_challengeId.removeAll(id); 0213 } 0214 0215 bool FicsDialog::autoAcceptChallenge() { 0216 return ui->autoCheckBox->isChecked(); 0217 } 0218 0219 bool FicsDialog::rated() { 0220 return ui->ratedCheckBox->isChecked(); 0221 } 0222 0223 void FicsDialog::rememberCheckBoxChanged( int state ) { 0224 Q_UNUSED(state) 0225 Settings::setAutoLogin(ui->rememberCheckBox->isChecked()); 0226 Settings::self()->save(); 0227 } 0228 0229 #include "moc_ficsdialog.cpp"