1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
|
/*
receive a file on DCC protocol
begin: Mit Aug 7 2002
copyright: (C) 2002 by Dario Abatianni
email: eisfuchs@tigress.com
*/
// Copyright (C) 2004-2007 Shintaro Matsuoka <shin@shoegazed.org>
// Copyright (C) 2004,2005 John Tapsell <john@geola.co.uk>
/*
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 "dcctransferrecv.h"
#include "dcccommon.h"
#include "channel.h"
#include "dcctransfermanager.h"
#include "konversationapplication.h"
#include "connectionmanager.h"
#include "server.h"
#include <kdebug.h>
#include <tdefileitem.h>
#include <kiconloader.h>
#include <tdelocale.h>
#include <tdemessagebox.h>
#include <kserversocket.h>
#include <kstandarddirs.h>
#include <kstreamsocket.h>
#include <kdirselectdialog.h>
#include <kuser.h>
#include <tdeio/job.h>
#include <tdeio/jobclasses.h>
#include <tdeio/netaccess.h>
class DccResumeDialog;
/*
*flow chart*
DccTransferRecv()
start() : called from DccTransferPanel when user pushes the accept button
| \
| requestResume() : called when user chooses to resume in DccResumeDialog. it emits the signal ResumeRequest()
|
| startResume() : called by "Server"
| |
connectToSender()
connectionSuccess() : called by recvSocket
*/
DccTransferRecv::DccTransferRecv(TQObject* parent)
: DccTransfer( DccTransfer::Receive, parent )
{
kdDebug() << "DccTransferRecv::DccTransferRecv()" << endl;
m_serverSocket = 0;
m_recvSocket = 0;
m_writeCacheHandler = 0;
m_connectionTimer = new TQTimer( this );
connect( m_connectionTimer, TQ_SIGNAL( timeout() ), this, TQ_SLOT( connectionTimeout() ) );
//timer hasn't started yet. qtimer will be deleted automatically when 'this' object is deleted
}
DccTransferRecv::~DccTransferRecv()
{
cleanUp();
}
TQString DccTransferRecv::getTypeText() const
{
return i18n( "Receive" );
}
TQPixmap DccTransferRecv::getTypeIcon() const
{
return TDEGlobal::iconLoader()->loadIcon( "go-down", TDEIcon::Small );
}
void DccTransferRecv::cleanUp()
{
kdDebug() << "DccTransferRecv::cleanUp()" << endl;
stopConnectionTimer();
finishTransferLogger();
if ( m_serverSocket )
{
m_serverSocket->close();
m_serverSocket = 0;
}
if ( m_recvSocket )
{
m_recvSocket->close();
m_recvSocket = 0; // the instance will be deleted automatically by its parent
}
if ( m_writeCacheHandler )
{
m_writeCacheHandler->closeNow();
m_writeCacheHandler->deleteLater();
m_writeCacheHandler = 0;
}
}
// just for convenience
void DccTransferRecv::failed( const TQString& errorMessage )
{
setStatus( Failed, errorMessage );
cleanUp();
emit done( this );
}
void DccTransferRecv::setPartnerIp( const TQString& ip )
{
if ( getStatus() == Configuring )
m_partnerIp = ip;
}
void DccTransferRecv::setPartnerPort( const TQString& port )
{
if ( getStatus() == Configuring )
m_partnerPort = port;
}
void DccTransferRecv::setFileSize( unsigned long fileSize )
{
if ( getStatus() == Configuring )
m_fileSize = fileSize;
}
void DccTransferRecv::setFileName( const TQString& fileName )
{
if ( getStatus() == Configuring )
m_fileName = fileName;
}
void DccTransferRecv::setFileURL( const KURL& url )
{
if ( getStatus() == Configuring || getStatus() == Queued )
m_fileURL = url;
}
void DccTransferRecv::setReverse( bool reverse, const TQString& reverseToken )
{
if ( getStatus() == Configuring )
{
m_reverse = reverse;
if ( reverse )
{
m_partnerPort = TQString::number( 0 );
m_reverseToken = reverseToken;
}
}
}
bool DccTransferRecv::queue()
{
kdDebug() << "DccTransferRecv::queue()" << endl;
if ( getStatus() != Configuring )
return false;
if ( m_partnerIp.isEmpty() || m_partnerPort.isEmpty() )
return false;
if (!kapp->authorize("allow_downloading"))
{
//note we have this after the initialisations so that item looks okay
//Do not have the rights to send the file. Shouldn't have gotten this far anyway
failed(i18n("The admin has restricted the right to receive files"));
return false;
}
// check if the sender IP is valid
if ( m_partnerIp == "0.0.0.0" )
{
failed( i18n( "Invalid sender address (%1)" ).arg( m_partnerIp ) );
return false;
}
// TODO: should we support it?
if ( m_fileSize == 0 )
{
failed( i18n( "Unsupported negotiation (filesize=0)" ) );
return false;
}
if ( m_fileName.isEmpty() )
{
m_fileName = "unnamed_file";
}
if ( m_fileURL.isEmpty() )
{
// determine default incoming file URL
// set default folder
if ( !Preferences::dccPath().isEmpty() )
m_fileURL = KURL( Preferences::dccPath() );
else
m_fileURL.setPath( KUser( KUser::UseRealUserID ).homeDir() ); // default folder is *not* specified
// add a slash if there is none
m_fileURL.adjustPath( 1 );
// Append folder with partner's name if wanted
if ( Preferences::dccCreateFolder() )
m_fileURL.addPath( m_partnerNick + '/' );
// Just incase anyone tries to do anything nasty
TQString fileNameSanitized = sanitizeFileName( m_fileName );
// Append partner's name to file name if wanted
if ( Preferences::dccAddPartner() )
m_fileURL.addPath( m_partnerNick + '.' + fileNameSanitized );
else
m_fileURL.addPath( fileNameSanitized );
}
return DccTransfer::queue();
}
void DccTransferRecv::abort() // public slot
{
kdDebug() << "DccTransferRecv::abort()" << endl;
if(m_writeCacheHandler)
{
m_writeCacheHandler->write( true ); // flush
}
setStatus( Aborted );
cleanUp();
emit done( this );
}
void DccTransferRecv::start() // public slot
{
kdDebug() << "DccTransferRecv::start() [BEGIN]" << endl;
if ( getStatus() != Queued )
return;
setStatus( Preparing );
prepareLocalKio( false, false );
kdDebug() << "DccTransferRecv::start() [END]" << endl;
}
void DccTransferRecv::prepareLocalKio( bool overwrite, bool resume, TDEIO::fileoffset_t startPosition /* = 0 */ )
{
kdDebug() << "DccTransferRecv::prepareLocalKio()" << endl
<< "DccTransferRecv::prepareLocalKio(): URL: " << m_fileURL << endl
<< "DccTransferRecv::prepareLocalKio(): Overwrite: " << overwrite << endl
<< "DccTransferRecv::prepareLocalKio(): Resume: " << resume << " (Position: " << TQString::number( startPosition ) << ")" << endl;
m_resumed = resume;
m_transferringPosition = startPosition;
if ( !createDirs( m_fileURL.upURL() ) )
{
askAndPrepareLocalKio( i18n( "<b>Cannot create the folder.</b><br>"
"Folder: %1<br>" )
.arg( m_fileURL.upURL().prettyURL() ),
DccResumeDialog::RA_Rename | DccResumeDialog::RA_Cancel,
DccResumeDialog::RA_Rename );
return;
}
TDEIO::TransferJob* transferJob = TDEIO::put( m_fileURL, -1, overwrite, m_resumed, false );
if ( !transferJob )
{
kdDebug() << "DccTransferRecv::prepareLocalKio(): TDEIO::put() returned NULL. what happened?" << endl;
failed( i18n( "Could not create a TDEIO instance" ) );
return;
}
connect( transferJob, TQ_SIGNAL( canResume( TDEIO::Job*, TDEIO::filesize_t ) ), this, TQ_SLOT( slotLocalCanResume( TDEIO::Job*, TDEIO::filesize_t ) ) );
connect( transferJob, TQ_SIGNAL( result( TDEIO::Job* ) ), this, TQ_SLOT( slotLocalGotResult( TDEIO::Job* ) ) );
connect( transferJob, TQ_SIGNAL( dataReq( TDEIO::Job*, TQByteArray& ) ), this, TQ_SLOT( slotLocalReady( TDEIO::Job* ) ) );
}
void DccTransferRecv::askAndPrepareLocalKio( const TQString& message, int enabledActions, DccResumeDialog::ReceiveAction defaultAction, TDEIO::fileoffset_t startPosition )
{
switch ( DccResumeDialog::ask( this, message, enabledActions, defaultAction ) )
{
case DccResumeDialog::RA_Resume:
prepareLocalKio( false, true, startPosition );
break;
case DccResumeDialog::RA_Overwrite:
prepareLocalKio( true, false );
break;
case DccResumeDialog::RA_Rename:
prepareLocalKio( false, false );
break;
case DccResumeDialog::RA_Cancel:
default:
setStatus( Queued );
}
}
bool DccTransferRecv::createDirs( const KURL& dirURL ) const
{
KURL kurl( dirURL );
TQString surl = kurl.url();
//First we split directories until we reach to the top,
//since we need to create directories one by one
TQStringList dirList;
while ( surl != kurl.upURL().url() )
{
dirList.prepend( surl );
kurl = kurl.upURL();
surl = kurl.url();
}
//Now we create the directories
TQStringList::ConstIterator it;
for ( it=dirList.begin() ; it!=dirList.end() ; ++it )
if ( !TDEIO::NetAccess::exists( *it, true, NULL ) )
if ( !TDEIO::NetAccess::mkdir( *it, NULL, -1 ) )
return false;
return true;
}
void DccTransferRecv::slotLocalCanResume( TDEIO::Job* job, TDEIO::filesize_t size )
{
kdDebug() << "DccTransferRecv::slotLocalCanResume() [BEGIN]" << endl
<< "DccTransferRecv::slotLocalCanResume(): size: " << TQString::number( size ) << endl;
if ( size != 0 )
{
TDEIO::TransferJob* transferJob = static_cast<TDEIO::TransferJob*>( job );
disconnect( transferJob, 0, 0, 0 );
transferJob->kill();
if ( KonversationApplication::instance()->getDccTransferManager()->isLocalFileInWritingProcess( m_fileURL ) )
{
askAndPrepareLocalKio( i18n( "<b>The file is used by another transfer.</b><br>"
"%1<br>" )
.arg( m_fileURL.prettyURL() ),
DccResumeDialog::RA_Rename | DccResumeDialog::RA_Cancel,
DccResumeDialog::RA_Rename );
}
else if ( Preferences::dccAutoResume() )
{
prepareLocalKio( false, true, size );
}
else
{
askAndPrepareLocalKio( i18n( "<b>A partial file exists.</b><br>"
"%1<br>"
"Size of the partial file: %2 bytes<br>" )
.arg( m_fileURL.prettyURL() )
.arg( TDEGlobal::locale()->formatNumber( size, 0 ) ),
DccResumeDialog::RA_Resume | DccResumeDialog::RA_Overwrite | DccResumeDialog::RA_Rename | DccResumeDialog::RA_Cancel,
DccResumeDialog::RA_Resume,
size );
}
}
kdDebug() << "DccTransferRecv::slotLocalCanResume() [END]" << endl;
}
void DccTransferRecv::slotLocalGotResult( TDEIO::Job* job )
{
kdDebug() << "DccTransferRecv::slotLocalGotResult() [BEGIN]" << endl;
TDEIO::TransferJob* transferJob = static_cast<TDEIO::TransferJob*>( job );
disconnect( transferJob, 0, 0, 0 );
switch ( transferJob->error() )
{
case 0: // no error
kdDebug() << "DccTransferRecv::slotLocalGotResult(): job->error() returned 0." << endl
<< "DccTransferRecv::slotLocalGotResult(): Why was I called in spite of no error?" << endl;
break;
case TDEIO::ERR_FILE_ALREADY_EXIST:
askAndPrepareLocalKio( i18n( "<b>The file already exists.</b><br>"
"%1<br>" )
.arg( m_fileURL.prettyURL() ),
DccResumeDialog::RA_Overwrite | DccResumeDialog::RA_Rename | DccResumeDialog::RA_Cancel,
DccResumeDialog::RA_Overwrite );
break;
default:
askAndPrepareLocalKio( i18n( "<b>Could not open the file.<br>"
"Error: %1</b><br>"
"%2<br>" )
.arg( transferJob->error() )
.arg( m_fileURL.prettyURL() ),
DccResumeDialog::RA_Rename | DccResumeDialog::RA_Cancel,
DccResumeDialog::RA_Rename );
}
kdDebug() << "DccTransferRecv::slotLocalGotResult() [END]" << endl;
}
void DccTransferRecv::slotLocalReady( TDEIO::Job* job )
{
kdDebug() << "DccTransferRecv::slotLocalReady()" << endl;
TDEIO::TransferJob* transferJob = static_cast<TDEIO::TransferJob*>( job );
disconnect( transferJob, 0, 0, 0 ); // WriteCacheHandler will control the job after this
m_writeCacheHandler = new DccTransferRecvWriteCacheHandler( transferJob );
connect( m_writeCacheHandler, TQ_SIGNAL( done() ), this, TQ_SLOT( slotLocalWriteDone() ) );
connect( m_writeCacheHandler, TQ_SIGNAL( gotError( const TQString& ) ), this, TQ_SLOT( slotLocalGotWriteError( const TQString& ) ) );
if ( !m_resumed )
connectWithSender();
else
requestResume();
}
void DccTransferRecv::connectWithSender()
{
if ( m_reverse )
{
if ( !startListeningForSender() )
return;
Server* server = KonversationApplication::instance()->getConnectionManager()->getServerByConnectionId( m_connectionId );
if ( !server )
{
failed( i18n( "Could not send Reverse DCC SEND acknowledgement to the partner via the IRC server." ) );
}
m_ownIp = DccCommon::getOwnIp( server );
m_ownPort = TQString::number( DccCommon::getServerSocketPort( m_serverSocket ) );
setStatus( WaitingRemote, i18n( "Waiting for connection" ) );
server->dccReverseSendAck( m_partnerNick, m_fileName, DccCommon::textIpToNumericalIp( m_ownIp ), m_ownPort, m_fileSize, m_reverseToken );
//FIXME: add connection timer here
}
else
{
connectToSendServer();
}
}
void DccTransferRecv::requestResume()
{
kdDebug() << "DccTransferRecv::requestResume()" << endl;
setStatus( WaitingRemote, i18n( "Waiting for remote host's acceptance" ) );
startConnectionTimer( 30 );
kdDebug() << "DccTransferRecv::requestResume(): requesting resume for " << m_partnerNick << " file " << m_fileName << " partner " << m_partnerPort << endl;
//TODO m_filename could have been sanitized - will this effect this?
Server* server = KonversationApplication::instance()->getConnectionManager()->getServerByConnectionId( m_connectionId );
if ( !server )
{
kdDebug() << "DccTransferSend::start(): could not retrieve the instance of Server. Connection id: " << m_connectionId << endl;
failed( i18n( "Could not send DCC RECV resume request to the partner via the IRC server." ) );
return;
}
server->dccResumeGetRequest( m_partnerNick, m_fileName, m_partnerPort, m_transferringPosition );
}
// public slot
void DccTransferRecv::startResume( unsigned long position )
{
kdDebug() << "DccTransferRecv::startResume(): position: " << position << endl;
stopConnectionTimer();
if ( (unsigned long)m_transferringPosition != position )
{
kdDebug() << "DccTransferRecv::startResume(): remote responsed an unexpected position" << endl
<< "DccTransferRecv::startResume(): expected: " << TQString::number( m_transferringPosition ) << endl
<< "DccTransferRecv::startResume(): remote response: " << position << endl;
failed( i18n( "Unexpected response from remote host" ) );
return;
}
connectWithSender();
}
void DccTransferRecv::connectToSendServer()
{
kdDebug() << "DccTransferRecv::connectToSendServer()" << endl;
// connect to sender
setStatus( Connecting );
m_recvSocket = new KNetwork::KStreamSocket( m_partnerIp, m_partnerPort, this);
m_recvSocket->setBlocking( false ); // asynchronous mode
m_recvSocket->setFamily( KNetwork::KResolver::InetFamily );
m_recvSocket->setResolutionEnabled( false );
m_recvSocket->setTimeout( 30000 );
m_recvSocket->enableRead( false );
m_recvSocket->enableWrite( false );
connect( m_recvSocket, TQ_SIGNAL( connected( const KResolverEntry& ) ), this, TQ_SLOT( startReceiving() ) );
connect( m_recvSocket, TQ_SIGNAL( gotError( int ) ), this, TQ_SLOT( connectionFailed( int ) ) );
kdDebug() << "DccTransferRecv::connectToServer(): attempting to connect to " << m_partnerIp << ":" << m_partnerPort << endl;
m_recvSocket->connect();
}
bool DccTransferRecv::startListeningForSender()
{
// Set up server socket
TQString failedReason;
if ( Preferences::dccSpecificSendPorts() )
m_serverSocket = DccCommon::createServerSocketAndListen( this, &failedReason, Preferences::dccSendPortsFirst(), Preferences::dccSendPortsLast() );
else
m_serverSocket = DccCommon::createServerSocketAndListen( this, &failedReason );
if ( !m_serverSocket )
{
failed( failedReason );
return false;
}
connect( m_serverSocket, TQ_SIGNAL( readyAccept() ), this, TQ_SLOT( slotServerSocketReadyAccept() ) );
connect( m_serverSocket, TQ_SIGNAL( gotError( int ) ), this, TQ_SLOT( slotServerSocketGotError( int ) ) );
return true;
}
void DccTransferRecv::slotServerSocketReadyAccept()
{
//stopConnectionTimer()
m_recvSocket = static_cast<KNetwork::KStreamSocket*>( m_serverSocket->accept() );
if ( !m_recvSocket )
{
failed( i18n( "Could not accept the connection. (Socket Error)" ) );
return;
}
connect( m_recvSocket, TQ_SIGNAL( gotError( int ) ), this, TQ_SLOT( connectionFailed( int ) ) );
// we don't need ServerSocket anymore
m_serverSocket->close();
startReceiving();
}
void DccTransferRecv::slotServerSocketGotError( int /* errorCode*/ )
{
failed( i18n( "Socket error: %1" ).arg( m_serverSocket->errorString() ) );
}
void DccTransferRecv::startReceiving()
{
kdDebug() << "DccTransferRecv::startReceiving()" << endl;
m_recvSocket->setBlocking( false ); // asynchronous mode
connect( m_recvSocket, TQ_SIGNAL( readyRead() ), this, TQ_SLOT( readData() ) );
connect( m_recvSocket, TQ_SIGNAL( readyWrite() ), this, TQ_SLOT( sendAck() ) );
connect( m_recvSocket, TQ_SIGNAL( closed() ), this, TQ_SLOT( slotSocketClosed() ) );
setStatus( Transferring );
m_transferStartPosition = m_transferringPosition;
m_recvSocket->enableRead( true );
m_recvSocket->enableWrite( false );
startTransferLogger(); // initialize CPS counter, ETA counter, etc...
}
// slot
void DccTransferRecv::connectionFailed( int errorCode )
{
kdDebug() << "DccTransferRecv::connectionFailed(): code = " << errorCode << ", string = " << m_recvSocket->TDESocketBase::errorString() << endl;
failed( i18n( "Connection failure: %1" ).arg( m_recvSocket->TDESocketBase::errorString() ) );
}
void DccTransferRecv::readData() // slot
{
//kdDebug() << "readData()" << endl;
int actual = m_recvSocket->readBlock( m_buffer, m_bufferSize );
if ( actual > 0 )
{
//actual is the size we read in, and is guaranteed to be less than m_bufferSize
m_transferringPosition += actual;
m_writeCacheHandler->append( m_buffer, actual );
m_writeCacheHandler->write( false );
m_recvSocket->enableWrite( true );
}
}
void DccTransferRecv::sendAck() // slot
{
//kdDebug() << "sendAck()" << endl;
TDEIO::fileoffset_t pos = intel( m_transferringPosition );
m_recvSocket->enableWrite( false );
m_recvSocket->writeBlock( (char*)&pos, 4 );
if ( m_transferringPosition == (TDEIO::fileoffset_t)m_fileSize )
{
kdDebug() << "DccTransferRecv::sendAck(): Sent final ACK." << endl;
m_recvSocket->enableRead( false );
disconnect( m_recvSocket, 0, 0, 0 );
finishTransferLogger();
m_writeCacheHandler->close(); // WriteCacheHandler will send the signal done()
}
else if ( m_transferringPosition > (TDEIO::fileoffset_t)m_fileSize )
{
kdDebug() << "DccTransferRecv::sendAck(): the remote host sent larger data than expected: " << TQString::number( m_transferringPosition ) << endl;
failed( i18n( "Transferring error" ) );
}
}
void DccTransferRecv::slotLocalWriteDone() // <-WriteCacheHandler::done()
{
kdDebug() << "DccTransferRecv::slotLocalWriteDone()" << endl;
setStatus( Done );
cleanUp();
emit done( this );
}
// <- WriteCacheHandler::gotError()
void DccTransferRecv::slotLocalGotWriteError( const TQString& errorString )
{
kdDebug() << "DccTransferRecv::slotLocalGotWriteError()" << endl;
failed( i18n( "TDEIO error: %1" ).arg( errorString ) );
}
void DccTransferRecv::startConnectionTimer( int sec )
{
stopConnectionTimer();
kdDebug() << "DccTransferRecv::startConnectionTimer()" << endl;
m_connectionTimer->start( sec*1000, true );
}
void DccTransferRecv::stopConnectionTimer()
{
if ( m_connectionTimer->isActive() )
{
m_connectionTimer->stop();
kdDebug() << "DccTransferRecv::stopConnectionTimer(): stop" << endl;
}
}
void DccTransferRecv::connectionTimeout() // slot
{
kdDebug() << "DccTransferRecv::connectionTimeout()" << endl;
failed( i18n( "Timed out" ) );
}
void DccTransferRecv::slotSocketClosed()
{
finishTransferLogger();
if ( getStatus() == Transferring )
failed( i18n( "Remote user disconnected" ) );
}
// WriteCacheHandler
DccTransferRecvWriteCacheHandler::DccTransferRecvWriteCacheHandler( TDEIO::TransferJob* transferJob )
: m_transferJob( transferJob )
{
m_writeReady = true;
m_cacheStream = 0;
connect( this, TQ_SIGNAL( dataFinished() ), m_transferJob, TQ_SLOT( slotFinished() ) );
connect( m_transferJob, TQ_SIGNAL( dataReq( TDEIO::Job*, TQByteArray& ) ), this, TQ_SLOT( slotKIODataReq( TDEIO::Job*, TQByteArray& ) ) );
connect( m_transferJob, TQ_SIGNAL( result( TDEIO::Job* ) ), this, TQ_SLOT( slotKIOResult( TDEIO::Job* ) ) );
m_transferJob->setAsyncDataEnabled( m_writeAsyncMode = true );
}
DccTransferRecvWriteCacheHandler::~DccTransferRecvWriteCacheHandler()
{
closeNow();
}
// public
void DccTransferRecvWriteCacheHandler::append( char* data, int size )
{
// sendAsyncData() and dataReq() cost a lot of time, so we should pack some caches.
// 1meg
static const unsigned int maxWritePacketSize = 1 * 1024 * 1024;
if ( m_cacheList.isEmpty() || m_cacheList.back().size() + size > maxWritePacketSize )
{
m_cacheList.append( TQByteArray() );
delete m_cacheStream;
m_cacheStream = new TQDataStream( m_cacheList.back(), IO_WriteOnly );
}
m_cacheStream->writeRawBytes( data, size );
}
// public
bool DccTransferRecvWriteCacheHandler::write( bool force )
{
// force == false: return without doing anything when the whole cache size is smaller than maxWritePacketSize
if ( m_cacheList.isEmpty() || !m_transferJob || !m_writeReady || !m_writeAsyncMode )
return false;
if ( !force && m_cacheList.count() < 2 )
return false;
// do write
m_writeReady = false;
m_transferJob->sendAsyncData( m_cacheList.front() );
//kdDebug() << "DTRWriteCacheHandler::write(): wrote " << m_cacheList.front().size() << " bytes." << endl;
m_cacheList.pop_front();
return true;
}
void DccTransferRecvWriteCacheHandler::close() // public
{
kdDebug() << "DTRWriteCacheHandler::close()" << endl;
write( true ); // write once if tdeio is ready to write
m_transferJob->setAsyncDataEnabled( m_writeAsyncMode = false );
kdDebug() << "DTRWriteCacheHandler::close(): switched to synchronized mode." << endl;
kdDebug() << "DTRWriteCacheHandler::close(): flushing... (remaining caches: " << m_cacheList.count() << ")" << endl;
}
void DccTransferRecvWriteCacheHandler::closeNow() // public
{
write( true ); // flush
if ( m_transferJob )
{
m_transferJob->kill();
m_transferJob = 0;
}
m_cacheList.clear();
delete m_cacheStream;
m_cacheStream = 0;
}
void DccTransferRecvWriteCacheHandler::slotKIODataReq( TDEIO::Job*, TQByteArray& data )
{
// We are in writeAsyncMode if there is more data to be read in from dcc
if ( m_writeAsyncMode )
m_writeReady = true;
else
{
// No more data left to read from incoming dcctransfer
if ( !m_cacheList.isEmpty() )
{
// once we write everything in cache, the file is complete.
// This function will be called once more after this last data is written.
data = m_cacheList.front();
kdDebug() << "DccTransferRecvWriteCacheHandler::slotKIODataReq(): will write " << m_cacheList.front().size() << " bytes." << endl;
m_cacheList.pop_front();
}
else
{
// finally, no data left to write or read.
kdDebug() << "DTRWriteCacheHandler::slotKIODataReq(): flushing done." << endl;
m_transferJob = 0;
emit done(); // -> DccTransferRecv::slotLocalWriteDone()
}
}
}
void DccTransferRecvWriteCacheHandler::slotKIOResult( TDEIO::Job* job )
{
Q_ASSERT( m_transferJob );
disconnect( m_transferJob, 0, 0, 0 );
m_transferJob = 0;
if ( job->error() )
{
TQString errorString = job->errorString();
closeNow();
emit gotError( errorString ); // -> DccTransferRecv::slotLocalGotWriteError()
}
}
#include "dcctransferrecv.moc"
|