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

knotes

  • sources
  • kde-4.12
  • kdepim
  • knotes
  • network
knotesnetrecv.cpp
Go to the documentation of this file.
1 /*******************************************************************
2  KNotes -- Notes for the KDE project
3 
4  Copyright (c) 2003, Daniel Martin <daniel.martin@pirack.com>
5  2004, 2006, Michael Brade <brade@kde.org>
6 
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.
11 
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.
16 
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.
20 
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 *******************************************************************/
32 
33 #include "knotesnetrecv.h"
34 
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>
44 
45 
46 // Maximum note size in chars we are going to accept,
47 // to prevent "note floods".
48 #define MAXBUFFER 4096
49 
50 // Maximum time we are going to wait between data receptions,
51 // to prevent memory and connection floods. In milliseconds.
52 #define MAXTIME 10000
53 
54 // Small buffer's size
55 #define SBSIZE 512
56 
57 KNotesNetworkReceiver::KNotesNetworkReceiver( QTcpSocket *s )
58  : QObject(), m_buffer( new QByteArray() ), m_sock( s )
59 {
60  QString date =
61  KGlobal::locale()->formatDateTime( QDateTime::currentDateTime(),
62  KLocale::ShortDate, false );
63 
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::fromLatin1( " [%1, %2]" )
67  .arg( m_sock->peerAddress().toString() )
68  .arg( date );
69 
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)));
74 
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 );
80 }
81 
82 KNotesNetworkReceiver::~KNotesNetworkReceiver()
83 {
84  delete m_buffer;
85  delete m_sock;
86 }
87 
88 void KNotesNetworkReceiver::slotDataAvailable()
89 {
90  char smallBuffer[SBSIZE];
91  int smallBufferLen;
92 
93  do {
94  // Append to "big buffer" only if we have some space left.
95  int curLen = m_buffer->count();
96 
97  smallBufferLen = m_sock->read( smallBuffer, SBSIZE );
98 
99  // Limit max transfer over buffer, to avoid overflow.
100  smallBufferLen = qMin( smallBufferLen, MAXBUFFER - curLen );
101 
102  if ( smallBufferLen > 0 ) {
103  m_buffer->resize( curLen + smallBufferLen );
104  memcpy( m_buffer->data() + curLen, smallBuffer, smallBufferLen );
105  }
106  } while ( smallBufferLen == SBSIZE );
107 
108  // If we are overflowing, close connection.
109  if ( m_buffer->count() == MAXBUFFER ) {
110  m_sock->close();
111  } else {
112  m_timer->start( MAXTIME );
113  }
114 }
115 
116 void KNotesNetworkReceiver::slotReceptionTimeout()
117 {
118  m_sock->close();
119 }
120 
121 void KNotesNetworkReceiver::slotConnectionClosed()
122 {
123  QTextCodec *codec = QTextCodec::codecForLocale();
124 
125  if ( m_timer->isActive() ) {
126  QString noteText = QString( codec->toUnicode( *m_buffer ) ).trimmed();
127 
128  // First line is the note title or, in case of ATnotes, the id
129  const int pos = noteText.indexOf( QRegExp( QLatin1String("[\r\n]") ) );
130  const QString noteTitle = noteText.left( pos ).trimmed() + m_titleAddon;
131 
132  noteText = noteText.mid( pos ).trimmed();
133 
134  if ( !noteText.isEmpty() ) {
135  emit sigNoteReceived( noteTitle, noteText );
136  }
137  }
138 
139  deleteLater();
140 }
141 
142 void KNotesNetworkReceiver::slotError( QAbstractSocket::SocketError error )
143 {
144  kWarning( 5500 ) <<"error type :"<< ( int ) error <<" error string : "<<m_sock->errorString();
145 }
146 
147 #include "knotesnetrecv.moc"
KNotesNetworkReceiver::~KNotesNetworkReceiver
~KNotesNetworkReceiver()
Definition: knotesnetrecv.cpp:82
KNotesNetworkReceiver::KNotesNetworkReceiver
KNotesNetworkReceiver(QTcpSocket *)
Definition: knotesnetrecv.cpp:57
KNotesNetworkReceiver::sigNoteReceived
void sigNoteReceived(const QString &, const QString &)
QObject
knotesnetrecv.h
MAXTIME
#define MAXTIME
Definition: knotesnetrecv.cpp:52
SBSIZE
#define SBSIZE
Definition: knotesnetrecv.cpp:55
MAXBUFFER
#define MAXBUFFER
Definition: knotesnetrecv.cpp:48
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:56:33 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

knotes

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

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal