00001 #include <netinet/in.h>
00002
00003 #include <qfile.h>
00004 #include <qsocket.h>
00005 #include <qhostaddress.h>
00006
00007 #include <opie2/odebug.h>
00008
00009
00010 #include "dcctransferrecv.h"
00011
00012
00013 DCCTransferRecv::DCCTransferRecv(Q_UINT32 ip4Addr, Q_UINT16 port, const QString &filename, unsigned int size)
00014 : DCCTransfer(ip4Addr, port, filename, size)
00015 {
00016 QHostAddress ip(ip4Addr);
00017 m_socket->connectToHost(ip.toString(), m_port);
00018 connect(m_socket, SIGNAL(readyRead()), this, SLOT(slotProcess()));
00019 connect(m_socket, SIGNAL(connectionClosed()), this, SLOT(slotFinished()));
00020
00021 m_file->open(IO_WriteOnly);
00022 }
00023
00024
00025 void DCCTransferRecv::slotProcess()
00026 {
00027 int availableBytes = m_socket->bytesAvailable();
00028 int receivedBytes = 0;
00029
00030 while(receivedBytes < availableBytes) {
00031 int bytes = m_socket->readBlock(m_buffer, m_bufSize);
00032 receivedBytes += bytes;
00033 m_file->writeBlock(m_buffer, bytes);
00034 }
00035
00036 m_file->flush();
00037 m_processedSize += availableBytes;
00038 unsigned long value = htonl(m_processedSize);
00039 m_socket->writeBlock((char*)&value, sizeof(unsigned long));
00040
00041 emit (progress((m_processedSize * 100) / m_totalSize));
00042 }
00043
00044 void DCCTransferRecv::slotFinished()
00045 {
00046 m_file->close();
00047
00048 if(m_processedSize == m_totalSize)
00049 emit(finished(this, DCCTransfer::Successfull));
00050 else
00051 emit(finished(this, DCCTransfer::PeerAborted));
00052 }
00053