kio Library API Documentation

dataslave.cpp

00001 /*
00002  *  This file is part of the KDE libraries
00003  *  Copyright (c) 2003 Leo Savernik <l.savernik@aon.at>
00004  *  Derived from slave.cpp
00005  *
00006  *  This library is free software; you can redistribute it and/or
00007  *  modify it under the terms of the GNU Library General Public
00008  *  License version 2 as published by the Free Software Foundation.
00009  *
00010  *  This library is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  *  Library General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU Library General Public License
00016  *  along with this library; see the file COPYING.LIB.  If not, write to
00017  *  the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00018  *  Boston, MA 02111-1307, USA.
00019  **/
00020 
00021 #include <config.h>
00022 
00023 #include "dataslave.h"
00024 #include "dataprotocol.h"
00025 
00026 #include <klocale.h>
00027 #include <kdebug.h>
00028 
00029 #include <qtimer.h>
00030 
00031 using namespace KIO;
00032 
00033 #define KIO_DATA_POLL_INTERVAL 0
00034 
00035 // don't forget to sync DISPATCH_DECL in dataslave.h
00036 #define DISPATCH_IMPL(type) \
00037     void DataSlave::dispatch_##type() { \
00038       if (_suspended) { \
00039         QueueStruct q(Queue_##type); \
00040         dispatchQueue.push_back(q); \
00041         if (!timer->isActive()) timer->start(KIO_DATA_POLL_INTERVAL); \
00042       } else \
00043         type(); \
00044     }
00045 
00046 // don't forget to sync DISPATCH_DECL1 in dataslave.h
00047 #define DISPATCH_IMPL1(type, paramtype, paramname) \
00048     void DataSlave::dispatch_##type(paramtype paramname) { \
00049       if (_suspended) { \
00050         QueueStruct q(Queue_##type); \
00051         q.paramname = paramname; \
00052         dispatchQueue.push_back(q); \
00053         if (!timer->isActive()) timer->start(KIO_DATA_POLL_INTERVAL); \
00054       } else \
00055         type(paramname); \
00056     }
00057 
00058 
00059 DataSlave::DataSlave() :
00060     Slave(true, 0, "data", QString::null)
00061 {
00062   _suspended = false;
00063   timer = new QTimer(this);
00064   connect(timer, SIGNAL(timeout()), SLOT(dispatchNext()));
00065 }
00066 
00067 DataSlave::~DataSlave() {
00068 }
00069 
00070 void DataSlave::hold(const KURL &/*url*/) {
00071   // ignored
00072 }
00073 
00074 void DataSlave::suspend() {
00075   _suspended = true;
00076   //kdDebug() << this << k_funcinfo << endl;
00077   timer->stop();
00078 }
00079 
00080 void DataSlave::resume() {
00081   _suspended = false;
00082   //kdDebug() << this << k_funcinfo << endl;
00083   // aarrrgh! This makes the once hyper fast and efficient data protocol
00084   // implementation slow as molasses. But it wouldn't work otherwise,
00085   // and I don't want to start messing around with threads
00086   timer->start(KIO_DATA_POLL_INTERVAL);
00087 }
00088 
00089 void DataSlave::dispatchNext() {
00090   if (dispatchQueue.empty()) {
00091     timer->stop();
00092     return;
00093   }
00094 
00095   const QueueStruct &q = dispatchQueue.front();
00096   //kdDebug() << this << k_funcinfo << "dispatching " << q.type << " " << dispatchQueue.size() << " left" << endl;
00097   switch (q.type) {
00098     case Queue_mimeType:    mimeType(q.s); break;
00099     case Queue_totalSize:   totalSize(q.size); break;
00100     case Queue_sendMetaData:    sendMetaData(); break;
00101     case Queue_data:        data(q.ba); break;
00102     case Queue_finished:    finished(); break;
00103   }/*end switch*/
00104 
00105   dispatchQueue.pop_front();
00106 }
00107 
00108 void DataSlave::send(int cmd, const QByteArray &arr) {
00109   QDataStream stream(arr, IO_ReadOnly);
00110 
00111   KURL url;
00112 
00113   switch (cmd) {
00114     case CMD_GET: {
00115       stream >> url;
00116       get(url);
00117       break;
00118     }
00119     case CMD_MIMETYPE: {
00120       stream >> url;
00121       mimetype(url);
00122       break;
00123     }
00124     // ignore these (must not emit error, otherwise SIGSEGV occurs)
00125     case CMD_META_DATA:
00126     case CMD_SUBURL:
00127       break;
00128     default:
00129       error(ERR_UNSUPPORTED_ACTION,
00130         unsupportedActionErrorString(QString::fromLatin1("data"),cmd));
00131   }/*end switch*/
00132 }
00133 
00134 bool DataSlave::suspended() {
00135   return _suspended;
00136 }
00137 
00138 void DataSlave::setHost(const QString &/*host*/, int /*port*/,
00139                      const QString &/*user*/, const QString &/*passwd*/) {
00140   // irrelevant -> will be ignored
00141 }
00142 
00143 void DataSlave::setConfig(const MetaData &/*config*/) {
00144   // FIXME: decide to handle this directly or not at all
00145 #if 0
00146     QByteArray data;
00147     QDataStream stream( data, IO_WriteOnly );
00148     stream << config;
00149     slaveconn.send( CMD_CONFIG, data );
00150 #endif
00151 }
00152 
00153 void DataSlave::setAllMetaData(const MetaData &md) {
00154   meta_data = md;
00155 }
00156 
00157 void DataSlave::sendMetaData() {
00158   emit metaData(meta_data);
00159 }
00160 
00161 void DataSlave::virtual_hook( int id, void* data ) {
00162   switch (id) {
00163     case VIRTUAL_SUSPEND: suspend(); return;
00164     case VIRTUAL_RESUME: resume(); return;
00165     case VIRTUAL_SEND: {
00166       SendParams *params = reinterpret_cast<SendParams *>(data);
00167       send(params->cmd, *params->arr);
00168       return;
00169     }
00170     case VIRTUAL_HOLD: {
00171       HoldParams *params = reinterpret_cast<HoldParams *>(data);
00172       hold(*params->url);
00173       return;
00174     }
00175     case VIRTUAL_SUSPENDED: {
00176       SuspendedParams *params = reinterpret_cast<SuspendedParams *>(data);
00177       params->retval = suspended();
00178       return;
00179     }
00180     case VIRTUAL_SET_HOST: {
00181       SetHostParams *params = reinterpret_cast<SetHostParams *>(data);
00182       setHost(*params->host,params->port,*params->user,*params->passwd);
00183       return;
00184     }
00185     case VIRTUAL_SET_CONFIG: {
00186       SetConfigParams *params = reinterpret_cast<SetConfigParams *>(data);
00187       setConfig(*params->config);
00188       return;
00189     }
00190     default:
00191       KIO::Slave::virtual_hook( id, data );
00192   }
00193 }
00194 
00195 DISPATCH_IMPL1(mimeType, const QString &, s)
00196 DISPATCH_IMPL1(totalSize, KIO::filesize_t, size)
00197 DISPATCH_IMPL(sendMetaData)
00198 DISPATCH_IMPL1(data, const QByteArray &, ba)
00199 DISPATCH_IMPL(finished)
00200 
00201 #undef DISPATCH_IMPL
00202 #undef DISPATCH_IMPL1
00203 
00204 #include "dataslave.moc"
KDE Logo
This file is part of the documentation for kio Library Version 3.3.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sun Jan 15 13:33:19 2006 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003