File indexing completed on 2024-12-15 04:51:47
0001 /******************************************************************* 0002 KNotes -- Notes for the KDE project 0003 0004 SPDX-FileCopyrightText: 2003 Daniel Martin <daniel.martin@pirack.com> 0005 SPDX-FileCopyrightText: 2004, 2006 Michael Brade <brade@kde.org> 0006 SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel.org> 0007 0008 SPDX-License-Identifier: GPL-2.0-or-later 0009 *******************************************************************/ 0010 0011 #include "notesnetworksender.h" 0012 0013 #include <KLocalizedString> 0014 #include <KMessageBox> 0015 0016 #include <QStringEncoder> 0017 0018 using namespace NoteShared; 0019 0020 NotesNetworkSender::NotesNetworkSender(QTcpSocket *socket) 0021 : QObject() 0022 , m_socket(socket) 0023 { 0024 // QObject:: prefix needed, otherwise the KStreamSocket::connect() 0025 // method is called!!! 0026 QObject::connect(m_socket, &QTcpSocket::connected, this, &NotesNetworkSender::slotConnected); 0027 0028 QObject::connect(m_socket, qOverload<QAbstractSocket::SocketError>(&QTcpSocket::errorOccurred), this, &NotesNetworkSender::slotError); 0029 0030 QObject::connect(m_socket, &QTcpSocket::disconnected, this, &NotesNetworkSender::slotClosed); 0031 0032 QObject::connect(m_socket, &QTcpSocket::bytesWritten, this, &NotesNetworkSender::slotWritten); 0033 } 0034 0035 NotesNetworkSender::~NotesNetworkSender() 0036 { 0037 delete m_socket; 0038 } 0039 0040 void NotesNetworkSender::setSenderId(const QString &sender) 0041 { 0042 QStringEncoder codec(QStringEncoder::System); 0043 m_sender = codec.encode(sender); 0044 } 0045 0046 void NotesNetworkSender::setNote(const QString &title, const QString &text) 0047 { 0048 QStringEncoder codec(QStringEncoder::System); 0049 m_title = codec.encode(title); 0050 m_note = codec.encode(text); 0051 } 0052 0053 void NotesNetworkSender::slotConnected() 0054 { 0055 if (m_sender.isEmpty()) { 0056 m_note.prepend(m_title + '\n'); 0057 } else { 0058 m_note.prepend(m_title + " (" + m_sender + ")\n"); 0059 } 0060 0061 m_socket->write(m_note); 0062 } 0063 0064 void NotesNetworkSender::slotWritten(qint64) 0065 { 0066 // If end of text reached, close connection 0067 if (m_socket->bytesToWrite() == 0) { 0068 m_socket->close(); 0069 } 0070 } 0071 0072 void NotesNetworkSender::slotError() 0073 { 0074 KMessageBox::error(nullptr, i18n("Communication error: %1", m_socket->errorString())); 0075 slotClosed(); 0076 } 0077 0078 void NotesNetworkSender::slotClosed() 0079 { 0080 deleteLater(); 0081 } 0082 0083 #include "moc_notesnetworksender.cpp"