TDE personal information management applications
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

153 lines
4.8 KiB

  1. /*******************************************************************
  2. KNotes -- Notes for the KDE project
  3. Copyright (c) 2003, Daniel Martin <daniel.martin@pirack.com>
  4. 2004, 2006, Michael Brade <brade@kde.org>
  5. This program is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License
  7. as published by the Free Software Foundation; either version 2
  8. of the License, or (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. In addition, as a special exception, the copyright holders give
  17. permission to link the code of this program with any edition of
  18. the TQt library by Trolltech AS, Norway (or with modified versions
  19. of TQt that use the same license as TQt), and distribute linked
  20. combinations including the two. You must obey the GNU General
  21. Public License in all respects for all of the code used other than
  22. TQt. If you modify this file, you may extend this exception to
  23. your version of the file, but you are not obligated to do so. If
  24. you do not wish to do so, delete this exception statement from
  25. your version.
  26. *******************************************************************/
  27. #include <tqtimer.h>
  28. #include <tqdatetime.h>
  29. #include <tqregexp.h>
  30. #include <tqcstring.h>
  31. #include <kdebug.h>
  32. #include <kbufferedsocket.h>
  33. #include <tdesocketaddress.h>
  34. #include <tdelocale.h>
  35. #include <tdeglobal.h>
  36. #include "knotesnetrecv.h"
  37. // Maximum note size in chars we are going to accept,
  38. // to prevent "note floods".
  39. #define MAXBUFFER 4096
  40. // Maximum time we are going to wait between data receptions,
  41. // to prevent memory and connection floods. In milliseconds.
  42. #define MAXTIME 10000
  43. // Small buffer's size
  44. #define SBSIZE 512
  45. using namespace KNetwork;
  46. KNotesNetworkReceiver::KNotesNetworkReceiver( TDEBufferedSocket *s )
  47. : TQObject(),
  48. m_buffer( new TQByteArray() ), m_sock( s )
  49. {
  50. TQString date = TDEGlobal::locale()->formatDateTime( TQDateTime::currentDateTime(), true, false );
  51. // Add the remote IP or hostname and the date to the title, to help the
  52. // user guess who wrote it.
  53. m_titleAddon = TQString(" [%1, %2]")
  54. .arg( m_sock->peerAddress().nodeName() )
  55. .arg( date );
  56. // Setup the communications
  57. connect( m_sock, TQT_SIGNAL(readyRead()), TQT_SLOT(slotDataAvailable()) );
  58. connect( m_sock, TQT_SIGNAL(closed()), TQT_SLOT(slotConnectionClosed()) );
  59. connect( m_sock, TQT_SIGNAL(gotError( int )), TQT_SLOT(slotError( int )) );
  60. m_sock->enableRead( true );
  61. // Setup the timer
  62. m_timer = new TQTimer( this, "m_timer" );
  63. connect( m_timer, TQT_SIGNAL(timeout()), TQT_SLOT(slotReceptionTimeout()) );
  64. m_timer->start( MAXTIME, true );
  65. }
  66. KNotesNetworkReceiver::~KNotesNetworkReceiver()
  67. {
  68. delete m_buffer;
  69. delete m_sock;
  70. }
  71. void KNotesNetworkReceiver::slotDataAvailable()
  72. {
  73. char smallBuffer[SBSIZE];
  74. int smallBufferLen;
  75. do
  76. {
  77. // Append to "big buffer" only if we have some space left.
  78. int curLen = m_buffer->count();
  79. smallBufferLen = m_sock->readBlock( smallBuffer, SBSIZE );
  80. // Limit max transfer over buffer, to avoid overflow.
  81. smallBufferLen = kMin( smallBufferLen, MAXBUFFER - curLen );
  82. if ( smallBufferLen > 0 )
  83. {
  84. m_buffer->resize( curLen + smallBufferLen );
  85. memcpy( m_buffer->data() + curLen, smallBuffer, smallBufferLen );
  86. }
  87. }
  88. while ( smallBufferLen == SBSIZE );
  89. // If we are overflowing, close connection.
  90. if ( m_buffer->count() == MAXBUFFER )
  91. m_sock->close();
  92. else
  93. m_timer->changeInterval( MAXTIME );
  94. }
  95. void KNotesNetworkReceiver::slotReceptionTimeout()
  96. {
  97. m_sock->close();
  98. }
  99. void KNotesNetworkReceiver::slotConnectionClosed()
  100. {
  101. if ( m_timer->isActive() )
  102. {
  103. TQString noteText = TQString( *m_buffer ).stripWhiteSpace();
  104. // First line is the note title or, in case of ATnotes, the id
  105. int pos = noteText.find( TQRegExp("[\r\n]") );
  106. TQString noteTitle = noteText.left( pos ).stripWhiteSpace() + m_titleAddon;
  107. noteText = noteText.mid( pos ).stripWhiteSpace();
  108. if ( !noteText.isEmpty() )
  109. emit sigNoteReceived( noteTitle, noteText );
  110. }
  111. deleteLater();
  112. }
  113. void KNotesNetworkReceiver::slotError( int err )
  114. {
  115. kdWarning() << k_funcinfo
  116. << m_sock->KNetwork::TDESocketBase::errorString( static_cast<TDESocketBase::SocketError>(err) )
  117. << endl;
  118. }
  119. #include "knotesnetrecv.moc"