1 /*
2     This file is part of Knights, a chess board for KDE SC 4.
3     Copyright 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 "computerprotocol.h"
23 #include "knightsdebug.h"
24 
25 #include <KProcess>
26 #include <KLocalizedString>
27 
28 using namespace Knights;
29 
ComputerProtocol(QObject * parent)30 ComputerProtocol::ComputerProtocol(QObject* parent): TextProtocol(parent), mProcess(nullptr) {
31 
32 }
33 
34 ComputerProtocol::~ComputerProtocol() = default;
35 
startProgram()36 void ComputerProtocol::startProgram() {
37 	QStringList args = attribute("program").toString().split ( QLatin1Char ( ' ' ) );
38 	QString program = args.takeFirst();
39 	setPlayerName ( program );
40 	mProcess = new KProcess ( this );
41 	mProcess->setProgram ( program, args );
42 	mProcess->setNextOpenMode ( QIODevice::ReadWrite | QIODevice::Unbuffered | QIODevice::Text );
43 	mProcess->setOutputChannelMode ( KProcess::SeparateChannels );
44 	mProcess->setReadChannel ( KProcess::StandardOutput );
45 	connect ( mProcess, &KProcess::readyReadStandardError, this, &ComputerProtocol::readError );
46 	setDevice(mProcess);
47 	qCDebug(LOG_KNIGHTS) << "Starting program" << program << "with args" << args;
48 	mProcess->start();
49 	if ( !mProcess->waitForStarted ( 1000 ) ) {
50 		Q_EMIT error ( InstallationError, i18n ( "Program <code>%1</code> could not be started, please check that it is installed.", program ) );
51 		return;
52 	}
53 }
54 
isComputer()55 bool ComputerProtocol::isComputer() {
56 	return true;
57 }
58 
toolWidgets()59 QList< Protocol::ToolWidgetData > ComputerProtocol::toolWidgets() {
60 	ChatWidget* console = createConsoleWidget();
61 	connect ( console, &ChatWidget::sendText, this, &ComputerProtocol::writeCheckMoves );
62 	setConsole ( console );
63 	ToolWidgetData data;
64 	data.widget = console;
65 	data.title = i18n("Console for %1 (%2)", playerName(), colorName ( color() ) );
66 	data.name = QLatin1String("console") + attribute("program").toString() + QLatin1Char( color() == White ? 'W' : 'B' );
67 	data.type = ConsoleToolWidget;
68 	data.owner = color();
69 	return QList< Protocol::ToolWidgetData >() << data;
70 }
71 
72 
readError()73 void ComputerProtocol::readError() {
74 	qCCritical(LOG_KNIGHTS) << mProcess->readAllStandardError();
75 }
76