kio Library API Documentation

slaveinterface.cpp

00001 /* This file is part of the KDE libraries
00002    Copyright (C) 2000 David Faure <faure@kde.org>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License version 2 as published by the Free Software Foundation.
00007 
00008    This library is distributed in the hope that it will be useful,
00009    but WITHOUT ANY WARRANTY; without even the implied warranty of
00010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011    Library General Public License for more details.
00012 
00013    You should have received a copy of the GNU Library General Public License
00014    along with this library; see the file COPYING.LIB.  If not, write to
00015    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00016    Boston, MA 02111-1307, USA.
00017 */
00018 
00019 #include "kio/slaveinterface.h"
00020 #include "kio/slavebase.h"
00021 #include "kio/connection.h"
00022 #include <errno.h>
00023 #include <assert.h>
00024 #include <kdebug.h>
00025 #include <stdlib.h>
00026 #include <sys/time.h>
00027 #include <unistd.h>
00028 #include <signal.h>
00029 #include <kio/observer.h>
00030 #include <kapplication.h>
00031 #include <dcopclient.h>
00032 #include <time.h>
00033 #include <qtimer.h>
00034 
00035 using namespace KIO;
00036 
00037 
00038 QDataStream &operator <<(QDataStream &s, const KIO::UDSEntry &e )
00039 {
00040     // On 32-bit platforms we send UDS_SIZE with UDS_SIZE_LARGE in front
00041     // of it to carry the 32 msb. We can't send a 64 bit UDS_SIZE because
00042     // that would break the compatibility of the wire-protocol with KDE 2.
00043     // On 64-bit platforms nothing has changed.
00044     if (sizeof(long) == 8)
00045     {
00046         s << (Q_UINT32)e.size();
00047         KIO::UDSEntry::ConstIterator it = e.begin();
00048         for( ; it != e.end(); ++it )
00049            s << *it;
00050         return s;
00051     }
00052 
00053     Q_UINT32 size = 0;
00054     KIO::UDSEntry::ConstIterator it = e.begin();
00055     for( ; it != e.end(); ++it )
00056     {
00057        size++;
00058        if ((*it).m_uds == KIO::UDS_SIZE)
00059           size++;
00060     }
00061     s << size;
00062     it = e.begin();
00063     for( ; it != e.end(); ++it )
00064     {
00065        if ((*it).m_uds == KIO::UDS_SIZE)
00066        {
00067           KIO::UDSAtom a;
00068           a.m_uds = KIO::UDS_SIZE_LARGE;
00069           a.m_long = (*it).m_long >> 32;
00070           s << a;
00071        }
00072        s << *it;
00073     }
00074     return s;
00075 }
00076 
00077 QDataStream &operator >>(QDataStream &s, KIO::UDSEntry &e )
00078 {
00079     e.clear();
00080     Q_UINT32 size;
00081     s >> size;
00082 
00083     // On 32-bit platforms we send UDS_SIZE with UDS_SIZE_LARGE in front
00084     // of it to carry the 32 msb. We can't send a 64 bit UDS_SIZE because
00085     // that would break the compatibility of the wire-protocol with KDE 2.
00086     // On 64-bit platforms nothing has changed.
00087     if (sizeof(long) == 8)
00088     {
00089        for(Q_UINT32 i = 0; i < size; i++)
00090        {
00091           KIO::UDSAtom a;
00092           s >> a;
00093           e.append(a);
00094        }
00095     }
00096     else
00097     {
00098        long long msb = 0;
00099        for(Q_UINT32 i = 0; i < size; i++)
00100        {
00101           KIO::UDSAtom a;
00102           s >> a;
00103           if (a.m_uds == KIO::UDS_SIZE_LARGE)
00104           {
00105              msb = a.m_long;
00106           }
00107           else
00108           {
00109              if (a.m_uds == KIO::UDS_SIZE)
00110              {
00111                 if (a.m_long < 0)
00112                    a.m_long += (long long) 1 << 32;
00113                 a.m_long += msb << 32;
00114              }
00115              e.append(a);
00116              msb = 0;
00117           }
00118        }
00119     }
00120     return s;
00121 }
00122 
00123 static const unsigned int max_nums = 8;
00124 
00125 class KIO::SlaveInterfacePrivate
00126 {
00127 public:
00128   SlaveInterfacePrivate() {
00129     slave_calcs_speed = false;
00130     start_time.tv_sec = 0;
00131     start_time.tv_usec = 0;
00132     last_time = 0;
00133     nums = 0;
00134     filesize = 0;
00135     offset = 0;
00136   }
00137   bool slave_calcs_speed;
00138   struct timeval start_time;
00139   uint nums;
00140   long times[max_nums];
00141   KIO::filesize_t sizes[max_nums];
00142   size_t last_time;
00143   KIO::filesize_t filesize, offset;
00144 
00145   QTimer speed_timer;
00146 };
00147 
00149 
00150 SlaveInterface::SlaveInterface( Connection * connection )
00151 {
00152     m_pConnection = connection;
00153     m_progressId = 0;
00154     signal( SIGPIPE, sigpipe_handler );
00155 
00156     d = new SlaveInterfacePrivate;
00157     connect(&d->speed_timer, SIGNAL(timeout()), SLOT(calcSpeed()));
00158 }
00159 
00160 SlaveInterface::~SlaveInterface()
00161 {
00162     // Note: no kdDebug() here (scheduler is deleted very late)
00163     m_pConnection = 0; // a bit like the "wasDeleted" of QObject...
00164 
00165     delete d;
00166 }
00167 
00168 static KIO::filesize_t readFilesize_t(QDataStream &stream)
00169 {
00170    KIO::filesize_t result;
00171    unsigned long ul;
00172    stream >> ul;
00173    result = ul;
00174    if (stream.atEnd())
00175       return result;
00176    stream >> ul;
00177    result += ((KIO::filesize_t)ul) << 32;
00178    return result;
00179 }
00180 
00181 
00182 bool SlaveInterface::dispatch()
00183 {
00184     assert( m_pConnection );
00185 
00186     int cmd;
00187     QByteArray data;
00188 
00189     if (m_pConnection->read( &cmd, data ) == -1)
00190       return false;
00191 
00192     return dispatch( cmd, data );
00193 }
00194 
00195 void SlaveInterface::calcSpeed()
00196 {
00197   if (d->slave_calcs_speed) {
00198     d->speed_timer.stop();
00199     return;
00200   }
00201 
00202   struct timeval tv;
00203   gettimeofday(&tv, 0);
00204 
00205   long diff = ((tv.tv_sec - d->start_time.tv_sec) * 1000000 +
00206            tv.tv_usec - d->start_time.tv_usec) / 1000;
00207   if (diff - d->last_time >= 900) {
00208     d->last_time = diff;
00209     if (d->nums == max_nums) {
00210       // let's hope gcc can optimize that well enough
00211       // otherwise I'd try memcpy :)
00212       for (unsigned int i = 1; i < max_nums; ++i) {
00213     d->times[i-1] = d->times[i];
00214     d->sizes[i-1] = d->sizes[i];
00215       }
00216       d->nums--;
00217     }
00218     d->times[d->nums] = diff;
00219     d->sizes[d->nums++] = d->filesize;
00220 
00221     KIO::filesize_t lspeed = 1000 * (d->sizes[d->nums-1] - d->sizes[0]) / (d->times[d->nums-1] - d->times[0]);
00222 
00223     // 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 ;
00224 
00225     if (!lspeed) {
00226       d->nums = 1;
00227       d->times[0] = diff;
00228       d->sizes[0] = d->filesize;
00229     }
00230     emit speed(lspeed);
00231   }
00232 }
00233 
00234 bool SlaveInterface::dispatch( int _cmd, const QByteArray &rawdata )
00235 {
00236     //kdDebug(7007) << "dispatch " << _cmd << endl;
00237 
00238     QDataStream stream( rawdata, IO_ReadOnly );
00239 
00240     QString str1;
00241     int i;
00242     Q_INT8 b;
00243     unsigned long ul;
00244 
00245     switch( _cmd ) {
00246     case MSG_DATA:
00247     emit data( rawdata );
00248     break;
00249     case MSG_DATA_REQ:
00250         emit dataReq();
00251     break;
00252     case MSG_FINISHED:
00253     //kdDebug(7007) << "Finished [this = " << this << "]" << endl;
00254         d->offset = 0;
00255         d->speed_timer.stop();
00256     emit finished();
00257     break;
00258     case MSG_STAT_ENTRY:
00259     {
00260         UDSEntry entry;
00261         stream >> entry;
00262         emit statEntry(entry);
00263     }
00264     break;
00265     case MSG_LIST_ENTRIES:
00266     {
00267         uint count;
00268         stream >> count;
00269 
00270         UDSEntryList list;
00271         UDSEntry entry;
00272         for (uint i = 0; i < count; i++) {
00273         stream >> entry;
00274         list.append(entry);
00275         }
00276         emit listEntries(list);
00277 
00278     }
00279     break;
00280     case MSG_RESUME: // From the put job
00281     {
00282         KIO::filesize_t offset = readFilesize_t(stream);
00283         emit canResume( offset );
00284     }
00285     break;
00286     case MSG_CANRESUME: // From the get job
00287         d->filesize = d->offset;
00288         emit canResume(0); // the arg doesn't matter
00289         break;
00290     case MSG_ERROR:
00291     stream >> i >> str1;
00292     kdDebug(7007) << "error " << i << " " << str1 << endl;
00293     emit error( i, str1 );
00294     break;
00295     case MSG_SLAVE_STATUS:
00296         {
00297            pid_t pid;
00298            QCString protocol;
00299            stream >> pid >> protocol >> str1 >> b;
00300            emit slaveStatus(pid, protocol, str1, (b != 0));
00301         }
00302         break;
00303     case MSG_CONNECTED:
00304     emit connected();
00305     break;
00306 
00307     case INF_TOTAL_SIZE:
00308     {
00309         KIO::filesize_t size = readFilesize_t(stream);
00310         gettimeofday(&d->start_time, 0);
00311         d->last_time = 0;
00312         d->filesize = d->offset;
00313         d->sizes[0] = d->filesize;
00314         d->times[0] = 0;
00315         d->nums = 1;
00316         d->speed_timer.start(1000);
00317         d->slave_calcs_speed = false;
00318         emit totalSize( size );
00319     }
00320     break;
00321     case INF_PROCESSED_SIZE:
00322     {
00323         KIO::filesize_t size = readFilesize_t(stream);
00324         emit processedSize( size );
00325         d->filesize = size;
00326     }
00327     break;
00328     case INF_SPEED:
00329     stream >> ul;
00330     d->slave_calcs_speed = true;
00331     d->speed_timer.stop();
00332 
00333     emit speed( ul );
00334     break;
00335     case INF_GETTING_FILE:
00336     break;
00337     case INF_ERROR_PAGE:
00338     emit errorPage();
00339     break;
00340     case INF_REDIRECTION:
00341       {
00342     KURL url;
00343     stream >> url;
00344 
00345     emit redirection( url );
00346       }
00347       break;
00348     case INF_MIME_TYPE:
00349     stream >> str1;
00350 
00351     emit mimeType( str1 );
00352         if (!m_pConnection->suspended())
00353             m_pConnection->sendnow( CMD_NONE, QByteArray() );
00354     break;
00355     case INF_WARNING:
00356     stream >> str1;
00357 
00358     emit warning( str1 );
00359     break;
00360     case INF_NEED_PASSWD: {
00361         AuthInfo info;
00362         stream >> info;
00363     openPassDlg( info );
00364     break;
00365     }
00366     case INF_MESSAGEBOX: {
00367     kdDebug(7007) << "needs a msg box" << endl;
00368     QString text, caption, buttonYes, buttonNo;
00369         int type;
00370     stream >> type >> text >> caption >> buttonYes >> buttonNo;
00371     messageBox(type, text, caption, buttonYes, buttonNo);
00372     break;
00373     }
00374     case INF_INFOMESSAGE: {
00375         QString msg;
00376         stream >> msg;
00377         infoMessage(msg);
00378         break;
00379     }
00380     case INF_META_DATA: {
00381         MetaData meta_data;
00382         stream >> meta_data;
00383         metaData(meta_data);
00384         break;
00385     }
00386     case MSG_NET_REQUEST: {
00387         QString host;
00388     QString slaveid;
00389         stream >> host >> slaveid;
00390         requestNetwork(host, slaveid);
00391         break;
00392     }
00393     case MSG_NET_DROP: {
00394         QString host;
00395     QString slaveid;
00396         stream >> host >> slaveid;
00397         dropNetwork(host, slaveid);
00398         break;
00399     }
00400     case MSG_NEED_SUBURL_DATA: {
00401         emit needSubURLData();
00402         break;
00403     }
00404     case MSG_AUTH_KEY: {
00405         bool keep;
00406         QCString key, group;
00407         stream >> key >> group >> keep;
00408         kdDebug(7007) << "Got auth-key:      " << key << endl
00409                       << "    group-key:     " << group << endl
00410                       << "    keep password: " << keep << endl;
00411         emit authorizationKey( key, group, keep );
00412         break;
00413     }
00414     case MSG_DEL_AUTH_KEY: {
00415         QCString key;
00416         stream >> key;
00417         kdDebug(7007) << "Delete auth-key: " << key << endl;
00418         emit delAuthorization( key );
00419     }
00420     default:
00421         kdWarning(7007) << "Slave sends unknown command (" << _cmd << "), dropping slave" << endl;
00422     return false;
00423     }
00424     return true;
00425 }
00426 
00427 void SlaveInterface::setOffset( KIO::filesize_t o)
00428 {
00429     d->offset = o;
00430 }
00431 
00432 KIO::filesize_t SlaveInterface::offset() const { return d->offset; }
00433 
00434 void SlaveInterface::requestNetwork(const QString &host, const QString &slaveid)
00435 {
00436     kdDebug(7007) << "requestNetwork " << host << slaveid << endl;
00437     QByteArray packedArgs;
00438     QDataStream stream( packedArgs, IO_WriteOnly );
00439     stream << true;
00440     m_pConnection->sendnow( INF_NETWORK_STATUS, packedArgs );
00441 }
00442 
00443 void SlaveInterface::dropNetwork(const QString &host, const QString &slaveid)
00444 {
00445     kdDebug(7007) << "dropNetwork " << host << slaveid << endl;
00446 }
00447 
00448 void SlaveInterface::sendResumeAnswer( bool resume )
00449 {
00450     kdDebug(7007) << "SlaveInterface::sendResumeAnswer ok for resuming :" << resume << endl;
00451     m_pConnection->sendnow( resume ? CMD_RESUMEANSWER : CMD_NONE, QByteArray() );
00452 }
00453 
00454 void SlaveInterface::openPassDlg( const QString& prompt, const QString& user, bool readOnly )
00455 {
00456     AuthInfo info;
00457     info.prompt = prompt;
00458     info.username = user;
00459     info.readOnly = readOnly;
00460     openPassDlg( info );
00461 }
00462 
00463 void SlaveInterface::openPassDlg( const QString& prompt, const QString& user,
00464                                   const QString& caption, const QString& comment,
00465                                   const QString& label, bool readOnly )
00466 {
00467     AuthInfo info;
00468     info.prompt = prompt;
00469     info.username = user;
00470     info.caption = caption;
00471     info.comment = comment;
00472     info.commentLabel = label;
00473     info.readOnly = readOnly;
00474     openPassDlg( info );
00475 }
00476 
00477 void SlaveInterface::openPassDlg( AuthInfo& info )
00478 {
00479     kdDebug(7007) << "SlaveInterface::openPassDlg: "
00480                   << "User= " << info.username
00481                   << ", Message= " << info.prompt << endl;
00482     bool result = Observer::self()->openPassDlg( info );
00483     if ( m_pConnection )
00484     {
00485         QByteArray data;
00486         QDataStream stream( data, IO_WriteOnly );
00487         if ( result )
00488         {
00489             stream << info;
00490             kdDebug(7007) << "SlaveInterface:::openPassDlg got: "
00491                           << "User= " << info.username
00492                           << ", Password= [hidden]" << endl;
00493             m_pConnection->sendnow( CMD_USERPASS, data );
00494         }
00495         else
00496             m_pConnection->sendnow( CMD_NONE, data );
00497     }
00498 }
00499 
00500 void SlaveInterface::messageBox( int type, const QString &text, const QString &_caption,
00501                                  const QString &buttonYes, const QString &buttonNo )
00502 {
00503     kdDebug(7007) << "messageBox " << type << " " << text << " - " << _caption << endl;
00504     QByteArray packedArgs;
00505     QDataStream stream( packedArgs, IO_WriteOnly );
00506 
00507     QString caption( _caption );
00508     if ( type == KIO::SlaveBase::SSLMessageBox )
00509         caption = QString::fromUtf8(kapp->dcopClient()->appId()); // hack, see observer.cpp
00510 
00511     emit needProgressId();
00512     kdDebug(7007) << "SlaveInterface::messageBox m_progressId=" << m_progressId << endl;
00513     int result = Observer::/*self()->*/messageBox( m_progressId, type, text, caption, buttonYes, buttonNo );
00514     if ( m_pConnection ) // Don't do anything if deleted meanwhile
00515     {
00516         kdDebug(7007) << this << " SlaveInterface result=" << result << endl;
00517         stream << result;
00518         m_pConnection->sendnow( CMD_MESSAGEBOXANSWER, packedArgs );
00519     }
00520 }
00521 
00522 void SlaveInterface::sigpipe_handler(int)
00523 {
00524     int saved_errno = errno;
00525     // Using kdDebug from a signal handler is not a good idea.
00526 #ifndef NDEBUG    
00527     char msg[1000];
00528     sprintf(msg, "*** SIGPIPE *** (ignored, pid = %ld)\n", (long) getpid());
00529     write(2, msg, strlen(msg));
00530 #endif    
00531 
00532     // Do nothing.
00533     // dispatch will return false and that will trigger ERR_SLAVE_DIED in slave.cpp
00534     errno = saved_errno;
00535 }
00536 
00537 void SlaveInterface::virtual_hook( int, void* )
00538 { /*BASE::virtual_hook( id, data );*/ }
00539 
00540 #include "slaveinterface.moc"
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.0.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Oct 8 12:21:33 2003 by doxygen 1.2.18 written by Dimitri van Heesch, © 1997-2001