File indexing completed on 2024-09-01 03:52:03
0001 /* 0002 This file is part of the KDE games library 0003 SPDX-FileCopyrightText: 2001 Andreas Beckermann (b_mann@gmx.de) 0004 SPDX-FileCopyrightText: 2001 Martin Heni (kde at heni-online.de) 0005 0006 SPDX-License-Identifier: LGPL-2.0-only 0007 */ 0008 0009 #include "kgamedialogconfig.h" 0010 0011 // own 0012 #include "kfourinline_debug.h" 0013 #include "kgameconnectdialog.h" 0014 // KDEGames 0015 #define USE_UNSTABLE_LIBKDEGAMESPRIVATE_API 0016 #include <libkdegamesprivate/kgame/kgame.h> 0017 #include <libkdegamesprivate/kgame/kgamechat.h> 0018 #include <libkdegamesprivate/kgame/kgameproperty.h> 0019 #include <libkdegamesprivate/kgame/kplayer.h> 0020 // KF 0021 #include <KLocalizedString> 0022 #include <KMessageBox> 0023 // Qt 0024 #include <QGroupBox> 0025 #include <QHBoxLayout> 0026 #include <QLabel> 0027 #include <QPushButton> 0028 #include <QVBoxLayout> 0029 0030 class KGameDialogConfigPrivate 0031 { 0032 public: 0033 KGameDialogConfigPrivate() 0034 { 0035 mOwner = nullptr; 0036 mGame = nullptr; 0037 0038 mAdmin = false; 0039 } 0040 0041 bool mAdmin; 0042 KGame *mGame; 0043 KPlayer *mOwner; 0044 }; 0045 0046 KGameDialogConfig::KGameDialogConfig(QWidget *parent) 0047 : QWidget(parent) 0048 , d(new KGameDialogConfigPrivate) 0049 { 0050 } 0051 0052 KGameDialogConfig::~KGameDialogConfig() 0053 { 0054 qCDebug(KFOURINLINE_LOG); 0055 delete d; 0056 } 0057 0058 void KGameDialogConfig::setKGame(KGame *g) 0059 { 0060 d->mGame = g; 0061 } 0062 0063 void KGameDialogConfig::setOwner(KPlayer *p) 0064 { 0065 d->mOwner = p; 0066 } 0067 0068 void KGameDialogConfig::setAdmin(bool a) 0069 { 0070 d->mAdmin = a; 0071 } 0072 0073 KGame *KGameDialogConfig::game() const 0074 { 0075 return d->mGame; 0076 } 0077 bool KGameDialogConfig::admin() const 0078 { 0079 return d->mAdmin; 0080 } 0081 KPlayer *KGameDialogConfig::owner() const 0082 { 0083 return d->mOwner; 0084 } 0085 0086 /////////////////////////// KGameDialogNetworkConfig ///////////////////////// 0087 class KGameDialogNetworkConfigPrivate 0088 { 0089 public: 0090 KGameDialogNetworkConfigPrivate() 0091 { 0092 mInitConnection = nullptr; 0093 mNetworkLabel = nullptr; 0094 mDisconnectButton = nullptr; 0095 mConnect = nullptr; 0096 mDefaultServer = true; 0097 } 0098 0099 // QPushButton* mInitConnection; 0100 QGroupBox *mInitConnection; 0101 QLabel *mNetworkLabel; 0102 QPushButton *mDisconnectButton; 0103 0104 bool mDefaultServer; 0105 QString mDefaultHost; 0106 unsigned short int mDefaultPort; 0107 KGameConnectWidget *mConnect; 0108 }; 0109 0110 KGameDialogNetworkConfig::KGameDialogNetworkConfig(QWidget *parent) 0111 : KGameDialogConfig(parent) 0112 { 0113 // qCDebug(KFOURINLINE_LOG) << ": this=" << this; 0114 d = new KGameDialogNetworkConfigPrivate(); 0115 0116 QVBoxLayout *topLayout = new QVBoxLayout(this); 0117 0118 QHBoxLayout *hb = new QHBoxLayout; 0119 topLayout->addLayout(hb); 0120 0121 d->mNetworkLabel = new QLabel(this); 0122 hb->addWidget(d->mNetworkLabel); 0123 0124 d->mDisconnectButton = new QPushButton(i18n("Disconnect"), this); 0125 connect(d->mDisconnectButton, &QPushButton::clicked, this, &KGameDialogNetworkConfig::slotExitConnection); 0126 hb->addWidget(d->mDisconnectButton); 0127 0128 d->mInitConnection = new QGroupBox(i18n("Network Configuration"), this); 0129 QHBoxLayout *gboxLay = new QHBoxLayout(d->mInitConnection); 0130 topLayout->addWidget(d->mInitConnection); 0131 0132 d->mConnect = new KGameConnectWidget(d->mInitConnection); 0133 gboxLay->addWidget(d->mConnect); 0134 connect(d->mConnect, &KGameConnectWidget::signalNetworkSetup, this, &KGameDialogNetworkConfig::slotInitConnection); 0135 connect(d->mConnect, &KGameConnectWidget::signalServerTypeChanged, this, &KGameDialogNetworkConfig::signalServerTypeChanged); 0136 0137 // Needs to be AFTER the creation of the dialogs 0138 setConnected(false); 0139 setDefaultNetworkInfo(QStringLiteral("localhost"), 7654, true); 0140 } 0141 0142 KGameDialogNetworkConfig::~KGameDialogNetworkConfig() 0143 { 0144 qCDebug(KFOURINLINE_LOG); 0145 delete d; 0146 } 0147 0148 void KGameDialogNetworkConfig::slotExitConnection() 0149 { 0150 qCDebug(KFOURINLINE_LOG) << " !!!!!!!!!!!!!!!!!!!!!!!"; 0151 if (game()) 0152 game()->disconnect(); 0153 setConnected(false, false); 0154 } 0155 0156 void KGameDialogNetworkConfig::slotInitConnection() 0157 { 0158 qCDebug(KFOURINLINE_LOG); 0159 bool connected = false; 0160 bool master = true; 0161 unsigned short int port = d->mConnect->port(); 0162 QString host = d->mConnect->host(); 0163 0164 if (host.isNull()) { 0165 master = true; 0166 if (game()) { 0167 game()->setDiscoveryInfo(d->mConnect->type(), d->mConnect->gameName()); 0168 connected = game()->offerConnections(port); 0169 } 0170 } else { 0171 master = false; 0172 if (game()) { 0173 connected = game()->connectToServer(host, port); 0174 } 0175 // We need to learn about failed connections 0176 if (game()) { 0177 connect(game(), &KGameNetwork::signalConnectionBroken, this, &KGameDialogNetworkConfig::slotConnectionBroken); 0178 } 0179 } 0180 setConnected(connected, master); 0181 } 0182 0183 void KGameDialogNetworkConfig::slotConnectionBroken() 0184 { 0185 qCDebug(KFOURINLINE_LOG); 0186 setConnected(false, false); 0187 KMessageBox::error(this, i18n("Cannot connect to the network")); 0188 } 0189 0190 void KGameDialogNetworkConfig::setConnected(bool connected, bool master) 0191 { 0192 if (!connected) { 0193 d->mNetworkLabel->setText(i18n("Network status: No Network")); 0194 d->mInitConnection->setEnabled(true); 0195 d->mDisconnectButton->setEnabled(false); 0196 return; 0197 } 0198 if (master) { 0199 d->mNetworkLabel->setText(i18n("Network status: You are MASTER")); 0200 } else { 0201 d->mNetworkLabel->setText(i18n("Network status: You are connected")); 0202 } 0203 d->mInitConnection->setEnabled(false); 0204 d->mDisconnectButton->setEnabled(true); 0205 } 0206 0207 void KGameDialogNetworkConfig::submitToKGame(KGame *, KPlayer *) 0208 { 0209 } 0210 0211 void KGameDialogNetworkConfig::setKGame(KGame *g) 0212 { 0213 KGameDialogConfig::setKGame(g); 0214 if (!game()) { 0215 setConnected(false); 0216 return; 0217 } 0218 setConnected(game()->isNetwork(), game()->isMaster()); 0219 } 0220 0221 void KGameDialogNetworkConfig::setDefaultNetworkInfo(const QString &host, unsigned short int port, bool server) 0222 { 0223 d->mDefaultPort = port; 0224 d->mDefaultHost = host; 0225 d->mDefaultServer = server; 0226 0227 d->mConnect->setHost(host); 0228 d->mConnect->setPort(port); 0229 if (server) { 0230 d->mConnect->setDefault(0); 0231 } else { 0232 d->mConnect->setDefault(1); 0233 } 0234 } 0235 0236 void KGameDialogNetworkConfig::setDiscoveryInfo(const QString &type, const QString &name) 0237 { 0238 d->mConnect->setType(type); 0239 d->mConnect->setName(name); 0240 } 0241 0242 #include "moc_kgamedialogconfig.cpp"