knotes

knotesnetsend.cpp
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 TQt library by Trolltech AS, Norway (or with modified versions
24  of TQt that use the same license as TQt), 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  TQt. 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 <tdelocale.h>
34 #include <tdemessagebox.h>
35 
36 #include "knotesnetsend.h"
37 
38 #define CONNECT_TIMEOUT 10000
39 
40 
41 KNotesNetworkSender::KNotesNetworkSender( const TQString& hostname, int port )
42  : TDEBufferedSocket( hostname, TQString::number( port ) ),
43  m_note( 0 ), m_title( 0 ), m_sender( 0 ), m_index( 0 )
44 {
45  enableRead( false );
46  enableWrite( false );
47  setTimeout( CONNECT_TIMEOUT );
48 
49  // TQObject:: prefix needed, otherwise the KStreamSocket::connect()
50  // mehtod is called!!!
51  TQObject::connect( this, TQ_SIGNAL(connected( const KResolverEntry& )),
52  TQ_SLOT(slotConnected( const KResolverEntry& )) );
53  TQObject::connect( this, TQ_SIGNAL(gotError( int )), TQ_SLOT(slotError( int )) );
54  TQObject::connect( this, TQ_SIGNAL(closed()), TQ_SLOT(slotClosed()) );
55  TQObject::connect( this, TQ_SIGNAL(readyWrite()), TQ_SLOT(slotReadyWrite()) );
56 }
57 
58 void KNotesNetworkSender::setSenderId( const TQString& sender )
59 {
60  m_sender = sender.ascii();
61 }
62 
63 void KNotesNetworkSender::setNote( const TQString& title, const TQString& text )
64 {
65  // TODO: support for unicode and rich text.
66  // Mmmmmm... how to behave with such heterogeneous environment?
67  // AFAIK, ATnotes does not allow UNICODE.
68  m_title = title.ascii();
69  m_note = text.ascii();
70 }
71 
72 void KNotesNetworkSender::slotConnected( const KResolverEntry& )
73 {
74  if ( m_sender.isEmpty() )
75  m_note.prepend( m_title + "\n");
76  else
77  m_note.prepend( m_title + " (" + m_sender + ")\n" );
78 
79  enableWrite( true );
80 }
81 
82 void KNotesNetworkSender::slotReadyWrite()
83 {
84  m_index += writeBlock( m_note.data() + m_index, m_note.length() - m_index );
85 
86  // If end of text reached, close connection
87  if ( m_index == m_note.length() )
88  close();
89 }
90 
91 void KNotesNetworkSender::slotError( int err )
92 {
93  KMessageBox::sorry( 0, i18n("Communication error: %1")
94  .arg( TQString(KNetwork::TDESocketBase::errorString( static_cast<TDESocketBase::SocketError>(err) )) )
95  );
96  slotClosed();
97 }
98 
99 void KNotesNetworkSender::slotClosed()
100 {
101  deleteLater();
102 }
103 
104 #include "knotesnetsend.moc"