File indexing completed on 2024-04-21 04:04:03

0001 /* This file is part of KsirK.
0002    Copyright (C) 2008 Gael de Chalendar <kleag@free.fr>
0003 
0004    KsirK is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU General Public
0006    License as published by the Free Software Foundation, either version 2
0007    of the License, or (at your option) any later version.
0008 
0009    This program is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    General Public License for more details.
0013 
0014    You should have received a copy of the GNU General Public License
0015    along with this program; if not, write to the Free Software
0016    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
0017    02110-1301, USA
0018 */
0019 
0020 #include "jabbergameui.h"
0021 #include "kgamewin.h"
0022 #include "GameLogic/gameautomaton.h"
0023 #include "ksirksettings.h"
0024 
0025 #include "ksirk_debug.h"
0026 #include <KStringHandler>
0027 #include <kwallet.h>
0028 
0029 #include <QUuid>
0030 
0031 using namespace Ksirk;
0032 using namespace Ksirk::GameLogic;
0033 
0034 KsirkJabberGameWidget::KsirkJabberGameWidget(QWidget* parent) :
0035     QWidget(parent),
0036     m_automaton(nullptr),
0037     m_previousGuiIndex(-1)
0038 {
0039   qCDebug(KSIRK_LOG);
0040   
0041   setupUi(this);
0042   stackedWidget->setCurrentIndex(0);
0043   jabberid->setText(Ksirk::KsirkSettings::jabberId());
0044 
0045   KWallet::Wallet* wallet = KWallet::Wallet::openWallet("ksirk", 0);
0046   if (wallet == nullptr)
0047   {
0048     password->setText(KStringHandler::obscure(Ksirk::KsirkSettings::jabberPassword()));
0049     roompassword->setText(KStringHandler::obscure(Ksirk::KsirkSettings::jabberPassword()));
0050   }
0051   else
0052   {
0053     if (wallet->createFolder("jabber") && wallet->setFolder("jabber"))
0054     {
0055       QByteArray pw;
0056       wallet->readEntry("password", pw);
0057       password->setText(QString::fromUtf8(pw.data()));
0058       wallet->readEntry("roompassword", pw);
0059       roompassword->setText(QString::fromUtf8(pw.data()));
0060     }
0061     delete wallet;
0062   }
0063   roomjid->setText(Ksirk::KsirkSettings::roomJid());
0064   nickname->setText(Ksirk::KsirkSettings::nickname());
0065 
0066   jabberstateled->setState(KLed::Off);
0067   chatroomstateled->setState(KLed::Off);
0068       
0069   connect(connectbutton,&QAbstractButton::clicked,this,&KsirkJabberGameWidget::slotJabberConnectButtonClicked);
0070   connect(joingamebutton,&QAbstractButton::clicked,this,&KsirkJabberGameWidget::slotJoinJabberGame);
0071   QObject::connect ( joinroombutton, &QAbstractButton::clicked, this, &KsirkJabberGameWidget::slotJoinRoom);
0072   
0073   QObject::connect(jabberTable, &QTableWidget::cellClicked, this, &KsirkJabberGameWidget::slotCellClicked);
0074   
0075   QStringList headers;
0076   headers.push_back(i18n("Nickname"));
0077   headers.push_back(i18n("Skin"));
0078   headers.push_back(i18n("Nb players"));
0079   jabberTable->setHorizontalHeaderLabels(headers);
0080 
0081   QObject::connect(cancelbutton, &QAbstractButton::clicked,this,&KsirkJabberGameWidget::slotCancel);
0082 }
0083 
0084 void KsirkJabberGameWidget::init(GameAutomaton* automaton)
0085 {
0086   qCDebug(KSIRK_LOG);
0087   m_automaton = automaton;
0088   jabberstateled->setState(
0089       (m_automaton->game()->jabberClient()->isConnected())
0090       ? KLed::On : KLed::Off);
0091   
0092   QObject::connect(startnewgamebutton,&QAbstractButton::clicked,m_automaton->game(),&KGameWindow::slotNewJabberGame);
0093   QObject::connect ( m_automaton->game()->jabberClient(), &JabberClient::csDisconnected, this, &KsirkJabberGameWidget::slotJabberDisconnected );
0094   QObject::connect ( m_automaton->game()->jabberClient(), &JabberClient::csError, this, &KsirkJabberGameWidget::slotJabberError );
0095   QObject::connect ( m_automaton->game()->jabberClient(), &JabberClient::tlsWarning, this, &KsirkJabberGameWidget::slotHandleTLSWarning );
0096   QObject::connect ( m_automaton->game()->jabberClient(), &JabberClient::connected, this, &KsirkJabberGameWidget::slotJabberConnected );
0097   QObject::connect ( m_automaton->game()->jabberClient(), &JabberClient::error, this, &KsirkJabberGameWidget::slotJabberClientError );
0098   QObject::connect ( m_automaton->game()->jabberClient(), &JabberClient::rosterRequestFinished, this, &KsirkJabberGameWidget::slotRosterRequestFinished );
0099   QObject::connect ( m_automaton->game()->jabberClient(), &JabberClient::groupChatJoined, this, &KsirkJabberGameWidget::slotGroupChatJoined);
0100   QObject::connect ( m_automaton->game()->jabberClient(), &JabberClient::groupChatLeft, this, &KsirkJabberGameWidget::slotGroupChatLeft);
0101   QObject::connect ( m_automaton->game()->jabberClient(), &JabberClient::groupChatPresence, this, &KsirkJabberGameWidget::slotGroupChatPresence);
0102   QObject::connect ( m_automaton->game()->jabberClient(), &JabberClient::groupChatError, this, &KsirkJabberGameWidget::slotGroupChatError);
0103   QObject::connect(automaton, &GameAutomaton::newJabberGame, this, &KsirkJabberGameWidget::slotNewJabberGame );
0104   QObject::connect(this, &KsirkJabberGameWidget::cancelled, m_automaton->game(), &KGameWindow::slotJabberGameCanceled);
0105 }
0106 
0107 void KsirkJabberGameWidget::slotJabberConnectButtonClicked()
0108 {
0109   qCDebug(KSIRK_LOG);
0110   
0111   KsirkSettings::setJabberId(jabberid->text());
0112   XMPP::Jid jid(jabberid->text()+'/'+jabberid->text());
0113   QString pass = password->text();
0114 
0115 
0116   KWallet::Wallet* wallet = KWallet::Wallet::openWallet("ksirk", 0);
0117   if (wallet == nullptr)
0118   {
0119     KsirkSettings::setJabberPassword(KStringHandler::obscure(pass));
0120   }
0121   else
0122   {
0123     if (wallet->createFolder("jabber") && wallet->setFolder("jabber"))
0124     {
0125       wallet->writeEntry("password", pass.toUtf8());
0126     }
0127     delete wallet;
0128   }
0129   
0130   KsirkSettings::self()->save();
0131 
0132   m_automaton->game()->jabberClient()->setOverrideHost ( true, jid.domain(), 5222 );
0133   JabberClient::ErrorCode res = m_automaton->game()->jabberClient()->connect(jid, pass);
0134 
0135   switch (res)
0136   {
0137     case JabberClient::Ok:
0138       qCDebug(KSIRK_LOG) << "Succesfull connexion";
0139       m_automaton->game()->jabberClient()->requestRoster ();
0140       break;
0141     case JabberClient::InvalidPassword:
0142       qCCritical(KSIRK_LOG) << "Password used to connect to the server was incorrect.";
0143       break;
0144     case JabberClient::AlreadyConnected:
0145       qCCritical(KSIRK_LOG) << "A new connection was attempted while the previous one hasn't been closed.";
0146       break;
0147     case JabberClient::NoTLS:
0148       qCCritical(KSIRK_LOG) << "Use of TLS has been forced (see @ref forceTLS) but TLS is not available, either server- or client-side.";
0149       break;
0150     case JabberClient::InvalidPasswordForMUC:
0151       qCCritical(KSIRK_LOG) << "A password is require to enter on this Multi-User Chat";
0152       break;
0153     case JabberClient::NicknameConflict:
0154       qCCritical(KSIRK_LOG) << "There is already someone with that nick connected to the Multi-User Chat";
0155       break;
0156     case JabberClient::BannedFromThisMUC:
0157       qCCritical(KSIRK_LOG) << "You can't join this Multi-User Chat because you were bannished";
0158       break;
0159     case JabberClient::MaxUsersReachedForThisMuc:
0160       qCCritical(KSIRK_LOG) << "You can't join this Multi-User Chat because it is full";
0161       break;
0162     default:
0163       qCCritical(KSIRK_LOG) << "Unknown error";
0164   }
0165   
0166 }
0167 
0168 void KsirkJabberGameWidget::slotJabberDisconnected()
0169 {
0170   qCDebug(KSIRK_LOG);
0171   stackedWidget->setCurrentIndex(0);
0172   jabberstateled->setState(KLed::Off);
0173 }
0174 
0175 void KsirkJabberGameWidget::slotJabberError(int error)
0176 {
0177   qCDebug(KSIRK_LOG) << error;
0178 }
0179 
0180 void KsirkJabberGameWidget::slotHandleTLSWarning(QCA::TLS::IdentityResult result, QCA::Validity validity)
0181 {
0182   qCDebug(KSIRK_LOG) << result << validity;
0183 }
0184 
0185 void KsirkJabberGameWidget::slotJabberConnected()
0186 {
0187   qCDebug(KSIRK_LOG) << "Connected to Jabber server.";
0188   jabberstateled->setState(KLed::On);
0189 }
0190 
0191 void KsirkJabberGameWidget::slotJabberClientError(JabberClient::ErrorCode error)
0192 {
0193   qCDebug(KSIRK_LOG) << error;
0194 }
0195 
0196 void KsirkJabberGameWidget::slotRosterRequestFinished ( bool success )
0197 {
0198   qCDebug(KSIRK_LOG) << success;
0199   if ( success )
0200   {
0201     stackedWidget->setCurrentIndex(1);
0202   }
0203 }
0204 
0205 void KsirkJabberGameWidget::slotJoinRoom()
0206 {
0207   qCDebug(KSIRK_LOG) << "Joining group chat...";
0208   XMPP::Jid roomJid(roomjid->text());
0209   QString groupchatHost = roomJid.domain();
0210   m_automaton->game()->setGroupchatHost(groupchatHost);
0211   QString groupchatRoom = roomJid.node();
0212   m_automaton->game()->setGroupchatRoom(groupchatRoom);
0213   QString groupchatNick = nickname->text();
0214   m_automaton->game()->setGroupchatNick(groupchatNick);
0215   QString groupchatPassword = roompassword->text();
0216   m_automaton->game()->setGroupchatPassword(groupchatPassword);
0217   KsirkSettings::setRoomJid(groupchatRoom+'@'+groupchatHost);
0218   KsirkSettings::setNickname(groupchatNick);
0219   KWallet::Wallet* wallet = KWallet::Wallet::openWallet("ksirk", 0);
0220   if (wallet == nullptr)
0221   {
0222     KsirkSettings::setRoomPassword(KStringHandler::obscure(groupchatPassword));
0223   }
0224   else
0225   {
0226     if (wallet->createFolder("jabber") && wallet->setFolder("jabber"))
0227     {
0228       wallet->writeEntry("roompassword", groupchatPassword.toUtf8());
0229     }
0230     delete wallet;
0231   }
0232   KsirkSettings::self()->save();
0233 
0234 
0235   if (groupchatPassword.isEmpty())
0236     m_automaton->game()->jabberClient()->joinGroupChat ( groupchatHost, groupchatRoom, groupchatNick);
0237   else
0238     m_automaton->game()->jabberClient()->joinGroupChat ( groupchatHost, groupchatRoom, groupchatNick, groupchatPassword);
0239 }
0240 
0241 void KsirkJabberGameWidget::slotGroupChatJoined(const XMPP::Jid & jid)
0242 {
0243   qCDebug(KSIRK_LOG) << jid.full();
0244   chatroomstateled->setState(KLed::On);
0245 
0246   qCDebug(KSIRK_LOG) << "Joined groupchat " << jid.full ();
0247 
0248   startnewgamebutton->setEnabled(true);
0249   joingamebutton->setEnabled(true);
0250   loadsavedgamebutton->setEnabled(true);
0251 }
0252 
0253 void KsirkJabberGameWidget::slotGroupChatLeft (const XMPP::Jid & jid)
0254 {
0255   qCDebug(KSIRK_LOG);
0256   Q_UNUSED(jid);
0257   chatroomstateled->setState(KLed::Off);
0258 
0259   startnewgamebutton->setEnabled(false);
0260   joingamebutton->setEnabled(false);
0261   loadsavedgamebutton->setEnabled(false);
0262 }
0263 
0264 void KsirkJabberGameWidget::slotGroupChatPresence (const XMPP::Jid & jid, const XMPP::Status & status)
0265 {
0266   qCDebug(KSIRK_LOG) << jid.full() << status.isAvailable();
0267   if (!status.isAvailable())
0268   {
0269     int i = 0;
0270     while ( i < jabberTable->rowCount())
0271     {
0272       if (jabberTable->itemAt(0,i)->text() == jid.full())
0273       {
0274         jabberTable->removeRow(i);
0275       }
0276       else
0277       {
0278         i++;
0279       }
0280     }
0281   }
0282 }
0283 
0284 void KsirkJabberGameWidget::slotGroupChatError (const XMPP::Jid & jid, int error, const QString & reason)
0285 {
0286   qCDebug(KSIRK_LOG) << jid.full() << error << reason;
0287 }
0288 
0289 void KsirkJabberGameWidget::slotNewJabberGame(const QString& nick,
0290                                         int nbPlayers,
0291                                         const QString& skin
0292                                         )
0293 {
0294   qCDebug(KSIRK_LOG) << nick << nbPlayers << skin;
0295   for (int i = 0; i < jabberTable->rowCount(); i++)
0296   {
0297     if (jabberTable->itemAt(0,i)->text() == nick)
0298     {
0299       qCDebug(KSIRK_LOG) << "This game is already listed";
0300       return;
0301     }
0302   }
0303   jabberTable->setSortingEnabled(false);
0304   jabberTable->setRowCount(jabberTable->rowCount()+1);
0305 
0306   QTableWidgetItem *newItem = new QTableWidgetItem(nick);
0307   jabberTable->setItem(jabberTable->rowCount()-1,0,newItem);
0308   newItem = new QTableWidgetItem(skin);
0309   jabberTable->setItem(jabberTable->rowCount()-1,1,newItem);
0310   newItem = new QTableWidgetItem(QString::number(nbPlayers));
0311   jabberTable->setItem(jabberTable->rowCount()-1,2,newItem);
0312 
0313   jabberTable->setSortingEnabled(false);
0314 }
0315 
0316 void KsirkJabberGameWidget::slotCellClicked(int row, int column)
0317 {
0318   Q_UNUSED(column);
0319   qCDebug(KSIRK_LOG) << row;
0320   m_nick = jabberTable->item(row,0)->text();
0321   m_nbPlayers = jabberTable->item(row,2)->text().toInt();
0322   qCDebug(KSIRK_LOG) << m_nick << m_nbPlayers;
0323 }
0324 
0325 void KsirkJabberGameWidget::slotJoinJabberGame()
0326 {
0327   qCDebug(KSIRK_LOG);
0328   m_automaton->joinJabberGame(m_nick);
0329 }
0330 
0331 void KsirkJabberGameWidget::slotCancel()
0332 {
0333   qCDebug(KSIRK_LOG);
0334   emit cancelled(m_previousGuiIndex);
0335 }
0336 
0337 #include "moc_jabbergameui.cpp"