19 #include "kio/slaveinterface.h"
20 #include "kio/slavebase.h"
21 #include "kio/connection.h"
29 #include <kio/observer.h>
30 #include <kapplication.h>
31 #include <dcopclient.h>
38 TQDataStream &operator <<(TQDataStream &s,
const KIO::UDSEntry &e )
47 KIO::UDSEntry::ConstIterator it = e.begin();
48 for( ; it != e.end(); ++it )
56 for( ; it != e.end(); ++it )
61 a.m_uds = KIO::UDS_SIZE_LARGE;
62 a.m_long = (*it).m_long >> 32;
82 for(TQ_UINT32 i = 0; i < size; i++)
86 if (a.m_uds == KIO::UDS_SIZE_LARGE)
95 a.m_long += (TQ_LLONG) 1 << 32;
96 a.m_long += msb << 32;
105 static const unsigned int max_nums = 8;
107 class KIO::SlaveInterfacePrivate
110 SlaveInterfacePrivate() {
111 slave_calcs_speed =
false;
112 start_time.tv_sec = 0;
113 start_time.tv_usec = 0;
119 bool slave_calcs_speed;
120 struct timeval start_time;
122 long times[max_nums];
132 SlaveInterface::SlaveInterface(
Connection * connection )
134 m_pConnection = connection;
137 d =
new SlaveInterfacePrivate;
138 connect(&d->speed_timer, TQT_SIGNAL(timeout()), TQT_SLOT(calcSpeed()));
141 SlaveInterface::~SlaveInterface()
163 bool SlaveInterface::dispatch()
165 assert( m_pConnection );
170 if (m_pConnection->
read( &cmd, data ) == -1)
173 return dispatch( cmd, data );
176 void SlaveInterface::calcSpeed()
178 if (d->slave_calcs_speed) {
179 d->speed_timer.stop();
184 gettimeofday(&tv, 0);
186 long diff = ((tv.tv_sec - d->start_time.tv_sec) * 1000000 +
187 tv.tv_usec - d->start_time.tv_usec) / 1000;
188 if (diff - d->last_time >= 900) {
190 if (d->nums == max_nums) {
193 for (
unsigned int i = 1; i < max_nums; ++i) {
194 d->times[i-1] = d->times[i];
195 d->sizes[i-1] = d->sizes[i];
199 d->times[d->nums] = diff;
200 d->sizes[d->nums++] = d->filesize - d->offset;
202 KIO::filesize_t lspeed = 1000 * (d->sizes[d->nums-1] - d->sizes[0]) / (d->times[d->nums-1] - d->times[0]);
215 d->sizes[0] = d->filesize - d->offset;
221 bool SlaveInterface::dispatch(
int _cmd,
const TQByteArray &rawdata )
225 TQDataStream stream( rawdata, IO_ReadOnly );
234 emit data( rawdata );
242 d->speed_timer.stop();
249 emit statEntry(entry);
252 case MSG_LIST_ENTRIES:
259 for (uint i = 0; i < count; i++) {
263 emit listEntries(list);
269 d->offset = readFilesize_t(stream);
270 emit canResume( d->offset );
274 d->filesize = d->offset;
279 kdDebug(7007) <<
"error " << i <<
" " << str1 << endl;
280 emit error( i, str1 );
282 case MSG_SLAVE_STATUS:
286 stream >> pid >> protocol >> str1 >> b;
287 emit slaveStatus(pid, protocol, str1, (b != 0));
297 gettimeofday(&d->start_time, 0);
299 d->filesize = d->offset;
300 d->sizes[0] = d->filesize - d->offset;
303 d->speed_timer.start(1000);
304 d->slave_calcs_speed =
false;
305 emit totalSize( size );
308 case INF_PROCESSED_SIZE:
311 emit processedSize( size );
317 d->slave_calcs_speed =
true;
318 d->speed_timer.stop();
322 case INF_GETTING_FILE:
327 case INF_REDIRECTION:
332 emit redirection( url );
338 emit mimeType( str1 );
340 m_pConnection->
sendnow( CMD_NONE, TQByteArray() );
345 emit warning( str1 );
347 case INF_NEED_PASSWD: {
353 case INF_MESSAGEBOX: {
354 kdDebug(7007) <<
"needs a msg box" << endl;
355 TQString text, caption, buttonYes, buttonNo, dontAskAgainName;
357 stream >> type >> text >> caption >> buttonYes >> buttonNo;
359 messageBox(type, text, caption, buttonYes, buttonNo);
361 stream >> dontAskAgainName;
362 messageBox(type, text, caption, buttonYes, buttonNo, dontAskAgainName);
366 case INF_INFOMESSAGE: {
372 case INF_META_DATA: {
378 case MSG_NET_REQUEST: {
381 stream >> host >> slaveid;
382 requestNetwork(host, slaveid);
388 stream >> host >> slaveid;
389 dropNetwork(host, slaveid);
392 case MSG_NEED_SUBURL_DATA: {
393 emit needSubURLData();
398 TQCString key, group;
399 stream >> key >> group >> keep;
400 kdDebug(7007) <<
"Got auth-key: " << key << endl
401 <<
" group-key: " << group << endl
402 <<
" keep password: " << keep << endl;
406 case MSG_DEL_AUTH_KEY: {
409 kdDebug(7007) <<
"Delete auth-key: " << key << endl;
413 kdWarning(7007) <<
"Slave sends unknown command (" << _cmd <<
"), dropping slave" << endl;
426 void SlaveInterface::requestNetwork(
const TQString &host,
const TQString &slaveid)
428 kdDebug(7007) <<
"requestNetwork " << host << slaveid << endl;
429 TQByteArray packedArgs;
430 TQDataStream stream( packedArgs, IO_WriteOnly );
432 m_pConnection->
sendnow( INF_NETWORK_STATUS, packedArgs );
435 void SlaveInterface::dropNetwork(
const TQString &host,
const TQString &slaveid)
437 kdDebug(7007) <<
"dropNetwork " << host << slaveid << endl;
442 kdDebug(7007) <<
"SlaveInterface::sendResumeAnswer ok for resuming :" << resume << endl;
443 m_pConnection->
sendnow( resume ? CMD_RESUMEANSWER : CMD_NONE, TQByteArray() );
456 const TQString& caption,
const TQString& comment,
457 const TQString& label,
bool readOnly )
471 kdDebug(7007) <<
"SlaveInterface::openPassDlg: "
473 <<
", Message= " << info.
prompt << endl;
478 TQDataStream stream( data, IO_WriteOnly );
482 kdDebug(7007) <<
"SlaveInterface:::openPassDlg got: "
484 <<
", Password= [hidden]" << endl;
485 m_pConnection->
sendnow( CMD_USERPASS, data );
488 m_pConnection->
sendnow( CMD_NONE, data );
492 void SlaveInterface::messageBox(
int type,
const TQString &text,
const TQString &_caption,
493 const TQString &buttonYes,
const TQString &buttonNo )
495 messageBox( type, text, _caption, buttonYes, buttonNo, TQString::null );
498 void SlaveInterface::messageBox(
int type,
const TQString &text,
const TQString &_caption,
499 const TQString &buttonYes,
const TQString &buttonNo,
const TQString &dontAskAgainName )
501 kdDebug(7007) <<
"messageBox " << type <<
" " << text <<
" - " << _caption <<
" " << dontAskAgainName << endl;
502 TQByteArray packedArgs;
503 TQDataStream stream( packedArgs, IO_WriteOnly );
505 TQString caption( _caption );
506 if ( type == KIO::SlaveBase::SSLMessageBox )
507 caption = TQString::fromUtf8(kapp->dcopClient()->appId());
509 emit needProgressId();
510 kdDebug(7007) <<
"SlaveInterface::messageBox m_progressId=" << m_progressId << endl;
511 TQGuardedPtr<SlaveInterface> me =
this;
513 int result =
Observer::messageBox( m_progressId, type, text, caption, buttonYes, buttonNo, dontAskAgainName );
514 if ( me && m_pConnection )
517 kdDebug(7007) <<
this <<
" SlaveInterface result=" << result << endl;
519 m_pConnection->
sendnow( CMD_MESSAGEBOXANSWER, packedArgs );
525 void SlaveInterface::sigpipe_handler(
int)
527 int saved_errno = errno;
531 sprintf(msg,
"*** SIGPIPE *** (ignored, pid = %ld)\n", (
long) getpid());
532 if (write(2, msg, strlen(msg)) < 0) {
544 void SlaveInterface::virtual_hook(
int,
void* )
547 #include "slaveinterface.moc"