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
|
/* This file is part of the KDE libraries
Copyright (C) 2000 David Faure <faure@kde.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License version 2 as published by the Free Software Foundation.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "kio/slaveinterface.h"
#include "kio/slavebase.h"
#include "kio/connection.h"
#include <errno.h>
#include <assert.h>
#include <kdebug.h>
#include <stdlib.h>
#include <sys/time.h>
#include <unistd.h>
#include <signal.h>
#include <kio/observer.h>
#include <kapplication.h>
#include <dcopclient.h>
#include <time.h>
#include <tqtimer.h>
using namespace KIO;
TQDataStream &operator <<(TQDataStream &s, const KIO::UDSEntry &e )
{
// On 32-bit platforms we send UDS_SIZE with UDS_SIZE_LARGE in front
// of it to carry the 32 msb. We can't send a 64 bit UDS_SIZE because
// that would break the compatibility of the wire-protocol with KDE 2.
// We do the same on 64-bit platforms in case we run in a mixed 32/64bit
// environment.
TQ_UINT32 size = 0;
KIO::UDSEntry::ConstIterator it = e.begin();
for( ; it != e.end(); ++it )
{
size++;
if ((*it).m_uds == KIO::UDS_SIZE)
size++;
}
s << size;
it = e.begin();
for( ; it != e.end(); ++it )
{
if ((*it).m_uds == KIO::UDS_SIZE)
{
KIO::UDSAtom a;
a.m_uds = KIO::UDS_SIZE_LARGE;
a.m_long = (*it).m_long >> 32;
s << a;
}
s << *it;
}
return s;
}
TQDataStream &operator >>(TQDataStream &s, KIO::UDSEntry &e )
{
e.clear();
TQ_UINT32 size;
s >> size;
// On 32-bit platforms we send UDS_SIZE with UDS_SIZE_LARGE in front
// of it to carry the 32 msb. We can't send a 64 bit UDS_SIZE because
// that would break the compatibility of the wire-protocol with KDE 2.
// We do the same on 64-bit platforms in case we run in a mixed 32/64bit
// environment.
TQ_LLONG msb = 0;
for(TQ_UINT32 i = 0; i < size; i++)
{
KIO::UDSAtom a;
s >> a;
if (a.m_uds == KIO::UDS_SIZE_LARGE)
{
msb = a.m_long;
}
else
{
if (a.m_uds == KIO::UDS_SIZE)
{
if (a.m_long < 0)
a.m_long += (TQ_LLONG) 1 << 32;
a.m_long += msb << 32;
}
e.append(a);
msb = 0;
}
}
return s;
}
static const unsigned int max_nums = 8;
class KIO::SlaveInterfacePrivate
{
public:
SlaveInterfacePrivate() {
slave_calcs_speed = false;
start_time.tv_sec = 0;
start_time.tv_usec = 0;
last_time = 0;
nums = 0;
filesize = 0;
offset = 0;
}
bool slave_calcs_speed;
struct timeval start_time;
uint nums;
long times[max_nums];
KIO::filesize_t sizes[max_nums];
size_t last_time;
KIO::filesize_t filesize, offset;
TQTimer speed_timer;
};
//////////////
SlaveInterface::SlaveInterface( Connection * connection )
{
m_pConnection = connection;
m_progressId = 0;
d = new SlaveInterfacePrivate;
connect(&d->speed_timer, TQT_SIGNAL(timeout()), TQT_SLOT(calcSpeed()));
}
SlaveInterface::~SlaveInterface()
{
// Note: no kdDebug() here (scheduler is deleted very late)
m_pConnection = 0; // a bit like the "wasDeleted" of TQObject...
delete d;
}
static KIO::filesize_t readFilesize_t(TQDataStream &stream)
{
KIO::filesize_t result;
unsigned long ul;
stream >> ul;
result = ul;
if (stream.atEnd())
return result;
stream >> ul;
result += ((KIO::filesize_t)ul) << 32;
return result;
}
bool SlaveInterface::dispatch()
{
assert( m_pConnection );
int cmd;
TQByteArray data;
if (m_pConnection->read( &cmd, data ) == -1)
return false;
return dispatch( cmd, data );
}
void SlaveInterface::calcSpeed()
{
if (d->slave_calcs_speed) {
d->speed_timer.stop();
return;
}
struct timeval tv;
gettimeofday(&tv, 0);
long diff = ((tv.tv_sec - d->start_time.tv_sec) * 1000000 +
tv.tv_usec - d->start_time.tv_usec) / 1000;
if (diff - d->last_time >= 900) {
d->last_time = diff;
if (d->nums == max_nums) {
// let's hope gcc can optimize that well enough
// otherwise I'd try memcpy :)
for (unsigned int i = 1; i < max_nums; ++i) {
d->times[i-1] = d->times[i];
d->sizes[i-1] = d->sizes[i];
}
d->nums--;
}
d->times[d->nums] = diff;
d->sizes[d->nums++] = d->filesize - d->offset;
KIO::filesize_t lspeed = 1000 * (d->sizes[d->nums-1] - d->sizes[0]) / (d->times[d->nums-1] - d->times[0]);
// kdDebug() << "proceeed " << (long)d->filesize << " " << diff << " "
// << long(d->sizes[d->nums-1] - d->sizes[0]) << " "
// << d->times[d->nums-1] - d->times[0] << " "
// << long(lspeed) << " " << double(d->filesize) / diff
// << " " << convertSize(lspeed) << " "
// << convertSize(long(double(d->filesize) / diff) * 1000) << " "
// << endl ;
if (!lspeed) {
d->nums = 1;
d->times[0] = diff;
d->sizes[0] = d->filesize - d->offset;
}
emit speed(lspeed);
}
}
bool SlaveInterface::dispatch( int _cmd, const TQByteArray &rawdata )
{
//kdDebug(7007) << "dispatch " << _cmd << endl;
TQDataStream stream( rawdata, IO_ReadOnly );
TQString str1;
TQ_INT32 i;
TQ_INT8 b;
TQ_UINT32 ul;
switch( _cmd ) {
case MSG_DATA:
emit data( rawdata );
break;
case MSG_DATA_REQ:
emit dataReq();
break;
case MSG_FINISHED:
//kdDebug(7007) << "Finished [this = " << this << "]" << endl;
d->offset = 0;
d->speed_timer.stop();
emit finished();
break;
case MSG_STAT_ENTRY:
{
UDSEntry entry;
stream >> entry;
emit statEntry(entry);
}
break;
case MSG_LIST_ENTRIES:
{
TQ_UINT32 count;
stream >> count;
UDSEntryList list;
UDSEntry entry;
for (uint i = 0; i < count; i++) {
stream >> entry;
list.append(entry);
}
emit listEntries(list);
}
break;
case MSG_RESUME: // From the put job
{
d->offset = readFilesize_t(stream);
emit canResume( d->offset );
}
break;
case MSG_CANRESUME: // From the get job
d->filesize = d->offset;
emit canResume(0); // the arg doesn't matter
break;
case MSG_ERROR:
stream >> i >> str1;
kdDebug(7007) << "error " << i << " " << str1 << endl;
emit error( i, str1 );
break;
case MSG_SLAVE_STATUS:
{
pid_t pid;
TQCString protocol;
stream >> pid >> protocol >> str1 >> b;
emit slaveStatus(pid, protocol, str1, (b != 0));
}
break;
case MSG_CONNECTED:
emit connected();
break;
case INF_TOTAL_SIZE:
{
KIO::filesize_t size = readFilesize_t(stream);
gettimeofday(&d->start_time, 0);
d->last_time = 0;
d->filesize = d->offset;
d->sizes[0] = d->filesize - d->offset;
d->times[0] = 0;
d->nums = 1;
d->speed_timer.start(1000);
d->slave_calcs_speed = false;
emit totalSize( size );
}
break;
case INF_PROCESSED_SIZE:
{
KIO::filesize_t size = readFilesize_t(stream);
emit processedSize( size );
d->filesize = size;
}
break;
case INF_SPEED:
stream >> ul;
d->slave_calcs_speed = true;
d->speed_timer.stop();
emit speed( ul );
break;
case INF_GETTING_FILE:
break;
case INF_ERROR_PAGE:
emit errorPage();
break;
case INF_REDIRECTION:
{
KURL url;
stream >> url;
emit redirection( url );
}
break;
case INF_MIME_TYPE:
stream >> str1;
emit mimeType( str1 );
if (!m_pConnection->suspended())
m_pConnection->sendnow( CMD_NONE, TQByteArray() );
break;
case INF_WARNING:
stream >> str1;
emit warning( str1 );
break;
case INF_NEED_PASSWD: {
AuthInfo info;
stream >> info;
openPassDlg( info );
break;
}
case INF_MESSAGEBOX: {
kdDebug(7007) << "needs a msg box" << endl;
TQString text, caption, buttonYes, buttonNo, dontAskAgainName;
int type;
stream >> type >> text >> caption >> buttonYes >> buttonNo;
if (stream.atEnd())
messageBox(type, text, caption, buttonYes, buttonNo);
else {
stream >> dontAskAgainName;
messageBox(type, text, caption, buttonYes, buttonNo, dontAskAgainName);
}
break;
}
case INF_INFOMESSAGE: {
TQString msg;
stream >> msg;
infoMessage(msg);
break;
}
case INF_META_DATA: {
MetaData meta_data;
stream >> meta_data;
metaData(meta_data);
break;
}
case MSG_NET_REQUEST: {
TQString host;
TQString slaveid;
stream >> host >> slaveid;
requestNetwork(host, slaveid);
break;
}
case MSG_NET_DROP: {
TQString host;
TQString slaveid;
stream >> host >> slaveid;
dropNetwork(host, slaveid);
break;
}
case MSG_NEED_SUBURL_DATA: {
emit needSubURLData();
break;
}
case MSG_AUTH_KEY: {
bool keep;
TQCString key, group;
stream >> key >> group >> keep;
kdDebug(7007) << "Got auth-key: " << key << endl
<< " group-key: " << group << endl
<< " keep password: " << keep << endl;
emit authorizationKey( key, group, keep );
break;
}
case MSG_DEL_AUTH_KEY: {
TQCString key;
stream >> key;
kdDebug(7007) << "Delete auth-key: " << key << endl;
emit delAuthorization( key );
}
default:
kdWarning(7007) << "Slave sends unknown command (" << _cmd << "), dropping slave" << endl;
return false;
}
return true;
}
void SlaveInterface::setOffset( KIO::filesize_t o)
{
d->offset = o;
}
KIO::filesize_t SlaveInterface::offset() const { return d->offset; }
void SlaveInterface::requestNetwork(const TQString &host, const TQString &slaveid)
{
kdDebug(7007) << "requestNetwork " << host << slaveid << endl;
TQByteArray packedArgs;
TQDataStream stream( packedArgs, IO_WriteOnly );
stream << true;
m_pConnection->sendnow( INF_NETWORK_STATUS, packedArgs );
}
void SlaveInterface::dropNetwork(const TQString &host, const TQString &slaveid)
{
kdDebug(7007) << "dropNetwork " << host << slaveid << endl;
}
void SlaveInterface::sendResumeAnswer( bool resume )
{
kdDebug(7007) << "SlaveInterface::sendResumeAnswer ok for resuming :" << resume << endl;
m_pConnection->sendnow( resume ? CMD_RESUMEANSWER : CMD_NONE, TQByteArray() );
}
void SlaveInterface::openPassDlg( const TQString& prompt, const TQString& user, bool readOnly )
{
AuthInfo info;
info.prompt = prompt;
info.username = user;
info.readOnly = readOnly;
openPassDlg( info );
}
void SlaveInterface::openPassDlg( const TQString& prompt, const TQString& user,
const TQString& caption, const TQString& comment,
const TQString& label, bool readOnly )
{
AuthInfo info;
info.prompt = prompt;
info.username = user;
info.caption = caption;
info.comment = comment;
info.commentLabel = label;
info.readOnly = readOnly;
openPassDlg( info );
}
void SlaveInterface::openPassDlg( AuthInfo& info )
{
kdDebug(7007) << "SlaveInterface::openPassDlg: "
<< "User= " << info.username
<< ", Message= " << info.prompt << endl;
bool result = Observer::self()->openPassDlg( info );
if ( m_pConnection )
{
TQByteArray data;
TQDataStream stream( data, IO_WriteOnly );
if ( result )
{
stream << info;
kdDebug(7007) << "SlaveInterface:::openPassDlg got: "
<< "User= " << info.username
<< ", Password= [hidden]" << endl;
m_pConnection->sendnow( CMD_USERPASS, data );
}
else
m_pConnection->sendnow( CMD_NONE, data );
}
}
void SlaveInterface::messageBox( int type, const TQString &text, const TQString &_caption,
const TQString &buttonYes, const TQString &buttonNo )
{
messageBox( type, text, _caption, buttonYes, buttonNo, TQString::null );
}
void SlaveInterface::messageBox( int type, const TQString &text, const TQString &_caption,
const TQString &buttonYes, const TQString &buttonNo, const TQString &dontAskAgainName )
{
kdDebug(7007) << "messageBox " << type << " " << text << " - " << _caption << " " << dontAskAgainName << endl;
TQByteArray packedArgs;
TQDataStream stream( packedArgs, IO_WriteOnly );
TQString caption( _caption );
if ( type == KIO::SlaveBase::SSLMessageBox )
caption = TQString::fromUtf8(kapp->dcopClient()->appId()); // hack, see observer.cpp
emit needProgressId();
kdDebug(7007) << "SlaveInterface::messageBox m_progressId=" << m_progressId << endl;
TQGuardedPtr<SlaveInterface> me = this;
m_pConnection->suspend();
int result = Observer::/*self()->*/messageBox( m_progressId, type, text, caption, buttonYes, buttonNo, dontAskAgainName );
if ( me && m_pConnection ) // Don't do anything if deleted meanwhile
{
m_pConnection->resume();
kdDebug(7007) << this << " SlaveInterface result=" << result << endl;
stream << result;
m_pConnection->sendnow( CMD_MESSAGEBOXANSWER, packedArgs );
}
}
// No longer used.
// Remove in KDE 4.0
void SlaveInterface::sigpipe_handler(int)
{
int saved_errno = errno;
// Using kdDebug from a signal handler is not a good idea.
#ifndef NDEBUG
char msg[1000];
sprintf(msg, "*** SIGPIPE *** (ignored, pid = %ld)\n", (long) getpid());
write(2, msg, strlen(msg));
#endif
// Do nothing.
// dispatch will return false and that will trigger ERR_SLAVE_DIED in slave.cpp
errno = saved_errno;
}
void SlaveInterface::virtual_hook( int, void* )
{ /*BASE::virtual_hook( id, data );*/ }
#include "slaveinterface.moc"
|