Don't offer to restart a Running (=busy) agent, it won't work.
[kdepim.git] / knotes / knotesnetrecv.cpp
blobe33f1531e6a9ef49e00ec07579990a59844e2380
1 /*******************************************************************
2 KNotes -- Notes for the KDE project
4 Copyright (c) 2003, Daniel Martin <daniel.martin@pirack.com>
5 2004, 2006, Michael Brade <brade@kde.org>
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 as published by the Free Software Foundation; either version 2
10 of the License, or (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 In addition, as a special exception, the copyright holders give
22 permission to link the code of this program with any edition of
23 the Qt library by Trolltech AS, Norway (or with modified versions
24 of Qt that use the same license as Qt), and distribute linked
25 combinations including the two. You must obey the GNU General
26 Public License in all respects for all of the code used other than
27 Qt. If you modify this file, you may extend this exception to
28 your version of the file, but you are not obligated to do so. If
29 you do not wish to do so, delete this exception statement from
30 your version.
31 *******************************************************************/
33 #include "knotesnetrecv.h"
35 #include <QDateTime>
36 #include <QHostAddress>
37 #include <QRegExp>
38 #include <QTcpSocket>
39 #include <QTimer>
40 #include <QTextCodec>
41 #include <kdebug.h>
42 #include <kglobal.h>
43 #include <klocale.h>
46 // Maximum note size in chars we are going to accept,
47 // to prevent "note floods".
48 #define MAXBUFFER 4096
50 // Maximum time we are going to wait between data receptions,
51 // to prevent memory and connection floods. In milliseconds.
52 #define MAXTIME 10000
54 // Small buffer's size
55 #define SBSIZE 512
57 KNotesNetworkReceiver::KNotesNetworkReceiver( QTcpSocket *s )
58 : QObject(), m_buffer( new QByteArray() ), m_sock( s )
60 QString date =
61 KGlobal::locale()->formatDateTime( QDateTime::currentDateTime(),
62 KLocale::ShortDate, false );
64 // Add the remote IP or hostname and the date to the title, to help the
65 // user guess who wrote it.
66 m_titleAddon = QString( " [%1, %2]" )
67 .arg( m_sock->peerAddress().toString() )
68 .arg( date );
70 // Setup the communications
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)));
75 // Setup the timer
76 m_timer = new QTimer( this );
77 m_timer->setSingleShot( true );
78 connect( m_timer, SIGNAL(timeout()), SLOT(slotReceptionTimeout()) );
79 m_timer->start( MAXTIME );
82 KNotesNetworkReceiver::~KNotesNetworkReceiver()
84 delete m_buffer;
85 delete m_sock;
88 void KNotesNetworkReceiver::slotDataAvailable()
90 char smallBuffer[SBSIZE];
91 int smallBufferLen;
93 do {
94 // Append to "big buffer" only if we have some space left.
95 int curLen = m_buffer->count();
97 smallBufferLen = m_sock->read( smallBuffer, SBSIZE );
99 // Limit max transfer over buffer, to avoid overflow.
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 );
108 // If we are overflowing, close connection.
109 if ( m_buffer->count() == MAXBUFFER ) {
110 m_sock->close();
111 } else {
112 m_timer->start( MAXTIME );
116 void KNotesNetworkReceiver::slotReceptionTimeout()
118 m_sock->close();
121 void KNotesNetworkReceiver::slotConnectionClosed()
123 QTextCodec *codec = QTextCodec::codecForLocale();
125 if ( m_timer->isActive() ) {
126 QString noteText = QString( codec->toUnicode( *m_buffer ) ).trimmed();
128 // First line is the note title or, in case of ATnotes, the id
129 int pos = noteText.indexOf( QRegExp( "[\r\n]" ) );
130 QString noteTitle = noteText.left( pos ).trimmed() + m_titleAddon;
132 noteText = noteText.mid( pos ).trimmed();
134 if ( !noteText.isEmpty() ) {
135 emit sigNoteReceived( noteTitle, noteText );
139 deleteLater();
142 void KNotesNetworkReceiver::slotError( QAbstractSocket::SocketError error )
144 kWarning( 5500 ) <<"error type :"<< ( int ) error <<" error string : "<<m_sock->errorString();
147 #include "knotesnetrecv.moc"