knotes

knotesnetsend.cpp

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 <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     // QObject:: prefix needed, otherwise the KStreamSocket::connect()
00050     // mehtod is called!!!
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     // TODO: support for unicode and rich text.
00066     // Mmmmmm... how to behave with such heterogeneous environment?
00067     // AFAIK, ATnotes does not allow UNICODE.
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     // If end of text reached, close connection
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"
KDE Home | KDE Accessibility Home | Description of Access Keys