File indexing completed on 2024-05-12 04:04:16

0001 /***************************************************************************
0002     File                 : textprotocol.cpp
0003     Project              : Knights
0004     Description          : Base class for text protocols
0005     --------------------------------------------------------------------
0006     SPDX-FileCopyrightText: 2017 Alexander Semke (alexander.semke@web.de)
0007     SPDX-FileCopyrightText: 2009-2011 Miha Čančula (miha@noughmad.eu)
0008 
0009  ***************************************************************************/
0010 
0011 /***************************************************************************
0012  *                                                                         *
0013  *  SPDX-License-Identifier: GPL-2.0-or-later
0014  *                                                                         *
0015  *  This program is distributed in the hope that it will be useful,        *
0016  *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
0017  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
0018  *  GNU General Public License for more details.                           *
0019  *                                                                         *
0020  *   You should have received a copy of the GNU General Public License     *
0021  *   along with this program; if not, write to the Free Software           *
0022  *   Foundation, Inc., 51 Franklin Street, Fifth Floor,                    *
0023  *   Boston, MA  02110-1301  USA                                           *
0024  *                                                                         *
0025  ***************************************************************************/
0026 #include "textprotocol.h"
0027 #include "knightsdebug.h"
0028 #include <QIODevice>
0029 
0030 using namespace Knights;
0031 
0032 TextProtocol::TextProtocol(QObject* parent): Protocol(parent), m_console(nullptr) {
0033 
0034 }
0035 
0036 TextProtocol::~TextProtocol() = default;
0037 
0038 void TextProtocol::readFromDevice() {
0039     while ( !stream.atEnd() ) {
0040         if ( stream.device()->canReadLine() ) {
0041             const QString text = line + stream.readLine().trimmed();
0042             if (!parseLine(text))
0043                 line = text;
0044         } else {
0045             const QString all = stream.readAll();
0046             for ( const QString& newLine : all.split( QLatin1Char('\n') ) ) {
0047                 const QString text = line + newLine.trimmed();
0048                 if ( !parseStub(text) && !parseLine(text) )
0049                     line = text;
0050             }
0051         }
0052     }
0053 }
0054 
0055 void TextProtocol::setDevice(QIODevice* device) {
0056     stream.setDevice(device);
0057     connect ( device, &QIODevice::readyRead, this, &TextProtocol::readFromDevice );
0058 }
0059 
0060 QIODevice* TextProtocol::device() const {
0061     return stream.device();
0062 }
0063 
0064 void TextProtocol::writeCheckMoves(const QString& text) {
0065     Move m = Move(text);
0066     if ( m.isValid() )
0067         Q_EMIT pieceMoved(m);
0068     write(text);
0069 }
0070 
0071 void TextProtocol::write(const QString& text) {
0072     qCDebug(LOG_KNIGHTS) << text;
0073         stream << text
0074                << Qt::endl;
0075 }
0076 
0077 void TextProtocol::write(const char* text) {
0078     qCDebug(LOG_KNIGHTS) << text;
0079         stream << text
0080                << Qt::endl;
0081 }
0082 
0083 void TextProtocol::setConsole(ChatWidget* widget) {
0084     m_console = widget;
0085     for ( const ChatWidget::Message& message : std::as_const(messages) )
0086         m_console->addText ( message );
0087     messages.clear();
0088 }
0089 
0090 ChatWidget* TextProtocol::console() const {
0091     return m_console;
0092 }
0093 
0094 void TextProtocol::writeToConsole(const QString& text, ChatWidget::MessageType type) {
0095     if ( m_console )
0096         m_console->addText ( text, type );
0097     else
0098         messages << qMakePair( text, type );
0099 }
0100 
0101 #include "moc_textprotocol.cpp"