• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • kdepim
  • Sitemap
  • Contact Us
 

knotes

knotesnetrecv.cpp

Go to the documentation of this file.
00001 /*******************************************************************
00002  KNotes -- Notes for the KDE project
00003 
00004  Copyright (c) 2003, Daniel Martin <daniel.martin@pirack.com>
00005                2004, 2006, Michael Brade <brade@kde.org>
00006 
00007  This program is free software; you can redistribute it and/or
00008  modify it under the terms of the GNU General Public License
00009  as published by the Free Software Foundation; either version 2
00010  of the License, or (at your option) any later version.
00011 
00012  This program is distributed in the hope that it will be useful,
00013  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  GNU General Public License for more details.
00016 
00017  You should have received a copy of the GNU General Public License
00018  along with this program; if not, write to the Free Software
00019  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020 
00021  In addition, as a special exception, the copyright holders give
00022  permission to link the code of this program with any edition of
00023  the Qt library by Trolltech AS, Norway (or with modified versions
00024  of Qt that use the same license as Qt), and distribute linked
00025  combinations including the two.  You must obey the GNU General
00026  Public License in all respects for all of the code used other than
00027  Qt.  If you modify this file, you may extend this exception to
00028  your version of the file, but you are not obligated to do so.  If
00029  you do not wish to do so, delete this exception statement from
00030  your version.
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 // Maximum note size in chars we are going to accept,
00045 // to prevent "note floods".
00046 #define MAXBUFFER 4096
00047 
00048 // Maximum time we are going to wait between data receptions,
00049 // to prevent memory and connection floods. In milliseconds.
00050 #define MAXTIME 10000
00051 
00052 // Small buffer's size
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   // Add the remote IP or hostname and the date to the title, to help the
00063   // user guess who wrote it.
00064   m_titleAddon = QString( " [%1, %2]" )
00065                   .arg( m_sock->peerAddress().toString() )
00066                   .arg( date );
00067   
00068   // Setup the communications
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   // Setup the timer
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     // Append to "big buffer" only if we have some space left.
00093     int curLen = m_buffer->count();
00094     
00095     smallBufferLen = m_sock->read( smallBuffer, SBSIZE );
00096     
00097     // Limit max transfer over buffer, to avoid overflow.
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   // If we are overflowing, close connection.
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     // First line is the note title or, in case of ATnotes, the id
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"

knotes

Skip menu "knotes"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members

kdepim

Skip menu "kdepim"
  • akonadi
  •   clients
  •   kabc
  •   kcal
  •   kcm
  • akregator
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt
  • kdgantt1
  • kjots
  • kleopatra
  • kmail
  • kmobiletools
  • knode
  • knotes
  • kontact
  • kontactinterfaces
  • korganizer
  •   korgac
  • kpilot
  • ktimetracker
  •   doc
  • libkdepim
  • libkholidays
  • libkleo
  • libkpgp
  • maildir
Generated for kdepim by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal