Logo Search packages:      
Sourcecode: kmess version File versions

inktransferp2p.cpp

/***************************************************************************
                          inktransferp2p.cpp -  description
                             -------------------
    begin                : Sun Mar 23 2008
    copyright            : (C) 2008 by Diederik van der Boor
    email                : "vdboor" --at-- "codingdomain.com"
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "inktransferp2p.h"

#include "../../kmessdebug.h"
#include "../mimemessage.h"



/**
 * The constructor
 */
00028 InkTransferP2P::InkTransferP2P( ApplicationList *applicationList )
: P2PApplication( applicationList )
{
  // Open the buffer directly.
  // The session is only used to receive data.
  inkBuffer_.open( QIODevice::ReadWrite );

  // Set the role / session mode of this class.
  // Is checked for by the ChatMaster, and P2PApplication base class.
  setMode( Application::APP_MODE_DATACAST );
  setDataCastSessionID( SESSION_ID );
}



/**
 * The destructor
 */
00046 InkTransferP2P::~InkTransferP2P()
{
}



/**
 * Called when data is received.
 *
 * This is the only method which will be called.
 * Ink transfer only uses the P2P transport layer to transmit data packets.
 *
 * @param  message  P2P message with the data.
 */
00060 void InkTransferP2P::gotData(const P2PMessage &message)
{
  kDebug() << "Received data...";
  // Write the data in the buffer.
  bool success = writeP2PDataToFile( message, &inkBuffer_ );
  if( ! success )
  {
    return;
  }

  // When all data is received, the parent class calls showTransferComplete().
  // That method will send the msnObjectReceived() signal.
}



/**
 * Called when the transfer is complete.
 * Reads the buffer, calls the inkReceived() signal.
 */
00080 void InkTransferP2P::showTransferComplete()
{
#ifdef KMESSDEBUG_MSNOBJECTTRANSFER_P2P
  kDebug() << "Last data part received, emitting signals";
#endif

  // Read the complete string. It's transmitted in multi byte format.
  const QByteArray &bufferDataUcs2 = inkBuffer_.data();
  QString bufferData( QString::fromUtf16( reinterpret_cast<const ushort*>( bufferDataUcs2.data() ), bufferDataUcs2.size() / 2 ) );

  // When a MimeMessage is sent over P2P,
  // there is a null-terminator after the head and body parts.
  int endMime = bufferData.indexOf("\r\n\r\n");
  if( endMime == -1 )
  {
    kWarning() << "Failed to find end of MIME header:" << bufferData;
    return;
  }

  // Get the ink header.
  MimeMessage mimeHeader( bufferData.left( endMime ) );

  // Test header.
  if( mimeHeader.getValue("Content-Type") != "image/gif" )
  {
    kWarning() << "Unable to parse incoming message of type" << mimeHeader.getValue("Content-Type") << "contact:" << getContactHandle();
    mimeHeader.print();
    return;
  }

  // Jump over the null terminator
  int bodyStart = endMime + 4;
  if( bufferData.at( bodyStart ) == '\0' )
  {
    bodyStart++;
  }

  // Avoid final null terminator
  int bodyEnd = bufferData.size();
  if( bufferData.at( bodyEnd - 1 ) == '\0' )
  {
    bodyEnd--;
  }

  // Get body
  QString mimeBody( bufferData.mid( bodyStart, bodyEnd - bodyStart ) );

  // Emit the signal.
  emit inkReceived( mimeBody, getContactHandle() );

  // ACK message will automatically be sent by the base class.
  // The ChatMaster class parses and validates the ink mime body.
  // Other issues (like bad message flags) are also handled.

  // Request termination
  endApplication();
}


#include "inktransferp2p.moc"

Generated by  Doxygen 1.6.0   Back to index