Diffstat (limited to 'noncore/net/opieirc/dcctransferrecv.cpp') (more/less context) (show whitespace changes)
-rw-r--r-- | noncore/net/opieirc/dcctransferrecv.cpp | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/noncore/net/opieirc/dcctransferrecv.cpp b/noncore/net/opieirc/dcctransferrecv.cpp new file mode 100644 index 0000000..58e8d09 --- a/dev/null +++ b/noncore/net/opieirc/dcctransferrecv.cpp | |||
@@ -0,0 +1,53 @@ | |||
1 | #include <netinet/in.h> | ||
2 | |||
3 | #include <qfile.h> | ||
4 | #include <qsocket.h> | ||
5 | #include <qhostaddress.h> | ||
6 | |||
7 | #include <opie2/odebug.h> | ||
8 | |||
9 | |||
10 | #include "dcctransferrecv.h" | ||
11 | |||
12 | |||
13 | DCCTransferRecv::DCCTransferRecv(Q_UINT32 ip4Addr, Q_UINT16 port, const QString &filename, unsigned int size) | ||
14 | : DCCTransfer(ip4Addr, port, filename, size) | ||
15 | { | ||
16 | QHostAddress ip(ip4Addr); | ||
17 | m_socket->connectToHost(ip.toString(), m_port); | ||
18 | connect(m_socket, SIGNAL(readyRead()), this, SLOT(slotProcess())); | ||
19 | connect(m_socket, SIGNAL(connectionClosed()), this, SLOT(slotFinished())); | ||
20 | |||
21 | m_file->open(IO_WriteOnly); | ||
22 | } | ||
23 | |||
24 | |||
25 | void DCCTransferRecv::slotProcess() | ||
26 | { | ||
27 | int availableBytes = m_socket->bytesAvailable(); | ||
28 | int receivedBytes = 0; | ||
29 | |||
30 | while(receivedBytes < availableBytes) { | ||
31 | int bytes = m_socket->readBlock(m_buffer, m_bufSize); | ||
32 | receivedBytes += bytes; | ||
33 | m_file->writeBlock(m_buffer, bytes); | ||
34 | } | ||
35 | |||
36 | m_file->flush(); | ||
37 | m_processedSize += availableBytes; | ||
38 | unsigned long value = htonl(m_processedSize); | ||
39 | m_socket->writeBlock((char*)&value, sizeof(unsigned long)); | ||
40 | |||
41 | emit (progress((m_processedSize * 100) / m_totalSize)); | ||
42 | } | ||
43 | |||
44 | void DCCTransferRecv::slotFinished() | ||
45 | { | ||
46 | m_file->close(); | ||
47 | |||
48 | if(m_processedSize == m_totalSize) | ||
49 | emit(finished(this, DCCTransfer::Successfull)); | ||
50 | else | ||
51 | emit(finished(this, DCCTransfer::PeerAborted)); | ||
52 | } | ||
53 | |||