knotes
knotesnetsend.cpp00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include <klocale.h>
00034 #include <kmessagebox.h>
00035
00036 #include "knotesnetsend.h"
00037
00038 #define CONNECT_TIMEOUT 10000
00039
00040
00041 KNotesNetworkSender::KNotesNetworkSender( const QString& hostname, int port )
00042 : KBufferedSocket( hostname, QString::number( port ) ),
00043 m_note( 0 ), m_title( 0 ), m_sender( 0 ), m_index( 0 )
00044 {
00045 enableRead( false );
00046 enableWrite( false );
00047 setTimeout( CONNECT_TIMEOUT );
00048
00049
00050
00051 QObject::connect( this, SIGNAL(connected( const KResolverEntry& )),
00052 SLOT(slotConnected( const KResolverEntry& )) );
00053 QObject::connect( this, SIGNAL(gotError( int )), SLOT(slotError( int )) );
00054 QObject::connect( this, SIGNAL(closed()), SLOT(slotClosed()) );
00055 QObject::connect( this, SIGNAL(readyWrite()), SLOT(slotReadyWrite()) );
00056 }
00057
00058 void KNotesNetworkSender::setSenderId( const QString& sender )
00059 {
00060 m_sender = sender.ascii();
00061 }
00062
00063 void KNotesNetworkSender::setNote( const QString& title, const QString& text )
00064 {
00065
00066
00067
00068 m_title = title.ascii();
00069 m_note = text.ascii();
00070 }
00071
00072 void KNotesNetworkSender::slotConnected( const KResolverEntry& )
00073 {
00074 if ( m_sender.isEmpty() )
00075 m_note.prepend( m_title + "\n");
00076 else
00077 m_note.prepend( m_title + " (" + m_sender + ")\n" );
00078
00079 enableWrite( true );
00080 }
00081
00082 void KNotesNetworkSender::slotReadyWrite()
00083 {
00084 m_index += writeBlock( m_note.data() + m_index, m_note.length() - m_index );
00085
00086
00087 if ( m_index == m_note.length() )
00088 close();
00089 }
00090
00091 void KNotesNetworkSender::slotError( int err )
00092 {
00093 KMessageBox::sorry( 0, i18n("Communication error: %1")
00094 .arg( errorString( static_cast<KSocketBase::SocketError>(err) ) )
00095 );
00096 slotClosed();
00097 }
00098
00099 void KNotesNetworkSender::slotClosed()
00100 {
00101 deleteLater();
00102 }
00103
00104 #include "knotesnetsend.moc"
|