knotes Library API Documentation

knotesnetsend.cpp

00001 /*******************************************************************
00002  KNotes -- Notes for the KDE project
00003 
00004  Copyright (c) 2003, Daniel Martin <daniel.martin@pirack.com>
00005                2004, 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, 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 #include <ksockaddr.h>
00036 
00037 #include "knotesnetsend.h"
00038 
00039 // This comes in seconds!
00040 #define CONNECT_TIMEOUT 10
00041 
00042 
00043 KNotesNetworkSender::KNotesNetworkSender( const QString& hostname, int port )
00044   : KExtendedSocket( hostname, port )
00045 {
00046     enableRead( false );
00047     enableWrite( false );
00048     setBlockingMode( true );    // Recommended by documentation.
00049     setTimeout( CONNECT_TIMEOUT );
00050 
00051     QObject::connect( this, SIGNAL(connectionSuccess()), SLOT(slotConnected()) );
00052     QObject::connect( this, SIGNAL(connectionFailed( int )), SLOT(slotError( int )) );
00053     QObject::connect( this, SIGNAL(closed( int )), SLOT(slotClosed( int )) );
00054     QObject::connect( this, SIGNAL(readyWrite()), SLOT(slotReadyWrite()) );
00055 }
00056 
00057 void KNotesNetworkSender::setSenderId( const QString& sender )
00058 {
00059     m_sender = sender.ascii();
00060 }
00061 
00062 void KNotesNetworkSender::setNote( const QString& title, const QString& text )
00063 {
00064     // TODO: support for unicode and richtext.
00065     // Mmmmmm... how to behave with such heterogeneous environment?
00066     // AFAIK, ATnotes does not allow UNICODE.
00067     m_title = title.ascii();
00068     m_note = text.ascii();
00069 }
00070 
00071 void KNotesNetworkSender::slotConnected()
00072 {
00073     if ( m_sender.isEmpty() )
00074         m_note.prepend( m_title + "\n");
00075     else
00076         m_note.prepend( m_title + " (" + m_sender + ")\n" );
00077 
00078     enableWrite( true );
00079 }
00080 
00081 void KNotesNetworkSender::slotReadyWrite()
00082 {
00083     m_index += writeBlock( m_note.data() + m_index, m_note.length() - m_index );
00084 
00085     // If end of text reached, close connection
00086     if ( m_index == m_note.length() )
00087         closeNow();
00088 }
00089 
00090 void KNotesNetworkSender::slotError( int errorCode )
00091 {
00092     KMessageBox::sorry( 0, i18n("Communication error: %1").arg(
00093                                 strError( status(), errorCode ) ) );
00094     slotClosed( 0 );
00095 }
00096 
00097 void KNotesNetworkSender::slotClosed( int /*state*/ )
00098 {
00099     delete this;
00100 }
00101 
00102 #include "knotesnetsend.moc"
KDE Logo
This file is part of the documentation for knotes Library Version 3.3.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Thu Aug 2 09:53:33 2007 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003