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 0005 SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0006 */ 0007 0008 #include "proto/protocol.h" 0009 #include "proto/chatwidget.h" 0010 #include "core/move.h" 0011 #include <gamemanager.h> 0012 0013 #include <KLocalizedString> 0014 0015 0016 namespace Knights { 0017 int id = qRegisterMetaType<Protocol::ErrorCode> ( "Protocol::ErrorCode" ); 0018 0019 QPointer<Protocol> Protocol::m_white = nullptr; 0020 QPointer<Protocol> Protocol::m_black = nullptr; 0021 0022 class ProtocolPrivate { 0023 public: 0024 0025 ProtocolPrivate(); 0026 0027 QVariantMap attributes; 0028 Protocol* white; 0029 Protocol* black; 0030 Color color; 0031 bool ready; 0032 int nextId; 0033 }; 0034 0035 ProtocolPrivate::ProtocolPrivate() 0036 : white(nullptr), 0037 black(nullptr), 0038 color(NoColor), 0039 ready(false), 0040 nextId(0) { 0041 0042 } 0043 0044 Protocol::Protocol ( QObject* parent ) : QObject ( parent ), d_ptr ( new ProtocolPrivate ) { 0045 } 0046 0047 Protocol::~Protocol() { 0048 delete d_ptr; 0049 } 0050 0051 QString Protocol::stringFromErrorCode ( Protocol::ErrorCode code ) { 0052 switch ( code ) { 0053 case NoError: 0054 return i18n ( "No Error" ); 0055 0056 case UserCancelled: 0057 return i18n ( "User Canceled" ); 0058 0059 case NetworkError: 0060 return i18n ( "Network Error" ); 0061 0062 case UnknownError: 0063 return i18n ( "Unknown Error" ); 0064 0065 case InstallationError: 0066 return i18n ( "Program Error" ); 0067 0068 default: 0069 return QString(); 0070 } 0071 } 0072 0073 void Protocol::setWhiteProtocol(Protocol* p) { 0074 p->setColor(White); 0075 m_white = p; 0076 } 0077 0078 void Protocol::setBlackProtocol(Protocol* p) { 0079 p->setColor(Black); 0080 m_black = p; 0081 } 0082 0083 Protocol* Protocol::white() { 0084 return m_white; 0085 } 0086 0087 Protocol* Protocol::black() { 0088 return m_black; 0089 } 0090 0091 Protocol* Protocol::byColor(Color color) { 0092 switch ( color ) { 0093 case White: 0094 return white(); 0095 case Black: 0096 return black(); 0097 case NoColor: 0098 return nullptr; 0099 } 0100 return nullptr; 0101 } 0102 void Protocol::setColor ( Color color ) { 0103 Q_D(Protocol); 0104 d->color = color; 0105 } 0106 0107 Color Protocol::color() const { 0108 Q_D(const Protocol); 0109 return d->color; 0110 } 0111 0112 void Protocol::setPlayerName ( const QString& name ) { 0113 setAttribute ( QStringLiteral ( "PlayerName" ), name ); 0114 } 0115 0116 QString Protocol::playerName() const { 0117 return attribute ( QStringLiteral ( "PlayerName" ) ).toString(); 0118 } 0119 0120 void Protocol::setAttribute ( const QString& attribute, QVariant value ) { 0121 Q_D ( Protocol ); 0122 d->attributes.insert ( attribute, value ); 0123 } 0124 0125 void Protocol::setAttribute ( const char* attribute, QVariant value ) { 0126 setAttribute( QLatin1String ( attribute ), value ); 0127 } 0128 0129 void Protocol::setAttributes ( QVariantMap attributes ) { 0130 Q_D ( Protocol ); 0131 for (auto it = attributes.constBegin(), end = attributes.constEnd(); it != end; ++it) { 0132 d->attributes.insert(it.key(), it.value()); 0133 } 0134 } 0135 0136 QVariant Protocol::attribute ( const QString& attribute ) const { 0137 Q_D ( const Protocol ); 0138 return d->attributes.value ( attribute ); 0139 } 0140 0141 QVariant Protocol::attribute ( const char* attribute ) const { 0142 return this->attribute ( QLatin1String ( attribute ) ); 0143 } 0144 0145 Protocol::Features Protocol::supportedFeatures() { 0146 return NoFeatures; 0147 } 0148 0149 int Protocol::timeRemaining() { 0150 return -1; 0151 } 0152 0153 QList< Protocol::ToolWidgetData > Protocol::toolWidgets() { 0154 return QList< Protocol::ToolWidgetData >(); 0155 } 0156 0157 void Protocol::setWinner(Color winner) { 0158 Q_UNUSED(winner); 0159 } 0160 0161 void Protocol::setTimeControl(const TimeControl& c) { 0162 Q_UNUSED(c); 0163 } 0164 0165 void Protocol::acceptOffer(const Offer& offer) { 0166 Q_UNUSED(offer); 0167 } 0168 0169 void Protocol::declineOffer(const Offer& offer) { 0170 Q_UNUSED(offer); 0171 } 0172 0173 ChatWidget* Protocol::createChatWidget() { 0174 return new ChatWidget; 0175 } 0176 0177 ChatWidget* Protocol::createConsoleWidget() { 0178 ChatWidget* console = new ChatWidget; 0179 console->setConsoleMode(true); 0180 return console; 0181 } 0182 0183 void Protocol::initComplete() { 0184 Q_D(Protocol); 0185 d->ready = true; 0186 Q_EMIT initSuccesful(); 0187 } 0188 0189 bool Protocol::isReady() { 0190 Q_D(const Protocol); 0191 return d->ready; 0192 } 0193 0194 bool Protocol::isLocal() { 0195 return false; 0196 } 0197 0198 bool Protocol::isComputer() { 0199 return false; 0200 } 0201 0202 void Protocol::setDifficulty(int depth, int memory) { 0203 Q_UNUSED(depth); 0204 Q_UNUSED(memory); 0205 } 0206 0207 } 0208 0209 #include "moc_protocol.cpp"