36 #include <QHostAddress>
48 #define MAXBUFFER 4096
58 :
QObject(), m_buffer( new QByteArray() ), m_sock( s )
61 KGlobal::locale()->formatDateTime( QDateTime::currentDateTime(),
62 KLocale::ShortDate,
false );
66 m_titleAddon = QString::fromLatin1(
" [%1, %2]" )
67 .arg( m_sock->peerAddress().toString() )
71 connect( m_sock, SIGNAL(readyRead()), SLOT(slotDataAvailable()) );
72 connect( m_sock, SIGNAL(disconnected()), SLOT(slotConnectionClosed()) );
73 connect( m_sock, SIGNAL(error(QAbstractSocket::SocketError)), SLOT(slotError(QAbstractSocket::SocketError)));
76 m_timer =
new QTimer(
this );
77 m_timer->setSingleShot(
true );
78 connect( m_timer, SIGNAL(timeout()), SLOT(slotReceptionTimeout()) );
88 void KNotesNetworkReceiver::slotDataAvailable()
95 int curLen = m_buffer->count();
97 smallBufferLen = m_sock->read( smallBuffer,
SBSIZE );
100 smallBufferLen = qMin( smallBufferLen,
MAXBUFFER - curLen );
102 if ( smallBufferLen > 0 ) {
103 m_buffer->resize( curLen + smallBufferLen );
104 memcpy( m_buffer->data() + curLen, smallBuffer, smallBufferLen );
106 }
while ( smallBufferLen ==
SBSIZE );
116 void KNotesNetworkReceiver::slotReceptionTimeout()
121 void KNotesNetworkReceiver::slotConnectionClosed()
123 QTextCodec *codec = QTextCodec::codecForLocale();
125 if ( m_timer->isActive() ) {
126 QString noteText = QString( codec->toUnicode( *m_buffer ) ).trimmed();
129 const int pos = noteText.indexOf( QRegExp( QLatin1String(
"[\r\n]") ) );
130 const QString noteTitle = noteText.left( pos ).trimmed() + m_titleAddon;
132 noteText = noteText.mid( pos ).trimmed();
134 if ( !noteText.isEmpty() ) {
142 void KNotesNetworkReceiver::slotError( QAbstractSocket::SocketError error )
144 kWarning( 5500 ) <<
"error type :"<< ( int ) error <<
" error string : "<<m_sock->errorString();
147 #include "knotesnetrecv.moc"
KNotesNetworkReceiver(QTcpSocket *)
void sigNoteReceived(const QString &, const QString &)