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
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 2 of
8 the License or (at your option) version 3 or any later version
9 accepted by the membership of KDE e.V. (or its successor approved
10 by the membership of KDE e.V.), which shall act as a proxy
11 defined in Section 14 of version 3 of the license.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "proto/protocol.h"
23 #include "proto/chatwidget.h"
24 #include "core/move.h"
25 #include <gamemanager.h>
26
27 #include <KLocalizedString>
28
29
30 namespace Knights {
31 int id = qRegisterMetaType<Protocol::ErrorCode> ( "Protocol::ErrorCode" );
32
33 QPointer<Protocol> Protocol::m_white = nullptr;
34 QPointer<Protocol> Protocol::m_black = nullptr;
35
36 class ProtocolPrivate {
37 public:
38
39 ProtocolPrivate();
40
41 QVariantMap attributes;
42 Protocol* white;
43 Protocol* black;
44 Color color;
45 bool ready;
46 int nextId;
47 };
48
ProtocolPrivate()49 ProtocolPrivate::ProtocolPrivate()
50 : white(nullptr),
51 black(nullptr),
52 color(NoColor),
53 ready(false),
54 nextId(0) {
55
56 }
57
Protocol(QObject * parent)58 Protocol::Protocol ( QObject* parent ) : QObject ( parent ), d_ptr ( new ProtocolPrivate ) {
59 }
60
~Protocol()61 Protocol::~Protocol() {
62 delete d_ptr;
63 }
64
stringFromErrorCode(Protocol::ErrorCode code)65 QString Protocol::stringFromErrorCode ( Protocol::ErrorCode code ) {
66 switch ( code ) {
67 case NoError:
68 return i18n ( "No Error" );
69
70 case UserCancelled:
71 return i18n ( "User Canceled" );
72
73 case NetworkError:
74 return i18n ( "Network Error" );
75
76 case UnknownError:
77 return i18n ( "Unknown Error" );
78
79 case InstallationError:
80 return i18n ( "Program Error" );
81
82 default:
83 return QString();
84 }
85 }
86
setWhiteProtocol(Protocol * p)87 void Protocol::setWhiteProtocol(Protocol* p) {
88 p->setColor(White);
89 m_white = p;
90 }
91
setBlackProtocol(Protocol * p)92 void Protocol::setBlackProtocol(Protocol* p) {
93 p->setColor(Black);
94 m_black = p;
95 }
96
white()97 Protocol* Protocol::white() {
98 return m_white;
99 }
100
black()101 Protocol* Protocol::black() {
102 return m_black;
103 }
104
byColor(Color color)105 Protocol* Protocol::byColor(Color color) {
106 switch ( color ) {
107 case White:
108 return white();
109 case Black:
110 return black();
111 case NoColor:
112 return nullptr;
113 }
114 return nullptr;
115 }
setColor(Color color)116 void Protocol::setColor ( Color color ) {
117 Q_D(Protocol);
118 d->color = color;
119 }
120
color() const121 Color Protocol::color() const {
122 Q_D(const Protocol);
123 return d->color;
124 }
125
setPlayerName(const QString & name)126 void Protocol::setPlayerName ( const QString& name ) {
127 setAttribute ( QStringLiteral ( "PlayerName" ), name );
128 }
129
playerName() const130 QString Protocol::playerName() const {
131 return attribute ( QStringLiteral ( "PlayerName" ) ).toString();
132 }
133
setAttribute(const QString & attribute,QVariant value)134 void Protocol::setAttribute ( const QString& attribute, QVariant value ) {
135 Q_D ( Protocol );
136 d->attributes.insert ( attribute, value );
137 }
138
setAttribute(const char * attribute,QVariant value)139 void Protocol::setAttribute ( const char* attribute, QVariant value ) {
140 setAttribute( QLatin1String ( attribute ), value );
141 }
142
setAttributes(QVariantMap attributes)143 void Protocol::setAttributes ( QVariantMap attributes ) {
144 Q_D ( Protocol );
145 for (auto it = attributes.constBegin(), end = attributes.constEnd(); it != end; ++it) {
146 d->attributes.insert(it.key(), it.value());
147 }
148 }
149
attribute(const QString & attribute) const150 QVariant Protocol::attribute ( const QString& attribute ) const {
151 Q_D ( const Protocol );
152 return d->attributes.value ( attribute );
153 }
154
attribute(const char * attribute) const155 QVariant Protocol::attribute ( const char* attribute ) const {
156 return this->attribute ( QLatin1String ( attribute ) );
157 }
158
supportedFeatures()159 Protocol::Features Protocol::supportedFeatures() {
160 return NoFeatures;
161 }
162
timeRemaining()163 int Protocol::timeRemaining() {
164 return -1;
165 }
166
toolWidgets()167 QList< Protocol::ToolWidgetData > Protocol::toolWidgets() {
168 return QList< Protocol::ToolWidgetData >();
169 }
170
setWinner(Color winner)171 void Protocol::setWinner(Color winner) {
172 Q_UNUSED(winner);
173 }
174
setTimeControl(const TimeControl & c)175 void Protocol::setTimeControl(const TimeControl& c) {
176 Q_UNUSED(c);
177 }
178
acceptOffer(const Offer & offer)179 void Protocol::acceptOffer(const Offer& offer) {
180 Q_UNUSED(offer);
181 }
182
declineOffer(const Offer & offer)183 void Protocol::declineOffer(const Offer& offer) {
184 Q_UNUSED(offer);
185 }
186
createChatWidget()187 ChatWidget* Protocol::createChatWidget() {
188 return new ChatWidget;
189 }
190
createConsoleWidget()191 ChatWidget* Protocol::createConsoleWidget() {
192 ChatWidget* console = new ChatWidget;
193 console->setConsoleMode(true);
194 return console;
195 }
196
initComplete()197 void Protocol::initComplete() {
198 Q_D(Protocol);
199 d->ready = true;
200 Q_EMIT initSuccesful();
201 }
202
isReady()203 bool Protocol::isReady() {
204 Q_D(const Protocol);
205 return d->ready;
206 }
207
isLocal()208 bool Protocol::isLocal() {
209 return false;
210 }
211
isComputer()212 bool Protocol::isComputer() {
213 return false;
214 }
215
setDifficulty(int depth,int memory)216 void Protocol::setDifficulty(int depth, int memory) {
217 Q_UNUSED(depth);
218 Q_UNUSED(memory);
219 }
220
221 }
222