34 #include <tqdatetime.h>
36 #include <tqcstring.h>
39 #include <kbufferedsocket.h>
40 #include <tdesocketaddress.h>
41 #include <tdelocale.h>
42 #include <tdeglobal.h>
44 #include "knotesnetrecv.h"
48 #define MAXBUFFER 4096
57 using namespace KNetwork;
60 KNotesNetworkReceiver::KNotesNetworkReceiver( TDEBufferedSocket *s )
62 m_buffer( new TQByteArray() ), m_sock( s )
64 TQString date = TDEGlobal::locale()->formatDateTime( TQDateTime::currentDateTime(),
true,
false );
68 m_titleAddon = TQString(
" [%1, %2]")
69 .arg( m_sock->peerAddress().nodeName() )
73 connect( m_sock, TQ_SIGNAL(readyRead()), TQ_SLOT(slotDataAvailable()) );
74 connect( m_sock, TQ_SIGNAL(closed()), TQ_SLOT(slotConnectionClosed()) );
75 connect( m_sock, TQ_SIGNAL(gotError(
int )), TQ_SLOT(slotError(
int )) );
77 m_sock->enableRead(
true );
80 m_timer =
new TQTimer(
this,
"m_timer" );
81 connect( m_timer, TQ_SIGNAL(timeout()), TQ_SLOT(slotReceptionTimeout()) );
82 m_timer->start( MAXTIME,
true );
85 KNotesNetworkReceiver::~KNotesNetworkReceiver()
91 void KNotesNetworkReceiver::slotDataAvailable()
93 char smallBuffer[SBSIZE];
99 int curLen = m_buffer->count();
101 smallBufferLen = m_sock->readBlock( smallBuffer, SBSIZE );
104 smallBufferLen = kMin( smallBufferLen, MAXBUFFER - curLen );
106 if ( smallBufferLen > 0 )
108 m_buffer->resize( curLen + smallBufferLen );
109 memcpy( m_buffer->data() + curLen, smallBuffer, smallBufferLen );
112 while ( smallBufferLen == SBSIZE );
115 if ( m_buffer->count() == MAXBUFFER )
118 m_timer->changeInterval( MAXTIME );
121 void KNotesNetworkReceiver::slotReceptionTimeout()
126 void KNotesNetworkReceiver::slotConnectionClosed()
128 if ( m_timer->isActive() )
130 TQString noteText = TQString( *m_buffer ).stripWhiteSpace();
133 int pos = noteText.find( TQRegExp(
"[\r\n]") );
134 TQString noteTitle = noteText.left( pos ).stripWhiteSpace() + m_titleAddon;
136 noteText = noteText.mid( pos ).stripWhiteSpace();
138 if ( !noteText.isEmpty() )
139 emit sigNoteReceived( noteTitle, noteText );
145 void KNotesNetworkReceiver::slotError(
int err )
147 kdWarning() << k_funcinfo
148 << m_sock->KNetwork::TDESocketBase::errorString(
static_cast<TDESocketBase::SocketError
>(err) )
152 #include "knotesnetrecv.moc"