textprotocol.cpp 3.33 KB
Newer Older
1
2
3
4
5
/***************************************************************************
    File                 : textprotocol.cpp
    Project              : Knights
    Description          : Base class for text protocols
    --------------------------------------------------------------------
6
7
    SPDX-FileCopyrightText: 2017 Alexander Semke (alexander.semke@web.de)
    SPDX-FileCopyrightText: 2009-2011 Miha Čančula (miha@noughmad.eu)
8
9
10
11
12

 ***************************************************************************/

/***************************************************************************
 *                                                                         *
13
 *  SPDX-License-Identifier: GPL-2.0-or-later
14
15
16
17
18
19
20
21
22
23
24
25
 *                                                                         *
 *  This program is distributed in the hope that it will be useful,        *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
 *  GNU General Public License for more details.                           *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the Free Software           *
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor,                    *
 *   Boston, MA  02110-1301  USA                                           *
 *                                                                         *
 ***************************************************************************/
26
#include "textprotocol.h"
Yuri Chornoivan's avatar
Yuri Chornoivan committed
27
#include "knightsdebug.h"
Laurent Montel's avatar
Laurent Montel committed
28
#include <QIODevice>
29
30
31

using namespace Knights;

32
TextProtocol::TextProtocol(QObject* parent): Protocol(parent), m_console(nullptr) {
33
34
35

}

36
TextProtocol::~TextProtocol() = default;
37

38
39
40
41
42
43
44
45
void TextProtocol::readFromDevice() {
	while ( !stream.atEnd() ) {
		if ( stream.device()->canReadLine() ) {
			const QString text = line + stream.readLine().trimmed();
			if (!parseLine(text))
				line = text;
		} else {
			const QString all = stream.readAll();
46
			for ( const QString& newLine : all.split( QLatin1Char('\n') ) ) {
47
48
49
50
51
				const QString text = line + newLine.trimmed();
				if ( !parseStub(text) && !parseLine(text) )
					line = text;
			}
		}
52
53
54
	}
}

55
56
57
void TextProtocol::setDevice(QIODevice* device) {
	stream.setDevice(device);
	connect ( device, &QIODevice::readyRead, this, &TextProtocol::readFromDevice );
58
59
}

60
61
QIODevice* TextProtocol::device() const {
	return stream.device();
62
63
}

64
65
66
void TextProtocol::writeCheckMoves(const QString& text) {
	Move m = Move(text);
	if ( m.isValid() )
67
		Q_EMIT pieceMoved(m);
68
	write(text);
69
70
}

71
72
void TextProtocol::write(const QString& text) {
	qCDebug(LOG_KNIGHTS) << text;
73
        stream << text
74
               << Qt::endl;
75
76
}

77
78
void TextProtocol::write(const char* text) {
	qCDebug(LOG_KNIGHTS) << text;
79
        stream << text
80
               << Qt::endl;
81
82
}

83
84
void TextProtocol::setConsole(ChatWidget* widget) {
	m_console = widget;
85
	for ( const ChatWidget::Message& message : qAsConst(messages) )
86
87
		m_console->addText ( message );
	messages.clear();
88
89
}

90
91
ChatWidget* TextProtocol::console() const {
	return m_console;
92
93
}

94
95
96
97
98
void TextProtocol::writeToConsole(const QString& text, ChatWidget::MessageType type) {
	if ( m_console )
		m_console->addText ( text, type );
	else
		messages << qMakePair( text, type );
99
}