knotes
knotesnetrecv.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include <QDateTime>
00034 #include <QHostAddress>
00035 #include <QRegExp>
00036 #include <QTcpSocket>
00037 #include <QTimer>
00038 #include <kdebug.h>
00039 #include <kglobal.h>
00040 #include <klocale.h>
00041
00042 #include "knotesnetrecv.h"
00043
00044
00045
00046 #define MAXBUFFER 4096
00047
00048
00049
00050 #define MAXTIME 10000
00051
00052
00053 #define SBSIZE 512
00054
00055 KNotesNetworkReceiver::KNotesNetworkReceiver( QTcpSocket *s )
00056 : QObject(), m_buffer( new QByteArray() ), m_sock( s )
00057 {
00058 QString date =
00059 KGlobal::locale()->formatDateTime( QDateTime::currentDateTime(),
00060 KLocale::ShortDate, false );
00061
00062
00063
00064 m_titleAddon = QString( " [%1, %2]" )
00065 .arg( m_sock->peerAddress().toString() )
00066 .arg( date );
00067
00068
00069 connect( m_sock, SIGNAL( readyRead() ), SLOT( slotDataAvailable() ) );
00070 connect( m_sock, SIGNAL( disconnected() ), SLOT( slotConnectionClosed() ) );
00071 connect( m_sock, SIGNAL( error( int ) ), SLOT( slotError( int ) ) );
00072
00073
00074 m_timer = new QTimer( this );
00075 m_timer->setSingleShot( true );
00076 connect( m_timer, SIGNAL( timeout() ), SLOT( slotReceptionTimeout() ) );
00077 m_timer->start( MAXTIME );
00078 }
00079
00080 KNotesNetworkReceiver::~KNotesNetworkReceiver()
00081 {
00082 delete m_buffer;
00083 delete m_sock;
00084 }
00085
00086 void KNotesNetworkReceiver::slotDataAvailable()
00087 {
00088 char smallBuffer[SBSIZE];
00089 int smallBufferLen;
00090
00091 do {
00092
00093 int curLen = m_buffer->count();
00094
00095 smallBufferLen = m_sock->read( smallBuffer, SBSIZE );
00096
00097
00098 smallBufferLen = qMin( smallBufferLen, MAXBUFFER - curLen );
00099
00100 if ( smallBufferLen > 0 ) {
00101 m_buffer->resize( curLen + smallBufferLen );
00102 memcpy( m_buffer->data() + curLen, smallBuffer, smallBufferLen );
00103 }
00104 } while ( smallBufferLen == SBSIZE );
00105
00106
00107 if ( m_buffer->count() == MAXBUFFER ) {
00108 m_sock->close();
00109 } else {
00110 m_timer->start( MAXTIME );
00111 }
00112 }
00113
00114 void KNotesNetworkReceiver::slotReceptionTimeout()
00115 {
00116 m_sock->close();
00117 }
00118
00119 void KNotesNetworkReceiver::slotConnectionClosed()
00120 {
00121 if ( m_timer->isActive() ) {
00122 QString noteText = QString( *m_buffer ).trimmed();
00123
00124
00125 int pos = noteText.indexOf( QRegExp( "[\r\n]" ) );
00126 QString noteTitle = noteText.left( pos ).trimmed() + m_titleAddon;
00127
00128 noteText = noteText.mid( pos ).trimmed();
00129
00130 if ( !noteText.isEmpty() ) {
00131 emit sigNoteReceived( noteTitle, noteText );
00132 }
00133 }
00134
00135 deleteLater();
00136 }
00137
00138 void KNotesNetworkReceiver::slotError( int err )
00139 {
00140 kWarning( 5500 ) << err << m_sock->errorString();
00141 }
00142
00143 #include "knotesnetrecv.moc"